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

CheckpointDiffQuery.test.ts

160 lines5.7 KB

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.makeUnsafe("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 };
40​
41describe("CheckpointDiffQueryLive", () => {
42 it("computes diffs using canonical turn-0 checkpoint refs", async () => {
43 const projectId = ProjectId.makeUnsafe("project-1");
44 const threadId = ThreadId.makeUnsafe("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 }> = [];
52​
53 const threadCheckpointContext = makeThreadCheckpointContext({
54 projectId,
55 threadId,
56 workspaceRoot: "/tmp/workspace",
57 worktreePath: null,
58 checkpointTurnCount: 1,
59 checkpointRef: toCheckpointRef,
60 });
61​
62 const checkpointStore: CheckpointStoreShape = {
63 isGitRepository: () => Effect.succeed(true),
64 captureCheckpoint: () => Effect.void,
65 hasCheckpointRef: ({ checkpointRef }) =>
66 Effect.sync(() => {
67 hasCheckpointRefCalls.push(checkpointRef);
68 return true;
69 }),
70 restoreCheckpoint: () => Effect.succeed(true),
71 diffCheckpoints: ({ fromCheckpointRef, toCheckpointRef, cwd }) =>
72 Effect.sync(() => {
73 diffCheckpointsCalls.push({ fromCheckpointRef, toCheckpointRef, cwd });
74 return "diff patch";
75 }),
76 deleteCheckpointRefs: () => Effect.void,
77 };
78​
79 const layer = CheckpointDiffQueryLive.pipe(
80 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
81 Layer.provideMerge(
82 Layer.succeed(ProjectionSnapshotQuery, {
83 getSnapshot: () =>
84 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
85 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
86 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
87 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
88 getThreadCheckpointContext: () => Effect.succeed(Option.some(threadCheckpointContext)),
89 }),
90 ),
91 );
92​
93 const result = await Effect.runPromise(
94 Effect.gen(function* () {
95 const query = yield* CheckpointDiffQuery;
96 return yield* query.getTurnDiff({
97 threadId,
98 fromTurnCount: 0,
99 toTurnCount: 1,
100 });
101 }).pipe(Effect.provide(layer)),
102 );
103​
104 const expectedFromRef = checkpointRefForThreadTurn(threadId, 0);
105 expect(hasCheckpointRefCalls).toEqual([expectedFromRef, toCheckpointRef]);
106 expect(diffCheckpointsCalls).toEqual([
107 {
108 cwd: "/tmp/workspace",
109 fromCheckpointRef: expectedFromRef,
110 toCheckpointRef,
111 },
112 ]);
113 expect(result).toEqual({
114 threadId,
115 fromTurnCount: 0,
116 toTurnCount: 1,
117 diff: "diff patch",
118 });
119 });
120​
121 it("fails when the thread is missing from the snapshot", async () => {
122 const threadId = ThreadId.makeUnsafe("thread-missing");
123​
124 const checkpointStore: CheckpointStoreShape = {
125 isGitRepository: () => Effect.succeed(true),
126 captureCheckpoint: () => Effect.void,
127 hasCheckpointRef: () => Effect.succeed(true),
128 restoreCheckpoint: () => Effect.succeed(true),
129 diffCheckpoints: () => Effect.succeed(""),
130 deleteCheckpointRefs: () => Effect.void,
131 };
132​
133 const layer = CheckpointDiffQueryLive.pipe(
134 Layer.provideMerge(Layer.succeed(CheckpointStore, checkpointStore)),
135 Layer.provideMerge(
136 Layer.succeed(ProjectionSnapshotQuery, {
137 getSnapshot: () =>
138 Effect.die("CheckpointDiffQuery should not request the full orchestration snapshot"),
139 getCounts: () => Effect.succeed({ projectCount: 0, threadCount: 0 }),
140 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
141 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
142 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
143 }),
144 ),
145 );
146​
147 await expect(
148 Effect.runPromise(
149 Effect.gen(function* () {
150 const query = yield* CheckpointDiffQuery;
151 return yield* query.getTurnDiff({
152 threadId,
153 fromTurnCount: 0,
154 toTurnCount: 1,
155 });
156 }).pipe(Effect.provide(layer)),
157 ),
158 ).rejects.toThrow("Thread 'thread-missing' not found.");
159 });
160});