开发者口径:主系统删整单/删商品,我方按是否入库分叉 —— 未入库直接取消(删行+留痕);已入库/已在库走退货流程(红冲,含既有库存回补)。例外:我方已完成(90) + 主系统删整单 ⇒ 不动、只留痕。 改:judgeByLookup 拆分 order_deleted(整单)与 cancelled(行级);状态闸门由「>=60 一律挂起」改为「仅 整单删除 && >=90 才不动」,其余一律按入库分叉落动作;整单删除写 operation_logs(main_system_order_deleted_follow,三语标签)。 核实更正:退货红冲路径 markOrderItemReturned 已调 reverseInboundStock(order_item_return=退货回冲入库)⇒ 库存回归本来就有,无需新增库存动作。 验证:server tsc 0;mainSystem* 16 文件/224 用例全绿;全量 server 套失败集合与基线逐文件相同(零新增失败);前端 tsc/i18n 绿;注入缺陷实证闸门用例能拦;门禁四闸过。
162 lines
7.3 KiB
TypeScript
162 lines
7.3 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { mapPrepRowsToOurItems } from '../src/services/mainSystemPrepLedger';
|
||
import { voidInventoryCoverCostForItem } from '../src/services/mainSystemReturnFollow';
|
||
|
||
/**
|
||
* 第三方订单退货跟随(2026-10-08,设计文档 20261008 §3.1/§3.4)的纯逻辑回归:
|
||
* ① 备货行 ↔ 本系统订单行的对照必须**按名字 + 出现次序**确定,不允许随机/时间兜底;
|
||
* ② 库存抵扣成本行的作废必须按 `order_code + sku_code` 打(因为那类成本行没有 order_item_id),
|
||
* 且必须带 `cost_source='inventory_cover'` 以免误伤正常采购成本。
|
||
*/
|
||
|
||
describe('mapPrepRowsToOurItems(行级对照)', () => {
|
||
it('同名多行按各自出现次序配对(可复现,不用时间兜底)', () => {
|
||
const prep = [
|
||
{ id: 'p1', itemName: '土豆' },
|
||
{ id: 'p2', itemName: '白菜' },
|
||
{ id: 'p3', itemName: '土豆' },
|
||
];
|
||
const ours = [
|
||
{ id: 'i1', item_name: '土豆' },
|
||
{ id: 'i2', item_name: '白菜' },
|
||
{ id: 'i3', item_name: '土豆' },
|
||
];
|
||
const map = mapPrepRowsToOurItems(prep, ours);
|
||
expect(map.get('p1')).toBe('i1');
|
||
expect(map.get('p2')).toBe('i2');
|
||
expect(map.get('p3')).toBe('i3');
|
||
expect(map.size).toBe(3);
|
||
});
|
||
|
||
it('对不上的行留空(宁可挂待处理,不猜)', () => {
|
||
const map = mapPrepRowsToOurItems(
|
||
[{ id: 'p1', itemName: '土豆' }, { id: 'p2', itemName: '没这个品' }],
|
||
[{ id: 'i1', item_name: '土豆' }]
|
||
);
|
||
expect(map.get('p1')).toBe('i1');
|
||
expect(map.has('p2')).toBe(false);
|
||
});
|
||
|
||
it('名称两边的空白差异不影响配对', () => {
|
||
const map = mapPrepRowsToOurItems(
|
||
[{ id: 'p1', itemName: ' 土豆 ' }],
|
||
[{ id: 'i1', item_name: '土豆' }]
|
||
);
|
||
expect(map.get('p1')).toBe('i1');
|
||
});
|
||
|
||
it('我方多出来的行不会被误配', () => {
|
||
const map = mapPrepRowsToOurItems(
|
||
[{ id: 'p1', itemName: '土豆' }],
|
||
[{ id: 'i1', item_name: '土豆' }, { id: 'i2', item_name: '土豆' }]
|
||
);
|
||
expect([...map.values()]).toEqual(['i1']);
|
||
});
|
||
});
|
||
|
||
describe('voidInventoryCoverCostForItem(库存抵扣补丁)', () => {
|
||
it('按 order_code + sku_code + cost_source=inventory_cover 作废,且幂等写法(amount=0 再跑无副作用)', async () => {
|
||
const calls: Array<{ text: string; params?: unknown[] }> = [];
|
||
const db = {
|
||
query: async (text: string, params?: unknown[]) => {
|
||
calls.push({ text, params });
|
||
return { rowCount: 1 };
|
||
},
|
||
};
|
||
await voidInventoryCoverCostForItem(db, {
|
||
orderCode: 'ORD_API_X',
|
||
skuCode: 'SKU1',
|
||
remark: '测试',
|
||
});
|
||
expect(calls).toHaveLength(1);
|
||
const sql = calls[0].text.replace(/\s+/g, ' ');
|
||
expect(sql).toContain('UPDATE cost_details');
|
||
expect(sql).toContain('amount = 0');
|
||
expect(sql).toContain('allocated_amount = 0');
|
||
expect(sql).toContain("status = 20");
|
||
expect(sql).toContain('order_code = $1 AND sku_code = $2');
|
||
expect(sql).toContain("cost_source = 'inventory_cover'");
|
||
// 绝不能用 order_item_id 作匹配(那类成本行 order_item_id 为空,这正是本次要补的缺口)
|
||
expect(sql).not.toContain('order_item_id');
|
||
expect(calls[0].params).toEqual(['ORD_API_X', 'SKU1', '测试']);
|
||
});
|
||
|
||
it('没有 sku 时不动账(避免按 order 全表误伤)', async () => {
|
||
let called = 0;
|
||
const db = { query: async () => { called += 1; return { rowCount: 0 }; } };
|
||
await voidInventoryCoverCostForItem(db, { orderCode: 'ORD_API_X', skuCode: '', remark: 'x' });
|
||
await voidInventoryCoverCostForItem(db, { orderCode: 'ORD_API_X', skuCode: null, remark: 'x' });
|
||
expect(called).toBe(0);
|
||
});
|
||
});
|
||
|
||
/**
|
||
* A2(2026-10-09 主系统增量改造后):退货判定改由 `POST /preparations/lookup` 权威回答。
|
||
* 这里钉住四态判定与三条安全底线:问不到不判定、备货行还在就不动、缺 sku 仍走归档兜底。
|
||
*/
|
||
describe('judgeByLookup:四态判定', () => {
|
||
const base = {
|
||
orderCode: 'O1', skuCode: 'S1', id: 'p1', prepareStatus: null as number | null, prepareTime: null,
|
||
prepareQty: null, purchasePrice: null, unit: null, orderStatus: null, orderDeleted: false,
|
||
orderItemPresent: true, isReturned: false, returnCode: null as string | null,
|
||
currentSupplierCode: 'SUP1', reassigned: false,
|
||
};
|
||
|
||
it('主系统侧仍在办理(found=true, status<30)→ inconsistent(不处置,交人工)', async () => {
|
||
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
|
||
expect(judgeByLookup({ ...base, found: true, prepareStatus: 10 })).toBe('inconsistent');
|
||
});
|
||
|
||
it('主系统侧已完成(found=true, status>=30)→ completed(不是退货)', async () => {
|
||
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
|
||
expect(judgeByLookup({ ...base, found: true, prepareStatus: 30 })).toBe('completed');
|
||
});
|
||
|
||
it('备货行没了但订单行仍在且未退货 → completed(正常完成)', async () => {
|
||
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
|
||
expect(judgeByLookup({ ...base, found: false })).toBe('completed');
|
||
});
|
||
|
||
it('已退货红冲(is_returned / return_code)→ returned', async () => {
|
||
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
|
||
expect(judgeByLookup({ ...base, found: false, isReturned: true })).toBe('returned');
|
||
expect(judgeByLookup({ ...base, found: false, returnCode: 'OIR_X' })).toBe('returned');
|
||
});
|
||
|
||
it('已改供别家 → reassigned(单独计数,处置同退货类)', async () => {
|
||
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
|
||
expect(
|
||
judgeByLookup({ ...base, found: false, currentSupplierCode: 'SUP_OTHER', reassigned: true })
|
||
).toBe('reassigned');
|
||
});
|
||
|
||
it('商品行被删 → cancelled;主系统**整单删除** → order_deleted(2026-10-12 拆分)', async () => {
|
||
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
|
||
// 只是该商品行没了(行级)⇒ cancelled(未入库删行 / 已入库退货)
|
||
expect(judgeByLookup({ ...base, found: false, orderItemPresent: false })).toBe('cancelled');
|
||
// 主系统**整单已删**(订单级)⇒ order_deleted(再按"是否已完成"决定动不动)
|
||
expect(judgeByLookup({ ...base, found: false, orderDeleted: true, orderItemPresent: false })).toBe(
|
||
'order_deleted'
|
||
);
|
||
});
|
||
|
||
it('源码钉住:仅"整单删除 + 已完成(90)"才不动;旧的 ≥60 挂起已下线', async () => {
|
||
const { readFileSync } = await import('node:fs');
|
||
const src = readFileSync(
|
||
new URL('../src/services/mainSystemReturnFollow.ts', import.meta.url),
|
||
'utf8'
|
||
);
|
||
// 闸门:只保护"整单删除 + ≥90"
|
||
expect(src).toContain("verdict === 'order_deleted' && facts.orderStatus >= 90");
|
||
// 旧口径"≥60 一律挂起"不得残留
|
||
expect(src).not.toContain('facts.orderStatus >= 60');
|
||
// 整单删除要留订单级操作日志
|
||
expect(src).toContain('main_system_order_deleted_follow');
|
||
});
|
||
|
||
it('该行没出现在 lookup 结果里 → unknown(**绝不**当退货)', async () => {
|
||
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
|
||
expect(judgeByLookup(undefined)).toBe('unknown');
|
||
});
|
||
});
|