|
| 1 | +import assert from "node:assert"; |
| 2 | +import { describe, it } from "node:test"; |
| 3 | +import { ProcessTerminal } from "../src/terminal.js"; |
| 4 | + |
| 5 | +function withEnv(name: string, value: string | undefined, fn: () => void): void { |
| 6 | + const previous = process.env[name]; |
| 7 | + if (value === undefined) delete process.env[name]; |
| 8 | + else process.env[name] = value; |
| 9 | + try { |
| 10 | + fn(); |
| 11 | + } finally { |
| 12 | + if (previous === undefined) delete process.env[name]; |
| 13 | + else process.env[name] = previous; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +describe("ProcessTerminal", () => { |
| 18 | + it("should skip the Kitty query inside Zellij and enable modifyOtherKeys immediately", () => { |
| 19 | + const terminal = new ProcessTerminal(); |
| 20 | + const writes: string[] = []; |
| 21 | + const stdinOnCalls: Array<{ event: string | symbol; listener: (...args: unknown[]) => void }> = []; |
| 22 | + const stdinRemoveCalls: Array<{ event: string | symbol; listener: (...args: unknown[]) => void }> = []; |
| 23 | + const stdoutRemoveCalls: Array<{ event: string | symbol; listener: (...args: unknown[]) => void }> = []; |
| 24 | + |
| 25 | + const originalStdoutWrite = process.stdout.write; |
| 26 | + const originalStdinOn = process.stdin.on; |
| 27 | + const originalStdinRemoveListener = process.stdin.removeListener; |
| 28 | + const originalStdinPause = process.stdin.pause; |
| 29 | + const originalStdoutRemoveListener = process.stdout.removeListener; |
| 30 | + |
| 31 | + process.stdout.write = ((chunk: string | Uint8Array) => { |
| 32 | + writes.push(typeof chunk === "string" ? chunk : Buffer.from(chunk).toString("utf8")); |
| 33 | + return true; |
| 34 | + }) as typeof process.stdout.write; |
| 35 | + process.stdin.on = ((event: string | symbol, listener: (...args: unknown[]) => void) => { |
| 36 | + stdinOnCalls.push({ event, listener }); |
| 37 | + return process.stdin; |
| 38 | + }) as typeof process.stdin.on; |
| 39 | + process.stdin.removeListener = ((event: string | symbol, listener: (...args: unknown[]) => void) => { |
| 40 | + stdinRemoveCalls.push({ event, listener }); |
| 41 | + return process.stdin; |
| 42 | + }) as typeof process.stdin.removeListener; |
| 43 | + process.stdin.pause = (() => process.stdin) as typeof process.stdin.pause; |
| 44 | + process.stdout.removeListener = ((event: string | symbol, listener: (...args: unknown[]) => void) => { |
| 45 | + stdoutRemoveCalls.push({ event, listener }); |
| 46 | + return process.stdout; |
| 47 | + }) as typeof process.stdout.removeListener; |
| 48 | + |
| 49 | + try { |
| 50 | + withEnv("ZELLIJ", "1", () => { |
| 51 | + ( |
| 52 | + terminal as unknown as { |
| 53 | + queryAndEnableKittyProtocol(): void; |
| 54 | + } |
| 55 | + ).queryAndEnableKittyProtocol(); |
| 56 | + }); |
| 57 | + |
| 58 | + assert.deepStrictEqual(writes, ["\x1b[>4;2m"]); |
| 59 | + assert.strictEqual(stdinOnCalls.length, 1); |
| 60 | + assert.strictEqual(stdinOnCalls[0]?.event, "data"); |
| 61 | + |
| 62 | + terminal.stop(); |
| 63 | + |
| 64 | + assert.deepStrictEqual(writes, ["\x1b[>4;2m", "\x1b[?2004l", "\x1b[>4;0m"]); |
| 65 | + assert.strictEqual(stdinRemoveCalls.length, 1); |
| 66 | + assert.strictEqual(stdinRemoveCalls[0]?.event, "data"); |
| 67 | + assert.strictEqual(stdoutRemoveCalls.length, 0); |
| 68 | + } finally { |
| 69 | + process.stdout.write = originalStdoutWrite; |
| 70 | + process.stdin.on = originalStdinOn; |
| 71 | + process.stdin.removeListener = originalStdinRemoveListener; |
| 72 | + process.stdin.pause = originalStdinPause; |
| 73 | + process.stdout.removeListener = originalStdoutRemoveListener; |
| 74 | + } |
| 75 | + }); |
| 76 | +}); |
0 commit comments