import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import path from 'node:path'; /** * 主系统「核货完毕」事实镜像(2026-10-11,设计文档 `20261011-免入库核货完毕标记.md`)。 * * 钉住四件事: * ① 事实只来自**拉取**(事件通道对"等量核货"静默)——lookup 是唯一可靠通道; * ② 落库只覆盖 payload 的 5 个事实键,**不得**顺手改其它键(台账 payload 是共享快照); * ③ 判定口径是 `actualQty != null` 且**仅免入库行**(送仓行已有"入库完成"标签); * ④ GET 路由**不调外部**(主系统抖动不得拖垮详情),刷新是独立 POST 且失败不 500。 */ const service = readFileSync( path.join(__dirname, '../src/services/mainSystemActualFacts.ts'), 'utf8' ); const routes = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/orderRoutes.ts'), 'utf8' ); const importLoop = readFileSync(path.join(__dirname, '../src/services/mainSystemOrderImport.ts'), 'utf8'); describe('核货事实:通道与落库', () => { it('用 lookup 拉(不受 active/archived 视图影响),不靠事件', () => { expect(service).toMatch(/lookupMainSystemPreparationStates/); // 代码里**不得**出现事件订阅/收件箱依赖(`actual_qty_request` 只允许出现在解释性注释里) const codeOnly = service .split('\n') .filter((line) => !line.trim().startsWith('*') && !line.trim().startsWith('//') && !line.trim().startsWith('/*')) .join('\n'); expect(codeOnly).not.toMatch(/actual_qty_request/); }); it('只覆盖 payload 的 5 个事实键(payload || jsonb_build_object)', () => { expect(service).toMatch(/payload \|\| jsonb_build_object/); for (const key of ['actualQty', 'actualQtyAt', 'qtyDiff', 'isInbounded', 'inboundAt']) { expect(service, `缺少事实键 ${key}`).toContain(`'${key}'`); } // 不得整包替换 payload(那是导入的职责,会把 skipInbound 等事实抹掉) expect(service).not.toMatch(/SET payload = \$/); }); it('判定用 actualQty != null(不用 actualQtyAt:免入库行实测恒为 null)', () => { expect(service).toMatch(/checked: actualQty !== null/); expect(service).toMatch(/不用 `actualQtyAt`/); }); }); describe('核货事实:路由与兜底', () => { it('GET 路由只读库,POST 路由才拉取,两者都要权限', () => { const getIdx = routes.indexOf("'/orders/:orderCode/main-system-prep-facts'"); const postIdx = routes.indexOf("'/orders/:orderCode/main-system-prep-facts/refresh'"); expect(getIdx).toBeGreaterThan(-1); expect(postIdx).toBeGreaterThan(-1); const getBlock = routes.slice(getIdx, getIdx + 900); const postBlock = routes.slice(postIdx, postIdx + 1100); expect(getBlock).toMatch(/listOrderActualFacts/); expect(getBlock).not.toMatch(/refreshOrderActualFacts/); expect(postBlock).toMatch(/refreshOrderActualFacts/); expect(getBlock).toMatch(/requireWorkflowPermission/); expect(postBlock).toMatch(/requireWorkflowPermission/); }); it('刷新失败不抛(详情必须照常展示库内已有事实)', () => { const idx = service.indexOf('export async function refreshOrderActualFacts'); const fn = service.slice(idx, idx + 900); expect(fn).toMatch(/catch \(error: unknown\)/); expect(fn).toMatch(/refreshed = false|reason =/); }); it('定时对账里挂了在飞单兜底(等量核货没人打开详情也能补上)', () => { expect(importLoop).toMatch(/refreshInFlightActualFactsForBranch/); }); });