/** * 仓内加工成品完工分配(阶段 2 原型)契约测试 * 设计:docs/design/20261102-warehouse-processing-raw-material-auto-purchase.md §4.2 / §12 */ import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; import { applyManualFinishedOutputAllocations, planFinishedOutputAllocation, type FinishedOutputDemandItem, } from '../src/services/processingFinishedOutputService'; import { parseProcessingFinishedNoInventoryConfig } from '../src/services/processingFinishedNoInventoryConfigService'; const ROOT = path.join(__dirname, '../..'); const read = (rel: string) => readFileSync(path.join(ROOT, rel), 'utf8'); function item( orderItemId: string, orderCode: string, demandQty: number, orderCreatedAt: string ): FinishedOutputDemandItem { return { orderItemId, orderCode, demandQty, orderCreatedAt }; } describe('历史分拣需求数量回填', () => { it('迁移把已完工成品的未开始分拣行需求改成实收分配量', () => { const migration = read('migrations/20261102_processing_picking_required_sync.sql'); expect(migration).toContain('SET required_qty = oi.received_qty'); expect(migration).toContain("processing_owner, '''')) = ''warehouse''"); expect(migration).toContain('received_stage'); expect(migration).toContain('pick_status, 0) < 30'); }); }); describe('加工完工同步分拣/配送数量', () => { it('完工分配后把未开始分拣行的 required_qty 改成该客户实际分配量', () => { const service = read('server/src/services/processingFinishedOutputService.ts'); expect(service).toContain('UPDATE picking_items pi'); expect(service).toContain('SET required_qty = $2::numeric'); expect(service).toContain('COALESCE(pt.pick_status, 0) < 30'); expect(service).toContain('row.allocatedQty'); }); }); describe('人工调整各客户成品分配', () => { const a = item('oi-a', 'ORD_A', 5, '2026-11-02T08:00:00Z'); const b = item('oi-b', 'ORD_B', 4, '2026-11-02T09:00:00Z'); it('产出 10 斤:人工 A 6 / B 4(A 多分 1 斤)', () => { const plan = applyManualFinishedOutputAllocations([a, b], 10, [ { orderItemId: 'oi-a', qty: 6 }, { orderItemId: 'oi-b', qty: 4 }, ]); expect(plan.allocations.map((x) => [x.orderCode, x.allocatedQty])).toEqual([ ['ORD_A', 6], ['ORD_B', 4], ]); }); it('人工合计与产出不一致时拒绝', () => { expect(() => applyManualFinishedOutputAllocations([a, b], 10, [ { orderItemId: 'oi-a', qty: 6 }, { orderItemId: 'oi-b', qty: 3 }, ]) ).toThrow(/不一致/); }); }); describe('成品产出分配:先到先得 + 余量平均分', () => { const a = item('oi-a', 'ORD_A', 4, '2026-11-02T08:00:00Z'); const b = item('oi-b', 'ORD_B', 5, '2026-11-02T09:00:00Z'); it('产出恰好满足:A 4 / B 5', () => { const plan = planFinishedOutputAllocation([b, a], 9); expect(plan.allocations.map((x) => [x.orderCode, x.allocatedQty])).toEqual([ ['ORD_A', 4], ['ORD_B', 5], ]); expect(plan.shortfallQty).toBe(0); expect(plan.excessQty).toBe(0); }); it('产出不足:先满足先创建的 A,再给 B', () => { const plan = planFinishedOutputAllocation([a, b], 7); expect(plan.allocations.map((x) => [x.orderCode, x.allocatedQty])).toEqual([ ['ORD_A', 4], ['ORD_B', 3], ]); expect(plan.shortfallQty).toBe(2); expect(plan.excessQty).toBe(0); }); it('产出有余量:先满足需求,余量按参与订单数平均分(A+1/B+1)', () => { const plan = planFinishedOutputAllocation([a, b], 11); expect(plan.allocations.map((x) => [x.orderCode, x.allocatedQty])).toEqual([ ['ORD_A', 5], ['ORD_B', 6], ]); expect(plan.excessQty).toBe(2); }); it('余量除不尽(0.001 粒度)时尾差按创建时间顺序补', () => { const c1 = item('oi-1', 'ORD_1', 1, '2026-11-02T08:00:00Z'); const c2 = item('oi-2', 'ORD_2', 1, '2026-11-02T09:00:00Z'); const plan = planFinishedOutputAllocation([a, b], 12); // 余量 3 → A/B 各 1.5(0.001 粒度可整除,直接平均) expect(plan.allocations.map((x) => [x.orderCode, x.allocatedQty])).toEqual([ ['ORD_A', 5.5], ['ORD_B', 6.5], ]); // 余量 0.001 无法分成两个 0.0005 → 整份给先创建的 A const planTiny = planFinishedOutputAllocation([c1, c2], 2.001); expect(planTiny.allocations.map((x) => [x.orderCode, x.allocatedQty])).toEqual([ ['ORD_1', 1.001], ['ORD_2', 1], ]); }); it('开发者场景:A 需 5 斤、B 需 4 斤,实际产出 10 斤 → 先满足 5/4,余 1 斤均分 → 5.5/4.5', () => { const oa = item('oi-a', 'ORD_A', 5, '2026-11-02T08:00:00Z'); const ob = item('oi-b', 'ORD_B', 4, '2026-11-02T09:00:00Z'); const plan = planFinishedOutputAllocation([ob, oa], 10); expect(plan.allocations.map((x) => [x.orderCode, x.allocatedQty])).toEqual([ ['ORD_A', 5.5], ['ORD_B', 4.5], ]); expect(plan.excessQty).toBe(1); expect(plan.shortfallQty).toBe(0); }); it('小数按 0.001 精度,分配之和恰好等于实际产出', () => { const c1 = item('oi-1', 'ORD_1', 1, '2026-11-02T08:00:00Z'); const c2 = item('oi-2', 'ORD_2', 1, '2026-11-02T09:00:00Z'); const planInexact = planFinishedOutputAllocation([c1, c2], 2.001); const sumInexact = planInexact.allocations.reduce((s, x) => s + x.allocatedQty, 0); expect(Math.round(sumInexact * 1000) / 1000).toBe(2.001); const planShort = planFinishedOutputAllocation([c1, c2], 0.5); expect(planShort.allocations[0].allocatedQty).toBe(0.5); expect(planShort.allocations[1].allocatedQty).toBe(0); }); it('同一订单多行:订单余量先按行平均,尾差给前面的行', () => { const a1 = item('oi-a1', 'ORD_A', 2, '2026-11-02T08:00:00Z'); const a2 = item('oi-a2', 'ORD_A', 2, '2026-11-02T08:00:00Z'); const b1 = item('oi-b1', 'ORD_B', 3, '2026-11-02T09:00:00Z'); const plan = planFinishedOutputAllocation([a1, a2, b1], 9); // 余量 2 → A/B 各 +1;A 的两行再平均分 1 → 各 +0.5 expect(plan.allocations.map((x) => [x.orderItemId, x.allocatedQty])).toEqual([ ['oi-a1', 2.5], ['oi-a2', 2.5], ['oi-b1', 4], ]); }); it('无产出/无需求时返回空分配', () => { expect(planFinishedOutputAllocation([a, b], 0).allocations.every((x) => x.allocatedQty === 0)).toBe(true); expect(planFinishedOutputAllocation([], 5).allocations).toEqual([]); }); }); describe('阶段 2 开关:默认开启(kill switch)', () => { it('未配置按默认开启;显式 enabled=false 才关闭', () => { expect(parseProcessingFinishedNoInventoryConfig(null).enabled).toBe(true); expect(parseProcessingFinishedNoInventoryConfig(undefined).enabled).toBe(true); expect(parseProcessingFinishedNoInventoryConfig({ enabled: 'false' }).enabled).toBe(true); expect(parseProcessingFinishedNoInventoryConfig({ enabled: true }).enabled).toBe(true); expect(parseProcessingFinishedNoInventoryConfig({ enabled: false }).enabled).toBe(false); expect(parseProcessingFinishedNoInventoryConfig(false).enabled).toBe(false); }); }); describe('阶段 2 原型边界(未接线,不得静默改库存/验收/订单状态)', () => { const service = read('server/src/services/processingFinishedOutputService.ts'); it('免入库路线写履约覆盖并推进订单,不卡订单流程', () => { expect(service).toContain('order_item_fulfillments'); expect(service).toContain('stock_cover_qty'); expect(service).toContain('tryAdvanceOrderToInspection'); expect(service).toContain('maybeAdvanceOrderAfterFulfillment'); expect(service).toContain('订单推进失败(不阻断回写)'); }); it('不写库存/流水/成本账本', () => { expect(service).not.toMatch(/INSERT\s+INTO\s+inventory\b/i); expect(service).not.toMatch(/INSERT\s+INTO\s+inventory_logs\b/i); expect(service).not.toMatch(/INSERT\s+INTO\s+cost_details\b/i); }); it('不动验收单、不推订单状态', () => { expect(service).not.toMatch(/DELETE\s+FROM\s+inspections/i); expect(service).not.toMatch(/UPDATE\s+orders\s+SET\s+order_status/i); expect(service).toContain('skipped'); }); it('实收回写走唯一写入口', () => { expect(service).toContain('writeOrderItemReceivedQty'); expect(service).toContain('receivedStage'); }); it('免入库条件 = 开关开 + 成品配置了原材料 BOM;未配置 BOM 仍走原待验流程', () => { const demand = read('server/src/services/processingDemandSheetService.ts'); expect(demand).toContain('isProcessingFinishedNoInventoryEnabled'); expect(demand).toContain('hasProcessingBomForOutput'); expect(demand).toContain('成品免入库,无需操作'); // 没有 BOM 时仍走原 updatePurchaseOrderStatus(生成待验) expect(demand).toContain('await updatePurchaseOrderStatus('); }); it('加工单完工自动触发:需求单 + BOM + 开关开 → 分配回写并置完工', () => { const task = read('server/src/services/processingTaskService.ts'); expect(task).toContain("String(row.source_type || '') === 'demand'"); expect(task).toContain('hasProcessingBomForOutput'); expect(task).toContain('applyFinishedOutputAllocation'); expect(task).toContain('PROCESS_STATUS.completed'); expect(task).toContain('成品免入库回写失败'); }); it('已回写任务不被成品验收重算降级', () => { const demand = read('server/src/services/processingDemandSheetService.ts'); expect(demand).toContain('finished_output_applied_at'); expect(demand).toContain('completed: true'); }); it('开关关闭时拒绝执行', () => { expect(service).toContain('feature_disabled'); expect(service).toContain('warehouse_processing_finished_no_inventory_enabled'); }); it('重复调用幂等:同产出直接返回,产出变化才重算覆盖', () => { expect(service).toContain('finished_output_applied_at'); expect(service).toContain('finished_output_actual_qty'); expect(service).toContain('alreadyApplied'); expect(service).toContain('reapplied'); expect(service).toContain("await client.query('ROLLBACK')"); }); it('成本回写复用既有完工结算(不再自己写 unit_cost)', () => { expect(service).toContain('settleDemandSheetCostOnCompletion'); expect(service).toContain('perOrderQty'); expect(service).not.toMatch(/UPDATE\s+order_items\s+SET\s+unit_cost/i); const demand = read('server/src/services/processingDemandSheetService.ts'); expect(demand).toContain('perOrderQty?:'); // 默认口径不变:未传 perOrderQty 仍走成品验收 actual_qty expect(demand).toContain('FROM inspections i'); }); it('幂等标记迁移只加列,不新增业务表', () => { const migration = read('migrations/20261102_processing_finished_output_apply.sql'); expect(migration).toContain('finished_output_applied_at'); expect(migration).toContain('finished_output_actual_qty'); expect(migration).toContain('finished_output_plan'); expect(migration).not.toContain('CREATE TABLE'); }); });