/** * features/chat/shipping/useChatShippingSendHandler.spec.ts * 用途:验证阶段 2 发送语义,只冻结当前输入框这批 Intake,并为仅附件发送补充用户可见的默认请求。 */ import { computed, ref } from 'vue'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { createShippingSendHandlers, type ShippingSendHandlerContext } from './useChatShippingSendHandler'; const createContext = ( overrides: Partial = {}, ): ShippingSendHandlerContext => ({ isShippingNoticeAgent: computed(() => true), isThinking: ref(false), isUploadingAttachments: ref(false), messages: ref([]), conversationId: ref('conv-send'), history: ref([]), input: ref(''), attachments: ref([]), shippingAutoFlowRequested: ref(true), resolvedShippingTaskId: ref(''), currentAgentId: computed(() => 1), selectedKb: ref(''), attachmentPolicy: computed(() => ({ anyEnabled: true })), getActiveShippingTaskId: () => '', lastAssistantMessageContent: () => '', lastAssistantShippingActions: () => [], sendMessage: vi.fn(async () => ({ ok: true } as never)), buildFileSpecsForSend: vi.fn(async () => []), loadHistory: vi.fn(async () => undefined), sendShippingMenuMessage: vi.fn(async () => undefined), startChatTurn: vi.fn(async () => true), openShippingInventoryPicker: vi.fn(() => true), executeShippingDecision: vi.fn(async () => true), executeShippingManualUpload: vi.fn(async () => true), handleShippingMenuAction: vi.fn(async () => undefined), handleShippingAutoUploadFile: vi.fn(async () => undefined), handleShippingAutoUploadNoticeBatch: vi.fn(async () => undefined), isShippingNoticeBatchSubmitting: () => false, hasBlockingNoticeIntake: () => false, hasNoticeIntakeFailures: () => false, hasBlockingNoticeDuplicates: () => false, hasCandidateReviewIssues: () => false, hasSelectedNoticeCandidates: () => false, openShippingDuplicateDialog: vi.fn(), getParsedIntakeNos: () => [], startOrderExtractionRun: vi.fn(async () => true), applyShippingMessageRecovery: vi.fn(async () => false), syncShippingConversationWorkspace: vi.fn(async () => true), refreshShippingActions: vi.fn(async () => undefined), shippingSuggestedPrompt: computed(() => ''), isShippingShortageQueryIntent: () => false, queryShippingShortageDetails: vi.fn(async () => true), msg: { warning: vi.fn(), info: vi.fn() }, t: (key: string) => key, ...overrides, }); describe('useChatShippingSendHandler', () => { beforeEach(() => { vi.clearAllMocks(); }); it('blocks send while current notice attachments are still previewing', async () => { const ctx = createContext({ attachments: ref([{ id: 'notice-waiting', kind: 'shipping_notice', type: 'document', uploadStatus: 'uploaded', parseStatus: 'parsing', uploadProgress: 100, originalFilename: 'orders.eml', fileSize: 100, intakeNo: 'SNI-WAITING', }]), hasBlockingNoticeIntake: () => true, }); const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx); const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', ''); expect(consumed).toBe(true); expect(ctx.startOrderExtractionRun).not.toHaveBeenCalled(); expect(ctx.msg.info).toHaveBeenCalledWith('chat.shippingNoticeWaitingForParse'); }); it('starts extraction only for the current parsed intake batch and removes only submitted notice attachments', async () => { const currentNotice = { id: 'notice-current', kind: 'shipping_notice' as const, type: 'document' as const, uploadStatus: 'uploaded' as const, parseStatus: 'parsed' as const, orderExtractionStatus: 'not_started' as const, uploadProgress: 100, originalFilename: 'orders-current.eml', fileSize: 100, intakeNo: 'SNI-CURRENT', uploadBatchId: 'batch-current', }; const laterNotice = { id: 'notice-later', kind: 'shipping_notice' as const, type: 'document' as const, uploadStatus: 'uploaded' as const, parseStatus: 'parsed' as const, orderExtractionStatus: 'not_started' as const, uploadProgress: 100, originalFilename: 'orders-later.eml', fileSize: 120, intakeNo: 'SNI-LATER', uploadBatchId: 'batch-later', }; const otherAttachment = { id: 'local-note', kind: 'local' as const, file: new File(['inventory'], 'inventory.xlsx', { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }), type: 'document' as const, uploadStatus: 'waiting' as const, uploadProgress: 0, }; const attachments = ref([currentNotice, laterNotice, otherAttachment]); const startOrderExtractionRun = vi.fn(async () => true); const ctx = createContext({ attachments, getParsedIntakeNos: () => ['SNI-CURRENT'], startOrderExtractionRun, }); const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx); const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '请优先处理当前批次', '请优先处理当前批次'); expect(consumed).toBe(true); expect(startOrderExtractionRun).toHaveBeenCalledWith( '请优先处理当前批次', ['SNI-CURRENT'], [{ name: 'orders-current.eml', size: 100, extension: 'eml', type: 'document', source: 'shipping_notice', }], 'batch-current', ); expect(attachments.value).toEqual([laterNotice, otherAttachment]); }); it('adds a visible default request when the user only sends attachments', async () => { const startOrderExtractionRun = vi.fn(async () => true); const ctx = createContext({ attachments: ref([{ id: 'notice-empty-note', kind: 'shipping_notice', type: 'document', uploadStatus: 'uploaded', parseStatus: 'parsed', orderExtractionStatus: 'not_started', uploadProgress: 100, originalFilename: 'orders.eml', fileSize: 100, intakeNo: 'SNI-EMPTY-NOTE', uploadBatchId: 'batch-empty-note', }]), getParsedIntakeNos: () => ['SNI-EMPTY-NOTE'], startOrderExtractionRun, }); const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx); const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', ''); expect(consumed).toBe(true); expect(startOrderExtractionRun).toHaveBeenCalledWith( '', ['SNI-EMPTY-NOTE'], [{ name: 'orders.eml', size: 100, extension: 'eml', type: 'document', source: 'shipping_notice', }], 'batch-empty-note', ); }); it('submits ready intakes from separate upload selections in one run', async () => { const attachments = ref([ { id: 'notice-first', kind: 'shipping_notice' as const, type: 'document' as const, uploadStatus: 'uploaded' as const, parseStatus: 'parsed' as const, orderExtractionStatus: 'not_started' as const, uploadProgress: 100, originalFilename: 'first.eml', fileSize: 100, intakeNo: 'SNI-FIRST', uploadBatchId: 'batch-first', }, { id: 'notice-second', kind: 'shipping_notice' as const, type: 'document' as const, uploadStatus: 'uploaded' as const, parseStatus: 'parsed' as const, orderExtractionStatus: 'not_started' as const, uploadProgress: 100, originalFilename: 'second.eml', fileSize: 120, intakeNo: 'SNI-SECOND', uploadBatchId: 'batch-second', }, ]); const startOrderExtractionRun = vi.fn(async () => true); const ctx = createContext({ attachments, getParsedIntakeNos: () => ['SNI-FIRST', 'SNI-SECOND'], startOrderExtractionRun, }); const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx); const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', ''); expect(consumed).toBe(true); expect(startOrderExtractionRun).toHaveBeenCalledWith( '', ['SNI-FIRST', 'SNI-SECOND'], [ { name: 'first.eml', size: 100, extension: 'eml', type: 'document', source: 'shipping_notice' }, { name: 'second.eml', size: 120, extension: 'eml', type: 'document', source: 'shipping_notice' }, ], ); expect(attachments.value).toEqual([]); }); it('keeps attachments in the input when starting the run fails', async () => { const attachment = { id: 'notice-start-failed', kind: 'shipping_notice' as const, type: 'document' as const, uploadStatus: 'uploaded' as const, parseStatus: 'parsed' as const, orderExtractionStatus: 'not_started' as const, uploadProgress: 100, originalFilename: 'orders.eml', fileSize: 100, intakeNo: 'SNI-START-FAILED', }; const attachments = ref([attachment]); const ctx = createContext({ attachments, getParsedIntakeNos: () => ['SNI-START-FAILED'], startOrderExtractionRun: vi.fn(async () => { throw new Error('run create failed'); }), }); const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx); const consumed = await tryHandleShippingAttachmentAutoFlow(undefined, '', ''); expect(consumed).toBe(true); expect(attachments.value).toEqual([attachment]); expect(ctx.msg.warning).toHaveBeenCalledWith('run create failed'); }); it('blocks order extraction when notice attachments exceed chat snapshot limit', async () => { const many = Array.from({ length: 201 }, (_, i) => ({ id: `notice-${i}`, kind: 'shipping_notice' as const, type: 'document' as const, uploadStatus: 'uploaded' as const, parseStatus: 'parsed' as const, uploadProgress: 100, originalFilename: `orders-${i}.eml`, fileSize: 100, intakeNo: `SNI-${i}`, })); const startOrderExtractionRun = vi.fn(async () => true); const ctx = createContext({ attachments: ref(many), getParsedIntakeNos: () => many.map((item) => item.intakeNo), startOrderExtractionRun, }); const { tryHandleShippingAttachmentAutoFlow } = createShippingSendHandlers(ctx); const handled = await tryHandleShippingAttachmentAutoFlow(undefined, '', ''); expect(handled).toBe(true); expect(startOrderExtractionRun).not.toHaveBeenCalled(); expect(ctx.msg.warning).toHaveBeenCalled(); }); it('warns and consumes send when user tries batch task switching (co8 batch API retired)', async () => { const ctx = createContext({ }); const { tryHandleShippingSend } = createShippingSendHandlers(ctx); const consumed = await tryHandleShippingSend(undefined, '下一票', '下一票'); expect(consumed).toBe(true); expect(ctx.startChatTurn).not.toHaveBeenCalled(); expect(ctx.msg.warning).toHaveBeenCalledWith('chat.shippingNoticeUi.batchSwitchRemoved'); }); });