Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

CheckpointDiffQuery.test.ts

186 lines5.7 KB

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 latestTurnId: TurnId.makeUnsafe("turn-1"),
49 createdAt: "2026-01-01T00:00:00.000Z",
50 updatedAt: "2026-01-01T00:00:00.000Z",
51 deletedAt: null,
52 messages: [],
53 activities: [],
54 checkpoints: [
55 {
56 turnId: TurnId.makeUnsafe("turn-1"),
57 checkpointTurnCount: input.checkpointTurnCount,
58 checkpointRef: input.checkpointRef,
59 status: "ready",
60 files: [],
61 assistantMessageId: null,
62 completedAt: "2026-01-01T00:00:00.000Z",
63 },
64 ],
65 session: null,
66 },
67 ],
68 };
70​
71describe("CheckpointDiffQueryLive", () => {
72 it("computes diffs using canonical turn-0 checkpoint refs", async () => {
73 const projectId = ProjectId.makeUnsafe("project-1");
74 const threadId = ThreadId.makeUnsafe("thread-1");
75 const toCheckpointRef = checkpointRefForThreadTurn(threadId, 1);
76 const hasCheckpointRefCalls: Array<CheckpointRef> = [];
77 const diffCheckpointsCalls: Array<{
78 readonly fromCheckpointRef: CheckpointRef;
79 readonly toCheckpointRef: CheckpointRef;
80 readonly cwd: string;
81 }> = [];
82​
83 const snapshot = makeSnapshot({
84 projectId,
85 threadId,
86 workspaceRoot: "/tmp/workspace",
87 worktreePath: null,
88 checkpointTurnCount: 1,
89 checkpointRef: toCheckpointRef,
90 });
91​
92 const checkpointStore: CheckpointStoreShape = {
93 isGitRepository: () => Effect.succeed(true),
94 captureCheckpoint: () => Effect.void,
95 hasCheckpointRef: ({ checkpointRef }) =>
96 Effect.sync(() => {
97 hasCheckpointRefCalls.push(checkpointRef);
98 return true;
99 }),
100 restoreCheckpoint: () => Effect.succeed(true),
101 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd }) =>
102 Effect.sync(() => {
103 diffCheckpointsCalls.push({ fromCheckpointRef, toCheckpointRef, cwd });
104 return "diff patch";
105 }),
106 deleteCheckpointRefs: () => Effect.void,
107 };
108​
109 const layer = CheckpointDiffQueryLive.pipe(
110 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
111 Layer.provideMerge(
112 Layer.succeed(ProjectionSnapshotQuery, {
113 getSnapshot: () => Effect.succeed(snapshot),
114 }),
115 ),
116 );
117​
118 const result = await Effect.runPromise(
119 Effect.gen(function* () {
120 const query = yield* CheckpointDiffQuery;
121 return yield* query.getTurnDiff({
122 threadId,
123 fromTurnCount: 0,
124 toTurnCount: 1,
125 });
126 }).pipe(Effect.provide(layer)),
127 );
128​
129 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
130 expect(hasCheckpointRefCalls).toEqual([expectedFromRef, toCheckpointRef]);
131 expect(diffCheckpointsCalls).toEqual([
132 {
133 cwd: "/tmp/workspace",
134 fromCheckpointRef: expectedFromRef,
135 toCheckpointRef,
136 },
137 ]);
138 expect(result).toEqual({
139 threadId,
140 fromTurnCount: 0,
141 toTurnCount: 1,
142 diff: "diff patch",
143 });
144 });
145​
146 it("fails when the thread is missing from the snapshot", async () => {
147 const threadId = ThreadId.makeUnsafe("thread-missing");
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: () => Effect.succeed(""),
155 deleteCheckpointRefs: () => Effect.void,
156 };
157​
158 const layer = CheckpointDiffQueryLive.pipe(
159 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
160 Layer.provideMerge(
161 Layer.succeed(ProjectionSnapshotQuery, {
162 getSnapshot: () =>
163 Effect.succeed({
164 snapshotSequence: 0,
165 projects: [],
166 threads: [],
167 updatedAt: "2026-01-01T00:00:00.000Z",
168 } satisfies OrchestrationReadModel),
169 }),
170 ),
171 );
172​
173 await expect(
174 Effect.runPromise(
175 Effect.gen(function* () {
176 const query = yield* CheckpointDiffQuery;
177 return yield* query.getTurnDiff({
178 threadId,
179 fromTurnCount: 0,
180 toTurnCount: 1,
181 });
182 }).pipe(Effect.provide(layer)),
183 ),
184 ).rejects.toThrow("Thread 'thread-missing' not found.");
185 });
186});