terminal.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. import assert from "node:assert";
  2. import { describe, it, mock } from "node:test";
  3. import { setKittyProtocolActive } from "../src/keys.ts";
  4. import { normalizeAppleTerminalInput, ProcessTerminal } from "../src/terminal.ts";
  5. describe("normalizeAppleTerminalInput", () => {
  6. it("rewrites Apple Terminal Return to CSI-u Shift+Enter when Shift is pressed", () => {
  7. assert.equal(normalizeAppleTerminalInput("\r", true, true), "\x1b[13;2u");
  8. });
  9. it("leaves Apple Terminal Return unchanged when Shift is not pressed", () => {
  10. assert.equal(normalizeAppleTerminalInput("\r", true, false), "\r");
  11. });
  12. it("leaves non-Apple Terminal Return unchanged when Shift is pressed", () => {
  13. assert.equal(normalizeAppleTerminalInput("\r", false, true), "\r");
  14. });
  15. it("leaves non-Return input unchanged", () => {
  16. assert.equal(normalizeAppleTerminalInput("\x1b[13;2u", true, true), "\x1b[13;2u");
  17. assert.equal(normalizeAppleTerminalInput("a", true, true), "a");
  18. });
  19. });
  20. describe("ProcessTerminal Kitty keyboard protocol negotiation", () => {
  21. type NegotiationHarness = {
  22. terminal: ProcessTerminal;
  23. writes: string[];
  24. send(data: string): void;
  25. getInput(): string | undefined;
  26. cleanup(): void;
  27. };
  28. function setupNegotiation(): NegotiationHarness {
  29. const terminal = new ProcessTerminal();
  30. const writes: string[] = [];
  31. let input: string | undefined;
  32. let dataHandler: ((data: string) => void) | undefined;
  33. let cleaned = false;
  34. const previousWrite = process.stdout.write;
  35. const previousOn = process.stdin.on;
  36. process.stdout.write = ((chunk: string | Uint8Array) => {
  37. writes.push(String(chunk));
  38. return true;
  39. }) as typeof process.stdout.write;
  40. process.stdin.on = ((event: string | symbol, listener: (...args: unknown[]) => void) => {
  41. if (event === "data") dataHandler = listener as (data: string) => void;
  42. return process.stdin;
  43. }) as typeof process.stdin.on;
  44. (
  45. terminal as unknown as {
  46. inputHandler?: (data: string) => void;
  47. queryAndEnableKittyProtocol(): void;
  48. }
  49. ).inputHandler = (data) => {
  50. input = data;
  51. };
  52. (terminal as unknown as { queryAndEnableKittyProtocol(): void }).queryAndEnableKittyProtocol();
  53. return {
  54. terminal,
  55. writes,
  56. send(data: string): void {
  57. dataHandler?.(data);
  58. },
  59. getInput(): string | undefined {
  60. return input;
  61. },
  62. cleanup(): void {
  63. if (cleaned) return;
  64. cleaned = true;
  65. try {
  66. terminal.stop();
  67. } finally {
  68. process.stdout.write = previousWrite;
  69. process.stdin.on = previousOn;
  70. setKittyProtocolActive(false);
  71. }
  72. },
  73. };
  74. }
  75. it("queries Kitty mode before enabling modifyOtherKeys fallback", () => {
  76. const harness = setupNegotiation();
  77. try {
  78. assert.equal(harness.writes[0], "\x1b[>7u\x1b[?u\x1b[c");
  79. assert.equal(harness.writes.includes("\x1b[>4;2m"), false);
  80. assert.equal(harness.terminal.kittyProtocolActive, false);
  81. } finally {
  82. harness.cleanup();
  83. }
  84. });
  85. it("activates Kitty mode for non-zero negotiated flags", () => {
  86. const harness = setupNegotiation();
  87. try {
  88. harness.send("\x1b[?7u");
  89. assert.equal(harness.getInput(), undefined);
  90. assert.equal(harness.terminal.kittyProtocolActive, true);
  91. assert.equal(harness.writes.includes("\x1b[>4;2m"), false);
  92. assert.equal(harness.writes.includes("\x1b[>4;0m"), false);
  93. harness.cleanup();
  94. assert.equal(harness.writes.filter((write) => write === "\x1b[<u").length, 1);
  95. assert.equal(harness.writes.includes("\x1b[>4;0m"), false);
  96. } finally {
  97. harness.cleanup();
  98. }
  99. });
  100. it("falls back to modifyOtherKeys for zero Kitty flags", () => {
  101. const harness = setupNegotiation();
  102. try {
  103. harness.send("\x1b[?0u");
  104. assert.equal(harness.getInput(), undefined);
  105. assert.equal(harness.terminal.kittyProtocolActive, false);
  106. assert.equal(harness.writes.filter((write) => write === "\x1b[>4;2m").length, 1);
  107. harness.cleanup();
  108. assert.equal(harness.writes.filter((write) => write === "\x1b[>4;0m").length, 1);
  109. } finally {
  110. harness.cleanup();
  111. }
  112. });
  113. it("falls back to modifyOtherKeys for device attributes without Kitty flags", () => {
  114. const harness = setupNegotiation();
  115. try {
  116. harness.send("\x1b[?62;4;52c");
  117. assert.equal(harness.getInput(), undefined);
  118. assert.equal(harness.terminal.kittyProtocolActive, false);
  119. assert.equal(harness.writes.filter((write) => write === "\x1b[>4;2m").length, 1);
  120. } finally {
  121. harness.cleanup();
  122. }
  123. });
  124. it("forwards normal input while waiting for Kitty response", () => {
  125. const harness = setupNegotiation();
  126. try {
  127. harness.send("a");
  128. assert.equal(harness.getInput(), "a");
  129. assert.equal(harness.terminal.kittyProtocolActive, false);
  130. } finally {
  131. harness.cleanup();
  132. }
  133. });
  134. it("tracks split Kitty confirmation", () => {
  135. mock.timers.enable({ apis: ["setTimeout"] });
  136. const harness = setupNegotiation();
  137. try {
  138. harness.send("\x1b[?7");
  139. mock.timers.tick(10);
  140. assert.equal(harness.getInput(), undefined);
  141. harness.send("u");
  142. assert.equal(harness.terminal.kittyProtocolActive, true);
  143. assert.equal(harness.writes.includes("\x1b[>4;2m"), false);
  144. } finally {
  145. harness.cleanup();
  146. mock.timers.reset();
  147. }
  148. });
  149. it("replays buffered CSI-prefix input when it is not a Kitty response", () => {
  150. mock.timers.enable({ apis: ["setTimeout"] });
  151. const harness = setupNegotiation();
  152. try {
  153. harness.send("\x1b[");
  154. mock.timers.tick(10);
  155. assert.equal(harness.getInput(), undefined);
  156. mock.timers.tick(150);
  157. assert.equal(harness.getInput(), "\x1b[");
  158. } finally {
  159. harness.cleanup();
  160. mock.timers.reset();
  161. }
  162. });
  163. });
  164. describe("ProcessTerminal dimensions", () => {
  165. it("falls back to COLUMNS and LINES before default dimensions", () => {
  166. const previousColumnsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "columns");
  167. const previousRowsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "rows");
  168. const previousColumns = process.env.COLUMNS;
  169. const previousLines = process.env.LINES;
  170. try {
  171. Object.defineProperty(process.stdout, "columns", { value: undefined, configurable: true });
  172. Object.defineProperty(process.stdout, "rows", { value: undefined, configurable: true });
  173. process.env.COLUMNS = "123";
  174. process.env.LINES = "45";
  175. const terminal = new ProcessTerminal();
  176. assert.equal(terminal.columns, 123);
  177. assert.equal(terminal.rows, 45);
  178. } finally {
  179. if (previousColumnsDescriptor) {
  180. Object.defineProperty(process.stdout, "columns", previousColumnsDescriptor);
  181. } else {
  182. Reflect.deleteProperty(process.stdout, "columns");
  183. }
  184. if (previousRowsDescriptor) {
  185. Object.defineProperty(process.stdout, "rows", previousRowsDescriptor);
  186. } else {
  187. Reflect.deleteProperty(process.stdout, "rows");
  188. }
  189. if (previousColumns === undefined) {
  190. delete process.env.COLUMNS;
  191. } else {
  192. process.env.COLUMNS = previousColumns;
  193. }
  194. if (previousLines === undefined) {
  195. delete process.env.LINES;
  196. } else {
  197. process.env.LINES = previousLines;
  198. }
  199. }
  200. });
  201. });