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

RunFinalizationService.test.ts

242 lines8.8 KB

1import { assert, it, vi } from "@effect/vitest";
2import {
3 CheckpointScopeId,
4 RunId,
5 ThreadId,
6 type OrchestrationV2ThreadShell,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9import * as Layer from "effect/Layer";
10​
11import * as PullRequestService from "../pullRequest/PullRequestService.ts";
12import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
13import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
14import * as CheckpointCapture from "./CheckpointCaptureService.ts";
15import * as ProjectionStore from "./ProjectionStore.ts";
16import * as RunFinalization from "./RunFinalizationService.ts";
17​
18it.effect("refreshes workspace after checkpoint capture without reading history", () => {
19 const threadId = ThreadId.make("thread_finalize");
20 const runId = RunId.make("run_finalize");
21 const scopeId = CheckpointScopeId.make("scope_finalize");
22 const capture = vi.fn(() => Effect.void);
23 const refresh = vi.fn(() => Effect.void);
24 const checkpointContext = {
25 runs: [],
26 checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }],
27 checkpoints: [],
28 };
29 const layer = RunFinalization.layer.pipe(
30 Layer.provide(
31 Layer.mergeAll(
32 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({ execute: capture }),
33 Layer.mock(ProjectionStore.ProjectionStoreV2)({
34 getThreadProjection: () =>
35 Effect.die("workspace refresh must not load transcript history"),
36 getCheckpointContext: () => Effect.succeed(checkpointContext),
37 }),
38 Layer.succeed(RunFinalization.RunFinalizationObserver, {
39 refresh,
40 refreshAfterTurn: () => Effect.void,
41 }),
42 ),
43 ),
44 );
45 return Effect.gen(function* () {
46 const service = yield* RunFinalization.RunFinalizationService;
47 yield* service.finalize({ threadId, runId, scopeId });
48 assert.equal(capture.mock.calls.length, 1);
49 assert.deepEqual(refresh.mock.calls[0], [{ cwd: "/repo", threadId, runId }]);
50 }).pipe(Effect.provide(layer));
51});
52​
53it.effect("completes finalization when the post-checkpoint workspace refresh fails", () => {
54 const threadId = ThreadId.make("thread_refresh_failure");
55 const runId = RunId.make("run_refresh_failure");
56 const scopeId = CheckpointScopeId.make("scope_refresh_failure");
57 const captured: string[] = [];
58 const layer = RunFinalization.layer.pipe(
59 Layer.provide(
60 Layer.mergeAll(
61 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({
62 execute: () =>
63 Effect.sync(() => {
64 captured.push("checkpoint");
65 }),
66 }),
67 Layer.mock(ProjectionStore.ProjectionStoreV2)({
68 getCheckpointContext: () =>
69 Effect.succeed({
70 runs: [],
71 checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }],
72 checkpoints: [],
73 }),
74 }),
75 Layer.succeed(RunFinalization.RunFinalizationObserver, {
76 refresh: () =>
77 Effect.gen(function* () {
78 captured.push("refresh");
79 return yield* new RunFinalization.RunFinalizationRefreshError({
80 cwd: "/repo",
81 cause: new Error("Git command timed out"),
82 });
83 }),
84 refreshAfterTurn: () => Effect.void,
85 }),
86 ),
87 ),
88 );
89 return Effect.gen(function* () {
90 const service = yield* RunFinalization.RunFinalizationService;
91 yield* service.finalize({ threadId, runId, scopeId });
92 assert.deepEqual(captured, ["checkpoint", "refresh"]);
93 }).pipe(Effect.provide(layer));
94});
95​
96it.effect("preserves checkpoint failures without refreshing the workspace", () => {
97 const threadId = ThreadId.make("thread_capture_failure");
98 const runId = RunId.make("run_capture_failure");
99 const scopeId = CheckpointScopeId.make("scope_capture_failure");
100 const captureError = new CheckpointCapture.CheckpointCaptureExecutionError({
101 threadId,
102 runId,
103 scopeId,
104 cause: new Error("Checkpoint capture failed"),
105 });
106 const refresh = vi.fn(() => Effect.void);
107 const layer = RunFinalization.layer.pipe(
108 Layer.provide(
109 Layer.mergeAll(
110 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({
111 execute: () => Effect.fail(captureError),
112 }),
113 Layer.mock(ProjectionStore.ProjectionStoreV2)({
114 getCheckpointContext: () => Effect.die("capture failed before context lookup"),
115 }),
116 Layer.succeed(RunFinalization.RunFinalizationObserver, {
117 refresh,
118 refreshAfterTurn: () => Effect.void,
119 }),
120 ),
121 ),
122 );
123 return Effect.gen(function* () {
124 const service = yield* RunFinalization.RunFinalizationService;
125 const error = yield* service.finalize({ threadId, runId, scopeId }).pipe(Effect.flip);
126 assert.equal(error.operation, "capture-checkpoint");
127 assert.strictEqual(error.cause, captureError);
128 assert.equal(refresh.mock.calls.length, 0);
129 }).pipe(Effect.provide(layer));
130});
131​
132it.effect("preserves checkpoint context read failures without refreshing the workspace", () => {
133 const threadId = ThreadId.make("thread_context_failure");
134 const runId = RunId.make("run_context_failure");
135 const scopeId = CheckpointScopeId.make("scope_context_failure");
136 const contextError = new ProjectionStore.ProjectionStoreReadError({
137 threadId,
138 cause: new Error("Checkpoint context read failed"),
139 });
140 const refresh = vi.fn(() => Effect.void);
141 const layer = RunFinalization.layer.pipe(
142 Layer.provide(
143 Layer.mergeAll(
144 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({ execute: () => Effect.void }),
145 Layer.mock(ProjectionStore.ProjectionStoreV2)({
146 getCheckpointContext: () => Effect.fail(contextError),
147 }),
148 Layer.succeed(RunFinalization.RunFinalizationObserver, {
149 refresh,
150 refreshAfterTurn: () => Effect.void,
151 }),
152 ),
153 ),
154 );
155 return Effect.gen(function* () {
156 const service = yield* RunFinalization.RunFinalizationService;
157 const error = yield* service.finalize({ threadId, runId, scopeId }).pipe(Effect.flip);
158 assert.equal(error.operation, "refresh-workspace");
159 assert.strictEqual(error.cause, contextError);
160 assert.equal(refresh.mock.calls.length, 0);
161 }).pipe(Effect.provide(layer));
162});
163​
164it.effect.each(
165 (
166 [
167 {
168 label: "discovers a new PR for the completed run's branch",
169 branch: "feature",
170 checkedOut: "feature",
171 activeRun: null,
172 expected: ["/repo"],
173 },
174 {
175 label: "leaves the default branch's PR cache alone",
176 branch: "main",
177 checkedOut: "main",
178 activeRun: null,
179 expected: [],
180 },
181 {
182 label: "does not refresh another thread's checkout",
183 branch: "feature",
184 checkedOut: "other",
185 activeRun: null,
186 expected: [],
187 },
188 {
189 label: "does not refresh during a newer active run",
190 branch: "feature",
191 checkedOut: "feature",
192 activeRun: "newer-run",
193 expected: [],
194 },
195 ] as const
196 ).map((scenario) => [scenario.label, scenario] as const),
197)("%s", ([, scenario]) => {
198 const refreshed: string[] = [];
199 const threadId = ThreadId.make("thread-pr-refresh");
200 const runId = RunId.make("completed-run");
201 const layer = RunFinalization.layerObserver.pipe(
202 Layer.provide(
203 Layer.mergeAll(
204 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
205 Layer.mock(PullRequestService.PullRequestService)({
206 refreshAfterTurn: () => Effect.void,
207 }),
208 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
209 refreshLocalStatus: () =>
210 Effect.succeed({
211 isRepo: true,
212 hasPrimaryRemote: true,
213 isDefaultRef: scenario.checkedOut === "main",
214 refName: scenario.checkedOut,
215 hasWorkingTreeChanges: false,
216 workingTree: { files: [], insertions: 0, deletions: 0 },
217 }),
218 refreshStatus: () =>
219 Effect.die("turn completion must preserve known PRs and lookup backoff"),
220 refreshPullRequestStatus: (cwd) =>
221 Effect.sync(() => {
222 refreshed.push(cwd);
223 return null;
224 }),
225 }),
226 Layer.mock(ProjectionStore.ProjectionStoreV2)({
227 getThreadShell: () =>
228 Effect.succeed({
229 id: threadId,
230 branch: scenario.branch,
231 activeRunId: scenario.activeRun === null ? null : RunId.make(scenario.activeRun),
232 } as OrchestrationV2ThreadShell),
233 }),
234 ),
235 ),
236 );
237 return Effect.gen(function* () {
238 const observer = yield* RunFinalization.RunFinalizationObserver;
239 yield* observer.refresh({ cwd: "/repo", threadId, runId });
240 assert.deepEqual(refreshed, [...scenario.expected]);
241 }).pipe(Effect.provide(layer));
242});