Open History to see the last commit
1import { assert, it, vi } from "@effect/vitest";
2import {
3 CheckpointScopeId,
4 RunId,
5 ThreadId,
6 type OrchestrationV2ThreadShell,
7 type OrchestrationV2ThreadProjection,
8} from "@t3tools/contracts";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11
12import * as PullRequestService from "../pullRequest/PullRequestService.ts";
13import * as VcsStatusBroadcaster from "../vcs/VcsStatusBroadcaster.ts";
14import * as WorkspaceEntries from "../workspace/WorkspaceEntries.ts";
15import * as CheckpointCapture from "./CheckpointCaptureService.ts";
16import * as ProjectionStore from "./ProjectionStore.ts";
17import * as RunFinalization from "./RunFinalizationService.ts";
18
19it.effect("captures the root checkpoint and refreshes workspace state", () => {
20 const threadId = ThreadId.make("thread_finalize");
21 const runId = RunId.make("run_finalize");
22 const scopeId = CheckpointScopeId.make("scope_finalize");
23 const capture = vi.fn(() => Effect.void);
24 const refresh = vi.fn(() => Effect.void);
25 const projection = {
26 checkpointScopes: [{ id: scopeId, cwd: "/repo" }],
27 } as unknown as OrchestrationV2ThreadProjection;
28 const layer = RunFinalization.layer.pipe(
29 Layer.provide(
30 Layer.mergeAll(
31 Layer.mock(CheckpointCapture.CheckpointCaptureServiceV2)({ execute: capture }),
32 Layer.mock(ProjectionStore.ProjectionStoreV2)({
33 getThreadProjection: () => Effect.succeed(projection),
34 }),
35 Layer.succeed(RunFinalization.RunFinalizationObserver, {
36 refresh,
37 refreshAfterTurn: Effect.void,
38 }),
39 ),
40 ),
41 );
42 return Effect.gen(function* () {
43 const service = yield* RunFinalization.RunFinalizationService;
44 yield* service.finalize({ threadId, runId, scopeId });
45 assert.equal(capture.mock.calls.length, 1);
46 assert.deepEqual(refresh.mock.calls[0], [{ cwd: "/repo", threadId, runId }]);
47 }).pipe(Effect.provide(layer));
48});
49
50for (const scenario of [
51 {
52 label: "discovers a new PR for the completed run's branch",
53 branch: "feature",
54 checkedOut: "feature",
55 activeRun: null,
56 expected: ["/repo"],
57 },
58 {
59 label: "leaves the default branch's PR cache alone",
60 branch: "main",
61 checkedOut: "main",
62 activeRun: null,
63 expected: [],
64 },
65 {
66 label: "does not refresh another thread's checkout",
67 branch: "feature",
68 checkedOut: "other",
69 activeRun: null,
70 expected: [],
71 },
72 {
73 label: "does not refresh during a newer active run",
74 branch: "feature",
75 checkedOut: "feature",
76 activeRun: "newer-run",
77 expected: [],
78 },
79] as const) {
80 it.effect(scenario.label, () => {
81 const refreshed: string[] = [];
82 const threadId = ThreadId.make("thread-pr-refresh");
83 const runId = RunId.make("completed-run");
84 const layer = RunFinalization.observerLive.pipe(
85 Layer.provide(
86 Layer.mergeAll(
87 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
88 Layer.mock(PullRequestService.PullRequestService)({ refreshAfterTurn: Effect.void }),
89 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
90 refreshLocalStatus: () =>
91 Effect.succeed({
92 isRepo: true,
93 hasPrimaryRemote: true,
94 isDefaultRef: scenario.checkedOut === "main",
95 refName: scenario.checkedOut,
96 hasWorkingTreeChanges: false,
97 workingTree: { files: [], insertions: 0, deletions: 0 },
98 }),
99 refreshStatus: () =>
100 Effect.die("turn completion must preserve known PRs and lookup backoff"),
101 refreshPullRequestStatus: (cwd) =>
102 Effect.sync(() => {
103 refreshed.push(cwd);
104 return null;
105 }),
106 }),
107 Layer.mock(ProjectionStore.ProjectionStoreV2)({
108 getThreadShell: () =>
109 Effect.succeed({
110 id: threadId,
111 branch: scenario.branch,
112 activeRunId: scenario.activeRun === null ? null : RunId.make(scenario.activeRun),
113 } as OrchestrationV2ThreadShell),
114 }),
115 ),
116 ),
117 );
118 return Effect.gen(function* () {
119 const observer = yield* RunFinalization.RunFinalizationObserver;
120 yield* observer.refresh({ cwd: "/repo", threadId, runId });
121 assert.deepEqual(refreshed, [...scenario.expected]);
122 }).pipe(Effect.provide(layer));
123 });
124}