Open History to see the last commit
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 "vite-plus/test";
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 attachStream: () => Effect.die(new Error("unused")),
58 write,
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 ),
67 ),
68 ),
69 ),
70 );
71
72 const result = await Effect.runPromise(
73 runner.runForThread({
74 threadId: "thread-1",
75 projectId: "project-1",
76 worktreePath: "/repo/worktrees/a",
77 }),
78 );
79
80 expect(result).toEqual({ status: "no-script" });
81 expect(open).not.toHaveBeenCalled();
82 expect(write).not.toHaveBeenCalled();
83 });
84
85 it("opens the deterministic setup terminal with worktree env and writes the command", async () => {
86 const open = vi.fn(() =>
87 Effect.succeed({
88 threadId: "thread-1",
89 terminalId: "setup-setup",
90 cwd: "/repo/worktrees/a",
91 worktreePath: "/repo/worktrees/a",
92 status: "running" as const,
93 pid: 123,
94 history: "",
95 exitCode: null,
96 exitSignal: null,
97 label: "setup-setup",
98 updatedAt: "2026-01-01T00:00:00.000Z",
99 }),
100 );
101 const write = vi.fn(() => Effect.void);
102 const project = makeProject([
103 {
104 id: "setup",
105 name: "Setup",
106 command: "bun install",
107 icon: "configure",
108 runOnWorktreeCreate: true,
109 },
110 ]);
111 const runner = await Effect.runPromise(
112 Effect.service(ProjectSetupScriptRunner).pipe(
113 Effect.provide(
114 ProjectSetupScriptRunnerLive.pipe(
115 Layer.provideMerge(makeProjectionSnapshotQueryLayer(project)),
116 Layer.provideMerge(
117 Layer.succeed(TerminalManager, {
118 open,
119 attachStream: () => Effect.die(new Error("unused")),
120 write,
121 resize: () => Effect.void,
122 clear: () => Effect.void,
123 restart: () => Effect.die(new Error("unused")),
124 close: () => Effect.void,
125 subscribe: () => Effect.succeed(() => undefined),
126 subscribeMetadata: () => Effect.succeed(() => undefined),
127 }),
128 ),
129 ),
130 ),
131 ),
132 );
133
134 const result = await Effect.runPromise(
135 runner.runForThread({
136 threadId: "thread-1",
137 projectCwd: "/repo/project",
138 worktreePath: "/repo/worktrees/a",
139 }),
140 );
141
142 expect(result).toEqual({
143 status: "started",
144 scriptId: "setup",
145 scriptName: "Setup",
146 terminalId: "setup-setup",
147 cwd: "/repo/worktrees/a",
148 });
149 expect(open).toHaveBeenCalledWith({
150 threadId: "thread-1",
151 terminalId: "setup-setup",
152 cwd: "/repo/worktrees/a",
153 worktreePath: "/repo/worktrees/a",
154 env: {
155 T3CODE_PROJECT_ROOT: "/repo/project",
156 T3CODE_WORKTREE_PATH: "/repo/worktrees/a",
157 },
158 });
159 expect(write).toHaveBeenCalledWith({
160 threadId: "thread-1",
161 terminalId: "setup-setup",
162 data: "bun install\r",
163 });
164 });
165});