@@ -36,6 +36,8 @@ vi.mock("../../../i18n", () => ({
3636 ) ,
3737} ) )
3838
39+ const LATEST_CHECKPOINT = "b" . repeat ( 40 )
40+
3941function createFixture ( ) {
4042 const taskDouble = {
4143 taskId : "test-task-123" ,
@@ -45,7 +47,7 @@ function createFixture() {
4547 { ts : 1 , type : "say" , say : "text" , text : "Initial task" } ,
4648 { ts : 2 , type : "say" , say : "checkpoint_saved" , text : "initial-checkpoint" } ,
4749 { ts : 3 , type : "say" , say : "user_feedback" , text : "Latest prompt" } ,
48- { ts : 4 , type : "say" , say : "checkpoint_saved" , text : "latest-prompt-checkpoint" } ,
50+ { ts : 4 , type : "say" , say : "checkpoint_saved" , text : LATEST_CHECKPOINT } ,
4951 { ts : 5 , type : "say" , say : "completion_result" , text : "Task complete" } ,
5052 { ts : 6 , type : "ask" , ask : "completion_result" , text : "" , partial : false } ,
5153 ] satisfies Task [ "clineMessages" ] ,
@@ -167,7 +169,7 @@ describe("webviewMessageHandler - checkpoint operations", () => {
167169 expect ( taskDouble . abortTask ) . toHaveBeenCalledOnce ( )
168170 expect ( taskDouble . checkpointRestore ) . toHaveBeenCalledWith ( {
169171 ts : 3 ,
170- commitHash : "latest-prompt-checkpoint" ,
172+ commitHash : LATEST_CHECKPOINT ,
171173 mode : "restore" ,
172174 operation : "delete" ,
173175 } )
@@ -227,7 +229,7 @@ describe("webviewMessageHandler - checkpoint operations", () => {
227229
228230 expect ( taskDouble . checkpointRestore ) . toHaveBeenCalledExactlyOnceWith ( {
229231 ts : 3 ,
230- commitHash : "latest-prompt-checkpoint" ,
232+ commitHash : LATEST_CHECKPOINT ,
231233 mode : "restore" ,
232234 operation : "edit" ,
233235 } )
@@ -290,14 +292,14 @@ describe("webviewMessageHandler - checkpoint operations", () => {
290292 fixture . taskDouble . checkpointRestore . mockRejectedValueOnce ( failure )
291293 const message : WebviewMessage =
292294 type === "checkpointRestore"
293- ? { type, payload : { ts : 4 , commitHash : "latest-prompt-checkpoint" , mode : "restore" } }
295+ ? { type, payload : { ts : 4 , commitHash : LATEST_CHECKPOINT , mode : "restore" } }
294296 : { type }
295297 await webviewMessageHandler ( fixture . provider , message )
296298
297299 expect ( fixture . providerDouble . cancelTask ) . toHaveBeenCalledOnce ( )
298300 expect ( fixture . taskDouble . checkpointRestore ) . toHaveBeenCalledWith ( {
299301 ts : 4 ,
300- commitHash : "latest-prompt-checkpoint" ,
302+ commitHash : LATEST_CHECKPOINT ,
301303 mode : "restore" ,
302304 } )
303305 expect ( console . error ) . toHaveBeenCalledWith ( `[${ type } ] checkpointRestore failed:` , failure )
@@ -309,12 +311,41 @@ describe("webviewMessageHandler - checkpoint operations", () => {
309311 expectUnchanged ( messages , history )
310312 } )
311313
314+ it . each ( [
315+ { name : "malformed" , commitHash : "HEAD~1" } ,
316+ { name : "unrecorded" , commitHash : "c" . repeat ( 40 ) } ,
317+ ] ) ( "checkpointRestore rejects a $name hash without cancelling the task" , async ( { commitHash } ) => {
318+ const messages = structuredClone ( fixture . task . clineMessages )
319+ const history = structuredClone ( fixture . task . apiConversationHistory )
320+ await webviewMessageHandler ( fixture . provider , {
321+ type : "checkpointRestore" ,
322+ payload : { ts : 4 , commitHash, mode : "restore" } ,
323+ } )
324+
325+ expect ( fixture . providerDouble . cancelTask ) . not . toHaveBeenCalled ( )
326+ expect ( fixture . taskDouble . checkpointRestore ) . not . toHaveBeenCalled ( )
327+ expect ( vscode . window . showErrorMessage ) . toHaveBeenCalledExactlyOnceWith (
328+ "common:errors.checkpoint_restore_failed: common:errors.checkpoint_invalid_hash" ,
329+ )
330+ expectUnchanged ( messages , history )
331+ } )
332+
333+ it ( "checkpointRestore cancels before restoring a recorded hash" , async ( ) => {
334+ await webviewMessageHandler ( fixture . provider , {
335+ type : "checkpointRestore" ,
336+ payload : { ts : 4 , commitHash : LATEST_CHECKPOINT , mode : "restore" } ,
337+ } )
338+ expect ( fixture . providerDouble . cancelTask . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
339+ fixture . taskDouble . checkpointRestore . mock . invocationCallOrder [ 0 ] ,
340+ )
341+ } )
342+
312343 describe ( "completion checkpoint actions" , ( ) => {
313344 it ( "diffs the checkpoint created after the latest prompt" , async ( ) => {
314345 await webviewMessageHandler ( fixture . provider , { type : "completionCheckpointDiff" } )
315346 expect ( fixture . taskDouble . checkpointDiff ) . toHaveBeenCalledWith ( {
316347 ts : 4 ,
317- commitHash : "latest-prompt-checkpoint" ,
348+ commitHash : LATEST_CHECKPOINT ,
318349 mode : "to-current" ,
319350 } )
320351 } )
@@ -323,7 +354,7 @@ describe("webviewMessageHandler - checkpoint operations", () => {
323354 await webviewMessageHandler ( fixture . provider , { type : "completionCheckpointRestore" } )
324355 expect ( fixture . taskDouble . checkpointRestore ) . toHaveBeenCalledWith ( {
325356 ts : 4 ,
326- commitHash : "latest-prompt-checkpoint" ,
357+ commitHash : LATEST_CHECKPOINT ,
327358 mode : "restore" ,
328359 } )
329360 expect ( fixture . providerDouble . cancelTask . mock . invocationCallOrder [ 0 ] ) . toBeLessThan (
0 commit comments