@@ -168,14 +168,19 @@ describe("VsCodeLmHandler", () => {
168168 chunks . push ( chunk )
169169 }
170170
171- expect ( chunks ) . toHaveLength ( 2 ) // Text chunk + usage chunk
172- expect ( chunks [ 0 ] ) . toEqual ( {
171+ expect ( chunks ) . toHaveLength ( 3 ) // Initial usage + text chunk + final usage chunk
172+ expect ( chunks [ 0 ] ) . toMatchObject ( {
173+ type : "usage" ,
174+ inputTokens : expect . any ( Number ) ,
175+ outputTokens : 0 ,
176+ } )
177+ expect ( chunks [ 1 ] ) . toEqual ( {
173178 type : "text" ,
174179 text : responseText ,
175180 } )
176- expect ( chunks [ 1 ] ) . toMatchObject ( {
181+ expect ( chunks [ 2 ] ) . toMatchObject ( {
177182 type : "usage" ,
178- inputTokens : expect . any ( Number ) ,
183+ inputTokens : 0 ,
179184 outputTokens : expect . any ( Number ) ,
180185 } )
181186 } )
@@ -216,8 +221,13 @@ describe("VsCodeLmHandler", () => {
216221 chunks . push ( chunk )
217222 }
218223
219- expect ( chunks ) . toHaveLength ( 2 ) // Tool call chunk + usage chunk
220- expect ( chunks [ 0 ] ) . toEqual ( {
224+ expect ( chunks ) . toHaveLength ( 3 ) // Initial usage + tool call chunk + final usage chunk
225+ expect ( chunks [ 0 ] ) . toMatchObject ( {
226+ type : "usage" ,
227+ inputTokens : expect . any ( Number ) ,
228+ outputTokens : 0 ,
229+ } )
230+ expect ( chunks [ 1 ] ) . toEqual ( {
221231 type : "text" ,
222232 text : JSON . stringify ( { type : "tool_call" , ...toolCallData } ) ,
223233 } )
@@ -234,7 +244,17 @@ describe("VsCodeLmHandler", () => {
234244
235245 mockLanguageModelChat . sendRequest . mockRejectedValueOnce ( new Error ( "API Error" ) )
236246
237- await expect ( handler . createMessage ( systemPrompt , messages ) . next ( ) ) . rejects . toThrow ( "API Error" )
247+ const stream = handler . createMessage ( systemPrompt , messages )
248+ // First chunk should be the initial usage
249+ const firstChunk = await stream . next ( )
250+ expect ( firstChunk . value ) . toMatchObject ( {
251+ type : "usage" ,
252+ inputTokens : expect . any ( Number ) ,
253+ outputTokens : 0 ,
254+ } )
255+
256+ // The error should occur when trying to get the next chunk
257+ await expect ( stream . next ( ) ) . rejects . toThrow ( "API Error" )
238258 } )
239259 } )
240260
@@ -262,6 +282,19 @@ describe("VsCodeLmHandler", () => {
262282 } )
263283
264284 describe ( "completePrompt" , ( ) => {
285+ beforeEach ( ( ) => {
286+ // Ensure we have a fresh mock for CancellationTokenSource
287+ const mockCancellationTokenSource = {
288+ token : {
289+ isCancellationRequested : false ,
290+ onCancellationRequested : vi . fn ( ) ,
291+ } ,
292+ cancel : vi . fn ( ) ,
293+ dispose : vi . fn ( ) ,
294+ }
295+ ; ( vscode . CancellationTokenSource as Mock ) . mockReturnValue ( mockCancellationTokenSource )
296+ } )
297+
265298 it ( "should complete single prompt" , async ( ) => {
266299 const mockModel = { ...mockLanguageModelChat }
267300 ; ( vscode . lm . selectChatModels as Mock ) . mockResolvedValueOnce ( [ mockModel ] )
0 commit comments