@@ -520,6 +520,7 @@ describe("ZooGatewayHandler", () => {
520520 temperature : ZOO_GATEWAY_DEFAULT_TEMPERATURE ,
521521 max_completion_tokens : 64000 ,
522522 } ) ,
523+ { } ,
523524 )
524525 } )
525526
@@ -534,6 +535,133 @@ describe("ZooGatewayHandler", () => {
534535 )
535536 } )
536537
538+ it ( "forwards a per-request signal and the timeout to the SDK" , async ( ) => {
539+ const handler = new ZooGatewayHandler ( mockOptions )
540+ const caller = new AbortController ( )
541+
542+ await handler . completePrompt ( "Test" , { abortSignal : caller . signal , timeoutMs : 5_000 } )
543+
544+ const requestOptions = mockCreate . mock . calls [ 0 ] [ 1 ] as { signal ?: AbortSignal ; timeout ?: number }
545+ expect ( requestOptions . signal ) . toBeInstanceOf ( AbortSignal )
546+ expect ( requestOptions . signal ) . not . toBe ( caller . signal )
547+ expect ( requestOptions . timeout ) . toBe ( 5_000 )
548+ } )
549+
550+ it ( "sends no signal when the caller gives none" , async ( ) => {
551+ const handler = new ZooGatewayHandler ( mockOptions )
552+
553+ await handler . completePrompt ( "Test" )
554+
555+ expect ( mockCreate . mock . calls [ 0 ] [ 1 ] ) . toEqual ( { } )
556+ } )
557+
558+ it ( "rejects with an AbortError and sends no request when the signal is pre-aborted" , async ( ) => {
559+ const handler = new ZooGatewayHandler ( mockOptions )
560+ const caller = new AbortController ( )
561+ caller . abort ( )
562+
563+ await expect ( handler . completePrompt ( "Test" , { abortSignal : caller . signal } ) ) . rejects . toMatchObject ( {
564+ name : "AbortError" ,
565+ } )
566+ expect ( mockCreate ) . not . toHaveBeenCalled ( )
567+ } )
568+
569+ it ( "rejects with an AbortError when the timeout alone expires during model lookup" , async ( ) => {
570+ const { getModels } = await import ( "../fetchers/modelCache" )
571+ vitest . mocked ( getModels ) . mockImplementation ( ( ) => new Promise ( ( ) => { } ) )
572+ const handler = new ZooGatewayHandler ( mockOptions )
573+
574+ await expect ( handler . completePrompt ( "Test" , { timeoutMs : 20 } ) ) . rejects . toMatchObject ( {
575+ name : "AbortError" ,
576+ message : "The Zoo Gateway request was aborted" ,
577+ } )
578+ expect ( mockCreate ) . not . toHaveBeenCalled ( )
579+ } )
580+
581+ it ( "rejects with an AbortError when the signal aborts during model lookup" , async ( ) => {
582+ const { getModels } = await import ( "../fetchers/modelCache" )
583+ vitest . mocked ( getModels ) . mockImplementation ( ( ) => new Promise ( ( ) => { } ) )
584+ const handler = new ZooGatewayHandler ( mockOptions )
585+ const caller = new AbortController ( )
586+
587+ const pending = handler . completePrompt ( "Test" , { abortSignal : caller . signal } )
588+ caller . abort ( )
589+
590+ await expect ( pending ) . rejects . toMatchObject ( {
591+ name : "AbortError" ,
592+ message : "The Zoo Gateway request was aborted" ,
593+ } )
594+ expect ( mockCreate ) . not . toHaveBeenCalled ( )
595+ } )
596+
597+ it ( "rejects with an AbortError at once when aborted mid-flight, without a gateway error" , async ( ) => {
598+ const handler = new ZooGatewayHandler ( mockOptions )
599+ const caller = new AbortController ( )
600+ let sdkSignal : AbortSignal | undefined
601+ // The SDK call never settles, as when the SDK sleeps between retries.
602+ mockCreate . mockImplementation ( ( _params : unknown , requestOptions : { signal ?: AbortSignal } ) => {
603+ sdkSignal = requestOptions . signal
604+ return new Promise ( ( ) => { } )
605+ } )
606+
607+ const pending = handler . completePrompt ( "Test" , { abortSignal : caller . signal } )
608+ await vitest . waitFor ( ( ) => expect ( mockCreate ) . toHaveBeenCalled ( ) )
609+ caller . abort ( )
610+
611+ await expect ( pending ) . rejects . toMatchObject ( {
612+ name : "AbortError" ,
613+ message : "The Zoo Gateway request was aborted" ,
614+ } )
615+ expect ( sdkSignal ?. aborted ) . toBe ( true )
616+ expect ( showErrorMessage ) . not . toHaveBeenCalled ( )
617+ } )
618+
619+ it ( "reports an SDK abort error after the caller aborts as an AbortError, not a gateway error" , async ( ) => {
620+ const handler = new ZooGatewayHandler ( mockOptions )
621+ const caller = new AbortController ( )
622+ mockCreate . mockImplementation ( async ( ) => {
623+ caller . abort ( )
624+ throw new Error ( "Request was aborted." )
625+ } )
626+
627+ await expect ( handler . completePrompt ( "Test" , { abortSignal : caller . signal } ) ) . rejects . toMatchObject ( {
628+ name : "AbortError" ,
629+ } )
630+ expect ( showErrorMessage ) . not . toHaveBeenCalled ( )
631+ } )
632+
633+ it ( "does not return a completion that resolves just before the caller aborts" , async ( ) => {
634+ const handler = new ZooGatewayHandler ( mockOptions )
635+ const caller = new AbortController ( )
636+ let resolveCompletion : ( value : unknown ) => void = ( ) => { }
637+ const completion = new Promise ( ( resolve ) => {
638+ resolveCompletion = resolve
639+ } )
640+ mockCreate . mockImplementation ( ( ) => completion )
641+
642+ const pending = handler . completePrompt ( "Test" , { abortSignal : caller . signal } )
643+ await vitest . waitFor ( ( ) => expect ( mockCreate ) . toHaveBeenCalled ( ) )
644+ // This callback runs after the handler's own race settles but before completePrompt resumes,
645+ // so only the check after the await can drop the late result.
646+ void completion . then ( ( ) => caller . abort ( ) )
647+ resolveCompletion ( { choices : [ { message : { role : "assistant" , content : "late" } } ] } )
648+
649+ await expect ( pending ) . rejects . toMatchObject ( { name : "AbortError" } )
650+ } )
651+
652+ it ( "removes the same abort listener from the caller's signal that it added" , async ( ) => {
653+ const handler = new ZooGatewayHandler ( mockOptions )
654+ const caller = new AbortController ( )
655+ const addListener = vitest . spyOn ( caller . signal , "addEventListener" )
656+ const removeListener = vitest . spyOn ( caller . signal , "removeEventListener" )
657+
658+ await handler . completePrompt ( "Test" , { abortSignal : caller . signal } )
659+
660+ const abortListeners = addListener . mock . calls . filter ( ( [ type ] ) => type === "abort" )
661+ expect ( abortListeners ) . toHaveLength ( 1 )
662+ expect ( removeListener ) . toHaveBeenCalledWith ( "abort" , abortListeners [ 0 ] [ 1 ] )
663+ } )
664+
537665 it ( "returns an empty string when the model returns no content" , async ( ) => {
538666 const handler = new ZooGatewayHandler ( mockOptions )
539667 mockCreate . mockImplementation ( async ( ) => ( {
0 commit comments