/** * features/chat/page/useChatPageConversation.spec.ts * 用途:验证新建聊天与 Agent 切换时清理发货通知待重试建批幂等状态。 * 依赖:vitest、vue refs/computed、useChatPageConversation、useChatShippingUploadHandler。 * 逻辑:覆盖 createNewChatView 不经 onConversationIdChanging 时仍生成新幂等键的回归场景。 */ import { computed, ref } from 'vue'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { createChatPageConversationApi } from './useChatPageConversation'; import { clearPendingNoticeBatchRetry, createShippingUploadHandlers, resetShippingNoticeBatchSubmitStateForTests, type ShippingUploadHandlerContext, } from '../shipping/useChatShippingUploadHandler'; const { listChatMessages } = vi.hoisted(() => ({ listChatMessages: vi.fn(), })); const { computeNoticeFilesContentFingerprint } = vi.hoisted(() => ({ computeNoticeFilesContentFingerprint: vi.fn(async (files: File[]) => { const content = await files[0]?.text(); return `fp-${content || 'empty'}`; }), })); vi.mock('../api/chat.api', () => ({ listChatMessages, })); vi.mock('../shipping/shippingNoticeBatchIdempotency', () => ({ computeNoticeFilesContentFingerprint, })); const createConversationContext = () => { const conversationId = ref('conv-a'); const messages = ref([]); const createNewChatView = vi.fn(() => { conversationId.value = null; messages.value = []; }); return { conversationId, messages, createNewChatView, api: createChatPageConversationApi({ deps: { t: (key: string) => key, msg: { warning: vi.fn() } }, route: { path: '/chat/42', query: {} } as never, router: { replace: vi.fn() } as never, currentAgentId: computed(() => 42), conversationId, activeRunId: ref(null), messages, isThinking: ref(false), loadConversationState: vi.fn(), createNewChatView, clearPendingNoticeBatchRetry, onConversationIdChanging: vi.fn(), onConversationLoadedEmpty: vi.fn(async () => undefined), onConversationLoadFailed: vi.fn(async () => undefined), isShippingNoticeAgent: computed(() => true), shippingTaskIdFromQuery: computed(() => ''), shippingBatchNoFromQuery: computed(() => ''), clearHistory: vi.fn(), clearAttachments: vi.fn(), input: ref(''), resetCurrentAgent: vi.fn(), copiedIndex: ref(null), resetPeripheralState: vi.fn(), filePreviewVisible: ref(false), previewFile: ref(null), previewFiles: ref([]), currentPreviewIndex: ref(0), }), }; }; const createUploadContext = ( conversationId: ReturnType>, startChatTurn: ReturnType, ): ShippingUploadHandlerContext => ({ conversationId, currentAgentId: computed(() => 42), attachments: ref([]), input: ref(''), shippingAutoFlowRequested: ref(false), shippingInventoryPickerRef: ref({ click: vi.fn() } as unknown as HTMLInputElement), shippingNoticePickerRef: ref({ click: vi.fn() } as unknown as HTMLInputElement), pendingShippingUploadTaskId: ref(''), pendingShippingNoticeTaskId: ref(''), pendingShippingInventoryFile: ref(null), resolvedShippingTaskId: ref(''), resolvedShippingBatchNo: ref(''), shippingBatchSummary: ref(null), shippingBatchTasks: ref([]), shippingInventorySelectionActive: ref(false), shippingInventorySelectionTaskId: ref(''), shippingInventorySchemaPreviewCache: ref({}), getActiveShippingTaskId: () => '', startChatTurn, refreshShippingActions: vi.fn(async () => undefined), msg: { warning: vi.fn() }, t: (key: string) => key, }); describe('useChatPageConversation', () => { beforeEach(() => { listChatMessages.mockReset(); computeNoticeFilesContentFingerprint.mockReset(); computeNoticeFilesContentFingerprint.mockImplementation(async (files: File[]) => { const content = await files[0]?.text(); return `fp-${content || 'empty'}`; }); resetShippingNoticeBatchSubmitStateForTests(); }); it('startNewChat clears pending notice batch retry and next upload uses a new idempotency key', async () => { const startChatTurn = vi.fn(async () => false); const { conversationId, createNewChatView, api } = createConversationContext(); const { handleShippingAutoUploadNoticeBatch } = createShippingUploadHandlers( createUploadContext(conversationId, startChatTurn), ); const file = new File(['notice'], 'notice.pdf', { type: 'application/pdf' }); await handleShippingAutoUploadNoticeBatch([file]); const firstKey = (startChatTurn.mock.calls[0]?.[0] as { payload?: Record })?.payload?.idempotency_key; api.startNewChat(); expect(createNewChatView).toHaveBeenCalledTimes(1); expect(conversationId.value).toBeNull(); await handleShippingAutoUploadNoticeBatch([file]); const secondKey = (startChatTurn.mock.calls[1]?.[0] as { payload?: Record })?.payload?.idempotency_key; expect(firstKey).toBeTruthy(); expect(secondKey).toBeTruthy(); expect(secondKey).not.toBe(firstKey); }); it('resetChatStateForAgentChange clears pending notice batch retry and next upload uses a new idempotency key', async () => { const startChatTurn = vi.fn(async () => false); const { conversationId, createNewChatView, api } = createConversationContext(); const { handleShippingAutoUploadNoticeBatch } = createShippingUploadHandlers( createUploadContext(conversationId, startChatTurn), ); const file = new File(['notice'], 'notice.pdf', { type: 'application/pdf' }); await handleShippingAutoUploadNoticeBatch([file]); const firstKey = (startChatTurn.mock.calls[0]?.[0] as { payload?: Record })?.payload?.idempotency_key; api.resetChatStateForAgentChange(); expect(createNewChatView).toHaveBeenCalledTimes(1); expect(conversationId.value).toBeNull(); await handleShippingAutoUploadNoticeBatch([file]); const secondKey = (startChatTurn.mock.calls[1]?.[0] as { payload?: Record })?.payload?.idempotency_key; expect(firstKey).toBeTruthy(); expect(secondKey).toBeTruthy(); expect(secondKey).not.toBe(firstKey); }); });