|
| 1 | +import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 2 | +import { describe, expect, it } from "@effect/vitest"; |
| 3 | +import { ProjectId, type TerminalEvent, type TerminalSessionSnapshot } from "@t3tools/contracts"; |
| 4 | +import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 5 | +import * as Deferred from "effect/Deferred"; |
| 6 | +import * as Effect from "effect/Effect"; |
| 7 | +import * as Fiber from "effect/Fiber"; |
| 8 | +import * as Layer from "effect/Layer"; |
| 9 | +import * as Queue from "effect/Queue"; |
| 10 | +import { TestClock } from "effect/testing"; |
| 11 | + |
| 12 | +import * as ServerSettings from "../serverSettings.ts"; |
| 13 | +import * as TerminalManager from "../terminal/Manager.ts"; |
| 14 | +import * as ProjectService from "./ProjectService.ts"; |
| 15 | +import * as ProjectSetupScriptRunner from "./ProjectSetupScriptRunner.ts"; |
| 16 | + |
| 17 | +const threadId = "thread-1"; |
| 18 | +const terminalId = "setup-setup"; |
| 19 | +const event = { threadId, terminalId, sequence: 1, createdAt: "2026-01-01T00:00:00.000Z" }; |
| 20 | + |
| 21 | +const openSnapshot = (history: string): TerminalSessionSnapshot => ({ |
| 22 | + threadId, |
| 23 | + terminalId, |
| 24 | + cwd: "/repo/.t3/worktrees/feature", |
| 25 | + worktreePath: null, |
| 26 | + status: "running", |
| 27 | + pid: 123, |
| 28 | + history, |
| 29 | + exitCode: null, |
| 30 | + exitSignal: null, |
| 31 | + label: "zsh", |
| 32 | + updatedAt: "2026-01-01T00:00:00.000Z", |
| 33 | +}); |
| 34 | + |
| 35 | +const makeHarness = Effect.fn(function* (options: { |
| 36 | + readonly history?: string; |
| 37 | + readonly opening?: Deferred.Deferred<void>; |
| 38 | +}) { |
| 39 | + const listeners = new Set<(event: TerminalEvent) => Effect.Effect<void>>(); |
| 40 | + const writes = yield* Queue.unbounded<string>(); |
| 41 | + const outputLines = yield* Queue.unbounded<string>(); |
| 42 | + const terminals = Layer.mock(TerminalManager.TerminalManager)({ |
| 43 | + open: () => |
| 44 | + (options.opening |
| 45 | + ? Deferred.succeed(options.opening, undefined).pipe(Effect.andThen(Effect.never)) |
| 46 | + : Effect.void |
| 47 | + ).pipe(Effect.as(openSnapshot(options.history ?? ""))), |
| 48 | + subscribe: (listener) => |
| 49 | + Effect.sync(() => { |
| 50 | + listeners.add(listener); |
| 51 | + return () => listeners.delete(listener); |
| 52 | + }), |
| 53 | + write: (input) => Queue.offer(writes, input.data).pipe(Effect.asVoid), |
| 54 | + closeIdle: () => Effect.void, |
| 55 | + }); |
| 56 | + const runner = yield* ProjectSetupScriptRunner.make.pipe( |
| 57 | + Effect.provide( |
| 58 | + Layer.mergeAll( |
| 59 | + terminals, |
| 60 | + Layer.mock(ProjectService.ProjectService)({}), |
| 61 | + ServerSettings.layerTest(), |
| 62 | + NodeCrypto.layer, |
| 63 | + ), |
| 64 | + ), |
| 65 | + Effect.provideService(HostProcess.Platform, "darwin"), |
| 66 | + Effect.provideService(HostProcess.Environment, { SHELL: "/bin/zsh" }), |
| 67 | + ); |
| 68 | + const start = runner.runForThread({ |
| 69 | + threadId, |
| 70 | + worktreePath: "/repo/.t3/worktrees/feature", |
| 71 | + observeCompletion: { onOutputLine: (line) => Queue.offer(outputLines, line) }, |
| 72 | + project: { |
| 73 | + id: ProjectId.make("project-1"), |
| 74 | + workspaceRoot: "/repo", |
| 75 | + scripts: [ |
| 76 | + { |
| 77 | + id: "setup", |
| 78 | + name: "Setup", |
| 79 | + command: "vp i", |
| 80 | + icon: "configure", |
| 81 | + runOnWorktreeCreate: true, |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + }); |
| 86 | + const emit = (terminalEvent: TerminalEvent) => |
| 87 | + Effect.forEach([...listeners], (listener) => listener(terminalEvent), { discard: true }); |
| 88 | + return { start, listeners, writes, outputLines, emit }; |
| 89 | +}); |
| 90 | + |
| 91 | +const setup = Effect.fn(function* (options: { readonly history?: string } = {}) { |
| 92 | + const harness = yield* makeHarness(options); |
| 93 | + const result = yield* harness.start; |
| 94 | + const completion = result.status === "started" ? result.completion : undefined; |
| 95 | + if (!completion) return yield* Effect.die("setup did not start an observed run"); |
| 96 | + return { ...harness, result, completion }; |
| 97 | +}); |
| 98 | + |
| 99 | +describe("ProjectSetupScriptRunner", () => { |
| 100 | + it.effect("shows a startup prompt and types only once the line editor starts", () => |
| 101 | + Effect.gen(function* () { |
| 102 | + const { result, writes, outputLines, emit } = yield* setup(); |
| 103 | + expect(result).toMatchObject({ status: "started", terminalId }); |
| 104 | + |
| 105 | + yield* emit({ ...event, type: "output", data: "Missing Node.\r\nInstall it? [y/N]: " }); |
| 106 | + yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" }); |
| 107 | + yield* TestClock.adjust("1 minute"); |
| 108 | + expect(yield* Queue.size(writes)).toBe(0); |
| 109 | + expect(yield* Queue.takeAll(outputLines)).toEqual(["Missing Node.", "Install it? [y/N]:"]); |
| 110 | + |
| 111 | + yield* emit({ ...event, type: "activity", hasRunningSubprocess: false, label: "zsh" }); |
| 112 | + yield* emit({ ...event, type: "output", data: "\r\n% \x1b[?2004h" }); |
| 113 | + expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/); |
| 114 | + }), |
| 115 | + ); |
| 116 | + |
| 117 | + it.effect("types into a quiet shell that has no line editor", () => |
| 118 | + Effect.gen(function* () { |
| 119 | + const { writes } = yield* setup(); |
| 120 | + yield* TestClock.adjust("3 seconds"); |
| 121 | + expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/); |
| 122 | + }), |
| 123 | + ); |
| 124 | + |
| 125 | + it.effect("ignores line editor state restored from an earlier shell", () => |
| 126 | + Effect.gen(function* () { |
| 127 | + const { writes, emit } = yield* setup({ history: "old run\r\n% \x1b[?2004h" }); |
| 128 | + yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" }); |
| 129 | + yield* TestClock.adjust("1 minute"); |
| 130 | + expect(yield* Queue.size(writes)).toBe(0); |
| 131 | + }), |
| 132 | + ); |
| 133 | + |
| 134 | + it.effect("forgets a busy startup child when the shell restarts", () => |
| 135 | + Effect.gen(function* () { |
| 136 | + const { writes, emit } = yield* setup(); |
| 137 | + yield* emit({ ...event, type: "activity", hasRunningSubprocess: true, label: "fnm" }); |
| 138 | + yield* TestClock.adjust("1 minute"); |
| 139 | + expect(yield* Queue.size(writes)).toBe(0); |
| 140 | + yield* emit({ ...event, type: "restarted", snapshot: openSnapshot("") }); |
| 141 | + // The restart lands inside a quiet window, so the next full window decides. |
| 142 | + yield* TestClock.adjust("6 seconds"); |
| 143 | + expect(yield* Queue.take(writes)).toMatch(/^ \( vp i\r\);/); |
| 144 | + }), |
| 145 | + ); |
| 146 | + |
| 147 | + it.effect("drops its terminal listeners when cancelled while the terminal opens", () => |
| 148 | + Effect.gen(function* () { |
| 149 | + const opening = yield* Deferred.make<void>(); |
| 150 | + const { start, listeners } = yield* makeHarness({ opening }); |
| 151 | + const fiber = yield* Effect.forkChild(start); |
| 152 | + yield* Deferred.await(opening); |
| 153 | + expect(listeners.size).toBe(2); |
| 154 | + yield* Fiber.interrupt(fiber); |
| 155 | + expect(listeners.size).toBe(0); |
| 156 | + }), |
| 157 | + ); |
| 158 | + |
| 159 | + it.effect("does not type into a shell that exits during startup", () => |
| 160 | + Effect.gen(function* () { |
| 161 | + const { completion, writes, emit } = yield* setup(); |
| 162 | + yield* emit({ ...event, type: "exited", exitCode: 1, exitSignal: null }); |
| 163 | + expect(yield* completion).toMatchObject({ exitCode: null }); |
| 164 | + yield* TestClock.adjust("1 minute"); |
| 165 | + expect(yield* Queue.size(writes)).toBe(0); |
| 166 | + }), |
| 167 | + ); |
| 168 | +}); |
0 commit comments