import { beforeEach, describe, expect, it, vi } from 'vitest'; const { resolveInboundCostUnitPrice, applyBidCostAfterInbound } = vi.hoisted(() => ({ resolveInboundCostUnitPrice: vi.fn(), applyBidCostAfterInbound: vi.fn(), })); vi.mock('../src/db', () => ({ pool: { connect: vi.fn(), query: vi.fn() }, getRawPoolQuery: () => vi.fn(), })); vi.mock('../src/services/bidAwardService', () => ({ resolveInboundCostUnitPrice, applyBidCostAfterInbound, })); import { writeInspectionInboundCostWithBidSafe } from '../src/services/inspectionComplianceService'; describe('writeInspectionInboundCostWithBidSafe', () => { beforeEach(() => { resolveInboundCostUnitPrice.mockReset(); applyBidCostAfterInbound.mockReset(); }); it('竞价步骤失败时 ROLLBACK SAVEPOINT 后降级写成本,外层事务可继续', async () => { const released: string[] = []; const rolledBack: string[] = []; const sqlLog: string[] = []; const client = { query: vi.fn(async (sql: string) => { const s = String(sql); sqlLog.push(s); if (s.startsWith('SAVEPOINT ')) return { rowCount: 0, rows: [] }; if (s.startsWith('RELEASE SAVEPOINT ')) { released.push(s.replace('RELEASE SAVEPOINT ', '').trim()); return { rowCount: 0, rows: [] }; } if (s.startsWith('ROLLBACK TO SAVEPOINT ')) { rolledBack.push(s.replace('ROLLBACK TO SAVEPOINT ', '').trim()); return { rowCount: 0, rows: [] }; } // writeInspectionCost:幂等检查 if (s.includes('reference_type = \'inspection\'') || s.includes("reference_type = 'inspection'")) { return { rowCount: 0, rows: [] }; } // reconcile 依赖的查询:返回空即可让路径走完或早退 if (s.includes('FROM orders') || s.includes('FROM inspection') || s.includes('cost_details')) { return { rowCount: 0, rows: [] }; } return { rowCount: 0, rows: [] }; }), }; resolveInboundCostUnitPrice.mockResolvedValue(8.5); applyBidCostAfterInbound.mockRejectedValue( Object.assign(new Error('bid sync failed'), { code: '23505', status: 409 }) ); await writeInspectionInboundCostWithBidSafe(client as any, { orderCode: 'ORD-BID-1', skuCode: 'SKU1', supplierCode: 'SUP1', actualQty: 10, purchasePrice: 7, branchCode: 'BR001', referenceId: 'insp-1', operatorId: null, }); expect(rolledBack).toContain('insp_bid_cost'); expect(released).toContain('insp_bid_cost_fallback'); expect(sqlLog.some((s) => s.includes('25P02'))).toBe(false); // 降级后不应再抛,且曾进入竞价路径 expect(resolveInboundCostUnitPrice).toHaveBeenCalled(); expect(applyBidCostAfterInbound).toHaveBeenCalled(); }); it('降级仍失败时抛出原始竞价错误而非 25P02', async () => { const primary = Object.assign(new Error('unique conflict on bid cost'), { code: '23505', status: 409, }); let bidPathDone = false; const client = { query: vi.fn(async (sql: string) => { const s = String(sql); if (s.startsWith('SAVEPOINT ') || s.startsWith('RELEASE ') || s.startsWith('ROLLBACK ')) { return { rowCount: 0, rows: [] }; } if (s.includes("reference_type = 'inspection'") || s.includes('reference_type = \'inspection\'')) { if (bidPathDone) { throw Object.assign(new Error('current transaction is aborted'), { code: '25P02' }); } return { rowCount: 0, rows: [] }; } if (s.includes('FROM orders') || s.includes('cost_details') || s.includes('FROM inspection')) { return { rowCount: 0, rows: [] }; } return { rowCount: 0, rows: [] }; }), }; resolveInboundCostUnitPrice.mockResolvedValue(9); applyBidCostAfterInbound.mockImplementation(async () => { bidPathDone = true; throw primary; }); // 强制降级路径的 writeInspectionCost 也失败:在 fallback 里对 reference 检查抛错 // 上面已在 bidPathDone 后抛 25P02;此处验证抛出的是 primary await expect( writeInspectionInboundCostWithBidSafe(client as any, { orderCode: 'ORD-BID-2', skuCode: 'SKU2', supplierCode: 'SUP2', actualQty: 2, purchasePrice: 5, referenceId: 'insp-2', }) ).rejects.toMatchObject({ code: '23505', message: 'unique conflict on bid cost' }); }); });