Files
SupplierSystem/server/tests/inboundSpecConflictGate.test.ts
Ubuntu 6d408d5c04
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
feat(入库): 规格一致性闸门 —— 把规格钉在 SKU 身份上(第一层,不动抵扣)
开发者批准「可以先按照你的思路进行开发 注意别影响到其他的业务功能」。

拦的是最危险的一类:操作员选了**已有规格**的商品(如「旺仔牛奶 / 1箱24瓶」),却在本次入库里
**人工改成另一种规格**(archive.spec = 1箱12瓶)。不拦的后果是双重的:
① 12 瓶的货挂进 24 瓶那条 SKU 的批次 ⇒ 以后按 SKU 抵扣时混装、发错包装;
② 档案回写("只回写人工改过的字段")会把该 SKU 的规格**覆盖成 1箱12瓶**。

判定口径(**只认归一化后精确相等**,不做模糊匹配 —— 模糊会造成"看着接近其实不同"的静默错配):
- 归一后相等(空白/大小写/乘号 * × ✕ x/全角斜杠,复用录单的 normalizeSpecForMatch)⇒ 放行;
- 该 SKU 档案**没有**规格 ⇒ 放行(那是"补齐",回写写的就是同一个值);
- 无 SKU(新品行)/ 人工没传 archive.spec / **AI 识别值** ⇒ 一律不参与比对 ⇒ **正常路径零影响**;
- 档案有规格且归一后不同 ⇒ 抛 409 `spec_conflict`(带 spec_conflicts 明细)。

交互:服务端在**组装写入行之前**校验;前端只在「规格…不一致」这类错误上弹 confirmDialog,
确认后带 confirm_spec_conflict: true 重提交(放行),取消则不提交;**其他任何错误照旧走既有路径**。

未动:抵扣算法/FIFO、批次表结构(不加列不迁移)、档案回写逻辑、识别口径。方案丙仍未做。

证据:`server/tests/inboundSpecConflictGate.test.ts` **5 通过**(口径/闸门在写入前/路由透传/
前端确认与重提交/三语);**真实数据实测** `listSpecConflicts`(只读脚本)6 种情形 →
冲突 1 行 `久大0添加精制盐 档案=350g*60袋 / 本次=350g*30袋`,写法差异 `350G×60袋` 归一后不冲突、
档案无规格的行不冲突(补齐)、无人工规格/新品行不冲突;`tsc` 前端 exit 0(后端仅剩既有
defaults.ts variablesSchema 缺失);设计门禁 8 通过/1 红(check-ai-cost)/1 未执行 与基线一致。
设计文档《入库规格一致性闸门》+ README 已登记。
2026-10-16 18:14:30 +08:00

80 lines
4.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { readFileSync } from 'node:fs';
import { describe, expect, it } from 'vitest';
/**
* 规格一致性闸门(第一层,2026-10-16 开发者批准)。
*
* 拦的是**最危险的那一类**:选了一条已有规格的商品(「旺仔牛奶 / 1箱24瓶」),
* 却在本次入库里**人工改成另一种规格**(`archive.spec = 1箱12瓶`)。不拦的后果:
* ① 12 瓶的货挂进 24 瓶那条 SKU 的批次 ⇒ 以后按 SKU 抵扣时混装、发错包装;
* ② 档案回写会把该 SKU 的规格**覆盖成 1箱12瓶**。
*
* 口径(**只认归一化后精确相等**,不做模糊匹配 —— 模糊会"看着接近其实不同"地静默错配):
* 只比「已匹配既有 SKU」+「人工确实传了 archive.spec」的行;
* 档案规格为空 ⇒ 不算冲突(那是补齐);找不到 SKU / 没人工规格 ⇒ 不算。
*
* 零误伤:AI 识别值(未人工改)不参与比对;未确认时抛 409,前端确认后带标记重提交。
*/
const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8');
const service = read('../src/services/stockInboundBatchService.ts');
const route = read('../src/modules/workflow/routes/inventoryRoutes.ts');
const api = read('../../src/utils/workflowApi.ts');
const panel = read('../../src/pages/inventory/StockInboundPanel.tsx');
describe('入库规格一致性闸门', () => {
it('① 服务端:只比「已有 SKU + 人工 archive.spec + 档案已有规格」,归一后不等才算冲突', () => {
const helper = service.slice(
service.indexOf('export async function listSpecConflicts('),
service.indexOf('export async function batchStockInboundRows(')
);
expect(helper).toContain('row.archive?.spec');
expect(helper).toContain("if (!stored) continue; // 档案没规格 ⇒ 这是补齐,不是冲突");
expect(helper).toContain('if (stored === t.rowSpec) continue;');
// 归一复用录单那一份(防两处口径漂移)
expect(helper).toContain('normalizeSpecForMatch');
expect(service).toContain("import { normalizeSpecForMatch } from './orderParseService';");
// 未命中的行不产生冲突(码/档案都没有的不算)
expect(helper).toContain('const hit = byCode.get(t.skuCode);');
expect(helper).toContain('if (!hit) continue;');
});
it('② 闸门只在未人工确认时拦:抛 409 spec_conflict 且带冲突明细', () => {
const fn = service.slice(
service.indexOf('export async function batchStockInboundRows('),
service.indexOf('const bulkRows: StockImportRowInput[] = [];')
);
expect(fn).toContain('if (input.confirmSpecConflict !== true) {');
expect(fn).toContain("status: 409, code: 'spec_conflict'");
expect(fn).toContain('spec_conflicts: conflicts');
// 闸门在写入之前(bulkRows 组装与事务之前)
expect(service.indexOf('if (input.confirmSpecConflict !== true) {')).toBeLessThan(
service.indexOf('const bulkRows: StockImportRowInput[] = [];')
);
});
it('③ 路由透传确认位并把冲突明细返回前端', () => {
expect(route).toContain('confirmSpecConflict: req.body?.confirm_spec_conflict === true');
expect(route).toContain('spec_conflicts: error?.spec_conflicts');
});
it('④ 前端:确认位进请求体;409 时弹确认,确认后带标记重提交,取消则不提交', () => {
expect(api).toContain('confirmSpecConflict?: boolean;');
expect(api).toContain('confirm_spec_conflict: input.confirmSpecConflict === true');
expect(panel).toContain("import { confirmDialog } from '../../utils/confirmDialog';");
expect(panel).toContain("t('inventory.stockIn.specConflictTitle')");
expect(panel).toContain("t('inventory.stockIn.specConflictOk')");
// 只有"规格…不一致"这类错误才触发弹窗,其他错误原样抛出(不影响其他失败路径)
expect(panel).toContain("if (!/规格/.test(message) || !/不一致/.test(message)) throw conflictError;");
expect(panel).toContain('confirmSpecConflict: true,');
expect(panel).toContain('setSubmitting(false);\n return;');
});
it('⑤ 三语文案齐备', () => {
for (const locale of ['zh-CN', 'en', 'id']) {
const dict = read(`../../src/i18n/locales/${locale}/inventory.ts`);
expect(dict, `${locale} 缺 specConflictTitle`).toContain("'stockIn.specConflictTitle'");
expect(dict, `${locale} 缺 specConflictOk`).toContain("'stockIn.specConflictOk'");
}
});
});