import { describe, expect, it, vi } from 'vitest'; import { assertOrderItemsFulfilled, dropStalePendingPreparationsForSku, isOrderItemFulfillmentValid, linkOrderItemToPreparation, linkOrderItemToPurchase, shouldDeletePendingPrepOnReconcile, shouldDropStalePrepOnSupplierSwitch, } from '../src/services/orderItemFulfillmentService'; describe('orderItemFulfillmentService', () => { it('links an order item to one aggregated supplier preparation', async () => { const query = vi.fn().mockResolvedValue({ rowCount: 1, rows: [] }); await linkOrderItemToPreparation( { query } as any, { orderItemId: '11111111-1111-1111-1111-111111111111', orderCode: 'ORD1', preparationId: '22222222-2222-2222-2222-222222222222', } ); expect(query).toHaveBeenCalledOnce(); expect(query.mock.calls[0][1]).toEqual([ '11111111-1111-1111-1111-111111111111', 'ORD1', '22222222-2222-2222-2222-222222222222', 0, ]); expect(query.mock.calls[0][0]).toContain('purchase_code = NULL'); }); it('switches an order item from preparation to purchase', async () => { const query = vi.fn().mockResolvedValue({ rowCount: 1, rows: [] }); await linkOrderItemToPurchase( { query } as any, { orderItemId: '11111111-1111-1111-1111-111111111111', orderCode: 'ORD1', purchaseCode: 'PUR1', } ); expect(query.mock.calls[0][0]).toContain('supplier_preparation_id = NULL'); expect(query.mock.calls[0][1]).toEqual([ '11111111-1111-1111-1111-111111111111', 'ORD1', 'PUR1', 0, ]); }); it('only accepts a live and matching source', async () => { const query = vi.fn().mockResolvedValue({ rowCount: 1, rows: [{ ok: 1 }] }); await expect( isOrderItemFulfillmentValid( { query } as any, '11111111-1111-1111-1111-111111111111' ) ).resolves.toBe(true); const sql = query.mock.calls[0][0] as string; expect(sql).toContain('sp.sku_code IS NOT DISTINCT FROM oi.sku_code'); expect(sql).toContain('po.purchase_status <> 40'); // 同日合单允许采购单挂在主单,不再要求 po.order_code = oi.order_code expect(sql).not.toMatch(/po\.order_code\s*=\s*oi\.order_code/); }); it('drops pending prep for old supplier after switch, keeps sibling and inbound completed', async () => { expect( shouldDeletePendingPrepOnReconcile({ prepareStatus: 10, hasMatchingActiveOrderItem: false }) ).toBe(true); expect( shouldDeletePendingPrepOnReconcile({ prepareStatus: 10, hasMatchingActiveOrderItem: true }) ).toBe(false); expect( shouldDeletePendingPrepOnReconcile({ prepareStatus: 30, hasMatchingActiveOrderItem: false }) ).toBe(false); expect( shouldDropStalePrepOnSupplierSwitch({ prepareStatus: 30, hasMatchingActiveOrderItem: false, inboundForThisSupplier: false, }) ).toBe(true); expect( shouldDropStalePrepOnSupplierSwitch({ prepareStatus: 30, hasMatchingActiveOrderItem: false, inboundForThisSupplier: true, }) ).toBe(false); const query = vi.fn(async (sql: string) => { const key = String(sql).replace(/\s+/g, ' '); if (key.includes('AND sp.supplier_code IS DISTINCT FROM $3')) { return { rows: [ { id: 'aaaaaaaa-1111-1111-1111-111111111111', sku_code: 'SKU_GARLIC', supplier_code: 'OLD_SUP', prepare_status: 30, inbound_for_supplier: false, }, ], }; } return { rows: [], rowCount: 1 }; }); const deleted = await dropStalePendingPreparationsForSku( { query } as any, { orderCode: 'ORD1', skuCode: 'SKU_GARLIC', keepSupplierCode: 'NEW_SUP' } ); expect(deleted).toBe(1); const sqls = query.mock.calls.map((c) => String(c[0])); expect(sqls.some((s) => s.includes('DELETE FROM supplier_preparations'))).toBe(true); expect(sqls.some((s) => s.includes('inbound_for_supplier') || s.includes('inbound_batch_no'))).toBe(true); expect( sqls.some( (s) => s.includes('DELETE FROM inspections i') && s.includes('order_items oi') && s.includes('inbound_batch_no') ) ).toBe(true); const inboundKeepQuery = vi.fn(async (sql: string) => { const key = String(sql).replace(/\s+/g, ' '); if (key.includes('AND sp.supplier_code IS DISTINCT FROM $3')) { return { rows: [ { id: 'bbbbbbbb-1111-1111-1111-111111111111', sku_code: 'SKU_GARLIC', supplier_code: 'OLD_SUP', prepare_status: 30, inbound_for_supplier: true, }, ], }; } return { rows: [], rowCount: 1 }; }); const inboundKept = await dropStalePendingPreparationsForSku( { query: inboundKeepQuery } as any, { orderCode: 'ORD1', skuCode: 'SKU_GARLIC', keepSupplierCode: 'NEW_SUP' } ); expect(inboundKept).toBe(0); expect( inboundKeepQuery.mock.calls.some((c) => String(c[0]).includes('DELETE FROM supplier_preparations')) ).toBe(false); }); it('rejects transaction commit when any item is uncovered', async () => { const query = vi.fn().mockResolvedValue({ rowCount: 1, rows: [{ id: 'item-1', item_name: '土豆', sku_code: 'SKU1' }], }); await expect( assertOrderItemsFulfilled({ query } as any, 'ORD1') ).rejects.toMatchObject({ code: 'order_item_fulfillment_incomplete', status: 409, }); }); });