import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; /** * 厂商备货行「分配即供货完成」(2026-10-12,开发者批准)。 * * 规则:凡是**分配给厂商**产生的备货行,建行即 `prepare_status = 30`(供货完成), * 不再等人工点「完成备货」;分公司只需打电话让厂商送货、等着入库。 * **自采(`purchase_orders`)不在此列**:仍走「自采 → 点完成(写 20 待验收)→ 验收入库」。 * * 分配 COMMIT 后补齐人工完成会做的下游副作用(订单行成本/报价回写、单位归一、 * 免入库/直送核货行与结算桶、建待验入库、推进订单),复用人工完成同一套逻辑, * 但**不经**人工完成闸门(分配时货未到、报告尚未产生)。 * * 这里只钉「编排 + 字面量」,真库行为由分配链路的集成测试与 15 分钟对账覆盖。 */ const read = (rel: string) => readFileSync(new URL(rel, import.meta.url), 'utf8'); describe('厂商备货行分配即供货完成', () => { const alloc = read('../src/services/orderAllocationService.ts'); it('建备货行即写 prepare_status=30 + prepare_time,不再写 10', () => { const insertStart = alloc.indexOf('INSERT INTO supplier_preparations ('); expect(insertStart).toBeGreaterThan(-1); const insertSql = alloc.slice(insertStart, insertStart + 600); expect(insertSql).toContain('prepare_status, prepare_time'); expect(insertSql).toMatch(/VALUES \([^)]*30, NOW\(\)/); // 旧字面量(建行即 10 待备货)不得再出现 expect(insertSql).not.toMatch(/VALUES \(\$1, \$2, \$3, \$4, \$5, \$6, 10,/); }); it('复分配 / 改量时把本供应商备货行直接置 30(含被复活的缺货行)', () => { const updateStart = alloc.indexOf('UPDATE supplier_preparations'); expect(updateStart).toBeGreaterThan(-1); const updateEnd = alloc.indexOf('WHERE id = $1', updateStart); const updateSql = alloc.slice(updateStart, updateEnd > updateStart ? updateEnd + 20 : updateStart + 1400); expect(updateSql).toMatch(/prepare_status = 30/); expect(updateSql).toMatch(/prepare_time = COALESCE\(prepare_time, NOW\(\)\)/); // 旧写法(40 复活为 10 待备货)不得残留 expect(updateSql).not.toContain('prepare_status = CASE WHEN prepare_status = 40 THEN 10'); }); it('分配 COMMIT 后在非极简流转分支补齐自动完成的副作用', () => { expect(alloc).toContain('finalizeAutoCompletedSupplierPreparations'); const hookStart = alloc.indexOf('async function runAllocationAfterCommitSideEffects'); const hookBody = alloc.slice( hookStart, alloc.indexOf('export async function reprocessOrderAllocation') ); expect(hookBody).toContain('finalizeAutoCompletedSupplierPreparations'); // 必须落在「非极简流转」那一支(极简流转不建验收单) expect(hookBody.indexOf('directToDeliveryEnabled')).toBeLessThan( hookBody.indexOf('finalizeAutoCompletedSupplierPreparations') ); }); it('录单 / 改单(外部事务路径)也在 COMMIT 后补一次', () => { const submit = read('../src/services/customerOrderSubmitService.ts'); const manage = read('../src/services/orderManageItemsService.ts'); expect(submit).toContain('finalizeAutoCompletedSupplierPreparations'); expect(manage).toContain('finalizeAutoCompletedSupplierPreparations'); }); it('finalize 复用人工完成的下游动作,不经人工完成闸门', () => { const statusSrc = read('../src/services/supplierPreparationStatusService.ts'); const start = statusSrc.indexOf( 'export async function finalizeAutoCompletedSupplierPreparations' ); expect(start).toBeGreaterThan(-1); const end = statusSrc.indexOf('export async function updatePreparationPurchasePrice', start); const body = statusSrc.slice(start, end); expect(body).toContain('afterPreparationCompleted'); expect(body).toContain('persistPrepNormalizedPrice'); // 不得走人工完成的服务入口 / 闸门(质量报告硬门禁、超量确认等) expect(body).not.toContain('updateSupplierPreparationStatus'); expect(body).not.toContain('assertQualityReportHardGate'); // 只处理厂商行:缺货 / 已转采购不动 expect(body).toContain('COALESCE(sp.is_stockout, false) = false'); expect(body).toContain('COALESCE(sp.transferred_to_purchase, false) = false'); }); });