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