| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- import assert from "node:assert";
- import { describe, it, mock } from "node:test";
- import { setKittyProtocolActive } from "../src/keys.ts";
- import { normalizeAppleTerminalInput, ProcessTerminal } from "../src/terminal.ts";
- describe("normalizeAppleTerminalInput", () => {
- it("rewrites Apple Terminal Return to CSI-u Shift+Enter when Shift is pressed", () => {
- assert.equal(normalizeAppleTerminalInput("\r", true, true), "\x1b[13;2u");
- });
- it("leaves Apple Terminal Return unchanged when Shift is not pressed", () => {
- assert.equal(normalizeAppleTerminalInput("\r", true, false), "\r");
- });
- it("leaves non-Apple Terminal Return unchanged when Shift is pressed", () => {
- assert.equal(normalizeAppleTerminalInput("\r", false, true), "\r");
- });
- it("leaves non-Return input unchanged", () => {
- assert.equal(normalizeAppleTerminalInput("\x1b[13;2u", true, true), "\x1b[13;2u");
- assert.equal(normalizeAppleTerminalInput("a", true, true), "a");
- });
- });
- describe("ProcessTerminal Kitty keyboard protocol negotiation", () => {
- type NegotiationHarness = {
- terminal: ProcessTerminal;
- writes: string[];
- send(data: string): void;
- getInput(): string | undefined;
- cleanup(): void;
- };
- function setupNegotiation(): NegotiationHarness {
- const terminal = new ProcessTerminal();
- const writes: string[] = [];
- let input: string | undefined;
- let dataHandler: ((data: string) => void) | undefined;
- let cleaned = false;
- const previousWrite = process.stdout.write;
- const previousOn = process.stdin.on;
- process.stdout.write = ((chunk: string | Uint8Array) => {
- writes.push(String(chunk));
- return true;
- }) as typeof process.stdout.write;
- process.stdin.on = ((event: string | symbol, listener: (...args: unknown[]) => void) => {
- if (event === "data") dataHandler = listener as (data: string) => void;
- return process.stdin;
- }) as typeof process.stdin.on;
- (
- terminal as unknown as {
- inputHandler?: (data: string) => void;
- queryAndEnableKittyProtocol(): void;
- }
- ).inputHandler = (data) => {
- input = data;
- };
- (terminal as unknown as { queryAndEnableKittyProtocol(): void }).queryAndEnableKittyProtocol();
- return {
- terminal,
- writes,
- send(data: string): void {
- dataHandler?.(data);
- },
- getInput(): string | undefined {
- return input;
- },
- cleanup(): void {
- if (cleaned) return;
- cleaned = true;
- try {
- terminal.stop();
- } finally {
- process.stdout.write = previousWrite;
- process.stdin.on = previousOn;
- setKittyProtocolActive(false);
- }
- },
- };
- }
- it("queries Kitty mode before enabling modifyOtherKeys fallback", () => {
- const harness = setupNegotiation();
- try {
- assert.equal(harness.writes[0], "\x1b[>7u\x1b[?u\x1b[c");
- assert.equal(harness.writes.includes("\x1b[>4;2m"), false);
- assert.equal(harness.terminal.kittyProtocolActive, false);
- } finally {
- harness.cleanup();
- }
- });
- it("activates Kitty mode for non-zero negotiated flags", () => {
- const harness = setupNegotiation();
- try {
- harness.send("\x1b[?7u");
- assert.equal(harness.getInput(), undefined);
- assert.equal(harness.terminal.kittyProtocolActive, true);
- assert.equal(harness.writes.includes("\x1b[>4;2m"), false);
- assert.equal(harness.writes.includes("\x1b[>4;0m"), false);
- harness.cleanup();
- assert.equal(harness.writes.filter((write) => write === "\x1b[<u").length, 1);
- assert.equal(harness.writes.includes("\x1b[>4;0m"), false);
- } finally {
- harness.cleanup();
- }
- });
- it("falls back to modifyOtherKeys for zero Kitty flags", () => {
- const harness = setupNegotiation();
- try {
- harness.send("\x1b[?0u");
- assert.equal(harness.getInput(), undefined);
- assert.equal(harness.terminal.kittyProtocolActive, false);
- assert.equal(harness.writes.filter((write) => write === "\x1b[>4;2m").length, 1);
- harness.cleanup();
- assert.equal(harness.writes.filter((write) => write === "\x1b[>4;0m").length, 1);
- } finally {
- harness.cleanup();
- }
- });
- it("falls back to modifyOtherKeys for device attributes without Kitty flags", () => {
- const harness = setupNegotiation();
- try {
- harness.send("\x1b[?62;4;52c");
- assert.equal(harness.getInput(), undefined);
- assert.equal(harness.terminal.kittyProtocolActive, false);
- assert.equal(harness.writes.filter((write) => write === "\x1b[>4;2m").length, 1);
- } finally {
- harness.cleanup();
- }
- });
- it("forwards normal input while waiting for Kitty response", () => {
- const harness = setupNegotiation();
- try {
- harness.send("a");
- assert.equal(harness.getInput(), "a");
- assert.equal(harness.terminal.kittyProtocolActive, false);
- } finally {
- harness.cleanup();
- }
- });
- it("tracks split Kitty confirmation", () => {
- mock.timers.enable({ apis: ["setTimeout"] });
- const harness = setupNegotiation();
- try {
- harness.send("\x1b[?7");
- mock.timers.tick(10);
- assert.equal(harness.getInput(), undefined);
- harness.send("u");
- assert.equal(harness.terminal.kittyProtocolActive, true);
- assert.equal(harness.writes.includes("\x1b[>4;2m"), false);
- } finally {
- harness.cleanup();
- mock.timers.reset();
- }
- });
- it("replays buffered CSI-prefix input when it is not a Kitty response", () => {
- mock.timers.enable({ apis: ["setTimeout"] });
- const harness = setupNegotiation();
- try {
- harness.send("\x1b[");
- mock.timers.tick(10);
- assert.equal(harness.getInput(), undefined);
- mock.timers.tick(150);
- assert.equal(harness.getInput(), "\x1b[");
- } finally {
- harness.cleanup();
- mock.timers.reset();
- }
- });
- });
- describe("ProcessTerminal dimensions", () => {
- it("falls back to COLUMNS and LINES before default dimensions", () => {
- const previousColumnsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "columns");
- const previousRowsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "rows");
- const previousColumns = process.env.COLUMNS;
- const previousLines = process.env.LINES;
- try {
- Object.defineProperty(process.stdout, "columns", { value: undefined, configurable: true });
- Object.defineProperty(process.stdout, "rows", { value: undefined, configurable: true });
- process.env.COLUMNS = "123";
- process.env.LINES = "45";
- const terminal = new ProcessTerminal();
- assert.equal(terminal.columns, 123);
- assert.equal(terminal.rows, 45);
- } finally {
- if (previousColumnsDescriptor) {
- Object.defineProperty(process.stdout, "columns", previousColumnsDescriptor);
- } else {
- Reflect.deleteProperty(process.stdout, "columns");
- }
- if (previousRowsDescriptor) {
- Object.defineProperty(process.stdout, "rows", previousRowsDescriptor);
- } else {
- Reflect.deleteProperty(process.stdout, "rows");
- }
- if (previousColumns === undefined) {
- delete process.env.COLUMNS;
- } else {
- process.env.COLUMNS = previousColumns;
- }
- if (previousLines === undefined) {
- delete process.env.LINES;
- } else {
- process.env.LINES = previousLines;
- }
- }
- });
- });
|