Files
opencode-manager/frontend/src/components/settings/TTSSettings.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

216 lines
7.4 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, act } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { ReactNode } from 'react'
import { TTSSettings } from './TTSSettings'
import { useSettings } from '@/hooks/useSettings'
import { useTTS } from '@/hooks/useTTS'
import { useTTSModels, useTTSVoices, useTTSDiscovery } from '@/hooks/useTTSDiscovery'
import { getAvailableVoiceNames, isWebSpeechSupported } from '@/lib/webSpeechSynthesizer'
import { createUseSettingsMock } from '@/test/test-utils'
import type { UserPreferences } from '@/api/types/settings'
vi.mock('@/hooks/useSettings')
vi.mock('@/hooks/useTTS')
vi.mock('@/hooks/useTTSDiscovery')
vi.mock('@/lib/webSpeechSynthesizer')
const fetchMock = vi.fn()
const baseTts: NonNullable<UserPreferences['tts']> = {
enabled: true,
provider: 'external',
autoPlay: false,
endpoint: 'https://api.openai.com',
apiKey: 'sk-test',
voice: 'alloy',
model: 'tts-1',
speed: 1,
availableVoices: [],
availableModels: [],
lastVoicesFetch: 0,
lastModelsFetch: 0,
}
function buildPreferences(overrides: Partial<UserPreferences> = {}): UserPreferences {
return {
theme: 'dark',
mode: 'build',
autoScroll: true,
expandDiffs: true,
expandToolCalls: false,
showReasoning: false,
simpleChatMode: false,
keyboardShortcuts: {},
customCommands: [],
tts: baseTts,
...overrides,
}
}
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { 'Content-Type': 'application/json' },
})
}
function renderTTSSettings() {
const queryClient = new QueryClient({
defaultOptions: { queries: { retry: false }, mutations: { retry: false } },
})
function Wrapper({ children }: { children: ReactNode }) {
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
}
return render(<TTSSettings />, { wrapper: Wrapper })
}
function mockHooks(preferences: UserPreferences = buildPreferences()) {
vi.mocked(useSettings).mockReturnValue(createUseSettingsMock({ preferences }))
vi.mocked(useTTS).mockReturnValue({
speak: vi.fn(),
speakWithConfig: vi.fn(),
speakMessage: vi.fn(),
queueMessage: vi.fn(),
stop: vi.fn(),
pause: vi.fn(),
resume: vi.fn(),
unlock: vi.fn(),
state: 'idle',
error: null,
currentText: null,
originalText: null,
activeMessageId: null,
isEnabled: true,
isPlaying: false,
isLoading: false,
isIdle: true,
isPaused: false,
})
vi.mocked(useTTSDiscovery).mockReturnValue({
refreshModels: vi.fn(),
refreshVoices: vi.fn(),
refreshAll: vi.fn(),
invalidateModels: vi.fn(),
invalidateVoices: vi.fn(),
invalidateAll: vi.fn(),
})
vi.mocked(useTTSModels).mockReturnValue({
data: undefined,
isLoading: false,
refetch: vi.fn(),
} as unknown as ReturnType<typeof useTTSModels>)
vi.mocked(useTTSVoices).mockReturnValue({
data: undefined,
isLoading: false,
refetch: vi.fn(),
} as unknown as ReturnType<typeof useTTSVoices>)
vi.mocked(isWebSpeechSupported).mockReturnValue(true)
vi.mocked(getAvailableVoiceNames).mockResolvedValue(['Alex'])
}
describe('TTSSettings audio cache', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.stubGlobal('fetch', fetchMock)
mockHooks()
})
afterEach(() => {
vi.unstubAllGlobals()
})
it('clears the server cache via DELETE /api/tts/cache and reports the count', async () => {
const user = userEvent.setup()
fetchMock.mockResolvedValueOnce(jsonResponse({ cleared: 3 }))
renderTTSSettings()
await user.click(screen.getByRole('button', { name: 'Clear audio cache' }))
expect(await screen.findByRole('status')).toHaveTextContent('Cleared 3 cached audio file(s).')
const [url, options] = fetchMock.mock.calls[0] as [string, RequestInit]
expect(String(url)).toContain('/api/tts/cache')
expect(options).toMatchObject({ method: 'DELETE' })
})
it('disables the button and shows a pending label while clearing', async () => {
const user = userEvent.setup()
let resolveResponse: (response: Response) => void = () => {}
fetchMock.mockReturnValueOnce(
new Promise<Response>((resolve) => {
resolveResponse = resolve
}),
)
renderTTSSettings()
await user.click(screen.getByRole('button', { name: 'Clear audio cache' }))
const pendingButton = screen.getByRole('button', { name: /clearing/i })
expect(pendingButton).toBeDisabled()
resolveResponse(jsonResponse({ cleared: 1 }))
expect(await screen.findByRole('status')).toHaveTextContent('Cleared 1 cached audio file(s).')
})
it('shows an actionable error and allows retrying after a failure', async () => {
const user = userEvent.setup()
fetchMock
.mockResolvedValueOnce(jsonResponse({ error: 'cache unavailable' }, 500))
.mockResolvedValueOnce(jsonResponse({ cleared: 2 }))
renderTTSSettings()
await user.click(screen.getByRole('button', { name: 'Clear audio cache' }))
expect(await screen.findByRole('alert')).toHaveTextContent('cache unavailable')
const retryButton = screen.getByRole('button', { name: 'Clear audio cache' })
expect(retryButton).toBeEnabled()
await user.click(retryButton)
expect(await screen.findByRole('status')).toHaveTextContent('Cleared 2 cached audio file(s).')
expect(fetchMock).toHaveBeenCalledTimes(2)
})
it('stays available when TTS is disabled or the builtin provider is selected', async () => {
mockHooks(buildPreferences({ tts: { ...baseTts, enabled: false } }))
const disabledRender = renderTTSSettings()
expect(screen.getByRole('button', { name: 'Clear audio cache' })).toBeInTheDocument()
disabledRender.unmount()
mockHooks(buildPreferences({ tts: { ...baseTts, provider: 'builtin', voice: 'Alex' } }))
renderTTSSettings()
expect(screen.getByRole('button', { name: 'Clear audio cache' })).toBeInTheDocument()
expect(await screen.findByText('1 voices available in your browser')).toBeInTheDocument()
})
it('discovers models and voices only when external TTS is configured', async () => {
renderTTSSettings()
await act(async () => {})
expect(vi.mocked(useTTSModels).mock.calls.at(-1)?.[1]).toBe(true)
expect(vi.mocked(useTTSVoices).mock.calls.at(-1)?.[1]).toBe(true)
})
it('skips server discovery while TTS is disabled, builtin, or missing credentials', async () => {
mockHooks(buildPreferences({ tts: { ...baseTts, enabled: false } }))
const disabledRender = renderTTSSettings()
await act(async () => {})
expect(vi.mocked(useTTSModels).mock.calls.at(-1)?.[1]).toBe(false)
expect(vi.mocked(useTTSVoices).mock.calls.at(-1)?.[1]).toBe(false)
disabledRender.unmount()
mockHooks(buildPreferences({ tts: { ...baseTts, provider: 'builtin', voice: 'Alex' } }))
const builtinRender = renderTTSSettings()
await act(async () => {})
expect(vi.mocked(useTTSModels).mock.calls.at(-1)?.[1]).toBe(false)
expect(vi.mocked(useTTSVoices).mock.calls.at(-1)?.[1]).toBe(false)
builtinRender.unmount()
mockHooks(buildPreferences({ tts: { ...baseTts, apiKey: '' } }))
renderTTSSettings()
await act(async () => {})
expect(vi.mocked(useTTSModels).mock.calls.at(-1)?.[1]).toBe(false)
expect(vi.mocked(useTTSVoices).mock.calls.at(-1)?.[1]).toBe(false)
})
})