import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; /** * 订单商品成本 →「库存抵扣成本」必须跟着批次成本变化(2026-10-24 开发者: * 「为什么订单详情我修改了抵扣的库存批次的成本价 但是订单商品成本里面库存抵扣成本不是动态变化的」)。 * * 根因:改价只 `UPDATE` 已有账本行;批次成本为 0 时抵扣**不写账本** ⇒ UPDATE 命中 0 行, * 成本页永远 ¥0。修法:命中 0 行且改后金额 > 0 ⇒ 补记 `inventory_cover`;同价保存也重算(幂等); * 另提供存量修补 `repairMissingCoverLedger` + 管理员接口。 */ const svc = readFileSync(new URL('../src/services/inventoryBatchCostService.ts', import.meta.url), 'utf8'); const routes = readFileSync( new URL('../src/modules/workflow/routes/orderRoutes.ts', import.meta.url), 'utf8' ); describe('库存抵扣成本账本:改价后能补记、能修补', () => { it('UPDATE 命中 0 行时补记 inventory_cover(不是只改已有行)', () => { expect(svc).toContain('if ((updated.rowCount || 0) === 0 && Number(row.after_amount) > 0)'); expect(svc).toContain("'purchase', 'stock_cover', $3, $3, 20"); expect(svc).toContain("'inventory_cover'"); }); it('价格没变也重算账本(幂等,支持"同价再保存一次就补齐")', () => { expect(svc).toContain('价格没变:不写调整流水,但**仍重算一次抵扣账本**'); expect(svc).toMatch(/await recalcCoverLedgerForBatchCostChange\(client, \{[\s\S]*impact,/); }); it('存量修补:只补未结算订单,缺行插入、金额不符更新', () => { expect(svc).toContain('export async function repairMissingCoverLedger'); expect(svc).toContain('out.skippedSettled += 1'); expect(svc).toContain('out.inserted += 1'); expect(svc).toContain('out.updated += 1'); }); it('管理员补账接口存在', () => { expect(routes).toContain("router.post('/cost/repair-cover-ledger'"); expect(routes).toContain('仅管理员可修补抵扣成本账本'); }); });