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

ProjectSetupScriptRunner.test.ts

266 lines9.2 KB

1import { describe, expect, it, vi } from "@effect/vitest";
2import { type OrchestrationProject, ProjectId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as Schema from "effect/Schema";
7​
8import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
9import * as ServerSettings from "../serverSettings.ts";
10import * as TerminalManager from "../terminal/Manager.ts";
11import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts";
12​
13const isProjectSetupScriptOperationError = Schema.is(
14 ProjectSetupScriptRunner.ProjectSetupScriptOperationError,
15);
16​
17const makeProject = (scripts: OrchestrationProject["scripts"]): OrchestrationProject => ({
18 id: ProjectId.make("project-1"),
19 title: "Project",
20 workspaceRoot: "/repo/project",
21 defaultModelSelection: null,
22 scripts,
23 createdAt: "2026-01-01T00:00:00.000Z",
24 updatedAt: "2026-01-01T00:00:00.000Z",
25 deletedAt: null,
26});
27​
28const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>
29 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
30 getUserInputActivity: () => Effect.die("unused"),
31 getCommandReadModel: () => Effect.die("unused"),
32 getSnapshot: () => Effect.die("unused"),
33 getShellSnapshot: () => Effect.die("unused"),
34 getArchivedShellSnapshot: () => Effect.die("unused"),
35 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 1 }),
36 getCounts: () => Effect.die("unused"),
37 getEventReplayStats: () => Effect.die("unused"),
38 getActiveProjectByWorkspaceRoot: (workspaceRoot) =>
39 Effect.succeed(
40 workspaceRoot === project.workspaceRoot ? Option.some(project) : Option.none(),
41 ),
42 getProjectShellById: (projectId) =>
43 Effect.succeed(projectId === project.id ? Option.some(project) : Option.none()),
44 getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),
45 getImportedAgentSessionSources: () => Effect.die("unused"),
46 getThreadCheckpointContext: () => Effect.die("unused"),
47 getFullThreadDiffContext: () => Effect.die("unused"),
48 getThreadRuntimeContext: () => Effect.die("unused"),
49 getTurnStartMessage: () => Effect.die("unused"),
50 getThreadShellById: () => Effect.die("unused"),
51 getThreadDetailById: () => Effect.die("unused"),
52 getThreadDetailSnapshot: () => Effect.die("unused"),
53 searchThreads: () => Effect.succeed({ matches: [] }),
54 });
55​
56const makeTerminalManagerLayer = (
57 overrides: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
58) =>
59 Layer.succeed(TerminalManager.TerminalManager, {
60 ...overrides,
61 attachStream: () => Effect.die(new Error("unused")),
62 resize: () => Effect.void,
63 clear: () => Effect.void,
64 restart: () => Effect.die(new Error("unused")),
65 close: () => Effect.void,
66 subscribe: () => Effect.succeed(() => undefined),
67 subscribeMetadata: () => Effect.succeed(() => undefined),
68 });
69​
70const testLayer = (
71 project: OrchestrationProject,
72 terminal: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
73 settings = ServerSettings.layerTest(),
74) =>
75 ProjectSetupScriptRunner.layer.pipe(
76 Layer.provideMerge(makeProjectionSnapshotQueryLayer(project)),
77 Layer.provideMerge(makeTerminalManagerLayer(terminal)),
78 Layer.provide(settings),
79 );
80​
81describe("ProjectSetupScriptRunner", () => {
82 it.effect("runs the inherited machine setup action in the checkout's worktree", () => {
83 const open = vi.fn(() =>
84 Effect.succeed({
85 threadId: "thread-1",
86 terminalId: "setup-default-setup",
87 cwd: "/repo/worktrees/a",
88 worktreePath: "/repo/worktrees/a",
89 status: "running" as const,
90 pid: 123,
91 history: "",
92 exitCode: null,
93 exitSignal: null,
94 label: "setup-default-setup",
95 updatedAt: "2026-01-01T00:00:00.000Z",
96 }),
97 );
98 const write = vi.fn(() => Effect.void);
99 return Effect.gen(function* () {
100 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
101 const result = yield* runner.runForThread({
102 threadId: "thread-1",
103 projectId: "project-1",
104 worktreePath: "/repo/worktrees/a",
105 });
106 expect(result).toMatchObject({ status: "started", scriptId: "default-setup" });
107 expect(open).toHaveBeenCalledWith({
108 threadId: "thread-1",
109 terminalId: "setup-default-setup",
110 cwd: "/repo/worktrees/a",
111 worktreePath: "/repo/worktrees/a",
112 env: { T3CODE_PROJECT_ROOT: "/repo/project", T3CODE_WORKTREE_PATH: "/repo/worktrees/a" },
113 });
114 expect(write).toHaveBeenCalledWith({
115 threadId: "thread-1",
116 terminalId: "setup-default-setup",
117 data: "npm install\r",
118 });
119 }).pipe(
120 Effect.provide(
121 testLayer(
122 makeProject([]),
123 { open, write },
124 ServerSettings.layerTest({
125 defaultProjectScripts: [
126 {
127 id: "default-setup",
128 name: "Setup",
129 command: "npm install",
130 icon: "configure",
131 runOnWorktreeCreate: true,
132 },
133 ],
134 }),
135 ),
136 ),
137 );
138 });
139​
140 it.effect("returns no-script when no setup script exists", () => {
141 const open = vi.fn(() => Effect.die("unexpected open"));
142 const write = vi.fn(() => Effect.die("unexpected write"));
143 const project = makeProject([]);
144​
145 return Effect.gen(function* () {
146 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
147 const result = yield* runner.runForThread({
148 threadId: "thread-1",
149 projectId: "project-1",
150 worktreePath: "/repo/worktrees/a",
151 });
152​
153 expect(result).toEqual({ status: "no-script" });
154 expect(open).not.toHaveBeenCalled();
155 expect(write).not.toHaveBeenCalled();
156 }).pipe(Effect.provide(testLayer(project, { open, write })));
157 });
158​
159 it.effect(
160 "opens the deterministic setup terminal with worktree env and writes the command",
161 () => {
162 const open = vi.fn(() =>
163 Effect.succeed({
164 threadId: "thread-1",
165 terminalId: "setup-setup",
166 cwd: "/repo/worktrees/a",
167 worktreePath: "/repo/worktrees/a",
168 status: "running" as const,
169 pid: 123,
170 history: "",
171 exitCode: null,
172 exitSignal: null,
173 label: "setup-setup",
174 updatedAt: "2026-01-01T00:00:00.000Z",
175 }),
176 );
177 const write = vi.fn(() => Effect.void);
178 const project = makeProject([
179 {
180 id: "setup",
181 name: "Setup",
182 command: "bun install",
183 icon: "configure",
184 runOnWorktreeCreate: true,
185 },
186 ]);
187​
188 return Effect.gen(function* () {
189 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
190 const result = yield* runner.runForThread({
191 threadId: "thread-1",
192 projectCwd: "/repo/project",
193 worktreePath: "/repo/worktrees/a",
194 });
195​
196 expect(result).toEqual({
197 status: "started",
198 scriptId: "setup",
199 scriptName: "Setup",
200 terminalId: "setup-setup",
201 cwd: "/repo/worktrees/a",
202 });
203 expect(open).toHaveBeenCalledWith({
204 threadId: "thread-1",
205 terminalId: "setup-setup",
206 cwd: "/repo/worktrees/a",
207 worktreePath: "/repo/worktrees/a",
208 env: {
209 T3CODE_PROJECT_ROOT: "/repo/project",
210 T3CODE_WORKTREE_PATH: "/repo/worktrees/a",
211 },
212 });
213 expect(write).toHaveBeenCalledWith({
214 threadId: "thread-1",
215 terminalId: "setup-setup",
216 data: "bun install\r",
217 });
218 }).pipe(Effect.provide(testLayer(project, { open, write })));
219 },
220 );
221​
222 it.effect("keeps terminal failures as the exact cause of a structured operation error", () => {
223 const rootCause = new Error("stat failed");
224 const terminalError = new TerminalManager.TerminalCwdStatError({
225 cwd: "/repo/worktrees/a",
226 cause: rootCause,
227 });
228 const project = makeProject([
229 {
230 id: "setup",
231 name: "Setup",
232 command: "bun install",
233 icon: "configure",
234 runOnWorktreeCreate: true,
235 },
236 ]);
237​
238 return Effect.gen(function* () {
239 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
240 const error = yield* runner
241 .runForThread({
242 threadId: "thread-1",
243 projectId: "project-1",
244 worktreePath: "/repo/worktrees/a",
245 })
246 .pipe(Effect.flip);
247​
248 expect(isProjectSetupScriptOperationError(error)).toBe(true);
249 if (isProjectSetupScriptOperationError(error)) {
250 expect(error.operation).toBe("openTerminal");
251 expect(error.threadId).toBe("thread-1");
252 expect(error.projectId).toBe("project-1");
253 expect(error.worktreePath).toBe("/repo/worktrees/a");
254 expect(error.cause).toBe(terminalError);
255 expect(terminalError.cause).toBe(rootCause);
256 }
257 }).pipe(
258 Effect.provide(
259 testLayer(project, {
260 open: () => Effect.fail(terminalError),
261 write: () => Effect.die("unexpected write"),
262 }),
263 ),
264 );
265 });
266});