@@ -86,17 +86,35 @@ describe("captionImagesWithGemini — OpenRouter provider", () => {
8686 vi . stubGlobal (
8787 "fetch" ,
8888 vi . fn (
89- async ( ) => new Response ( "invalid api key" , { status : 401 , statusText : "Unauthorized" } ) ,
89+ async ( ) =>
90+ new Response ( "provider detail containing or-bad-key" , {
91+ status : 401 ,
92+ statusText : "Unauthorized" ,
93+ } ) ,
9094 ) ,
9195 ) ;
9296
9397 const warnings : string [ ] = [ ] ;
94- // captionOne throws on !res.ok, but the throw is per-image inside
95- // Promise.allSettled, so it's filtered out as a rejected result rather than
96- // bubbling up — same silent degradation as the existing Gemini path.
97- const captions = await captionImagesWithGemini ( dir , ( ) => { } , warnings ) ;
98+ let outcome : VisionCaptionOutcome | undefined ;
99+ const captions = await captionImagesWithGemini ( dir , ( ) => { } , warnings , {
100+ onOutcome : ( value ) => {
101+ outcome = value ;
102+ } ,
103+ } ) ;
98104
99105 expect ( captions ) . toEqual ( { } ) ;
106+ expect ( warnings ) . toEqual ( [ "OpenRouter vision failed for 1 asset(s); captions omitted." ] ) ;
107+ expect ( warnings . join ( " " ) ) . not . toContain ( "or-bad-key" ) ;
108+ expect ( outcome ) . toEqual ( {
109+ timedOutRequests : 0 ,
110+ failedRequests : 1 ,
111+ budgetExhausted : false ,
112+ } ) ;
113+ if ( ! outcome ) throw new Error ( "Expected vision caption outcome" ) ;
114+ expect ( resolveVisionPhaseCompletion ( outcome , 10_000 ) ) . toEqual ( {
115+ status : "degraded" ,
116+ reason : "provider-error" ,
117+ } ) ;
100118 } ) ;
101119
102120 it ( "terminates captioning when the vision provider never responds" , async ( ) => {
@@ -125,6 +143,94 @@ describe("captionImagesWithGemini — OpenRouter provider", () => {
125143 expect ( result ) . toEqual ( { } ) ;
126144 } ) ;
127145
146+ it . each ( [
147+ { label : "success JSON" , status : 200 , partialBody : '{"choices":[' } ,
148+ { label : "failure text" , status : 500 , partialBody : "upstream failed: " } ,
149+ ] ) (
150+ "terminates captioning when OpenRouter $label body never ends" ,
151+ async ( { status, partialBody } ) => {
152+ const dir = makeProjectWithImages ( ) ;
153+ dirs . push ( dir ) ;
154+ vi . stubEnv ( "OPENROUTER_API_KEY" , "or-stalled-body-key" ) ;
155+ vi . stubEnv ( "HYPERFRAMES_VISION_TIMEOUT_MS" , "20" ) ;
156+
157+ vi . stubGlobal (
158+ "fetch" ,
159+ vi . fn ( async ( ) => {
160+ const body = new ReadableStream < Uint8Array > ( {
161+ start ( controller ) {
162+ controller . enqueue ( new TextEncoder ( ) . encode ( partialBody ) ) ;
163+ } ,
164+ } ) ;
165+ return new Response ( body , {
166+ status,
167+ headers : { "content-type" : "application/json" } ,
168+ } ) ;
169+ } ) ,
170+ ) ;
171+
172+ const warnings : string [ ] = [ ] ;
173+ let outcome : VisionCaptionOutcome | undefined ;
174+ const result = await Promise . race ( [
175+ captionImagesWithGemini ( dir , ( ) => { } , warnings , {
176+ onOutcome : ( value ) => {
177+ outcome = value ;
178+ } ,
179+ } ) ,
180+ new Promise < "hung" > ( ( resolve ) => setTimeout ( ( ) => resolve ( "hung" ) , 250 ) ) ,
181+ ] ) ;
182+
183+ expect ( result ) . toEqual ( { } ) ;
184+ expect ( warnings ) . toEqual ( [ "OpenRouter vision timed out for 1 asset(s); captions omitted." ] ) ;
185+ expect ( outcome ) . toEqual ( {
186+ timedOutRequests : 1 ,
187+ failedRequests : 0 ,
188+ budgetExhausted : false ,
189+ } ) ;
190+ } ,
191+ ) ;
192+
193+ it ( "keeps successful captions while reporting a failed sibling request" , async ( ) => {
194+ const dir = makeProjectWithImages ( [ "failed.png" , "success.png" ] ) ;
195+ dirs . push ( dir ) ;
196+ vi . stubEnv ( "OPENROUTER_API_KEY" , "or-mixed-failure-key" ) ;
197+
198+ let requestCount = 0 ;
199+ vi . stubGlobal (
200+ "fetch" ,
201+ vi . fn ( async ( ) => {
202+ requestCount ++ ;
203+ if ( requestCount === 1 ) {
204+ return new Response ( "upstream exploded with or-mixed-failure-key" , {
205+ status : 500 ,
206+ statusText : "Internal Server Error" ,
207+ } ) ;
208+ }
209+ return new Response (
210+ JSON . stringify ( { choices : [ { message : { content : "Successful image." } } ] } ) ,
211+ { status : 200 , headers : { "content-type" : "application/json" } } ,
212+ ) ;
213+ } ) ,
214+ ) ;
215+
216+ const warnings : string [ ] = [ ] ;
217+ let outcome : VisionCaptionOutcome | undefined ;
218+ const captions = await captionImagesWithGemini ( dir , ( ) => { } , warnings , {
219+ onOutcome : ( value ) => {
220+ outcome = value ;
221+ } ,
222+ } ) ;
223+
224+ expect ( captions ) . toEqual ( { "success.png" : "Successful image." } ) ;
225+ expect ( warnings ) . toEqual ( [ "OpenRouter vision failed for 1 asset(s); captions omitted." ] ) ;
226+ expect ( warnings . join ( " " ) ) . not . toContain ( "or-mixed-failure-key" ) ;
227+ expect ( outcome ) . toEqual ( {
228+ timedOutRequests : 0 ,
229+ failedRequests : 1 ,
230+ budgetExhausted : false ,
231+ } ) ;
232+ } ) ;
233+
128234 it ( "keeps a successful sibling caption when another request times out" , async ( ) => {
129235 const dir = makeProjectWithImages ( [ "hang.png" , "success.png" ] ) ;
130236 dirs . push ( dir ) ;
@@ -163,7 +269,11 @@ describe("captionImagesWithGemini — OpenRouter provider", () => {
163269 } ) ;
164270
165271 expect ( captions ) . toEqual ( { "success.png" : "Successful image." } ) ;
166- expect ( outcome ) . toEqual ( { timedOutRequests : 1 , budgetExhausted : false } ) ;
272+ expect ( outcome ) . toEqual ( {
273+ timedOutRequests : 1 ,
274+ failedRequests : 0 ,
275+ budgetExhausted : false ,
276+ } ) ;
167277 if ( ! outcome ) throw new Error ( "Expected vision caption outcome" ) ;
168278 expect ( resolveVisionPhaseCompletion ( outcome , 10_000 ) ) . toEqual ( {
169279 status : "degraded" ,
@@ -242,4 +352,36 @@ describe("captionImagesWithGemini — Gemini provider", () => {
242352
243353 expect ( result ) . toEqual ( { } ) ;
244354 } ) ;
355+
356+ it ( "reports a rejected Gemini request without leaking its error detail" , async ( ) => {
357+ const dir = makeProjectWithImages ( ) ;
358+ dirs . push ( dir ) ;
359+ vi . stubEnv ( "OPENROUTER_API_KEY" , "" ) ;
360+ vi . stubEnv ( "GEMINI_API_KEY" , "gemini-secret-key" ) ;
361+ generateContentMock . mockRejectedValue (
362+ new Error ( "provider echoed gemini-secret-key in its response" ) ,
363+ ) ;
364+
365+ const warnings : string [ ] = [ ] ;
366+ let outcome : VisionCaptionOutcome | undefined ;
367+ const captions = await captionImagesWithGemini ( dir , ( ) => { } , warnings , {
368+ onOutcome : ( value ) => {
369+ outcome = value ;
370+ } ,
371+ } ) ;
372+
373+ expect ( captions ) . toEqual ( { } ) ;
374+ expect ( warnings ) . toEqual ( [ "Gemini vision failed for 1 asset(s); captions omitted." ] ) ;
375+ expect ( warnings . join ( " " ) ) . not . toContain ( "gemini-secret-key" ) ;
376+ expect ( outcome ) . toEqual ( {
377+ timedOutRequests : 0 ,
378+ failedRequests : 1 ,
379+ budgetExhausted : false ,
380+ } ) ;
381+ if ( ! outcome ) throw new Error ( "Expected vision caption outcome" ) ;
382+ expect ( resolveVisionPhaseCompletion ( outcome , 10_000 ) ) . toEqual ( {
383+ status : "degraded" ,
384+ reason : "provider-error" ,
385+ } ) ;
386+ } ) ;
245387} ) ;
0 commit comments