@@ -7,7 +7,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
77import type { Task } from "../../task/Task"
88import type { ChangeJournalEntry } from "../changeJournal"
99import { getCheckpointService } from "../index"
10- import { appendChange } from "../changeJournal"
10+ import * as changeJournal from "../changeJournal"
11+ import { appendChange , journalPath } from "../changeJournal"
1112import { restoreLatestFile , rollbackFile , rollbackStep } from "../rollback"
1213
1314vi . mock ( "../index" , ( ) => ( {
@@ -98,6 +99,48 @@ describe("rollbackFile (B3c: undo the step's write to the file)", () => {
9899 expect ( service . restoreFile ) . toHaveBeenCalledWith ( "sha-1" , "src/a.ts" )
99100 } )
100101
102+ it ( "rejects rolling back a step that is not the file's latest change" , async ( ) => {
103+ // The file was written again by a later step (sha-2): rolling back the
104+ // older step (sha-1) would overwrite the newer state, so it is
105+ // rejected instead of silently destroying it.
106+ await seedJournal ( [
107+ { path : "src/a.ts" , operation : "create" , checkpointId : "sha-1" } ,
108+ { path : "src/a.ts" , operation : "update" , checkpointId : "sha-2" } ,
109+ ] )
110+ const service = serviceWith ( "base-0" )
111+ mockedGetCheckpointService . mockResolvedValue ( service )
112+
113+ const outcome = await rollbackFile ( makeTask ( ) , "sha-1" , "src/a.ts" )
114+
115+ expect ( outcome ) . toEqual ( {
116+ filePath : "src/a.ts" ,
117+ success : false ,
118+ error : "File was modified in a later step; roll back the latest change card first" ,
119+ } )
120+ expect ( service . restoreFile ) . not . toHaveBeenCalled ( )
121+ } )
122+
123+ it ( "fails when the journal location is unavailable (no global storage)" , async ( ) => {
124+ // No context on the provider double → the journal cannot even be
125+ // located: a clear failure, not a silent miss on the file lookup.
126+ const task = {
127+ taskId : "task-rollback" ,
128+ providerRef : { deref : vi . fn ( ) . mockReturnValue ( undefined ) } ,
129+ } as unknown as Task
130+
131+ const service = serviceWith ( "base-0" )
132+ mockedGetCheckpointService . mockResolvedValue ( service )
133+
134+ const outcome = await rollbackFile ( task , "sha-1" , "src/a.ts" )
135+
136+ expect ( outcome ) . toEqual ( {
137+ filePath : "src/a.ts" ,
138+ success : false ,
139+ error : "Change journal is unavailable for this task" ,
140+ } )
141+ expect ( service . restoreFile ) . not . toHaveBeenCalled ( )
142+ } )
143+
101144 it ( "fails cleanly when the file is not part of the given step checkpoint" , async ( ) => {
102145 await seedJournal ( [ { path : "src/a.ts" , operation : "create" , checkpointId : "sha-1" } ] )
103146 const service = serviceWith ( "base-0" )
@@ -231,6 +274,28 @@ describe("rollbackStep (B3c: undo every file of the step)", () => {
231274 expect ( service . restoreFile ) . toHaveBeenCalledTimes ( 1 )
232275 } )
233276
277+ it ( "rejects the stale file of a step while restoring the others" , async ( ) => {
278+ // src/a.ts was written again after this step's checkpoint, so its
279+ // sha-2 entry is no longer the file's latest: only src/b.ts (whose
280+ // latest entry IS sha-2) is restored.
281+ await seedJournal ( [
282+ { path : "src/a.ts" , operation : "create" , checkpointId : "sha-1" } ,
283+ { path : "src/a.ts" , operation : "update" , checkpointId : "sha-2" } ,
284+ { path : "src/b.ts" , operation : "create" , checkpointId : "sha-2" } ,
285+ { path : "src/a.ts" , operation : "update" , checkpointId : "sha-3" } ,
286+ ] )
287+ const service = serviceWith ( "base-0" )
288+ mockedGetCheckpointService . mockResolvedValue ( service )
289+
290+ const outcome = await rollbackStep ( makeTask ( ) , [ "src/a.ts" , "src/b.ts" ] , "sha-2" )
291+
292+ expect ( outcome . files [ 0 ] . success ) . toBe ( false )
293+ expect ( outcome . files [ 0 ] . error ) . toBe ( "File was modified in a later step; roll back the latest change card first" )
294+ expect ( outcome . files [ 1 ] ) . toEqual ( { filePath : "src/b.ts" , success : true } )
295+ expect ( service . restoreFile ) . toHaveBeenCalledTimes ( 1 )
296+ expect ( service . restoreFile ) . toHaveBeenCalledWith ( "base-0" , "src/b.ts" )
297+ } )
298+
234299 it ( "falls back to the latest journal entry per file without a step checkpoint id" , async ( ) => {
235300 await seedJournal ( [
236301 { path : "src/a.ts" , operation : "create" , checkpointId : "sha-1" } ,
@@ -258,8 +323,10 @@ describe("rollbackStep (B3c: undo every file of the step)", () => {
258323 expect ( outcome . files [ 1 ] . error ) . toBe ( "No change journal entry for this file" )
259324 } )
260325
261- it ( "treats a missing global storage directory as an empty journal" , async ( ) => {
262- // No context on the provider double → no journal location to read.
326+ it ( "fails per file when the journal location is unavailable (no global storage)" , async ( ) => {
327+ // No context on the provider double → the journal cannot even be
328+ // located. That is a failure, not an empty journal: reporting the
329+ // step as merely "not part of this checkpoint" would be misleading.
263330 const task = {
264331 taskId : "task-rollback" ,
265332 providerRef : { deref : vi . fn ( ) . mockReturnValue ( undefined ) } ,
@@ -272,7 +339,7 @@ describe("rollbackStep (B3c: undo every file of the step)", () => {
272339
273340 expect ( outcome . checkpointId ) . toBe ( "sha-2" )
274341 expect ( outcome . files [ 0 ] . success ) . toBe ( false )
275- expect ( outcome . files [ 0 ] . error ) . toBe ( "File is not part of this step's checkpoint " )
342+ expect ( outcome . files [ 0 ] . error ) . toBe ( "Change journal is unavailable for this task " )
276343 expect ( service . restoreFile ) . not . toHaveBeenCalled ( )
277344 } )
278345
@@ -315,6 +382,53 @@ describe("restoreLatestFile (B3c: forward direction)", () => {
315382 expect ( service . restoreFile ) . not . toHaveBeenCalled ( )
316383 } )
317384
385+ it ( "fails when the journal location is unavailable (no global storage)" , async ( ) => {
386+ // An unavailable journal is not "the task wrote nothing": a no-op
387+ // success would claim a restore that never happened.
388+ const task = {
389+ taskId : "task-rollback" ,
390+ providerRef : { deref : vi . fn ( ) . mockReturnValue ( undefined ) } ,
391+ } as unknown as Task
392+
393+ const service = serviceWith ( "base-0" )
394+ mockedGetCheckpointService . mockResolvedValue ( service )
395+
396+ const outcome = await restoreLatestFile ( task , "src/a.ts" )
397+
398+ expect ( outcome ) . toEqual ( {
399+ filePath : "src/a.ts" ,
400+ success : false ,
401+ error : "Change journal is unavailable for this task" ,
402+ } )
403+ expect ( service . restoreFile ) . not . toHaveBeenCalled ( )
404+ } )
405+
406+ it ( "fails when the journal cannot be read (an I/O error is not an empty journal)" , async ( ) => {
407+ // A directory at the journal path makes readFile fail with EISDIR —
408+ // a stand-in for any permission or I/O failure (EACCES etc.).
409+ await fs . mkdir ( journalPath ( globalStorageDir , "task-rollback" ) , { recursive : true } )
410+ const service = serviceWith ( "base-0" )
411+ mockedGetCheckpointService . mockResolvedValue ( service )
412+
413+ const outcome = await restoreLatestFile ( makeTask ( ) , "src/a.ts" )
414+
415+ expect ( outcome . success ) . toBe ( false )
416+ expect ( outcome . error ) . toContain ( "Change journal could not be read" )
417+ expect ( service . restoreFile ) . not . toHaveBeenCalled ( )
418+ } )
419+
420+ it ( "stringifies a non-Error journal read failure into the outcome" , async ( ) => {
421+ vi . spyOn ( changeJournal , "loadChanges" ) . mockRejectedValueOnce ( "raw journal failure" )
422+ const service = serviceWith ( "base-0" )
423+ mockedGetCheckpointService . mockResolvedValue ( service )
424+
425+ const outcome = await restoreLatestFile ( makeTask ( ) , "src/a.ts" )
426+
427+ expect ( outcome . success ) . toBe ( false )
428+ expect ( outcome . error ) . toBe ( "Change journal could not be read: raw journal failure" )
429+ expect ( service . restoreFile ) . not . toHaveBeenCalled ( )
430+ } )
431+
318432 it ( "fails cleanly when checkpoints are not enabled" , async ( ) => {
319433 mockedGetCheckpointService . mockResolvedValue ( undefined )
320434
0 commit comments