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