Orkas is an open-source (MIT), local-first AI team for your desktop: a Commander turns your goal into to-dos and coordinates 16 built-in specialist agents plus Claude Code, Codex, OpenCode, OpenClaw and Hermes, with projects, scheduled automations and connectors. Free app for macOS & Windows; built-in models or your own keys.
JavaScript agent-collaborationagent-harnessagent-orchestrationagent-teamagent-workflowai-assistantai-automationai-employeeai-productivityai-teamai-workflowautonomous-agentsclaude-code-guiclaude-coworkdesktop-agentlocal-firstmulti-agentmulti-agent-systemself-evolving-agentstask-automation
Merge main and preserve microphone admission and capture ownership
wyt6f99ada514a7
diff --cc src/renderer/modules/voice-input.jsindex 5f8f325d,e6c2ef8a..d58a1449--- a/src/renderer/modules/voice-input.js+++ b/src/renderer/modules/voice-input.js@@@ -136,14 -76,16 +136,22 @@@return;}this._mediaStream = mediaStream;+ this._emit('onStage', 'microphone_ms');let ws;try {- if (typeof this._url === 'function') this._url = await this._url();- if (this._pendingClose) { this.teardownCapture(); return; }- ws = new WebSocket(this.connectUrl());++ const url = typeof this._url === 'function' ? await this._url() : this._url;++ if (epoch !== this._captureEpoch) return;++ if (this._pendingClose) {++ this._teardownCapture();++ return;++ }++ this._url = url;+ ws = new WebSocket(this._connectUrl());} catch (e) {++ if (epoch !== this._captureEpoch) return;try { if (voiceLog) voiceLog.error('ASR websocket constructor failed', _errorInfo(e)); } catch {}- this.teardownCapture();- this.emit('onError', e);+ this._teardownCapture();+ this._emit('onError', e);return;}this._ws = ws;@@@ -673,281 -403,122 +681,278 @@@ registerProcessor('orkas-asr-capture',await uiAlert(tr(errors[code] || 'error_unavailable').replace('{seconds}', '120'));}- function attach(button, editor) {- if (!button || !editor || button.dataset.voiceBound === '1') return;- button.dataset.voiceBound = '1';- let attempt = null;- let phase = 'idle';- function paint(next) {- phase = next;- button.dataset.voicePhase = phase;- button.classList.toggle('is-recording', phase === 'recording' || phase === 'connecting');- button.classList.toggle('is-busy', phase === 'finalizing');- button.title = tr(phase === 'idle' ? 'title' : phase === 'finalizing' ? 'finalizing' : 'stop_title');- button.setAttribute('aria-label', button.title);- button.setAttribute('aria-pressed', String(phase === 'recording'));+ function attach(buttonEl, textareaEl) {+ if (!buttonEl || !textareaEl || buttonEl.dataset.voiceBound === '1') return;+ buttonEl.dataset.voiceBound = '1';+ if (!(window.orkas && (typeof window.orkas.voiceInput?.createSession === 'function' || typeof window.orkas.invoke === 'function'))) {+ // No Server-backed voice support in this build — hide the affordance.+ buttonEl.style.display = 'none';+ return;+ }++ let phase = 'idle'; // 'idle' | 'connecting' | 'recording' | 'finalizing'+ let activeAttempt = null;++ function clearLimitTimer(attempt) {+ if (!attempt) return;+ if (attempt.limitTimer) { try { clearTimeout(attempt.limitTimer); } catch {} }+ attempt.limitTimer = null;+ }++ function startLimitTimer(attempt) {+ clearLimitTimer(attempt);+ attempt.limitTimer = setTimeout(() => {+ attempt.limitTimer = null;+ if (activeAttempt !== attempt || !attempt.client || attempt.phase !== 'recording') return;+ _toast(_limitMessage(120));+ stopRecording({ auto: true });+ }, VOICE_MAX_DURATION_MS);+ }++ function setPhase(p) {+ phase = p;+ buttonEl.dataset.voicePhase = p;+ buttonEl.classList.toggle('is-recording', p === 'recording');+ buttonEl.classList.toggle('is-busy', p === 'connecting' || p === 'finalizing');+ const titleKey = p === 'connecting' ? 'chat.mic_connecting' : p === 'finalizing' ? 'chat.mic_finalizing' : (p === 'recording' || p === 'connecting')+ ? 'chat.mic_stop_title' : 'chat.mic_title';+ const titleFallback = p === 'connecting' ? 'Connecting…' : p === 'finalizing' ? 'Finishing recognition…' : (p === 'recording' || p === 'connecting')+ ? 'Stop recording' : 'Voice input';+ buttonEl.title = _t(titleKey, titleFallback);+ buttonEl.setAttribute('aria-label', buttonEl.title);+ buttonEl.setAttribute('aria-pressed', String(p === 'recording'));}- function reset(current) {- clearTimeout(current.limit); clearInterval(current.timer);- current.insertion?.dispose();- if (attempt !== current) return;- attempt = null;- if (active === cancel) active = null;- paint('idle');++ function setAttemptPhase(attempt, nextPhase) {+ attempt.phase = nextPhase;+ if (activeAttempt === attempt) setPhase(nextPhase);+ }++ function applyTranscript(attempt, text) {+ if (activeAttempt !== attempt || attempt.cancelled) return;+ const inserted = String(text || '');+ const needsSpace = attempt.before && !/\s$/.test(attempt.before) && inserted ? ' ' : '';+ if (!attempt.insertion?.replace(needsSpace + inserted)) {+ attempt.finished = true;+ reset(attempt);+ try { attempt.client?.close(); } catch {}+ return;+ }+ }++ function reset(attempt) {+ reportVoiceTiming(attempt);+ clearLimitTimer(attempt);++ if (attempt.visibilityTimer !== null) clearInterval(attempt.visibilityTimer);++ attempt.visibilityTimer = null;+ attempt.insertion?.dispose();+ if (activeAttempt !== attempt) return;+ activeAttempt = null;+ if (cancelActive === cancel) cancelActive = null;+ setPhase('idle');}+function cancel() {+ const attempt = activeAttempt;if (!attempt) return;- const current = attempt;- reset(current);- current.client?.close();+ attempt.cancelled = true;+ attempt.finished = true;+ reset(attempt);+ try { attempt.client?.close(); } catch {}}+ textareaEl.addEventListener('composer-owner-change', cancel);+ window.addEventListener('beforeunload', cancel);+ document.addEventListener('visibilitychange', () => { if (document.hidden) cancel(); });+ document.addEventListener('keydown', (event) => { if (event.key === 'Escape') cancel(); });+ window.addEventListener('i18n-change', () => setPhase(phase));async function refreshHint() {if (phase !== 'idle') return;try {const status = await invoke('status');if (phase !== 'idle' || !status?.ok) return;- button.title = tr(status.configured ? 'title' : 'key_required');- button.setAttribute('aria-label', button.title);+ buttonEl.title = tr(status.configured ? 'title' : 'key_required');+ buttonEl.setAttribute('aria-label', buttonEl.title);} catch { /* The click path reports a recoverable status failure. */ }}- button.addEventListener('mouseenter', refreshHint);- button.addEventListener('focus', refreshHint);- editor.addEventListener('composer-owner-change', cancel);- window.addEventListener('beforeunload', cancel);- document.addEventListener('visibilitychange', () => { if (document.hidden) cancel(); });- document.addEventListener('keydown', (event) => { if (event.key === 'Escape' && attempt) cancel(); });- window.addEventListener('i18n-change', () => paint(phase));-- async function start() {- if (active) active();- const current = { client: null, insertion: composerInsertion(editor), limit: null, timer: null, terminal: false, text: '', stopped: false };- attempt = current; active = cancel; paint('connecting');- const before = composerText(editor).slice(0, composerSelection(editor).start);+ buttonEl.addEventListener('mouseenter', refreshHint);+ buttonEl.addEventListener('focus', refreshHint);++ async function startRecording() {+ if (activeAttempt) return;+ if (cancelActive) cancelActive();+ const attempt = {+ timingStart: voiceNow(), timings: {}, timingReported: false,+ recordingStarted: false,+ finished: false,+ cancelled: false,+ phase: 'connecting',+ before: composerText(textareaEl).slice(0, composerSelection(textareaEl).start),+ insertion: composerInsertion(textareaEl),+ limitTimer: null,++ visibilityTimer: null,+ client: null,+ };+ activeAttempt = attempt;+ cancelActive = cancel;+ setAttemptPhase(attempt, 'connecting');try {const status = await invoke('status');- if (attempt !== current) return;- if (!status?.ok) { reset(current); await guidance('unavailable'); return; }- if (!status.configured) { reset(current); await guidance('api_key_required'); return; }- const permission = await invoke('ensureMicrophoneAccess');- if (attempt !== current) return;- if (!permission?.ok || !permission.granted) {- reset(current); await guidance(permission?.status === 'restricted' ? 'restricted' : 'NotAllowedError'); return;+ if (activeAttempt !== attempt || attempt.cancelled) return;+ if (!status?.ok) { reset(attempt); await publicGuidance('unavailable'); return; }+ if (!status.configured) { reset(attempt); await publicGuidance('api_key_required'); return; }+ } catch { if (activeAttempt === attempt) { reset(attempt); await publicGuidance('unavailable'); } return; }+ if (typeof window.orkas.voiceInput?.ensureMicrophoneAccess === 'function' || typeof window.orkas.invoke === 'function') {+ try {+ const access = await invoke('ensureMicrophoneAccess');+ if (activeAttempt !== attempt || attempt.cancelled) return;+ if (!access || access.ok === false || !access.granted) {+ attempt.finished = true;+ reset(attempt);+ if (access?.status === 'restricted') await publicGuidance('restricted');+ else _alert(_t('chat.mic_error_permission', 'Microphone access was blocked. Allow it in your system settings to use voice input.'));+ return;+ }+ } catch (e) {+ if (activeAttempt !== attempt || attempt.cancelled) return;+ try { if (voiceLog) voiceLog.error('microphone access preflight failed', _errorInfo(e)); } catch {}+ attempt.finished = true;+ _alert(_t('chat.mic_error_unavailable', 'Voice input is unavailable right now.'));+ reset(attempt);+ return;}- const client = new AsrClient(async () => {- const session = await invoke('createSession');- if (!session?.ok || !session.url) throw Object.assign(new Error(), { code: session?.code || 'unavailable' });- return session.url;- }, {- onOpen() {- if (attempt !== current) { client.close(); return; }- paint('recording');- current.timer = setInterval(() => {- if (attempt !== current) return;- if (editor.getClientRects().length === 0) { cancel(); return; }- }, 1000);- current.limit = setTimeout(() => {- if (attempt !== current) return;- uiToast(tr('limit_reached').replace('{seconds}', '120'));- stop();- }, VOICE_MAX_DURATION_MS);- },- onResult({ text, last }) {- if (attempt !== current) return;- if (text && !current.insertion.replace((before && !/\s$/.test(before) ? ' ' : '') + text)) { cancel(); return; }- current.text = text;- if (last) { current.terminal = true; reset(current); client.close(); if (!text.trim()) guidance('empty_transcript'); }- },- onError(error) {- if (attempt !== current) { client.close(); return; }- reset(current); client.close(); guidance(error?.code || error?.name || 'unavailable');- },- onClose() {- if (attempt !== current) return;- reset(current);- if (!current.terminal) guidance('incomplete_transcript');- },- });- current.client = client;- await client.start();- } catch {- if (attempt !== current) return;- cancel(); await guidance('unavailable');+ }- let url = '';- try {- const r = await invoke('createSession');- if (activeAttempt !== attempt || attempt.cancelled) return;- if (r && (r.error === 'E_ORKAS_API_KEY_REQUIRED' || ['api_key_required', 'invalid_api_key', 'insufficient_scope', 'rate_limit_exceeded'].includes(r.code))) {- attempt.finished = true;- await publicGuidance(r.code || 'api_key_required');- reset(attempt);- return;- }- if (!r || r.ok === false || !r.url) throw new Error((r && r.error) || 'no_url');- url = r.url;- markVoiceTiming(attempt, 'session_ms');- } catch (e) {- if (activeAttempt !== attempt || attempt.cancelled) return;- try { if (voiceLog) voiceLog.error('failed to resolve ASR websocket URL', _errorInfo(e)); } catch {}- attempt.finished = true;- _alert(_t('chat.mic_error_unavailable', 'Voice input is unavailable right now.'));- reset(attempt);- return;- }+ if (activeAttempt !== attempt || attempt.cancelled) return;+ if (!attempt.insertion?.valid()) { reset(attempt); return; }- const attemptClient = new AsrClient(url, {++ // Resolve the one-use ticket only after getUserMedia admits this device.++ const attemptClient = new AsrClient(async () => {++ const result = await invoke('createSession');++ if (activeAttempt !== attempt || attempt.cancelled) return '';++ if (!attempt.insertion?.valid()) { cancel(); return ''; }++ if (!result || result.ok === false || !result.url) {++ throw Object.assign(new Error(), { code: result?.code++ || (result?.error === 'E_ORKAS_API_KEY_REQUIRED' ? 'api_key_required' : 'unavailable') });++ }++ markVoiceTiming(attempt, 'session_ms');++ return result.url;++ }, {+ onStage: (stage) => { if (activeAttempt === attempt) markVoiceTiming(attempt, stage); },+ onOpen: () => {+ if (activeAttempt !== attempt) return;+ markVoiceTiming(attempt, 'capture_ready_ms');+ attempt.recordingStarted = true;+ setAttemptPhase(attempt, 'recording');+ startLimitTimer(attempt);++ attempt.visibilityTimer = setInterval(() => {++ if (activeAttempt !== attempt) return;++ if (textareaEl.getClientRects().length === 0) cancel();++ }, 1000);+ },+ onResult: ({ text, last }) => {+ if (activeAttempt !== attempt) return;+ if (text) markVoiceTiming(attempt, 'first_text_ms');+ if (last) markVoiceTiming(attempt, 'final_text_ms');+ if (text) applyTranscript(attempt, text);+ if (last && activeAttempt === attempt) {+ attempt.finished = true; reset(attempt); attemptClient.close();+ if (!String(text || '').trim()) publicGuidance('empty_transcript');+ }+ },+ onError: (err) => {+ if (activeAttempt !== attempt || attempt.finished) {+ try { attemptClient.close(); } catch {}+ return;+ }+ try { if (voiceLog) voiceLog.error('voice client error', { phase: attempt.phase, error: _errorInfo(err) }); } catch {}+ if (err?.code || ['NotReadableError', 'TrackStartError'].includes(err?.name)) {+ attempt.finished = true; attemptClient.close(); reset(attempt);+ publicGuidance(err.code || err.name); return;+ }+ const name = err && typeof err === 'object'+ ? [err.name, err.message].map((value) => String(value || '').trim()).filter(Boolean).join(' ')+ : String(err || '');+ const errorCode = _voiceErrorCode(name);+ attempt.finished = true;+ if (errorCode === 'timeout') {+ _toast(_limitMessage(err && err.maxSeconds));+ } else {+ const permission = errorCode === 'permission_denied' || _isPermissionError(name);+ const noDevice = errorCode === 'no_device' || _isNoDeviceError(name);+ const loggedOut = errorCode === 'unauthorized' || errorCode === 'session_expired';+ const insufficientQuota = errorCode === 'insufficient_quota';+ _alert(loggedOut+ ? _t('settings.orkas_api.key_required', 'Enter an API Key first.')+ : insufficientQuota+ ? _t('chat.mic_insufficient_credits', 'Not enough credits for voice input.')+ : permission+ ? _t('chat.mic_error_permission', 'Microphone access was blocked. Allow it in your system settings to use voice input.')+ : noDevice+ ? _t('chat.mic_error_no_device', 'No microphone was found.')+ : _t('chat.mic_error_unavailable', 'Voice input is unavailable right now.'));+ }+ try { attemptClient.close(); } catch {}+ reset(attempt);+ },+ onClose: () => {+ if (activeAttempt !== attempt) return;+ if (attempt.finished) {+ reset(attempt);+ return;+ }+ reset(attempt);+ if (!attempt.cancelled) publicGuidance('incomplete_transcript');+ },+ });+ attempt.client = attemptClient;+ try {+ await attemptClient.start();+ } catch (e) {+ if (activeAttempt !== attempt || attempt.finished) return;+ try { if (voiceLog) voiceLog.error('voice client start failed', _errorInfo(e)); } catch {}+ attempt.finished = true;+ _alert(_t('chat.mic_error_unavailable', 'Voice input is unavailable right now.'));+ reset(attempt);}}- function stop() {- if (!attempt || phase === 'finalizing') return;- if (phase === 'connecting') { cancel(); return; }- clearTimeout(attempt.limit);- paint('finalizing');- attempt.client.stop();++ function stopRecording(options) {+ const attempt = activeAttempt;+ if (!attempt || attempt.phase === 'finalizing') return;+ const automatic = !!(options && options.auto);+ attempt.cancelled = !automatic && !attempt.recordingStarted;+ clearLimitTimer(attempt);+ if (attempt.cancelled) {+ const attemptClient = attempt.client;+ attempt.finished = true;+ reset(attempt);+ try { if (attemptClient) attemptClient.close(); } catch {}+ return;+ }+ markVoiceTiming(attempt, 'stop_ms');+ setAttemptPhase(attempt, 'finalizing');+ try {+ if (!attempt.client) throw new Error('voice_client_unavailable');+ attempt.client.stop();+ } catch (e) {+ attempt.finished = true;+ reset(attempt);+ }}- button.addEventListener('click', () => { if (attempt) stop(); else start(); });- paint('idle');++ buttonEl.addEventListener('click', () => {+ const starting = phase === 'idle' && !activeAttempt;+ if (starting) startRecording();+ else if (activeAttempt) stopRecording();+ });+ setPhase('idle');}- window.VoiceInput = { attach, cancel: () => active?.(), createClient: (url, callbacks) => new AsrClient(url, callbacks) };++ // Expose the transport factory for deterministic capture and cancellation tests.+ window.VoiceInput = { attach, cancel: () => cancelActive?.(), createClient: (url, callbacks) => new AsrClient(url, callbacks) };+document.addEventListener('DOMContentLoaded', () => {- for (const [button, input] of [['new-chat-mic-btn', 'new-chat-input'], ['chat-mic-btn', 'chat-input'],- ['project-chat-mic-btn', 'project-chat-input'], ['auto-mic-btn', 'auto-task-input']]) {- attach(document.getElementById(button), document.getElementById(input));- }+ attach(document.getElementById('new-chat-mic-btn'), document.getElementById('new-chat-input'));+ attach(document.getElementById('chat-mic-btn'), document.getElementById('chat-input'));+ attach(document.getElementById('project-chat-mic-btn'), document.getElementById('project-chat-input'));+ attach(document.getElementById('auto-mic-btn'), document.getElementById('auto-task-input'));});})();diff --cc test/main/features/voice_input.test.tsindex 96a56c42,596c7930..69fc931a--- a/test/main/features/voice_input.test.ts+++ b/test/main/features/voice_input.test.ts@@@ -1,91 -1,41 +1,91 @@@-import { beforeEach, describe, expect, it, vi } from 'vitest';-const mocks = vi.hoisted(() => ({ key: '', status: 'not-determined', ask: vi.fn(), fetch: vi.fn() }));-vi.mock('../../../src/main/features/auth', () => ({ getOrkasApiKey: () => mocks.key }));+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++const state = vi.hoisted(() => ({ key: 'test-speech-key', uid: 'local-owner', status: 'granted', ask: vi.fn(async () => true) }));+vi.mock('../../../src/main/features/auth', () => ({ getOrkasApiKey: () => state.key }));+vi.mock('../../../src/main/features/users', () => ({ getActiveUserId: () => state.uid }));vi.mock('electron', () => ({ systemPreferences: {- getMediaAccessStatus: () => mocks.status,- askForMediaAccess: mocks.ask,+ getMediaAccessStatus: vi.fn(() => state.status), askForMediaAccess: state.ask,} }));-import { createVoiceSession, ensureMicrophoneAccess, getVoiceStatus } from '../../../src/main/features/voice_input';-describe('API-key voice input', () => {- beforeEach(() => { mocks.key = ''; vi.clearAllMocks(); vi.stubGlobal('fetch', mocks.fetch); });- it('requires the shared key before contacting the service', async () => {+import { createVoiceSession, getVoiceStatus, ensureMicrophoneAccess } from '../../../src/main/features/voice_input';++const ticket = 't'.repeat(43);+describe('API-key voice session', () => {+ beforeEach(() => { state.key = 'test-speech-key'; state.uid = 'local-owner'; state.status = 'granted'; state.ask.mockClear(); });+ afterEach(() => { vi.unstubAllGlobals(); vi.useRealTimers(); });++ it('exchanges a main-process credential for a fixed-origin ticket without exposing the key', async () => {+ const fetcher = vi.fn(async () => new Response(JSON.stringify({ ticket, expires_in: 30, max_seconds: 120 })));+ vi.stubGlobal('fetch', fetcher);+ const result = await createVoiceSession();+ expect(getVoiceStatus()).toEqual({ configured: true });+ expect(result).toEqual({ ok: true, url: `wss://orkas.ai/api/voice/asr_ws?ticket=${ticket}`, maxSeconds: 120 });+ expect(fetcher).toHaveBeenCalledTimes(1);+ const [url, options] = fetcher.mock.calls[0] as unknown as [string, RequestInit];+ expect(url).toBe('https://orkas.ai/v1/audio/transcription_sessions');+ expect(options).toMatchObject({ method: 'POST', redirect: 'error', body: '{}',+ headers: { Authorization: 'Bearer test-speech-key', 'Content-Type': 'application/json' } });+ expect(options.signal).toBeInstanceOf(AbortSignal);+ expect(JSON.stringify(result)).not.toContain(state.key);+ });++ it('requires a configured key before making any request', async () => {+ state.key = '';+ const fetcher = vi.fn(); vi.stubGlobal('fetch', fetcher);+ expect(await createVoiceSession()).toEqual({ ok: false, code: 'api_key_required' });+ expect(fetcher).not.toHaveBeenCalled();expect(getVoiceStatus()).toEqual({ configured: false });- expect(await createVoiceSession()).toMatchObject({ ok: false, code: 'api_key_required' });- expect(mocks.fetch).not.toHaveBeenCalled();});- it('keeps the key in main and returns only the fixed-origin ticket URL', async () => {- mocks.key = 'synthetic-private-key';- mocks.fetch.mockResolvedValue(new Response(JSON.stringify({ ticket: 't'.repeat(43), expires_in: 30 }), { status: 200 }));+- it.each([401, 403, 429, 500])('returns a bounded public failure for HTTP %s', async (status) => {++ it.each([401, 403, 429, 500, 503])('returns a bounded public failure for HTTP %s', async (status) => {+ vi.stubGlobal('fetch', vi.fn(async () => new Response('private upstream error', { status })));const result = await createVoiceSession();- expect(result).toMatchObject({ ok: true, url: 'wss://orkas.ai/api/voice/asr_ws?ticket=' + 't'.repeat(43) });- expect(JSON.stringify(result)).not.toContain(mocks.key);- expect(mocks.fetch.mock.calls[0][1]).toMatchObject({ redirect: 'error', headers: { Authorization: 'Bearer ' + mocks.key } });- });- it('maps authorization and temporary failures without exposing provider text', async () => {- mocks.key = 'synthetic-private-key';- for (const [status, code] of [[401, 'invalid_api_key'], [403, 'insufficient_scope'], [503, 'unavailable']] as const) {- mocks.fetch.mockResolvedValue(new Response('private server detail', { status }));- expect(await createVoiceSession()).toEqual({ ok: false, code });- }- });- it('does not repeat a rejected macOS permission request', async () => {- mocks.status = 'denied';- expect(await ensureMicrophoneAccess('darwin')).toEqual({ granted: false, status: 'denied' });- expect(mocks.ask).not.toHaveBeenCalled();- mocks.status = 'not-determined'; mocks.ask.mockResolvedValue(true);+ expect(result).toEqual({ ok: false, code: status === 401 ? 'invalid_api_key' : status === 403 ? 'insufficient_scope' : status === 429 ? 'rate_limit_exceeded' : 'unavailable' });+ expect(JSON.stringify(result)).not.toContain('private');+ });++ it.each([+ { ticket: 'short' }, { ticket: 'x'.repeat(44) }, { ticket: `${'x'.repeat(42)}&` },+ { url: 'wss://untrusted.example/asr', ticket: '' }, { ticket: 123 },+ ])('rejects malformed ticket responses without constructing an arbitrary transport', async (body) => {+ vi.stubGlobal('fetch', vi.fn(async () => new Response(JSON.stringify(body))));+ expect(await createVoiceSession()).toEqual({ ok: false, code: 'unavailable' });+ });++ it.each(['key', 'uid'])('discards a late ticket when the local %s changes', async (field) => {+ let finish!: (response: Response) => void;+ vi.stubGlobal('fetch', vi.fn(() => new Promise<Response>((resolve) => { finish = resolve; })));+ const pending = createVoiceSession();+ state[field as 'key' | 'uid'] = 'replacement';+ finish(new Response(JSON.stringify({ ticket })));+ expect(await pending).toEqual({ ok: false, code: 'unavailable' });+ });++ it('keeps the timeout active until the control response body finishes', async () => {+ vi.useFakeTimers();+ vi.stubGlobal('fetch', vi.fn(async (_url: string, options: RequestInit) => ({+ ok: true, status: 200,+ json: () => new Promise((_resolve, reject) => options.signal!.addEventListener('abort', () => reject(new Error('aborted')), { once: true })),+ })));+ const pending = createVoiceSession();+ await vi.advanceTimersByTimeAsync(60_001);+ expect(await pending).toEqual({ ok: false, code: 'unavailable' });+ });+});++// The macOS preflight never repeats a denied prompt; capture remains authoritative+// on other platforms (unknown is not a verified microphone grant).+describe('voice microphone preflight', () => {+ it.each(['denied', 'restricted'])('preserves %s without another OS request', async (status) => {+ state.status = status; state.ask.mockClear();+ expect(await ensureMicrophoneAccess('darwin')).toEqual({ granted: false, status });+ expect(state.ask).not.toHaveBeenCalled();+ });+ it('asks only for undecided macOS permission and leaves non-mac capture authoritative', async () => {+ state.status = 'not-determined'; state.ask.mockClear(); state.ask.mockResolvedValue(true);expect(await ensureMicrophoneAccess('darwin')).toEqual({ granted: true, status: 'granted' });- expect(mocks.ask).toHaveBeenCalledWith('microphone');+ expect(state.ask).toHaveBeenCalledWith('microphone');+ expect(await ensureMicrophoneAccess('linux')).toEqual({ granted: true, status: 'unknown' });expect(await ensureMicrophoneAccess('win32')).toEqual({ granted: true, status: 'unknown' });});});diff --cc test/renderer/voice-input.test.tsindex e1b41ba1,029c57a4..93267a31--- a/test/renderer/voice-input.test.ts+++ b/test/renderer/voice-input.test.ts@@@ -1,1102 -1,260 +1,1197 @@@-import { afterEach, describe, expect, it, vi } from 'vitest';-import { readFileSync } from 'node:fs';-import path from 'node:path';-import vm from 'node:vm';+import { composerAccessorSource } from './composer-test-source';+import { afterEach, describe, it, expect, vi } from 'vitest';+import * as fs from 'node:fs';+import * as path from 'node:path';+import * as vm from 'node:vm';+import { gzipSync, gunzipSync } from 'node:zlib';-function element() {- const events: Record<string, Function> = {};+function makeButton(id: string) {+ const listeners: Record<string, Function> = {};const classes = new Set<string>();- return { id: 'new-chat-mic-btn', value: 'Draft:', dataset: {} as any, title: '', hidden: false,- classList: { toggle: (name: string, enabled: boolean) => enabled ? classes.add(name) : classes.delete(name),- contains: (name: string) => classes.has(name) },- parentNode: { insertBefore: vi.fn() }, setAttribute: vi.fn(), getClientRects: () => [1],- addEventListener: (name: string, fn: Function) => { events[name] = fn; },- dispatch: (name: string) => events[name]?.(), focus: vi.fn() };+ return {+ id,+ dataset: {} as Record<string, string>,+ style: {} as Record<string, string>,+ title: '',+ classList: {+ contains(name: string) { return classes.has(name); },+ toggle(name: string, active: boolean) {+ if (active) classes.add(name);+ else classes.delete(name);+ },+ },+ parentNode: { insertBefore: vi.fn() },+ setAttribute: vi.fn(),+ addEventListener(name: string, handler: Function) { listeners[name] = handler; },+ click() { return listeners.click?.(); },+ };}-function load(overrides: Record<string, any> = {}) {- const button = element(), input = element();- let socket: any;- const track = { stop: vi.fn() };- const invoke = vi.fn(async (channel: string) => channel === 'voice.status' ? { ok: true, configured: true }- : channel === 'voice.ensureMicrophoneAccess' ? { ok: true, granted: true, status: 'granted' }- : { ok: true, url: 'wss://orkas.ai/api/voice/asr_ws?ticket=synthetic' });- const getUserMedia = vi.fn(async () => ({ getTracks: () => [track] }));- class Socket {- static OPEN = 1; static CONNECTING = 0;- readyState = 0; bufferedAmount = 0;- onopen: any; onmessage: any; onclose: any; onerror: any;- constructor() { socket = this; }- send = vi.fn();- close() { this.readyState = 3; this.onclose?.({ code: 1000 }); }- open() { this.readyState = 1; this.onopen?.(); this.onmessage?.({ data: '{"event":"ready"}' }); }- result(text: string, last: boolean) { this.onmessage?.({ data: JSON.stringify({ is_last_package: last, payload_msg: { result: { text } } }) }); }- }- const node = () => ({ connect() {}, disconnect() {} });- class AudioContext {- state = 'running'; sampleRate = 48000; destination = {};- createMediaStreamSource = node;- createScriptProcessor = () => ({ ...node(), onaudioprocess: null });- createGain = () => ({ ...node(), gain: { value: 1 } });- close = async () => {};- }++function makeTextarea() {+ return {+ value: '',+ selectionStart: 0,+ selectionEnd: 0,+ setSelectionRange() {},+ dispatchEvent() {},+ addEventListener() {},+ getClientRects: () => [1],+ focus() {},+ };+}++function loadVoiceInput(voice: any, _unused?: undefined, options: {+ sourcePath?: string;+ globals?: Record<string, unknown>;+} = {}) {+ const alerts: string[] = [];+ const windowEvents: Record<string, Function> = {};+ const documentEvents: Record<string, Function> = {};+ const english = JSON.parse(fs.readFileSync(path.join(__dirname, '../../src/renderer/locales/en.json'), 'utf8'));+ const windowObject: any = {+ orkas: { voiceInput: voice },+ addEventListener: (name: string, fn: Function) => { windowEvents[name] = fn; },+ alert(message: string) { alerts.push(String(message)); },+ };const context: any = {- window: { orkas: { invoke }, AudioContext, addEventListener: vi.fn(), localStorage: { getItem: () => null, setItem: vi.fn() } },- document: { addEventListener: vi.fn(), getElementById: () => input, createElement: () => element(), hidden: false },- navigator: { mediaDevices: { getUserMedia } }, WebSocket: Socket,- composerText: () => input.value, composerSelection: () => ({ start: input.value.length }),- composerInsertion: () => ({ valid: () => true, replace: (text: string) => { input.value = 'Draft:' + text; return true; }, dispose() {} }),- t: (key: string) => key, uiConfirm: vi.fn(async () => true), uiChoice: vi.fn(async () => null),- uiAlert: vi.fn(), uiToast: vi.fn(), setView: vi.fn(),- setTimeout, clearTimeout, setInterval, clearInterval, URL, Event, Date,- createLogger: () => ({ info() {}, warn() {}, error() {} }),- ...overrides,+ console,+ window: windowObject,+ document: { addEventListener: (name: string, fn: Function) => { documentEvents[name] = fn; }, getElementById: () => null, hidden: false },+ t: (key: string) => english[key] || key,+ uiAlert: (message: string) => { alerts.push(String(message)); },+ uiChoice: vi.fn(async () => null), uiConfirm: vi.fn(), uiToast: vi.fn(),+ setInterval, clearInterval,+ navigator: {},+ composerInsertion: () => ({ valid: () => true, replace: () => true, dispose() {} }),+ setTimeout,+ clearTimeout,+ URL,+ Date,+ Math,+ Event: class {},+ ...options.globals,};- context.window.window = context.window;+ windowObject.window = windowObject;vm.createContext(context);- vm.runInContext(readFileSync(path.join(__dirname, '../../src/renderer/modules/voice-input.js'), 'utf8'), context);- context.window.VoiceInput.attach(button, input);- return { context, button, input, invoke, getUserMedia, track, socket: () => socket };+ vm.runInContext(composerAccessorSource, context);+ const source = fs.readFileSync(options.sourcePath || path.join(__dirname, '../../src/renderer/modules/voice-input.js'), 'utf8');+ vm.runInContext(source, context);+ return { context, alerts, windowEvents, documentEvents };}-const flush = async () => { for (let i = 0; i < 15; i++) await Promise.resolve(); };-afterEach(() => vi.useRealTimers());--describe('voice input user journey', () => {- it('uses the Orkas icon-only active and finishing states throughout dictation', async () => {- vi.useFakeTimers();- const h = load();- const expectState = (phase: string, recording: boolean, busy: boolean) => {- expect(h.button.dataset.voicePhase).toBe(phase);- expect(h.button.classList.contains('is-recording')).toBe(recording);- expect(h.button.classList.contains('is-busy')).toBe(busy);- expect(h.button.parentNode.insertBefore).not.toHaveBeenCalled();- };- expectState('idle', false, false);- await h.button.dispatch('click'); await flush();- expect(h.context.uiConfirm).not.toHaveBeenCalled();- expect(h.invoke).toHaveBeenCalledWith('voice.ensureMicrophoneAccess');- expectState('connecting', true, false);- expect(h.button.title).toBe('chat.mic_stop_title');- h.socket().open(); await flush();- expectState('recording', true, false);- await h.button.dispatch('click'); await flush();- expectState('finalizing', false, true);- h.socket().result('spoken note', true);- expectState('idle', false, false);- expect(h.button.title).toBe('chat.mic_title');- });- it('guides a missing key before requesting the microphone', async () => {- const h = load();- h.invoke.mockResolvedValue({ ok: true, configured: false } as any);- await h.button.dispatch('click'); await flush();- expect(h.context.uiChoice).toHaveBeenCalledWith(expect.objectContaining({ message: 'chat.mic_key_required' }));- expect(h.getUserMedia).not.toHaveBeenCalled();- expect(h.input.value).toBe('Draft:');- });- it('does not contact recognition after OS permission denial', async () => {- const h = load();- h.invoke.mockImplementation(async (channel: string) => channel === 'voice.status' ? { ok: true, configured: true } : { ok: true, granted: false, status: 'denied' } as any);- await h.button.dispatch('click'); await flush();- expect(h.getUserMedia).not.toHaveBeenCalled();- expect(h.invoke.mock.calls.map(c => c[0])).not.toContain('voice.createSession');- expect(h.context.uiAlert).toHaveBeenCalledWith('chat.mic_error_permission');- });- it('does not misreport an unavailable credential status as an absent key', async () => {- const h = load();- h.invoke.mockResolvedValue({ ok: false } as any);- await h.button.dispatch('click'); await flush();- expect(h.context.uiAlert).toHaveBeenCalledWith('chat.mic_error_unavailable');- expect(h.context.uiChoice).not.toHaveBeenCalled();- expect(h.getUserMedia).not.toHaveBeenCalled();- });- it('preserves the draft when the microphone is absent or busy', async () => {- for (const name of ['NotFoundError', 'NotReadableError']) {- const h = load(); h.getUserMedia.mockRejectedValue(Object.assign(new Error(), { name }));- await h.button.dispatch('click'); await flush();- expect(h.invoke.mock.calls.map(c => c[0])).not.toContain('voice.createSession');- expect(h.input.value).toBe('Draft:');- expect(h.context.uiAlert).toHaveBeenCalledWith(name === 'NotFoundError' ? 'chat.mic_error_no_device' : 'chat.mic_error_busy');++function installAudioCaptureStub(context: any) {+ const node = () => ({ connect() {}, disconnect() {} });+ const captures: any[] = [];+ context.window.AudioContext = class {+ state = 'running';+ sampleRate = 48_000;+ destination = {};+ createMediaStreamSource() { return node(); }+ createScriptProcessor() {+ const capture = { ...node(), onaudioprocess: null };+ captures.push(capture);+ return capture;}+ createGain() { return { ...node(), gain: { value: 1 } }; }+ close() { return Promise.resolve(); }+ };+ return captures;+}++function loadCompressedCapture(sourcePath: string, failCompression = false) {+ let releaseFirst: () => void = () => {};+ const first = new Promise<void>((resolve) => { releaseFirst = resolve; });+ let compressions = 0;+ // Driver completion is controlled; compression bytes use real gzip. This+ // exercises capture -> production transport -> wire without a microphone.+ class Compression {+ constructor(_format: string) {+ if (failCompression) throw new Error('synthetic gzip failure');+ }+ }+ class AudioBlob {+ constructor(private parts: Uint8Array[]) {}+ stream() {+ return {+ pipeThrough: () => gzipSync(Buffer.concat(this.parts.map((part) => Buffer.from(part)))),+ };+ }+ }+ class AudioResponse {+ constructor(private bytes: Buffer) {}+ async arrayBuffer() {+ if (++compressions === 1) await first;+ return Uint8Array.from(this.bytes).buffer;+ }+ }+ const { context } = loadVoiceInput({}, undefined, {+ sourcePath,+ globals: { CompressionStream: Compression, DecompressionStream: class {}, Blob: AudioBlob, Response: AudioResponse },});- it('inserts recognized text for review and releases the microphone on stop', async () => {- vi.useFakeTimers();- const h = load(); await h.button.dispatch('click'); await flush();- h.socket().open(); await flush();- h.socket().result('spoken note', false);- expect(h.input.value).toBe('Draft: spoken note');- await h.button.dispatch('click'); await flush();- expect(h.track.stop).toHaveBeenCalledTimes(1);- h.socket().result('spoken note.', true);- expect(h.input.value).toBe('Draft: spoken note.');- expect(h.button.dataset.voicePhase).toBe('idle');- expect(h.invoke.mock.calls.map(c => c[0])).not.toContain('groupChat.send');- });- it.each(['success', 'unresponsive', 'cancelled'])(- 'drains the speech tail or cancels incomplete capture safely: %s', async (scenario) => {- vi.useFakeTimers();- const h = load();- let processor: any, Processor: any, blob: Blob;- let mainPort: any;- class WorkletProcessor {- port = { onmessage: null as any, postMessage: (data: any) => {- queueMicrotask(() => mainPort.onmessage?.({ data }));- } };+ const sockets: any[] = [];+ class Socket {+ static CONNECTING = 0;+ static OPEN = 1;+ static CLOSED = 3;+ readyState = Socket.CONNECTING;+ frames: (Buffer | string)[] = [];+ onopen?: () => void;+ onmessage?: (event: any) => void;+ onclose?: () => void;+ constructor(readonly url: string) { sockets.push(this); }+ open(ready = true) { this.readyState = Socket.OPEN; this.onopen?.(); if (ready) this.ready(); }+ ready() { this.onmessage?.({ data: '{"event":"ready"}' }); }+ send(value: Uint8Array | string) {+ if (this.readyState !== Socket.OPEN) throw new Error('socket is closed');+ this.frames.push(typeof value === 'string' ? value : Buffer.from(value));}- class WorkletNode {- port = mainPort = { onmessage: null as any, postMessage: (data: any) => {- if (data.flush && scenario !== 'success') return;- queueMicrotask(() => processor.port.onmessage?.({ data }));- } };- constructor() { processor = new Processor(); }- connect() {}- disconnect() {}+ close() { this.readyState = Socket.CLOSED; this.onclose?.(); }+ }+ context.WebSocket = Socket;+ context.setTimeout = () => 1;+ context.navigator.mediaDevices = {+ getUserMedia: async () => ({ getTracks: () => [{ stop() {} }] }),+ };+ const captures = installAudioCaptureStub(context);+ const errors: Error[] = [];+ const client = context.window.VoiceInput.createClient('wss://example.test/asr', {+ onError: (error: Error) => errors.push(error),+ });+ const capture = (sample: number) => captures.at(-1).onaudioprocess({+ inputBuffer: { getChannelData: () => new Float32Array([sample, sample, sample]) },+ });+ const decode = (frames: (Buffer | string)[]) => frames.map((value) => (+ typeof value === 'string' ? value : Array.from(gunzipSync(value))+ ));+ return { client, sockets, errors, capture, decode, releaseFirst };+}++function loadWorkletCapture(sourcePath: string, options: {+ delayedModule?: boolean;+ delayedPermission?: boolean;+ dropFlush?: boolean;+ resumeFails?: boolean;+} = {}) {+ const modules = new Map<string, string>();+ let releaseModule: () => void = () => {};+ const moduleGate = new Promise<void>((resolve) => { releaseModule = resolve; });+ let grantPermission: (value: any) => void = () => {};+ const permission = new Promise((resolve) => { grantPermission = resolve; });+ class WorkletBlob {+ constructor(readonly parts: string[]) {}+ }+ class WorkletURL extends URL {+ static createObjectURL(blob: WorkletBlob) {+ const url = `blob:worklet-${modules.size}`;+ modules.set(url, blob.parts.join(''));+ return url;}- const BaseContext = h.context.window.AudioContext;- h.context.window.AudioContext = class extends BaseContext {- sampleRate = 16000;- audioWorklet = { addModule: async () => {- vm.runInNewContext(await blob.text(), {- AudioWorkletProcessor: WorkletProcessor,- registerProcessor: (_: string, value: any) => { Processor = value; },- });- } };+ static revokeObjectURL(_url: string) {}+ }+ const { context } = loadVoiceInput({}, undefined, {+ sourcePath, globals: { Blob: WorkletBlob, URL: WorkletURL },+ });+ const nodes: any[] = [];+ const sockets: any[] = [];+ const timers = new Map<number, { callback: () => void; delay: number }>();+ let timerId = 0;+ let tracksStopped = 0;+ let Processor: any;+ let channel: any;+ class WorkletNode {+ port = {+ onmessage: null as any,+ postMessage: (data: any) => {+ if (options.dropFlush && data.flush) return;+ queueMicrotask(() => this.processor.port.onmessage?.({ data }));+ },};- h.context.window.AudioWorkletNode = h.context.AudioWorkletNode = WorkletNode;- h.context.Blob = Blob;- h.context.URL = class extends URL {- static createObjectURL(value: Blob) { blob = value; return 'blob:voice-test'; }- static revokeObjectURL() {}+ processor: any;+ constructor() {+ channel = this.port;+ this.processor = new Processor();+ nodes.push(this);+ }+ connect() {}+ disconnect() {}+ }+ class CaptureContext {+ state = options.resumeFails ? 'suspended' : 'running';+ resume() { return Promise.reject(new Error('audio_resume_failed')); }+ sampleRate = 16_000;+ destination = {};+ audioWorklet = {+ addModule: async (url: string) => {+ if (options.delayedModule) await moduleGate;+ const source = modules.get(url);+ if (!source) throw new Error('missing owned worklet module');+ vm.runInNewContext(source, {+ AudioWorkletProcessor: class {+ port: any;+ constructor() {+ const owner = channel;+ // Resolve the node's port when it is constructed, after module+ // registration. Production processor messages remain unchanged.+ this.port = {+ onmessage: null,+ postMessage: (data: any) => queueMicrotask(() => owner.onmessage?.({ data })),+ };+ }+ },+ registerProcessor: (_name: string, implementation: any) => { Processor = implementation; },+ });+ },};- await h.button.dispatch('click'); await flush();- const socket = h.socket(); socket.open(); await flush();- // At 16kHz, this is 200ms of complete packets plus a 40ms speech tail.- for (let i = 0; i < 30; i++) {- processor.process([[new Float32Array(128).fill(i < 25 ? .25 : -.5)]], [[new Float32Array(128)]]);+ createMediaStreamSource() { return { connect() {}, disconnect() {} }; }+ createGain() { return { gain: { value: 1 }, connect() {}, disconnect() {} }; }+ close() { return Promise.resolve(); }+ }+ class Socket {+ static CONNECTING = 0;+ static OPEN = 1;+ static CLOSED = 3;+ readyState = Socket.CONNECTING;+ frames: (Buffer | string)[] = [];+ onopen?: () => void;+ onmessage?: (event: any) => void;+ onclose?: () => void;+ constructor(_url: string) { sockets.push(this); }+ open(ready = true) { this.readyState = Socket.OPEN; this.onopen?.(); if (ready) this.ready(); }+ ready() { this.onmessage?.({ data: '{"event":"ready"}' }); }+ send(value: Uint8Array | string) {+ this.frames.push(typeof value === 'string' ? value : Buffer.from(value));+ }+ close() { this.readyState = Socket.CLOSED; this.onclose?.(); }+ }+ const stream = { getTracks: () => [{ stop: () => { tracksStopped++; } }] };+ context.WebSocket = Socket;+ context.AudioWorkletNode = WorkletNode;+ context.window.AudioWorkletNode = WorkletNode;+ context.window.AudioContext = CaptureContext;+ context.navigator.mediaDevices = {+ getUserMedia: () => options.delayedPermission ? permission : Promise.resolve(stream),+ };+ context.setTimeout = (callback: () => void, delay: number) => {+ timers.set(++timerId, { callback, delay });+ return timerId;+ };+ context.clearTimeout = (id: number) => timers.delete(id);+ const errors: Error[] = [];+ const opened: boolean[] = [];+ const client = context.window.VoiceInput.createClient('wss://example.test/asr', {+ onOpen: () => opened.push(true),+ onError: (error: Error) => errors.push(error),+ });+ const processSamples = (count: number) => {+ for (let offset = 0; offset < count; offset += 128) {+ nodes.at(-1).processor.process(+ [[new Float32Array(Math.min(128, count - offset)).fill(0.25)]],+ [[new Float32Array(128)]],+ );+ }+ };+ return {+ client, nodes, sockets, errors, opened, timers, processSamples, releaseModule,+ grantPermission: () => grantPermission(stream),+ tracksStopped: () => tracksStopped,+ };+}++function loadDelayedTranscript(sourcePath: string) {+ let releaseFirst: () => void = () => {};+ const first = new Promise<void>((resolve) => { releaseFirst = resolve; });+ let decodes = 0;+ let tracksStopped = 0;+ class Compression {}+ class Decompression {}+ class IncomingBlob {+ constructor(private parts: Uint8Array[]) {}+ stream() {+ return { pipeThrough: (codec: any) => {+ const input = Buffer.concat(this.parts.map((part) => Buffer.from(part)));+ const incoming = codec instanceof Decompression;+ return { incoming, bytes: incoming ? gunzipSync(input) : gzipSync(input) };+ } };+ }+ }+ class IncomingResponse {+ constructor(private payload: { incoming: boolean; bytes: Buffer }) {}+ async arrayBuffer() {+ if (this.payload.incoming && ++decodes === 1) await first;+ return Uint8Array.from(this.payload.bytes).buffer;+ }+ }+ const { context } = loadVoiceInput({}, undefined, {+ sourcePath,+ globals: {+ CompressionStream: Compression, DecompressionStream: Decompression,+ Blob: IncomingBlob, Response: IncomingResponse, TextDecoder,+ },+ });+ const sockets: any[] = [];+ class Socket {+ static CONNECTING = 0;+ static OPEN = 1;+ static CLOSED = 3;+ readyState = Socket.CONNECTING;+ onopen?: () => void;+ onmessage?: (event: any) => void;+ onclose?: (event: any) => void;+ constructor(_url: string) { sockets.push(this); }+ open(ready = true) { this.readyState = Socket.OPEN; this.onopen?.(); if (ready) this.ready(); }+ ready() { this.onmessage?.({ data: '{"event":"ready"}' }); }+ receive(text: string, terminal = false) {+ const payload = gzipSync(Buffer.from(JSON.stringify({ result: { text } })));+ const frame = Buffer.alloc(12 + payload.length);+ frame.set([0x11, terminal ? 0x93 : 0x91, 0x11, 0]);+ frame.writeInt32BE(terminal ? -2 : 1, 4);+ frame.writeUInt32BE(payload.length, 8);+ payload.copy(frame, 12);+ this.onmessage?.({ data: frame.buffer.slice(frame.byteOffset, frame.byteOffset + frame.length) });+ }+ send() {}+ close() {+ this.readyState = Socket.CLOSED;+ this.onclose?.({ code: 1000, reason: '', wasClean: true });}- await flush();- await h.button.dispatch('click'); await flush();- expect(h.track.stop).toHaveBeenCalledTimes(1);- if (scenario !== 'success') {- expect(socket.send.mock.calls.some((call: any[]) => call[0] === '__END__')).toBe(false);- if (scenario === 'cancelled') h.context.window.VoiceInput.cancel();- await vi.advanceTimersByTimeAsync(500);- await flush();- expect(socket.send.mock.calls.some((call: any[]) => call[0] === '__END__')).toBe(false);- expect(h.button.dataset.voicePhase).toBe('idle');- expect(h.input.value).toBe('Draft:');- if (scenario === 'unresponsive') {- expect(h.context.uiAlert).toHaveBeenCalledWith('chat.mic_incomplete');- } else {- expect(h.context.uiAlert).not.toHaveBeenCalled();+ }+ context.WebSocket = Socket;+ context.navigator.mediaDevices = {+ getUserMedia: async () => ({ getTracks: () => [{ stop: () => { tracksStopped++; } }] }),+ };+ installAudioCaptureStub(context);+ const timeline: any[] = [];+ const client = context.window.VoiceInput.createClient('wss://example.test/asr', {+ onResult: ({ text, last }: { text: string; last: boolean }) => timeline.push(['result', text, last]),+ onClose: () => timeline.push(['close']),+ onError: (error: Error) => timeline.push(['error', error.message]),+ });+ return { client, sockets, timeline, releaseFirst, tracksStopped: () => tracksStopped };+}++describe.each([+ ['desktop', path.join(__dirname, '../../src/renderer/modules/voice-input.js')],+])('%s compressed transcript ordering', (_platform, sourcePath) => {+ it('delivers partial then terminal text in wire order despite different decode completion times', async () => {+ const fixture = loadDelayedTranscript(sourcePath);+ await fixture.client.start();+ fixture.sockets[0].open();+ fixture.sockets[0].receive('partial');+ fixture.sockets[0].receive('terminal', true);+ await new Promise<void>((resolve) => setImmediate(resolve));+ const whileFirstPending = [...fixture.timeline];+ fixture.releaseFirst();+ await vi.waitFor(() => expect(fixture.timeline).toHaveLength(2));+ expect(whileFirstPending).toEqual([]);+ expect(fixture.timeline).toEqual([['result', 'partial', false], ['result', 'terminal', true]]);+ fixture.client.close();+ });++ it.each([false, true])('delivers terminal before natural close with immediate device release (stopped=%s)', async (stopped) => {+ const fixture = loadDelayedTranscript(sourcePath);+ await fixture.client.start();+ fixture.sockets[0].open();+ if (stopped) {+ fixture.client.stop();+ await new Promise<void>((resolve) => setImmediate(resolve));+ }+ fixture.sockets[0].receive('terminal', true);+ fixture.sockets[0].close(); // The peer closes after sending its final frame.+ await new Promise<void>((resolve) => setImmediate(resolve));+ const whilePending = [...fixture.timeline];+ expect(fixture.tracksStopped()).toBe(1);+ fixture.releaseFirst();+ await vi.waitFor(() => expect(fixture.timeline).toHaveLength(2));+ expect(whilePending).toEqual([]);+ expect(fixture.timeline).toEqual([['result', 'terminal', true], ['close']]);+ });++ it('starts a new recognition without reusing the previous connection transcript', async () => {+ const fixture = loadDelayedTranscript(sourcePath);+ fixture.releaseFirst();+ await fixture.client.start();+ fixture.sockets[0].open();+ fixture.sockets[0].receive('previous recognition', true);+ await vi.waitFor(() => expect(fixture.timeline).toContainEqual(['result', 'previous recognition', true]));+ fixture.client.close();+ await fixture.client.start();+ fixture.sockets[1].open();+ fixture.sockets[1].receive('', true);+ await vi.waitFor(() => expect(fixture.timeline).toHaveLength(3));+ expect(fixture.timeline[2]).toEqual(['result', '', true]);+ fixture.client.close();+ });++ it('discards an explicitly cancelled decode without blocking a replacement connection', async () => {+ const fixture = loadDelayedTranscript(sourcePath);+ await fixture.client.start();+ fixture.sockets[0].open();+ fixture.sockets[0].receive('cancelled terminal', true);+ await new Promise<void>((resolve) => setImmediate(resolve));+ fixture.client.close();+ await fixture.client.start();+ fixture.sockets[1].open();+ fixture.sockets[1].receive('new terminal', true);+ await vi.waitFor(() => expect(fixture.timeline).toContainEqual(['result', 'new terminal', true]));+ fixture.releaseFirst();+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(fixture.timeline).toEqual([['close'], ['result', 'new terminal', true]]);+ fixture.client.close();+ });+});++describe.each([+ ['desktop', path.join(__dirname, '../../src/renderer/modules/voice-input.js')],+])('%s worklet capture ownership', (_platform, sourcePath) => {+ it('flushes the final partial 200ms packet before END and releases the microphone immediately', async () => {+ const fixture = loadWorkletCapture(sourcePath);+ await fixture.client.start();+ fixture.sockets[0].open();+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(fixture.nodes).toHaveLength(1);+ fixture.processSamples(3840); // Independently specified 240ms at16kHz.+ await new Promise<void>((resolve) => setImmediate(resolve));+ fixture.client.stop();+ expect(fixture.tracksStopped()).toBe(1);+ await vi.waitFor(() => expect(fixture.sockets[0].frames.at(-1)).toBe('__END__'));+ const pcm = Buffer.concat(fixture.sockets[0].frames.filter(Buffer.isBuffer));+ const expected = Buffer.alloc(7680);+ for (let offset = 0; offset < expected.length; offset += 2) expected.writeInt16LE(0x1fff, offset);+ expect(pcm.length).toBe(7680);+ expect(pcm).toEqual(expected);+ expect(fixture.errors).toEqual([]);+ fixture.client.close();+ });++ it('never announces recording if the audio device cannot resume', async () => {+ const fixture = loadWorkletCapture(sourcePath, { resumeFails: true });+ await fixture.client.start(); fixture.sockets[0].open();+ await new Promise<void>(resolve => setImmediate(resolve));+ expect(fixture.opened).toEqual([]);+ expect(fixture.nodes).toEqual([]);+ expect(fixture.errors.map(error => error.message)).toEqual(['audio_resume_failed']);+ fixture.client.close();+ expect(fixture.tracksStopped()).toBe(1);+ });++ it('announces recording only after the capture graph is ready', async () => {+ const fixture = loadWorkletCapture(sourcePath, { delayedModule: true });+ await fixture.client.start(); fixture.sockets[0].open();+ await new Promise<void>(resolve => setImmediate(resolve));+ expect(fixture.opened).toEqual([]);+ expect(fixture.nodes).toEqual([]);+ fixture.releaseModule();+ await new Promise<void>(resolve => setImmediate(resolve));+ expect(fixture.opened).toEqual([true]);+ expect(fixture.nodes).toHaveLength(1);+ fixture.client.close();+ });++ it('does not create capture resources after a pending worklet load is stopped', async () => {+ const fixture = loadWorkletCapture(sourcePath, { delayedModule: true });+ await fixture.client.start();+ fixture.sockets[0].open();+ fixture.client.stop();+ fixture.releaseModule();+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(fixture.nodes).toEqual([]);+ expect(fixture.errors).toEqual([]);+ expect(fixture.tracksStopped()).toBe(1);+ fixture.client.close();+ });++ it('releases a late permission grant after close without opening a recording socket', async () => {+ const fixture = loadWorkletCapture(sourcePath, { delayedPermission: true });+ const starting = fixture.client.start();+ fixture.client.close();+ fixture.grantPermission();+ await starting;+ expect(fixture.sockets).toEqual([]);+ expect(fixture.tracksStopped()).toBe(1);+ expect(fixture.errors).toEqual([]);+ });++ it('reports incomplete capture instead of END when the processor never acknowledges flush', async () => {+ const fixture = loadWorkletCapture(sourcePath, { dropFlush: true });+ await fixture.client.start();+ fixture.sockets[0].open();+ await new Promise<void>((resolve) => setImmediate(resolve));+ fixture.processSamples(128);+ fixture.client.stop();+ const deadline = [...fixture.timers.values()].find((timer) => timer.delay === 500);+ expect(deadline).toBeDefined();+ deadline!.callback();+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(fixture.sockets[0].frames).not.toContain('__END__');+ expect(fixture.errors).toHaveLength(1);+ expect(fixture.errors[0].message).toBe('incomplete_capture');+ expect(fixture.tracksStopped()).toBe(1);+ });++ it('cancels a pending flush without a late END or an error after explicit close', async () => {+ const fixture = loadWorkletCapture(sourcePath);+ await fixture.client.start();+ fixture.sockets[0].open();+ await new Promise<void>((resolve) => setImmediate(resolve));+ fixture.processSamples(128);+ fixture.client.stop();+ fixture.client.close();+ await new Promise<void>((resolve) => setImmediate(resolve));+ expect(fixture.sockets[0].frames).not.toContain('__END__');+ expect(fixture.errors).toEqual([]);+ expect(fixture.tracksStopped()).toBe(1);+ });+});++describe.each([+ ['desktop', path.join(__dirname, '../../src/renderer/modules/voice-input.js')],+])('%s compressed audio transport', (_platform, sourcePath) => {+ it('keeps captured speech in order and sends END only after all accepted audio', async () => {+ const fixture = loadCompressedCapture(sourcePath);+ await fixture.client.start();+ fixture.sockets[0].open();+ fixture.capture(0.25);+ fixture.capture(0.5);+ fixture.client.stop();+ await new Promise<void>((resolve) => setImmediate(resolve));+ const framesWhileFirstPending = [...fixture.sockets[0].frames];+ fixture.releaseFirst();+ await vi.waitFor(() => expect(fixture.sockets[0].frames).toHaveLength(4));++ expect(framesWhileFirstPending).toEqual([]);+ expect(fixture.decode(fixture.sockets[0].frames)).toEqual([+ [0xff, 0x1f], [0xff, 0x3f], [], '__END__',+ ]);+ expect(fixture.errors).toEqual([]);+ fixture.client.close();+ });++ it('discards compression owned by a closed socket without leaking it into a restarted connection', async () => {+ const fixture = loadCompressedCapture(sourcePath);+ await fixture.client.start();+ fixture.sockets[0].open();+ fixture.capture(0.25);+ await new Promise<void>((resolve) => setImmediate(resolve));+ fixture.client.close();+ await fixture.client.start();+ fixture.sockets[1].open();+ fixture.capture(0.5);+ await vi.waitFor(() => expect(fixture.sockets[1].frames).toHaveLength(1));+ fixture.releaseFirst();+ await new Promise<void>((resolve) => setImmediate(resolve));++ expect(fixture.sockets[0].frames).toEqual([]);+ expect(fixture.decode(fixture.sockets[1].frames)).toEqual([[0xff, 0x3f]]);+ expect(fixture.errors).toEqual([]);+ fixture.client.close();+ });++ it('reports compression failure instead of sending raw PCM through a gzip connection', async () => {+ const fixture = loadCompressedCapture(sourcePath, true);+ await fixture.client.start();+ fixture.sockets[0].open();+ fixture.capture(0.25);+ await new Promise<void>((resolve) => setImmediate(resolve));++ expect(fixture.errors).toHaveLength(1);+ expect(fixture.errors[0].message).toBe('synthetic gzip failure');+ expect(fixture.sockets[0].frames).toEqual([]);+ fixture.client.close();+ });+});++describe('voice capture interruption', () => {+ it('does not start capture when the user stops while the socket is connecting', async () => {+ const stoppedTracks: string[] = [];+ const opened: string[] = [];+ const errors: string[] = [];+ let socket: any;+ class FakeWebSocket {+ static CONNECTING = 0;+ static OPEN = 1;+ static CLOSED = 3;+ readyState = FakeWebSocket.CONNECTING;+ binaryType = '';+ onopen: Function | null = null;+ onmessage: Function | null = null;+ onerror: Function | null = null;+ onclose: Function | null = null;++ constructor(_url: string) {+ socket = this;+ }++ send() {}++ close() {+ // Chromium emits an error when a CONNECTING socket is closed by the+ // client. It must remain an intentional cancellation to the UI.+ this.onerror?.({ type: 'error' });+ this.readyState = FakeWebSocket.CLOSED;+ this.onclose?.({ code: 1000, reason: '', wasClean: true });+ }++ open() {+ this.readyState = FakeWebSocket.OPEN;+ this.onopen?.();}- return;}- const sent = socket.send.mock.calls.map((call: any[]) => call[0]);- const audio = sent.filter((data: any) => ArrayBuffer.isView(data) && data.byteLength > 0);- expect(audio.reduce((n: number, data: any) => n + data.byteLength, 0)).toBe(7680);- expect(Array.from(audio.at(-1).slice(-2))).toEqual([0, 192]);- expect(sent.filter((value: any) => value === '__END__')).toHaveLength(1);- expect(sent.at(-1)).toBe('__END__');- socket.result('complete spoken note', true);- expect(h.input.value).toBe('Draft: complete spoken note');- });- it('cancels a pending permission request without resurrecting recording', async () => {- vi.useFakeTimers();- const h = load(); let grant: any;- h.getUserMedia.mockImplementation(() => new Promise(resolve => { grant = resolve; }));- await h.button.dispatch('click'); await flush();- await h.button.dispatch('click');- grant({ getTracks: () => [h.track] }); await flush();- expect(h.track.stop).toHaveBeenCalledTimes(1);- expect(h.socket()).toBeUndefined();++ const { context } = loadVoiceInput({+ createSession: async () => ({ ok: true, url: 'wss://example.test/asr' }),+ });+ context.WebSocket = FakeWebSocket;+ context.navigator.mediaDevices = {+ getUserMedia: async () => ({+ getTracks: () => [{ stop: () => stoppedTracks.push('stopped') }],+ }),+ };+ // The close fallback is irrelevant to this deterministic socket and+ // should not keep the test process alive.+ context.setTimeout = () => 1;++ const client = context.window.VoiceInput.createClient('wss://example.test/asr', {+ onOpen: () => opened.push('opened'),+ onError: (error: Error) => errors.push(error.message),+ });+ await client.start();+ client.stop();+ socket.open();++ expect(opened).toEqual([]);+ expect(errors).toEqual([]);+ expect(stoppedTracks).toEqual(['stopped']);+ expect(socket.readyState).toBe(FakeWebSocket.CLOSED);+ });++ it('settles once and releases a late microphone grant after an early stop', async () => {+ let grantMicrophone: Function = () => {};+ const stoppedTracks: string[] = [];+ const closed: string[] = [];+ const { context } = loadVoiceInput({+ createSession: async () => ({ ok: true, url: 'wss://example.test/asr' }),+ });+ context.navigator.mediaDevices = {+ getUserMedia: () => new Promise((resolve) => { grantMicrophone = resolve; }),+ };+ context.setTimeout = () => 1;++ const client = context.window.VoiceInput.createClient('wss://example.test/asr', {+ onClose: () => closed.push('closed'),+ });+ const starting = client.start();+ client.stop();+ grantMicrophone({+ getTracks: () => [{ stop: () => stoppedTracks.push('stopped') }],+ });+ await starting;++ expect(closed).toEqual(['closed']);+ expect(stoppedTracks).toEqual(['stopped']);+ });+});+++// Controller tests keep ticket, microphone and socket boundaries deterministic;+// the capture/wire cases above own audio framing. No account or telemetry facade.+function controllerHarness(voiceOverrides: Record<string, unknown> = {}, globals: Record<string, unknown> = {}) {+ const voice = {+ status: vi.fn(async () => ({ ok: true, configured: true })),+ createSession: vi.fn(async () => ({ ok: true, url: 'wss://example.test/asr?ticket=one-use' })),+ ensureMicrophoneAccess: vi.fn(async () => ({ ok: true, granted: true })),+ ...voiceOverrides,+ };+ const { context, alerts, windowEvents, documentEvents } = loadVoiceInput(voice, undefined, { globals });+ const sockets: any[] = [];+ const stopped = vi.fn();+ const send = vi.fn();+ const timers = new Map<number, Function>();+ let nextTimer = 0;+ context.setTimeout = (callback: Function) => { timers.set(++nextTimer, callback); return nextTimer; };+ context.clearTimeout = (id: number) => timers.delete(id);+ context.sendMessage = send;+ context.sendNewChat = send;+ const listeners: Record<string, Function> = {};+ const textarea = { ...makeTextarea(), value: 'Keep this draft', selectionStart: 15, selectionEnd: 15,+ addEventListener: (name: string, callback: Function) => { listeners[name] = callback; },+ dispatchEvent: vi.fn(),+ };+ let valid = true;+ const replacements: string[] = [];+ context.composerInsertion = () => {+ const from = textarea.selectionStart;+ let to = textarea.selectionEnd;+ let disposed = false;+ return {+ valid: () => valid && !disposed,+ replace(text: string) {+ if (!valid || disposed) return false;+ replacements.push(text);+ textarea.value = textarea.value.slice(0, from) + text + textarea.value.slice(to);+ to = from + text.length;+ return true;+ },+ dispose() { disposed = true; },+ };+ };+ class Socket {+ static CONNECTING = 0; static OPEN = 1; static CLOSED = 3;+ readyState = Socket.CONNECTING;+ onopen: Function | null = null; onmessage: Function | null = null;+ onerror: Function | null = null; onclose: Function | null = null;+ send = vi.fn();+ close = vi.fn(() => { this.readyState = Socket.CLOSED; this.onclose?.({code:1000}); });+ constructor(readonly url: string) { sockets.push(this); }+ open(ready = true) { this.readyState = Socket.OPEN; this.onopen?.(); if (ready) this.ready(); }+ ready() { this.onmessage?.({ data: '{"event":"ready"}' }); }+ result(text: string, last = false) { this.onmessage?.({data:JSON.stringify({result:{text,last}})}); }+ error(reason: string) { this.onmessage?.({data:JSON.stringify({event:'error',reason})}); }+ }+ context.WebSocket = Socket;+ const capture = vi.fn(async () => ({getTracks: () => [{stop:stopped}]}));+ context.navigator.mediaDevices = {getUserMedia:capture};+ const captureNodes = installAudioCaptureStub(context);+ const button = makeButton('chat-mic-btn');+ context.window.VoiceInput.attach(button, textarea);+ const settle = async () => { for (let i = 0; i < 20; i++) await Promise.resolve(); };+ const start = async () => { button.click(); await settle(); };+ const ownerChange = () => listeners['composer-owner-change']?.();+ return { context, voice, alerts, button, textarea, sockets, capture, stopped, send,+ replacements, captureNodes, timers, start, settle, ownerChange, windowEvents, documentEvents, invalidate: () => { valid = false; } };+}++describe('voice input controller ticket and composer ownership', () => {+ it('requests an API key without capturing audio or changing the draft', async () => {+ const h = controllerHarness({status:vi.fn(async () => ({ok:true,configured:false}))});+ await h.start();+ expect(h.context.uiChoice).toHaveBeenCalledWith(expect.objectContaining({ message: h.context.t('chat.mic_key_required') }));+ expect(h.voice.ensureMicrophoneAccess).not.toHaveBeenCalled();+ expect(h.capture).not.toHaveBeenCalled();+ expect(h.sockets).toEqual([]);+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.send).not.toHaveBeenCalled();+ });++ it('honors microphone preflight denial before capture and leaves the draft editable', async () => {+ const h = controllerHarness({ensureMicrophoneAccess:vi.fn(async () => ({ok:false,granted:false}))});+ await h.start();+ expect(h.alerts).toEqual(['Microphone access was blocked. Allow it in your system settings to use voice input.']);+ expect(h.capture).not.toHaveBeenCalled();+ expect(h.sockets).toEqual([]);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ });++ it('reports rejected ticket requests without exposing server errors or consuming the draft', async () => {+ const h = controllerHarness({createSession:vi.fn(async () => { throw new Error('private-ticket-url-and-key'); })});+ await h.start();+ expect(h.alerts).toEqual(['Voice input is unavailable right now.']);- expect(h.capture).not.toHaveBeenCalled();++ expect(h.capture).toHaveBeenCalledTimes(1);++ expect(h.stopped).toHaveBeenCalledTimes(1);++ expect(h.captureNodes).toHaveLength(0);++ expect(h.sockets).toEqual([]);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ });++ it('cancels a pending ticket and permits a new attempt while the old response is outstanding', async () => {+ let resolveTicket: Function = () => {};+ const request = vi.fn().mockImplementationOnce(() => new Promise(resolve => { resolveTicket = resolve; }))+ .mockResolvedValue({ok:true,url:'wss://example.test/asr?ticket=new'});+ const h = controllerHarness({createSession:request});+ await h.start();+ h.button.click();+ expect(h.button.dataset.voicePhase).toBe('idle');+ await h.start();+ expect(h.sockets).toHaveLength(1);+ resolveTicket({ok:true,url:'wss://example.test/asr?ticket=stale'});+ await h.settle();+ expect(h.sockets).toHaveLength(1);+ expect(h.sockets[0].url).toContain('ticket=new');- expect(h.capture).toHaveBeenCalledTimes(1);++ expect(h.capture).toHaveBeenCalledTimes(2);++ expect(h.stopped).toHaveBeenCalledTimes(1);++ expect(h.captureNodes).toHaveLength(0);+ expect(h.button.dataset.voicePhase).toBe('connecting');+ expect(h.alerts).toEqual([]);+ h.ownerChange();++ expect(h.stopped).toHaveBeenCalledTimes(2);+ });++ it('abandons pending microphone preflight when the user stops', async () => {+ let allow: Function = () => {};+ const h = controllerHarness({ensureMicrophoneAccess:vi.fn(() => new Promise(resolve => {allow=resolve;}))});+ await h.start();+ h.button.click();+ allow({ok:true,granted:true});+ await h.settle();+ expect(h.capture).not.toHaveBeenCalled();+ expect(h.sockets).toEqual([]);+ expect(h.alerts).toEqual([]);+ expect(h.button.dataset.voicePhase).toBe('idle');+ });++ it('does not attach an outstanding ticket to a new composer owner', async () => {+ let resolveTicket: Function = () => {};+ const h = controllerHarness({createSession:vi.fn(() => new Promise(resolve => {resolveTicket=resolve;}))});+ await h.start();+ h.ownerChange();+ h.textarea.value = 'Other task draft';+ resolveTicket({ok:true,url:'wss://example.test/asr?ticket=stale'});+ await h.settle();- expect(h.capture).not.toHaveBeenCalled();++ expect(h.capture).toHaveBeenCalledTimes(1);++ expect(h.stopped).toHaveBeenCalledTimes(1);++ expect(h.captureNodes).toHaveLength(0);+ expect(h.sockets).toEqual([]);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Other task draft');+ });++ it('closes recording on owner changes and ignores late transcripts', async () => {+ const h = controllerHarness();+ await h.start();+ const socket = h.sockets[0]; socket.open();+ h.ownerChange();+ h.textarea.value = 'New owner draft';+ socket.result('stale transcript', true);+ expect(socket.close).toHaveBeenCalledTimes(1);+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.replacements).toEqual([]);+ expect(h.textarea.value).toBe('New owner draft');+ expect(h.timers.size).toBe(0);expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.send).not.toHaveBeenCalled();+ });++ it('ignores a prior socket close while a later attempt is connecting', async () => {+ const h = controllerHarness();+ await h.start();+ const old = h.sockets[0];+ h.button.click();+ await h.start();+ const current = h.sockets[1];+ old.onclose?.({code:1000});+ expect(h.button.dataset.voicePhase).toBe('connecting');+ expect(current.close).not.toHaveBeenCalled();+ expect(h.alerts).toEqual([]);+ current.open();+ expect(h.button.dataset.voicePhase).toBe('recording');+ h.ownerChange();+ });++ it('recovers from socket handshake failure with no raw ticket error in the UI', async () => {+ const h = controllerHarness();+ await h.start();+ h.sockets[0].onerror?.({type:'error',message:'private-ticket'});+ expect(h.alerts).toEqual(['Voice input is unavailable right now.']);+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.sockets[0].close).toHaveBeenCalledTimes(1);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ });++ it.each([['invalid_api_key','chat.mic_invalid_key'],+ ['insufficient_quota','chat.mic_insufficient_credits']])('keeps %s failures recoverable without sending', async (reason,message) => {+ const h = controllerHarness();+ await h.start(); h.sockets[0].open(); h.sockets[0].error(reason);+ expect(h.context.uiChoice).toHaveBeenCalledWith(expect.objectContaining({ message: h.context.t(message) }));+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.timers.size).toBe(0);+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.send).not.toHaveBeenCalled();+ });++ it('retains a partial transcript for review if the socket closes before a terminal result', async () => {+ const h = controllerHarness();+ await h.start(); const socket = h.sockets[0]; socket.open();+ socket.result('partial words');+ socket.close(); await h.settle();+ expect(h.textarea.value).toBe('Keep this draft partial words');+ expect(h.alerts).toEqual([h.context.t('chat.mic_incomplete')]);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.send).not.toHaveBeenCalled();+ expect(h.textarea.dispatchEvent).not.toHaveBeenCalled();+ });++ it('replaces the owned transcript range and keeps final words for explicit send', async () => {+ const h = controllerHarness();+ await h.start(); const socket = h.sockets[0]; socket.open();+ socket.result('first words');+ h.button.click();+ expect(h.button.dataset.voicePhase).toBe('finalizing');+ socket.result('corrected final words',true); await h.settle();+ expect(h.textarea.value).toBe('Keep this draft corrected final words');+ expect(h.replacements).toEqual([' first words',' corrected final words']);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.send).not.toHaveBeenCalled();+ expect(h.textarea.dispatchEvent).not.toHaveBeenCalled();+ expect(h.alerts).toEqual([]);+ });++ it('does not overwrite a range invalidated by an intervening user edit', async () => {+ const h = controllerHarness();+ await h.start(); const socket = h.sockets[0]; socket.open();+ h.invalidate(); h.textarea.value = 'Manually corrected draft';+ socket.result('late words',true);+ expect(h.textarea.value).toBe('Manually corrected draft');+ expect(h.replacements).toEqual([]);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.send).not.toHaveBeenCalled();+ });+});++// Remote UI/permission guidance uses the existing controller, preserving all+// wire-order and composer-owner regression cases above.+describe('public voice recording controls', () => {+ it('records one bounded timing summary without retaining the key, audio or recognized text', async () => {+ let now = 0;+ const records: any[] = [];+ const h = controllerHarness({}, { performance: { now: () => now },+ createLogger: () => ({ info: (...args: any[]) => records.push(args), warn() {}, error() {} }) });+ await h.start(); now = 300; h.sockets[0].open(false);+ now = 900; h.sockets[0].ready(); await h.settle();+ now = 1400; h.sockets[0].result('private spoken text');+ now = 2000; h.button.click();+ now = 2400; h.sockets[0].result('private spoken text', true); await h.settle();+ const summaries = records.filter(record => record[0] === 'voice timing');+ expect(summaries).toHaveLength(1);+ expect(summaries[0][1]).toMatchObject({ socket_open_ms: 300, service_ready_ms: 900,+ capture_ready_ms: 900, first_text_ms: 1400, stop_ms: 2000, final_text_ms: 2400,+ final_wait_ms: 400, total_ms: 2400 });+ expect(JSON.stringify(records)).not.toMatch(/private spoken text|one-use|example.test/);+ });++ it('waits for service readiness before recording, and a duplicate ready cannot recreate capture', async () => {+ const h = controllerHarness();+ await h.start();+ const socket = h.sockets[0]; socket.open(false);expect(h.button.classList.contains('is-recording')).toBe(false);- expect(h.button.classList.contains('is-busy')).toBe(false);- });- it('does not resurrect capture when stopped during worklet initialization', async () => {- vi.useFakeTimers();- const h = load();- let ready: () => void = () => {};- const BaseContext = h.context.window.AudioContext;- h.context.window.AudioContext = class extends BaseContext {- audioWorklet = { addModule: () => new Promise<void>((resolve) => { ready = resolve; }) };+ expect(h.button.dataset.voicePhase).toBe('connecting');+ expect(h.captureNodes).toHaveLength(0);+ expect(socket.send).not.toHaveBeenCalled();+ socket.ready(); await h.settle();+ expect(h.button.dataset.voicePhase).toBe('recording');+ expect(h.captureNodes).toHaveLength(1);+ socket.ready(); await h.settle();+ expect(h.captureNodes).toHaveLength(1);+ h.ownerChange();+ });+ it('cancels while waiting for ready without capturing late audio or altering the draft', async () => {+ const h = controllerHarness(); await h.start();+ const socket = h.sockets[0]; socket.open(false);+ h.button.click(); socket.ready(); await h.settle();+ expect(h.captureNodes).toHaveLength(0);+ expect(socket.send).not.toHaveBeenCalled();+ expect(socket.close).toHaveBeenCalledTimes(1);+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.alerts).toEqual([]);+ expect(h.timers.size).toBe(0);+ });+ it('bounds a missing ready event and lets the user retry with the draft intact', async () => {+ const h = controllerHarness(); await h.start();+ const socket = h.sockets[0]; socket.open(false);+ expect(h.timers.size).toBe(1);+ [...h.timers.values()][0](); await h.settle();+ expect(h.captureNodes).toHaveLength(0);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.alerts).toEqual([h.context.t('chat.mic_error_unavailable')]);+ await h.start(); h.sockets[1].open();+ expect(h.button.dataset.voicePhase).toBe('recording');+ h.ownerChange();+ });+ it('starts without an introduction and keeps icon-only state across repeat attempts', async () => {+ const h = controllerHarness();+ const state = (phase: string, recording: boolean, busy: boolean) => {+ expect(h.button.dataset.voicePhase).toBe(phase);+ expect(h.button.classList.contains('is-recording')).toBe(recording);+ expect(h.button.classList.contains('is-busy')).toBe(busy);+ expect(h.button.parentNode.insertBefore).not.toHaveBeenCalled();};- h.context.Blob = Blob;- const WorkletNode = vi.fn();- h.context.window.AudioWorkletNode = h.context.AudioWorkletNode = WorkletNode;- await h.button.dispatch('click'); await flush();- const socket = h.socket(); socket.open(); await flush();- await h.button.dispatch('click'); await flush();- ready(); await flush();- expect(WorkletNode).not.toHaveBeenCalled();- expect(h.track.stop).toHaveBeenCalledTimes(1);- expect(h.context.uiAlert).not.toHaveBeenCalled();- socket.result('review this note', true);- expect(h.input.value).toBe('Draft: review this note');+ state('idle', false, false);+ for (let n = 0; n < 2; n++) {+ await h.start(); state('connecting', false, true);+ const socket = h.sockets[n]; socket.open(); state('recording', true, false);+ h.button.click(); state('finalizing', false, true);+ expect(h.button.title).toBe(h.context.t('chat.mic_finalizing'));+ socket.result('review these words', true); await h.settle(); state('idle', false, false);+ }+ expect(h.context.uiConfirm).not.toHaveBeenCalled();+ expect(h.send).not.toHaveBeenCalled();+ });+ it('keeps a failed status lookup distinct from a missing key', async () => {+ const h = controllerHarness({ status: vi.fn(async () => ({ ok: false })) });+ await h.start();+ expect(h.alerts).toEqual([h.context.t('chat.mic_error_unavailable')]);+ expect(h.context.uiChoice).not.toHaveBeenCalled();+ expect(h.voice.createSession).not.toHaveBeenCalled();+ expect(h.capture).not.toHaveBeenCalled();+ expect(h.textarea.value).toBe('Keep this draft');+ });+ it.each(['Escape', 'hidden', 'unload', 'explicit'])('releases a recording on %s and ignores late transcript', async (event) => {+ const h = controllerHarness(); await h.start(); const socket = h.sockets[0]; socket.open();+ if (event === 'Escape') h.documentEvents.keydown({ key: 'Escape' });+ if (event === 'hidden') { h.context.document.hidden = true; h.documentEvents.visibilitychange(); }+ if (event === 'unload') h.windowEvents.beforeunload();+ if (event === 'explicit') h.context.window.VoiceInput.cancel();+ socket.result('late text', true);+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.alerts).toEqual([]);+ expect(h.send).not.toHaveBeenCalled();+ });+});++// Union boundaries: bounded diagnostics and precise recovery remain independent+// of successful transcript insertion. Transport faults cannot silently lose audio.+describe('voice union failure boundaries', () => {+ it.each(['private-upstream-reason', 'network'])('bounds diagnostic code for %s without retaining payloads', async (reason) => {+ const records: unknown[][] = [];+ const log = (...args: unknown[]) => records.push(args);+ const h = controllerHarness({}, { createLogger: () => ({ info: log, warn: log, error: log }) });+ await h.start(); const socket = h.sockets[0]; socket.open(); socket.error(reason);+ const diagnostic = records.find(entry => entry[0] === 'voice client error') as any;+ expect(diagnostic).toBeDefined();+ expect(diagnostic[1].error).toEqual({ code: reason === 'network' ? 'network' : 'unknown' });+ if (reason !== 'network') expect(JSON.stringify(records)).not.toContain(reason);+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.send).not.toHaveBeenCalled();+ });+ it('distinguishes a system-policy restriction before ticket exchange or capture', async () => {+ const h = controllerHarness({ ensureMicrophoneAccess: vi.fn(async () => ({ ok: true, granted: false, status: 'restricted' })) });+ await h.start();+ expect(h.alerts).toEqual([h.context.t('chat.mic_error_restricted')]);+ expect(h.voice.createSession).not.toHaveBeenCalled();+ expect(h.capture).not.toHaveBeenCalled();+ expect(h.sockets).toEqual([]);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ });+ it('allows the exact 256KiB queued boundary and cancels before adding audio beyond it', async () => {+ const h = controllerHarness(); await h.start(); const socket = h.sockets[0]; socket.open();+ const capture = (sample: number) => h.captureNodes[0].onaudioprocess({+ inputBuffer: { getChannelData: () => new Float32Array([sample, sample, sample]) },+ });+ socket.bufferedAmount = 256 * 1024; capture(.25); await h.settle();+ expect(socket.send).toHaveBeenCalledTimes(1);+ socket.bufferedAmount++; capture(.5); await h.settle();+ expect(socket.send).toHaveBeenCalledTimes(1);+ expect(socket.close).toHaveBeenCalledTimes(1);+ expect(h.alerts).toEqual([h.context.t('chat.mic_error_unavailable')]);+ expect(h.stopped).toHaveBeenCalledTimes(1);+ expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.send).not.toHaveBeenCalled();+ });+ it('reports a socket send exception and closes capture without leaking the raw failure', async () => {+ const records: unknown[][] = []; const log = (...args: unknown[]) => records.push(args);+ const h = controllerHarness({}, { createLogger: () => ({ info: log, warn: log, error: log }) });+ await h.start(); const socket = h.sockets[0]; socket.open();+ socket.send.mockImplementation(() => { throw new Error('private-websocket-send-detail'); });+ h.captureNodes[0].onaudioprocess({ inputBuffer: { getChannelData: () => new Float32Array([.25, .25, .25]) } });+ await h.settle();+ expect(socket.send).toHaveBeenCalledTimes(1);+ expect(socket.close).toHaveBeenCalledTimes(1);+ expect(JSON.stringify(records)).not.toContain('private-websocket-send-detail');+ expect(h.alerts).toEqual([h.context.t('chat.mic_error_unavailable')]);+ expect(h.stopped).toHaveBeenCalledTimes(1);expect(h.button.dataset.voicePhase).toBe('idle');+ expect(h.textarea.value).toBe('Keep this draft');+ expect(h.send).not.toHaveBeenCalled();});- it('keeps the selected draft untouched when recognition produces no speech', async () => {- vi.useFakeTimers();- const h = load();- const replace = vi.fn(() => true);- h.context.composerInsertion = () => ({ replace, dispose() {} });- await h.button.dispatch('click'); await flush();- h.socket().open(); await flush();- h.socket().result('', true);- expect(replace).not.toHaveBeenCalled();- expect(h.context.uiAlert).toHaveBeenCalledWith('chat.mic_empty');- expect(h.track.stop).toHaveBeenCalledTimes(1);- });- it('releases capture and ignores late transcripts after the composer changes owner', async () => {- vi.useFakeTimers();- const h = load();- await h.button.dispatch('click'); await flush();- const socket = h.socket(); socket.open(); await flush();- h.input.dispatch('composer-owner-change');- socket.result('a previous conversation', true);- expect(h.input.value).toBe('Draft:');- expect(h.track.stop).toHaveBeenCalledTimes(1);+});++++// Remote microphone admission and hidden-composer cleanup must coexist with++// local ready/gzip/ordering/epoch ownership, not replace that transport.++describe('merged remote voice admission and editor lifetime', () => {++ it.each(['NotFoundError', 'NotReadableError'])('does not request a ticket when microphone acquisition fails with %s', async (name) => {++ const h = controllerHarness();++ h.capture.mockRejectedValue(Object.assign(new Error(), { name }));++ await h.start();++ expect(h.voice.createSession).not.toHaveBeenCalled();++ expect(h.sockets).toEqual([]);++ expect(h.captureNodes).toHaveLength(0);++ expect(h.textarea.value).toBe('Keep this draft');++ expect(h.alerts).toEqual([h.context.t(name === 'NotFoundError' ? 'chat.mic_error_no_device' : 'chat.mic_error_busy')]);+ expect(h.button.dataset.voicePhase).toBe('idle');++ expect(h.send).not.toHaveBeenCalled();+ });++++ it('releases a late microphone grant after cancellation without requesting a ticket', async () => {++ const h = controllerHarness();++ let grant!: (stream: unknown) => void;++ h.capture.mockImplementation(() => new Promise(resolve => { grant = resolve; }));++ await h.start();++ h.button.click();++ grant({ getTracks: () => [{ stop: h.stopped }] });++ await h.settle();++ expect(h.voice.createSession).not.toHaveBeenCalled();++ expect(h.stopped).toHaveBeenCalledTimes(1);++ expect(h.sockets).toEqual([]);++ expect(h.button.dataset.voicePhase).toBe('idle');++ expect(h.textarea.value).toBe('Keep this draft');++ expect(h.alerts).toEqual([]);++ });++++ it('preserves a selected draft range when a terminal response contains no speech', async () => {++ const h = controllerHarness(); h.textarea.selectionStart = 0;++ await h.start(); h.sockets[0].open(); h.sockets[0].result('', true); await h.settle();++ expect(h.replacements).toEqual([]);++ expect(h.textarea.value).toBe('Keep this draft');++ expect(h.alerts).toEqual([h.context.t('chat.mic_empty')]);++ expect(h.stopped).toHaveBeenCalledTimes(1);++ expect(h.button.dataset.voicePhase).toBe('idle');++ expect(h.send).not.toHaveBeenCalled();++ });++++ it('cancels recording when its editor becomes hidden and clears its visibility watcher', async () => {++ const h = controllerHarness();++ const watchers = new Map<number, Function>(); let next = 0;++ h.context.setInterval = (fn: Function) => { watchers.set(++next, fn); return next; };++ h.context.clearInterval = (id: number) => watchers.delete(id);++ await h.start(); const socket = h.sockets[0]; socket.open(); await h.settle();++ expect(watchers.size).toBe(1);++ h.textarea.getClientRects = () => [];++ for (const callback of [...watchers.values()]) callback();++ socket.result('belongs to the hidden composer', true); await h.settle();++ expect(watchers.size).toBe(0);++ expect(h.stopped).toHaveBeenCalledTimes(1);++ expect(socket.close).toHaveBeenCalledTimes(1);++ expect(h.button.dataset.voicePhase).toBe('idle');++ expect(h.textarea.value).toBe('Keep this draft');++ expect(h.alerts).toEqual([]);++ expect(h.send).not.toHaveBeenCalled();++ });++ it('ignores an old ticket result after the same client starts a replacement capture', async () => {++ const h = controllerHarness();++ let resolveOld!: (url: string) => void;++ const resolveUrl = vi.fn().mockImplementationOnce(() => new Promise<string>(resolve => { resolveOld = resolve; }))++ .mockResolvedValue('wss://example.test/asr?ticket=replacement');++ const result = vi.fn();++ const client = h.context.window.VoiceInput.createClient(resolveUrl, { onResult: result });++ const first = client.start(); await h.settle();++ client.close();++ await client.start(); const socket = h.sockets[0]; socket.open(); await h.settle();++ expect(h.stopped).toHaveBeenCalledTimes(1);++ resolveOld('wss://example.test/asr?ticket=obsolete'); await first; await h.settle();++ expect(h.sockets).toHaveLength(1);++ expect(socket.url).toContain('ticket=replacement');++ expect(h.stopped).toHaveBeenCalledTimes(1);++ h.captureNodes[0].onaudioprocess({ inputBuffer: { getChannelData: () => new Float32Array([.25, .25, .25]) } });++ await h.settle();++ expect(Array.from(socket.send.mock.calls[0][0] as Uint8Array)).toEqual([255, 31]);++ socket.result('current speech', true);++ expect(result).toHaveBeenCalledExactlyOnceWith({ text: 'current speech', last: true });++ client.close();++ expect(h.stopped).toHaveBeenCalledTimes(2);++ });+++ });