|
1 | 1 | import { describe, expect, test } from "bun:test"; |
2 | 2 | import type { CliInput } from "../src/core/types"; |
3 | | -import { resolveRuntimeCliInput, shouldUsePagerMode, usesPipedPatchInput } from "../src/core/terminal"; |
| 3 | +import { openControllingTerminal, resolveRuntimeCliInput, shouldUsePagerMode, usesPipedPatchInput } from "../src/core/terminal"; |
4 | 4 |
|
5 | 5 | function createPatchInput(file?: string, pager = false): CliInput { |
6 | 6 | return { |
@@ -35,3 +35,65 @@ describe("terminal runtime defaults", () => { |
35 | 35 | expect(resolveRuntimeCliInput(input, true).options.pager).toBe(true); |
36 | 36 | }); |
37 | 37 | }); |
| 38 | + |
| 39 | +describe("controlling terminal attachment", () => { |
| 40 | + test("opens /dev/tty for read and write and closes both streams", () => { |
| 41 | + const calls: Array<[string, string]> = []; |
| 42 | + let stdinDestroyed = false; |
| 43 | + let stdoutDestroyed = false; |
| 44 | + |
| 45 | + const stdin = { |
| 46 | + destroy() { |
| 47 | + stdinDestroyed = true; |
| 48 | + }, |
| 49 | + } as never; |
| 50 | + const stdout = { |
| 51 | + destroy() { |
| 52 | + stdoutDestroyed = true; |
| 53 | + }, |
| 54 | + } as never; |
| 55 | + |
| 56 | + const controllingTerminal = openControllingTerminal({ |
| 57 | + openSync(path, flags) { |
| 58 | + calls.push([String(path), String(flags)]); |
| 59 | + return flags === "r" ? 11 : 12; |
| 60 | + }, |
| 61 | + createReadStream(fd) { |
| 62 | + expect(fd).toBe(11); |
| 63 | + return stdin; |
| 64 | + }, |
| 65 | + createWriteStream(fd) { |
| 66 | + expect(fd).toBe(12); |
| 67 | + return stdout; |
| 68 | + }, |
| 69 | + }); |
| 70 | + |
| 71 | + expect(controllingTerminal).not.toBeNull(); |
| 72 | + expect(calls).toEqual([ |
| 73 | + ["/dev/tty", "r"], |
| 74 | + ["/dev/tty", "w"], |
| 75 | + ]); |
| 76 | + expect(controllingTerminal?.stdin).toBe(stdin); |
| 77 | + expect(controllingTerminal?.stdout).toBe(stdout); |
| 78 | + |
| 79 | + controllingTerminal?.close(); |
| 80 | + expect(stdinDestroyed).toBe(true); |
| 81 | + expect(stdoutDestroyed).toBe(true); |
| 82 | + }); |
| 83 | + |
| 84 | + test("returns null when the controlling terminal cannot be opened", () => { |
| 85 | + const controllingTerminal = openControllingTerminal({ |
| 86 | + openSync() { |
| 87 | + throw new Error("no tty"); |
| 88 | + }, |
| 89 | + createReadStream() { |
| 90 | + throw new Error("unreachable"); |
| 91 | + }, |
| 92 | + createWriteStream() { |
| 93 | + throw new Error("unreachable"); |
| 94 | + }, |
| 95 | + }); |
| 96 | + |
| 97 | + expect(controllingTerminal).toBeNull(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments