import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * 主系统导入单「无抵扣默认自采」:分公司没有可用采购员时按自由池建自采单。 * * 真机事故(2026-10-22 体验账号 BR003):行被标成 INTERNAL(自采),但 * createPurchaseOrderForItem 因"无可用采购员"静默 return false ⇒ 自采表 0 行, * 订单永远卡在 20 待分配(状态提示"尚有未履约明细…请先分配供应商或转自采")。 */ describe('导入单默认自采:无采购员按自由池建单(不静默卡单)', () => { const service = readFileSync( new URL('../src/services/orderAllocationService.ts', import.meta.url), 'utf8' ); it('createPurchaseOrderForItem 支持 allowMissingPurchaser,且未开启时保持旧行为', () => { expect(service).toContain('allowMissingPurchaser?: boolean'); expect(service).toContain('if (params.allowMissingPurchaser) {'); expect(service).toContain('free_pool'); // 未开启分支仍 return false(其它分配路径行为不变) expect(service).toContain('} else {\n return false;\n }'); }); it('ensureSelfPurchaseForImportedOrder 传入 allowMissingPurchaser(仅该导入口径)', () => { expect(service).toContain('allowMissingPurchaser: true'); // 传参位置必须在 ensureSelfPurchaseForImportedOrder 内 const fnStart = service.indexOf('export async function ensureSelfPurchaseForImportedOrder'); expect(fnStart).toBeGreaterThan(0); // 函数较长(含内部 SQL/接口),取后续 8000 字符覆盖完整函数体 const body = service.slice(fnStart, fnStart + 8000); expect(body).toContain('allowMissingPurchaser: true'); }); it('部分抵扣缺口默认自采分支同样传 allowMissingPurchaser(同一类半成品)', () => { const gapStart = service.indexOf('async function ensureSelfPurchaseForCoverGap'); expect(gapStart).toBeGreaterThan(0); const body = service.slice(gapStart, gapStart + 6000); expect(body).toContain('allowMissingPurchaser: true'); expect(body).toContain('部分抵扣缺口 ${attempted} 行需转自采但一张采购单未建'); }); it('行命中却一张没建时留 warn(不再完全静默)', () => { expect(service).toContain('标记自采 ${rows.rowCount} 行但未建任何采购单'); }); });