Open History to see the last commit
1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { assert, it, vi } from "@effect/vitest";
3import { ProjectId } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7
8import * as TerminalManager from "../terminal/Manager.ts";
9import * as ServerSettings from "../serverSettings.ts";
10import * as ProjectService from "./ProjectService.ts";
11import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts";
12
13it.effect("resolves setup scripts through the standalone project service", () => {
14 const open = vi.fn((input: Parameters<TerminalManager.TerminalManager["Service"]["open"]>[0]) =>
15 Effect.succeed({
16 threadId: input.threadId,
17 terminalId: input.terminalId,
18 cwd: input.cwd,
19 worktreePath: input.worktreePath ?? null,
20 status: "running" as const,
21 pid: 123,
22 history: "",
23 exitCode: null,
24 exitSignal: null,
25 label: "Shell",
26 updatedAt: "2026-06-20T00:00:00.000Z",
27 }),
28 );
29 const write = vi.fn(
30 (_input: Parameters<TerminalManager.TerminalManager["Service"]["write"]>[0]) => Effect.void,
31 );
32 const listeners: Array<Parameters<TerminalManager.TerminalManager["Service"]["subscribe"]>[0]> =
33 [];
34 const subscribe: TerminalManager.TerminalManager["Service"]["subscribe"] = (listener) =>
35 Effect.sync(() => {
36 listeners.push(listener);
37 return () => undefined;
38 });
39 const projectId = ProjectId.make("project:setup-runner-v2");
40 const project = {
41 id: projectId,
42 title: "Project",
43 workspaceRoot: "/repo",
44 repositoryIdentity: null,
45 faviconPath: null,
46 defaultModelSelection: null,
47 scripts: [
48 {
49 id: "setup",
50 name: "Setup",
51 command: "vp install",
52 icon: "configure" as const,
53 runOnWorktreeCreate: true,
54 },
55 ],
56 createdAt: "2026-06-20T00:00:00.000Z",
57 updatedAt: "2026-06-20T00:00:00.000Z",
58 deletedAt: null,
59 };
60 const layer = ProjectSetupScriptRunner.layer.pipe(
61 Layer.provide(
62 Layer.mergeAll(
63 Layer.mock(ProjectService.ProjectService)({
64 getById: () => Effect.succeed(Option.some(project)),
65 }),
66 Layer.mock(TerminalManager.TerminalManager)({ open, write, subscribe }),
67 ServerSettings.layerTest(),
68 NodeCrypto.layer,
69 ),
70 ),
71 );
72
73 return Effect.gen(function* () {
74 const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner;
75 const result = yield* runner.runForThread({
76 threadId: "thread-1",
77 projectId,
78 worktreePath: "/repo-worktree",
79 });
80 assert.deepEqual(result, {
81 status: "started",
82 async: true,
83 scriptId: "setup",
84 scriptName: "Setup",
85 scriptCommand: "vp install",
86 terminalId: "setup-setup",
87 cwd: "/repo-worktree",
88 });
89 assert.equal(open.mock.calls[0]?.[0].cwd, "/repo-worktree");
90 assert.deepEqual(open.mock.calls[0]?.[0].env, {
91 T3CODE_PROJECT_ROOT: "/repo",
92 T3CODE_WORKTREE_PATH: "/repo-worktree",
93 COLORTERM: "",
94 NO_COLOR: "1",
95 FORCE_COLOR: "0",
96 });
97 assert.equal(write.mock.calls[0]?.[0].data, "vp install\r");
98 const lines: string[] = [];
99 const observed = yield* runner.runForThread({
100 threadId: "thread-1",
101 projectId,
102 worktreePath: "/repo-worktree",
103 observeCompletion: {
104 onOutputLine: (line) =>
105 Effect.sync(() => {
106 lines.push(line);
107 }),
108 },
109 });
110 assert.equal(observed.status, "started");
111 const listener = listeners[0]!;
112 yield* listener({
113 type: "output",
114 threadId: "thread-1",
115 terminalId: "setup-setup",
116 data: "Downloading 10%\rDownloading 20%\r\nDone\n",
117 });
118 assert.deepEqual(lines, ["Downloading 10%", "Downloading 20%", "Done"]);
119 yield* listener({ type: "closed", threadId: "thread-1", terminalId: "setup-setup" });
120 }).pipe(Effect.provide(layer));
121});