Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions frontend/src/composables/useVoiceInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FakeMediaRecorder {
ondataavailable: ((e: { data: Blob }) => void) | null = null
onstart: (() => void) | null = null
onstop: (() => void) | null = null
onerror: ((event: Event) => void) | null = null
constructor() { FakeMediaRecorder.instances.push(this) }
start() {
this.state = 'recording'
Expand Down Expand Up @@ -240,3 +241,56 @@ test('unmount suppresses pending transcription and releases the microphone', asy
await f.voice.start()
assert.equal(FakeMediaRecorder.instances.length, 1, 'disposed input cannot restart capture')
})

for (const stage of ['constructor', 'start'] as const) {
test(`recorder ${stage} failure releases the microphone and allows retry`, async (t) => {
t.mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'] })
const { voice, tracks, errors, sent } = setup()
const error = new DOMException('Encoder unavailable', 'NotSupportedError')
if (stage === 'constructor') {
globalThis.MediaRecorder = class extends FakeMediaRecorder {
constructor() { super(); throw error }
} as unknown as typeof MediaRecorder
} else {
t.mock.method(FakeMediaRecorder.prototype, 'start', () => { throw error })
}
await voice.start()
assert.equal(voice.state.value, 'idle')
assert.equal(tracks[0].readyState, 'ended')
assert.deepEqual(errors, ['device'])
assert.deepEqual(sent, [])
t.mock.restoreAll()
globalThis.MediaRecorder = FakeMediaRecorder as unknown as typeof MediaRecorder
await voice.start()
assert.equal(voice.state.value, 'recording')
assert.equal(tracks.length, 2)
voice.cancel()
})
}

test('recorder error discards partial audio, clears timers and permits another recording', async (t) => {
t.mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'] })
const { voice, errors, tracks, sent, texts } = setup()
await voice.start()
t.mock.timers.tick(1000)
voice.stop()
const failed = FakeMediaRecorder.instances[0]
failed.onerror?.(new Event('error'))
// Browsers dispatch dataavailable and stop after a recording error.
failed.ondataavailable?.({ data: new Blob(['incomplete recording']) })
failed.onstop?.()
await flush()
assert.equal(voice.state.value, 'idle')
assert.equal(tracks[0].readyState, 'ended')
assert.deepEqual(errors, ['device'])
assert.deepEqual(sent, [])
assert.deepEqual(texts, [])
await voice.start()
assert.equal(voice.state.value, 'recording')
t.mock.timers.tick(VOICE_TAIL_MS)
assert.equal(voice.state.value, 'recording', 'old stop timer cannot stop the new recording')
voice.cancel()
const elapsed = voice.elapsedMs.value
t.mock.timers.tick(1000)
assert.equal(voice.elapsedMs.value, elapsed, 'failed recording timer was cleared')
})
33 changes: 31 additions & 2 deletions frontend/src/composables/useVoiceInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ export function useVoiceInput(options: {
}
};

const failRecording = (error: unknown) => {
if (recorder) {
// Error events are followed by dataavailable/stop. Do not transcribe
// their partial audio or let them reset a subsequent recording.
recorder.onstart = null;
recorder.ondataavailable = null;
recorder.onstop = null;
recorder.onerror = null;
}
recorder = null;
startedAt = null;
chunks = [];
clearRecordingTimers();
releaseStream();
state.value = 'idle';
options.onError('device', error);
};

const start = async () => {
if (disposed || state.value !== 'idle') return;
if (typeof window !== 'undefined' && !window.isSecureContext) {
Expand Down Expand Up @@ -162,8 +180,15 @@ export function useVoiceInput(options: {
}

chunks = [];
const active = new MediaRecorder(stream!);
let active: MediaRecorder;
try {
active = new MediaRecorder(stream!);
} catch (err) {
failRecording(err);
return;
}
recorder = active;
active.onerror = failRecording;
active.ondataavailable = (e) => { if (e.data.size > 0) chunks.push(e.data); };
active.onstart = () => {
const begin = Date.now();
Expand All @@ -188,7 +213,11 @@ export function useVoiceInput(options: {
}
void transcribe(new Blob(chunks, { type }), durationMs);
};
active.start();
try {
active.start();
} catch (err) {
failRecording(err);
}
};

const stop = () => {
Expand Down