Files
SupplierSystem/server/tests/mainSystemReturnFollow.test.ts
T
Ubuntu aa2db57b0f
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(main-system): 免入库单取货链路 + 实收数量「事实登记 + 事后同步」
免入库(主系统骑手上门取货):
- 50 展示「待取货」、副文案「待主系统骑手上门取货」、不建我方配送任务(守卫与开关解耦)
- 我方 50 即回写备货完成;两开关(SKIP_INBOUND_AWAIT_PICKUP / FULFILLMENT_EVENTS)改默认开

核货事实镜像(「核货完毕」标记):
- 按主系统 lookup 的 actual_qty 落台账,新增 GET/POST /orders/:orderCode/main-system-prep-facts[/refresh]
- 详情行内显示「核货完毕」(含核货量),分配对账兜底刷新

实收数量/单位口径变更(对方先改 + 我方事后同步):
- 台账加 applied_at;inbound 与 picking_check 同为事实登记(驳回不回滚)
- 同意才落我方值:免入库改装车核货量、送仓改验收实收(已入库走既有库存调整)、已结算/已结案只留痕
- 计划量与金额一起改,并同步结算投影(syncStatementsForOrderSafe)
- 审核页语义改「主系统实际值(已生效)/ 是否同步」+ 已处理历史(双方时间与落值结论)

台账对照与回写修复:
- 行级对照改整单一次性(同名商品不再重复认领)+ 存量纠正 healOrderPrepMapping
- 堵住客户绑定回填旁路(免入库单不再被派我方骑手)
- 回写触发函数改对象参数(修 branch/order 传反导致的静默不回写)

文档:设计文档 4 篇 +《给主系统 AI 的对接改造清单》§2.9(第六批契约)

回归:服务端全量与 HEAD 基线一致(24 个失败文件同集合);前端 140 文件 / 699 测试全绿;tsc 双端干净
2026-10-09 21:52:17 +08:00

144 lines
6.4 KiB
TypeScript
Raw 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 { 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(取消履约)', async () => {
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
expect(judgeByLookup({ ...base, found: false, orderItemPresent: false })).toBe('cancelled');
expect(judgeByLookup({ ...base, found: false, orderDeleted: true, orderItemPresent: false })).toBe('cancelled');
});
it('该行没出现在 lookup 结果里 → unknown(**绝不**当退货)', async () => {
const { judgeByLookup } = await import('../src/services/mainSystemReturnFollow');
expect(judgeByLookup(undefined)).toBe('unknown');
});
});