@@ -306,6 +306,60 @@ describe("openai tts", () => {
306306 ) . rejects . toThrow ( "OpenAI TTS API error (503): temporary upstream outage" ) ;
307307 } ) ;
308308
309+ it . each ( [
310+ { name : "JSON error" , contentType : "application/json" , body : '{"error":"denied"}' } ,
311+ { name : "problem JSON" , contentType : "application/problem+json" , body : '{"title":"denied"}' } ,
312+ { name : "HTML" , contentType : "text/html; charset=utf-8" , body : "<html>sign in</html>" } ,
313+ { name : "empty audio" , contentType : "audio/mpeg" , body : "" } ,
314+ ] ) (
315+ "rejects a successful $name response as synthesized audio" ,
316+ async ( { contentType, body } ) => {
317+ globalThis . fetch = vi
318+ . fn ( )
319+ . mockResolvedValue (
320+ new Response ( body , { status : 200 , headers : { "content-type" : contentType } } ) ,
321+ ) as unknown as typeof fetch ;
322+
323+ await expect (
324+ openaiTTS ( {
325+ text : "hello" ,
326+ apiKey : "test-key" ,
327+ baseUrl : "https://api.openai.com/v1" ,
328+ model : "gpt-4o-mini-tts" ,
329+ voice : "alloy" ,
330+ responseFormat : "mp3" ,
331+ timeoutMs : 5_000 ,
332+ } ) ,
333+ ) . rejects . toThrow ( "OpenAI TTS API error: malformed audio response" ) ;
334+ } ,
335+ ) ;
336+
337+ it . each ( [
338+ { name : "audio content type" , contentType : "audio/mpeg" } ,
339+ { name : "generic binary content type" , contentType : "application/octet-stream" } ,
340+ { name : "missing content type" , contentType : undefined } ,
341+ ] ) ( "preserves nonempty $name speech responses" , async ( { contentType } ) => {
342+ const audio = Buffer . from ( "audio-bytes" ) ;
343+ globalThis . fetch = vi . fn ( ) . mockResolvedValue (
344+ new Response ( audio , {
345+ status : 200 ,
346+ ...( contentType ? { headers : { "content-type" : contentType } } : { } ) ,
347+ } ) ,
348+ ) as unknown as typeof fetch ;
349+
350+ await expect (
351+ openaiTTS ( {
352+ text : "hello" ,
353+ apiKey : "test-key" ,
354+ baseUrl : "https://api.openai.com/v1" ,
355+ model : "gpt-4o-mini-tts" ,
356+ voice : "alloy" ,
357+ responseFormat : "mp3" ,
358+ timeoutMs : 5_000 ,
359+ } ) ,
360+ ) . resolves . toEqual ( audio ) ;
361+ } ) ;
362+
309363 it ( "caps streamed audio responses instead of buffering oversized TTS output" , async ( ) => {
310364 const streamed = createStreamingErrorResponse ( {
311365 status : 200 ,
0 commit comments