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 readonly ignoreWhitespace: boolean;
52 }> = [];
53
54 const threadCheckpointContext = makeThreadCheckpointContext({
55 projectId,
56 threadId,
57 workspaceRoot: "/tmp/workspace",
58 worktreePath: null,
59 checkpointTurnCount: 1,
60 checkpointRef: toCheckpointRef,
61 });
62
63 const checkpointStore: CheckpointStoreShape = {
64 isGitRepository: () => Effect.succeed(true),
65 captureCheckpoint: () => Effect.void,
66 hasCheckpointRef: ({ checkpointRef }) =>
67 Effect.sync(() => {
68 hasCheckpointRefCalls.push(checkpointRef);
69 return true;
70 }),
71 restoreCheckpoint: () => Effect.succeed(true),
72 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
73 Effect.sync(() => {
74 diffCheckpointsCalls.push({
75 fromCheckpointRef,
76 toCheckpointRef,
77 cwd,
78 ignoreWhitespace,
79 });
80 return "diff patch";
81 }),
82 deleteCheckpointRefs: () => Effect.void,
83 };
84
85 const layer = CheckpointDiffQueryLive.pipe(
86 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
87 Layer.provideMerge(
88 Layer.succeed(ProjectionSnapshotQuery, {
89 getCommandReadModel: () =>
90 Effect.die("CheckpointDiffQuery should not request the command read model"),
91 getSnapshot: () =>
92 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
93 getShellSnapshot: () =>
94 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
95 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
96 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
97 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
98 getProjectShellById: () => Effect.succeed(Option.none()),
99 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
100 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
101 getThreadShellById: () => Effect.succeed(Option.none()),
102 getThreadDetailById: () => Effect.succeed(Option.none()),
103 }),
104 ),
105 );
106
107 const result = await Effect.runPromise(
108 Effect.gen(function* () {
109 const query = yield* CheckpointDiffQuery;
110 return yield* query.getTurnDiff({
111 threadId,
112 fromTurnCount: 0,
113 toTurnCount: 1,
114 ignoreWhitespace: true,
115 });
116 }).pipe(Effect.provide(layer)),
117 );
118
119 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
120 expect(hasCheckpointRefCalls).toEqual([expectedFromRef, toCheckpointRef]);
121 expect(diffCheckpointsCalls).toEqual([
122 {
123 cwd: "/tmp/workspace",
124 fromCheckpointRef: expectedFromRef,
125 toCheckpointRef,
126 ignoreWhitespace: true,
127 },
128 ]);
129 expect(result).toEqual({
130 threadId,
131 fromTurnCount: 0,
132 toTurnCount: 1,
133 diff: "diff patch",
134 });
135 });
136
137 it("defaults to hide whitespace changes", async () => {
138 const projectId = ProjectId.make("project-default-whitespace");
139 const threadId = ThreadId.make("thread-default-whitespace");
140 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
141 const diffCheckpointsCalls: Array<{ readonly ignoreWhitespace: boolean }> = [];
142
143 const threadCheckpointContext = makeThreadCheckpointContext({
144 projectId,
145 threadId,
146 workspaceRoot: "/tmp/workspace",
147 worktreePath: null,
148 checkpointTurnCount: 1,
149 checkpointRef: toCheckpointRef,
150 });
151
152 const checkpointStore: CheckpointStoreShape = {
153 isGitRepository: () => Effect.succeed(true),
154 captureCheckpoint: () => Effect.void,
155 hasCheckpointRef: () => Effect.succeed(true),
156 restoreCheckpoint: () => Effect.succeed(true),
157 diffCheckpoints: ({ ignoreWhitespace }) =>
158 Effect.sync(() => {
159 diffCheckpointsCalls.push({ ignoreWhitespace });
160 return "diff patch";
161 }),
162 deleteCheckpointRefs: () => Effect.void,
163 };
164
165 const layer = CheckpointDiffQueryLive.pipe(
166 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
167 Layer.provideMerge(
168 Layer.succeed(ProjectionSnapshotQuery, {
169 getCommandReadModel: () =>
170 Effect.die("CheckpointDiffQuery should not request the command read model"),
171 getSnapshot: () =>
172 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
173 getShellSnapshot: () =>
174 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
175 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
176 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
177 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
178 getProjectShellById: () => Effect.succeed(Option.none()),
179 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
180 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
181 getThreadShellById: () => Effect.succeed(Option.none()),
182 getThreadDetailById: () => Effect.succeed(Option.none()),
183 }),
184 ),
185 );
186
187 await Effect.runPromise(
188 Effect.gen(function* () {
189 const query = yield* CheckpointDiffQuery;
190 return yield* query.getTurnDiff({
191 threadId,
192 fromTurnCount: 0,
193 toTurnCount: 1,
194 });
195 }).pipe(Effect.provide(layer)),
196 );
197
198 expect(diffCheckpointsCalls).toEqual([{ ignoreWhitespace: true }]);
199 });
200
201 it("fails when the thread is missing from the snapshot", async () => {
202 const threadId = ThreadId.make("thread-missing");
203
204 const checkpointStore: CheckpointStoreShape = {
205 isGitRepository: () => Effect.succeed(true),
206 captureCheckpoint: () => Effect.void,
207 hasCheckpointRef: () => Effect.succeed(true),
208 restoreCheckpoint: () => Effect.succeed(true),
209 diffCheckpoints: () => Effect.succeed(""),
210 deleteCheckpointRefs: () => Effect.void,
211 };
212
213 const layer = CheckpointDiffQueryLive.pipe(
214 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
215 Layer.provideMerge(
216 Layer.succeed(ProjectionSnapshotQuery, {
217 getCommandReadModel: () =>
218 Effect.die("CheckpointDiffQuery should not request the command read model"),
219 getSnapshot: () =>
220 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
221 getShellSnapshot: () =>
222 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
223 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
224 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
225 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
226 getProjectShellById: () => Effect.succeed(Option.none()),
227 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
228 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
229 getThreadShellById: () => Effect.succeed(Option.none()),
230 getThreadDetailById: () => Effect.succeed(Option.none()),
231 }),
232 ),
233 );
234
235 await expect(
236 Effect.runPromise(
237 Effect.gen(function* () {
238 const query = yield* CheckpointDiffQuery;
239 return yield* query.getTurnDiff({
240 threadId,
241 fromTurnCount: 0,
242 toTurnCount: 1,
243 });
244 }).pipe(Effect.provide(layer)),
245 ),
246 ).rejects.toThrow("Thread 'thread-missing' not found.");
247 });
248});