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

ProjectSetupScriptRunner.test.ts

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