169 lines
6.6 KiB
TypeScript
169 lines
6.6 KiB
TypeScript
/**
|
|
* 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: {
|
|
t: ((key: string, params?: Record<string, unknown>) => `${key}${params ? JSON.stringify(params) : ''}`),
|
|
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.at(-1);
|
|
}
|
|
|
|
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('explicitly retries only the selected failed attachment', async () => {
|
|
const api = createAttachments();
|
|
const first = selectLocalFile(api, 'first.pdf');
|
|
const second = selectLocalFile(api, 'second.pdf');
|
|
if (first?.kind === 'local') first.uploadStatus = 'failed';
|
|
if (second?.kind === 'local') {
|
|
second.uploadStatus = 'uploaded';
|
|
second.uploadFileId = 'upload-second';
|
|
}
|
|
uploadChatFileMock.mockResolvedValueOnce({ id: 'upload-first-retry' });
|
|
|
|
await expect(api.retryAttachment(first?.id || '')).resolves.toBe(true);
|
|
|
|
expect(uploadChatFileMock).toHaveBeenCalledOnce();
|
|
expect(uploadChatFileMock).toHaveBeenCalledWith(7, first && 'file' in first ? first.file : undefined);
|
|
expect(first).toMatchObject({ uploadStatus: 'uploaded', uploadFileId: 'upload-first-retry' });
|
|
expect(second).toMatchObject({ uploadStatus: 'uploaded', uploadFileId: 'upload-second' });
|
|
});
|
|
|
|
it('keeps explicit retry failures available for another retry', async () => {
|
|
const api = createAttachments();
|
|
const attachment = selectLocalFile(api);
|
|
if (attachment?.kind === 'local') attachment.uploadStatus = 'failed';
|
|
uploadChatFileMock.mockRejectedValueOnce(new Error('still unavailable'));
|
|
|
|
await expect(api.retryAttachment(attachment?.id || '')).resolves.toBe(false);
|
|
|
|
expect(attachment).toMatchObject({
|
|
uploadStatus: 'failed',
|
|
uploadProgress: 0,
|
|
errorMessage: 'still unavailable',
|
|
});
|
|
expect(api.attachments.value).toHaveLength(1);
|
|
});
|
|
|
|
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);
|
|
});
|
|
});
|