@@ -38,8 +38,12 @@ vi.mock("fs/promises", async (importOriginal) => {
3838 }
3939} )
4040
41+ const { mockPWaitFor } = vi . hoisted ( ( ) => {
42+ return { mockPWaitFor : vi . fn ( ) . mockImplementation ( async ( ) => Promise . resolve ( ) ) }
43+ } )
44+
4145vi . mock ( "p-wait-for" , ( ) => ( {
42- default : vi . fn ( ) . mockImplementation ( async ( ) => Promise . resolve ( ) ) ,
46+ default : mockPWaitFor ,
4347} ) )
4448
4549vi . mock ( "vscode" , ( ) => {
@@ -344,4 +348,99 @@ describe("flushPendingToolResultsToHistory", () => {
344348 expect ( ( task . apiConversationHistory [ 0 ] as any ) . ts ) . toBeGreaterThanOrEqual ( beforeTs )
345349 expect ( ( task . apiConversationHistory [ 0 ] as any ) . ts ) . toBeLessThanOrEqual ( afterTs )
346350 } )
351+
352+ it ( "should skip waiting for assistantMessageSavedToHistory when flag is already true" , async ( ) => {
353+ const task = new Task ( {
354+ provider : mockProvider ,
355+ apiConfiguration : mockApiConfig ,
356+ task : "test task" ,
357+ startTask : false ,
358+ } )
359+
360+ // Set flag to true (assistant message already saved)
361+ task . assistantMessageSavedToHistory = true
362+
363+ // Set up pending tool result
364+ task . userMessageContent = [
365+ {
366+ type : "tool_result" ,
367+ tool_use_id : "tool-skip-wait" ,
368+ content : "Result when flag is true" ,
369+ } ,
370+ ]
371+
372+ // Clear mock call history
373+ mockPWaitFor . mockClear ( )
374+
375+ await task . flushPendingToolResultsToHistory ( )
376+
377+ // Should not have called pWaitFor since flag was already true
378+ expect ( mockPWaitFor ) . not . toHaveBeenCalled ( )
379+
380+ // Should still save the message
381+ expect ( task . apiConversationHistory . length ) . toBe ( 1 )
382+ expect ( ( task . apiConversationHistory [ 0 ] . content as any [ ] ) [ 0 ] . tool_use_id ) . toBe ( "tool-skip-wait" )
383+ } )
384+
385+ it ( "should wait for assistantMessageSavedToHistory when flag is false" , async ( ) => {
386+ const task = new Task ( {
387+ provider : mockProvider ,
388+ apiConfiguration : mockApiConfig ,
389+ task : "test task" ,
390+ startTask : false ,
391+ } )
392+
393+ // Flag is false by default - assistant message not yet saved
394+ expect ( task . assistantMessageSavedToHistory ) . toBe ( false )
395+
396+ // Set up pending tool result
397+ task . userMessageContent = [
398+ {
399+ type : "tool_result" ,
400+ tool_use_id : "tool-wait" ,
401+ content : "Result when flag is false" ,
402+ } ,
403+ ]
404+
405+ // Clear mock call history
406+ mockPWaitFor . mockClear ( )
407+
408+ await task . flushPendingToolResultsToHistory ( )
409+
410+ // Should have called pWaitFor since flag was false
411+ expect ( mockPWaitFor ) . toHaveBeenCalled ( )
412+
413+ // Should still save the message (mock resolves immediately)
414+ expect ( task . apiConversationHistory . length ) . toBe ( 1 )
415+ } )
416+
417+ it ( "should not flush when task is aborted during wait" , async ( ) => {
418+ const task = new Task ( {
419+ provider : mockProvider ,
420+ apiConfiguration : mockApiConfig ,
421+ task : "test task" ,
422+ startTask : false ,
423+ } )
424+
425+ // Flag is false - will need to wait
426+ task . assistantMessageSavedToHistory = false
427+
428+ // Set up pending tool result
429+ task . userMessageContent = [
430+ {
431+ type : "tool_result" ,
432+ tool_use_id : "tool-aborted" ,
433+ content : "Should not be saved" ,
434+ } ,
435+ ]
436+
437+ // Set abort flag - this will cause the condition in pWaitFor to return true
438+ // AND will cause early return after the wait
439+ task . abort = true
440+
441+ await task . flushPendingToolResultsToHistory ( )
442+
443+ // Should not have saved anything since task was aborted
444+ expect ( task . apiConversationHistory . length ) . toBe ( 0 )
445+ } )
347446} )
0 commit comments