Files
SupplierSystem/server/tests/mainSystemAppendGuard.test.ts
T
Ubuntu b117bb9b8f fix(主系统导入): 「分配了却永远补不进订单」的卡死态 + 每轮一致性巡检
开发者报障(真库 BR003):主系统那边「米块」(prep 2f8564f6…,配送日 2026-10-13、prepare_status=10)
一直分配着,甚至推了「完结后改价 0 → 2」的待审;但我方订单 ORD_API_20261012_BR003_0002 里没有这一行,
自动对账跑了十几轮也补不进来(两侧口径永久分叉)。

根因两层(缺一不会卡死):
1) 源头 recordImportedPrepItems 的 disposition 只看"行在不在主系统 active 列表",
   写死 isStockout ? 'skipped_stockout' : 'imported' ⇒ **配不上我方订单行也写 imported**;
   再叠加 upsert 的 CASE(pending → EXCLUDED.disposition),它从"可重试的 pending"被升级成 imported;
2) 判据 needsAppendToOrder 只重试 pending/skipped_stockout ⇒ imported + 空链接**永久不补行**。

修法(三处,互相兜底):
- 源头:disposition 改为 isStockout ? 'skipped_stockout' : mapping.get(prepId) ? 'imported' : 'pending'
  —— 只有真配上订单行才写 imported,配不上保持 pending(可重试);
- 判据:链接为空一律视为"我方没有这一行"⇒ 要补;只排除人工/业务终态
  APPEND_BLOCKED_DISPOSITIONS = {cancelled, returned, completed, suspended}
  (历史数据已是 imported+空链接的也能自愈);
- 防复发巡检:importMainSystemOrders()(webhook 与定时对账共用入口)每轮跑
  auditUnlinkedImportedRows(branch):查 disposition='imported' AND our_order_item_id IS NULL,
  > 0 就打带商品名的告警,让"又被写坏了"当场可见。

不动:追加阀门 DAY_APPEND_CEILING=60、同日多单旋转、syncOrderManageItems 链路、状态链与分配信号量。

真库验证(BR003):
- 修前:台账 imported + 空链接;旧单 6 行无米块;
- 跑一轮真导入 ⇒ created:1 新建当日单 ORD_API_20261013_BR003_0002(status 30、配送日 2026-10-13、
  客户 MAIN_BR002),单行「米块 / SKU78955576135 / 斤 / 8.00 / ¥2.00」并自动库存抵扣 8;
- 台账链接已填(804d6fa4…)、disposition=imported;旧单未被塞行(6→6,无重复);
- auditUnlinkedImportedRows('BR003') = 0。

用例:server/tests/mainSystemAppendGuard.test.ts(10 例:空链接+imported 必须补 / 终态不补 /
缺货不补 / 源头 disposition 依赖 mapping / 巡检被主入口调用 的源码钉)。
服务端 tsc 0 错;全量 26 failed / 2679 passed 与基线持平(唯一差异仍是对方 WIP 的 pickingZoneOrder)。
文档:docs/design/20261012-主系统同日多单-发货后新分配另开补单.md §8(现象/两层根因/三处修法/真库验证/同类风险)。
2026-10-13 16:28:59 +08:00

92 lines
4.3 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 { readFileSync } from 'node:fs';
import path from 'node:path';
import { needsAppendToOrder } from '../src/services/mainSystemOrderImport';
/**
* **主系统分配了、我方订单里却没有那一行** 的回归(2026-10-13 真库事故:
* BR003「米块」prep `2f8564f6…` —— 主系统一直分配着、我方订单
* `ORD_API_20261012_BR003_0002` 里永远没有它)。
*
* 根因两层(都已修):
* ① 源头:`recordImportedPrepItems` 只看"行在不在主系统 active 列表",**不看有没有配上我方订单行**
* ⇒ 配不上的行也被写成 `disposition='imported'`;
* ② 判据:`needsAppendToOrder` 只重试 `pending`/`skipped_stockout` ⇒ `imported` + 空链接**永久卡死**。
*
* 现在:源头配不上就写 `pending`(可重试);判据把"链接为空"一律视为要补(只排除人工/业务终态);
* 并每轮导入做一次**一致性巡检**把这种自相矛盾态打成告警。
*/
const importSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemOrderImport.ts'),
'utf8'
);
const ledgerSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemPrepLedger.ts'),
'utf8'
);
const PREP = '2f8564f6-da45-43f1-b074-2457bfe08672';
function ledgerRow(overrides: Partial<{ prep_id: string; our_order_item_id: string | null; disposition: string | null }> = {}) {
return { prep_id: PREP, our_order_item_id: null as string | null, disposition: 'imported' as string | null, ...overrides };
}
describe('追加判据:台账 ↔ 我方订单行的唯一闸门', () => {
it('① 台账里没有这一行(主系统后来才分配)⇒ 要补', () => {
expect(needsAppendToOrder({ id: PREP }, [])).toBe(true);
expect(needsAppendToOrder({ id: PREP }, undefined)).toBe(true);
});
it('② 已配上我方订单行 ⇒ 不补(幂等,不重复加行)', () => {
expect(needsAppendToOrder({ id: PREP }, [ledgerRow({ our_order_item_id: 'item-1' })])).toBe(false);
});
it('③ **回归**:`imported` + 链接为空 ⇒ 要补(旧实现在这里永久卡死)', () => {
expect(needsAppendToOrder({ id: PREP }, [ledgerRow({ disposition: 'imported' })])).toBe(true);
});
it('④ 其它非终态处置 + 链接为空 ⇒ 也要补(pending / skipped_stockout / 未知值兜底)', () => {
for (const disposition of ['pending', 'skipped_stockout', 'weird_future_value', null]) {
expect(needsAppendToOrder({ id: PREP }, [ledgerRow({ disposition })])).toBe(true);
}
});
it('⑤ 人工/业务终态 ⇒ 不自动补(cancelled / returned / completed / suspended)', () => {
for (const disposition of ['cancelled', 'returned', 'completed', 'suspended']) {
expect(needsAppendToOrder({ id: PREP }, [ledgerRow({ disposition })])).toBe(false);
}
});
it('⑥ 主系统说这行缺货(isStockout)⇒ 不补', () => {
expect(needsAppendToOrder({ id: PREP, isStockout: true }, [])).toBe(false);
});
});
describe('源头:配不上订单行就不许写 imported(源码钉)', () => {
it('① disposition 必须依赖 mapping(配不上 ⇒ pending,可重试)', () => {
expect(ledgerSrc).toContain("isStockoutRow(row) ? 'skipped_stockout' : mapping.get(prepId) ? 'imported' : 'pending'");
});
it('② 判据不再用旧的"只重试 pending/skipped_stockout"白名单', () => {
expect(importSrc).not.toContain('APPEND_RETRYABLE_DISPOSITIONS');
expect(importSrc).toContain("const APPEND_BLOCKED_DISPOSITIONS = new Set(['cancelled', 'returned', 'completed', 'suspended'])");
});
});
describe('防复发:每轮导入做一次一致性巡检(自相矛盾态必须可见)', () => {
it('① 巡检函数存在,且查的是 imported + 空链接', () => {
expect(importSrc).toContain('export async function auditUnlinkedImportedRows');
expect(importSrc).toMatch(/disposition = 'imported'/);
expect(importSrc).toMatch(/our_order_item_id IS NULL/);
});
it('② 导入主入口(webhook 与定时对账共用)每轮都调用它,且失败不影响导入', () => {
const at = importSrc.indexOf('export async function importMainSystemOrders(');
const body = importSrc.slice(at, at + 1600);
expect(body).toContain('await auditUnlinkedImportedRows(branchCode)');
expect(body).toContain('不影响导入');
});
});