Repository navigation
Expand file tree
/
Copy pathWorktreeSetupTracker.test.ts
More file actions
143 lines (126 loc) · 5.31 KB
/
Copy pathWorktreeSetupTracker.test.ts
File metadata and controls
143 lines (126 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { describe, expect, it } from "@effect/vitest";
import { ThreadId } from "@t3tools/contracts";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
import * as Fiber from "effect/Fiber";
import * as Stream from "effect/Stream";
import * as WorktreeSetupTracker from "./WorktreeSetupTracker.ts";
const threadId = ThreadId.make("thread-1");
describe("WorktreeSetupTracker", () => {
it.effect("records stage transitions, checkout progress, and the final phase", () =>
Effect.gen(function* () {
const tracker = yield* WorktreeSetupTracker.make;
yield* tracker.begin({
threadId,
branch: "feature",
baseRef: "main",
stages: ["checkout", "fetch", "agent"],
fiber: null,
});
const initial = yield* tracker.get(threadId);
expect(initial?.stages.map((stage) => stage.id)).toEqual(["fetch", "checkout", "agent"]);
expect(initial?.phase).toBe("running");
yield* tracker.stageStatus(threadId, "fetch", "running");
yield* tracker.stageStatus(threadId, "fetch", "done", "origin/main at abc1234");
yield* tracker.stageStatus(threadId, "checkout", "running");
yield* tracker.stage(threadId, "checkout", { percent: 42, detail: "42 / 100 files" });
yield* tracker.finish(threadId, "failed", "boom");
const final = yield* tracker.get(threadId);
expect(final?.phase).toBe("failed");
expect(final?.error).toBe("boom");
const [fetch, checkout, agent] = final?.stages ?? [];
expect(fetch).toMatchObject({ status: "done", detail: "origin/main at abc1234" });
expect(fetch?.startedAt).not.toBeNull();
expect(fetch?.endedAt).not.toBeNull();
expect(checkout).toMatchObject({ status: "failed", percent: 42 });
expect(agent?.status).toBe("pending");
expect(final?.sequence).toBeGreaterThan(initial?.sequence ?? 0);
}),
);
it.effect("stream emits the current snapshot first and then only newer ones", () =>
Effect.gen(function* () {
const tracker = yield* WorktreeSetupTracker.make;
yield* tracker.begin({
threadId,
branch: null,
baseRef: null,
stages: ["agent"],
fiber: null,
});
const collected = yield* tracker.stream(threadId).pipe(
Stream.takeUntil((snapshot) => snapshot?.sequence === 2),
Stream.runCollect,
Effect.forkChild,
);
yield* Effect.yieldNow;
yield* tracker.stageStatus(threadId, "agent", "running");
yield* tracker.appendTail(threadId, "agent", "line 1");
const snapshots = yield* Fiber.join(collected);
const sequences = snapshots.map((snapshot) => snapshot?.sequence ?? -1);
expect(sequences.at(-1)).toBe(2);
expect(sequences).toEqual([...sequences].toSorted((a, b) => a - b));
expect(snapshots.at(-1)?.stages[0]?.tail).toEqual(["line 1"]);
}),
);
it.effect("a new setup on the same thread keeps sequences increasing", () =>
Effect.gen(function* () {
const tracker = yield* WorktreeSetupTracker.make;
yield* tracker.begin({
threadId,
branch: "first",
baseRef: null,
stages: ["agent"],
fiber: null,
});
yield* tracker.finish(threadId, "failed", "boom");
const failedSequence = (yield* tracker.get(threadId))?.sequence ?? -1;
const collected = yield* tracker.stream(threadId).pipe(
Stream.takeUntil((snapshot) => snapshot?.branch === "second"),
Stream.runCollect,
Effect.forkChild,
);
yield* Effect.yieldNow;
yield* tracker.begin({
threadId,
branch: "second",
baseRef: null,
stages: ["agent"],
fiber: null,
});
const snapshots = yield* Fiber.join(collected);
const last = snapshots.at(-1);
expect(last?.phase).toBe("running");
expect(last?.sequence).toBeGreaterThan(failedSequence);
}),
);
it.effect("cancel interrupts the bootstrap fiber and reports whether one was running", () =>
Effect.gen(function* () {
const tracker = yield* WorktreeSetupTracker.make;
const started = yield* Deferred.make<void>();
const fiber = yield* Deferred.succeed(started, undefined).pipe(
Effect.andThen(Effect.never),
Effect.forkChild,
);
yield* Deferred.await(started);
yield* tracker.begin({ threadId, branch: null, baseRef: null, stages: ["agent"], fiber });
expect(yield* tracker.cancel(threadId)).toBe(true);
const exit = yield* Fiber.await(fiber);
expect(Exit.hasInterrupts(exit)).toBe(true);
yield* tracker.finish(threadId, "cancelled");
expect(yield* tracker.cancel(threadId)).toBe(false);
expect(yield* tracker.cancel(ThreadId.make("unknown"))).toBe(false);
}),
);
it.effect("markUncancellable makes a later cancel a no-op while the setup keeps running", () =>
Effect.gen(function* () {
const tracker = yield* WorktreeSetupTracker.make;
const fiber = yield* Effect.forkChild(Effect.never);
yield* tracker.begin({ threadId, branch: null, baseRef: null, stages: ["agent"], fiber });
yield* tracker.markUncancellable(threadId);
expect(yield* tracker.cancel(threadId)).toBe(false);
expect((yield* tracker.get(threadId))?.phase).toBe("running");
yield* Fiber.interrupt(fiber);
}),
);
});