input.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. import assert from "node:assert";
  2. import { describe, it } from "node:test";
  3. import { Input } from "../src/components/input.ts";
  4. import { visibleWidth } from "../src/utils.ts";
  5. describe("Input component", () => {
  6. it("submits value including backslash on Enter", () => {
  7. const input = new Input();
  8. let submitted: string | undefined;
  9. input.onSubmit = (value) => {
  10. submitted = value;
  11. };
  12. // Type hello, then backslash, then Enter
  13. input.handleInput("h");
  14. input.handleInput("e");
  15. input.handleInput("l");
  16. input.handleInput("l");
  17. input.handleInput("o");
  18. input.handleInput("\\");
  19. input.handleInput("\r");
  20. // Input is single-line, no backslash+Enter workaround
  21. assert.strictEqual(submitted, "hello\\");
  22. });
  23. it("inserts backslash as regular character", () => {
  24. const input = new Input();
  25. input.handleInput("\\");
  26. input.handleInput("x");
  27. assert.strictEqual(input.getValue(), "\\x");
  28. });
  29. describe("render", () => {
  30. it("does not overflow with wide CJK and fullwidth text", () => {
  31. const width = 93;
  32. const cases = [
  33. "가나다라마바사아자차카타파하 한글 텍스트가 터미널 너비를 초과하면 크래시가 발생합니다 이것은 재현용 테스트입니다",
  34. "これはテスト文章です。日本語のテキストが正しく表示されるかどうかを確認するためのサンプルテキストです。あいうえお",
  35. "这是一段测试文本,用于验证中文字符在终端中的显示宽度是否被正确计算,如果不正确就会导致用户界面崩溃的问题",
  36. "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklm",
  37. ];
  38. const cursorPositions = [
  39. { label: "start", move: (_input: Input) => {} },
  40. {
  41. label: "middle",
  42. move: (input: Input) => {
  43. for (let i = 0; i < 10; i++) input.handleInput("\x1b[C");
  44. },
  45. },
  46. { label: "end", move: (input: Input) => input.handleInput("\x05") },
  47. ];
  48. for (const text of cases) {
  49. for (const { label, move } of cursorPositions) {
  50. const input = new Input();
  51. input.setValue(text);
  52. input.focused = true;
  53. move(input);
  54. const [line] = input.render(width);
  55. assert.ok(line);
  56. assert.ok(visibleWidth(line) <= width, `rendered line overflowed for ${text} at ${label}`);
  57. }
  58. }
  59. });
  60. it("keeps the cursor visible when horizontally scrolling wide text", () => {
  61. const input = new Input();
  62. const width = 20;
  63. const text = "가나다라마바사아자차카타파하";
  64. input.setValue(text);
  65. input.focused = true;
  66. input.handleInput("\x01");
  67. for (let i = 0; i < 5; i++) input.handleInput("\x1b[C");
  68. const [line] = input.render(width);
  69. assert.ok(line);
  70. assert.ok(visibleWidth(line) <= width);
  71. });
  72. });
  73. describe("Kill ring", () => {
  74. it("Ctrl+W saves deleted text to kill ring and Ctrl+Y yanks it", () => {
  75. const input = new Input();
  76. input.setValue("foo bar baz");
  77. // Move cursor to end
  78. input.handleInput("\x05"); // Ctrl+E
  79. input.handleInput("\x17"); // Ctrl+W - deletes "baz"
  80. assert.strictEqual(input.getValue(), "foo bar ");
  81. // Move to beginning and yank
  82. input.handleInput("\x01"); // Ctrl+A
  83. input.handleInput("\x19"); // Ctrl+Y
  84. assert.strictEqual(input.getValue(), "bazfoo bar ");
  85. });
  86. it("Ctrl+W preserves ASCII punctuation boundaries", () => {
  87. const input = new Input();
  88. input.setValue("foo.bar");
  89. input.handleInput("\x05"); // Ctrl+E
  90. input.handleInput("\x17"); // Ctrl+W - deletes "bar"
  91. assert.strictEqual(input.getValue(), "foo.");
  92. input.setValue("foo:bar");
  93. input.handleInput("\x05"); // Ctrl+E
  94. input.handleInput("\x17"); // Ctrl+W - deletes "bar"
  95. assert.strictEqual(input.getValue(), "foo:");
  96. });
  97. it("Ctrl+W handles Unicode word boundaries", () => {
  98. const input = new Input();
  99. // "你好世界。你好,世界" segments as: 你好|世界|。|你好|,|世界
  100. input.setValue("你好世界。你好,世界");
  101. input.handleInput("\x05"); // Ctrl+E
  102. input.handleInput("\x17"); // Ctrl+W - deletes "世界"
  103. assert.strictEqual(input.getValue(), "你好世界。你好,");
  104. input.handleInput("\x17"); // Ctrl+W - deletes ","
  105. assert.strictEqual(input.getValue(), "你好世界。你好");
  106. input.handleInput("\x17"); // Ctrl+W - deletes "你好"
  107. assert.strictEqual(input.getValue(), "你好世界。");
  108. input.handleInput("\x17"); // Ctrl+W - deletes "。"
  109. assert.strictEqual(input.getValue(), "你好世界");
  110. input.handleInput("\x17"); // Ctrl+W - deletes "世界"
  111. assert.strictEqual(input.getValue(), "你好");
  112. input.handleInput("\x17"); // Ctrl+W - deletes "你好"
  113. assert.strictEqual(input.getValue(), "");
  114. });
  115. it("Ctrl+U saves deleted text to kill ring", () => {
  116. const input = new Input();
  117. input.setValue("hello world");
  118. // Move cursor to after "hello "
  119. input.handleInput("\x01"); // Ctrl+A
  120. for (let i = 0; i < 6; i++) input.handleInput("\x1b[C");
  121. input.handleInput("\x15"); // Ctrl+U - deletes "hello "
  122. assert.strictEqual(input.getValue(), "world");
  123. input.handleInput("\x19"); // Ctrl+Y
  124. assert.strictEqual(input.getValue(), "hello world");
  125. });
  126. it("Ctrl+K saves deleted text to kill ring", () => {
  127. const input = new Input();
  128. input.setValue("hello world");
  129. input.handleInput("\x01"); // Ctrl+A
  130. input.handleInput("\x0b"); // Ctrl+K - deletes "hello world"
  131. assert.strictEqual(input.getValue(), "");
  132. input.handleInput("\x19"); // Ctrl+Y
  133. assert.strictEqual(input.getValue(), "hello world");
  134. });
  135. it("Ctrl+Y does nothing when kill ring is empty", () => {
  136. const input = new Input();
  137. input.setValue("test");
  138. input.handleInput("\x05"); // Ctrl+E
  139. input.handleInput("\x19"); // Ctrl+Y
  140. assert.strictEqual(input.getValue(), "test");
  141. });
  142. it("Alt+Y cycles through kill ring after Ctrl+Y", () => {
  143. const input = new Input();
  144. // Create kill ring with multiple entries
  145. input.setValue("first");
  146. input.handleInput("\x05"); // Ctrl+E
  147. input.handleInput("\x17"); // Ctrl+W - deletes "first"
  148. input.setValue("second");
  149. input.handleInput("\x05"); // Ctrl+E
  150. input.handleInput("\x17"); // Ctrl+W - deletes "second"
  151. input.setValue("third");
  152. input.handleInput("\x05"); // Ctrl+E
  153. input.handleInput("\x17"); // Ctrl+W - deletes "third"
  154. assert.strictEqual(input.getValue(), "");
  155. input.handleInput("\x19"); // Ctrl+Y - yanks "third"
  156. assert.strictEqual(input.getValue(), "third");
  157. input.handleInput("\x1by"); // Alt+Y - cycles to "second"
  158. assert.strictEqual(input.getValue(), "second");
  159. input.handleInput("\x1by"); // Alt+Y - cycles to "first"
  160. assert.strictEqual(input.getValue(), "first");
  161. input.handleInput("\x1by"); // Alt+Y - cycles back to "third"
  162. assert.strictEqual(input.getValue(), "third");
  163. });
  164. it("Alt+Y does nothing if not preceded by yank", () => {
  165. const input = new Input();
  166. input.setValue("test");
  167. input.handleInput("\x05"); // Ctrl+E
  168. input.handleInput("\x17"); // Ctrl+W - deletes "test"
  169. input.setValue("other");
  170. input.handleInput("\x05"); // Ctrl+E
  171. // Type something to break the yank chain
  172. input.handleInput("x");
  173. assert.strictEqual(input.getValue(), "otherx");
  174. input.handleInput("\x1by"); // Alt+Y - should do nothing
  175. assert.strictEqual(input.getValue(), "otherx");
  176. });
  177. it("Alt+Y does nothing if kill ring has one entry", () => {
  178. const input = new Input();
  179. input.setValue("only");
  180. input.handleInput("\x05"); // Ctrl+E
  181. input.handleInput("\x17"); // Ctrl+W - deletes "only"
  182. input.handleInput("\x19"); // Ctrl+Y - yanks "only"
  183. assert.strictEqual(input.getValue(), "only");
  184. input.handleInput("\x1by"); // Alt+Y - should do nothing
  185. assert.strictEqual(input.getValue(), "only");
  186. });
  187. it("consecutive Ctrl+W accumulates into one kill ring entry", () => {
  188. const input = new Input();
  189. input.setValue("one two three");
  190. input.handleInput("\x05"); // Ctrl+E
  191. input.handleInput("\x17"); // Ctrl+W - deletes "three"
  192. input.handleInput("\x17"); // Ctrl+W - deletes "two "
  193. input.handleInput("\x17"); // Ctrl+W - deletes "one "
  194. assert.strictEqual(input.getValue(), "");
  195. input.handleInput("\x19"); // Ctrl+Y
  196. assert.strictEqual(input.getValue(), "one two three");
  197. });
  198. it("non-delete actions break kill accumulation", () => {
  199. const input = new Input();
  200. input.setValue("foo bar baz");
  201. input.handleInput("\x05"); // Ctrl+E
  202. input.handleInput("\x17"); // Ctrl+W - deletes "baz"
  203. assert.strictEqual(input.getValue(), "foo bar ");
  204. input.handleInput("x"); // Typing breaks accumulation
  205. assert.strictEqual(input.getValue(), "foo bar x");
  206. input.handleInput("\x17"); // Ctrl+W - deletes "x" (separate entry)
  207. assert.strictEqual(input.getValue(), "foo bar ");
  208. input.handleInput("\x19"); // Ctrl+Y - most recent is "x"
  209. assert.strictEqual(input.getValue(), "foo bar x");
  210. input.handleInput("\x1by"); // Alt+Y - cycle to "baz"
  211. assert.strictEqual(input.getValue(), "foo bar baz");
  212. });
  213. it("non-yank actions break Alt+Y chain", () => {
  214. const input = new Input();
  215. input.setValue("first");
  216. input.handleInput("\x05"); // Ctrl+E
  217. input.handleInput("\x17"); // Ctrl+W
  218. input.setValue("second");
  219. input.handleInput("\x05"); // Ctrl+E
  220. input.handleInput("\x17"); // Ctrl+W
  221. input.setValue("");
  222. input.handleInput("\x19"); // Ctrl+Y - yanks "second"
  223. assert.strictEqual(input.getValue(), "second");
  224. input.handleInput("x"); // Breaks yank chain
  225. assert.strictEqual(input.getValue(), "secondx");
  226. input.handleInput("\x1by"); // Alt+Y - should do nothing
  227. assert.strictEqual(input.getValue(), "secondx");
  228. });
  229. it("kill ring rotation persists after cycling", () => {
  230. const input = new Input();
  231. input.setValue("first");
  232. input.handleInput("\x05"); // Ctrl+E
  233. input.handleInput("\x17"); // deletes "first"
  234. input.setValue("second");
  235. input.handleInput("\x05"); // Ctrl+E
  236. input.handleInput("\x17"); // deletes "second"
  237. input.setValue("third");
  238. input.handleInput("\x05"); // Ctrl+E
  239. input.handleInput("\x17"); // deletes "third"
  240. input.setValue("");
  241. input.handleInput("\x19"); // Ctrl+Y - yanks "third"
  242. input.handleInput("\x1by"); // Alt+Y - cycles to "second"
  243. assert.strictEqual(input.getValue(), "second");
  244. // Break chain and start fresh
  245. input.handleInput("x");
  246. input.setValue("");
  247. // New yank should get "second" (now at end after rotation)
  248. input.handleInput("\x19"); // Ctrl+Y
  249. assert.strictEqual(input.getValue(), "second");
  250. });
  251. it("backward deletions prepend, forward deletions append during accumulation", () => {
  252. const input = new Input();
  253. input.setValue("prefix|suffix");
  254. // Position cursor at "|"
  255. input.handleInput("\x01"); // Ctrl+A
  256. for (let i = 0; i < 6; i++) input.handleInput("\x1b[C"); // Move right 6
  257. input.handleInput("\x0b"); // Ctrl+K - deletes "|suffix" (forward)
  258. assert.strictEqual(input.getValue(), "prefix");
  259. input.handleInput("\x19"); // Ctrl+Y
  260. assert.strictEqual(input.getValue(), "prefix|suffix");
  261. });
  262. it("Alt+D deletes word forward and saves to kill ring", () => {
  263. const input = new Input();
  264. input.setValue("hello world test");
  265. input.handleInput("\x01"); // Ctrl+A
  266. input.handleInput("\x1bd"); // Alt+D - deletes "hello"
  267. assert.strictEqual(input.getValue(), " world test");
  268. input.handleInput("\x1bd"); // Alt+D - deletes " world"
  269. assert.strictEqual(input.getValue(), " test");
  270. // Yank should get accumulated text
  271. input.handleInput("\x19"); // Ctrl+Y
  272. assert.strictEqual(input.getValue(), "hello world test");
  273. });
  274. it("Alt+D preserves ASCII punctuation boundaries", () => {
  275. const input = new Input();
  276. input.setValue("foo.bar baz");
  277. input.handleInput("\x01"); // Ctrl+A
  278. input.handleInput("\x1bd"); // Alt+D - deletes "foo"
  279. assert.strictEqual(input.getValue(), ".bar baz");
  280. input.handleInput("\x1bd"); // Alt+D - deletes "."
  281. assert.strictEqual(input.getValue(), "bar baz");
  282. input.handleInput("\x1bd"); // Alt+D - deletes "bar"
  283. assert.strictEqual(input.getValue(), " baz");
  284. });
  285. it("Alt+D handles Unicode word boundaries", () => {
  286. const input = new Input();
  287. // "你好世界。你好,世界" segments as: 你好|世界|。|你好|,|世界
  288. input.setValue("你好世界。你好,世界");
  289. input.handleInput("\x01"); // Ctrl+A
  290. input.handleInput("\x1bd"); // Alt+D - deletes "你好"
  291. assert.strictEqual(input.getValue(), "世界。你好,世界");
  292. input.handleInput("\x1bd"); // Alt+D - deletes "世界"
  293. assert.strictEqual(input.getValue(), "。你好,世界");
  294. input.handleInput("\x1bd"); // Alt+D - deletes "。"
  295. assert.strictEqual(input.getValue(), "你好,世界");
  296. input.handleInput("\x1bd"); // Alt+D - deletes "你好"
  297. assert.strictEqual(input.getValue(), ",世界");
  298. input.handleInput("\x1bd"); // Alt+D - deletes ","
  299. assert.strictEqual(input.getValue(), "世界");
  300. input.handleInput("\x1bd"); // Alt+D - deletes "世界"
  301. assert.strictEqual(input.getValue(), "");
  302. });
  303. it("handles yank in middle of text", () => {
  304. const input = new Input();
  305. input.setValue("word");
  306. input.handleInput("\x05"); // Ctrl+E
  307. input.handleInput("\x17"); // Ctrl+W - deletes "word"
  308. input.setValue("hello world");
  309. // Move to middle (after "hello ")
  310. input.handleInput("\x01"); // Ctrl+A
  311. for (let i = 0; i < 6; i++) input.handleInput("\x1b[C");
  312. input.handleInput("\x19"); // Ctrl+Y
  313. assert.strictEqual(input.getValue(), "hello wordworld");
  314. });
  315. it("handles yank-pop in middle of text", () => {
  316. const input = new Input();
  317. // Create two kill ring entries
  318. input.setValue("FIRST");
  319. input.handleInput("\x05"); // Ctrl+E
  320. input.handleInput("\x17"); // Ctrl+W - deletes "FIRST"
  321. input.setValue("SECOND");
  322. input.handleInput("\x05"); // Ctrl+E
  323. input.handleInput("\x17"); // Ctrl+W - deletes "SECOND"
  324. // Set up "hello world" and position cursor after "hello "
  325. input.setValue("hello world");
  326. input.handleInput("\x01"); // Ctrl+A
  327. for (let i = 0; i < 6; i++) input.handleInput("\x1b[C");
  328. input.handleInput("\x19"); // Ctrl+Y - yanks "SECOND"
  329. assert.strictEqual(input.getValue(), "hello SECONDworld");
  330. input.handleInput("\x1by"); // Alt+Y - replaces with "FIRST"
  331. assert.strictEqual(input.getValue(), "hello FIRSTworld");
  332. });
  333. });
  334. describe("Undo", () => {
  335. it("does nothing when undo stack is empty", () => {
  336. const input = new Input();
  337. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  338. assert.strictEqual(input.getValue(), "");
  339. });
  340. it("coalesces consecutive word characters into one undo unit", () => {
  341. const input = new Input();
  342. input.handleInput("h");
  343. input.handleInput("e");
  344. input.handleInput("l");
  345. input.handleInput("l");
  346. input.handleInput("o");
  347. input.handleInput(" ");
  348. input.handleInput("w");
  349. input.handleInput("o");
  350. input.handleInput("r");
  351. input.handleInput("l");
  352. input.handleInput("d");
  353. assert.strictEqual(input.getValue(), "hello world");
  354. // Undo removes " world"
  355. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  356. assert.strictEqual(input.getValue(), "hello");
  357. // Undo removes "hello"
  358. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  359. assert.strictEqual(input.getValue(), "");
  360. });
  361. it("undoes spaces one at a time", () => {
  362. const input = new Input();
  363. input.handleInput("h");
  364. input.handleInput("e");
  365. input.handleInput("l");
  366. input.handleInput("l");
  367. input.handleInput("o");
  368. input.handleInput(" ");
  369. input.handleInput(" ");
  370. assert.strictEqual(input.getValue(), "hello ");
  371. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes second " "
  372. assert.strictEqual(input.getValue(), "hello ");
  373. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes first " "
  374. assert.strictEqual(input.getValue(), "hello");
  375. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo) - removes "hello"
  376. assert.strictEqual(input.getValue(), "");
  377. });
  378. it("undoes backspace", () => {
  379. const input = new Input();
  380. input.handleInput("h");
  381. input.handleInput("e");
  382. input.handleInput("l");
  383. input.handleInput("l");
  384. input.handleInput("o");
  385. input.handleInput("\x7f"); // Backspace
  386. assert.strictEqual(input.getValue(), "hell");
  387. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  388. assert.strictEqual(input.getValue(), "hello");
  389. });
  390. it("undoes forward delete", () => {
  391. const input = new Input();
  392. input.handleInput("h");
  393. input.handleInput("e");
  394. input.handleInput("l");
  395. input.handleInput("l");
  396. input.handleInput("o");
  397. input.handleInput("\x01"); // Ctrl+A - go to start
  398. input.handleInput("\x1b[C"); // Right arrow
  399. input.handleInput("\x1b[3~"); // Delete key
  400. assert.strictEqual(input.getValue(), "hllo");
  401. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  402. assert.strictEqual(input.getValue(), "hello");
  403. });
  404. it("undoes Ctrl+W (delete word backward)", () => {
  405. const input = new Input();
  406. input.handleInput("h");
  407. input.handleInput("e");
  408. input.handleInput("l");
  409. input.handleInput("l");
  410. input.handleInput("o");
  411. input.handleInput(" ");
  412. input.handleInput("w");
  413. input.handleInput("o");
  414. input.handleInput("r");
  415. input.handleInput("l");
  416. input.handleInput("d");
  417. assert.strictEqual(input.getValue(), "hello world");
  418. input.handleInput("\x17"); // Ctrl+W
  419. assert.strictEqual(input.getValue(), "hello ");
  420. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  421. assert.strictEqual(input.getValue(), "hello world");
  422. });
  423. it("undoes Ctrl+K (delete to line end)", () => {
  424. const input = new Input();
  425. input.handleInput("h");
  426. input.handleInput("e");
  427. input.handleInput("l");
  428. input.handleInput("l");
  429. input.handleInput("o");
  430. input.handleInput(" ");
  431. input.handleInput("w");
  432. input.handleInput("o");
  433. input.handleInput("r");
  434. input.handleInput("l");
  435. input.handleInput("d");
  436. input.handleInput("\x01"); // Ctrl+A
  437. for (let i = 0; i < 6; i++) input.handleInput("\x1b[C");
  438. input.handleInput("\x0b"); // Ctrl+K
  439. assert.strictEqual(input.getValue(), "hello ");
  440. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  441. assert.strictEqual(input.getValue(), "hello world");
  442. });
  443. it("undoes Ctrl+U (delete to line start)", () => {
  444. const input = new Input();
  445. input.handleInput("h");
  446. input.handleInput("e");
  447. input.handleInput("l");
  448. input.handleInput("l");
  449. input.handleInput("o");
  450. input.handleInput(" ");
  451. input.handleInput("w");
  452. input.handleInput("o");
  453. input.handleInput("r");
  454. input.handleInput("l");
  455. input.handleInput("d");
  456. input.handleInput("\x01"); // Ctrl+A
  457. for (let i = 0; i < 6; i++) input.handleInput("\x1b[C");
  458. input.handleInput("\x15"); // Ctrl+U
  459. assert.strictEqual(input.getValue(), "world");
  460. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  461. assert.strictEqual(input.getValue(), "hello world");
  462. });
  463. it("undoes yank", () => {
  464. const input = new Input();
  465. input.handleInput("h");
  466. input.handleInput("e");
  467. input.handleInput("l");
  468. input.handleInput("l");
  469. input.handleInput("o");
  470. input.handleInput(" ");
  471. input.handleInput("\x17"); // Ctrl+W - delete "hello "
  472. input.handleInput("\x19"); // Ctrl+Y - yank
  473. assert.strictEqual(input.getValue(), "hello ");
  474. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  475. assert.strictEqual(input.getValue(), "");
  476. });
  477. it("undoes paste atomically", () => {
  478. const input = new Input();
  479. input.setValue("hello world");
  480. input.handleInput("\x01"); // Ctrl+A
  481. for (let i = 0; i < 5; i++) input.handleInput("\x1b[C");
  482. // Simulate bracketed paste
  483. input.handleInput("\x1b[200~beep boop\x1b[201~");
  484. assert.strictEqual(input.getValue(), "hellobeep boop world");
  485. // Single undo should restore entire pre-paste state
  486. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  487. assert.strictEqual(input.getValue(), "hello world");
  488. });
  489. it("undoes Alt+D (delete word forward)", () => {
  490. const input = new Input();
  491. input.setValue("hello world");
  492. input.handleInput("\x01"); // Ctrl+A
  493. input.handleInput("\x1bd"); // Alt+D - deletes "hello"
  494. assert.strictEqual(input.getValue(), " world");
  495. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  496. assert.strictEqual(input.getValue(), "hello world");
  497. });
  498. it("cursor movement starts new undo unit", () => {
  499. const input = new Input();
  500. input.handleInput("a");
  501. input.handleInput("b");
  502. input.handleInput("c");
  503. input.handleInput("\x01"); // Ctrl+A - movement breaks coalescing
  504. input.handleInput("\x05"); // Ctrl+E
  505. input.handleInput("d");
  506. input.handleInput("e");
  507. assert.strictEqual(input.getValue(), "abcde");
  508. // Undo removes "de" (typed after movement)
  509. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  510. assert.strictEqual(input.getValue(), "abc");
  511. // Undo removes "abc"
  512. input.handleInput("\x1b[45;5u"); // Ctrl+- (undo)
  513. assert.strictEqual(input.getValue(), "");
  514. });
  515. });
  516. });