import { beforeEach, describe, expect, it, vi } from 'vitest'; const { query, release, connect } = vi.hoisted(() => { const query = vi.fn(); const release = vi.fn(); const connect = vi.fn(async () => ({ query, release })); return { query, release, connect }; }); vi.mock('../src/db', () => ({ pool: { connect, query }, getRawPoolQuery: () => vi.fn(), })); vi.mock('../src/services/inspectionRejectFulfillmentService', () => ({ createSupplierInspectionIfNeeded: vi.fn(async () => false), hasOpenPendingInspection: vi.fn(async () => false), hasSuccessfulInboundInspection: vi.fn(async () => false), prunePendingInspectionsWhenInboundCovered: vi.fn(async () => 0), pruneStalePendingInspectionsAfterPass: vi.fn(async () => 0), })); vi.mock('../src/services/processingTaskService', () => ({ ensureProcessingTaskForInspection: vi.fn(async () => undefined), syncProcessingTasksForOrder: vi.fn(async () => undefined), })); vi.mock('../src/services/orderItemFulfillmentService', () => ({ isOrderItemFulfillmentValid: vi.fn(async () => true), listUncoveredOrderItems: vi.fn(async () => []), })); vi.mock('../src/services/orderInspectionStateService', () => ({ isOrderItemInboundCovered: vi.fn(async () => false), isMergeGuestOrderItem: vi.fn(async () => false), })); vi.mock('../src/services/inspectionDisplayService', () => ({ lookupInspectionItemName: vi.fn(async () => '商品'), })); import { countPendingSupplierPreparations, demotePrematureInspectionStatus, ensureInspectionsForOrder, tryAdvanceOrderToInspection, } from '../src/services/preparationFulfillmentService'; import { computeEffectiveOrderStatus } from '../src/services/orderStatusDisplayService'; import { createSupplierInspectionIfNeeded } from '../src/services/inspectionRejectFulfillmentService'; describe('prep before inspection gate', () => { beforeEach(() => { query.mockReset(); release.mockReset(); connect.mockClear(); vi.mocked(createSupplierInspectionIfNeeded).mockClear(); }); it('countPendingSupplierPreparations 以 prepare_status < 30 为准', async () => { query.mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 2 }] }); const cnt = await countPendingSupplierPreparations('ORD1'); expect(cnt).toBe(2); const sql = String(query.mock.calls[0][0]); expect(sql).toMatch(/prepare_status.*<\s*30/); expect(sql).not.toMatch(/NOT EXISTS/); }); it('备货未完成时 ensureInspectionsForOrder 不建待验', async () => { const clientQuery = vi.fn(async (sql: string) => { const s = String(sql); if (s.includes('DELETE FROM inspections i')) { return { rowCount: 1, rows: [{ id: 'ghost-1' }] }; } if (s.includes('FROM supplier_preparations sp') && s.includes('prepare_qty')) { return { rowCount: 1, rows: [ { order_code: 'ORD1', sku_code: 'SKU_DRY', supplier_code: 'SUP1', prepare_qty: 10, purchase_price: 5, item_name: '干杂', prepare_status: 10, }, ], }; } if (s.includes('FROM order_items oi')) { return { rowCount: 1, rows: [ { id: 'item-1', sku_code: 'SKU_DRY', quantity: 10, price: 8, item_name: '干杂', supplier_code: 'SUP1', stock_cover_qty: 0, }, ], }; } if (s.includes('order_item_fulfillments')) { return { rowCount: 1, rows: [{}] }; } return { rowCount: 0, rows: [] }; }); const created = await ensureInspectionsForOrder('ORD1', 'BR001', { client: { query: clientQuery } as any, }); expect(created).toBe(0); expect(createSupplierInspectionIfNeeded).not.toHaveBeenCalled(); }); it('tryAdvance:备货未完成不进 40;误进 40 可回退', async () => { // demote path when status=40 query .mockResolvedValueOnce({ rowCount: 1, rows: [{ order_status: 40, branch_code: 'BR001' }], }) // countPendingSupplierPreparations .mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 1 }] }) // countPendingPurchases .mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 0 }] }) // prune .mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 'i1' }] }) // demote update .mockResolvedValueOnce({ rowCount: 1, rows: [{ order_code: 'ORD1' }] }); const demoted = await tryAdvanceOrderToInspection('ORD1'); expect(demoted).toMatchObject({ advanced: false, newStatus: 30 }); query.mockReset(); // status=30, pending preps > 0 → block advance query .mockResolvedValueOnce({ rowCount: 1, rows: [{ order_status: 30, branch_code: 'BR001' }], }) // listUncovered empty via mocked listUncoveredOrderItems // pending preps .mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 1 }] }) .mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 0 }] }); const blocked = await tryAdvanceOrderToInspection('ORD1'); expect(blocked.advanced).toBe(false); expect(String(blocked.message)).toMatch(/备货未完成/); }); it('demotePrematureInspectionStatus 在 pending 时回退 40→30', async () => { query .mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 2 }] }) // pending preps .mockResolvedValueOnce({ rowCount: 1, rows: [{ cnt: 0 }] }) // pending purchases .mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 'ghost' }] }) // prune .mockResolvedValueOnce({ rowCount: 1, rows: [{ order_code: 'ORD1' }] }); // update const result = await demotePrematureInspectionStatus('ORD1'); expect(result.demoted).toBe(true); expect(result.pruned).toBe(1); }); it('computeEffectiveOrderStatus:有未完成备货时不显示 40', () => { const status = computeEffectiveOrderStatus( 40, { allocationFulfilled: true, pendingPreps: 1, pendingPurchases: 0, itemCount: 3, inspectionCount: 2, pendingProcessing: 0, pendingPicks: 0, outboundPicks: 0, deliveryStarted: false, deliveryEnded: false, customerSigned: false, }, 'pending', true ); expect(status).toBe(30); }); });