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

CheckpointDiffQuery.test.ts

158 lines5.3 KB

1import { assert, it, vi } from "@effect/vitest";
2import {
3 CheckpointRef,
4 CheckpointScopeId,
5 RunId,
6 ThreadId,
7 type OrchestrationV2ThreadProjection,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11​
12import { checkpointRefForScopeOrdinal } from "../orchestration-v2/CheckpointService.ts";
13import { OrchestratorProjectionError } from "../orchestration-v2/Orchestrator.ts";
14import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts";
15import * as CheckpointDiffQuery from "./CheckpointDiffQuery.ts";
16import * as CheckpointStore from "./CheckpointStore.ts";
17import {
18 CheckpointRefUnavailableError,
19 CheckpointThreadNotFoundError,
20 CheckpointTurnRangeUnavailableError,
21} from "./Errors.ts";
22​
23const threadId = ThreadId.make("thread:checkpoint-diff-v2");
24const firstRunId = RunId.make("run:checkpoint-diff-v2:1");
25const secondRunId = RunId.make("run:checkpoint-diff-v2:2");
26const firstScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:1");
27const secondScopeId = CheckpointScopeId.make("scope:checkpoint-diff-v2:2");
28const secondRef = CheckpointRef.make("refs/t3/test/second");
29​
30function makeProjection(): OrchestrationV2ThreadProjection {
31 return {
32 runs: [
33 { id: firstRunId, ordinal: 1 },
34 { id: secondRunId, ordinal: 2 },
35 ],
36 checkpointScopes: [
37 { id: firstScopeId, runId: firstRunId, kind: "root_run", cwd: "/repo" },
38 { id: secondScopeId, runId: secondRunId, kind: "root_run", cwd: "/repo" },
39 ],
40 checkpoints: [
41 {
42 scopeId: secondScopeId,
43 appRunOrdinal: 2,
44 status: "ready",
45 ref: secondRef,
46 },
47 ],
48 } as unknown as OrchestrationV2ThreadProjection;
50​
51function makeLayer(input: {
52 readonly projection: Effect.Effect<OrchestrationV2ThreadProjection, OrchestratorProjectionError>;
53 readonly diffCheckpoints?: CheckpointStore.CheckpointStore["Service"]["diffCheckpoints"];
54}) {
55 return CheckpointDiffQuery.layer.pipe(
56 Layer.provide(
57 Layer.mergeAll(
58 Layer.mock(ThreadManagement.ThreadManagementService)({
59 getThreadProjection: () => input.projection,
60 }),
61 Layer.mock(CheckpointStore.CheckpointStore)({
62 diffCheckpoints: input.diffCheckpoints ?? (() => Effect.succeed("diff")),
63 }),
64 ),
65 ),
66 );
68​
69it.effect("computes V2 run diffs from projected checkpoint scopes", () => {
70 const diffCheckpoints = vi.fn((_input: CheckpointStore.DiffCheckpointsInput) =>
71 Effect.succeed("diff --git a/file b/file"),
72 );
73 const layer = makeLayer({ projection: Effect.succeed(makeProjection()), diffCheckpoints });
74​
75 return Effect.gen(function* () {
76 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
77 const result = yield* query.getFullThreadDiff({ threadId, toTurnCount: 2 });
78​
79 assert.deepEqual(result, {
80 threadId,
81 fromTurnCount: 0,
82 toTurnCount: 2,
83 diff: "diff --git a/file b/file",
84 });
85 assert.deepEqual(diffCheckpoints.mock.calls[0]?.[0], {
86 cwd: "/repo",
87 fromCheckpointRef: checkpointRefForScopeOrdinal({
88 scopeId: firstScopeId,
89 ordinalWithinScope: 0,
90 }),
91 toCheckpointRef: secondRef,
92 fallbackFromToHead: false,
93 ignoreWhitespace: true,
94 });
95 }).pipe(Effect.provide(layer));
96});
97​
98it.effect("preserves the typed missing-thread error contract", () => {
99 const layer = makeLayer({
100 projection: Effect.fail(new OrchestratorProjectionError({ threadId })),
101 });
102​
103 return Effect.gen(function* () {
104 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
105 const error = yield* query
106 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 1 })
107 .pipe(Effect.flip);
108​
109 assert.instanceOf(error, CheckpointThreadNotFoundError);
110 assert.deepEqual(
111 { operation: error.operation, threadId: error.threadId },
112 { operation: "CheckpointDiffQuery.getTurnDiff", threadId },
113 );
114 }).pipe(Effect.provide(layer));
115});
116​
117it.effect("preserves the typed unavailable-range error contract", () => {
118 const layer = makeLayer({ projection: Effect.succeed(makeProjection()) });
119​
120 return Effect.gen(function* () {
121 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
122 const error = yield* query
123 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 3 })
124 .pipe(Effect.flip);
125​
126 assert.instanceOf(error, CheckpointTurnRangeUnavailableError);
127 assert.deepEqual(
128 {
129 requestedTurnCount: error.requestedTurnCount,
130 availableTurnCount: error.availableTurnCount,
131 },
132 { requestedTurnCount: 3, availableTurnCount: 2 },
133 );
134 }).pipe(Effect.provide(layer));
135});
136​
137it.effect("preserves the typed missing-baseline-ref error contract", () => {
138 const projection = makeProjection();
139 const layer = makeLayer({
140 projection: Effect.succeed({
141 ...projection,
142 checkpointScopes: projection.checkpointScopes.filter((scope) => scope.id !== firstScopeId),
143 }),
144 });
145​
146 return Effect.gen(function* () {
147 const query = yield* CheckpointDiffQuery.CheckpointDiffQuery;
148 const error = yield* query
149 .getTurnDiff({ threadId, fromTurnCount: 0, toTurnCount: 2 })
150 .pipe(Effect.flip);
151​
152 assert.instanceOf(error, CheckpointRefUnavailableError);
153 assert.deepEqual(
154 { checkpoint: error.checkpoint, turnCount: error.turnCount },
155 { checkpoint: "from", turnCount: 0 },
156 );
157 }).pipe(Effect.provide(layer));
158});