Open History to see the last commit
1import { assert, it } 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("completes finalization when the post-checkpoint workspace refresh fails", () => {
19 const threadId = ThreadId.make("thread_refresh_failure");
20 const runId = RunId.make("run_refresh_failure");
21 const scopeId = CheckpointScopeId.make("scope_refresh_failure");
22 const captured: string[] = [];
23 const layer = RunFinalization.layer.pipe(
24 Layer.provide(
25 Layer.mergeAll(
26 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({
27 execute: () =>
28 Effect.sync(() => {
29 captured.push("checkpoint");
30 }),
31 }),
32 Layer.mock(ProjectionStore.ProjectionStoreV2)({
33 getCheckpointContext: () =>
34 Effect.succeed({
35 runs: [],
36 checkpointScopes: [{ id: scopeId, runId, kind: "root_run" as const, cwd: "/repo" }],
37 checkpoints: [],
38 }),
39 }),
40 Layer.succeed(RunFinalization.RunFinalizationObserver, {
41 refresh: () =>
42 Effect.gen(function* () {
43 captured.push("refresh");
44 return yield* new RunFinalization.RunFinalizationRefreshError({
45 cwd: "/repo",
46 cause: new Error("Git command timed out"),
47 });
48 }),
49 refreshAfterTurn: () => Effect.void,
50 }),
51 ),
52 ),
53 );
54 return Effect.gen(function* () {
55 const service = yield* RunFinalization.RunFinalizationService;
56 yield* service.finalize({ threadId, runId, scopeId });
57 assert.deepEqual(captured, ["checkpoint", "refresh"]);
58 }).pipe(Effect.provide(layer));
59});
60
61it.effect.each(
62 (
63 [
64 {
65 label: "discovers a new PR for the completed run's branch",
66 branch: "feature",
67 checkedOut: "feature",
68 activeRun: null,
69 expected: ["/repo"],
70 },
71 {
72 label: "leaves the default branch's PR cache alone",
73 branch: "main",
74 checkedOut: "main",
75 activeRun: null,
76 expected: [],
77 },
78 {
79 label: "does not refresh another thread's checkout",
80 branch: "feature",
81 checkedOut: "other",
82 activeRun: null,
83 expected: [],
84 },
85 {
86 label: "does not refresh during a newer active run",
87 branch: "feature",
88 checkedOut: "feature",
89 activeRun: "newer-run",
90 expected: [],
91 },
92 ] as const
93 ).map((scenario) => [scenario.label, scenario] as const),
94)("%s", ([, scenario]) => {
95 const refreshed: string[] = [];
96 const threadId = ThreadId.make("thread-pr-refresh");
97 const runId = RunId.make("completed-run");
98 const layer = RunFinalization.layerObserver.pipe(
99 Layer.provide(
100 Layer.mergeAll(
101 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
102 Layer.mock(PullRequestService.PullRequestService)({
103 refreshAfterTurn: () => Effect.void,
104 }),
105 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
106 refreshLocalStatus: () =>
107 Effect.succeed({
108 isRepo: true,
109 hasPrimaryRemote: true,
110 isDefaultRef: scenario.checkedOut === "main",
111 refName: scenario.checkedOut,
112 hasWorkingTreeChanges: false,
113 workingTree: { files: [], insertions: 0, deletions: 0 },
114 }),
115 refreshStatus: () =>
116 Effect.die("turn completion must preserve known PRs and lookup backoff"),
117 refreshPullRequestStatus: (cwd) =>
118 Effect.sync(() => {
119 refreshed.push(cwd);
120 return null;
121 }),
122 }),
123 Layer.mock(ProjectionStore.ProjectionStoreV2)({
124 getThreadShell: () =>
125 Effect.succeed({
126 id: threadId,
127 branch: scenario.branch,
128 activeRunId: scenario.activeRun === null ? null : RunId.make(scenario.activeRun),
129 } as OrchestrationV2ThreadShell),
130 }),
131 ),
132 ),
133 );
134 return Effect.gen(function* () {
135 const observer = yield* RunFinalization.RunFinalizationObserver;
136 yield* observer.refresh({ cwd: "/repo", threadId, runId });
137 assert.deepEqual(refreshed, [...scenario.expected]);
138 }).pipe(Effect.provide(layer));
139});