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

ProjectSetupScriptRunner.test.ts

160 lines5.3 KB

1import { ProjectId, type OrchestrationProject } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import { describe, expect, it, vi } from "vitest";
6​
7import { ProjectionSnapshotQuery } from "../../orchestration/Services/ProjectionSnapshotQuery.ts";
8import { TerminalManager } from "../../terminal/Services/Manager.ts";
9import { ProjectSetupScriptRunner } from "../Services/ProjectSetupScriptRunner.ts";
10import { ProjectSetupScriptRunnerLive } from "./ProjectSetupScriptRunner.ts";
11​
12const makeProject = (scripts: OrchestrationProject["scripts"]): OrchestrationProject => ({
13 id: ProjectId.make("project-1"),
14 title: "Project",
15 workspaceRoot: "/repo/project",
16 defaultModelSelection: null,
17 scripts,
18 createdAt: "2026-01-01T00:00:00.000Z",
19 updatedAt: "2026-01-01T00:00:00.000Z",
20 deletedAt: null,
21});
22​
23const makeProjectionSnapshotQueryLayer = (project: OrchestrationProject) =>
24 Layer.succeed(ProjectionSnapshotQuery, {
25 getCommandReadModel: () => Effect.die("unused"),
26 getSnapshot: () => Effect.die("unused"),
27 getShellSnapshot: () => Effect.die("unused"),
28 getArchivedShellSnapshot: () => Effect.die("unused"),
29 getSnapshotSequence: () => Effect.succeed({ snapshotSequence: 1 }),
30 getCounts: () => Effect.die("unused"),
31 getActiveProjectByWorkspaceRoot: (workspaceRoot) =>
32 Effect.succeed(
33 workspaceRoot === project.workspaceRoot ? Option.some(project) : Option.none(),
34 ),
35 getProjectShellById: (projectId) =>
36 Effect.succeed(projectId === project.id ? Option.some(project) : Option.none()),
37 getFirstActiveThreadIdByProjectId: () => Effect.die("unused"),
38 getThreadCheckpointContext: () => Effect.die("unused"),
39 getFullThreadDiffContext: () => Effect.die("unused"),
40 getThreadShellById: () => Effect.die("unused"),
41 getThreadDetailById: () => Effect.die("unused"),
42 });
43​
44describe("ProjectSetupScriptRunner", () => {
45 it("returns no-script when no setup script exists", async () => {
46 const open = vi.fn();
47 const write = vi.fn();
48 const project = makeProject([]);
49 const runner = await Effect.runPromise(
50 Effect.service(ProjectSetupScriptRunner).pipe(
51 Effect.provide(
52 ProjectSetupScriptRunnerLive.pipe(
53 Layer.provideMerge(makeProjectionSnapshotQueryLayer(project)),
54 Layer.provideMerge(
55 Layer.succeed(TerminalManager, {
56 open,
57 write,
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 }),
64 ),
65 ),
66 ),
67 ),
68 );
69​
70 const result = await Effect.runPromise(
71 runner.runForThread({
72 threadId: "thread-1",
73 projectId: "project-1",
74 worktreePath: "/repo/worktrees/a",
75 }),
76 );
77​
78 expect(result).toEqual({ status: "no-script" });
79 expect(open).not.toHaveBeenCalled();
80 expect(write).not.toHaveBeenCalled();
81 });
82​
83 it("opens the deterministic setup terminal with worktree env and writes the command", async () => {
84 const open = vi.fn(() =>
85 Effect.succeed({
86 threadId: "thread-1",
87 terminalId: "setup-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 updatedAt: "2026-01-01T00:00:00.000Z",
96 }),
97 );
98 const write = vi.fn(() => Effect.void);
99 const project = makeProject([
100 {
101 id: "setup",
102 name: "Setup",
103 command: "bun install",
104 icon: "configure",
105 runOnWorktreeCreate: true,
106 },
107 ]);
108 const runner = await Effect.runPromise(
109 Effect.service(ProjectSetupScriptRunner).pipe(
110 Effect.provide(
111 ProjectSetupScriptRunnerLive.pipe(
112 Layer.provideMerge(makeProjectionSnapshotQueryLayer(project)),
113 Layer.provideMerge(
114 Layer.succeed(TerminalManager, {
115 open,
116 write,
117 resize: () => Effect.void,
118 clear: () => Effect.void,
119 restart: () => Effect.die(new Error("unused")),
120 close: () => Effect.void,
121 subscribe: () => Effect.succeed(() => undefined),
122 }),
123 ),
124 ),
125 ),
126 ),
127 );
128​
129 const result = await Effect.runPromise(
130 runner.runForThread({
131 threadId: "thread-1",
132 projectCwd: "/repo/project",
133 worktreePath: "/repo/worktrees/a",
134 }),
135 );
136​
137 expect(result).toEqual({
138 status: "started",
139 scriptId: "setup",
140 scriptName: "Setup",
141 terminalId: "setup-setup",
142 cwd: "/repo/worktrees/a",
143 });
144 expect(open).toHaveBeenCalledWith({
145 threadId: "thread-1",
146 terminalId: "setup-setup",
147 cwd: "/repo/worktrees/a",
148 worktreePath: "/repo/worktrees/a",
149 env: {
150 T3CODE_PROJECT_ROOT: "/repo/project",
151 T3CODE_WORKTREE_PATH: "/repo/worktrees/a",
152 },
153 });
154 expect(write).toHaveBeenCalledWith({
155 threadId: "thread-1",
156 terminalId: "setup-setup",
157 data: "bun install\r",
158 });
159 });
160});