import { beforeEach, describe, expect, it, vi } from 'vitest'; /** * T1 · BIZ-14 独立边界复核(2026-10-19,tester)。 * * 针对修复后的**唯一写入方** `inspectionComplianceService.writeReattributedSelfPurchaseActual`: * ① 累计入库 = 0 ⇒ **不写** `purchase_status=30`、不写 `actual_qty`(不留"30 但实收为空"半写); * ② 累计入库 > 0 ⇒ 一条 UPDATE 同时写 30 + `actual_qty`(值 = 累计入库量,不是计划量); * ③ 幂等:连调两次结果一致(实收量不累加、不改写第二次); * ④ 入参不全 ⇒ 直接返回 null,一次查询都不发。 */ const H = vi.hoisted(() => ({ poolQuery: vi.fn() })); vi.mock('../src/db', () => ({ pool: { query: (...args: unknown[]) => H.poolQuery(...args), connect: vi.fn() }, query: (...args: unknown[]) => H.poolQuery(...args), getRawPoolQuery: () => H.poolQuery, })); import { writeReattributedSelfPurchaseActual } from '../src/services/inspectionComplianceService'; type Row = Record; type Call = { sql: string; params: unknown[] }; const state = { batches: [] as Array<{ actual_qty: number | string }>, calls: [] as Call[], /** 同订单同 SKU 有效订单行数(R-A:无 sourcePurchaseCodes 时先查;>1 跳过写入) */ siblings: 1, }; const db = { query: async (sql: string, params: unknown[] = []) => { const text = String(sql); state.calls.push({ sql: text, params }); // R-A(2026-10-19):无 sourcePurchaseCodes 时先查同订单同 SKU 有效行数 if (text.includes('COUNT(*)::int AS cnt') && text.includes('FROM order_items')) { return { rows: [{ cnt: state.siblings }], rowCount: 1 }; } if (text.includes('FROM inspections')) { return { rows: state.batches, rowCount: state.batches.length }; } if (text.includes('UPDATE purchase_orders')) { return { rows: [], rowCount: 1 }; } throw new Error(`fake db: 未处理的 SQL: ${text}`); }, }; function run(overrides: Row = {}): Promise { return writeReattributedSelfPurchaseActual(db as never, { purchaseCode: 'PURC1', orderCode: 'ORD1', skuCode: 'SKU1', actualPrice: 20, ...overrides, }); } beforeEach(() => { state.batches = []; state.calls = []; state.siblings = 1; }); describe('BIZ-14 边界①:累计入库 = 0 ⇒ 不写 30 / actual_qty', () => { it('无已完成入库批次 ⇒ 返回 null、零写入', async () => { const result = await run(); expect(result).toBeNull(); expect(state.calls.some((call) => /UPDATE\s+purchase_orders/i.test(call.sql))).toBe(false); expect(state.calls.some((call) => /purchase_status\s*=\s*30/.test(call.sql))).toBe(false); expect(state.calls.some((call) => /actual_qty\s*=/.test(call.sql))).toBe(false); }); }); describe('BIZ-14 边界②:累计入库 > 0 ⇒ 一次 UPDATE 写 30 + actual_qty', () => { it('多批次求和 2 + 3.5 = 5.5;actual_qty 用累计量而不是计划量', async () => { state.batches = [{ actual_qty: 2 }, { actual_qty: '3.5' }]; const result = await run(); expect(result).toBe(5.5); const writes = state.calls.filter((call) => /UPDATE\s+purchase_orders/i.test(call.sql)); expect(writes).toHaveLength(1); expect(writes[0].sql).toMatch(/purchase_status\s*=\s*30/); expect(writes[0].sql).toMatch(/actual_qty\s*=\s*\$2::numeric/); expect(writes[0].params).toEqual(['PURC1', 5.5, 20]); // 只写一行采购单(不得顺带改其它表) expect(state.calls.filter((call) => /\b(INSERT|DELETE)\b/i.test(call.sql))).toHaveLength(0); }); it('actualPrice 缺省 ⇒ 传 null,SQL 用 COALESCE 保留既有 actual_price', async () => { state.batches = [{ actual_qty: 1 }]; const result = await run({ actualPrice: null }); expect(result).toBe(1); const write = state.calls.find((call) => /UPDATE\s+purchase_orders/i.test(call.sql))!; expect(write.params).toEqual(['PURC1', 1, null]); expect(write.sql).toMatch(/COALESCE\(\$3::numeric, actual_price\)/); }); }); describe('BIZ-14 边界③:幂等(连调两次结果一致)', () => { it('同一累计入库量连调两次 ⇒ 两次 UPDATE 的 actual_qty 都是 5.5,不累加', async () => { state.batches = [{ actual_qty: 2 }, { actual_qty: 3.5 }]; const first = await run(); const second = await run(); expect(first).toBe(5.5); expect(second).toBe(5.5); const writes = state.calls.filter((call) => /UPDATE\s+purchase_orders/i.test(call.sql)); expect(writes).toHaveLength(2); expect(writes.map((call) => call.params[1])).toEqual([5.5, 5.5]); expect(writes.map((call) => call.params[0])).toEqual(['PURC1', 'PURC1']); }); }); describe('BIZ-14 边界④:入参不全 ⇒ 不发任何查询', () => { it.each([ ['purchaseCode', { purchaseCode: '' }], ['orderCode', { orderCode: ' ' }], ['skuCode', { skuCode: '' }], ])('%s 为空 ⇒ 返回 null 且零查询', async (_name, overrides) => { state.batches = [{ actual_qty: 9 }]; const result = await run(overrides); expect(result).toBeNull(); expect(state.calls).toHaveLength(0); }); });