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

ProjectSetupScriptRunner.test.ts

200 lines7.3 KB

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