feat: integrate learning module
Some checks failed
Electron E2E / Electron E2E (macos-latest) (push) Has been cancelled
Electron E2E / Electron E2E (ubuntu-latest) (push) Has been cancelled
Electron E2E / Electron E2E (windows-latest) (push) Has been cancelled

This commit is contained in:
inman
2026-08-16 20:52:16 +08:00
parent 26b52d76e3
commit 01bee3188b
107 changed files with 6318 additions and 12063 deletions

View File

@@ -0,0 +1,70 @@
// @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]);
});
});