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} 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
53for (const scenario of [
54 {
55 label: "discovers a new PR for the completed run's branch",
56 branch: "feature",
57 checkedOut: "feature",
58 activeRun: null,
59 expected: ["/repo"],
60 },
61 {
62 label: "leaves the default branch's PR cache alone",
63 branch: "main",
64 checkedOut: "main",
65 activeRun: null,
66 expected: [],
67 },
68 {
69 label: "does not refresh another thread's checkout",
70 branch: "feature",
71 checkedOut: "other",
72 activeRun: null,
73 expected: [],
74 },
75 {
76 label: "does not refresh during a newer active run",
77 branch: "feature",
78 checkedOut: "feature",
79 activeRun: "newer-run",
80 expected: [],
81 },
82] as const) {
83 it.effect(scenario.label, () => {
84 const refreshed: string[] = [];
85 const threadId = ThreadId.make("thread-pr-refresh");
86 const runId = RunId.make("completed-run");
87 const layer = RunFinalization.observerLive.pipe(
88 Layer.provide(
89 Layer.mergeAll(
90 Layer.mock(WorkspaceEntries.WorkspaceEntries)({ refresh: () => Effect.void }),
91 Layer.mock(PullRequestService.PullRequestService)({
92 refreshAfterTurn: () => Effect.void,
93 }),
94 Layer.mock(VcsStatusBroadcaster.VcsStatusBroadcaster)({
95 refreshLocalStatus: () =>
96 Effect.succeed({
97 isRepo: true,
98 hasPrimaryRemote: true,
99 isDefaultRef: scenario.checkedOut === "main",
100 refName: scenario.checkedOut,
101 hasWorkingTreeChanges: false,
102 workingTree: { files: [], insertions: 0, deletions: 0 },
103 }),
104 refreshStatus: () =>
105 Effect.die("turn completion must preserve known PRs and lookup backoff"),
106 refreshPullRequestStatus: (cwd) =>
107 Effect.sync(() => {
108 refreshed.push(cwd);
109 return null;
110 }),
111 }),
112 Layer.mock(ProjectionStore.ProjectionStoreV2)({
113 getThreadShell: () =>
114 Effect.succeed({
115 id: threadId,
116 branch: scenario.branch,
117 activeRunId: scenario.activeRun === null ? null : RunId.make(scenario.activeRun),
118 } as OrchestrationV2ThreadShell),
119 }),
120 ),
121 ),
122 );
123 return Effect.gen(function* () {
124 const observer = yield* RunFinalization.RunFinalizationObserver;
125 yield* observer.refresh({ cwd: "/repo", threadId, runId });
126 assert.deepEqual(refreshed, [...scenario.expected]);
127 }).pipe(Effect.provide(layer));
128 });
129}