@@ -823,6 +823,20 @@ describe("NativeOllamaHandler", () => {
823823 it ( "should call client.abort() when timeoutMs is reached" , async ( ) => {
824824 const testTimeout = 5000
825825 let capturedFn : ( ( ) => void ) | undefined
826+ let capturedInstanceAbort : ( ( ) => void ) | undefined
827+
828+ // Capture the per-request client's abort spy so the assertion targets
829+ // the actual abort() call rather than just the constructor call.
830+ OllamaMock . mockImplementation ( function ( options ?: { host ?: string } ) {
831+ const instanceAbort = vitest . fn ( )
832+ capturedInstanceAbort = instanceAbort
833+ return {
834+ chat : mockChat ,
835+ abort : instanceAbort ,
836+ _host : options ?. host ?? "http://localhost:11434" ,
837+ _instanceAbort : instanceAbort ,
838+ }
839+ } )
826840
827841 // The timer id is never consumed (the callback is captured and fired
828842 // manually), so bridge the ambient setTimeout signature through unknown.
@@ -842,7 +856,8 @@ describe("NativeOllamaHandler", () => {
842856 expect ( capturedFn ) . toBeDefined ( )
843857 if ( capturedFn ) capturedFn ( )
844858 // The timeout callback should have invoked client.abort() on the request-local instance
845- expect ( OllamaMock ) . toHaveBeenCalled ( )
859+ expect ( capturedInstanceAbort ) . toBeDefined ( )
860+ expect ( capturedInstanceAbort ) . toHaveBeenCalledTimes ( 1 )
846861 } )
847862
848863 it ( "should call instance.abort() when abortSignal is aborted" , async ( ) => {
@@ -902,7 +917,22 @@ describe("NativeOllamaHandler", () => {
902917 expect ( capturedInstanceAbort ) . toHaveBeenCalledTimes ( 1 )
903918 } )
904919
905- it ( "should not create a request-local client or timer for non-positive timeoutMs" , async ( ) => {
920+ it ( "should reject with AbortError without fetching models when abortSignal is already aborted" , async ( ) => {
921+ const controller = new AbortController ( )
922+ controller . abort ( )
923+
924+ await expect (
925+ handler . completePrompt ( "Test prompt" , { abortSignal : controller . signal } ) ,
926+ ) . rejects . toMatchObject ( {
927+ name : "AbortError" ,
928+ } )
929+
930+ // The pre-aborted check must short-circuit before any network call.
931+ expect ( mockGetOllamaModels ) . not . toHaveBeenCalled ( )
932+ expect ( mockChat ) . not . toHaveBeenCalled ( )
933+ } )
934+
935+ it ( "should not start a timeout timer for non-positive timeoutMs" , async ( ) => {
906936 const setTimeoutSpy = vitest . spyOn ( global , "setTimeout" )
907937 mockChat . mockResolvedValue ( {
908938 message : { content : "Response" } ,
0 commit comments