/** * 「改批次成本 ⇒ 重算订单抵扣账本」的入口一致性(2026-10-15 缺陷)。 * * 缺陷:**库存管理「修改」**改批次成本时只写 `inventory.cost_price`,什么都不重算; * 而订单详情「抵扣成本价」是**直读账本**(`cost_details.inventory_cover` ÷ 抵扣量) * ⇒ 改完成本订单里还是旧价(开发者实测:批次 11.00、订单仍显示 10.00;本地实测账本停在 10)。 * * 口径(2026-10-15 开发者裁决):**只修库存管理「修改」这一条入口**;重算沿用 2026-10-14 的口径 * ——「只重算**未结算**订单;已结算(客户结算单 statement_status >= 20)不回头改」, * 并要求统一留痕(备注必填 + `inventory_logs('adjust')` + 审计)。 * * 设计/口径出处:`inventoryBatchCostService` 文件头注释 + 本测试。 */ import { readFileSync } from 'fs'; import { describe, expect, it } from 'vitest'; const costService = readFileSync( new URL('../src/services/inventoryBatchCostService.ts', import.meta.url), 'utf8' ); const editService = readFileSync( new URL('../src/services/inventoryBatchEditService.ts', import.meta.url), 'utf8' ); describe('共享重算函数(口径一处)', () => { it('存在导出的共享函数:重算未结算账本 + 写 adjust 流水', () => { expect(costService).toContain( 'export async function recalcCoverLedgerForBatchCostChange(' ); // 已结算跳过(2026-10-14 口径,勿改) expect(costService).toMatch( /for \(const row of impact\.rows\) \{\s*\n\s*if \(row\.skipped === 'settled'\) continue;/ ); // 只动抵扣账本行(inventory_cover) expect(costService).toContain("AND cost_source = 'inventory_cover'"); // 留痕:与库存调整同表同口径 expect(costService).toMatch(/INSERT INTO inventory_logs \(/); expect(costService).toContain("'adjust'"); }); it('抵扣弹窗「改成本」也走同一个函数(不再各写一套)', () => { expect(costService).toContain('await recalcCoverLedgerForBatchCostChange(client, {'); }); }); describe('库存管理「修改」这条入口', () => { it('改成本时在同事务里调用共享重算', () => { expect(editService).toContain("const { recalcCoverLedgerForBatchCostChange } = await import('./inventoryBatchCostService');"); expect(editService).toContain('await recalcCoverLedgerForBatchCostChange(client, {'); // 必须在 COMMIT 之前(同一事务):重算代码块出现在 'COMMIT' 之前 const recalcAt = editService.indexOf('recalcCoverLedgerForBatchCostChange(client, {'); const commitAt = editService.indexOf("await client.query('COMMIT')"); expect(recalcAt).toBeGreaterThan(-1); expect(commitAt).toBeGreaterThan(recalcAt); }); it('只在成本价真的变了才重算(其它字段修改不触发)', () => { expect(editService).toContain('costChange = { before: Number(beforeBatch.cost_price) || 0, after: Number(next) || 0 };'); expect(editService).toContain('if (costChange) {'); // 未变化时 costChange 保持 null(幂等:重复保存同价不写流水/不动账本) expect(editService).toMatch(/if \(\(beforeBatch\.cost_price \?\? null\) !== \(next \?\? null\)\) \{/); }); it('备注选填,但留痕不留空(2026-10-15 开发者:改为可填)', () => { // 空备注不再拦(原来 throw '请填写修改备注') expect(editService).not.toContain('请填写修改备注'); // 空时补中性标签 ⇒ inventory_logs / 审计里永远有可读留痕 expect(editService).toContain("trimText(input.remark) || '库存批次修改'"); }); it('把重算结果回给调用方并写进审计(可追溯改了几个未结算单)', () => { expect(editService).toContain('cost_recalc?: { affected: number; skipped_settled: number } | null;'); expect(editService).toContain('cost_recalc: costRecalc,'); // 2026-10-15:审计补「身份 + 商品档案前值 + 改造后的同名列快照」⇒ 日志能显示 前 → 后 expect(editService).toContain('newValue: { ...input, ...afterAudit, changed, cost_recalc: costRecalc },'); expect(editService).toContain('...(beforeProductAudit || {}),'); }); it('审计留全前后值:身份/批次级前值 + 提交后重读的同名列快照', () => { // 身份(否则日志里看不出改的是哪个商品/批次,只会显示「库存 」) expect(editService).toContain('batch_no: String(row.batch_no || \'\'),'); expect(editService).toContain('...beforeBatch,'); // 改造后重读一遍(含推导出来的 expiry_date —— 它不在请求体里) expect(editService).toContain('let afterAudit: Record = {};'); expect(editService).toMatch(/SELECT i\.batch_no, i\.sku_code, s\.sku_name/); expect(editService).toContain('expiry_date: a.expiry_date == null ? null : toDateOnly(a.expiry_date),'); // 快照失败不得影响审计写入(更不能回滚业务) expect(editService).toContain("console.error('[inventoryBatchEdit] after-snapshot failed (审计仍写):', afterError);"); }); });