Files
syops/frontend/src/features/chat/page/useChatPageConversation.spec.ts
T
hugo 1f3ecb87b7 feat(shipping_notice): 新增货代订单全流程业务模块及配套基建
本次提交新增货代订单处理全流程的完整业务能力、数据库迁移与测试用例:
1.  统一追加式审计事件体系,支持全链路不可篡改的业务追溯
2.  货代任务撤销、版本替代管控逻辑,规范已上传订单的合规版本更替
3.  订单抽取路由审计、模板自动冻结资格、路由计数等核心业务基建
4.  EML订单补充表解析契约与合并逻辑,完善订单补全能力
5.  Chatflow只读能力令牌校验机制,保障工具调用安全
6.  人工复核证据校验服务,规范人工审核的证据留存
所有变更均配套完整的单元测试与迁移契约测试,支持受控回退
2026-07-28 08:56:58 +08:00

167 lines
6.1 KiB
TypeScript

/**
* 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<string | null>('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<typeof ref<string | null>>,
startChatTurn: ReturnType<typeof vi.fn>,
): 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<string, unknown> })?.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<string, unknown> })?.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<string, unknown> })?.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<string, unknown> })?.payload?.idempotency_key;
expect(firstKey).toBeTruthy();
expect(secondKey).toBeTruthy();
expect(secondKey).not.toBe(firstKey);
});
});