71 lines
2.7 KiB
TypeScript
71 lines
2.7 KiB
TypeScript
// @vitest-environment node
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { createLearningGenerationClient } from '@electron/services/learning-generation-client';
|
|
import type { LearningGenerationOptions } from '../../shared/learning';
|
|
|
|
describe('Learning generation Main client', () => {
|
|
const fetchImpl = vi.fn<typeof fetch>();
|
|
const getAccessToken = vi.fn();
|
|
const options: LearningGenerationOptions = {
|
|
requirement: '用案例教我二次函数',
|
|
enableWebSearch: true,
|
|
enableImageGeneration: false,
|
|
enableVideoGeneration: false,
|
|
enableTTS: true,
|
|
interactiveMode: true,
|
|
taskEngineMode: false,
|
|
};
|
|
|
|
beforeEach(() => {
|
|
fetchImpl.mockReset();
|
|
getAccessToken.mockReset();
|
|
getAccessToken.mockResolvedValue('works-token');
|
|
fetchImpl.mockResolvedValue(new Response(JSON.stringify({
|
|
contractVersion: 2,
|
|
jobId: 'job-1',
|
|
status: 'queued',
|
|
mode: 'single',
|
|
done: false,
|
|
}), { status: 202, headers: { 'Content-Type': 'application/json' } }));
|
|
});
|
|
|
|
it('uses canonical JSON when there are no materials', async () => {
|
|
const client = createLearningGenerationClient({ fetchImpl, getAccessToken, apiBaseUrl: 'https://square.example' });
|
|
|
|
await client.start({ options, materials: [] });
|
|
|
|
expect(fetchImpl).toHaveBeenCalledWith(
|
|
'https://square.example/api/learning/generations',
|
|
expect.objectContaining({
|
|
method: 'POST',
|
|
headers: expect.objectContaining({
|
|
Authorization: 'Bearer works-token',
|
|
'Content-Type': 'application/json',
|
|
}),
|
|
body: JSON.stringify(options),
|
|
}),
|
|
);
|
|
});
|
|
|
|
it('uses options JSON plus repeated material parts in selected order', async () => {
|
|
const client = createLearningGenerationClient({ fetchImpl, getAccessToken, apiBaseUrl: 'https://square.example' });
|
|
|
|
await client.start({
|
|
options,
|
|
materials: [
|
|
{ id: 'b', name: '第二份.txt', mimeType: 'text/plain', size: 3, lastModified: 2, order: 2, bytes: new Uint8Array([4, 5, 6]) },
|
|
{ id: 'a', name: '第一份.md', mimeType: 'text/markdown', size: 3, lastModified: 1, order: 1, bytes: new Uint8Array([1, 2, 3]) },
|
|
],
|
|
});
|
|
|
|
const init = fetchImpl.mock.calls[0][1]!;
|
|
expect(init.headers).toEqual({ Accept: 'application/json', Authorization: 'Bearer works-token' });
|
|
expect(init.body).toBeInstanceOf(FormData);
|
|
const form = init.body as FormData;
|
|
expect(JSON.parse(String(form.get('options')))).toEqual(options);
|
|
const materials = form.getAll('materials') as File[];
|
|
expect(materials.map((file) => file.name)).toEqual(['第一份.md', '第二份.txt']);
|
|
expect(Array.from(new Uint8Array(await materials[0].arrayBuffer()))).toEqual([1, 2, 3]);
|
|
});
|
|
});
|