mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Merge pull request #416 from chriswritescode-dev/fix/tts-overlap-and-cache-race
fix(tts): prevent overlapping playback and corrupted audio cache
This commit is contained in:
9 files changed
+1145
-167
No files matched your search
@@ -0,0 +1,181 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { render, screen } 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(),
|
||||
stop: vi.fn(),
|
||||
state: 'idle',
|
||||
error: null,
|
||||
currentText: null,
|
||||
originalText: null,
|
||||
activeMessageId: null,
|
||||
isEnabled: true,
|
||||
isPlaying: false,
|
||||
isLoading: false,
|
||||
isIdle: true,
|
||||
})
|
||||
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()
|
||||
})
|
||||
})
|
||||
@@ -1,12 +1,13 @@
|
||||
import { useEffect, useState, useRef } from 'react'
|
||||
import { useForm } from 'react-hook-form'
|
||||
import { useMutation } from '@tanstack/react-query'
|
||||
import { zodResolver } from '@hookform/resolvers/zod'
|
||||
import { z } from 'zod'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
import { useTTS } from '@/hooks/useTTS'
|
||||
import { useTTSModels, useTTSVoices, useTTSDiscovery } from '@/hooks/useTTSDiscovery'
|
||||
import { getAvailableVoiceNames, isWebSpeechSupported } from '@/lib/webSpeechSynthesizer'
|
||||
import { Loader2, Volume2, XCircle, RefreshCw, MonitorSpeaker, Globe, CheckCircle2 } from 'lucide-react'
|
||||
import { Loader2, Volume2, XCircle, RefreshCw, MonitorSpeaker, Globe, CheckCircle2, Trash2 } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { SquareFill } from '@/components/ui/square-fill'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@@ -14,6 +15,7 @@ import { Switch } from '@/components/ui/switch'
|
||||
import { Slider } from '@/components/ui/slider'
|
||||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
|
||||
import { Combobox } from '@/components/ui/combobox'
|
||||
import { ttsApi } from '@/api/tts'
|
||||
import { DEFAULT_TTS_CONFIG } from '@/api/types/settings'
|
||||
|
||||
const TEST_PHRASE = 'Text to speech is working correctly.'
|
||||
@@ -240,6 +242,15 @@ export function TTSSettings() {
|
||||
stop()
|
||||
}
|
||||
|
||||
const clearCacheMutation = useMutation({
|
||||
mutationFn: () => ttsApi.clearCache(),
|
||||
})
|
||||
|
||||
const clearCacheError =
|
||||
clearCacheMutation.error instanceof Error
|
||||
? clearCacheMutation.error.message
|
||||
: 'Unable to clear the audio cache'
|
||||
|
||||
return (
|
||||
<div className="bg-card border border-border rounded-lg p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
@@ -681,6 +692,50 @@ export function TTSSettings() {
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
<div className="flex flex-col gap-3 rounded-lg border border-border p-4 sm:flex-row sm:items-center sm:justify-between">
|
||||
<div className="space-y-0.5">
|
||||
<div className="text-base font-medium">Clear audio cache</div>
|
||||
<p className="text-sm text-muted-foreground">
|
||||
Removes all cached server TTS audio. Audio is regenerated on next use, and active playback or synthesis may repopulate the cache.
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Does not affect model or voice discovery.
|
||||
</p>
|
||||
{clearCacheMutation.isSuccess && (
|
||||
<p role="status" className="text-sm text-success flex items-center gap-1">
|
||||
<CheckCircle2 className="h-4 w-4" />
|
||||
Cleared {clearCacheMutation.data?.cleared ?? 0} cached audio file(s).
|
||||
</p>
|
||||
)}
|
||||
{clearCacheMutation.isError && (
|
||||
<p role="alert" className="text-sm text-destructive flex items-center gap-1">
|
||||
<XCircle className="h-4 w-4" />
|
||||
Could not clear cached audio: {clearCacheError}. Try again.
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
size="sm"
|
||||
className="self-start sm:self-auto"
|
||||
onClick={() => clearCacheMutation.mutate()}
|
||||
disabled={clearCacheMutation.isPending}
|
||||
>
|
||||
{clearCacheMutation.isPending ? (
|
||||
<>
|
||||
<Loader2 className="h-4 w-4 animate-spin mr-2" />
|
||||
Clearing...
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Trash2 className="h-4 w-4 mr-2" />
|
||||
Clear audio cache
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user