@@ -145,9 +145,11 @@ describe("KimiCodeHandler", () => {
145145
146146 await expect ( handler . completePrompt ( "test" , { abortSignal : signal , timeoutMs : 5_000 } ) ) . resolves . toBe ( "retried" )
147147
148- // Both the initial attempt and the retry after the forced refresh carry the options.
149- expect ( createCompletion ) . toHaveBeenNthCalledWith ( 1 , expect . anything ( ) , { signal, timeout : 5_000 } )
150- expect ( createCompletion ) . toHaveBeenNthCalledWith ( 2 , expect . anything ( ) , { signal, timeout : 5_000 } )
148+ // Both the initial attempt and the retry after the forced refresh carry the caller's own signal.
149+ expect ( createCompletion . mock . calls [ 0 ] [ 1 ] . signal ) . toBe ( signal )
150+ expect ( createCompletion . mock . calls [ 0 ] [ 1 ] . timeout ) . toBe ( 5_000 )
151+ expect ( createCompletion . mock . calls [ 1 ] [ 1 ] . signal ) . toBe ( signal )
152+ expect ( createCompletion . mock . calls [ 1 ] [ 1 ] . timeout ) . toBe ( 5_000 )
151153 } )
152154
153155 it ( "does not start preparation when the caller already aborted" , async ( ) => {
@@ -159,6 +161,26 @@ describe("KimiCodeHandler", () => {
159161 expect ( mockGetAccessToken ) . not . toHaveBeenCalled ( )
160162 } )
161163
164+ it ( "stops before model discovery and completion when aborted during token resolution" , async ( ) => {
165+ const handler = new KimiCodeHandler ( { kimiCodeAuthMethod : "oauth" } )
166+ let resolveToken ! : ( token : string ) => void
167+ mockGetAccessToken . mockReturnValueOnce (
168+ new Promise < string > ( ( resolve ) => {
169+ resolveToken = resolve
170+ } ) ,
171+ )
172+ const controller = new AbortController ( )
173+
174+ const completion = handler . completePrompt ( "test" , { abortSignal : controller . signal } )
175+ await vi . waitFor ( ( ) => expect ( resolveToken ) . toBeDefined ( ) )
176+ controller . abort ( )
177+ resolveToken ( "oauth-token" )
178+
179+ await expect ( completion ) . rejects . toMatchObject ( { name : "AbortError" } )
180+ // The cancelled request never reaches model discovery or the provider.
181+ expect ( mockGetModels ) . not . toHaveBeenCalled ( )
182+ } )
183+
162184 it ( "does not retry on 401 when using API key auth" , async ( ) => {
163185 const handler = new KimiCodeHandler ( { kimiCodeAuthMethod : "api-key" , kimiCodeApiKey : "key" } )
164186 vi . spyOn ( globalThis , "fetch" ) . mockResolvedValueOnce ( new Response ( null , { status : 401 } ) )
0 commit comments