来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
185 lines
9.8 KiB
TypeScript
185 lines
9.8 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 「允许成本价为 0」的**一致性契约**(2026-09-24)。
|
||
*
|
||
* 背景:全仓多处读取点用 `COALESCE(NULLIF(actual_price,0), purchase_price)` 表达
|
||
* 「实采价优先,没有实采价就用下单价」(金额重算 / 倒挂判定 / 移动端与开放接口展示)。
|
||
* 开关让 0 成为合法实采价后,如果写入侧还留着下单价(或把 0 写成 NULL),
|
||
* 真实的 0 成本就会被**静默回退成下单价**。本测试钉住三条:
|
||
* ① 写入侧:实采价为 0 时必须把下单价与金额一并清零(且只在"确实写了 0"的路径上做);
|
||
* ② 「未填」与「填 0」等价(开关开启时未填按 0 落库,不再沿用下单价);
|
||
* ③ 禁止 `Number(price) || null` 这类把 0 变 NULL 的写法。
|
||
*
|
||
* ⚠️ 反面:**绝不要**把清零做成数据级规则 —— 加菜凭证(source_type='extra_dish',
|
||
* actual_price=0 表示"未填"、成本记在下单价里)与待采购行依赖那个回退。
|
||
*/
|
||
|
||
const SRC = (rel: string) => readFileSync(path.join(__dirname, '..', rel), 'utf8');
|
||
|
||
describe('清零助手 clearPlanPriceForZeroCost', () => {
|
||
const src = SRC('src/services/supplyCostPrice.ts');
|
||
|
||
it('只在该单实际价为 0 时才清零(自带守卫,幂等)', () => {
|
||
expect(src).toContain('export async function clearPlanPriceForZeroCost');
|
||
expect(src).toContain('WHERE purchase_code = $1 AND COALESCE(actual_price, 0) = 0');
|
||
expect(src).toContain('SET purchase_price = 0, total_amount = 0');
|
||
});
|
||
|
||
it('注释必须写明"不得升级为数据级规则"及其原因(保护加菜凭证与待采购行)', () => {
|
||
expect(src).toContain('绝不要升级成数据级规则');
|
||
expect(src).toContain("extra_dish");
|
||
});
|
||
});
|
||
|
||
describe('完成自采:未填=0,且清零下单价', () => {
|
||
const src = SRC('src/services/purchaseOperationService.ts');
|
||
|
||
it('开关开启时「未填」按 0 落库(不再沿用登记价)', () => {
|
||
expect(src).toContain("if (zeroCostAllowedForBranch && (actualPrice === null || Number(actualPrice) === 0))");
|
||
expect(src).toContain('if (actualPrice === null) actualPrice = 0;');
|
||
});
|
||
|
||
it('实采价为 0 时调用清零助手(含开关开启的普通自采行)', () => {
|
||
expect(src).toContain('clearPlanPriceForZeroCost');
|
||
expect(src).toContain('branchAllowsZero');
|
||
expect(src).toContain('Boolean(flagRow.rows[0]?.self_processed) || linked || branchAllowsZero');
|
||
// 旧写法(只对仓内加工行清)不得回退
|
||
expect(src).not.toContain('zeroWarehousePurchaseCost');
|
||
});
|
||
});
|
||
|
||
describe('改实采价 / 验收回写:同样清零', () => {
|
||
it('purchaseAmendService:nextPrice 为 0 时清零', () => {
|
||
const src = SRC('src/services/purchaseAmendService.ts');
|
||
expect(src).toContain('clearPlanPriceForZeroCost');
|
||
expect(src).toContain('if (Number(nextPrice) === 0)');
|
||
});
|
||
|
||
it('inspectionComplianceService:禁止 `Number(...) || null` 把 0 变 NULL', () => {
|
||
const src = SRC('src/services/inspectionComplianceService.ts');
|
||
expect(src).not.toContain('const price = Number(row.purchase_price) || null;');
|
||
expect(src).toContain('const rawPrice = row.purchase_price;');
|
||
});
|
||
|
||
it('inspectionComplianceService:供应商关单回写与自采累计回写都会清零', () => {
|
||
const src = SRC('src/services/inspectionComplianceService.ts');
|
||
const calls = src.match(/clearPlanPriceForZeroCost\(/g)?.length ?? 0;
|
||
expect(calls, '清零调用点少于 2 处(供应商关单 + 自采累计)').toBeGreaterThanOrEqual(2);
|
||
expect(src).toContain('if (price === 0) await clearPlanPriceForZeroCost(client, code);');
|
||
expect(src).toContain('if (Number(params.actualPrice) === 0)');
|
||
});
|
||
});
|
||
|
||
describe('采购改实采价 → 订单行成本回写:0 也要回写(2026-09-24 裁决:只放开这一条路径)', () => {
|
||
const src = SRC('src/services/purchaseAmendService.ts');
|
||
|
||
it('开关开启时 0 也回写订单行成本(改前 `nextPrice > 0` 把它直接拦掉)', () => {
|
||
expect(src).toContain('const allowZeroWriteback =');
|
||
expect(src).toContain('(nextPrice === 0 &&');
|
||
expect(src).toContain('if (allowZeroWriteback && !input.preserveOrderEnteredCost) {');
|
||
// 旧的一道 `> 0` 闸门不得回退
|
||
expect(src).not.toContain('if (nextPrice > 0 && !input.preserveOrderEnteredCost) {');
|
||
});
|
||
|
||
it('开关由服务端自己查(不信客户端);开关关闭时行为不变', () => {
|
||
// 同文件校验点与回写点共用同一个开关读取函数
|
||
const allowZeroCalls = src.match(/isZeroSupplyCostAllowed\(/g)?.length ?? 0;
|
||
expect(allowZeroCalls, '校验点 + 回写点共 2 处').toBeGreaterThanOrEqual(2);
|
||
});
|
||
|
||
it('仍未验收(无实收)的行不回写 —— 方案 C 的守卫不得丢', () => {
|
||
expect(src).toContain('if (beforeQty > 0 && row.order_code && row.sku_code && !input.skipOrderItemSync) {');
|
||
});
|
||
});
|
||
|
||
describe('备货改采购价 → 订单行成本(2026-09-24 裁决:不论备货状态都回写 + 0 放行)', () => {
|
||
const src = SRC('src/services/supplierPreparationStatusService.ts');
|
||
|
||
it('0 也回写订单行(开关开启时),开关关闭时沿用 `> 0` 旧行为', () => {
|
||
expect(src).toContain('async function syncPrepOrderLineUnitCost(');
|
||
expect(src).toContain('if (!(unitCostPrice > 0) && !(unitCostPrice === 0 && allowZeroCost)) return;');
|
||
});
|
||
|
||
it('未完成备货(status<30)改价也要回写订单行(原先直接 continue)', () => {
|
||
const block = src.slice(src.indexOf('if (status < 30) {'), src.indexOf('const unitChanged ='));
|
||
expect(block).toContain('await syncPrepOrderLineUnitCost(');
|
||
expect(block).toContain('allowZeroCostForBranch');
|
||
// 旧的「未完成直接跳过」不得回退
|
||
expect(src).not.toContain('// 未完成:仅落库价/单位/数量/归一列;完成后:订单结算 + 验收价/成本闭环');
|
||
});
|
||
|
||
it('已完成路径改用同一个助手(避免两处口径漂移)', () => {
|
||
expect(src).toContain('await syncPrepOrderLineUnitCost(db, prep, prep.allowZero);');
|
||
expect(src).not.toContain('if (unitCostPrice > 0 && prep.sku_code) {');
|
||
});
|
||
});
|
||
|
||
describe('订单改成本价 → 供应商备货价(2026-09-24 裁决 A:按状态收窄 + 待确认单重算 + 0 放行)', () => {
|
||
const src = SRC('src/services/orderEnteredCostWritebackService.ts');
|
||
|
||
it('「已结算」按 statement_status ≥ 20 判定,不再只看有没有结算行', () => {
|
||
expect(src).toContain('JOIN supplier_statements ss ON ss.statement_no = sl.statement_no');
|
||
expect(src).toContain('AND COALESCE(ss.statement_status, 0) >= 20');
|
||
// 旧写法:只看「有没有 supplier 结算行」(免入库行一备货完成就永久锁死)不得回退
|
||
expect(src).not.toContain(
|
||
"SELECT 1 FROM statement_lines\n WHERE order_code = $1 AND sku_code = $2 AND statement_type = 'supplier'"
|
||
);
|
||
});
|
||
|
||
it('0 按开关放行(服务端自查),开关关闭时保持「cost <= 0 不回写」', () => {
|
||
expect(src).toContain(
|
||
'const allowZeroCost = await isZeroSupplyCostAllowed(trim(input.branchCode) || null);'
|
||
);
|
||
expect(src).toContain("cost < 0 || (cost === 0 && !allowZeroCost)");
|
||
});
|
||
|
||
it('待确认(<20)结算单在改价后重算,金额与备货价保持一致', () => {
|
||
expect(src).toContain('AND COALESCE(ss.statement_status, 0) < 20');
|
||
expect(src).toContain('upsertSupplierStatementForDay(supplierCode, day, client)');
|
||
});
|
||
|
||
it('订单保存调用点不再用 `> 0` 提前把 0 挡掉(判定统一收进服务)', () => {
|
||
const manage = SRC('src/services/orderManageItemsService.ts');
|
||
expect(manage).not.toContain('if (costChanged && Number(unitCost) > 0) {');
|
||
expect(manage).toContain('if (costChanged) {');
|
||
});
|
||
|
||
it('结算投影:金额 0 照写(订单行口径,方案 B),不再「0 当缺失」门控', () => {
|
||
const fin = SRC('src/services/financeReconciliationService.ts');
|
||
// 生成器不再因金额为 0 早退(0 是合法值:订单行成本价可为 0)
|
||
expect(fin).not.toContain("if (total <= 0 && !existing.rows.length) return 'empty';");
|
||
expect(fin).not.toContain('if (!allowZero) {');
|
||
expect(fin).toContain('// 方案 B(2026-09-24):金额 0 是**合法值**');
|
||
// 桶不存在(该供应商当日已无有效订单行)时才清孤儿行
|
||
expect(fin).toContain("async function clearSupplierStatementWithoutBasis(");
|
||
expect(fin).toContain("): Promise<'empty'> {");
|
||
});
|
||
|
||
it('订单→备货回写必须同步重算归一价(否则下游按旧归一价取价)', () => {
|
||
// 下游取价口径:`normalized > 0 ? normalized : price`
|
||
expect(src).toContain('persistPrepNormalizedPrice');
|
||
expect(src).toContain(
|
||
'RETURNING id::text AS id, sku_code, unit, purchase_price, branch_code'
|
||
);
|
||
// 助手必须从备货侧导出(两路径同口径),不得在订单侧另写一份换算
|
||
const prepSvc = SRC('src/services/supplierPreparationStatusService.ts');
|
||
expect(prepSvc).toContain('export async function persistPrepNormalizedPrice(');
|
||
});
|
||
});
|
||
|
||
describe('前端:未填=0 的语义与服务端一致', () => {
|
||
it('独立入库面板:开关开启时不再拦空白(空按 0 提交)', () => {
|
||
const src = SRC('../src/pages/inventory/StockInboundPanel.tsx');
|
||
expect(src).toContain('allowZeroCost\n ? !Number.isFinite(c) || c < 0');
|
||
expect(src).not.toContain("if (!String(unitCost).trim() || !Number.isFinite(c) || c < 0 || (!allowZeroCost && c === 0))");
|
||
});
|
||
|
||
it('小程序管理端面板:注释写明空串按 0 处理', () => {
|
||
const src = SRC('../miniprogram/components/admin-preparations-panel/index.js');
|
||
expect(src).toContain('未填(空串)按 0 处理');
|
||
});
|
||
});
|