@@ -12,14 +12,22 @@ vitest.mock("@roo-code/telemetry", () => ({
1212
1313import { Anthropic } from "@anthropic-ai/sdk"
1414
15+ import type { GenerateContentResponse } from "@google/genai"
16+
1517import { type ModelInfo , geminiDefaultModelId , ApiProviderError } from "@roo-code/types"
1618
1719import { t } from "i18next"
1820import { GeminiHandler } from "../gemini"
1921import { asyncStreamFrom , collectStream } from "../../../test-utils/stream"
22+ import { makeCreateMessageMetadata } from "../../../test-utils/api"
2023
2124const GEMINI_MODEL_NAME = geminiDefaultModelId
2225
26+ // @google /genai's GenerateContentResponse exposes `text` via a getter backed by
27+ // `candidates`, so the stub only carries the field the provider reads; the double
28+ // cast is the least-friction way to satisfy the class type in mocks.
29+ const stubGenerateContentResponse = ( text : string ) => ( { text } ) as unknown as GenerateContentResponse
30+
2331describe ( "GeminiHandler" , ( ) => {
2432 let handler : GeminiHandler
2533
@@ -342,6 +350,71 @@ describe("GeminiHandler", () => {
342350 const result = await handler . completePrompt ( "Test prompt" )
343351 expect ( result ) . toBe ( "" )
344352 } )
353+
354+ it ( "should pass abort signal through to client via config.abortSignal" , async ( ) => {
355+ const controller = new AbortController ( )
356+ vi . mocked ( handler [ "client" ] . models . generateContent ) . mockResolvedValue (
357+ stubGenerateContentResponse ( "response" ) ,
358+ )
359+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal } )
360+ expect ( handler [ "client" ] . models . generateContent ) . toHaveBeenCalledWith ( {
361+ model : GEMINI_MODEL_NAME ,
362+ contents : [ { role : "user" , parts : [ { text : "test prompt" } ] } ] ,
363+ config : {
364+ abortSignal : controller . signal ,
365+ httpOptions : undefined ,
366+ temperature : 1 ,
367+ } ,
368+ } )
369+ } )
370+
371+ it ( "should work without options (backward compatible)" , async ( ) => {
372+ vi . mocked ( handler [ "client" ] . models . generateContent ) . mockResolvedValue (
373+ stubGenerateContentResponse ( "response" ) ,
374+ )
375+ const result = await handler . completePrompt ( "test prompt" )
376+ expect ( result ) . toBe ( "response" )
377+ expect ( handler [ "client" ] . models . generateContent ) . toHaveBeenCalledWith ( {
378+ model : GEMINI_MODEL_NAME ,
379+ contents : [ { role : "user" , parts : [ { text : "test prompt" } ] } ] ,
380+ config : {
381+ httpOptions : undefined ,
382+ temperature : 1 ,
383+ } ,
384+ } )
385+ } )
386+
387+ it ( "should pass timeoutMs through to client via httpOptions with abortSignal on config" , async ( ) => {
388+ const controller = new AbortController ( )
389+ vi . mocked ( handler [ "client" ] . models . generateContent ) . mockResolvedValue (
390+ stubGenerateContentResponse ( "response" ) ,
391+ )
392+ await handler . completePrompt ( "test prompt" , { abortSignal : controller . signal , timeoutMs : 10000 } )
393+ expect ( handler [ "client" ] . models . generateContent ) . toHaveBeenCalledWith ( {
394+ model : GEMINI_MODEL_NAME ,
395+ contents : [ { role : "user" , parts : [ { text : "test prompt" } ] } ] ,
396+ config : {
397+ abortSignal : controller . signal ,
398+ httpOptions : { timeout : 10000 } ,
399+ temperature : 1 ,
400+ } ,
401+ } )
402+ } )
403+
404+ it ( "should pass only timeoutMs when no signal is provided" , async ( ) => {
405+ vi . mocked ( handler [ "client" ] . models . generateContent ) . mockResolvedValue (
406+ stubGenerateContentResponse ( "response" ) ,
407+ )
408+ await handler . completePrompt ( "test prompt" , { timeoutMs : 5000 } )
409+ expect ( handler [ "client" ] . models . generateContent ) . toHaveBeenCalledWith ( {
410+ model : GEMINI_MODEL_NAME ,
411+ contents : [ { role : "user" , parts : [ { text : "test prompt" } ] } ] ,
412+ config : {
413+ httpOptions : { timeout : 5000 } ,
414+ temperature : 1 ,
415+ } ,
416+ } )
417+ } )
345418 } )
346419
347420 describe ( "getModel" , ( ) => {
@@ -475,6 +548,142 @@ describe("GeminiHandler", () => {
475548 } )
476549 } )
477550
551+ describe ( "completePrompt request options" , ( ) => {
552+ it ( "should pass timeout and baseUrl through httpOptions" , async ( ) => {
553+ const handlerWithBaseUrl = new GeminiHandler ( {
554+ apiKey : "test-key" ,
555+ apiModelId : GEMINI_MODEL_NAME ,
556+ geminiApiKey : "test-key" ,
557+ googleGeminiBaseUrl : "https://gemini.example.test" ,
558+ } )
559+ handlerWithBaseUrl [ "client" ] = handler [ "client" ]
560+ vi . mocked ( handler [ "client" ] . models . generateContent ) . mockResolvedValue (
561+ stubGenerateContentResponse ( "Response" ) ,
562+ )
563+
564+ const result = await handlerWithBaseUrl . completePrompt ( "Test prompt" , { timeoutMs : 1234 } )
565+
566+ expect ( result ) . toBe ( "Response" )
567+ expect ( handler [ "client" ] . models . generateContent ) . toHaveBeenCalledWith (
568+ expect . objectContaining ( {
569+ config : expect . objectContaining ( {
570+ httpOptions : {
571+ timeout : 1234 ,
572+ baseUrl : "https://gemini.example.test" ,
573+ } ,
574+ } ) ,
575+ } ) ,
576+ )
577+ } )
578+
579+ it ( "should pass abortSignal on config instead of httpOptions" , async ( ) => {
580+ const controller = new AbortController ( )
581+ vi . mocked ( handler [ "client" ] . models . generateContent ) . mockResolvedValue (
582+ stubGenerateContentResponse ( "Response" ) ,
583+ )
584+
585+ await handler . completePrompt ( "Test prompt" , { abortSignal : controller . signal } )
586+
587+ expect ( handler [ "client" ] . models . generateContent ) . toHaveBeenCalledWith (
588+ expect . objectContaining ( {
589+ config : expect . objectContaining ( {
590+ abortSignal : controller . signal ,
591+ httpOptions : undefined ,
592+ } ) ,
593+ } ) ,
594+ )
595+ } )
596+
597+ it ( "should omit httpOptions when timeoutMs and baseUrl are not provided" , async ( ) => {
598+ vi . mocked ( handler [ "client" ] . models . generateContent ) . mockResolvedValue (
599+ stubGenerateContentResponse ( "Response" ) ,
600+ )
601+
602+ await handler . completePrompt ( "Test prompt" )
603+
604+ expect ( handler [ "client" ] . models . generateContent ) . toHaveBeenCalledWith (
605+ expect . objectContaining ( {
606+ config : expect . objectContaining ( {
607+ httpOptions : undefined ,
608+ } ) ,
609+ } ) ,
610+ )
611+ } )
612+ } )
613+
614+ describe ( "createMessage abort signal (bridging)" , ( ) => {
615+ const messages : Anthropic . Messages . MessageParam [ ] = [
616+ {
617+ role : "user" ,
618+ content : "Hello" ,
619+ } ,
620+ ]
621+
622+ it ( "should reject immediately with AbortError when the external signal is pre-aborted" , async ( ) => {
623+ const controller = new AbortController ( )
624+ controller . abort ( )
625+
626+ const stream = handler . createMessage (
627+ "You are a helpful assistant" ,
628+ messages ,
629+ makeCreateMessageMetadata ( { abortSignal : controller . signal } ) ,
630+ )
631+
632+ const error = await collectStream ( stream ) . catch ( ( e : unknown ) => e )
633+ expect ( error ) . toBeInstanceOf ( Error )
634+ expect ( ( error as Error ) . name ) . toBe ( "AbortError" )
635+ expect ( handler [ "client" ] . models . generateContentStream ) . not . toHaveBeenCalled ( )
636+ } )
637+
638+ it ( "should abort the in-flight request when the external signal is triggered" , async ( ) => {
639+ const controller = new AbortController ( )
640+ let capturedSignal : AbortSignal | undefined
641+ const stub = vi . fn ( ) . mockImplementation ( async ( params : { config ?: { abortSignal ?: AbortSignal } } ) => {
642+ capturedSignal = params . config ?. abortSignal
643+ return ( async function * ( ) {
644+ yield { text : "partial" }
645+ if ( capturedSignal ?. aborted ) {
646+ throw new DOMException ( "aborted" , "AbortError" )
647+ }
648+ await new Promise ( ( _resolve , reject ) => {
649+ capturedSignal ?. addEventListener (
650+ "abort" ,
651+ ( ) => reject ( new DOMException ( "aborted" , "AbortError" ) ) ,
652+ { once : true } ,
653+ )
654+ } )
655+ } ) ( )
656+ } )
657+ handler [ "client" ] . models . generateContentStream = stub
658+
659+ const stream = handler . createMessage (
660+ "You are a helpful assistant" ,
661+ messages ,
662+ makeCreateMessageMetadata ( { abortSignal : controller . signal } ) ,
663+ )
664+
665+ const collector = collectStream ( stream ) . catch ( ( e : unknown ) => e )
666+ await new Promise ( ( resolve ) => setTimeout ( resolve , 10 ) )
667+ controller . abort ( )
668+
669+ const error = await collector
670+ expect ( error ) . toBeInstanceOf ( Error )
671+ expect ( ( error as Error ) . name ) . toBe ( "AbortError" )
672+ expect ( capturedSignal ) . toBeDefined ( )
673+ expect ( capturedSignal ?. aborted ) . toBe ( true )
674+ } )
675+
676+ it ( "should not set config.abortSignal when no external signal is provided" , async ( ) => {
677+ const stub = vi . fn ( ) . mockReturnValue ( ( async function * ( ) { } ) ( ) )
678+ handler [ "client" ] . models . generateContentStream = stub
679+
680+ await collectStream ( handler . createMessage ( "You are a helpful assistant" , messages ) )
681+
682+ const config = stub . mock . calls [ 0 ] [ 0 ] . config
683+ expect ( config . abortSignal ) . toBeUndefined ( )
684+ } )
685+ } )
686+
478687 describe ( "error telemetry" , ( ) => {
479688 const mockMessages : Anthropic . Messages . MessageParam [ ] = [
480689 {
0 commit comments