feat(shipping-notice): 新增货代客户管理功能,支持客户导入、识别证据管理与客户确认
This commit is contained in:
1 parent
b595e157e9
commit
4fbaed2b4c
176 files changed
+14011
-994
No files matched your search
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* features/chat/composables/useChatAttachments.spec.ts
|
||||
* 用途:验证普通聊天附件的逐文件上传状态、成功缓存、失败重试与删除保护。
|
||||
* 依赖:Vitest、Vue 响应式 API、mock 后的 chat.api。
|
||||
* 逻辑:直接调用 useChatAttachments,断言附件状态与生成的 Dify 文件参数保持一致。
|
||||
*/
|
||||
import { computed, ref } from 'vue';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { uploadChatFile } from '../api/chat.api';
|
||||
import { useChatAttachments } from './useChatAttachments';
|
||||
import type { ChatDeps } from './chat.deps';
|
||||
|
||||
vi.mock('../api/chat.api', () => ({
|
||||
uploadChatFile: vi.fn(),
|
||||
}));
|
||||
|
||||
const uploadChatFileMock = vi.mocked(uploadChatFile);
|
||||
|
||||
/** 创建满足 composable 依赖的最小测试上下文。 */
|
||||
function createAttachments() {
|
||||
return useChatAttachments({
|
||||
deps: {
|
||||
msg: { warning: vi.fn() },
|
||||
} as unknown as ChatDeps,
|
||||
currentAgentId: computed(() => 7),
|
||||
currentAgent: ref({
|
||||
capabilities: { file_upload: true },
|
||||
dify_parameters: {
|
||||
file_upload: {
|
||||
document: { enabled: true, transfer_methods: ['local_file'] },
|
||||
},
|
||||
},
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
/** 向附件状态中加入一个普通本地文件。 */
|
||||
function selectLocalFile(api: ReturnType<typeof createAttachments>, name = 'order.pdf') {
|
||||
const file = new File(['content'], name, { type: 'application/pdf' });
|
||||
api.handleLocalFilesSelected({ target: { files: [file], value: 'selected' } } as unknown as Event);
|
||||
return api.attachments.value[0];
|
||||
}
|
||||
|
||||
describe('useChatAttachments upload states', () => {
|
||||
beforeEach(() => {
|
||||
uploadChatFileMock.mockReset();
|
||||
});
|
||||
|
||||
it('marks a successful local upload ready and reuses its upload id', async () => {
|
||||
const api = createAttachments();
|
||||
const attachment = selectLocalFile(api);
|
||||
uploadChatFileMock.mockResolvedValue({ id: 'upload-1' });
|
||||
|
||||
const firstSpecs = await api.buildFileSpecsForSend();
|
||||
const secondSpecs = await api.buildFileSpecsForSend();
|
||||
|
||||
expect(attachment).toMatchObject({
|
||||
kind: 'local',
|
||||
uploadStatus: 'uploaded',
|
||||
uploadProgress: 100,
|
||||
uploadFileId: 'upload-1',
|
||||
});
|
||||
expect(firstSpecs).toEqual([{ type: 'document', transfer_method: 'local_file', upload_file_id: 'upload-1' }]);
|
||||
expect(secondSpecs).toEqual(firstSpecs);
|
||||
expect(uploadChatFileMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps a failed attachment and retries it on the next send', async () => {
|
||||
const api = createAttachments();
|
||||
const attachment = selectLocalFile(api);
|
||||
uploadChatFileMock
|
||||
.mockRejectedValueOnce(new Error('network unavailable'))
|
||||
.mockResolvedValueOnce({ id: 'upload-retry' });
|
||||
|
||||
await expect(api.buildFileSpecsForSend()).rejects.toThrow('network unavailable');
|
||||
expect(attachment).toMatchObject({
|
||||
uploadStatus: 'failed',
|
||||
uploadProgress: 0,
|
||||
errorMessage: 'network unavailable',
|
||||
});
|
||||
|
||||
await expect(api.buildFileSpecsForSend()).resolves.toEqual([
|
||||
{ type: 'document', transfer_method: 'local_file', upload_file_id: 'upload-retry' },
|
||||
]);
|
||||
expect(attachment).toMatchObject({ uploadStatus: 'uploaded', uploadFileId: 'upload-retry' });
|
||||
expect(attachment && 'errorMessage' in attachment ? attachment.errorMessage : undefined).toBeUndefined();
|
||||
});
|
||||
|
||||
it('reuses earlier successful files when a later file failed', async () => {
|
||||
const api = createAttachments();
|
||||
selectLocalFile(api, 'first.pdf');
|
||||
selectLocalFile(api, 'second.pdf');
|
||||
uploadChatFileMock
|
||||
.mockResolvedValueOnce({ id: 'upload-first' })
|
||||
.mockRejectedValueOnce(new Error('second failed'))
|
||||
.mockResolvedValueOnce({ id: 'upload-second' });
|
||||
|
||||
await expect(api.buildFileSpecsForSend()).rejects.toThrow('second failed');
|
||||
expect(api.attachments.value[0]).toMatchObject({
|
||||
uploadStatus: 'uploaded',
|
||||
uploadFileId: 'upload-first',
|
||||
});
|
||||
expect(api.attachments.value[1]).toMatchObject({ uploadStatus: 'failed' });
|
||||
|
||||
await expect(api.buildFileSpecsForSend()).resolves.toEqual([
|
||||
{ type: 'document', transfer_method: 'local_file', upload_file_id: 'upload-first' },
|
||||
{ type: 'document', transfer_method: 'local_file', upload_file_id: 'upload-second' },
|
||||
]);
|
||||
expect(uploadChatFileMock).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('does not remove a local attachment while its request is uploading', async () => {
|
||||
const api = createAttachments();
|
||||
const attachment = selectLocalFile(api);
|
||||
let resolveUpload: ((value: { id: string }) => void) | undefined;
|
||||
uploadChatFileMock.mockImplementation(() => new Promise((resolve) => {
|
||||
resolveUpload = resolve;
|
||||
}));
|
||||
|
||||
const buildPromise = api.buildFileSpecsForSend();
|
||||
await Promise.resolve();
|
||||
expect(attachment && 'uploadStatus' in attachment ? attachment.uploadStatus : undefined).toBe('uploading');
|
||||
|
||||
api.removeAttachment(attachment?.id || '');
|
||||
expect(api.attachments.value).toHaveLength(1);
|
||||
|
||||
resolveUpload?.({ id: 'upload-finished' });
|
||||
await buildPromise;
|
||||
api.removeAttachment(attachment?.id || '');
|
||||
expect(api.attachments.value).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user