@@ -73,10 +73,16 @@ describe("describeStems", () => {
7373 expect ( result ) . toEqual ( { stems : measured . stems , description : null } ) ;
7474 } ) ;
7575
76- it . each ( [ "api key" , "access token" ] ) (
77- "uses the HeyGen %s for base64 audio and schema-enforced summary requests" ,
78- async ( credential ) => {
79- vi . stubEnv ( credential === "api key" ? "HEYGEN_API_KEY" : "HEYGEN_ACCESS_TOKEN" , "signed-in" ) ;
76+ it . each ( [
77+ { credential : "HEYGEN_API_KEY" , headers : { "x-api-key" : "signed-in" } } ,
78+ {
79+ credential : "HEYGEN_ACCESS_TOKEN" ,
80+ headers : { authorization : "Bearer signed-in" , "X-HeyGen-Source" : "cli" } ,
81+ } ,
82+ ] ) (
83+ "uses $credential for base64 audio and schema-enforced summary requests" ,
84+ async ( { credential, headers } ) => {
85+ vi . stubEnv ( credential , "signed-in" ) ;
8086 vi . stubEnv ( "HYPERFRAMES_AUDIO_API_KEY" , "not-the-description-key" ) ;
8187 vi . stubEnv ( "HYPERFRAMES_AUDIO_STEMS_URL" , "https://other.test/stems" ) ;
8288 vi . stubEnv ( "HYPERFRAMES_READ_AUDIO_MODEL" , "not-the-model" ) ;
@@ -91,9 +97,7 @@ describe("describeStems", () => {
9197 expect ( init ?. headers ) . toEqual ( {
9298 "content-type" : "application/json" ,
9399 "X-HeyGen-Client-Source" : "hyperframes" ,
94- ...( credential === "api key"
95- ? { "x-api-key" : "signed-in" }
96- : { authorization : "Bearer signed-in" , "X-HeyGen-Source" : "cli" } ) ,
100+ ...headers ,
97101 } ) ;
98102 const request = JSON . parse ( String ( init ?. body ) ) ;
99103 expect ( request . model ) . toBe ( "gemini-4-argon" ) ;
@@ -148,7 +152,7 @@ describe("describeStems", () => {
148152 ] ) ;
149153 await describeAudio ( warnings ) ;
150154 expect ( fetch ) . toHaveBeenCalledTimes ( 3 ) ;
151- vi . stubEnv ( credential === "api key" ? "HEYGEN_API_KEY" : "HEYGEN_ACCESS_TOKEN" , "" ) ;
155+ vi . stubEnv ( credential , "" ) ;
152156 expect ( ( await describeAudio ( warnings ) ) . description ) . toBeNull ( ) ;
153157 expect ( fetch ) . toHaveBeenCalledTimes ( 3 ) ;
154158 expect ( warnings ) . toHaveLength ( 1 ) ;
@@ -229,6 +233,12 @@ describe("describeStems", () => {
229233 JSON . stringify ( { overall : "Words" } ) ,
230234 JSON . stringify ( { overall : "Words" , bgm_prompt : 7 } ) ,
231235 JSON . stringify ( { overall : "" , bgm_prompt : null } ) ,
236+ JSON . stringify ( { overall : " " , bgm_prompt : null } ) ,
237+ JSON . stringify ( { overall : "Words" , bgm_prompt : " " } ) ,
238+ JSON . stringify ( { overall : 7 , bgm_prompt : null } ) ,
239+ JSON . stringify ( { bgm_prompt : null } ) ,
240+ "null" ,
241+ "[]" ,
232242 ] ) ( "rejects a summary outside the schema instead of guessing (%s)" , async ( answer ) => {
233243 vi . stubEnv ( "HEYGEN_API_KEY" , "test-key" ) ;
234244 const fetch = vi
@@ -244,6 +254,54 @@ describe("describeStems", () => {
244254 expect ( fetch ) . toHaveBeenCalledTimes ( 3 ) ;
245255 } ) ;
246256
257+ it . each ( [ null , [ ] , { } , { content : null } , { content : [ null , { } , { type : "text" , text : 7 } ] } ] ) (
258+ "rejects a response without text (%j)" ,
259+ async ( answer ) => {
260+ vi . stubEnv ( "HEYGEN_API_KEY" , "test-key" ) ;
261+ vi . spyOn ( globalThis , "fetch" ) . mockImplementation ( async ( ) => Response . json ( answer ) ) ;
262+ const warnings : string [ ] = [ ] ;
263+ expect ( await describeAudio ( warnings ) ) . toEqual ( { stems : measured . stems , description : null } ) ;
264+ expect ( warnings ) . toEqual ( [ "stems not described: the description service returned no text" ] ) ;
265+ } ,
266+ ) ;
267+
268+ it ( "joins only text blocks in a mixed response" , async ( ) => {
269+ vi . stubEnv ( "HEYGEN_API_KEY" , "test-key" ) ;
270+ vi . spyOn ( globalThis , "fetch" ) . mockImplementation ( async ( _url , init ) => {
271+ if ( JSON . parse ( String ( init ?. body ) ) . output_config )
272+ return response ( JSON . stringify ( { overall : "Speech." , bgm_prompt : null } ) ) ;
273+ return Response . json ( {
274+ content : [
275+ null ,
276+ { } ,
277+ { type : "audio" , text : "ignored" } ,
278+ { type : "text" , text : 7 } ,
279+ { type : "text" , text : "Spoken " } ,
280+ { type : "text" , text : "words." } ,
281+ ] ,
282+ } ) ;
283+ } ) ;
284+ const warnings : string [ ] = [ ] ;
285+ const result = await describeAudio ( warnings ) ;
286+ expect ( result . stems . map ( ( stem ) => stem . description ) ) . toEqual ( [
287+ "Spoken words." ,
288+ "Spoken words." ,
289+ ] ) ;
290+ expect ( warnings ) . toEqual ( [ ] ) ;
291+ } ) ;
292+
293+ it ( "keys cached descriptions on content rather than the credential or endpoint" , async ( ) => {
294+ vi . stubEnv ( "HEYGEN_API_KEY" , "first-key" ) ;
295+ const fetch = mockMessages ( ) ;
296+ const warnings : string [ ] = [ ] ;
297+ const first = await describeAudio ( warnings ) ;
298+ vi . stubEnv ( "HEYGEN_API_KEY" , "second-key" ) ;
299+ vi . stubEnv ( "HEYGEN_API_URL" , "https://another.heygen.test" ) ;
300+ expect ( await describeAudio ( warnings ) ) . toEqual ( first ) ;
301+ expect ( fetch ) . toHaveBeenCalledTimes ( 3 ) ;
302+ expect ( warnings ) . toEqual ( [ ] ) ;
303+ } ) ;
304+
247305 it ( "does not describe silent stems" , async ( ) => {
248306 const fetch = vi . spyOn ( globalThis , "fetch" ) ;
249307 const warnings : string [ ] = [ ] ;
0 commit comments