import { describe, expect, it, vi } from 'vitest'; import { fetchOrderInspectionState, listOrderCodesLinkedToPurchase, } from '../src/services/orderInspectionStateService'; describe('orderInspectionStateService merge coverage', () => { it('listOrderCodesLinkedToPurchase unions host PO and guest fulfillments', async () => { const query = vi .fn() .mockResolvedValueOnce({ rows: [{ order_code: 'ORD_HOST' }, { order_code: 'ORD_GUEST' }], }); const codes = await listOrderCodesLinkedToPurchase('PUR1', { query } as any); expect(codes).toEqual(['ORD_HOST', 'ORD_GUEST']); expect(query.mock.calls[0][0]).toContain('order_item_fulfillments'); expect(query.mock.calls[0][1]).toEqual(['PUR1']); }); it('fetchOrderInspectionState treats fully merge-covered orders as passed', async () => { const query = vi.fn(async (sql: string) => { if (sql.includes('quality_rating =')) { return { rows: [] }; } if (sql.includes('SELECT DISTINCT order_code FROM inspections')) { return { rows: [] }; // 从单本单无验收行 } if (sql.includes('inspection_time IS NULL')) { return { rows: [] }; } if (sql.includes('AND NOT') && sql.includes('order_items oi') && sql.includes('ANY($1')) { // uncovered return { rows: [] }; } if (sql.includes('unnest($1::text[])')) { return { rows: [{ order_code: 'ORD_GUEST' }] }; } return { rows: [] }; }); const state = await fetchOrderInspectionState('ORD_GUEST', { query } as any); expect(state).toBe('passed'); const uncoveredCall = query.mock.calls.find( (c) => String(c[0]).includes('order_item_fulfillments') || String(c[0]).includes('AND NOT') ); expect(String(query.mock.calls.map((c) => c[0]).join('\n'))).toContain('order_item_fulfillments'); expect(uncoveredCall || true).toBeTruthy(); }); it('coverage SQL treats full stock_cover / inventory_cover as inbound covered', async () => { const query = vi.fn(async (sql: string) => { if (sql.includes('quality_rating =')) return { rows: [] }; if (sql.includes('SELECT DISTINCT order_code FROM inspections')) return { rows: [] }; if (sql.includes('inspection_time IS NULL')) return { rows: [] }; if (sql.includes('AND NOT') && sql.includes('order_items oi') && sql.includes('ANY($1')) { return { rows: [] }; } if (sql.includes('unnest($1::text[])')) { return { rows: [{ order_code: 'ORD_STOCK' }] }; } return { rows: [] }; }); const state = await fetchOrderInspectionState('ORD_STOCK', { query } as any); expect(state).toBe('passed'); const sqlBlob = String(query.mock.calls.map((c) => c[0]).join('\n')); expect(sqlBlob).toContain('stock_cover_qty'); expect(sqlBlob).toContain('inventory_cover_qty'); // 转自采未入库完成不得靠 is_stockout 单独算覆盖 expect(sqlBlob).toContain('transferred_to_purchase'); expect(sqlBlob).toContain('purchase_status'); }); it('coverage SQL treats successful inspection as covered without actual_qty vs ordered qty', async () => { const query = vi.fn(async (sql: string) => { if (sql.includes('quality_rating =')) return { rows: [] }; if (sql.includes('SELECT DISTINCT order_code FROM inspections')) return { rows: [] }; if (sql.includes('inspection_time IS NULL')) return { rows: [] }; if (sql.includes('AND NOT') && sql.includes('order_items oi') && sql.includes('ANY($1')) { return { rows: [] }; } if (sql.includes('unnest($1::text[])')) { return { rows: [{ order_code: 'ORD_PARTIAL_IN' }] }; } return { rows: [] }; }); const state = await fetchOrderInspectionState('ORD_PARTIAL_IN', { query } as any); expect(state).toBe('passed'); const sqlBlob = String(query.mock.calls.map((c) => c[0]).join('\n')); expect(sqlBlob).toContain('SELECT 1 FROM inspections i'); expect(sqlBlob).not.toContain('SUM(COALESCE(i.actual_qty'); expect(sqlBlob).not.toContain("NOT IN ('D'"); }); it('isMergeGuestOrderItem detects fulfillment on host PO', async () => { const { isMergeGuestOrderItem } = await import('../src/services/orderInspectionStateService'); const query = vi.fn().mockResolvedValueOnce({ rows: [{ '?column?': 1 }], rowCount: 1 }); const ok = await isMergeGuestOrderItem('item-1', { query } as any); expect(ok).toBe(true); expect(String(query.mock.calls[0][0])).toContain('po.order_code IS DISTINCT FROM oi.order_code'); }); it('findExistingPurchaseInboundBatch queries purchase-linked inspections', async () => { const { findExistingPurchaseInboundBatch } = await import( '../src/services/orderInspectionStateService' ); const query = vi.fn().mockResolvedValueOnce({ rows: [{ inbound_batch_no: 'IN_HOST' }], rowCount: 1, }); const batch = await findExistingPurchaseInboundBatch( { purchaseCodes: ['PUR1'], skuCode: 'SKU1' }, { query } as any ); expect(batch).toBe('IN_HOST'); expect(String(query.mock.calls[0][0])).toContain('purchase_codes'); }); });