免入库(主系统骑手上门取货): - 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 双端干净
123 lines
5.1 KiB
TypeScript
123 lines
5.1 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
||
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { mapOrderPrepRowsToOurItems } from '../src/services/mainSystemPrepLedger';
|
||
|
||
/**
|
||
* 整单(跨主系统单)一对一对照(2026-10-11 修复)。
|
||
*
|
||
* 实测缺陷:一张我方订单由多张主系统单合并而来,同名商品会在多张单里各出现一次
|
||
* (`…_0006` 肉片 3.5 斤 / `…_0007` 肉片 6 斤)。旧实现**按单各算一次** ⇒ 两张单都认领同一条订单行,
|
||
* 后一张单的行永远配不上 ⇒ 详情掉进「本系统录单」、台账两条备货行指向同一订单行、回写把 3.5 写成 6。
|
||
*/
|
||
const ourItems = [
|
||
{ id: 'item-meat-6', item_name: '肉片', quantity: 6 },
|
||
{ id: 'item-meat-35', item_name: '肉片', quantity: 3.5 },
|
||
{ id: 'item-pork', item_name: '五花肉', quantity: 20 },
|
||
];
|
||
|
||
describe('整单对照:一对一、不重复认领', () => {
|
||
it('同名商品跨两张主系统单 ⇒ 按数量配对到各自的订单行(实测用例)', () => {
|
||
const mapping = mapOrderPrepRowsToOurItems(
|
||
[
|
||
{ orderCode: 'ORD_WEB_..._0006', rows: [{ id: 'prep-35', itemName: '肉片', prepareQty: 3.5 }] },
|
||
{ orderCode: 'ORD_WEB_..._0007', rows: [{ id: 'prep-6', itemName: '肉片', prepareQty: 6 }] },
|
||
],
|
||
ourItems
|
||
);
|
||
expect(mapping.get('prep-35')).toBe('item-meat-35');
|
||
expect(mapping.get('prep-6')).toBe('item-meat-6');
|
||
expect(new Set(mapping.values()).size).toBe(2); // 一对一:不得复用同一条订单行
|
||
});
|
||
|
||
it('数量缺失时退回出现次序,仍是一对一', () => {
|
||
const mapping = mapOrderPrepRowsToOurItems(
|
||
[
|
||
{ orderCode: 'A', rows: [{ id: 'p1', itemName: '肉片', prepareQty: null }] },
|
||
{ orderCode: 'B', rows: [{ id: 'p2', itemName: '肉片', prepareQty: null }] },
|
||
],
|
||
ourItems
|
||
);
|
||
expect(mapping.get('p1')).toBe('item-meat-6');
|
||
expect(mapping.get('p2')).toBe('item-meat-35');
|
||
});
|
||
|
||
it('缺货行不占位;同名行按剩余候选依次配;用完即留空(绝不复用)', () => {
|
||
const mapping = mapOrderPrepRowsToOurItems(
|
||
[
|
||
{
|
||
orderCode: 'A',
|
||
rows: [
|
||
{ id: 'stockout', itemName: '肉片', prepareQty: 6, isStockout: true },
|
||
{ id: 'real', itemName: '肉片', prepareQty: 6 },
|
||
{ id: 'second', itemName: '肉片', prepareQty: 9 },
|
||
{ id: 'third', itemName: '肉片', prepareQty: 12 },
|
||
{ id: 'unknown', itemName: '不存在的商品', prepareQty: 1 },
|
||
],
|
||
},
|
||
],
|
||
ourItems
|
||
);
|
||
expect(mapping.has('stockout')).toBe(false); // 缺货行不占匹配位
|
||
expect(mapping.get('real')).toBe('item-meat-6'); // 数量吻合优先
|
||
expect(mapping.get('second')).toBe('item-meat-35'); // 剩哪条配哪条(旧口径"出现次序")
|
||
expect(mapping.has('third')).toBe(false); // 我方同名行已用完 ⇒ 留空,不猜
|
||
expect(mapping.has('unknown')).toBe(false);
|
||
expect(new Set(mapping.values()).size).toBe(mapping.size); // 全局一对一
|
||
});
|
||
|
||
it('不同商品各走各的桶', () => {
|
||
const mapping = mapOrderPrepRowsToOurItems(
|
||
[
|
||
{
|
||
orderCode: 'A',
|
||
rows: [
|
||
{ id: 'p-meat', itemName: '肉片', prepareQty: 3.5 },
|
||
{ id: 'p-pork', itemName: '五花肉', prepareQty: 20 },
|
||
],
|
||
},
|
||
],
|
||
ourItems
|
||
);
|
||
expect(mapping.get('p-meat')).toBe('item-meat-35');
|
||
expect(mapping.get('p-pork')).toBe('item-pork');
|
||
});
|
||
});
|
||
|
||
describe('导入链路必须整单算一次对照(防回退)', () => {
|
||
const importSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystemOrderImport.ts'),
|
||
'utf8'
|
||
);
|
||
const ledgerSrc = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystemPrepLedger.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
it('persistRows 算一次整单对照,同时喂给台账与来源单号落列', () => {
|
||
const idx = importSrc.indexOf('const persistRows');
|
||
const body = importSrc.slice(idx, idx + 3000);
|
||
expect(body).toMatch(/mapOrderPrepRowsToOurItems\(/);
|
||
expect(body).toMatch(/mapping: orderMapping/);
|
||
expect(body).toMatch(/stampMainOrderCodes\(\{[^}]*mapping: orderMapping/s);
|
||
// 台账写入必须用同一份对照(不得再按单各算一次)
|
||
expect(body).not.toMatch(/recordImportedPrepItems\(\{[\s\S]{0,400}?mapPrepRowsToOurItems\(/);
|
||
});
|
||
|
||
it('台账写入优先用外部传入的对照', () => {
|
||
const idx = ledgerSrc.indexOf('export async function recordImportedPrepItems');
|
||
const body = ledgerSrc.slice(idx, idx + 1200);
|
||
expect(body).toMatch(/params\.mapping \?\?/);
|
||
});
|
||
|
||
it('存量错值有纠正入口,且纠正规则保守(只动 NULL / 重复认领)', () => {
|
||
const idx = ledgerSrc.indexOf('export async function healOrderPrepMapping');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const body = ledgerSrc.slice(idx, idx + 4000);
|
||
expect(body).toMatch(/duplicated\.has\(stored\)/);
|
||
expect(body).toMatch(/if \(!shouldBe\) continue;/);
|
||
// 必须挂进导入链路,否则错值不会自愈
|
||
expect(importSrc).toMatch(/healOrderPrepMapping\(/);
|
||
});
|
||
});
|