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