fix(tts): bound synthesis requests and tolerate cache write failures

This commit is contained in:
Chris Scott committed 2026-10-09 23:57:09 -04:00
1 parent 1bfcdd0496
commit 1eba33c6f1
2 files changed
+78 -23

No files matched your search

+42
View File
@@ -341,6 +341,7 @@ describe('TTS route handlers', () => {
expect.objectContaining({
method: 'POST',
headers: expect.objectContaining({ Authorization: 'Bearer test-api-key' }),
signal: expect.any(AbortSignal),
}),
)
expect(mockWriteFile).toHaveBeenCalledWith(
@@ -353,6 +354,28 @@ describe('TTS route handlers', () => {
)
})
it('returns synthesized audio when the cache write fails', async () => {
mockStat.mockRejectedValue(new Error('not found'))
mockReaddir.mockResolvedValue([] as any)
mockWriteFile.mockRejectedValue(new Error('disk full'))
mockUnlink.mockResolvedValue(undefined)
const fetchMock = vi.fn().mockResolvedValue({
ok: true,
arrayBuffer: async () => Buffer.from('fresh-audio'),
})
vi.stubGlobal('fetch', fetchMock)
const res = await app.request('/synthesize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Hello' }),
})
expect(res.status).toBe(200)
expect(res.headers.get('X-Cache')).toBe('MISS')
expect(Buffer.from(await res.arrayBuffer()).toString()).toBe('fresh-audio')
})
it('deduplicates concurrent synthesis requests for the same text', async () => {
mockStat.mockRejectedValue(new Error('not found'))
mockReaddir.mockResolvedValue([] as any)
@@ -416,6 +439,25 @@ describe('TTS route handlers', () => {
})
})
it('returns an upstream error when synthesis times out', async () => {
mockStat.mockRejectedValue(new Error('not found'))
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new DOMException('The operation timed out.', 'TimeoutError')))
const res = await app.request('/synthesize', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text: 'Hello' }),
})
expect(res.status).toBe(500)
expect(await res.json()).toEqual({
error: 'TTS API request failed',
details: 'TTS upstream request timed out',
voice: 'alloy',
availableVoices: [],
})
})
it('lists models and stores them when not cached', async () => {
mockDiscoverModelsCached.mockResolvedValue({ models: ['tts-1', 'tts-1-hd'], cached: false })