@@ -4,7 +4,7 @@ import { describe, expect, it, vi, beforeEach } from "vitest"
44import type { ExtensionMessage , WebviewMessage } from "@roo-code/types"
55
66import { webviewMessageHandler } from "../webviewMessageHandler"
7- import { rollbackFile , rollbackStep } from "../../checkpoints/rollback"
7+ import { restoreLatestFile , rollbackFile , rollbackStep } from "../../checkpoints/rollback"
88import type { Task } from "../../task/Task"
99import type { ClineProvider } from "../ClineProvider"
1010
@@ -14,6 +14,7 @@ import type { ClineProvider } from "../ClineProvider"
1414vi . mock ( "../../checkpoints/rollback" , ( ) => ( {
1515 rollbackFile : vi . fn ( ) ,
1616 rollbackStep : vi . fn ( ) ,
17+ restoreLatestFile : vi . fn ( ) ,
1718} ) )
1819
1920vi . mock ( "vscode" , ( ) => ( {
@@ -197,10 +198,104 @@ describe("webviewMessageHandler - change card rollback", () => {
197198 type : "checkpointRollbackStep" ,
198199 payload : { cardTs : 1000 , filePaths : [ ] } ,
199200 } )
201+ await webviewMessageHandler ( provider , {
202+ type : "checkpointRestoreLatestFile" ,
203+ payload : { cardTs : 1000 } as unknown as WebviewMessage [ "payload" ] ,
204+ } )
200205
201206 expect ( rollbackFile ) . not . toHaveBeenCalled ( )
202207 expect ( rollbackStep ) . not . toHaveBeenCalled ( )
208+ expect ( restoreLatestFile ) . not . toHaveBeenCalled ( )
203209 expect ( postMessageToWebview ) . not . toHaveBeenCalled ( )
204210 } )
205211 } )
212+
213+ describe ( "checkpointRestoreLatestFile" , ( ) => {
214+ it ( "restores the file to its latest recorded version and posts the success outcome" , async ( ) => {
215+ vi . mocked ( restoreLatestFile ) . mockResolvedValueOnce ( { filePath : "src/a.ts" , success : true } )
216+
217+ await webviewMessageHandler ( provider , {
218+ type : "checkpointRestoreLatestFile" ,
219+ payload : { cardTs : 1000 , filePath : "src/a.ts" } ,
220+ } )
221+
222+ expect ( restoreLatestFile ) . toHaveBeenCalledWith ( mockTask , "src/a.ts" )
223+ expect ( postMessageToWebview ) . toHaveBeenCalledWith ( {
224+ type : "checkpointRollbackResult" ,
225+ checkpointRollbackResult : {
226+ cardTs : 1000 ,
227+ kind : "restore-latest" ,
228+ filePath : "src/a.ts" ,
229+ success : true ,
230+ } ,
231+ } )
232+ } )
233+
234+ it ( "flags a no-op restore-latest so the card can report it" , async ( ) => {
235+ vi . mocked ( restoreLatestFile ) . mockResolvedValueOnce ( { filePath : "src/a.ts" , success : true , noOp : true } )
236+
237+ await webviewMessageHandler ( provider , {
238+ type : "checkpointRestoreLatestFile" ,
239+ payload : { cardTs : 1000 , filePath : "src/a.ts" } ,
240+ } )
241+
242+ expect ( postMessageToWebview ) . toHaveBeenCalledWith ( {
243+ type : "checkpointRollbackResult" ,
244+ checkpointRollbackResult : {
245+ cardTs : 1000 ,
246+ kind : "restore-latest" ,
247+ filePath : "src/a.ts" ,
248+ success : true ,
249+ noOp : true ,
250+ } ,
251+ } )
252+ } )
253+
254+ it ( "posts the error outcome when the restore fails" , async ( ) => {
255+ vi . mocked ( restoreLatestFile ) . mockResolvedValueOnce ( {
256+ filePath : "src/a.ts" ,
257+ success : false ,
258+ error : "Checkpoints are not enabled for this task" ,
259+ } )
260+
261+ await webviewMessageHandler ( provider , {
262+ type : "checkpointRestoreLatestFile" ,
263+ payload : { cardTs : 1000 , filePath : "src/a.ts" } ,
264+ } )
265+
266+ expect ( postMessageToWebview ) . toHaveBeenCalledWith ( {
267+ type : "checkpointRollbackResult" ,
268+ checkpointRollbackResult : {
269+ cardTs : 1000 ,
270+ kind : "restore-latest" ,
271+ filePath : "src/a.ts" ,
272+ success : false ,
273+ error : "Checkpoints are not enabled for this task" ,
274+ } ,
275+ } )
276+ } )
277+
278+ it ( "posts a correlated failure result when there is no current task" , async ( ) => {
279+ const emptyProvider = makeProvider ( undefined )
280+
281+ await webviewMessageHandler ( emptyProvider , {
282+ type : "checkpointRestoreLatestFile" ,
283+ payload : { cardTs : 1000 , filePath : "src/a.ts" } ,
284+ } )
285+
286+ expect ( restoreLatestFile ) . not . toHaveBeenCalled ( )
287+ // The requesting card must clear its pending state, so the handler
288+ // posts a correlated failure instead of nothing.
289+ expect ( postMessageToWebview ) . toHaveBeenCalledWith ( {
290+ type : "checkpointRollbackResult" ,
291+ checkpointRollbackResult : {
292+ cardTs : 1000 ,
293+ kind : "restore-latest" ,
294+ filePath : "src/a.ts" ,
295+ success : false ,
296+ error : "No active task to restore from." ,
297+ } ,
298+ } )
299+ } )
300+ } )
206301} )
0 commit comments