11import { describe , it , expect , vi , beforeEach , afterEach } from "vitest"
22import { checkpointSave , checkpointRestore , checkpointDiff , getCheckpointService } from "../index"
33import { MessageManager } from "../../message-manager"
4+ import { RepoPerTaskCheckpointService } from "../../../services/checkpoints"
45import * as vscode from "vscode"
56
7+ const RESTORE_HASH = "a" . repeat ( 40 )
8+ const HASH_1 = "1" . repeat ( 40 )
9+ const HASH_2 = "2" . repeat ( 40 )
10+
611// Mock vscode
712vi . mock ( "vscode" , ( ) => ( {
813 window : {
@@ -207,6 +212,7 @@ describe("Checkpoint functionality", () => {
207212 { ts : 1 , say : "user" , text : "Message 1" } ,
208213 { ts : 2 , say : "assistant" , text : "Message 2" } ,
209214 { ts : 3 , say : "user" , text : "Message 3" } ,
215+ { ts : 4 , say : "checkpoint_saved" , text : RESTORE_HASH } ,
210216 ]
211217 mockTask . apiConversationHistory = [
212218 { ts : 1 , role : "user" , content : [ { type : "text" , text : "Message 1" } ] } ,
@@ -218,12 +224,12 @@ describe("Checkpoint functionality", () => {
218224 it ( "should restore checkpoint for delete operation" , async ( ) => {
219225 await checkpointRestore ( mockTask , {
220226 ts : 2 ,
221- commitHash : "abc123" ,
227+ commitHash : RESTORE_HASH ,
222228 mode : "restore" ,
223229 operation : "delete" ,
224230 } )
225231
226- expect ( mockCheckpointService . restoreCheckpoint ) . toHaveBeenCalledWith ( "abc123" )
232+ expect ( mockCheckpointService . restoreCheckpoint ) . toHaveBeenCalledWith ( RESTORE_HASH )
227233 expect ( mockTask . overwriteApiConversationHistory ) . toHaveBeenCalledWith ( [
228234 { ts : 1 , role : "user" , content : [ { type : "text" , text : "Message 1" } ] } ,
229235 ] )
@@ -234,12 +240,12 @@ describe("Checkpoint functionality", () => {
234240 it ( "should restore checkpoint for edit operation" , async ( ) => {
235241 await checkpointRestore ( mockTask , {
236242 ts : 2 ,
237- commitHash : "abc123" ,
243+ commitHash : RESTORE_HASH ,
238244 mode : "restore" ,
239245 operation : "edit" ,
240246 } )
241247
242- expect ( mockCheckpointService . restoreCheckpoint ) . toHaveBeenCalledWith ( "abc123" )
248+ expect ( mockCheckpointService . restoreCheckpoint ) . toHaveBeenCalledWith ( RESTORE_HASH )
243249 expect ( mockTask . overwriteApiConversationHistory ) . toHaveBeenCalledWith ( [
244250 { ts : 1 , role : "user" , content : [ { type : "text" , text : "Message 1" } ] } ,
245251 ] )
@@ -254,11 +260,11 @@ describe("Checkpoint functionality", () => {
254260 it ( "should handle preview mode without modifying messages" , async ( ) => {
255261 await checkpointRestore ( mockTask , {
256262 ts : 2 ,
257- commitHash : "abc123" ,
263+ commitHash : RESTORE_HASH ,
258264 mode : "preview" ,
259265 } )
260266
261- expect ( mockCheckpointService . restoreCheckpoint ) . toHaveBeenCalledWith ( "abc123" )
267+ expect ( mockCheckpointService . restoreCheckpoint ) . toHaveBeenCalledWith ( RESTORE_HASH )
262268 expect ( mockTask . overwriteApiConversationHistory ) . not . toHaveBeenCalled ( )
263269 expect ( mockTask . overwriteClineMessages ) . not . toHaveBeenCalled ( )
264270 expect ( mockProvider . cancelTask ) . toHaveBeenCalled ( )
@@ -267,7 +273,7 @@ describe("Checkpoint functionality", () => {
267273 it ( "should handle missing message gracefully" , async ( ) => {
268274 await checkpointRestore ( mockTask , {
269275 ts : 999 , // Non-existent timestamp
270- commitHash : "abc123" ,
276+ commitHash : RESTORE_HASH ,
271277 mode : "restore" ,
272278 } )
273279
@@ -285,7 +291,7 @@ describe("Checkpoint functionality", () => {
285291 await expect (
286292 checkpointRestore ( mockTask , {
287293 ts : 2 ,
288- commitHash : "abc123" ,
294+ commitHash : RESTORE_HASH ,
289295 mode : "restore" ,
290296 operation,
291297 } ) ,
@@ -302,15 +308,31 @@ describe("Checkpoint functionality", () => {
302308 expect ( mockProvider . log ) . toHaveBeenCalledWith ( "[checkpointRestore] failed to restore checkpoint" )
303309 } ,
304310 )
311+
312+ it . each ( [
313+ [ "a malformed hash" , "HEAD~1" ] ,
314+ [ "an abbreviated hash" , "a" . repeat ( 7 ) ] ,
315+ [ "a well-formed but unrecorded hash" , "b" . repeat ( 40 ) ] ,
316+ [ "a non-string" , 42 ] ,
317+ ] ) ( "rejects %s before any Git call" , async ( _label , commitHash ) => {
318+ await expect (
319+ checkpointRestore ( mockTask , { ts : 2 , commitHash : commitHash as string , mode : "restore" } ) ,
320+ ) . rejects . toThrow ( "common:errors.checkpoint_invalid_hash" )
321+
322+ expect ( RepoPerTaskCheckpointService . create ) . not . toHaveBeenCalled ( )
323+ expect ( mockCheckpointService . restoreCheckpoint ) . not . toHaveBeenCalled ( )
324+ expect ( mockTask . overwriteClineMessages ) . not . toHaveBeenCalled ( )
325+ expect ( mockProvider . cancelTask ) . not . toHaveBeenCalled ( )
326+ } )
305327 } )
306328
307329 describe ( "checkpointDiff" , ( ) => {
308330 beforeEach ( ( ) => {
309331 mockTask . clineMessages = [
310332 { ts : 1 , say : "user" , text : "Message 1" } ,
311- { ts : 2 , say : "checkpoint_saved" , text : "commit1" } ,
333+ { ts : 2 , say : "checkpoint_saved" , text : HASH_1 } ,
312334 { ts : 3 , say : "user" , text : "Message 2" } ,
313- { ts : 4 , say : "checkpoint_saved" , text : "commit2" } ,
335+ { ts : 4 , say : "checkpoint_saved" , text : HASH_2 } ,
314336 ]
315337 } )
316338
@@ -325,12 +347,12 @@ describe("Checkpoint functionality", () => {
325347
326348 await checkpointDiff ( mockTask , {
327349 ts : 4 ,
328- commitHash : "commit2" ,
350+ commitHash : HASH_2 ,
329351 mode : "to-current" ,
330352 } )
331353
332354 expect ( mockCheckpointService . getDiff ) . toHaveBeenCalledWith ( {
333- from : "commit2" ,
355+ from : HASH_2 ,
334356 to : undefined ,
335357 } )
336358 expect ( vscode . commands . executeCommand ) . toHaveBeenCalledWith (
@@ -350,13 +372,13 @@ describe("Checkpoint functionality", () => {
350372 mockCheckpointService . getDiff . mockResolvedValue ( mockChanges )
351373 await checkpointDiff ( mockTask , {
352374 ts : 4 ,
353- commitHash : "commit1" ,
375+ commitHash : HASH_1 ,
354376 mode : "checkpoint" ,
355377 } )
356378
357379 expect ( mockCheckpointService . getDiff ) . toHaveBeenCalledWith ( {
358- from : "commit1" ,
359- to : "commit2" ,
380+ from : HASH_1 ,
381+ to : HASH_2 ,
360382 } )
361383 expect ( vscode . commands . executeCommand ) . toHaveBeenCalledWith (
362384 "vscode.changes" ,
@@ -376,13 +398,13 @@ describe("Checkpoint functionality", () => {
376398
377399 await checkpointDiff ( mockTask , {
378400 ts : 4 ,
379- commitHash : "commit1" ,
401+ commitHash : HASH_1 ,
380402 mode : "checkpoint" ,
381403 } )
382404
383405 expect ( mockCheckpointService . getDiff ) . toHaveBeenCalledWith ( {
384- from : "commit1" , // Should find the next checkpoint
385- to : "commit2" ,
406+ from : HASH_1 , // Should find the next checkpoint
407+ to : HASH_2 ,
386408 } )
387409 } )
388410
@@ -391,20 +413,32 @@ describe("Checkpoint functionality", () => {
391413
392414 await checkpointDiff ( mockTask , {
393415 ts : 4 ,
394- commitHash : "commit2" ,
416+ commitHash : HASH_2 ,
395417 mode : "to-current" ,
396418 } )
397419
398420 expect ( vscode . window . showInformationMessage ) . toHaveBeenCalledWith ( "common:errors.checkpoint_no_changes" )
399421 expect ( vscode . commands . executeCommand ) . not . toHaveBeenCalled ( )
400422 } )
401423
424+ it . each ( [
425+ [ "a malformed hash" , "HEAD~1" ] ,
426+ [ "a well-formed but unrecorded hash" , "b" . repeat ( 40 ) ] ,
427+ [ "a non-string" , 42 ] ,
428+ ] ) ( "rejects %s before any Git call" , async ( _label , commitHash ) => {
429+ await checkpointDiff ( mockTask , { commitHash : commitHash as string , mode : "to-current" } )
430+
431+ expect ( vscode . window . showErrorMessage ) . toHaveBeenCalledWith ( "common:errors.checkpoint_invalid_hash" )
432+ expect ( mockCheckpointService . getDiff ) . not . toHaveBeenCalled ( )
433+ expect ( mockTask . enableCheckpoints ) . toBe ( true )
434+ } )
435+
402436 it ( "should disable checkpoints on error" , async ( ) => {
403437 mockCheckpointService . getDiff . mockRejectedValue ( new Error ( "Diff failed" ) )
404438
405439 await checkpointDiff ( mockTask , {
406440 ts : 4 ,
407- commitHash : "commit2" ,
441+ commitHash : HASH_2 ,
408442 mode : "to-current" ,
409443 } )
410444
0 commit comments