Open History to see the last commit
1import { CheckpointRef, ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
2import { Effect, Layer, Option } from "effect";
3import { describe, expect, it } from "vitest";
4
5import {
6 ProjectionSnapshotQuery,
7 type ProjectionThreadCheckpointContext,
8} from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
9import { checkpointRefForThreadTurn } from "../Utils.ts";
10import { CheckpointDiffQueryLive } from "./CheckpointDiffQuery.ts";
11import { CheckpointStore, type CheckpointStoreShape } from "../Services/CheckpointStore.ts";
12import { CheckpointDiffQuery } from "../Services/CheckpointDiffQuery.ts";
13
14function makeThreadCheckpointContext(input: {
15 readonly projectId: ProjectId;
16 readonly threadId: ThreadId;
17 readonly workspaceRoot: string;
18 readonly worktreePath: string | null;
19 readonly checkpointTurnCount: number;
20 readonly checkpointRef: CheckpointRef;
21}): ProjectionThreadCheckpointContext {
22 return {
23 threadId: input.threadId,
24 projectId: input.projectId,
25 workspaceRoot: input.workspaceRoot,
26 worktreePath: input.worktreePath,
27 checkpoints: [
28 {
29 turnId: TurnId.make("turn-1"),
30 checkpointTurnCount: input.checkpointTurnCount,
31 checkpointRef: input.checkpointRef,
32 status: "ready",
33 files: [],
34 assistantMessageId: null,
35 completedAt: "2026-01-01T00:00:00.000Z",
36 },
37 ],
38 };
39}
40
41describe("CheckpointDiffQueryLive", () => {
42 it("computes diffs using canonical turn-0 checkpoint refs", async () => {
43 const projectId = ProjectId.make("project-1");
44 const threadId = ThreadId.make("thread-1");
45 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
46 const hasCheckpointRefCalls: Array<CheckpointRef> = [];
47 const diffCheckpointsCalls: Array<{
48 readonly fromCheckpointRef: CheckpointRef;
49 readonly toCheckpointRef: CheckpointRef;
50 readonly cwd: string;
51 }> = [];
52
53 const threadCheckpointContext = makeThreadCheckpointContext({
54 projectId,
55 threadId,
56 workspaceRoot: "/tmp/workspace",
57 worktreePath: null,
58 checkpointTurnCount: 1,
59 checkpointRef: toCheckpointRef,
60 });
61
62 const checkpointStore: CheckpointStoreShape = {
63 isGitRepository: () => Effect.succeed(true),
64 captureCheckpoint: () => Effect.void,
65 hasCheckpointRef: ({ checkpointRef }) =>
66 Effect.sync(() => {
67 hasCheckpointRefCalls.push(checkpointRef);
68 return true;
69 }),
70 restoreCheckpoint: () => Effect.succeed(true),
71 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd }) =>
72 Effect.sync(() => {
73 diffCheckpointsCalls.push({ fromCheckpointRef, toCheckpointRef, cwd });
74 return "diff patch";
75 }),
76 deleteCheckpointRefs: () => Effect.void,
77 };
78
79 const layer = CheckpointDiffQueryLive.pipe(
80 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
81 Layer.provideMerge(
82 Layer.succeed(ProjectionSnapshotQuery, {
83 getSnapshot: () =>
84 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
85 getShellSnapshot: () =>
86 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
87 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
88 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
89 getProjectShellById: () => Effect.succeed(Option.none()),
90 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
91 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
92 getThreadShellById: () => Effect.succeed(Option.none()),
93 getThreadDetailById: () => Effect.succeed(Option.none()),
94 }),
95 ),
96 );
97
98 const result = await Effect.runPromise(
99 Effect.gen(function* () {
100 const query = yield* CheckpointDiffQuery;
101 return yield* query.getTurnDiff({
102 threadId,
103 fromTurnCount: 0,
104 toTurnCount: 1,
105 });
106 }).pipe(Effect.provide(layer)),
107 );
108
109 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
110 expect(hasCheckpointRefCalls).toEqual([expectedFromRef, toCheckpointRef]);
111 expect(diffCheckpointsCalls).toEqual([
112 {
113 cwd: "/tmp/workspace",
114 fromCheckpointRef: expectedFromRef,
115 toCheckpointRef,
116 },
117 ]);
118 expect(result).toEqual({
119 threadId,
120 fromTurnCount: 0,
121 toTurnCount: 1,
122 diff: "diff patch",
123 });
124 });
125
126 it("fails when the thread is missing from the snapshot", async () => {
127 const threadId = ThreadId.make("thread-missing");
128
129 const checkpointStore: CheckpointStoreShape = {
130 isGitRepository: () => Effect.succeed(true),
131 captureCheckpoint: () => Effect.void,
132 hasCheckpointRef: () => Effect.succeed(true),
133 restoreCheckpoint: () => Effect.succeed(true),
134 diffCheckpoints: () => Effect.succeed(""),
135 deleteCheckpointRefs: () => Effect.void,
136 };
137
138 const layer = CheckpointDiffQueryLive.pipe(
139 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
140 Layer.provideMerge(
141 Layer.succeed(ProjectionSnapshotQuery, {
142 getSnapshot: () =>
143 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
144 getShellSnapshot: () =>
145 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
146 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
147 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
148 getProjectShellById: () => Effect.succeed(Option.none()),
149 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
150 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
151 getThreadShellById: () => Effect.succeed(Option.none()),
152 getThreadDetailById: () => Effect.succeed(Option.none()),
153 }),
154 ),
155 );
156
157 await expect(
158 Effect.runPromise(
159 Effect.gen(function* () {
160 const query = yield* CheckpointDiffQuery;
161 return yield* query.getTurnDiff({
162 threadId,
163 fromTurnCount: 0,
164 toTurnCount: 1,
165 });
166 }).pipe(Effect.provide(layer)),
167 ),
168 ).rejects.toThrow("Thread 'thread-missing' not found.");
169 });
170});