@@ -5808,6 +5808,18 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58085808 } )
58095809
58105810 describe ( "getTaskWithId" , ( ) => {
5811+ const apiHistoryPath = path . join ( "/test/task/path" , "api_conversation_history.json" )
5812+
5813+ beforeEach ( async ( ) => {
5814+ const fsp = await import ( "fs/promises" )
5815+ vi . mocked ( fsp . readFile ) . mockReset ( ) . mockResolvedValue ( "" )
5816+ } )
5817+
5818+ afterEach ( async ( ) => {
5819+ const fsp = await import ( "fs/promises" )
5820+ vi . mocked ( fsp . readFile ) . mockReset ( ) . mockResolvedValue ( "" )
5821+ } )
5822+
58115823 it ( "does not restore a deleted file-backed task from legacy history" , async ( ) => {
58125824 const historyItem = {
58135825 id : "deleted-task" ,
@@ -5864,12 +5876,15 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58645876 const fsp = await import ( "fs/promises" )
58655877 const missing = Object . assign ( new Error ( "missing" ) , { code : "ENOENT" } )
58665878 const messages = [ { role : "user" , content : "committed" } ]
5867- vi . mocked ( fsp . readFile )
5868- . mockRejectedValueOnce ( missing )
5869- . mockRejectedValueOnce ( missing )
5870- . mockResolvedValueOnce ( JSON . stringify ( messages ) )
5879+ let historyReads = 0
5880+ vi . mocked ( fsp . readFile ) . mockImplementation ( async ( filePath ) => {
5881+ if ( filePath !== apiHistoryPath ) return ""
5882+ if ( ++ historyReads <= 2 ) throw missing
5883+ return JSON . stringify ( messages )
5884+ } )
58715885 const result = await provider . getTaskWithId ( historyItem . id )
58725886 expect ( result . apiConversationHistory ) . toEqual ( messages )
5887+ expect ( historyReads ) . toBe ( 3 )
58735888 expect ( recoverJsonFile ) . toHaveBeenCalledWith ( result . apiConversationHistoryFilePath )
58745889 } )
58755890
@@ -5879,7 +5894,10 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58795894 key === "taskHistory" ? [ historyItem ] : undefined ,
58805895 )
58815896 const fsp = await import ( "fs/promises" )
5882- vi . mocked ( fsp . readFile ) . mockRejectedValueOnce ( Object . assign ( new Error ( code ) , { code } ) )
5897+ vi . mocked ( fsp . readFile ) . mockImplementation ( async ( filePath ) => {
5898+ if ( filePath === apiHistoryPath ) throw Object . assign ( new Error ( code ) , { code } )
5899+ return ""
5900+ } )
58835901 await expect ( provider . getTaskWithId ( historyItem . id ) ) . rejects . toMatchObject ( { code } )
58845902 } )
58855903
@@ -5894,7 +5912,9 @@ describe("ClineProvider - Comprehensive Edit/Delete Edge Cases", () => {
58945912
58955913 // Make readFile return corrupted JSON
58965914 const fsp = await import ( "fs/promises" )
5897- vi . mocked ( fsp . readFile ) . mockResolvedValueOnce ( "{not valid json!!!" as never )
5915+ vi . mocked ( fsp . readFile ) . mockImplementation ( async ( filePath ) =>
5916+ filePath === apiHistoryPath ? "{not valid json!!!" : "" ,
5917+ )
58985918
58995919 const deleteTaskSpy = vi . spyOn ( provider , "deleteTaskFromState" )
59005920
0 commit comments