@@ -369,4 +369,42 @@ describe('generateText', () => {
369369 ] ) ;
370370 expect ( result . text ) . toBe ( 'I could not execute that tool call.' ) ;
371371 } ) ;
372+
373+ it ( 'auto-builds fallback chain when fallbackProviders is undefined and primary throws 429' , async ( ) => {
374+ hoisted . generateCompletion
375+ . mockRejectedValueOnce ( new Error ( '429 rate limit exceeded' ) )
376+ . mockResolvedValueOnce ( {
377+ modelId : 'gpt-4o-mini' ,
378+ usage : { promptTokens : 5 , completionTokens : 3 , totalTokens : 8 } ,
379+ choices : [ { message : { role : 'assistant' , content : 'fallback reply' } , finishReason : 'stop' } ] ,
380+ } ) ;
381+
382+ process . env . ANTHROPIC_API_KEY = 'test-anthropic-key' ;
383+ try {
384+ const result = await generateText ( {
385+ model : 'openai:gpt-4o' ,
386+ prompt : 'hello' ,
387+ } ) ;
388+ expect ( result . text ) . toBe ( 'fallback reply' ) ;
389+ } finally {
390+ delete process . env . ANTHROPIC_API_KEY ;
391+ }
392+ } ) ;
393+
394+ it ( 'does NOT fallback when fallbackProviders is explicitly []' , async ( ) => {
395+ hoisted . generateCompletion . mockRejectedValueOnce ( new Error ( '429 rate limit exceeded' ) ) ;
396+
397+ process . env . ANTHROPIC_API_KEY = 'test-anthropic-key' ;
398+ try {
399+ await expect (
400+ generateText ( {
401+ model : 'openai:gpt-4o' ,
402+ prompt : 'hello' ,
403+ fallbackProviders : [ ] ,
404+ } )
405+ ) . rejects . toThrow ( '429' ) ;
406+ } finally {
407+ delete process . env . ANTHROPIC_API_KEY ;
408+ }
409+ } ) ;
372410} ) ;
0 commit comments