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

ProjectSetupScriptRunner.test.ts

166 lines5.2 KB

1import { Effect, Layer, Stream } from "effect";
2import { describe, expect, it, vi } from "vitest";
3import type { OrchestrationReadModel } from "@t3tools/contracts";
4​
5import { OrchestrationEngineService } from "../../orchestration/Services/OrchestrationEngine.ts";
6import { TerminalManager } from "../../terminal/Services/Manager.ts";
7import { ProjectSetupScriptRunner } from "../Services/ProjectSetupScriptRunner.ts";
8import { ProjectSetupScriptRunnerLive } from "./ProjectSetupScriptRunner.ts";
9​
10const emptySnapshot = (
11 scripts: OrchestrationReadModel["projects"][number]["scripts"],
12): OrchestrationReadModel =>
13 ({
14 snapshotSequence: 1,
15 updatedAt: "2026-01-01T00:00:00.000Z",
16 projects: [
17 {
18 id: "project-1",
19 title: "Project",
20 workspaceRoot: "/repo/project",
21 defaultModelSelection: null,
22 scripts,
23 createdAt: "2026-01-01T00:00:00.000Z",
24 updatedAt: "2026-01-01T00:00:00.000Z",
25 deletedAt: null,
26 },
27 ],
28 threads: [],
29 providerSessions: [],
30 providerStatuses: [],
31 pendingApprovals: [],
32 latestTurnByThreadId: {},
33 }) as unknown as OrchestrationReadModel;
34​
35describe("ProjectSetupScriptRunner", () => {
36 it("returns no-script when no setup script exists", async () => {
37 const open = vi.fn();
38 const write = vi.fn();
39 const runner = await Effect.runPromise(
40 Effect.service(ProjectSetupScriptRunner).pipe(
41 Effect.provide(
42 ProjectSetupScriptRunnerLive.pipe(
43 Layer.provideMerge(
44 Layer.succeed(OrchestrationEngineService, {
45 getReadModel: () => Effect.succeed(emptySnapshot([])),
46 readEvents: () => Stream.empty,
47 dispatch: () => Effect.die(new Error("unused")),
48 streamDomainEvents: Stream.empty,
49 }),
50 ),
51 Layer.provideMerge(
52 Layer.succeed(TerminalManager, {
53 open,
54 write,
55 resize: () => Effect.void,
56 clear: () => Effect.void,
57 restart: () => Effect.die(new Error("unused")),
58 close: () => Effect.void,
59 subscribe: () => Effect.succeed(() => undefined),
60 }),
61 ),
62 ),
63 ),
64 ),
65 );
66​
67 const result = await Effect.runPromise(
68 runner.runForThread({
69 threadId: "thread-1",
70 projectId: "project-1",
71 worktreePath: "/repo/worktrees/a",
72 }),
73 );
74​
75 expect(result).toEqual({ status: "no-script" });
76 expect(open).not.toHaveBeenCalled();
77 expect(write).not.toHaveBeenCalled();
78 });
79​
80 it("opens the deterministic setup terminal with worktree env and writes the command", async () => {
81 const open = vi.fn(() =>
82 Effect.succeed({
83 threadId: "thread-1",
84 terminalId: "setup-setup",
85 cwd: "/repo/worktrees/a",
86 worktreePath: "/repo/worktrees/a",
87 status: "running" as const,
88 pid: 123,
89 history: "",
90 exitCode: null,
91 exitSignal: null,
92 updatedAt: "2026-01-01T00:00:00.000Z",
93 }),
94 );
95 const write = vi.fn(() => Effect.void);
96 const runner = await Effect.runPromise(
97 Effect.service(ProjectSetupScriptRunner).pipe(
98 Effect.provide(
99 ProjectSetupScriptRunnerLive.pipe(
100 Layer.provideMerge(
101 Layer.succeed(OrchestrationEngineService, {
102 getReadModel: () =>
103 Effect.succeed(
104 emptySnapshot([
105 {
106 id: "setup",
107 name: "Setup",
108 command: "bun install",
109 icon: "configure",
110 runOnWorktreeCreate: true,
111 },
112 ]),
113 ),
114 readEvents: () => Stream.empty,
115 dispatch: () => Effect.die(new Error("unused")),
116 streamDomainEvents: Stream.empty,
117 }),
118 ),
119 Layer.provideMerge(
120 Layer.succeed(TerminalManager, {
121 open,
122 write,
123 resize: () => Effect.void,
124 clear: () => Effect.void,
125 restart: () => Effect.die(new Error("unused")),
126 close: () => Effect.void,
127 subscribe: () => Effect.succeed(() => undefined),
128 }),
129 ),
130 ),
131 ),
132 ),
133 );
134​
135 const result = await Effect.runPromise(
136 runner.runForThread({
137 threadId: "thread-1",
138 projectCwd: "/repo/project",
139 worktreePath: "/repo/worktrees/a",
140 }),
141 );
142​
143 expect(result).toEqual({
144 status: "started",
145 scriptId: "setup",
146 scriptName: "Setup",
147 terminalId: "setup-setup",
148 cwd: "/repo/worktrees/a",
149 });
150 expect(open).toHaveBeenCalledWith({
151 threadId: "thread-1",
152 terminalId: "setup-setup",
153 cwd: "/repo/worktrees/a",
154 worktreePath: "/repo/worktrees/a",
155 env: {
156 T3CODE_PROJECT_ROOT: "/repo/project",
157 T3CODE_WORKTREE_PATH: "/repo/worktrees/a",
158 },
159 });
160 expect(write).toHaveBeenCalledWith({
161 threadId: "thread-1",
162 terminalId: "setup-setup",
163 data: "bun install\r",
164 });
165 });
166});