Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import { ThreadId, WorktreeSetupSnapshot } from "@t3tools/contracts";
3import * as Deferred from "effect/Deferred";
4import * as Duration from "effect/Duration";
5import * as Effect from "effect/Effect";
6import * as Exit from "effect/Exit";
7import * as Fiber from "effect/Fiber";
8import * as Schema from "effect/Schema";
9import * as Stream from "effect/Stream";
10import * as TestClock from "effect/testing/TestClock";
11
12import * as WorktreeSetupTracker from "./WorktreeSetupTracker.ts";
13
14const threadId = ThreadId.make("thread-1");
15
16describe("WorktreeSetupTracker", () => {
17 it.effect("records stage transitions, checkout progress, and the final phase", () =>
18 Effect.gen(function* () {
19 const tracker = yield* WorktreeSetupTracker.make;
20 yield* tracker.begin({
21 threadId,
22 branch: "feature",
23 baseRef: "main",
24 stages: ["checkout", "fetch", "agent"],
25 fiber: null,
26 });
27
28 const initial = yield* tracker.get(threadId);
29 expect(initial?.stages.map((stage) => stage.id)).toEqual(["fetch", "checkout", "agent"]);
30 expect(initial?.phase).toBe("running");
31
32 yield* tracker.stageStatus(threadId, "fetch", "running");
33 yield* tracker.stageStatus(threadId, "fetch", "done", "origin/main at abc1234");
34 yield* tracker.stageStatus(threadId, "checkout", "running");
35 yield* tracker.stage(threadId, "checkout", { percent: 42, detail: "42 / 100 files" });
36 yield* tracker.finish(threadId, "failed", "boom");
37
38 const final = yield* tracker.get(threadId);
39 expect(final?.phase).toBe("failed");
40 expect(final?.error).toBe("boom");
41 const [fetch, checkout, agent] = final?.stages ?? [];
42 expect(fetch).toMatchObject({ status: "done", detail: "origin/main at abc1234" });
43 expect(fetch?.startedAt).not.toBeNull();
44 expect(fetch?.endedAt).not.toBeNull();
45 expect(checkout).toMatchObject({ status: "failed", percent: 42 });
46 expect(agent?.status).toBe("pending");
47 expect(final?.sequence).toBeGreaterThan(initial?.sequence ?? 0);
48 }),
49 );
50
51 it.effect("stream emits the current snapshot first and then only newer ones", () =>
52 Effect.gen(function* () {
53 const tracker = yield* WorktreeSetupTracker.make;
54 yield* tracker.begin({
55 threadId,
56 branch: null,
57 baseRef: null,
58 stages: ["agent"],
59 fiber: null,
60 });
61
62 const collected = yield* tracker.stream(threadId).pipe(
63 Stream.takeUntil((snapshot) => snapshot?.sequence === 2),
64 Stream.runCollect,
65 Effect.forkChild,
66 );
67 yield* Effect.yieldNow;
68 yield* tracker.stageStatus(threadId, "agent", "running");
69 yield* tracker.appendTail(threadId, "agent", "line 1");
70
71 const snapshots = yield* Fiber.join(collected);
72 const sequences = snapshots.map((snapshot) => snapshot?.sequence ?? -1);
73 expect(sequences.at(-1)).toBe(2);
74 expect(sequences).toEqual([...sequences].toSorted((a, b) => a - b));
75 expect(snapshots.at(-1)?.stages[0]?.tail).toEqual(["line 1"]);
76 }),
77 );
78
79 it.effect("stream never steps back behind the snapshot it started from", () =>
80 Effect.gen(function* () {
81 const tracker = yield* WorktreeSetupTracker.make;
82 yield* tracker.begin({
83 threadId,
84 branch: null,
85 baseRef: null,
86 stages: ["agent"],
87 fiber: null,
88 });
89 yield* tracker.stageStatus(threadId, "agent", "running");
90 yield* tracker.stageStatus(threadId, "agent", "done");
91
92 const collected = yield* tracker.stream(threadId).pipe(
93 Stream.takeUntil((snapshot) => snapshot?.phase === "done"),
94 Stream.runCollect,
95 Effect.forkChild,
96 );
97 yield* Effect.yieldNow;
98 yield* tracker.finish(threadId, "done");
99
100 const snapshots = yield* Fiber.join(collected);
101 expect(snapshots.length).toBeGreaterThan(0);
102 expect(snapshots.every((snapshot) => (snapshot?.sequence ?? -1) >= 2)).toBe(true);
103 expect(snapshots.at(-1)?.phase).toBe("done");
104 }),
105 );
106
107 it.effect("a new setup on the same thread keeps sequences increasing", () =>
108 Effect.gen(function* () {
109 const tracker = yield* WorktreeSetupTracker.make;
110 yield* tracker.begin({
111 threadId,
112 branch: "first",
113 baseRef: null,
114 stages: ["agent"],
115 fiber: null,
116 });
117 yield* tracker.finish(threadId, "failed", "boom");
118 const failedSequence = (yield* tracker.get(threadId))?.sequence ?? -1;
119
120 const collected = yield* tracker.stream(threadId).pipe(
121 Stream.takeUntil((snapshot) => snapshot?.branch === "second"),
122 Stream.runCollect,
123 Effect.forkChild,
124 );
125 yield* Effect.yieldNow;
126 yield* tracker.begin({
127 threadId,
128 branch: "second",
129 baseRef: null,
130 stages: ["agent"],
131 fiber: null,
132 });
133
134 const snapshots = yield* Fiber.join(collected);
135 const last = snapshots.at(-1);
136 expect(last?.phase).toBe("running");
137 expect(last?.sequence).toBeGreaterThan(failedSequence);
138 }),
139 );
140
141 it.effect("finished setups are dropped after the retention window", () =>
142 Effect.gen(function* () {
143 const tracker = yield* WorktreeSetupTracker.make;
144 yield* tracker.begin({
145 threadId,
146 branch: null,
147 baseRef: null,
148 stages: ["agent"],
149 fiber: null,
150 });
151 yield* tracker.finish(threadId, "done");
152 expect((yield* tracker.get(threadId))?.phase).toBe("done");
153
154 yield* TestClock.adjust(Duration.seconds(31));
155 expect(yield* tracker.get(threadId)).toBeNull();
156 }),
157 );
158
159 it.effect("cancel interrupts the bootstrap fiber and reports whether one was running", () =>
160 Effect.gen(function* () {
161 const tracker = yield* WorktreeSetupTracker.make;
162 const started = yield* Deferred.make<void>();
163 const fiber = yield* Deferred.succeed(started, undefined).pipe(
164 Effect.andThen(Effect.never),
165 Effect.forkChild,
166 );
167 yield* Deferred.await(started);
168 yield* tracker.begin({ threadId, branch: null, baseRef: null, stages: ["agent"], fiber });
169
170 expect(yield* tracker.cancel(threadId)).toBe(true);
171 const exit = yield* Fiber.await(fiber);
172 expect(Exit.hasInterrupts(exit)).toBe(true);
173
174 yield* tracker.finish(threadId, "cancelled");
175 expect(yield* tracker.cancel(threadId)).toBe(false);
176 expect(yield* tracker.cancel(ThreadId.make("unknown"))).toBe(false);
177 }),
178 );
179
180 it.effect("markUncancellable makes a later cancel a no-op while the setup keeps running", () =>
181 Effect.gen(function* () {
182 const tracker = yield* WorktreeSetupTracker.make;
183 const fiber = yield* Effect.forkChild(Effect.never);
184 yield* tracker.begin({ threadId, branch: null, baseRef: null, stages: ["agent"], fiber });
185
186 yield* tracker.markUncancellable(threadId);
187 expect(yield* tracker.cancel(threadId)).toBe(false);
188 expect((yield* tracker.get(threadId))?.phase).toBe("running");
189 yield* Fiber.interrupt(fiber);
190 }),
191 );
192
193 it.effect("clamps free text to the contract limits before publishing", () =>
194 Effect.gen(function* () {
195 const tracker = yield* WorktreeSetupTracker.make;
196 yield* tracker.begin({
197 threadId,
198 branch: null,
199 baseRef: null,
200 stages: ["checkout", "setup-script"],
201 fiber: null,
202 });
203 const long = "x".repeat(2_000);
204
205 yield* tracker.stageStatus(threadId, "checkout", "done", long);
206 yield* tracker.stage(threadId, "setup-script", { detail: long });
207 yield* tracker.appendTail(threadId, "setup-script", long);
208 yield* tracker.finish(threadId, "failed", long);
209
210 const snapshot = yield* tracker.get(threadId);
211 expect(snapshot?.stages[0]?.detail?.length).toBe(200);
212 expect(snapshot?.stages[1]?.detail?.length).toBe(200);
213 expect(snapshot?.stages[1]?.tail[0]?.length).toBe(400);
214 expect(snapshot?.error?.length).toBe(1000);
215 expect(Schema.is(WorktreeSetupSnapshot)(snapshot)).toBe(true);
216 }),
217 );
218});