Files
opencode-manager/frontend/src/hooks/useVoiceConversation.test.tsx
T
Chris Scott 67cfd862b1 fix(tts): let voice mode read replies aloud on iOS
iOS rejects play() outside a user gesture unless the same media element was
already played inside one. Each chunk used a new Audio element, so replies in
voice mode failed with "Reading the reply failed".

- reuse one audio element for all external TTS playback
- add tts.unlock(), which plays a short silent clip on that element, and call
  it from the voice mode start and orb taps
- route every playback failure through failPlayback(message) and pass the
  message to onEnd so voice mode shows the underlying reason
- report a blocked play() as "Audio playback was blocked by the browser"
2026-10-23 14:08:33 -04:00

601 lines
21 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { act, renderHook, waitFor } from '@testing-library/react'
import { useReducer, useEffect } from 'react'
import { VOICE_MODE_INSTRUCTION_KEY, VOICE_NO_SPEECH_TIMEOUT_MS } from '@opencode-manager/shared/opencode'
import type { SessionMessageAssistant } from '@opencode-manager/shared/opencode'
import type { TTSEndReason } from '@/contexts/tts-context'
import type { SttEndOfSpeechReason } from '@/lib/webSpeechRecognizer'
import { useVoiceConversation } from './useVoiceConversation'
import type { UseVoiceConversationInput } from './useVoiceConversation'
interface MockStartOptions {
endpointing?: { pauseMs: number; noSpeechTimeoutMs: number }
onEndOfSpeech?: (reason: SttEndOfSpeechReason, finalText: string) => void
}
interface MockSttValues {
isRecording: boolean
isStarting: boolean
isProcessing: boolean
isEndpointing: boolean
isExternalProvider: boolean
isEnabled: boolean
isSupported: boolean
isError: boolean
error: string | null
transcript: string
interimTranscript: string
state: 'idle' | 'listening' | 'processing' | 'error'
}
const harness = vi.hoisted(() => ({
useStt: vi.fn(),
useTts: vi.fn(),
putSessionInstruction: vi.fn(),
removeSessionInstruction: vi.fn(),
listSessionInstructionKeys: vi.fn(),
startRecording: vi.fn(),
stopRecording: vi.fn(),
abortRecording: vi.fn(),
speakMessage: vi.fn(),
ttsStop: vi.fn(),
ttsUnlock: vi.fn(),
ttsPause: vi.fn(),
ttsResume: vi.fn(),
sendTurn: vi.fn(),
interrupt: vi.fn(),
onEditInTextBox: vi.fn(),
}))
vi.mock('@/hooks/useSTT', () => ({ useSTT: harness.useStt }))
vi.mock('@/hooks/useTTS', () => ({ useTTS: harness.useTts }))
vi.mock('@/api/opencode', () => ({
putSessionInstruction: harness.putSessionInstruction,
removeSessionInstruction: harness.removeSessionInstruction,
listSessionInstructionKeys: harness.listSessionInstructionKeys,
}))
const sttListeners = new Set<() => void>()
const sttValue: MockSttValues = {
isRecording: false,
isStarting: false,
isProcessing: false,
isEndpointing: false,
isExternalProvider: false,
isEnabled: true,
isSupported: true,
isError: false,
error: null,
transcript: '',
interimTranscript: '',
state: 'idle',
}
const ttsValue = {
isEnabled: true,
isPlaying: false,
isPaused: false,
error: null as string | null,
speakMessage: harness.speakMessage,
stop: harness.ttsStop,
unlock: harness.ttsUnlock,
pause: harness.ttsPause,
resume: harness.ttsResume,
}
function useMockStt() {
const [, forceRender] = useReducer((value: number) => value + 1, 0)
useEffect(() => {
sttListeners.add(forceRender)
return () => {
sttListeners.delete(forceRender)
}
}, [])
return {
...sttValue,
startRecording: harness.startRecording,
stopRecording: harness.stopRecording,
abortRecording: harness.abortRecording,
}
}
function updateStt(patch: Partial<MockSttValues>) {
Object.assign(sttValue, patch)
act(() => {
for (const listener of sttListeners) listener()
})
}
function lastStartOptions(): MockStartOptions {
const calls = harness.startRecording.mock.calls
return (calls[calls.length - 1]?.[0] ?? {}) as MockStartOptions
}
async function endOfSpeech(reason: SttEndOfSpeechReason, finalText: string) {
const options = lastStartOptions()
await act(async () => {
options.onEndOfSpeech?.(reason, finalText)
})
}
function speechEndHandler(callIndex = 0): (reason: TTSEndReason, error?: string) => void {
const options = harness.speakMessage.mock.calls[callIndex]?.[2] as
| { onEnd?: (reason: TTSEndReason, error?: string) => void }
| undefined
if (!options?.onEnd) throw new Error('speakMessage was not called with an onEnd handler')
return options.onEnd
}
function assistantMessage(id: string, text: string): SessionMessageAssistant {
return {
id,
type: 'assistant',
agent: 'build',
model: { id: 'claude', providerID: 'anthropic' },
content: [{ type: 'text', text }],
time: { created: 1, completed: 2 },
}
}
function createProps(
overrides: Partial<UseVoiceConversationInput> = {},
): UseVoiceConversationInput {
return {
sessionID: 'ses_1',
serverId: 'local',
messages: [],
pending: [],
status: 'idle',
hasPendingApproval: false,
pauseMs: 1200,
sendTurn: harness.sendTurn,
interrupt: harness.interrupt,
onEditInTextBox: harness.onEditInTextBox,
...overrides,
}
}
describe('useVoiceConversation', () => {
beforeEach(() => {
vi.clearAllMocks()
Object.assign(sttValue, {
isRecording: false,
isStarting: false,
isProcessing: false,
isEndpointing: false,
isExternalProvider: false,
isEnabled: true,
isSupported: true,
isError: false,
error: null,
transcript: '',
interimTranscript: '',
state: 'idle',
} satisfies MockSttValues)
Object.assign(ttsValue, {
isEnabled: true,
isPlaying: false,
isPaused: false,
error: null,
})
sttListeners.clear()
harness.useStt.mockImplementation(useMockStt)
harness.useTts.mockImplementation(() => ttsValue)
harness.putSessionInstruction.mockResolvedValue(undefined)
harness.removeSessionInstruction.mockResolvedValue(undefined)
harness.listSessionInstructionKeys.mockResolvedValue([])
harness.sendTurn.mockResolvedValue(undefined)
harness.interrupt.mockResolvedValue(undefined)
harness.startRecording.mockResolvedValue(true)
})
afterEach(() => {
vi.useRealTimers()
})
it('starts listening and opens capture with the pause budget when idle', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(result.current.isActive).toBe(true)
expect(harness.ttsUnlock).toHaveBeenCalledTimes(1)
expect(result.current.label).toBe('Listening')
expect(harness.startRecording).toHaveBeenCalledTimes(1)
expect(lastStartOptions().endpointing).toEqual({ pauseMs: 1200, noSpeechTimeoutMs: 10_000 })
expect(harness.putSessionInstruction).toHaveBeenCalledWith(
'ses_1',
VOICE_MODE_INSTRUCTION_KEY,
expect.any(String),
'local',
)
})
it('sends a finished utterance and works on the reply', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
expect(harness.sendTurn).toHaveBeenCalledWith('hello there', { busy: false })
expect(result.current.label).toBe('Working')
})
it('reads a completed reply and listens again once it ends', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('speaking'))
expect(harness.speakMessage).toHaveBeenCalledWith('msg_1', 'Done', expect.any(Object))
expect(result.current.label).toBe('Speaking')
expect(result.current.canStopAgent).toBe(false)
act(() => speechEndHandler(0)('completed'))
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(harness.startRecording).toHaveBeenCalledTimes(2)
expect(result.current.label).toBe('Listening')
})
it('marks replies as heard without speaking when text-to-speech is disabled', async () => {
ttsValue.isEnabled = false
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(harness.speakMessage).not.toHaveBeenCalled()
expect(harness.startRecording).toHaveBeenCalledTimes(2)
})
it('records a message while the agent works and sends it as busy', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps({ status: 'busy' }),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('working'))
expect(result.current.orbLabel).toBe('Add a message')
act(() => result.current.tapOrb())
await waitFor(() => expect(result.current.phase).toBe('recording'))
expect(harness.ttsUnlock).toHaveBeenCalledTimes(2)
expect(harness.startRecording).toHaveBeenCalledTimes(1)
expect(result.current.label).toBe('Adding to queue')
await endOfSpeech('pause', 'also check the tests')
await waitFor(() => expect(result.current.phase).toBe('working'))
expect(harness.sendTurn).toHaveBeenCalledWith('also check the tests', { busy: true })
})
it('pauses and resumes a reading that is held mid-reply', async () => {
ttsValue.isPlaying = true
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('speaking'))
act(() => result.current.pause())
expect(harness.ttsPause).toHaveBeenCalledTimes(1)
expect(result.current.phase).toBe('paused')
expect(result.current.hasHeldReading).toBe(true)
expect(result.current.orbLabel).toBe('Continue reading')
expect(result.current.hint).toBe('Tap to continue')
ttsValue.isPaused = true
act(() => result.current.resume())
expect(harness.ttsResume).toHaveBeenCalledTimes(1)
expect(result.current.phase).toBe('speaking')
expect(result.current.hasHeldReading).toBe(false)
})
it('removes the session instruction on exit', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
act(() => result.current.exit())
expect(result.current.phase).toBe('off')
expect(result.current.isActive).toBe(false)
await waitFor(() =>
expect(harness.removeSessionInstruction).toHaveBeenCalledWith(
'ses_1',
VOICE_MODE_INSTRUCTION_KEY,
'local',
),
)
})
it('hands the live transcript to the composer when editing in the text box', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({ transcript: 'edit this turn' })
expect(result.current.liveTranscript).toBe('edit this turn')
expect(result.current.canEditInTextBox).toBe(true)
act(() => result.current.editInTextBox())
await waitFor(() => expect(harness.onEditInTextBox).toHaveBeenCalledWith('edit this turn'))
expect(result.current.phase).toBe('off')
})
it('ignores speech-to-text placeholders in the live transcript', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({ isRecording: true, interimTranscript: 'Recording...' })
expect(result.current.liveTranscript).toBe('')
expect(result.current.canEditInTextBox).toBe(false)
})
it('keeps an external-provider dictation open past the no-speech timeout', async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({ isExternalProvider: true, isRecording: true, interimTranscript: 'Recording...' })
act(() => {
vi.advanceTimersByTime(VOICE_NO_SPEECH_TIMEOUT_MS + 2_000)
})
expect(harness.abortRecording).not.toHaveBeenCalled()
expect(result.current.phase).toBe('listening')
expect(result.current.label).toBe('Listening')
})
it('keeps an external-provider capture running when the session starts a turn', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({ isExternalProvider: true, isRecording: true, interimTranscript: 'Recording...' })
rerender(createProps({ status: 'busy' }))
expect(harness.abortRecording).not.toHaveBeenCalled()
expect(result.current.phase).toBe('listening')
expect(result.current.label).toBe('Listening')
})
it('pauses when the browser provider captures no speech for the timeout', async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({ isRecording: true })
act(() => {
vi.advanceTimersByTime(VOICE_NO_SPEECH_TIMEOUT_MS)
})
expect(harness.abortRecording).toHaveBeenCalled()
expect(result.current.phase).toBe('paused')
expect(result.current.label).toBe("Didn't catch that")
})
it('pauses without a message when the turn produces no speech', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('no-speech', '')
expect(harness.abortRecording).toHaveBeenCalled()
expect(harness.sendTurn).not.toHaveBeenCalled()
expect(result.current.phase).toBe('paused')
expect(result.current.label).toBe("Didn't catch that")
expect(result.current.hint).toBe('Tap to talk')
expect(result.current.orbLabel).toBe('Resume voice conversation')
})
it('pauses with an error when capture cannot start', async () => {
harness.startRecording.mockResolvedValue(false)
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('paused'))
expect(result.current.label).toBe('Speech recognition failed')
expect(result.current.hint).toBe('Allow microphone access, then tap to retry')
})
it('shows the specific microphone failure and how to fix it', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({
isError: true,
error: 'Microphone access denied. Please allow microphone permissions.',
})
await waitFor(() => expect(result.current.phase).toBe('paused'))
expect(result.current.label).toBe(
'Microphone access denied. Please allow microphone permissions.',
)
expect(result.current.hint).toBe('Allow microphone access, then tap to retry')
})
it('keeps a plain retry for a failure that retrying can clear', async () => {
harness.sendTurn.mockRejectedValueOnce(new Error('network down'))
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('paused'))
expect(result.current.label).toBe('network down')
expect(result.current.hint).toBe('Tap to retry')
})
it('reports that the microphone is starting until capture begins', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({ isStarting: true })
expect(result.current.label).toBe('Starting…')
expect(result.current.hint).toBe('')
updateStt({ isStarting: false, isRecording: true })
expect(result.current.label).toBe('Listening')
expect(result.current.hint).toBe('Tap to send')
})
it('pauses when speech recognition reports an error while listening', async () => {
const { result } = renderHook((props: UseVoiceConversationInput) => useVoiceConversation(props), {
initialProps: createProps(),
})
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
updateStt({ isError: true, error: 'Microphone access denied' })
await waitFor(() => expect(result.current.phase).toBe('paused'))
expect(result.current.label).toBe('Microphone access denied')
})
it('pauses instead of finishing the turn when reading the reply fails', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('speaking'))
act(() => speechEndHandler(0)('error', 'Audio playback was blocked by the browser'))
expect(result.current.phase).toBe('paused')
expect(result.current.label).toBe(
'Reading the reply failed: Audio playback was blocked by the browser',
)
expect(result.current.hint).toBe('Tap to retry')
expect(harness.startRecording).toHaveBeenCalledTimes(1)
})
it('keeps the failure label plain when the player reports no detail', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', 'Done')] }))
await waitFor(() => expect(result.current.phase).toBe('speaking'))
act(() => speechEndHandler(0)('error'))
expect(result.current.phase).toBe('paused')
expect(result.current.label).toBe('Reading the reply failed')
})
it('marks a reply with nothing to read as heard and listens again', async () => {
const { result, rerender } = renderHook(
(props: UseVoiceConversationInput) => useVoiceConversation(props),
{ initialProps: createProps() },
)
act(() => result.current.start())
await waitFor(() => expect(result.current.phase).toBe('listening'))
await endOfSpeech('pause', 'hello there')
await waitFor(() => expect(result.current.phase).toBe('working'))
rerender(createProps({ messages: [assistantMessage('msg_1', '```ts\nconst answer = 42\n```')] }))
await waitFor(() => expect(result.current.phase).toBe('listening'))
expect(harness.speakMessage).not.toHaveBeenCalled()
expect(harness.startRecording).toHaveBeenCalledTimes(2)
})
})