62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
/* @vitest-environment jsdom */
|
|
|
|
import { afterEach, expect, test, vi } from 'vitest';
|
|
|
|
import {
|
|
parseCreationAgentDocumentInput,
|
|
validateCreationAgentDocumentInputFile,
|
|
} from './creationAgentDocumentInput';
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
test('creation agent document input validation accepts supported text documents', () => {
|
|
expect(() => {
|
|
validateCreationAgentDocumentInputFile(
|
|
new File(['世界设定'], '世界设定.MD', { type: 'text/markdown' }),
|
|
);
|
|
}).not.toThrow();
|
|
});
|
|
|
|
test('creation agent document input validation accepts docx documents', () => {
|
|
expect(() => {
|
|
validateCreationAgentDocumentInputFile(
|
|
new File(['binary'], '世界设定.docx', {
|
|
type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
|
|
}),
|
|
);
|
|
}).not.toThrow();
|
|
});
|
|
|
|
test('creation agent document input validation rejects unsupported documents', () => {
|
|
expect(() => {
|
|
validateCreationAgentDocumentInputFile(
|
|
new File(['binary'], '世界设定.pdf', {
|
|
type: 'application/pdf',
|
|
}),
|
|
);
|
|
}).toThrow('暂时只支持 txt、md、docx、csv、json 文档。');
|
|
});
|
|
|
|
test('creation agent document input validation rejects oversized documents', () => {
|
|
const oversizedContent = new Uint8Array(256 * 1024 + 1);
|
|
|
|
expect(() => {
|
|
validateCreationAgentDocumentInputFile(
|
|
new File([oversizedContent], '世界设定.txt', { type: 'text/plain' }),
|
|
);
|
|
}).toThrow('文档过大,请上传 256KB 以内的文本文件。');
|
|
});
|
|
|
|
test('creation agent document input parse skips network for unsupported files', async () => {
|
|
const fetchSpy = vi.fn();
|
|
vi.stubGlobal('fetch', fetchSpy);
|
|
|
|
await expect(
|
|
parseCreationAgentDocumentInput(new File(['binary'], '世界设定.pdf')),
|
|
).rejects.toThrow('暂时只支持 txt、md、docx、csv、json 文档。');
|
|
|
|
expect(fetchSpy).not.toHaveBeenCalled();
|
|
});
|