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 TerminalManager from "../terminal/Manager.ts";
10import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts";
11
12const isProjectSetupScriptOperationError = Schema.is(
13 ProjectSetupScriptRunner.ProjectSetupScriptOperationError,
14);
15
16const makeProject = (scripts: OrchestrationProject["scripts"]): OrchestrationProject => ({
17 id: ProjectId.make("project-1"),
18 title: "Project",
19 workspaceRoot: "/repo/project",
20 defaultModelSelection: null,
21 scripts,
22 createdAt: "2026-01-01T00:00:00.000Z",
23 updatedAt: "2026-01-01T00:00:00.000Z",
24 deletedAt: null,
25});
26
27const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>
28 Layer.succeed(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
29 getCommandReadModel: () => Effect.die("unused"),
30 getSnapshot: () => Effect.die("unused"),
31 getShellSnapshot: () => Effect.die("unused"),
32 getArchivedShellSnapshot: () => Effect.die("unused"),
33 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 1 }),
34 getCounts: () => Effect.die("unused"),
35 getEventReplayStats: () => Effect.die("unused"),
36 getActiveProjectByWorkspaceRoot: (workspaceRoot) =>
37 Effect.succeed(
38 workspaceRoot === project.workspaceRoot ? Option.some(project) : Option.none(),
39 ),
40 getProjectShellById: (projectId) =>
41 Effect.succeed(projectId === project.id ? Option.some(project) : Option.none()),
42 getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),
43 getThreadCheckpointContext: () => Effect.die("unused"),
44 getFullThreadDiffContext: () => Effect.die("unused"),
45 getThreadShellById: () => Effect.die("unused"),
46 getThreadDetailById: () => Effect.die("unused"),
47 getThreadDetailSnapshot: () => Effect.die("unused"),
48 searchThreads: () => Effect.succeed({ matches: [] }),
49 });
50
51const makeTerminalManagerLayer = (
52 overrides: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
53) =>
54 Layer.succeed(TerminalManager.TerminalManager, {
55 ...overrides,
56 attachStream: () => Effect.die(new Error("unused")),
57 resize: () => Effect.void,
58 clear: () => Effect.void,
59 restart: () => Effect.die(new Error("unused")),
60 close: () => Effect.void,
61 subscribe: () => Effect.succeed(() => undefined),
62 subscribeMetadata: () => Effect.succeed(() => undefined),
63 });
64
65const testLayer = (
66 project: OrchestrationProject,
67 terminal: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
68) =>
69 ProjectSetupScriptRunner.layer.pipe(
70 Layer.provideMerge(makeProjectionSnapshotQueryLayer(project)),
71 Layer.provideMerge(makeTerminalManagerLayer(terminal)),
72 );
73
74describe("ProjectSetupScriptRunner", () => {
75 it.effect("returns no-script when no setup script exists", () => {
76 const open = vi.fn(() => Effect.die("unexpected open"));
77 const write = vi.fn(() => Effect.die("unexpected write"));
78 const project = makeProject([]);
79
80 return Effect.gen(function* () {
81 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
82 const result = yield* runner.runForThread({
83 threadId: "thread-1",
84 projectId: "project-1",
85 worktreePath: "/repo/worktrees/a",
86 });
87
88 expect(result).toEqual({ status: "no-script" });
89 expect(open).not.toHaveBeenCalled();
90 expect(write).not.toHaveBeenCalled();
91 }).pipe(Effect.provide(testLayer(project, { open, write })));
92 });
93
94 it.effect(
95 "opens the deterministic setup terminal with worktree env and writes the command",
96 () => {
97 const open = vi.fn(() =>
98 Effect.succeed({
99 threadId: "thread-1",
100 terminalId: "setup-setup",
101 cwd: "/repo/worktrees/a",
102 worktreePath: "/repo/worktrees/a",
103 status: "running" as const,
104 pid: 123,
105 history: "",
106 exitCode: null,
107 exitSignal: null,
108 label: "setup-setup",
109 updatedAt: "2026-01-01T00:00:00.000Z",
110 }),
111 );
112 const write = vi.fn(() => Effect.void);
113 const project = makeProject([
114 {
115 id: "setup",
116 name: "Setup",
117 command: "bun install",
118 icon: "configure",
119 runOnWorktreeCreate: true,
120 },
121 ]);
122
123 return Effect.gen(function* () {
124 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
125 const result = yield* runner.runForThread({
126 threadId: "thread-1",
127 projectCwd: "/repo/project",
128 worktreePath: "/repo/worktrees/a",
129 });
130
131 expect(result).toEqual({
132 status: "started",
133 scriptId: "setup",
134 scriptName: "Setup",
135 terminalId: "setup-setup",
136 cwd: "/repo/worktrees/a",
137 });
138 expect(open).toHaveBeenCalledWith({
139 threadId: "thread-1",
140 terminalId: "setup-setup",
141 cwd: "/repo/worktrees/a",
142 worktreePath: "/repo/worktrees/a",
143 env: {
144 T3CODE_PROJECT_ROOT: "/repo/project",
145 T3CODE_WORKTREE_PATH: "/repo/worktrees/a",
146 },
147 });
148 expect(write).toHaveBeenCalledWith({
149 threadId: "thread-1",
150 terminalId: "setup-setup",
151 data: "bun install\r",
152 });
153 }).pipe(Effect.provide(testLayer(project, { open, write })));
154 },
155 );
156
157 it.effect("keeps terminal failures as the exact cause of a structured operation error", () => {
158 const rootCause = new Error("stat failed");
159 const terminalError = new TerminalManager.TerminalCwdStatError({
160 cwd: "/repo/worktrees/a",
161 cause: rootCause,
162 });
163 const project = makeProject([
164 {
165 id: "setup",
166 name: "Setup",
167 command: "bun install",
168 icon: "configure",
169 runOnWorktreeCreate: true,
170 },
171 ]);
172
173 return Effect.gen(function* () {
174 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
175 const error = yield* runner
176 .runForThread({
177 threadId: "thread-1",
178 projectId: "project-1",
179 worktreePath: "/repo/worktrees/a",
180 })
181 .pipe(Effect.flip);
182
183 expect(isProjectSetupScriptOperationError(error)).toBe(true);
184 if (isProjectSetupScriptOperationError(error)) {
185 expect(error.operation).toBe("openTerminal");
186 expect(error.threadId).toBe("thread-1");
187 expect(error.projectId).toBe("project-1");
188 expect(error.worktreePath).toBe("/repo/worktrees/a");
189 expect(error.cause).toBe(terminalError);
190 expect(terminalError.cause).toBe(rootCause);
191 }
192 }).pipe(
193 Effect.provide(
194 testLayer(project, {
195 open: () => Effect.fail(terminalError),
196 write: () => Effect.die("unexpected write"),
197 }),
198 ),
199 );
200 });
201});