import { describe, expect, it, vi } from 'vitest'; /** * 2026-11-07 事故回归(主系统 a2b8df4d 移植): * 同订单 + 同 SKU + 同加工方式只允许一条 order 加工任务。 * * 旧逻辑只按 inspection_id 查重:同一订单同 SKU 有两张验收单时,第二张会再 INSERT 一条 * source_type='order' 的加工任务(task_code=NULL、界面出现两颗待加工行)。 */ const calls: Array<{ text: string; params: unknown[] }> = []; let existingOrderTask: { id: string } | null = { id: 'existing-order-task' }; vi.mock('../src/db', () => { const q = async (text: string, params: unknown[] = []) => { calls.push({ text: String(text), params }); const sql = String(text); // 工作流开关(system_configs) if (sql.includes('system_configs')) return { rows: [{ config_value: { enabled: true } }], rowCount: 1 }; // resolveProcessingContextForInspection:订单行工艺 if (sql.includes('FROM order_items oi') && sql.includes('processing_method')) { return { rows: [{ processing_method: '去皮', processing_owner: 'warehouse', process_unit_price: 5 }], rowCount: 1 }; } // orderSkuHasWarehouseDemand:无需求行 if (sql.includes('processing_demand_items')) return { rows: [], rowCount: 0 }; // 旧查重:按 inspection_id —— 第二张验收单查不到 if (sql.includes('WHERE inspection_id = $1')) return { rows: [], rowCount: 0 }; // 新守卫:同 order + sku + method + source_type='order' + status<50 if (sql.includes("source_type = 'order'") && sql.includes('processing_method = $3')) { return { rows: existingOrderTask ? [existingOrderTask] : [], rowCount: existingOrderTask ? 1 : 0 }; } if (sql.includes('UPDATE processing_tasks')) return { rows: [], rowCount: 1 }; return { rows: [], rowCount: 0 }; }; return { pool: { query: q, connect: vi.fn() }, query: q, getRawPoolQuery: () => q, getRawPoolConnect: vi.fn(), }; }); const { ensureProcessingTaskForInspection } = await import('../src/services/processingTaskService'); const BASE_INPUT = { inspectionId: 'insp-second', orderCode: 'ORD_X', skuCode: 'SKU86017523428', processingDetails: { processing_method: '去皮', processing_owner: 'warehouse' }, prepareQty: 15, itemName: '土豆', branchCode: 'BR002', sourceQty: 15, }; describe('ensureProcessingTaskForInspection 一商品一加工单', () => { it('同订单+同SKU+同加工方式已有 order 任务 ⇒ 复用(更新)并返回 false,不再 INSERT', async () => { existingOrderTask = { id: 'existing-order-task' }; calls.length = 0; const created = await ensureProcessingTaskForInspection(BASE_INPUT, { query: async (t: string, p: unknown[] = []) => { // 复用同一 mock 逻辑(通过动态 import 的 db mock 不可直接拿 q,这里重新实现最小集) const sql = String(t); calls.push({ text: sql, params: p }); if (sql.includes('system_configs')) return { rows: [{ config_value: { enabled: true } }], rowCount: 1 } as never; if (sql.includes('FROM order_items oi')) return { rows: [{ processing_method: '去皮', processing_owner: 'warehouse', process_unit_price: 5 }], rowCount: 1 } as never; if (sql.includes('processing_demand_items')) return { rows: [], rowCount: 0 } as never; if (sql.includes('WHERE inspection_id = $1')) return { rows: [], rowCount: 0 } as never; if (sql.includes("source_type = 'order'") && sql.includes('processing_method = $3')) return { rows: [existingOrderTask], rowCount: 1 } as never; if (sql.includes('UPDATE processing_tasks')) return { rows: [], rowCount: 1 } as never; return { rows: [], rowCount: 0 } as never; } } as never); expect(created).toBe(false); const sqls = calls.map((c) => c.text); expect(sqls.some((s) => s.includes("source_type = 'order'"))).toBe(true); expect(sqls.some((s) => s.includes('INSERT INTO processing_tasks'))).toBe(false); }); });