@@ -624,6 +624,40 @@ describe("VsCodeLmHandler", () => {
624624 expect ( removeEventListenerSpy ) . toHaveBeenCalledWith ( "abort" , expect . any ( Function ) )
625625 } )
626626
627+ it ( "should cancel the request token when the consumer stops consuming early" , async ( ) => {
628+ const systemPrompt = "You are a helpful assistant"
629+ const messages : Anthropic . Messages . MessageParam [ ] = [ { role : "user" as const , content : "Hello" } ]
630+
631+ // The mock stream yields one chunk and then stays pending, so the host
632+ // request is still in flight when the consumer gives up.
633+ let releaseStream : ( ) => void = ( ) => { }
634+ const streamGate = new Promise < void > ( ( resolve ) => {
635+ releaseStream = resolve
636+ } )
637+ mockLanguageModelChat . sendRequest . mockResolvedValueOnce ( {
638+ stream : ( async function * ( ) {
639+ yield new vscode . LanguageModelTextPart ( "Hello" )
640+ await streamGate
641+ } ) ( ) ,
642+ text : ( async function * ( ) {
643+ yield "Hello"
644+ await streamGate
645+ } ) ( ) ,
646+ } )
647+
648+ const stream = handler . createMessage ( systemPrompt , messages )
649+ await stream . next ( )
650+ // Stop consuming before the stream finishes: the premature closure must
651+ // cancel the request token so the host stops the request (dispose alone
652+ // frees resources without cancelling).
653+ // (the AsyncGenerator type requires the return value argument)
654+ await stream . return ( undefined )
655+
656+ const tokenSource = tokenSourceInstance ( )
657+ expect ( tokenSource . cancel ) . toHaveBeenCalled ( )
658+ expect ( tokenSource . dispose ) . toHaveBeenCalled ( )
659+ } )
660+
627661 it ( "should throw a Zoo Code branded error on stream error with error-like object" , async ( ) => {
628662 const systemPrompt = "You are a helpful assistant"
629663 const messages : Anthropic . Messages . MessageParam [ ] = [
@@ -1279,9 +1313,10 @@ describe("VsCodeLmHandler", () => {
12791313 const promise = handler . completePrompt ( "Test prompt" , { abortSignal : controller . signal } )
12801314 await expect ( promise ) . rejects . toSatisfy ( ( error ) => error instanceof Error && error . name === "AbortError" )
12811315
1282- // Fails fast before invoking the host: no cancellation token source is
1283- // created and the sendRequest is never called.
1316+ // Fails fast before invoking the host: the pre-aborted signal cancels the
1317+ // request token and sendRequest is never called.
12841318 expect ( mockLanguageModelChat . sendRequest ) . toHaveBeenCalledTimes ( 0 )
1319+ expect ( tokenSourceInstance ( ) . cancel ) . toHaveBeenCalled ( )
12851320 } )
12861321
12871322 it ( "should reject with an AbortError when the signal aborts mid-flight" , async ( ) => {
0 commit comments