Open History to see the last commit
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 getThreadTurnState: () => Effect.die("unused"),
52 getForkSourceHead: () => Effect.die("unused"),
53 getThreadDetailById: () => Effect.die("unused"),
54 getThreadDetailSnapshot: () => Effect.die("unused"),
55 searchThreads: () => Effect.succeed({ matches: [] }),
56 });
57
58const makeTerminalManagerLayer = (
59 overrides: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
60) =>
61 Layer.succeed(TerminalManager.TerminalManager, {
62 ...overrides,
63 attachStream: () => Effect.die(new Error("unused")),
64 resize: () => Effect.void,
65 clear: () => Effect.void,
66 restart: () => Effect.die(new Error("unused")),
67 close: () => Effect.void,
68 subscribe: () => Effect.succeed(() => undefined),
69 subscribeMetadata: () => Effect.succeed(() => undefined),
70 });
71
72const testLayer = (
73 project: OrchestrationProject,
74 terminal: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
75 settings = ServerSettings.layerTest(),
76) =>
77 ProjectSetupScriptRunner.layer.pipe(
78 Layer.provideMerge(makeProjectionSnapshotQueryLayer(project)),
79 Layer.provideMerge(makeTerminalManagerLayer(terminal)),
80 Layer.provide(settings),
81 );
82
83describe("ProjectSetupScriptRunner", () => {
84 it.effect("runs the inherited machine setup action in the checkout's worktree", () => {
85 const open = vi.fn(() =>
86 Effect.succeed({
87 threadId: "thread-1",
88 terminalId: "setup-default-setup",
89 cwd: "/repo/worktrees/a",
90 worktreePath: "/repo/worktrees/a",
91 status: "running" as const,
92 pid: 123,
93 history: "",
94 exitCode: null,
95 exitSignal: null,
96 label: "setup-default-setup",
97 updatedAt: "2026-01-01T00:00:00.000Z",
98 }),
99 );
100 const write = vi.fn(() => Effect.void);
101 return Effect.gen(function* () {
102 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
103 const result = yield* runner.runForThread({
104 threadId: "thread-1",
105 projectId: "project-1",
106 worktreePath: "/repo/worktrees/a",
107 });
108 expect(result).toMatchObject({ status: "started", scriptId: "default-setup" });
109 expect(open).toHaveBeenCalledWith({
110 threadId: "thread-1",
111 terminalId: "setup-default-setup",
112 cwd: "/repo/worktrees/a",
113 worktreePath: "/repo/worktrees/a",
114 env: { T3CODE_PROJECT_ROOT: "/repo/project", T3CODE_WORKTREE_PATH: "/repo/worktrees/a" },
115 });
116 expect(write).toHaveBeenCalledWith({
117 threadId: "thread-1",
118 terminalId: "setup-default-setup",
119 data: "npm install\r",
120 });
121 }).pipe(
122 Effect.provide(
123 testLayer(
124 makeProject([]),
125 { open, write },
126 ServerSettings.layerTest({
127 defaultProjectScripts: [
128 {
129 id: "default-setup",
130 name: "Setup",
131 command: "npm install",
132 icon: "configure",
133 runOnWorktreeCreate: true,
134 },
135 ],
136 }),
137 ),
138 ),
139 );
140 });
141
142 it.effect("returns no-script when no setup script exists", () => {
143 const open = vi.fn(() => Effect.die("unexpected open"));
144 const write = vi.fn(() => Effect.die("unexpected write"));
145 const project = makeProject([]);
146
147 return Effect.gen(function* () {
148 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
149 const result = yield* runner.runForThread({
150 threadId: "thread-1",
151 projectId: "project-1",
152 worktreePath: "/repo/worktrees/a",
153 });
154
155 expect(result).toEqual({ status: "no-script" });
156 expect(open).not.toHaveBeenCalled();
157 expect(write).not.toHaveBeenCalled();
158 }).pipe(Effect.provide(testLayer(project, { open, write })));
159 });
160
161 it.effect(
162 "opens the deterministic setup terminal with worktree env and writes the command",
163 () => {
164 const open = vi.fn(() =>
165 Effect.succeed({
166 threadId: "thread-1",
167 terminalId: "setup-setup",
168 cwd: "/repo/worktrees/a",
169 worktreePath: "/repo/worktrees/a",
170 status: "running" as const,
171 pid: 123,
172 history: "",
173 exitCode: null,
174 exitSignal: null,
175 label: "setup-setup",
176 updatedAt: "2026-01-01T00:00:00.000Z",
177 }),
178 );
179 const write = vi.fn(() => Effect.void);
180 const project = makeProject([
181 {
182 id: "setup",
183 name: "Setup",
184 command: "bun install",
185 icon: "configure",
186 runOnWorktreeCreate: true,
187 },
188 ]);
189
190 return Effect.gen(function* () {
191 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
192 const result = yield* runner.runForThread({
193 threadId: "thread-1",
194 projectCwd: "/repo/project",
195 worktreePath: "/repo/worktrees/a",
196 });
197
198 expect(result).toEqual({
199 status: "started",
200 scriptId: "setup",
201 scriptName: "Setup",
202 terminalId: "setup-setup",
203 cwd: "/repo/worktrees/a",
204 });
205 expect(open).toHaveBeenCalledWith({
206 threadId: "thread-1",
207 terminalId: "setup-setup",
208 cwd: "/repo/worktrees/a",
209 worktreePath: "/repo/worktrees/a",
210 env: {
211 T3CODE_PROJECT_ROOT: "/repo/project",
212 T3CODE_WORKTREE_PATH: "/repo/worktrees/a",
213 },
214 });
215 expect(write).toHaveBeenCalledWith({
216 threadId: "thread-1",
217 terminalId: "setup-setup",
218 data: "bun install\r",
219 });
220 }).pipe(Effect.provide(testLayer(project, { open, write })));
221 },
222 );
223
224 it.effect("keeps terminal failures as the exact cause of a structured operation error", () => {
225 const rootCause = new Error("stat failed");
226 const terminalError = new TerminalManager.TerminalCwdStatError({
227 cwd: "/repo/worktrees/a",
228 cause: rootCause,
229 });
230 const project = makeProject([
231 {
232 id: "setup",
233 name: "Setup",
234 command: "bun install",
235 icon: "configure",
236 runOnWorktreeCreate: true,
237 },
238 ]);
239
240 return Effect.gen(function* () {
241 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
242 const error = yield* runner
243 .runForThread({
244 threadId: "thread-1",
245 projectId: "project-1",
246 worktreePath: "/repo/worktrees/a",
247 })
248 .pipe(Effect.flip);
249
250 expect(isProjectSetupScriptOperationError(error)).toBe(true);
251 if (isProjectSetupScriptOperationError(error)) {
252 expect(error.operation).toBe("openTerminal");
253 expect(error.threadId).toBe("thread-1");
254 expect(error.projectId).toBe("project-1");
255 expect(error.worktreePath).toBe("/repo/worktrees/a");
256 expect(error.cause).toBe(terminalError);
257 expect(terminalError.cause).toBe(rootCause);
258 }
259 }).pipe(
260 Effect.provide(
261 testLayer(project, {
262 open: () => Effect.fail(terminalError),
263 write: () => Effect.die("unexpected write"),
264 }),
265 ),
266 );
267 });
268});