/** * features/chat/e2e/in-transit-transfer.spec.ts * 用途:在途仓自动调拨全链路契约冒烟(对应 TDD §10 主路径)—— * 匹配 → 选择子集 → 生成调拨单 → Run 恢复 → 下载留底。 * 依赖:Vitest、vi.mock(transfer.api)、useChatTransfer。 * 逻辑:不启浏览器;用组合式函数 + Mock API 断言产品契约,避免 e2e 环境依赖 * (与仓库 shipping e2e 约定一致)。 */ import { describe, expect, it, vi, beforeEach } from 'vitest'; const api = vi.hoisted(() => ({ createTransferMatching: vi.fn(), getTransferMatching: vi.fn(), executeTransferItems: vi.fn(), getTransferRun: vi.fn(), retryTransferItem: vi.fn(), manualCloseTransferItem: vi.fn(), downloadTransferReceiptFiles: vi.fn(), downloadTransferErpInventory: vi.fn(), downloadTransferExceptions: vi.fn(), })); vi.mock('@/features/chat/api/transfer.api', () => api); import { useChatTransfer } from '@/features/chat/composables/useChatTransfer'; import type { TransferMatchingDetail } from '@/features/chat/types/transfer.types'; const detail: TransferMatchingDetail = { summary: { matching_id: 'MTG-20260806-001', status: 'ready_to_execute', business_date: '2026-08-06', eligible_receipt_date_to: '2026-08-05', expires_at: '', counts: { file_invoice_count: 3, not_matured_count: 1, matched_count: 2, executable_count: 2, blocked_count: 0 }, estimated_transfer_quantity: '2400', estimated_external_write_count: 2, matching_hash: 'sha256:abc', }, sources: [{ source_code: 'DBS-AWSBI', stage: 'file_ready', file_no: 'REC-1', file_version: 'v3' }], items: [ { item_id: 11, source_code: 'DBS-AWSBI', material_no: 'MAT-1', material_name: '连接器组件', specification: 'A', invoice_no: 'INV-001', from_warehouse: 'DBS-TRANSIT', from_quantity: '1200', to_warehouse: 'HUB-CN01', to_quantity: '1200', result: 'executable' }, { item_id: 12, source_code: 'DBS-AWSBI', material_no: 'MAT-2', material_name: '控制器组件', specification: 'B', invoice_no: 'INV-002', from_warehouse: 'DBS-TRANSIT', from_quantity: '1200', to_warehouse: 'HUB-CN01', to_quantity: '1200', result: 'executable' }, { item_id: 13, source_code: 'DBS-AWSSG', material_no: 'MAT-41', material_name: '连接器', specification: 'C', invoice_no: 'INV-041', from_warehouse: '', from_quantity: null, to_warehouse: '', to_quantity: '400', result: 'not_matured' }, ], }; const runResult = { run_no: 'RUN-20260806-001', status: 'success', result: { external_documents: [{ document_no: 'TRF-10001', from_warehouse: 'DBS-TRANSIT', to_warehouse: 'HUB-CN01', material_count: 2, total_quantity: '2400', posting_status: 'posted', lines: [ { material_no: 'MAT-1', material_name: '连接器组件', specification: 'A', invoice_no: 'INV-001', quantity: '1200' }, { material_no: 'MAT-2', material_name: '控制器组件', specification: 'B', invoice_no: 'INV-002', quantity: '1200' }, ], }], exceptions: [], }, }; describe('in-transit-transfer e2e contract', () => { beforeEach(() => { vi.clearAllMocks(); api.createTransferMatching.mockResolvedValue(detail.summary); api.getTransferMatching.mockResolvedValue(detail); api.executeTransferItems.mockResolvedValue({ run_no: 'RUN-20260806-001', status: 'success' }); api.getTransferRun.mockResolvedValue(runResult); api.downloadTransferReceiptFiles.mockResolvedValue({ blob: new Blob(['zip']), filename: 'receipt-files.zip' }); api.downloadTransferErpInventory.mockResolvedValue({ blob: new Blob(['csv']), filename: 'erp.csv' }); }); it('主路径:匹配 → 只选 item 11 → 按明细行生成调拨单 → 结果按 ERP 单号聚合', async () => { const transfer = useChatTransfer(); await transfer.createMatching(['DBS-AWSBI', 'DBS-AWSSG'], '2026-08-06'); // 未成熟项默认不勾选 expect(transfer.selected.value.has(13)).toBe(false); // 取消 item 12,只执行 item 11 transfer.toggleSelect(12, false); await transfer.execute(); expect(api.executeTransferItems).toHaveBeenCalledWith([11]); expect(transfer.run.value?.result.external_documents[0].document_no).toBe('TRF-10001'); }); it('刷新恢复:重建 composable 后按 run_no 恢复最终结果(决策 17)', async () => { const transfer = useChatTransfer(); transfer.runNo.value = 'RUN-20260806-001'; await transfer.refreshRun(); expect(transfer.run.value?.status).toBe('success'); expect(transfer.run.value?.result.external_documents[0].material_count).toBe(2); }); it('下载:收料来源文件与 ERP 库存表(决策 21/22 前端入口)', async () => { const transfer = useChatTransfer(); transfer.runNo.value = 'RUN-20260806-001'; await transfer.downloadReceiptFiles(); await transfer.downloadErpInventory(); expect(api.downloadTransferReceiptFiles).toHaveBeenCalledWith('RUN-20260806-001'); expect(api.downloadTransferErpInventory).toHaveBeenCalledWith('RUN-20260806-001'); }); it('异常处理:人工重试与人工关闭走独立 API', async () => { api.retryTransferItem.mockResolvedValue({ item_id: 1, status: 'ready_to_execute', attempt_no: 2 }); api.manualCloseTransferItem.mockResolvedValue({ item_id: 2, status: 'manually_closed' }); const transfer = useChatTransfer(); await transfer.retry(1); await transfer.manualClose(2, '线下已处理'); expect(api.retryTransferItem).toHaveBeenCalledWith(1); expect(api.manualCloseTransferItem).toHaveBeenCalledWith(2, '线下已处理'); }); });