import { describe, expect, it, vi } from 'vitest'; import { createLearningSpeechClient } from '@electron/services/learning-speech-client'; describe('Learning speech client', () => { it('keeps authentication in Main and forwards bounded multipart audio', async () => { const fetchImpl = vi.fn().mockResolvedValue(new Response( JSON.stringify({ text: '什么是变量', model: 'stt' }), { status: 200, headers: { 'Content-Type': 'application/json' } }, )); const client = createLearningSpeechClient({ fetchImpl, getAccessToken: vi.fn().mockResolvedValue('main-owned-token'), apiBaseUrl: 'https://square.example', }); await expect(client.transcribe({ audio: new Uint8Array([1, 2, 3]), fileName: 'voice.webm', mimeType: 'audio/webm', language: 'zh-CN', })).resolves.toEqual({ text: '什么是变量' }); const [url, init] = fetchImpl.mock.calls[0]; expect(url).toBe('https://square.example/api/speech/transcriptions'); expect(init?.headers).toEqual({ Authorization: 'Bearer main-owned-token' }); const form = init?.body as FormData; expect(form.get('language')).toBe('zh-CN'); const file = form.get('audio') as File; expect(file.name).toBe('voice.webm'); expect(new Uint8Array(await file.arrayBuffer())).toEqual(new Uint8Array([1, 2, 3])); }); });