import { describe, expect, it } from 'vitest'; /** * T1 · BIZ-3 独立边界复核(2026-10-19,tester)。 * * 不复用 dev 的 mock:这里用一个**按行数据真算**的假 DB(不是把返回值写死),钉住 * ① 无本单订单号(自采/独立加工)⇒ `otherReserved` = 全部在用预留,且**省掉 own 查询**; * ② 多订单号/重复订单号 ⇒ own 只算一次(旧写法逐单号调 `sumOtherOrderReservations` 会算 N 倍); * ③ 不传 `reservationExemptOrderCodes` ⇒ `getClaimableStockQty` 逐字沿用旧口径(只豁免一个 orderCode); * ④ 数据不一致(own > total)时 `otherReserved` 夹到 0;切分/可扣量路径**只读**。 */ type Reservation = { order_code: string | null; sku_code: string; qty: number; status: number; branch_code: string | null; }; type Call = { sql: string; params: unknown[] }; const state = { reservations: [] as Reservation[], available: 0, calls: [] as Call[], /** 强制覆盖(模拟数据不一致),null = 按行算 */ forceTotal: null as number | null, forceOwn: null as number | null, }; function sum(rows: Reservation[]): number { return Math.round(rows.reduce((acc, row) => acc + (Number(row.qty) || 0), 0) * 1000) / 1000; } /** 只实现本用例用到的 4 类 SQL;按 SQL 文本取参数,结果来自 state 行数据 */ const db = { query: async (sql: string, params: unknown[] = []) => { const text = String(sql); state.calls.push({ sql: text, params }); const branch = (params[1] as string | null) ?? null; const branchOk = (row: Reservation) => !branch || row.branch_code == null || row.branch_code === branch; const active = state.reservations.filter( (row) => row.status === 10 && row.sku_code === params[0] && branchOk(row) ); if (text.includes('FROM inventory_reservations')) { if (text.includes('order_code = ANY($2::text[])')) { const codes = new Set((params[1] as string[]) || []); return { rows: [ { total: state.forceOwn ?? sum(state.reservations.filter((row) => row.status === 10 && row.sku_code === params[0] && row.order_code != null && codes.has(row.order_code))), }, ], rowCount: 1, }; } if (text.includes('NOT (order_code = ANY($3::text[]))')) { const codes = new Set((params[2] as string[]) || []); return { rows: [{ total: sum(active.filter((row) => row.order_code == null || !codes.has(row.order_code))) }], rowCount: 1, }; } if (text.includes('order_code IS DISTINCT FROM $3')) { const code = String(params[2] ?? ''); return { rows: [{ total: sum(active.filter((row) => code === '' || row.order_code === null || row.order_code !== code)) }], rowCount: 1, }; } // 全量在用预留(total) return { rows: [{ total: state.forceTotal ?? sum(active) }], rowCount: 1 }; } if (text.includes('FROM inventory')) { return { rows: [{ total: state.available }], rowCount: 1 }; } throw new Error(`fake db: 未处理的 SQL: ${text}`); }, }; const { resolveProcessingPickReservationSplit } = await import('../src/services/processingPickReservationService'); const { getClaimableStockQty } = await import('../src/services/inventoryStockCoverService'); function seed(): void { state.calls = []; state.available = 0; state.forceTotal = null; state.forceOwn = null; state.reservations = [ { order_code: 'A', sku_code: 'SKU1', qty: 2, status: 10, branch_code: 'BR002' }, { order_code: 'A', sku_code: 'SKU1', qty: 2, status: 10, branch_code: 'BR002' }, { order_code: 'B', sku_code: 'SKU1', qty: 3, status: 10, branch_code: 'BR002' }, { order_code: 'C', sku_code: 'SKU1', qty: 1, status: 10, branch_code: 'BR002' }, { order_code: null, sku_code: 'SKU1', qty: 2, status: 10, branch_code: 'BR002' }, { order_code: 'D', sku_code: 'SKU1', qty: 99, status: 20, branch_code: 'BR002' }, { order_code: 'E', sku_code: 'OTHER', qty: 50, status: 10, branch_code: 'BR002' }, { order_code: 'F', sku_code: 'SKU1', qty: 7, status: 10, branch_code: 'BR001' }, ]; } describe('BIZ-3 边界①:无本单订单号(自采/独立加工)', () => { it('other = 全部在用预留,own = 0,且只查一次(不查 own)', async () => { seed(); const split = await resolveProcessingPickReservationSplit(db as never, { orderCodes: [], skuCode: 'SKU1', branchCode: 'BR002', }); // 4(A) + 3(B) + 1(C) + 2(无订单号);status=20、其它 SKU、其它分公司都不计 expect(split).toEqual({ totalReserved: 10, ownReserved: 0, otherReserved: 10 }); expect(state.calls).toHaveLength(1); expect(state.calls[0].sql).toContain('status = 10'); expect(state.calls.some((call) => call.sql.includes('order_code = ANY($2::text[])'))).toBe(false); }); }); describe('BIZ-3 边界②:多订单号/重复订单号 ⇒ own 只算一次', () => { it('orderCodes=[A,A,B] ⇒ total=10、own=7、other=3(不是 N 倍)', async () => { seed(); const split = await resolveProcessingPickReservationSplit(db as never, { orderCodes: ['A', 'A', 'B'], skuCode: 'SKU1', branchCode: 'BR002', }); expect(split).toEqual({ totalReserved: 10, ownReserved: 7, otherReserved: 3 }); // 旧写法:A 的他单 = B(3)+C(1)+null(2)=6;B 的他单 = A(4)+C(1)+null(2)=7 ⇒ other=13(N 倍) expect(split.otherReserved).toBeLessThan(6); const ownCall = state.calls.find((call) => call.sql.includes('order_code = ANY($2::text[])')); expect(ownCall).toBeTruthy(); expect(ownCall!.params[1]).toEqual(['A', 'B']); // 去重后一次传入 // 只读:切分不得产生任何写语句 expect(state.calls.some((call) => /\b(INSERT|UPDATE|DELETE)\b/i.test(call.sql))).toBe(false); }); it('own > total(数据不一致)⇒ other 夹到 0,不为负', async () => { seed(); state.forceTotal = 4; state.forceOwn = 5; const split = await resolveProcessingPickReservationSplit(db as never, { orderCodes: ['A', 'B'], skuCode: 'SKU1', branchCode: 'BR002', }); expect(split).toEqual({ totalReserved: 4, ownReserved: 5, otherReserved: 0 }); }); }); describe('BIZ-3 边界③:不传 reservationExemptOrderCodes ⇒ getClaimableStockQty 旧口径', () => { it('旧口径只豁免 orderCode 一个:available=10、A=4、B=3、C=1、无主=2 ⇒ claimable=4', async () => { seed(); state.available = 10; const claimable = await getClaimableStockQty(db as never, 'SKU1', 'BR002', 'A'); expect(claimable).toBe(4); // 10 − (B3 + C1 + 无主2) = 4;只豁免 A 自己的 4,B 仍算他单 const otherCall = state.calls.find((call) => call.sql.includes('order_code IS DISTINCT FROM $3')); expect(otherCall).toBeTruthy(); expect(otherCall!.params[2]).toBe('A'); }); it('不传 orderCode ⇒ 全部在用预留都算他单:10 − (4+3+1+2) = 0', async () => { seed(); state.available = 10; expect(await getClaimableStockQty(db as never, 'SKU1', 'BR002', null)).toBe(0); }); it('传空数组 ⇒ 与旧口径一致(不触发数组豁免查询)', async () => { seed(); state.available = 10; expect(await getClaimableStockQty(db as never, 'SKU1', 'BR002', null, [])).toBe(0); expect(state.calls.some((call) => call.sql.includes('NOT (order_code = ANY($3::text[]))'))).toBe(false); }); it('传豁免数组 ⇒ 一次豁免多个订单号:免 A ⇒ 4;免 A+B ⇒ 10 − (C1 + 无主2) = 7', async () => { seed(); state.available = 10; expect(await getClaimableStockQty(db as never, 'SKU1', 'BR002', 'A', ['A'])).toBe(4); state.calls = []; expect(await getClaimableStockQty(db as never, 'SKU1', 'BR002', 'A', ['A', 'B'])).toBe(7); const exemptCall = state.calls.find((call) => call.sql.includes('NOT (order_code = ANY($3::text[]))')); expect(exemptCall).toBeTruthy(); expect(exemptCall!.params[2]).toEqual(['A', 'B']); }); });