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

ProjectSetupScriptRunner.test.ts

172 lines6.7 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { describe, expect, it } from "@effect/vitest";
3import { ProjectId, type TerminalEvent, type TerminalSessionSnapshot } from "@t3tools/contracts";
4import * as HostProcess from "@t3tools/shared/HostProcess";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Layer from "effect/Layer";
9import * as Queue from "effect/Queue";
10import { TestClock } from "effect/testing";
11​
12import * as ServerSettings from "../serverSettings.ts";
13import * as TerminalManager from "../terminal/Manager.ts";
14import * as ProjectionStore from "../orchestration-v2/ProjectionStore.ts";
15import * as ProjectService from "./ProjectService.ts";
16import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts";
17​
18const threadId = "thread-1";
19const terminalId = "setup-setup";
20const event = { threadId, terminalId, sequence: 1, createdAt: "2026-01-01T00:00:00.000Z" };
21​
22const openSnapshot = (history: string): TerminalSessionSnapshot => ({
23 threadId,
24 terminalId,
25 cwd: "/repo/.t3/worktrees/feature",
26 worktreePath: null,
27 status: "running",
28 pid: 123,
29 history,
30 exitCode: null,
31 exitSignal: null,
32 label: "zsh",
33 updatedAt: "2026-01-01T00:00:00.000Z",
34});
35​
36const makeHarness = Effect.fn(function* (options: {
37 readonly history?: string;
38 readonly opening?: Deferred.Deferred<void>;
39}) {
40 const listeners = new Set<(event: TerminalEvent) => Effect.Effect<void>>();
41 const writes = yield* Queue.unbounded<string>();
42 const outputLines = yield* Queue.unbounded<string>();
43 const terminals = Layer.mock(TerminalManager.TerminalManager)({
44 open: () =>
45 (options.opening
46 ? Deferred.succeed(options.opening, undefined).pipe(Effect.andThen(Effect.never))
47 : Effect.void
48 ).pipe(Effect.as(openSnapshot(options.history ?? ""))),
49 subscribe: (listener) =>
50 Effect.sync(() => {
51 listeners.add(listener);
52 return () => listeners.delete(listener);
53 }),
54 write: (input) => Queue.offer(writes, input.data).pipe(Effect.asVoid),
55 closeIdle: () => Effect.void,
56 });
57 const runner = yield* ProjectSetupScriptRunner.make.pipe(
58 Effect.provide(
59 Layer.mergeAll(
60 terminals,
61 Layer.mock(ProjectService.ProjectService)({}),
62 Layer.mock(ProjectionStore.ProjectionStoreV2)({
63 getThreadShell: () => Effect.succeed(null),
64 }),
65 ServerSettings.layerTest(),
66 NodeCrypto.layer,
67 ),
68 ),
69 Effect.provideService(HostProcess.Platform, "darwin"),
70 Effect.provideService(HostProcess.Environment, { SHELL: "/bin/zsh" }),
71 );
72 const start = runner.runForThread({
73 threadId,
74 worktreePath: "/repo/.t3/worktrees/feature",
75 observeCompletion: { onOutputLine: (line) => Queue.offer(outputLines, line) },
76 project: {
77 id: ProjectId.make("project-1"),
78 workspaceRoot: "/repo",
79 scripts: [
80 {
81 id: "setup",
82 name: "Setup",
83 command: "vp i",
84 icon: "configure",
85 runOnWorktreeCreate: true,
86 },
87 ],
88 },
89 });
90 const emit = (terminalEvent: TerminalEvent) =>
91 Effect.forEach([...listeners], (listener) => listener(terminalEvent), { discard: true });
92 return { start, listeners, writes, outputLines, emit };
93});
94​
95const setup = Effect.fn(function* (options: { readonly history?: string } = {}) {
96 const harness = yield* makeHarness(options);
97 const result = yield* harness.start;
98 const completion = result.status === "started" ? result.completion : undefined;
99 if (!completion) return yield* Effect.die("setup did not start an observed run");
100 return { ...harness, result, completion };
101});
102​
103describe("ProjectSetupScriptRunner", () => {
104 it.effect("shows a startup prompt and types only once the line editor starts", () =>
105 Effect.gen(function* () {
106 const { result, writes, outputLines, emit } = yield* setup();
107 expect(result).toMatchObject({ status: "started", terminalId });
108​
109 yield* emit({ ...event, type: "output", data: "Missing Node.\r\nInstall it? [y/N]: " });
110 yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" });
111 yield* TestClock.adjust("1 minute");
112 expect(yield* Queue.size(writes)).toBe(0);
113 expect(yield* Queue.takeAll(outputLines)).toEqual(["Missing Node.", "Install it? [y/N]:"]);
114​
115 yield* emit({ ...event, type: "activity", hasRunningSubprocess: false, label: "zsh" });
116 yield* emit({ ...event, type: "output", data: "\r\n% \x1b[?2004h" });
117 expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/);
118 }),
119 );
120​
121 it.effect("types into a quiet shell that has no line editor", () =>
122 Effect.gen(function* () {
123 const { writes } = yield* setup();
124 yield* TestClock.adjust("3 seconds");
125 expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/);
126 }),
127 );
128​
129 it.effect("ignores line editor state restored from an earlier shell", () =>
130 Effect.gen(function* () {
131 const { writes, emit } = yield* setup({ history: "old run\r\n% \x1b[?2004h" });
132 yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" });
133 yield* TestClock.adjust("1 minute");
134 expect(yield* Queue.size(writes)).toBe(0);
135 }),
136 );
137​
138 it.effect("forgets a busy startup child when the shell restarts", () =>
139 Effect.gen(function* () {
140 const { writes, emit } = yield* setup();
141 yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" });
142 yield* TestClock.adjust("1 minute");
143 expect(yield* Queue.size(writes)).toBe(0);
144 yield* emit({ ...event, type: "restarted", snapshot: openSnapshot("") });
145 // The restart lands inside a quiet window, so the next full window decides.
146 yield* TestClock.adjust("6 seconds");
147 expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/);
148 }),
149 );
150​
151 it.effect("drops its terminal listeners when cancelled while the terminal opens", () =>
152 Effect.gen(function* () {
153 const opening = yield* Deferred.make<void>();
154 const { start, listeners } = yield* makeHarness({ opening });
155 const fiber = yield* Effect.forkChild(start);
156 yield* Deferred.await(opening);
157 expect(listeners.size).toBe(2);
158 yield* Fiber.interrupt(fiber);
159 expect(listeners.size).toBe(0);
160 }),
161 );
162​
163 it.effect("does not type into a shell that exits during startup", () =>
164 Effect.gen(function* () {
165 const { completion, writes, emit } = yield* setup();
166 yield* emit({ ...event, type: "exited", exitCode: 1, exitSignal: null });
167 expect(yield* completion).toMatchObject({ exitCode: null });
168 yield* TestClock.adjust("1 minute");
169 expect(yield* Queue.size(writes)).toBe(0);
170 }),
171 );
172});