@@ -871,11 +871,11 @@ describe("MistralHandler", () => {
871871 vi . useFakeTimers ( )
872872 // The merged deadline uses native AbortSignal.timeout; drive it with fake
873873 // timers so the test is deterministic and does not wait in real time.
874- const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) . mockImplementation ( ( ( ms : number ) => {
874+ const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) . mockImplementation ( ( ms = 0 ) => {
875875 const controller = new AbortController ( )
876876 setTimeout ( ( ) => controller . abort ( new DOMException ( "timed out" , "TimeoutError" ) ) , ms )
877877 return controller . signal
878- } ) as unknown as typeof AbortSignal . timeout )
878+ } )
879879 try {
880880 mockComplete . mockImplementation (
881881 ( _options : unknown , requestOptions ?: { fetchOptions ?: { signal ?: AbortSignal } } ) =>
@@ -908,6 +908,74 @@ describe("MistralHandler", () => {
908908 }
909909 } )
910910
911+ it ( "keeps TimeoutError when the timeout fires before a later caller abort" , async ( ) => {
912+ vi . useFakeTimers ( )
913+ const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) . mockImplementation ( ( ms = 0 ) => {
914+ const controller = new AbortController ( )
915+ setTimeout ( ( ) => controller . abort ( new DOMException ( "timed out" , "TimeoutError" ) ) , ms )
916+ return controller . signal
917+ } )
918+ const caller = new AbortController ( )
919+ try {
920+ mockComplete . mockImplementation (
921+ ( _options : unknown , requestOptions ?: { fetchOptions ?: { signal ?: AbortSignal } } ) =>
922+ new Promise < never > ( ( _resolve , reject ) => {
923+ const signal = requestOptions ?. fetchOptions ?. signal
924+ const fail = ( ) =>
925+ reject ( Object . assign ( new Error ( "Request timed out" ) , { name : "RequestTimeoutError" } ) )
926+ signal ?. addEventListener ( "abort" , fail , { once : true } )
927+ } ) ,
928+ )
929+
930+ const promise = handler . completePrompt ( "Test prompt" , { abortSignal : caller . signal , timeoutMs : 100 } )
931+ const assertion = expect ( promise ) . rejects . toMatchObject ( { name : "TimeoutError" } )
932+ // Timeout fires first, then the caller also aborts before the rejection settles.
933+ await vi . advanceTimersByTimeAsync ( 120 )
934+ caller . abort ( )
935+ await assertion
936+ } finally {
937+ timeoutSpy . mockRestore ( )
938+ vi . useRealTimers ( )
939+ }
940+ } )
941+
942+ it ( "keeps AbortError when the caller aborts before the timeout fires" , async ( ) => {
943+ vi . useFakeTimers ( )
944+ const timeoutSpy = vi . spyOn ( AbortSignal , "timeout" ) . mockImplementation ( ( ms = 0 ) => {
945+ const controller = new AbortController ( )
946+ setTimeout ( ( ) => controller . abort ( new DOMException ( "timed out" , "TimeoutError" ) ) , ms )
947+ return controller . signal
948+ } )
949+ const caller = new AbortController ( )
950+ try {
951+ mockComplete . mockImplementation (
952+ ( _options : unknown , requestOptions ?: { fetchOptions ?: { signal ?: AbortSignal } } ) =>
953+ new Promise < never > ( ( _resolve , reject ) => {
954+ const signal = requestOptions ?. fetchOptions ?. signal
955+ const fail = ( ) => reject ( new Error ( "socket closed" ) )
956+ caller . signal . addEventListener (
957+ "abort" ,
958+ ( ) => {
959+ // Caller aborts first; the mock then rejects a plain error.
960+ fail ( )
961+ } ,
962+ { once : true } ,
963+ )
964+ if ( signal ?. aborted ) fail ( )
965+ } ) ,
966+ )
967+
968+ const promise = handler . completePrompt ( "Test prompt" , { abortSignal : caller . signal , timeoutMs : 100 } )
969+ const assertion = expect ( promise ) . rejects . toMatchObject ( { name : "AbortError" } )
970+ caller . abort ( )
971+ await assertion
972+ expect ( caller . signal . aborted ) . toBe ( true )
973+ } finally {
974+ timeoutSpy . mockRestore ( )
975+ vi . useRealTimers ( )
976+ }
977+ } )
978+
911979 it ( "should surface a standard AbortError when the SDK throws an abort error before the signal flag propagates" , async ( ) => {
912980 // The signal flag has not propagated yet, but the SDK rejects with its
913981 // own abort error: isRequestAborted must catch the error-name branch.
0 commit comments