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

serverRuntimeStartup.worktreeSetup.test.ts

158 lines5.2 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import {
3 EventId,
4 type OrchestrationCommand,
5 ThreadId,
6 WORKTREE_SETUP_ACTIVITY_KIND,
7 WorktreeSetupSnapshot,
8 worktreeSetupActivityId,
9 type WorktreeSetupPhase,
10} from "@t3tools/contracts";
11import { assert, it } from "@effect/vitest";
12import * as Effect from "effect/Effect";
13import * as Schema from "effect/Schema";
14import * as Stream from "effect/Stream";
15​
16import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
17import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
18import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
19​
20const startedAt = "2026-08-20T12:00:00.000Z";
21​
22const snapshotFor = (
23 threadId: ThreadId,
24 phase: WorktreeSetupPhase,
25 agentStatus: "pending" | "done" = phase === "running" ? "pending" : "done",
26): WorktreeSetupSnapshot => ({
27 threadId,
28 phase,
29 startedAt,
30 endedAt: phase === "running" ? null : startedAt,
31 branch: "feature",
32 baseRef: "main",
33 worktreePath: null,
34 setupScript: null,
35 stages: [
36 {
37 id: "checkout",
38 status: "done",
39 startedAt,
40 endedAt: startedAt,
41 percent: null,
42 detail: null,
43 tail: [],
44 },
45 {
46 id: "setup-script",
47 status: phase === "running" ? "running" : "done",
48 startedAt,
49 endedAt: phase === "running" ? null : startedAt,
50 percent: null,
51 detail: null,
52 tail: [],
53 },
54 {
55 id: "agent",
56 status: agentStatus,
57 startedAt: null,
58 endedAt: null,
59 percent: null,
60 detail: null,
61 tail: [],
62 },
63 ],
64 error: null,
65 sequence: 4,
66});
67​
68const recordedSetup = (id: string, phase: WorktreeSetupPhase, agentStatus?: "pending" | "done") => {
69 const threadId = ThreadId.make(id);
70 return {
71 id: EventId.make(worktreeSetupActivityId(threadId)),
72 tone: "info" as const,
73 kind: WORKTREE_SETUP_ACTIVITY_KIND,
74 summary: "Setting up worktree",
75 payload: snapshotFor(threadId, phase, agentStatus),
76 turnId: null,
77 createdAt: startedAt,
78 };
79};
80​
81const run = (activities: ReadonlyArray<ReturnType<typeof recordedSetup>>) =>
82 Effect.gen(function* () {
83 const dispatched: Array<OrchestrationCommand> = [];
84 yield* ServerRuntimeStartup.reconcileWorktreeSetups.pipe(
85 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
86 listActivitiesByKind: (kind: string) =>
87 Effect.succeed(kind === WORKTREE_SETUP_ACTIVITY_KIND ? activities : []),
88 } as unknown as ProjectionSnapshotQuery.ProjectionSnapshotQuery["Service"]),
89 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
90 readEvents: () => Stream.empty,
91 readThreadEvents: () => Stream.empty,
92 getThreadReplayStats: () => Effect.die("unused"),
93 dispatch: (command) =>
94 Effect.sync(() => {
95 dispatched.push(command);
96 return { sequence: dispatched.length };
97 }),
98 streamDomainEvents: Stream.empty,
99 subscribeDomainEvents: Effect.succeed(Stream.empty),
100 withWorktreeCleanup: <A, E>(_paths: ReadonlyArray<string>, effect: Effect.Effect<A, E>) =>
101 effect,
102 latestSequence: Effect.succeed(0),
103 }),
104 Effect.provide(NodeServices.layer),
105 );
106 return dispatched;
107 });
108​
109it.effect("marks setups still recorded as running failed after a restart", () =>
110 Effect.gen(function* () {
111 const dispatched = yield* run([
112 recordedSetup("thread-running", "running"),
113 recordedSetup("thread-done", "done"),
114 recordedSetup("thread-failed", "failed"),
115 ]);
116​
117 assert.equal(dispatched.length, 1);
118 const command = dispatched[0]!;
119 assert.equal(command.type, "thread.activity.append");
120 if (command.type !== "thread.activity.append") return;
121 assert.equal(command.threadId, ThreadId.make("thread-running"));
122 assert.equal(command.activity.id, worktreeSetupActivityId(ThreadId.make("thread-running")));
123 assert.equal(command.activity.tone, "error");
124 const payload = yield* Schema.decodeUnknownEffect(WorktreeSetupSnapshot)(
125 command.activity.payload,
126 );
127 assert.equal(payload.phase, "failed");
128 assert.isNotNull(payload.endedAt);
129 assert.equal(payload.sequence, 5);
130 assert.deepEqual(
131 payload.stages.map((stage) => stage.status),
132 ["done", "failed", "failed"],
133 );
134 }),
135);
136​
137it.effect(
138 "settles an async setup script whose turn already started without failing the setup",
139 () =>
140 Effect.gen(function* () {
141 const dispatched = yield* run([recordedSetup("thread-async", "running", "done")]);
142​
143 assert.equal(dispatched.length, 1);
144 const command = dispatched[0]!;
145 if (command.type !== "thread.activity.append") return assert.fail(command.type);
146 const payload = yield* Schema.decodeUnknownEffect(WorktreeSetupSnapshot)(
147 command.activity.payload,
148 );
149 // The turn is live; only the background script was lost. Nothing asks the
150 // user to resend, and the setup reads as done with a failed script stage.
151 assert.equal(payload.phase, "done");
152 assert.isNull(payload.error);
153 assert.deepEqual(
154 payload.stages.map((stage) => stage.status),
155 ["done", "failed", "done"],
156 );
157 }),
158);