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