import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; /** * 自采改价 → **订单行成本回写**与「有无实收」解耦(2026-10-14 方案 A,开发者批准)。 * * 现象:自采价改成 1.9,但「采购备货风险提示」仍显示 4、继续报 —— 因为风险成本取 * `GREATEST(自采价, order_items.unit_cost)`,而订单行成本没跟着变。 * 根因:`amendPurchaseActual` 的「回写订单行成本」整段写在 `if (beforeQty > 0 …)` 里; * 自采 UI 路由恒传 `preserveActualQty: true`,未验收(`actual_qty IS NULL`)时 `beforeQty` * 退化成 0 ⇒ 整段跳过。 * * 本文件钉住三条不变量(源码级,防回退): * ① 成本回写**不在** `beforeQty > 0` 分支里(已移出); * ② 解耦条件 = `beforeQty > 0 || 价格确实变了`; * ③ 「数量/履约」同步仍由 `beforeQty > 0` 把关(方案 C 不变)。 * * 设计:docs/design/20260922-order-cost-writeback-to-supply.md(2026-10-14 修订) */ const svc = readFileSync( path.join(__dirname, '../src/services/purchaseAmendService.ts'), 'utf8' ); const routes = readFileSync( path.join(__dirname, '../src/modules/workflow/routes/purchaseRoutes.ts'), 'utf8' ); describe('自采改价:订单行成本回写与「有无实收」解耦', () => { it('① 成本回写已移出 `beforeQty > 0` 分支(排在 else-if 数量/单位分支之后)', () => { const unitOnlyBranch = svc.indexOf( "else if (!input.skipOrderItemSync && nextUnit !== beforeUnit" ); const costWriteback = svc.indexOf("await import('./orderItemUnitCostService')"); expect(unitOnlyBranch).toBeGreaterThan(-1); expect(costWriteback).toBeGreaterThan(-1); // 成本回写在 unit-only 分支之后 ⇒ 不再受 beforeQty 独占分支约束 expect(costWriteback).toBeGreaterThan(unitOnlyBranch); }); it('② 解耦条件:`beforeQty > 0 || 价格确实变了`', () => { expect(svc).toContain('(beforeQty > 0 || Math.abs(nextPrice - beforePrice) > 1e-9)'); // 既有守卫不变 expect(svc).toContain('!input.skipOrderItemSync &&'); expect(svc).toContain('!input.preserveOrderEnteredCost &&'); }); it('③ 数量/履约同步仍由 beforeQty>0 把关(方案 C 不变)', () => { expect(svc).toContain( 'if (beforeQty > 0 && row.order_code && row.sku_code && !input.skipOrderItemSync) {' ); expect(svc).toContain('syncOrderItemsFromSupplyCompletion'); }); it('④ 自采路由仍恒传 preserveActualQty=true(记录导致改前漏回写的约束)', () => { expect(routes).toContain('preserveActualQty: true'); }); });