stdin-buffer.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /**
  2. * Tests for StdinBuffer
  3. *
  4. * Based on code from OpenTUI (https://github.com/anomalyco/opentui)
  5. * MIT License - Copyright (c) 2025 opentui
  6. */
  7. import assert from "node:assert";
  8. import { beforeEach, describe, it } from "node:test";
  9. import { StdinBuffer } from "../src/stdin-buffer.ts";
  10. describe("StdinBuffer", () => {
  11. let buffer: StdinBuffer;
  12. let emittedSequences: string[];
  13. beforeEach(() => {
  14. buffer = new StdinBuffer({ timeout: 10 });
  15. // Collect emitted sequences
  16. emittedSequences = [];
  17. buffer.on("data", (sequence) => {
  18. emittedSequences.push(sequence);
  19. });
  20. });
  21. // Helper to process data through the buffer
  22. function processInput(data: string | Buffer): void {
  23. buffer.process(data);
  24. }
  25. // Helper to wait for async operations
  26. async function wait(ms: number): Promise<void> {
  27. return new Promise((resolve) => setTimeout(resolve, ms));
  28. }
  29. describe("Regular Characters", () => {
  30. it("should pass through regular characters immediately", () => {
  31. processInput("a");
  32. assert.deepStrictEqual(emittedSequences, ["a"]);
  33. });
  34. it("should pass through multiple regular characters", () => {
  35. processInput("abc");
  36. assert.deepStrictEqual(emittedSequences, ["a", "b", "c"]);
  37. });
  38. it("should handle unicode characters", () => {
  39. processInput("hello 世界");
  40. assert.deepStrictEqual(emittedSequences, ["h", "e", "l", "l", "o", " ", "世", "界"]);
  41. });
  42. });
  43. describe("Complete Escape Sequences", () => {
  44. it("should pass through complete mouse SGR sequences", () => {
  45. const mouseSeq = "\x1b[<35;20;5m";
  46. processInput(mouseSeq);
  47. assert.deepStrictEqual(emittedSequences, [mouseSeq]);
  48. });
  49. it("should pass through complete arrow key sequences", () => {
  50. const upArrow = "\x1b[A";
  51. processInput(upArrow);
  52. assert.deepStrictEqual(emittedSequences, [upArrow]);
  53. });
  54. it("should pass through complete function key sequences", () => {
  55. const f1 = "\x1b[11~";
  56. processInput(f1);
  57. assert.deepStrictEqual(emittedSequences, [f1]);
  58. });
  59. it("should pass through meta key sequences", () => {
  60. const metaA = "\x1ba";
  61. processInput(metaA);
  62. assert.deepStrictEqual(emittedSequences, [metaA]);
  63. });
  64. it("should pass through SS3 sequences", () => {
  65. const ss3 = "\x1bOA";
  66. processInput(ss3);
  67. assert.deepStrictEqual(emittedSequences, [ss3]);
  68. });
  69. });
  70. describe("Partial Escape Sequences", () => {
  71. it("should buffer incomplete mouse SGR sequence", async () => {
  72. processInput("\x1b");
  73. assert.deepStrictEqual(emittedSequences, []);
  74. assert.strictEqual(buffer.getBuffer(), "\x1b");
  75. processInput("[<35");
  76. assert.deepStrictEqual(emittedSequences, []);
  77. assert.strictEqual(buffer.getBuffer(), "\x1b[<35");
  78. processInput(";20;5m");
  79. assert.deepStrictEqual(emittedSequences, ["\x1b[<35;20;5m"]);
  80. assert.strictEqual(buffer.getBuffer(), "");
  81. });
  82. it("should buffer incomplete CSI sequence", () => {
  83. processInput("\x1b[");
  84. assert.deepStrictEqual(emittedSequences, []);
  85. processInput("1;");
  86. assert.deepStrictEqual(emittedSequences, []);
  87. processInput("5H");
  88. assert.deepStrictEqual(emittedSequences, ["\x1b[1;5H"]);
  89. });
  90. it("should buffer split across many chunks", () => {
  91. processInput("\x1b");
  92. processInput("[");
  93. processInput("<");
  94. processInput("3");
  95. processInput("5");
  96. processInput(";");
  97. processInput("2");
  98. processInput("0");
  99. processInput(";");
  100. processInput("5");
  101. processInput("m");
  102. assert.deepStrictEqual(emittedSequences, ["\x1b[<35;20;5m"]);
  103. });
  104. it("should flush incomplete sequence after timeout", async () => {
  105. processInput("\x1b[<35");
  106. assert.deepStrictEqual(emittedSequences, []);
  107. // Wait for timeout
  108. await wait(15);
  109. assert.deepStrictEqual(emittedSequences, ["\x1b[<35"]);
  110. });
  111. });
  112. describe("Mixed Content", () => {
  113. it("should handle characters followed by escape sequence", () => {
  114. processInput("abc\x1b[A");
  115. assert.deepStrictEqual(emittedSequences, ["a", "b", "c", "\x1b[A"]);
  116. });
  117. it("should handle escape sequence followed by characters", () => {
  118. processInput("\x1b[Aabc");
  119. assert.deepStrictEqual(emittedSequences, ["\x1b[A", "a", "b", "c"]);
  120. });
  121. it("should handle multiple complete sequences", () => {
  122. processInput("\x1b[A\x1b[B\x1b[C");
  123. assert.deepStrictEqual(emittedSequences, ["\x1b[A", "\x1b[B", "\x1b[C"]);
  124. });
  125. it("should handle partial sequence with preceding characters", () => {
  126. processInput("abc\x1b[<35");
  127. assert.deepStrictEqual(emittedSequences, ["a", "b", "c"]);
  128. assert.strictEqual(buffer.getBuffer(), "\x1b[<35");
  129. processInput(";20;5m");
  130. assert.deepStrictEqual(emittedSequences, ["a", "b", "c", "\x1b[<35;20;5m"]);
  131. });
  132. });
  133. describe("Kitty Keyboard Protocol", () => {
  134. it("should handle Kitty CSI u press events", () => {
  135. // Press 'a' in Kitty protocol
  136. processInput("\x1b[97u");
  137. assert.deepStrictEqual(emittedSequences, ["\x1b[97u"]);
  138. });
  139. it("should handle Kitty CSI u release events", () => {
  140. // Release 'a' in Kitty protocol
  141. processInput("\x1b[97;1:3u");
  142. assert.deepStrictEqual(emittedSequences, ["\x1b[97;1:3u"]);
  143. });
  144. it("should handle batched Kitty press and release", () => {
  145. // Press 'a', release 'a' batched together (common over SSH)
  146. processInput("\x1b[97u\x1b[97;1:3u");
  147. assert.deepStrictEqual(emittedSequences, ["\x1b[97u", "\x1b[97;1:3u"]);
  148. });
  149. it("should handle multiple batched Kitty events", () => {
  150. // Press 'a', release 'a', press 'b', release 'b'
  151. processInput("\x1b[97u\x1b[97;1:3u\x1b[98u\x1b[98;1:3u");
  152. assert.deepStrictEqual(emittedSequences, ["\x1b[97u", "\x1b[97;1:3u", "\x1b[98u", "\x1b[98;1:3u"]);
  153. });
  154. it("should handle Kitty arrow keys with event type", () => {
  155. // Up arrow press with event type
  156. processInput("\x1b[1;1:1A");
  157. assert.deepStrictEqual(emittedSequences, ["\x1b[1;1:1A"]);
  158. });
  159. it("should handle Kitty functional keys with event type", () => {
  160. // Delete key release
  161. processInput("\x1b[3;1:3~");
  162. assert.deepStrictEqual(emittedSequences, ["\x1b[3;1:3~"]);
  163. });
  164. it("should split ESC+ESC+CSI into standalone ESC and the CSI sequence (WezTerm Escape key regression)", () => {
  165. // WezTerm with enable_kitty_keyboard sends Escape key press as raw \x1b
  166. // and the release as a full Kitty CSI-u sequence, concatenated.
  167. // The buffer must not treat \x1b\x1b as a complete meta-key when the
  168. // following byte starts a new escape sequence.
  169. processInput("\x1b\x1b[27;129:3u");
  170. assert.deepStrictEqual(emittedSequences, ["\x1b", "\x1b[27;129:3u"]);
  171. });
  172. it("should split ESC+ESC+CSI with no modifier (no num_lock)", () => {
  173. processInput("\x1b\x1b[27;1:3u");
  174. assert.deepStrictEqual(emittedSequences, ["\x1b", "\x1b[27;1:3u"]);
  175. });
  176. it("should still emit ESC+ESC as a single sequence when not followed by a new escape", () => {
  177. // \x1b\x1b alone (no following CSI) stays as-is — e.g. ctrl+alt+[
  178. processInput("\x1b\x1b");
  179. assert.deepStrictEqual(emittedSequences, ["\x1b\x1b"]);
  180. });
  181. it("should handle plain characters mixed with Kitty sequences", () => {
  182. // Plain 'a' followed by Kitty release
  183. processInput("a\x1b[97;1:3u");
  184. assert.deepStrictEqual(emittedSequences, ["a", "\x1b[97;1:3u"]);
  185. });
  186. it("should drop raw duplicate character after matching Kitty printable sequence", () => {
  187. processInput("\x1b[224uà");
  188. assert.deepStrictEqual(emittedSequences, ["\x1b[224u"]);
  189. });
  190. it("should drop raw duplicate character after matching Kitty printable sequence across chunks", () => {
  191. processInput("\x1b[64u");
  192. processInput("@");
  193. assert.deepStrictEqual(emittedSequences, ["\x1b[64u"]);
  194. });
  195. it("should keep non-matching plain character after Kitty printable sequence", () => {
  196. processInput("\x1b[97ub");
  197. assert.deepStrictEqual(emittedSequences, ["\x1b[97u", "b"]);
  198. });
  199. it("should keep raw character after modified Kitty printable sequence", () => {
  200. processInput("\x1b[64;3u@");
  201. assert.deepStrictEqual(emittedSequences, ["\x1b[64;3u", "@"]);
  202. });
  203. it("should handle rapid typing simulation with Kitty protocol", () => {
  204. // Simulates typing "hi" quickly with releases interleaved
  205. processInput("\x1b[104u\x1b[104;1:3u\x1b[105u\x1b[105;1:3u");
  206. assert.deepStrictEqual(emittedSequences, ["\x1b[104u", "\x1b[104;1:3u", "\x1b[105u", "\x1b[105;1:3u"]);
  207. });
  208. });
  209. describe("Mouse Events", () => {
  210. it("should handle mouse press event", () => {
  211. processInput("\x1b[<0;10;5M");
  212. assert.deepStrictEqual(emittedSequences, ["\x1b[<0;10;5M"]);
  213. });
  214. it("should handle mouse release event", () => {
  215. processInput("\x1b[<0;10;5m");
  216. assert.deepStrictEqual(emittedSequences, ["\x1b[<0;10;5m"]);
  217. });
  218. it("should handle mouse move event", () => {
  219. processInput("\x1b[<35;20;5m");
  220. assert.deepStrictEqual(emittedSequences, ["\x1b[<35;20;5m"]);
  221. });
  222. it("should handle split mouse events", () => {
  223. processInput("\x1b[<3");
  224. processInput("5;1");
  225. processInput("5;");
  226. processInput("10m");
  227. assert.deepStrictEqual(emittedSequences, ["\x1b[<35;15;10m"]);
  228. });
  229. it("should handle multiple mouse events", () => {
  230. processInput("\x1b[<35;1;1m\x1b[<35;2;2m\x1b[<35;3;3m");
  231. assert.deepStrictEqual(emittedSequences, ["\x1b[<35;1;1m", "\x1b[<35;2;2m", "\x1b[<35;3;3m"]);
  232. });
  233. it("should handle old-style mouse sequence (ESC[M + 3 bytes)", () => {
  234. processInput("\x1b[M abc");
  235. assert.deepStrictEqual(emittedSequences, ["\x1b[M ab", "c"]);
  236. });
  237. it("should buffer incomplete old-style mouse sequence", () => {
  238. processInput("\x1b[M");
  239. assert.strictEqual(buffer.getBuffer(), "\x1b[M");
  240. processInput(" a");
  241. assert.strictEqual(buffer.getBuffer(), "\x1b[M a");
  242. processInput("b");
  243. assert.deepStrictEqual(emittedSequences, ["\x1b[M ab"]);
  244. });
  245. });
  246. describe("Edge Cases", () => {
  247. it("should handle empty input", () => {
  248. processInput("");
  249. // Empty string emits an empty data event
  250. assert.deepStrictEqual(emittedSequences, [""]);
  251. });
  252. it("should handle lone escape character with timeout", async () => {
  253. processInput("\x1b");
  254. assert.deepStrictEqual(emittedSequences, []);
  255. // After timeout, should emit
  256. await wait(15);
  257. assert.deepStrictEqual(emittedSequences, ["\x1b"]);
  258. });
  259. it("should handle lone escape character with explicit flush", () => {
  260. processInput("\x1b");
  261. assert.deepStrictEqual(emittedSequences, []);
  262. const flushed = buffer.flush();
  263. assert.deepStrictEqual(flushed, ["\x1b"]);
  264. });
  265. it("should handle buffer input", () => {
  266. processInput(Buffer.from("\x1b[A"));
  267. assert.deepStrictEqual(emittedSequences, ["\x1b[A"]);
  268. });
  269. it("should handle very long sequences", () => {
  270. const longSeq = `\x1b[${"1;".repeat(50)}H`;
  271. processInput(longSeq);
  272. assert.deepStrictEqual(emittedSequences, [longSeq]);
  273. });
  274. });
  275. describe("Flush", () => {
  276. it("should flush incomplete sequences", () => {
  277. processInput("\x1b[<35");
  278. const flushed = buffer.flush();
  279. assert.deepStrictEqual(flushed, ["\x1b[<35"]);
  280. assert.strictEqual(buffer.getBuffer(), "");
  281. });
  282. it("should return empty array if nothing to flush", () => {
  283. const flushed = buffer.flush();
  284. assert.deepStrictEqual(flushed, []);
  285. });
  286. it("should emit flushed data via timeout", async () => {
  287. processInput("\x1b[<35");
  288. assert.deepStrictEqual(emittedSequences, []);
  289. // Wait for timeout to flush
  290. await wait(15);
  291. assert.deepStrictEqual(emittedSequences, ["\x1b[<35"]);
  292. });
  293. });
  294. describe("Clear", () => {
  295. it("should clear buffered content without emitting", () => {
  296. processInput("\x1b[<35");
  297. assert.strictEqual(buffer.getBuffer(), "\x1b[<35");
  298. buffer.clear();
  299. assert.strictEqual(buffer.getBuffer(), "");
  300. assert.deepStrictEqual(emittedSequences, []);
  301. });
  302. });
  303. describe("Bracketed Paste", () => {
  304. let emittedPaste: string[] = [];
  305. beforeEach(() => {
  306. buffer = new StdinBuffer({ timeout: 10 });
  307. // Collect emitted sequences
  308. emittedSequences = [];
  309. buffer.on("data", (sequence) => {
  310. emittedSequences.push(sequence);
  311. });
  312. // Collect paste events
  313. emittedPaste = [];
  314. buffer.on("paste", (data) => {
  315. emittedPaste.push(data);
  316. });
  317. });
  318. it("should emit paste event for complete bracketed paste", () => {
  319. const pasteStart = "\x1b[200~";
  320. const pasteEnd = "\x1b[201~";
  321. const content = "hello world";
  322. processInput(pasteStart + content + pasteEnd);
  323. assert.deepStrictEqual(emittedPaste, ["hello world"]);
  324. assert.deepStrictEqual(emittedSequences, []); // No data events during paste
  325. });
  326. it("should handle paste arriving in chunks", () => {
  327. processInput("\x1b[200~");
  328. assert.deepStrictEqual(emittedPaste, []);
  329. processInput("hello ");
  330. assert.deepStrictEqual(emittedPaste, []);
  331. processInput("world\x1b[201~");
  332. assert.deepStrictEqual(emittedPaste, ["hello world"]);
  333. assert.deepStrictEqual(emittedSequences, []);
  334. });
  335. it("should handle paste with input before and after", () => {
  336. processInput("a");
  337. processInput("\x1b[200~pasted\x1b[201~");
  338. processInput("b");
  339. assert.deepStrictEqual(emittedSequences, ["a", "b"]);
  340. assert.deepStrictEqual(emittedPaste, ["pasted"]);
  341. });
  342. it("should handle paste with newlines", () => {
  343. processInput("\x1b[200~line1\nline2\nline3\x1b[201~");
  344. assert.deepStrictEqual(emittedPaste, ["line1\nline2\nline3"]);
  345. assert.deepStrictEqual(emittedSequences, []);
  346. });
  347. it("should handle paste with unicode", () => {
  348. processInput("\x1b[200~Hello 世界 🎉\x1b[201~");
  349. assert.deepStrictEqual(emittedPaste, ["Hello 世界 🎉"]);
  350. assert.deepStrictEqual(emittedSequences, []);
  351. });
  352. });
  353. describe("Destroy", () => {
  354. it("should clear buffer on destroy", () => {
  355. processInput("\x1b[<35");
  356. assert.strictEqual(buffer.getBuffer(), "\x1b[<35");
  357. buffer.destroy();
  358. assert.strictEqual(buffer.getBuffer(), "");
  359. });
  360. it("should clear pending timeouts on destroy", async () => {
  361. processInput("\x1b[<35");
  362. buffer.destroy();
  363. // Wait longer than timeout
  364. await wait(15);
  365. // Should not have emitted anything
  366. assert.deepStrictEqual(emittedSequences, []);
  367. });
  368. });
  369. });