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

CheckpointDiffQuery.test.ts

197 lines6.1 KB

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