@@ -320,6 +320,50 @@ describe.each([[RepoPerTaskCheckpointService, "RepoPerTaskCheckpointService"]])(
320320 await expect ( fs . readFile ( untrackedFile , "utf-8" ) ) . rejects . toThrow ( )
321321 } )
322322
323+ it ( "does not modify the workspace when restoring a missing commit" , async ( ) => {
324+ await fs . writeFile ( testFile , "Committed content" )
325+ const commit = await service . saveCheckpoint ( "Checkpoint before loss" )
326+ expect ( commit ?. commit ) . toBeTruthy ( )
327+
328+ const untrackedFile = path . join ( service . workspaceDir , "precious-untracked.txt" )
329+ await fs . writeFile ( untrackedFile , "Unsaved user work" )
330+ await fs . writeFile ( testFile , "Uncommitted edit" )
331+
332+ const errorHandler = vi . fn ( )
333+ const restoreHandler = vi . fn ( )
334+ service . on ( "error" , errorHandler )
335+ service . on ( "restore" , restoreHandler )
336+ const checkpointsBeforeRestore = service . getCheckpoints ( )
337+ const missingCommitHash = "0" . repeat ( 40 )
338+ const expectedMessage = `Cannot restore checkpoint ${ missingCommitHash } : this checkpoint no longer exists in the checkpoint repository. No files were changed.`
339+
340+ await expect ( service . restoreCheckpoint ( missingCommitHash ) ) . rejects . toThrow ( expectedMessage )
341+
342+ expect ( await fs . readFile ( untrackedFile , "utf-8" ) ) . toBe ( "Unsaved user work" )
343+ expect ( await fs . readFile ( testFile , "utf-8" ) ) . toBe ( "Uncommitted edit" )
344+ expect ( service . getCheckpoints ( ) ) . toEqual ( checkpointsBeforeRestore )
345+ expect ( errorHandler ) . toHaveBeenCalledExactlyOnceWith ( {
346+ type : "error" ,
347+ error : new Error ( expectedMessage ) ,
348+ } )
349+ expect ( restoreHandler ) . not . toHaveBeenCalled ( )
350+ } )
351+
352+ it ( "still restores normally to a valid commit" , async ( ) => {
353+ await fs . writeFile ( testFile , "Checkpointed content" )
354+ const commit = await service . saveCheckpoint ( "Valid checkpoint" )
355+ expect ( commit ?. commit ) . toBeTruthy ( )
356+
357+ await fs . writeFile ( testFile , "Later edit" )
358+ const laterTrackedFile = path . join ( service . workspaceDir , "created-after.txt" )
359+ await fs . writeFile ( laterTrackedFile , "Created after checkpoint" )
360+ await service . saveCheckpoint ( "After checkpoint" )
361+
362+ await service . restoreCheckpoint ( commit ! . commit )
363+ expect ( await fs . readFile ( testFile , "utf-8" ) ) . toBe ( "Checkpointed content" )
364+ await expect ( fs . readFile ( laterTrackedFile , "utf-8" ) ) . rejects . toThrow ( )
365+ } )
366+
323367 it ( "does not create a checkpoint for ignored files" , async ( ) => {
324368 // Create a file that matches an ignored pattern (e.g., .log file).
325369 const ignoredFile = path . join ( service . workspaceDir , "ignored.log" )
0 commit comments