@@ -5,6 +5,7 @@ import path from "path"
55import { afterEach , beforeEach , describe , expect , it , vi } from "vitest"
66
77import type { Task } from "../../task/Task"
8+ import type { RepoPerTaskCheckpointService } from "../../../services/checkpoints"
89import type { ChangeJournalEntry } from "../changeJournal"
910import { getCheckpointService } from "../index"
1011import * as changeJournal from "../changeJournal"
@@ -18,21 +19,29 @@ vi.mock("../index", () => ({
1819 checkpointDiff : vi . fn ( ) ,
1920} ) )
2021
21- const mockedGetCheckpointService = getCheckpointService as unknown as ReturnType < typeof vi . fn >
22+ const mockedGetCheckpointService = vi . mocked ( getCheckpointService )
2223
23- function makeTask ( ) : Task {
24+ /**
25+ * A minimal Task double: the rollback code reads only taskId and
26+ * providerRef.deref(). The double assertion is confined to this factory
27+ * (AGENTS.md last resort - the real Task carries the full session surface)
28+ * so every call site stays typed and cast-free.
29+ */
30+ function makeTask ( provider : unknown = { context : { globalStorageUri : { fsPath : globalStorageDir } } } ) : Task {
2431 return {
2532 taskId : "task-rollback" ,
26- providerRef : {
27- deref : vi . fn ( ) . mockReturnValue ( { context : { globalStorageUri : { fsPath : globalStorageDir } } } ) ,
28- } ,
33+ providerRef : { deref : vi . fn ( ) . mockReturnValue ( provider ) } ,
2934 } as unknown as Task
3035}
3136
32- /** A checkpoint-service double with a recording restoreFile and a baseline. */
33- function serviceWith ( baseHash : string | undefined ) {
37+ /**
38+ * A checkpoint-service double with a recording restoreFile and a baseline.
39+ * The rollback code consumes only these two members; the double assertion
40+ * is confined to this factory so the vi.mocked handle stays typed.
41+ */
42+ function serviceWith ( baseHash : string | undefined ) : RepoPerTaskCheckpointService {
3443 const restoreFile = vi . fn ( ) . mockResolvedValue ( undefined )
35- return { baseHash, restoreFile }
44+ return { baseHash, restoreFile } as unknown as RepoPerTaskCheckpointService
3645}
3746
3847async function seedJournal ( entries : ChangeJournalEntry [ ] ) : Promise < void > {
@@ -123,10 +132,7 @@ describe("rollbackFile (B3c: undo the step's write to the file)", () => {
123132 it ( "fails when the journal location is unavailable (no global storage)" , async ( ) => {
124133 // No context on the provider double → the journal cannot even be
125134 // 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
135+ const task = makeTask ( null )
130136
131137 const service = serviceWith ( "base-0" )
132138 mockedGetCheckpointService . mockResolvedValue ( service )
@@ -186,7 +192,7 @@ describe("rollbackFile (B3c: undo the step's write to the file)", () => {
186192 it ( "reports the service error without throwing" , async ( ) => {
187193 await seedJournal ( [ { path : "src/a.ts" , operation : "create" , checkpointId : "sha-1" } ] )
188194 const service = serviceWith ( "base-0" )
189- service . restoreFile . mockRejectedValue ( new Error ( "pathspec did not match" ) )
195+ vi . mocked ( service . restoreFile ) . mockRejectedValue ( new Error ( "pathspec did not match" ) )
190196 mockedGetCheckpointService . mockResolvedValue ( service )
191197
192198 const outcome = await rollbackFile ( makeTask ( ) , "sha-1" , "src/a.ts" )
@@ -198,7 +204,7 @@ describe("rollbackFile (B3c: undo the step's write to the file)", () => {
198204 it ( "stringifies non-Error rejections into the outcome" , async ( ) => {
199205 await seedJournal ( [ { path : "src/a.ts" , operation : "create" , checkpointId : "sha-1" } ] )
200206 const service = serviceWith ( "base-0" )
201- service . restoreFile . mockRejectedValue ( "raw failure" )
207+ vi . mocked ( service . restoreFile ) . mockRejectedValue ( "raw failure" )
202208 mockedGetCheckpointService . mockResolvedValue ( service )
203209
204210 const outcome = await rollbackFile ( makeTask ( ) , "sha-1" , "src/a.ts" )
@@ -237,7 +243,7 @@ describe("rollbackStep (B3c: undo every file of the step)", () => {
237243 { path : "src/b.ts" , operation : "create" , checkpointId : "sha-2" } ,
238244 ] )
239245 const service = serviceWith ( "base-0" )
240- service . restoreFile . mockRejectedValueOnce ( new Error ( "pathspec did not match" ) )
246+ vi . mocked ( service . restoreFile ) . mockRejectedValueOnce ( new Error ( "pathspec did not match" ) )
241247 mockedGetCheckpointService . mockResolvedValue ( service )
242248
243249 const outcome = await rollbackStep ( makeTask ( ) , [ "src/a.ts" , "src/b.ts" ] , "sha-2" )
@@ -327,10 +333,7 @@ describe("rollbackStep (B3c: undo every file of the step)", () => {
327333 // No context on the provider double → the journal cannot even be
328334 // located. That is a failure, not an empty journal: reporting the
329335 // step as merely "not part of this checkpoint" would be misleading.
330- const task = {
331- taskId : "task-rollback" ,
332- providerRef : { deref : vi . fn ( ) . mockReturnValue ( undefined ) } ,
333- } as unknown as Task
336+ const task = makeTask ( null )
334337
335338 const service = serviceWith ( "base-0" )
336339 mockedGetCheckpointService . mockResolvedValue ( service )
@@ -385,10 +388,7 @@ describe("restoreLatestFile (B3c: forward direction)", () => {
385388 it ( "fails when the journal location is unavailable (no global storage)" , async ( ) => {
386389 // An unavailable journal is not "the task wrote nothing": a no-op
387390 // 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
391+ const task = makeTask ( null )
392392
393393 const service = serviceWith ( "base-0" )
394394 mockedGetCheckpointService . mockResolvedValue ( service )
@@ -476,7 +476,7 @@ describe("restoreLatestFile (B3c: forward direction)", () => {
476476 it ( "reports the service error without throwing" , async ( ) => {
477477 await seedJournal ( [ { path : "src/a.ts" , operation : "create" , checkpointId : "sha-1" } ] )
478478 const service = serviceWith ( "base-0" )
479- service . restoreFile . mockRejectedValue ( new Error ( "index.lock held" ) )
479+ vi . mocked ( service . restoreFile ) . mockRejectedValue ( new Error ( "index.lock held" ) )
480480 mockedGetCheckpointService . mockResolvedValue ( service )
481481
482482 const outcome = await restoreLatestFile ( makeTask ( ) , "src/a.ts" )
0 commit comments