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 listThreadIdsByWorktreePath: () => Effect.die("unused"),
49 searchThreads: () => Effect.succeed({ matches: [] }),
50 });
51
52const makeTerminalManagerLayer = (
53 overrides: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
54) =>
55 Layer.succeed(TerminalManager.TerminalManager, {
56 ...overrides,
57 attachStream: () => Effect.die(new Error("unused")),
58 resize: () => Effect.void,
59 clear: () => Effect.void,
60 restart: () => Effect.die(new Error("unused")),
61 close: () => Effect.void,
62 subscribe: () => Effect.succeed(() => undefined),
63 subscribeMetadata: () => Effect.succeed(() => undefined),
64 });
65
66const testLayer = (
67 project: OrchestrationProject,
68 terminal: Pick<TerminalManager.TerminalManager["Service"], "open" | "write">,
69) =>
70 ProjectSetupScriptRunner.layer.pipe(
71 Layer.provideMerge(makeProjectionSnapshotQueryLayer(project)),
72 Layer.provideMerge(makeTerminalManagerLayer(terminal)),
73 );
74
75describe("ProjectSetupScriptRunner", () => {
76 it.effect("returns no-script when no setup script exists", () => {
77 const open = vi.fn(() => Effect.die("unexpected open"));
78 const write = vi.fn(() => Effect.die("unexpected write"));
79 const project = makeProject([]);
80
81 return Effect.gen(function* () {
82 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
83 const result = yield* runner.runForThread({
84 threadId: "thread-1",
85 projectId: "project-1",
86 worktreePath: "/repo/worktrees/a",
87 });
88
89 expect(result).toEqual({ status: "no-script" });
90 expect(open).not.toHaveBeenCalled();
91 expect(write).not.toHaveBeenCalled();
92 }).pipe(Effect.provide(testLayer(project, { open, write })));
93 });
94
95 it.effect(
96 "opens the deterministic setup terminal with worktree env and writes the command",
97 () => {
98 const open = vi.fn(() =>
99 Effect.succeed({
100 threadId: "thread-1",
101 terminalId: "setup-setup",
102 cwd: "/repo/worktrees/a",
103 worktreePath: "/repo/worktrees/a",
104 status: "running" as const,
105 pid: 123,
106 history: "",
107 exitCode: null,
108 exitSignal: null,
109 label: "setup-setup",
110 updatedAt: "2026-01-01T00:00:00.000Z",
111 }),
112 );
113 const write = vi.fn(() => Effect.void);
114 const project = makeProject([
115 {
116 id: "setup",
117 name: "Setup",
118 command: "bun install",
119 icon: "configure",
120 runOnWorktreeCreate: true,
121 },
122 ]);
123
124 return Effect.gen(function* () {
125 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
126 const result = yield* runner.runForThread({
127 threadId: "thread-1",
128 projectCwd: "/repo/project",
129 worktreePath: "/repo/worktrees/a",
130 });
131
132 expect(result).toEqual({
133 status: "started",
134 scriptId: "setup",
135 scriptName: "Setup",
136 terminalId: "setup-setup",
137 cwd: "/repo/worktrees/a",
138 });
139 expect(open).toHaveBeenCalledWith({
140 threadId: "thread-1",
141 terminalId: "setup-setup",
142 cwd: "/repo/worktrees/a",
143 worktreePath: "/repo/worktrees/a",
144 env: {
145 T3CODE_PROJECT_ROOT: "/repo/project",
146 T3CODE_WORKTREE_PATH: "/repo/worktrees/a",
147 },
148 });
149 expect(write).toHaveBeenCalledWith({
150 threadId: "thread-1",
151 terminalId: "setup-setup",
152 data: "bun install\r",
153 });
154 }).pipe(Effect.provide(testLayer(project, { open, write })));
155 },
156 );
157
158 it.effect("keeps terminal failures as the exact cause of a structured operation error", () => {
159 const rootCause = new Error("stat failed");
160 const terminalError = new TerminalManager.TerminalCwdStatError({
161 cwd: "/repo/worktrees/a",
162 cause: rootCause,
163 });
164 const project = makeProject([
165 {
166 id: "setup",
167 name: "Setup",
168 command: "bun install",
169 icon: "configure",
170 runOnWorktreeCreate: true,
171 },
172 ]);
173
174 return Effect.gen(function* () {
175 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
176 const error = yield* runner
177 .runForThread({
178 threadId: "thread-1",
179 projectId: "project-1",
180 worktreePath: "/repo/worktrees/a",
181 })
182 .pipe(Effect.flip);
183
184 expect(isProjectSetupScriptOperationError(error)).toBe(true);
185 if (isProjectSetupScriptOperationError(error)) {
186 expect(error.operation).toBe("openTerminal");
187 expect(error.threadId).toBe("thread-1");
188 expect(error.projectId).toBe("project-1");
189 expect(error.worktreePath).toBe("/repo/worktrees/a");
190 expect(error.cause).toBe(terminalError);
191 expect(terminalError.cause).toBe(rootCause);
192 }
193 }).pipe(
194 Effect.provide(
195 testLayer(project, {
196 open: () => Effect.fail(terminalError),
197 write: () => Effect.die("unexpected write"),
198 }),
199 ),
200 );
201 });
202});