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

ProjectSetupScriptRunner.test.ts

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