Open History to see the last commit
1import { CheckpointRef, ProjectId, ThreadId, TurnId } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import { describe, expect, it } from "vite-plus/test";
6
7import {
8 ProjectionSnapshotQuery,
9 type ProjectionThreadCheckpointContext,
10} from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
11import { checkpointRefForThreadTurn } from "../Utils.ts";
12import { CheckpointDiffQueryLive } from "./CheckpointDiffQuery.ts";
13import { CheckpointStore, type CheckpointStoreShape } from "../Services/CheckpointStore.ts";
14import { CheckpointDiffQuery } from "../Services/CheckpointDiffQuery.ts";
15
16function makeThreadCheckpointContext(input: {
17 readonly projectId: ProjectId;
18 readonly threadId: ThreadId;
19 readonly workspaceRoot: string;
20 readonly worktreePath: string | null;
21 readonly checkpointTurnCount: number;
22 readonly checkpointRef: CheckpointRef;
23}): ProjectionThreadCheckpointContext {
24 return {
25 threadId: input.threadId,
26 projectId: input.projectId,
27 workspaceRoot: input.workspaceRoot,
28 worktreePath: input.worktreePath,
29 checkpoints: [
30 {
31 turnId: TurnId.make("turn-1"),
32 checkpointTurnCount: input.checkpointTurnCount,
33 checkpointRef: input.checkpointRef,
34 status: "ready",
35 files: [],
36 assistantMessageId: null,
37 completedAt: "2026-01-01T00:00:00.000Z",
38 },
39 ],
40 };
41}
42
43describe("CheckpointDiffQueryLive", () => {
44 it("uses the narrow full-thread context lookup for all-turns diffs", async () => {
45 const projectId = ProjectId.make("project-full-thread");
46 const threadId = ThreadId.make("thread-full-thread");
47 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 4);
48 let getThreadCheckpointContextCalls = 0;
49 let getFullThreadDiffContextCalls = 0;
50 const diffCheckpointsCalls: Array<{
51 readonly fromCheckpointRef: CheckpointRef;
52 readonly toCheckpointRef: CheckpointRef;
53 readonly cwd: string;
54 readonly ignoreWhitespace: boolean;
55 }> = [];
56
57 const checkpointStore: CheckpointStoreShape = {
58 isGitRepository: () => Effect.succeed(true),
59 captureCheckpoint: () => Effect.void,
60 hasCheckpointRef: () => Effect.succeed(true),
61 restoreCheckpoint: () => Effect.succeed(true),
62 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
63 Effect.sync(() => {
64 diffCheckpointsCalls.push({
65 fromCheckpointRef,
66 toCheckpointRef,
67 cwd,
68 ignoreWhitespace,
69 });
70 return "full thread diff patch";
71 }),
72 deleteCheckpointRefs: () => Effect.void,
73 };
74
75 const layer = CheckpointDiffQueryLive.pipe(
76 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
77 Layer.provideMerge(
78 Layer.succeed(ProjectionSnapshotQuery, {
79 getCommandReadModel: () =>
80 Effect.die("CheckpointDiffQuery should not request the command read model"),
81 getSnapshot: () =>
82 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
83 getShellSnapshot: () =>
84 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
85 getArchivedShellSnapshot: () =>
86 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
87 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
88 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
89 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
90 getProjectShellById: () => Effect.succeed(Option.none()),
91 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
92 getThreadCheckpointContext: () =>
93 Effect.sync(() => {
94 getThreadCheckpointContextCalls += 1;
95 return Option.none();
96 }),
97 getFullThreadDiffContext: () =>
98 Effect.sync(() => {
99 getFullThreadDiffContextCalls += 1;
100 return Option.some({
101 threadId,
102 projectId,
103 workspaceRoot: "/tmp/workspace",
104 worktreePath: "/tmp/worktree",
105 latestCheckpointTurnCount: 4,
106 toCheckpointRef,
107 });
108 }),
109 getThreadShellById: () => Effect.succeed(Option.none()),
110 getThreadDetailById: () => Effect.succeed(Option.none()),
111 }),
112 ),
113 );
114
115 const result = await Effect.runPromise(
116 Effect.gen(function* () {
117 const query = yield* CheckpointDiffQuery;
118 return yield* query.getFullThreadDiff({
119 threadId,
120 toTurnCount: 4,
121 ignoreWhitespace: true,
122 });
123 }).pipe(Effect.provide(layer)),
124 );
125
126 expect(getThreadCheckpointContextCalls).toBe(0);
127 expect(getFullThreadDiffContextCalls).toBe(1);
128 expect(diffCheckpointsCalls).toEqual([
129 {
130 cwd: "/tmp/worktree",
131 fromCheckpointRef: checkpointRefForThreadTurn(threadId, 0),
132 toCheckpointRef,
133 ignoreWhitespace: true,
134 },
135 ]);
136 expect(result).toEqual({
137 threadId,
138 fromTurnCount: 0,
139 toTurnCount: 4,
140 diff: "full thread diff patch",
141 });
142 });
143
144 it("computes diffs using canonical turn-0 checkpoint refs", async () => {
145 const projectId = ProjectId.make("project-1");
146 const threadId = ThreadId.make("thread-1");
147 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
148 const diffCheckpointsCalls: Array<{
149 readonly fromCheckpointRef: CheckpointRef;
150 readonly toCheckpointRef: CheckpointRef;
151 readonly cwd: string;
152 readonly ignoreWhitespace: boolean;
153 }> = [];
154
155 const threadCheckpointContext = makeThreadCheckpointContext({
156 projectId,
157 threadId,
158 workspaceRoot: "/tmp/workspace",
159 worktreePath: null,
160 checkpointTurnCount: 1,
161 checkpointRef: toCheckpointRef,
162 });
163
164 const checkpointStore: CheckpointStoreShape = {
165 isGitRepository: () => Effect.succeed(true),
166 captureCheckpoint: () => Effect.void,
167 hasCheckpointRef: () => Effect.succeed(true),
168 restoreCheckpoint: () => Effect.succeed(true),
169 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd, ignoreWhitespace }) =>
170 Effect.sync(() => {
171 diffCheckpointsCalls.push({
172 fromCheckpointRef,
173 toCheckpointRef,
174 cwd,
175 ignoreWhitespace,
176 });
177 return "diff patch";
178 }),
179 deleteCheckpointRefs: () => Effect.void,
180 };
181
182 const layer = CheckpointDiffQueryLive.pipe(
183 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
184 Layer.provideMerge(
185 Layer.succeed(ProjectionSnapshotQuery, {
186 getCommandReadModel: () =>
187 Effect.die("CheckpointDiffQuery should not request the command read model"),
188 getSnapshot: () =>
189 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
190 getShellSnapshot: () =>
191 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
192 getArchivedShellSnapshot: () =>
193 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
194 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
195 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
196 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
197 getProjectShellById: () => Effect.succeed(Option.none()),
198 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
199 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
200 getFullThreadDiffContext: () => Effect.die("unused"),
201 getThreadShellById: () => Effect.succeed(Option.none()),
202 getThreadDetailById: () => Effect.succeed(Option.none()),
203 }),
204 ),
205 );
206
207 const result = await Effect.runPromise(
208 Effect.gen(function* () {
209 const query = yield* CheckpointDiffQuery;
210 return yield* query.getTurnDiff({
211 threadId,
212 fromTurnCount: 0,
213 toTurnCount: 1,
214 ignoreWhitespace: true,
215 });
216 }).pipe(Effect.provide(layer)),
217 );
218
219 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
220 expect(diffCheckpointsCalls).toEqual([
221 {
222 cwd: "/tmp/workspace",
223 fromCheckpointRef: expectedFromRef,
224 toCheckpointRef,
225 ignoreWhitespace: true,
226 },
227 ]);
228 expect(result).toEqual({
229 threadId,
230 fromTurnCount: 0,
231 toTurnCount: 1,
232 diff: "diff patch",
233 });
234 });
235
236 it("defaults to hide whitespace changes", async () => {
237 const projectId = ProjectId.make("project-default-whitespace");
238 const threadId = ThreadId.make("thread-default-whitespace");
239 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
240 const diffCheckpointsCalls: Array<{ readonly ignoreWhitespace: boolean }> = [];
241
242 const threadCheckpointContext = makeThreadCheckpointContext({
243 projectId,
244 threadId,
245 workspaceRoot: "/tmp/workspace",
246 worktreePath: null,
247 checkpointTurnCount: 1,
248 checkpointRef: toCheckpointRef,
249 });
250
251 const checkpointStore: CheckpointStoreShape = {
252 isGitRepository: () => Effect.succeed(true),
253 captureCheckpoint: () => Effect.void,
254 hasCheckpointRef: () => Effect.succeed(true),
255 restoreCheckpoint: () => Effect.succeed(true),
256 diffCheckpoints: ({ ignoreWhitespace }) =>
257 Effect.sync(() => {
258 diffCheckpointsCalls.push({ ignoreWhitespace });
259 return "diff patch";
260 }),
261 deleteCheckpointRefs: () => Effect.void,
262 };
263
264 const layer = CheckpointDiffQueryLive.pipe(
265 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
266 Layer.provideMerge(
267 Layer.succeed(ProjectionSnapshotQuery, {
268 getCommandReadModel: () =>
269 Effect.die("CheckpointDiffQuery should not request the command read model"),
270 getSnapshot: () =>
271 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
272 getShellSnapshot: () =>
273 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
274 getArchivedShellSnapshot: () =>
275 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
276 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
277 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
278 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
279 getProjectShellById: () => Effect.succeed(Option.none()),
280 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
281 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
282 getFullThreadDiffContext: () => Effect.die("unused"),
283 getThreadShellById: () => Effect.succeed(Option.none()),
284 getThreadDetailById: () => Effect.succeed(Option.none()),
285 }),
286 ),
287 );
288
289 await Effect.runPromise(
290 Effect.gen(function* () {
291 const query = yield* CheckpointDiffQuery;
292 return yield* query.getTurnDiff({
293 threadId,
294 fromTurnCount: 0,
295 toTurnCount: 1,
296 });
297 }).pipe(Effect.provide(layer)),
298 );
299
300 expect(diffCheckpointsCalls).toEqual([{ ignoreWhitespace: true }]);
301 });
302
303 it("does not preflight checkpoint refs before diffing", async () => {
304 const projectId = ProjectId.make("project-no-preflight");
305 const threadId = ThreadId.make("thread-no-preflight");
306 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
307 let hasCheckpointRefCallCount = 0;
308
309 const threadCheckpointContext = makeThreadCheckpointContext({
310 projectId,
311 threadId,
312 workspaceRoot: "/tmp/workspace",
313 worktreePath: null,
314 checkpointTurnCount: 1,
315 checkpointRef: toCheckpointRef,
316 });
317
318 const checkpointStore: CheckpointStoreShape = {
319 isGitRepository: () => Effect.succeed(true),
320 captureCheckpoint: () => Effect.void,
321 hasCheckpointRef: () =>
322 Effect.sync(() => {
323 hasCheckpointRefCallCount += 1;
324 return true;
325 }),
326 restoreCheckpoint: () => Effect.succeed(true),
327 diffCheckpoints: () => Effect.succeed("diff patch"),
328 deleteCheckpointRefs: () => Effect.void,
329 };
330
331 const layer = CheckpointDiffQueryLive.pipe(
332 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
333 Layer.provideMerge(
334 Layer.succeed(ProjectionSnapshotQuery, {
335 getCommandReadModel: () =>
336 Effect.die("CheckpointDiffQuery should not request the command read model"),
337 getSnapshot: () =>
338 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
339 getShellSnapshot: () =>
340 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
341 getArchivedShellSnapshot: () =>
342 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
343 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
344 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
345 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
346 getProjectShellById: () => Effect.succeed(Option.none()),
347 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
348 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
349 getFullThreadDiffContext: () => Effect.die("unused"),
350 getThreadShellById: () => Effect.succeed(Option.none()),
351 getThreadDetailById: () => Effect.succeed(Option.none()),
352 }),
353 ),
354 );
355
356 await Effect.runPromise(
357 Effect.gen(function* () {
358 const query = yield* CheckpointDiffQuery;
359 return yield* query.getTurnDiff({
360 threadId,
361 fromTurnCount: 0,
362 toTurnCount: 1,
363 ignoreWhitespace: true,
364 });
365 }).pipe(Effect.provide(layer)),
366 );
367
368 expect(hasCheckpointRefCallCount).toBe(0);
369 });
370
371 it("fails when the thread is missing from the snapshot", async () => {
372 const threadId = ThreadId.make("thread-missing");
373
374 const checkpointStore: CheckpointStoreShape = {
375 isGitRepository: () => Effect.succeed(true),
376 captureCheckpoint: () => Effect.void,
377 hasCheckpointRef: () => Effect.succeed(true),
378 restoreCheckpoint: () => Effect.succeed(true),
379 diffCheckpoints: () => Effect.succeed(""),
380 deleteCheckpointRefs: () => Effect.void,
381 };
382
383 const layer = CheckpointDiffQueryLive.pipe(
384 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
385 Layer.provideMerge(
386 Layer.succeed(ProjectionSnapshotQuery, {
387 getCommandReadModel: () =>
388 Effect.die("CheckpointDiffQuery should not request the command read model"),
389 getSnapshot: () =>
390 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
391 getShellSnapshot: () =>
392 Effect.die("CheckpointDiffQuery should not request the orchestration shell snapshot"),
393 getArchivedShellSnapshot: () =>
394 Effect.die("CheckpointDiffQuery should not request archived shell snapshots"),
395 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 0 }),
396 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
397 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
398 getProjectShellById: () => Effect.succeed(Option.none()),
399 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
400 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
401 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
402 getThreadShellById: () => Effect.succeed(Option.none()),
403 getThreadDetailById: () => Effect.succeed(Option.none()),
404 }),
405 ),
406 );
407
408 await expect(
409 Effect.runPromise(
410 Effect.gen(function* () {
411 const query = yield* CheckpointDiffQuery;
412 return yield* query.getTurnDiff({
413 threadId,
414 fromTurnCount: 0,
415 toTurnCount: 1,
416 });
417 }).pipe(Effect.provide(layer)),
418 ),
419 ).rejects.toThrow("Thread 'thread-missing' not found.");
420 });
421});