import { describe, expect, it } from 'vitest'; import { readFileSync } from 'node:fs'; import path from 'node:path'; import { sumInventoryHoldingBalance } from '../src/services/inventorySurplusCostService'; /** * **方案 B:可抵额度按批次现价派生**(2026-10-14 开发者批准)—— 不变量 I1–I4。 * * 漏洞(改批次成本漏了一半):额度原本取自 `cost_details(inventory_holding)`(「账外持有桶」) * 的**存量金额** ⇒ 改 `inventory.cost_price` 不会动它,额度长期停在旧价上;而桶金额是历史累加值, * 与「批次现价 × 在库量」没有恒等关系,只能靠每个改价入口去"重估桶"补偿 —— 改价入口共 3 处 * (入库列表改价 / 库存调整 / 加工成品批次成本),漏一处就再次脱节。 * * 方案 B:额度 = `SUM(available_qty × cost_price)`(只数价 > 0 的批次)⇒ 改价后**自动跟随**。 * 设计:docs/design/20261014-可抵额度按批次现价派生(不再读持有桶).md */ const SRC = path.join(__dirname, '..', 'src'); const read = (rel: string) => readFileSync(path.join(SRC, rel), 'utf8'); type Batch = { batch_no: string; available_qty: number; cost_price: number }; type Row = Record; /** 模拟派生额度与批次列表两条查询;`calls` 用于断言"没查 cost_details"与参数作用域 */ function makeDb(batches: Batch[]) { const calls: Array<{ sql: string; params: unknown[] }> = []; const query = async (sql: string, params: unknown[] = []) => { const text = String(sql); calls.push({ sql: text, params }); // 派生额度聚合:Σ(可用量 × 成本价),只数价 > 0(与 SQL 同口径) if (text.includes('SUM(available_qty * COALESCE(cost_price')) { const total = batches .filter((b) => Number(b.cost_price) > 0) .reduce((s, b) => s + Number(b.available_qty || 0) * Number(b.cost_price || 0), 0); return { rowCount: 1, rows: [{ total }] }; } // 批次列表(FIFO)——必须放在聚合之后,两者都含 FROM inventory + available_qty if (text.includes('FROM inventory') && text.includes('available_qty')) { return { rowCount: batches.length, rows: batches as unknown as Row[] }; } return { rowCount: 0, rows: [] }; }; return { query, calls, db: { query } as never }; } describe('① 额度派生口径(I2 / I4)', () => { it('额度 = Σ(可用量 × 成本价)', async () => { const { db } = makeDb([ { batch_no: 'A', available_qty: 10, cost_price: 3 }, { batch_no: 'B', available_qty: 5, cost_price: 4 }, ]); expect(await sumInventoryHoldingBalance(db, { skuCode: 'SKU1', branchCode: 'BR002' })).toBe(50); }); it('I4:成本价 ≤ 0 的批次折算不出金额 ⇒ 不进额度', async () => { const { db } = makeDb([ { batch_no: 'A', available_qty: 10, cost_price: 0 }, { batch_no: 'B', available_qty: 5, cost_price: 4 }, ]); expect(await sumInventoryHoldingBalance(db, { skuCode: 'SKU1', branchCode: 'BR002' })).toBe(20); }); it('I2:额度与 cost_details 无关(一次查询、不碰那张表、带 SKU/分公司作用域)', async () => { const { db, calls } = makeDb([{ batch_no: 'A', available_qty: 10, cost_price: 3 }]); await sumInventoryHoldingBalance(db, { skuCode: 'SKU1', branchCode: 'BR002' }); expect(calls).toHaveLength(1); expect(calls[0].sql).toContain('FROM inventory'); expect(calls[0].sql).not.toContain('cost_details'); expect(calls[0].params).toEqual(['SKU1', 'BR002']); }); it('SKU 为空 ⇒ 0,且不打库', async () => { const { db, calls } = makeDb([{ batch_no: 'A', available_qty: 10, cost_price: 3 }]); expect(await sumInventoryHoldingBalance(db, { skuCode: ' ' })).toBe(0); expect(calls).toHaveLength(0); }); }); describe('② I1 / I3:改价 ⇒ 额度按 Δ × 可用量跟随(不再需要"重估桶")', () => { const amountFor = async (price: number, qty = 40) => { const { db } = makeDb([{ batch_no: 'A', available_qty: qty, cost_price: price }]); return sumInventoryHoldingBalance(db, { skuCode: 'SKU1', branchCode: 'BR002' }); }; it('I1:价 ¥3 → ¥3.5(可用 40)⇒ 额度 ¥120 → ¥140,变化 = Δ¥0.5 × 40', async () => { const before = await amountFor(3); const after = await amountFor(3.5); expect(before).toBe(120); expect(after).toBe(140); expect(Math.round((after - before) * 100) / 100).toBe(20); }); it('I3:同价重复求值幂等(预览/取消可反复)', async () => { expect(await amountFor(3.33)).toBe(await amountFor(3.33)); }); it('I3:改回原价 ⇒ 回到原额度(可回滚)', async () => { const before = await amountFor(3); await amountFor(3.5); expect(await amountFor(3)).toBe(before); }); }); describe('③ 回退档 sumCoverableOutsourcedQty:额度派生后逐批取值退化为 min(需求, 批次可用)', () => { async function coverable(batches: Batch[], needQty: number) { const { sumCoverableOutsourcedQty } = await import('../src/services/inventoryStockCoverService'); const { db } = makeDb(batches); return sumCoverableOutsourcedQty(db, 'SKU1', 'BR002', needQty); } it('需求超过在库 ⇒ 只抵价 > 0 批次的可用量(价 0 的批次不参与)', async () => { expect( await coverable( [ { batch_no: 'A', available_qty: 10, cost_price: 5 }, { batch_no: 'B', available_qty: 3, cost_price: 0 }, ], 20 ) ).toBe(10); }); it('需求小于在库 ⇒ 按 FIFO 截断到需求', async () => { expect( await coverable( [ { batch_no: 'A', available_qty: 10, cost_price: 5 }, { batch_no: 'B', available_qty: 10, cost_price: 5 }, ], 4 ) ).toBe(4); }); it('全部批次价 ≤ 0 ⇒ 0(折算不出成本,不抵)', async () => { expect(await coverable([{ batch_no: 'A', available_qty: 10, cost_price: 0 }], 20)).toBe(0); }); }); describe('④ 源码守卫:额度不得再读「账外持有桶」', () => { it('sumInventoryHoldingBalance 的查询不再出现 cost_details / inventory_holding', () => { const src = read('services/inventorySurplusCostService.ts'); const start = src.indexOf('export async function sumInventoryHoldingBalance'); expect(start).toBeGreaterThan(-1); const body = src.slice(start, src.indexOf('\n}', start)); expect(body).toContain('FROM inventory'); expect(body).not.toContain('cost_details'); expect(body).not.toContain('inventory_holding'); }); it('改批次成本不需要「重估桶」:该服务不写 inventory_holding(方案 A 已废弃)', () => { const src = read('services/inventoryBatchCostService.ts'); expect(src).not.toContain("'inventory_holding'"); expect(src).toContain('方案 B'); }); });