The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeTerminal from "@effect/platform-node-shared/NodeTerminal" |
| 2 | import { assert, describe, it } from "@effect/vitest" |
| 3 | import * as Effect from "effect/Effect" |
| 4 | import * as Queue from "effect/Queue" |
| 5 | import { spawn, spawnSync } from "node:child_process" |
| 6 | import { join } from "node:path" |
| 7 | |
| 8 | const resize = (columns: number, rows: number) => |
| 9 | Effect.sync(() => { |
| 10 | Object.assign(process.stdout, { columns, rows }) |
| 11 | process.stdout.emit("resize") |
| 12 | }) |
| 13 | |
| 14 | const mockSize = (columns: number, rows: number) => |
| 15 | Effect.acquireRelease( |
| 16 | Effect.sync(() => { |
| 17 | const original = { columns: process.stdout.columns, rows: process.stdout.rows } |
| 18 | Object.assign(process.stdout, { columns, rows }) |
| 19 | return original |
| 20 | }), |
| 21 | (original) => Effect.sync(() => Object.assign(process.stdout, original)) |
| 22 | ) |
| 23 | |
| 24 | const fixture = join(__dirname, "fixtures", "node-terminal.ts") |
| 25 | |
| 26 | const runFixture = (mode: string, input: string) => |
| 27 | spawnSync(process.execPath, [fixture, mode], { |
| 28 | encoding: "utf8", |
| 29 | input, |
| 30 | // Includes Node startup and loading the fixture imports under CI worker load. |
| 31 | timeout: 10_000 |
| 32 | }) |
| 33 | |
| 34 | const assertResult = (mode: string, input: string, expected: string) => { |
| 35 | const result = runFixture(mode, input) |
| 36 | assert.isUndefined(result.error) |
| 37 | assert.strictEqual(result.status, 0, result.stderr) |
| 38 | assert.isTrue(result.stderr.includes(`RESULT ${expected}`), result.stderr) |
| 39 | } |
| 40 | |
| 41 | const assertOpenResult = (mode: string, input: string, expected: string) => |
| 42 | Effect.callback<void>((resume) => { |
| 43 | const child = spawn(process.execPath, [fixture, mode]) |
| 44 | let stderr = "" |
| 45 | child.stderr.setEncoding("utf8") |
| 46 | child.stderr.on("data", (data) => { |
| 47 | stderr += data |
| 48 | if (stderr.includes("RESULT ")) { |
| 49 | child.stdin.end() |
| 50 | } |
| 51 | }) |
| 52 | child.on("exit", (code) => { |
| 53 | resume( |
| 54 | code === 0 && stderr.includes(`RESULT ${expected}`) |
| 55 | ? Effect.void |
| 56 | : Effect.die(new Error(stderr)) |
| 57 | ) |
| 58 | }) |
| 59 | child.stdin.write(input) |
| 60 | return Effect.sync(() => child.kill()) |
| 61 | }) |
| 62 | |
| 63 | const runOpenSelect = (input: string, customQuit = false) => |
| 64 | Effect.callback<{ stdout: string; stderr: string }>((resume) => { |
| 65 | const child = spawn(process.execPath, [fixture, customQuit ? "select-custom-quit" : "select"]) |
| 66 | let stdout = "" |
| 67 | let stderr = "" |
| 68 | let submitted = false |
| 69 | child.stdout.setEncoding("utf8") |
| 70 | child.stderr.setEncoding("utf8") |
| 71 | child.stdout.on("data", (data) => { |
| 72 | stdout += data |
| 73 | // Wait for bare Esc to be decoded before submitting the custom prompt. |
| 74 | if (customQuit && !submitted && stdout.includes("\x07")) { |
| 75 | submitted = true |
| 76 | child.stdin.write("\r") |
| 77 | } |
| 78 | }) |
| 79 | child.stderr.on("data", (data) => { |
| 80 | stderr += data |
| 81 | if (stderr.includes("RESULT ")) { |
| 82 | child.stdin.end() |
| 83 | } |
| 84 | }) |
| 85 | child.on("error", (error) => resume(Effect.die(error))) |
| 86 | child.on("close", (code) => { |
| 87 | resume(code === 0 ? Effect.succeed({ stdout, stderr }) : Effect.die(new Error(stderr))) |
| 88 | }) |
| 89 | child.stdin.write(input) |
| 90 | return Effect.sync(() => child.kill()) |
| 91 | }) |
| 92 | |
| 93 | // spawnSync blocks the Vitest worker; concurrent tests share a running timeout |
| 94 | // while waiting for other fixture processes to finish. |
| 95 | describe("NodeTerminal", { concurrent: false, timeout: 15_000 }, () => { |
| 96 | it.effect("bare Esc cancels Select with QuitError and restores the cursor while stdin remains open", () => |
| 97 | Effect.gen(function*() { |
| 98 | const { stderr, stdout } = yield* runOpenSelect("\x1b") |
| 99 | assert.include(stderr, "RESULT \"QuitError\"", `Bare Esc output: ${JSON.stringify(stdout)}`) |
| 100 | assert.include(stdout, "\x1b[?25l") |
| 101 | assert.isTrue(stdout.endsWith("\x1b[?25h"), stdout) |
| 102 | })) |
| 103 | |
| 104 | it.effect("an arrow escape sequence navigates Select and Enter submits", () => |
| 105 | Effect.gen(function*() { |
| 106 | const { stderr } = yield* runOpenSelect("\x1b[B\r") |
| 107 | assert.include(stderr, "RESULT \"banana\"") |
| 108 | })) |
| 109 | |
| 110 | it.effect("an explicit shouldQuit predicate can keep Esc non-cancelling", () => |
| 111 | Effect.gen(function*() { |
| 112 | const { stderr, stdout } = yield* runOpenSelect("\x1b", true) |
| 113 | assert.include(stdout, "\x07") |
| 114 | assert.include(stderr, "RESULT \"apple\"") |
| 115 | })) |
| 116 | |
| 117 | it("does not install a readline interface until the terminal is used", () => { |
| 118 | assertResult("unused", "", "{\"dataListeners\":0}") |
| 119 | }) |
| 120 | |
| 121 | it("fails a prompt with QuitError after piped input is exhausted", () => { |
| 122 | assertResult("prompts", "y\n", "{\"first\":true,\"second\":\"QuitError\"}") |
| 123 | }) |
| 124 | |
| 125 | it("delivers buffered keypresses before ending the input queue", () => { |
| 126 | assertResult("read-input", "yn", "{\"first\":\"y\",\"second\":\"n\",\"ended\":true}") |
| 127 | }) |
| 128 | |
| 129 | it("flushes an unterminated line before failing readLine with QuitError at EOF", () => { |
| 130 | assertResult("read-line", "last line", "{\"first\":\"last line\",\"second\":\"QuitError\"}") |
| 131 | }) |
| 132 | |
| 133 | it("preserves lines buffered between sequential readLine calls", () => { |
| 134 | assertResult("read-lines", "first\nsecond\n", "{\"first\":\"first\",\"second\":\"second\"}") |
| 135 | }) |
| 136 | |
| 137 | it("fails readLine with QuitError when stdin ended before initialization", () => { |
| 138 | assertResult("read-line-after-end", "", "\"QuitError\"") |
| 139 | }) |
| 140 | |
| 141 | it("leaves a TTY in cooked mode for readLine", () => { |
| 142 | assertResult("tty-read-line", "line\n", "{\"line\":\"line\",\"rawMode\":[]}") |
| 143 | }) |
| 144 | |
| 145 | it("enables raw mode on a TTY while reading key input", () => { |
| 146 | assertResult("tty-read-input", "y", "{\"input\":\"y\",\"rawMode\":[true,false]}") |
| 147 | }) |
| 148 | |
| 149 | it.effect("disposes readline after its idle TTL", () => |
| 150 | assertOpenResult( |
| 151 | "read-line-disposed", |
| 152 | "line\n", |
| 153 | "{\"line\":\"line\",\"duringTtl\":1,\"dataListeners\":0}" |
| 154 | )) |
| 155 | |
| 156 | it.effect("size offers the current size, then only the latest resize", () => |
| 157 | Effect.gen(function*() { |
| 158 | yield* mockSize(80, 24) |
| 159 | const terminal = yield* NodeTerminal.make() |
| 160 | const sizes = yield* terminal.size |
| 161 | assert.deepStrictEqual(yield* Queue.take(sizes), { columns: 80, rows: 24 }) |
| 162 | yield* resize(100, 30) |
| 163 | yield* resize(120, 50) |
| 164 | assert.deepStrictEqual(yield* Queue.takeAll(sizes), [{ columns: 120, rows: 50 }]) |
| 165 | })) |
| 166 | |
| 167 | it.effect("size removes its resize listener when the scope closes", () => |
| 168 | Effect.gen(function*() { |
| 169 | const terminal = yield* NodeTerminal.make() |
| 170 | const listeners = process.stdout.listenerCount("resize") |
| 171 | yield* terminal.size.pipe( |
| 172 | Effect.tap(() => Effect.sync(() => assert.strictEqual(process.stdout.listenerCount("resize"), listeners + 1))), |
| 173 | Effect.scoped |
| 174 | ) |
| 175 | assert.strictEqual(process.stdout.listenerCount("resize"), listeners) |
| 176 | })) |
| 177 | }) |