import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import { assert, it, vi } from "@effect/vitest"; import { ProjectId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as TerminalManager from "../terminal/Manager.ts"; import * as ServerSettings from "../serverSettings.ts"; import * as ProjectService from "./ProjectService.ts"; import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts"; it.effect("resolves setup scripts through the standalone project service", () => { const open = vi.fn((input: Parameters[0]) => Effect.succeed({ threadId: input.threadId, terminalId: input.terminalId, cwd: input.cwd, worktreePath: input.worktreePath ?? null, status: "running" as const, pid: 123, history: "", exitCode: null, exitSignal: null, label: "Shell", updatedAt: "2026-06-20T00:00:00.000Z", }), ); const write = vi.fn( (_input: Parameters[0]) => Effect.void, ); const closeIdle = vi.fn( (_input: Parameters[0]) => Effect.void, ); const listeners: Array[0]> = []; const subscribe: TerminalManager.TerminalManager["Service"]["subscribe"] = (listener) => Effect.sync(() => { listeners.push(listener); return () => undefined; }); const projectId = ProjectId.make("project:setup-runner-v2"); const project = { id: projectId, title: "Project", workspaceRoot: "/repo", repositoryIdentity: null, faviconPath: null, defaultModelSelection: null, scripts: [ { id: "setup", name: "Setup", command: "vp install", icon: "configure" as const, runOnWorktreeCreate: true, }, { id: "clean", name: "Clean", command: "cargo clean", icon: "build" as const, runOnWorktreeCreate: false, runOnSettle: true, }, ], createdAt: "2026-06-20T00:00:00.000Z", updatedAt: "2026-06-20T00:00:00.000Z", deletedAt: null, }; const layer = ProjectSetupScriptRunner.layer.pipe( Layer.provide( Layer.mergeAll( Layer.mock(ProjectService.ProjectService)({ getById: () => Effect.succeed(Option.some(project)), }), Layer.mock(TerminalManager.TerminalManager)({ open, write, subscribe, closeIdle }), ServerSettings.layerTest(), NodeCrypto.layer, ), ), ); return Effect.gen(function* () { const runner = yield* ProjectSetupScriptRunner.ProjectSetupScriptRunner; const result = yield* runner.runForThread({ threadId: "thread-1", projectId, worktreePath: "/repo-worktree", }); assert.deepEqual(result, { status: "started", async: true, scriptId: "setup", scriptName: "Setup", scriptCommand: "vp install", terminalId: "setup-setup", cwd: "/repo-worktree", }); assert.equal(open.mock.calls[0]?.[0].cwd, "/repo-worktree"); assert.deepEqual(open.mock.calls[0]?.[0].env, { T3CODE_PROJECT_ROOT: "/repo", T3CODE_WORKTREE_PATH: "/repo-worktree", COLORTERM: "", NO_COLOR: "1", FORCE_COLOR: "0", }); assert.equal(write.mock.calls[0]?.[0].data, "vp install\r"); const lines: string[] = []; const observed = yield* runner.runForThread({ threadId: "thread-1", projectId, worktreePath: "/repo-worktree", observeCompletion: { onOutputLine: (line) => Effect.sync(() => { lines.push(line); }), }, }); assert.equal(observed.status, "started"); const listener = listeners[0]!; yield* listener({ type: "output", threadId: "thread-1", terminalId: "setup-setup", data: "Downloading 10%\rDownloading 20%\r\nDone\n", }); assert.deepEqual(lines, ["Downloading 10%", "Downloading 20%", "Done"]); yield* listener({ type: "closed", threadId: "thread-1", terminalId: "setup-setup" }); const settle = yield* runner.runForThread({ threadId: "thread-1", projectId, worktreePath: "/repo-worktree", trigger: "settle", }); const settleTerminalId = settle.status === "started" ? settle.terminalId : ""; assert.match(settleTerminalId, /^settle-clean-/); assert.equal(write.mock.calls.at(-1)?.[0].data, "cargo clean\r"); // A clean run closes its shell once the prompt is back, not at the // sentinel, so the prompt redraw is not taken for new activity. const observedSettle = yield* runner.runForThread({ threadId: "thread-1", projectId, worktreePath: "/repo-worktree", trigger: "settle", observeCompletion: {}, }); const observedTerminalId = observedSettle.status === "started" ? observedSettle.terminalId : ""; // Each settle gets its own shell, so a busy one is never typed into. assert.notEqual(observedTerminalId, settleTerminalId); const token = /__T3_SETUP_DONE___(\w+):/.exec(write.mock.calls.at(-1)?.[0].data ?? "")?.[1]; const settleListener = listeners.at(-1)!; const completion = yield* Effect.forkChild( observedSettle.status === "started" && observedSettle.completion ? observedSettle.completion : Effect.die("no completion"), ); yield* settleListener({ type: "output", threadId: "thread-1", terminalId: observedTerminalId, data: `\r\n__T3_SETUP_DONE___${token}:0\r\n`, }); yield* Effect.yieldNow; assert.equal(closeIdle.mock.calls.length, 0); yield* settleListener({ type: "output", threadId: "thread-1", terminalId: observedTerminalId, data: "$ ", }); assert.deepEqual((yield* Fiber.join(completion)).exitCode, 0); assert.deepEqual(closeIdle.mock.calls[0]?.[0], { threadId: "thread-1", terminalId: observedTerminalId, }); }).pipe(Effect.provide(layer)); });