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('不影响导入'); }); });