70160
Ce
cel-tui / files
cel-tui / packages / core / src / cel.test.ts
75.8 KB
1import { afterEach, describe, expect, test } from "bun:test"; 2import { HStack, VStack } from "./primitives/stacks.js"; 3import { Text } from "./primitives/text.js"; 4import { TextInput } from "./primitives/text-input.js"; 5import type { TerminalStartOptions } from "./terminal.js"; 6import { MockTerminal } from "./terminal.js"; 7import { testCel as cel, kittyEncode } from "./test-helpers.js"; 8 9// Kitty protocol byte sequences for commonly used keys 10const TAB = kittyEncode("tab"); 11const ENTER = kittyEncode("enter"); 12const ESCAPE = kittyEncode("escape"); 13const SHIFT_TAB = kittyEncode("shift+tab"); 14const CTRL_S = kittyEncode("ctrl+s"); 15const LEGACY_CTRL_R = "\x12"; 16// biome-ignore lint/complexity/useRegexLiterals: using RegExp here avoids control-character regex diagnostics. 17const TITLE_SEQUENCE_RE = new RegExp( 18 String.raw`\x1b\][02];([^\x07\x1b]*)(?:\x07|\x1b\\)`, 19 "g", 20); 21 22function extractTitlePayloads(output: string): string[] { 23 return Array.from(output.matchAll(TITLE_SEQUENCE_RE), (match) => { 24 const payload = match[1]; 25 if (payload === undefined) { 26 throw new Error("Missing terminal title payload"); 27 } 28 return payload; 29 }); 30} 31 32describe("cel end-to-end", () => { 33 let term: MockTerminal; 34 35 afterEach(() => { 36 cel.stop(); 37 }); 38 39 function setup(columns = 20, rows = 5): MockTerminal { 40 term = new MockTerminal(columns, rows); 41 cel.init(term); 42 return term; 43 } 44 45 async function waitForRender(): Promise<void> { 46 await cel._flush(); 47 } 48 49 function currentBuffer() { 50 const buf = cel._getBuffer(); 51 expect(buf).not.toBeNull(); 52 if (buf === null) { 53 throw new Error("Expected a rendered buffer"); 54 } 55 return buf; 56 } 57 58 test("renders a single Text node", async () => { 59 const term = setup(10, 1); 60 cel.viewport(() => Text("Hello")); 61 await waitForRender(); 62 63 expect(term.output).toContain("Hello"); 64 }); 65 66 test("renders styled text with color", async () => { 67 const term = setup(10, 1); 68 cel.viewport(() => Text("Red", { fgColor: "color01" })); 69 await waitForRender(); 70 71 expect(term.output).toContain("\x1b[31m"); 72 expect(term.output).toContain("Red"); 73 }); 74 75 test("renders bold text", async () => { 76 const term = setup(10, 1); 77 cel.viewport(() => Text("Bold", { bold: true })); 78 await waitForRender(); 79 80 expect(term.output).toContain("\x1b[1m"); 81 expect(term.output).toContain("Bold"); 82 }); 83 84 test("renders text with repeat fill", async () => { 85 const term = setup(10, 1); 86 cel.viewport(() => Text("-", { repeat: "fill" })); 87 await waitForRender(); 88 89 expect(term.output).toContain("----------"); 90 }); 91 92 test("renders VStack with children", async () => { 93 const term = setup(10, 4); 94 cel.viewport(() => VStack({}, [Text("Line 1"), Text("Line 2")])); 95 await waitForRender(); 96 97 expect(term.output).toContain("Line 1"); 98 expect(term.output).toContain("Line 2"); 99 }); 100 101 test("renders HStack with children", async () => { 102 const term = setup(20, 1); 103 cel.viewport(() => HStack({}, [Text("Left"), Text("Right")])); 104 await waitForRender(); 105 106 expect(term.output).toContain("Left"); 107 expect(term.output).toContain("Right"); 108 }); 109 110 test("re-renders on cel.render()", async () => { 111 const term = setup(10, 1); 112 let text = "First"; 113 cel.viewport(() => Text(text)); 114 await waitForRender(); 115 116 expect(term.output).toContain("First"); 117 118 term.clearOutput(); 119 text = "Second"; 120 cel.render(); 121 await waitForRender(); 122 123 expect(term.output).toContain("Second"); 124 }); 125 126 test("does not write terminal output for an unchanged frame", async () => { 127 const term = setup(10, 1); 128 cel.viewport(() => Text("same")); 129 await waitForRender(); 130 term.clearOutput(); 131 132 cel.render(); 133 await waitForRender(); 134 135 expect(term.output).toBe(""); 136 }); 137 138 test("redraw re-emits the full frame even when cells are unchanged", async () => { 139 const term = setup(10, 1); 140 cel.viewport(() => Text("same")); 141 await waitForRender(); 142 term.clearOutput(); 143 144 cel.redraw(); 145 await waitForRender(); 146 147 expect(term.output).toContain("\x1b[H\x1b[0msame"); 148 }); 149 150 test("redraw explicitly repairs an unchanged native cursor", async () => { 151 const term = setup(4, 1); 152 const props = { value: "hi", onChange: () => {}, focused: true }; 153 cel.viewport(() => TextInput(props)); 154 await waitForRender(); 155 term.clearOutput(); 156 157 cel.redraw(); 158 await waitForRender(); 159 160 expect(term.output).not.toContain("\x1b7"); 161 expect(term.output).not.toContain("\x1b8"); 162 expect(term.output).toContain("\x1b[1;3H\x1b[1 q\x1b[?25h"); 163 }); 164 165 test("setTheme replaces the active theme and performs a full redraw", async () => { 166 const term = setup(10, 1); 167 cel.viewport(() => Text("X", { fgColor: "color01" })); 168 await waitForRender(); 169 term.clearOutput(); 170 171 cel.setTheme({ 172 color00: 0, 173 color01: "#123456", 174 color02: 2, 175 color03: 3, 176 color04: 4, 177 color05: 5, 178 color06: 6, 179 color07: 7, 180 color08: 8, 181 color09: 9, 182 color10: 10, 183 color11: 11, 184 color12: 12, 185 color13: 13, 186 color14: 14, 187 color15: 15, 188 }); 189 await waitForRender(); 190 191 expect(term.output).toContain("\x1b[38;2;18;52;86mX"); 192 }); 193 194 test("batches multiple render calls", async () => { 195 const term = setup(10, 1); 196 let count = 0; 197 let renderCount = 0; 198 199 cel.viewport(() => { 200 renderCount++; 201 return Text(`Count: ${count}`); 202 }); 203 await waitForRender(); 204 expect(renderCount).toBe(1); 205 206 // Multiple synchronous render calls — should batch into one 207 count = 1; 208 cel.render(); 209 count = 2; 210 cel.render(); 211 count = 3; 212 cel.render(); 213 214 await waitForRender(); 215 216 expect(renderCount).toBe(2); 217 // Differential rendering: initial full render has "Count: 0", 218 // subsequent render emits only the changed character "3" 219 expect(term.output).toContain("Count: 0"); 220 expect(term.output).toContain("3"); 221 }); 222 223 test("wraps output in synchronized output markers", async () => { 224 const term = setup(10, 1); 225 cel.viewport(() => Text("Hi")); 226 await waitForRender(); 227 228 expect(term.output).toContain("\x1b[?2026h"); 229 expect(term.output).toContain("\x1b[?2026l"); 230 }); 231 232 describe("terminal title", () => { 233 test("setTitle writes a terminal title OSC sequence with the requested text", () => { 234 const term = setup(); 235 236 cel.setTitle("cel-tui demo"); 237 238 expect(extractTitlePayloads(term.output)).toEqual(["cel-tui demo"]); 239 }); 240 241 test("setTitle strips control characters from the title text", () => { 242 const term = setup(); 243 244 cel.setTitle("ab\x00c\nd\te\x1bf"); 245 246 expect(extractTitlePayloads(term.output)).toEqual(["abcdef"]); 247 }); 248 249 test("setTitle can be called multiple times and writes each title update", () => { 250 const term = setup(); 251 252 cel.setTitle("first"); 253 cel.setTitle("second"); 254 255 expect(extractTitlePayloads(term.output)).toEqual(["first", "second"]); 256 }); 257 258 test("setTitle suppresses duplicate sanitized titles", () => { 259 const term = setup(); 260 261 cel.setTitle("same"); 262 cel.setTitle("sa\nme"); 263 264 expect(extractTitlePayloads(term.output)).toEqual(["same"]); 265 }); 266 267 test("stop does not emit a title restore sequence", () => { 268 const term = setup(); 269 cel.setTitle("session"); 270 term.clearOutput(); 271 272 cel.stop(); 273 274 expect(extractTitlePayloads(term.output)).toEqual([]); 275 }); 276 }); 277 278 describe("input handling", () => { 279 test("init forwards advanced Kitty keyboard configuration", () => { 280 class ConfigTerminal extends MockTerminal { 281 startOptions?: TerminalStartOptions; 282 283 override start( 284 onInput: (data: string) => void, 285 onResize: () => void, 286 options?: TerminalStartOptions, 287 ): void { 288 this.startOptions = options; 289 super.start(onInput, onResize, options); 290 } 291 } 292 293 cel.stop(); 294 const configured = new ConfigTerminal(20, 5); 295 cel.init(configured, { 296 kittyKeyboard: { 297 reportEventTypes: true, 298 reportAssociatedText: true, 299 }, 300 }); 301 302 expect(configured.startOptions).toEqual({ 303 kittyKeyboard: { 304 reportEventTypes: true, 305 reportAssociatedText: true, 306 }, 307 }); 308 }); 309 310 test("routes advanced event metadata while release skips default actions", async () => { 311 const term = setup(20, 5); 312 let value = ""; 313 const inputEvents: string[] = []; 314 const rootEvents: string[] = []; 315 316 cel.viewport(() => 317 VStack( 318 { 319 onKeyPress: (_key, event) => { 320 rootEvents.push(`${event.eventType}:${event.text ?? ""}`); 321 }, 322 }, 323 [ 324 TextInput({ 325 value, 326 focused: true, 327 onChange: (next) => { 328 value = next; 329 }, 330 onKeyPress: (_key, event) => { 331 inputEvents.push(`${event.eventType}:${event.text ?? ""}`); 332 }, 333 }), 334 ], 335 ), 336 ); 337 await waitForRender(); 338 339 term.sendInput( 340 "\x1b[97;1:1;97u\x1b[97;1:2;97u\x1b[97;1:3;97u\x1b[13;1:3u", 341 ); 342 await waitForRender(); 343 344 expect(value).toBe("aa"); 345 expect(inputEvents).toEqual([ 346 "press:a", 347 "repeat:a", 348 "release:a", 349 "release:", 350 ]); 351 expect(rootEvents).toEqual(["release:a", "release:"]); 352 }); 353 354 test("release never triggers focused-container activation", async () => { 355 const term = setup(20, 5); 356 let clicks = 0; 357 cel.viewport(() => 358 HStack( 359 { 360 focused: true, 361 onClick: () => { 362 clicks++; 363 }, 364 }, 365 [Text("action")], 366 ), 367 ); 368 await waitForRender(); 369 370 term.sendInput("\x1b[13;1:3u"); 371 await waitForRender(); 372 expect(clicks).toBe(0); 373 374 term.sendInput("\x1b[13;1:2u"); 375 await waitForRender(); 376 expect(clicks).toBe(1); 377 }); 378 379 test("inserts multi-codepoint associated text as one advanced event", async () => { 380 const term = setup(20, 5); 381 let value = ""; 382 const events: string[] = []; 383 cel.viewport(() => 384 TextInput({ 385 value, 386 focused: true, 387 onChange: (next) => { 388 value = next; 389 }, 390 onKeyPress: (_key, event) => { 391 events.push(event.text ?? ""); 392 }, 393 }), 394 ); 395 await waitForRender(); 396 397 term.sendInput("\x1b[0;;128105:8205:128187u"); 398 await waitForRender(); 399 400 expect(value).toBe("👩💻"); 401 expect(events).toEqual(["👩💻"]); 402 }); 403 404 test("all-key events without associated text do not guess TextInput output", async () => { 405 const term = setup(20, 5); 406 let value = ""; 407 cel.viewport(() => 408 TextInput({ 409 value, 410 focused: true, 411 onChange: (next) => { 412 value = next; 413 }, 414 }), 415 ); 416 await waitForRender(); 417 418 term.sendInput("\x1b[97;1u"); 419 await waitForRender(); 420 421 expect(value).toBe(""); 422 }); 423 424 test("release Escape never triggers fallback blur", async () => { 425 const term = setup(20, 5); 426 let blurCount = 0; 427 cel.viewport(() => 428 TextInput({ 429 value: "", 430 focused: true, 431 onChange: () => {}, 432 onBlur: () => { 433 blurCount++; 434 }, 435 }), 436 ); 437 await waitForRender(); 438 439 term.sendInput("\x1b[27;1:3u"); 440 await waitForRender(); 441 expect(blurCount).toBe(0); 442 443 term.sendInput("\x1b[27;1:1u"); 444 await waitForRender(); 445 expect(blurCount).toBe(1); 446 }); 447 test("onClick fires on mouse click", async () => { 448 const term = setup(20, 5); 449 let clicked = false; 450 cel.viewport(() => 451 VStack({ width: 20, height: 5 }, [ 452 HStack( 453 { 454 onClick: () => { 455 clicked = true; 456 }, 457 }, 458 [Text("Click me")], 459 ), 460 ]), 461 ); 462 await waitForRender(); 463 464 // SGR mouse click release at (3, 0): ESC [ < 0 ; 4 ; 1 m 465 term.sendInput("\x1b[<0;4;1m"); 466 await waitForRender(); 467 468 expect(clicked).toBe(true); 469 }); 470 471 test("onKeyPress fires on key input", async () => { 472 const term = setup(20, 5); 473 let receivedKey = ""; 474 cel.viewport(() => 475 VStack( 476 { 477 width: 20, 478 height: 5, 479 onKeyPress: (key) => { 480 receivedKey = key; 481 }, 482 }, 483 [Text("hello")], 484 ), 485 ); 486 await waitForRender(); 487 488 // Send Ctrl+S 489 term.sendInput(CTRL_S); 490 await waitForRender(); 491 492 expect(receivedKey).toBe("ctrl+s"); 493 }); 494 495 test("batched keyboard events are all processed in order", async () => { 496 const term = setup(20, 5); 497 const receivedKeys: string[] = []; 498 cel.viewport(() => 499 VStack( 500 { 501 width: 20, 502 height: 5, 503 onKeyPress: (key) => { 504 receivedKeys.push(key); 505 }, 506 }, 507 [Text("hello")], 508 ), 509 ); 510 await waitForRender(); 511 512 term.sendInput(`${LEGACY_CTRL_R}${CTRL_S}`); 513 await waitForRender(); 514 515 expect(receivedKeys).toEqual(["ctrl+r", "ctrl+s"]); 516 }); 517 518 test("onScroll fires on mouse wheel using the adaptive default step", async () => { 519 const term = setup(20, 12); 520 let scrollOffset = 0; 521 cel.viewport(() => 522 VStack( 523 { 524 width: 20, 525 height: 12, 526 overflow: "scroll", 527 scrollOffset: scrollOffset, 528 onScroll: (offset) => { 529 scrollOffset = offset; 530 }, 531 }, 532 // 12-row viewport → adaptive wheel step = floor(12 / 3) = 4 533 Array.from({ length: 20 }, (_, i) => Text(`line ${i + 1}`)), 534 ), 535 ); 536 await waitForRender(); 537 538 term.sendInput("\x1b[<65;4;3M"); 539 await waitForRender(); 540 541 expect(scrollOffset).toBe(4); 542 }); 543 544 test("batched mouse scroll events are all processed", async () => { 545 const term = setup(20, 12); 546 let scrollOffset = 0; 547 cel.viewport(() => 548 VStack( 549 { 550 width: 20, 551 height: 12, 552 overflow: "scroll", 553 scrollOffset: scrollOffset, 554 onScroll: (offset) => { 555 scrollOffset = offset; 556 }, 557 }, 558 Array.from({ length: 30 }, (_, i) => Text(`line${i + 1}`)), 559 ), 560 ); 561 await waitForRender(); 562 563 // 3 scroll-down events batched in one data chunk (as real terminals do) 564 // 12-row viewport → adaptive wheel step = 4, so total = 12 565 term.sendInput("\x1b[<65;4;3M\x1b[<65;4;3M\x1b[<65;4;3M"); 566 await waitForRender(); 567 568 expect(scrollOffset).toBe(12); 569 }); 570 571 test("inner onScroll returning false propagates wheel input to parent", async () => { 572 const term = setup(20, 12); 573 let outerOffset = 0; 574 const innerOffsets: number[] = []; 575 const outerOffsets: number[] = []; 576 577 cel.viewport(() => 578 VStack( 579 { 580 width: 20, 581 height: 12, 582 overflow: "scroll", 583 scrollOffset: outerOffset, 584 onScroll: (offset) => { 585 outerOffset = offset; 586 outerOffsets.push(offset); 587 }, 588 }, 589 [ 590 VStack( 591 { 592 height: 6, 593 overflow: "scroll", 594 scrollOffset: 0, 595 onScroll: (offset) => { 596 innerOffsets.push(offset); 597 return false; 598 }, 599 }, 600 Array.from({ length: 10 }, (_, i) => Text(`inner ${i + 1}`)), 601 ), 602 ...Array.from({ length: 20 }, (_, i) => Text(`outer ${i + 1}`)), 603 ], 604 ), 605 ); 606 await waitForRender(); 607 608 term.sendInput("\x1b[<65;4;3M"); 609 await waitForRender(); 610 611 expect(innerOffsets).toEqual([3]); 612 expect(outerOffsets).toEqual([4]); 613 expect(outerOffset).toBe(4); 614 }); 615 616 test("void inner onScroll consumes wheel input and does not notify parent", async () => { 617 const term = setup(20, 12); 618 let innerOffset = 0; 619 let parentCalled = false; 620 621 cel.viewport(() => 622 VStack( 623 { 624 width: 20, 625 height: 12, 626 overflow: "scroll", 627 scrollOffset: 0, 628 onScroll: () => { 629 parentCalled = true; 630 }, 631 }, 632 [ 633 VStack( 634 { 635 height: 6, 636 overflow: "scroll", 637 scrollOffset: innerOffset, 638 onScroll: (offset) => { 639 innerOffset = offset; 640 }, 641 }, 642 Array.from({ length: 10 }, (_, i) => Text(`inner ${i + 1}`)), 643 ), 644 ...Array.from({ length: 20 }, (_, i) => Text(`outer ${i + 1}`)), 645 ], 646 ), 647 ); 648 await waitForRender(); 649 650 term.sendInput("\x1b[<65;4;3M"); 651 await waitForRender(); 652 653 expect(innerOffset).toBe(3); 654 expect(parentCalled).toBe(false); 655 }); 656 657 test("batched propagated scrolls skip the declined inner accumulator", async () => { 658 const term = setup(20, 12); 659 let outerOffset = 0; 660 const innerOffsets: number[] = []; 661 const outerOffsets: number[] = []; 662 663 cel.viewport(() => 664 VStack( 665 { 666 width: 20, 667 height: 12, 668 overflow: "scroll", 669 scrollOffset: outerOffset, 670 onScroll: (offset) => { 671 outerOffset = offset; 672 outerOffsets.push(offset); 673 }, 674 }, 675 [ 676 VStack( 677 { 678 height: 6, 679 overflow: "scroll", 680 scrollOffset: 0, 681 onScroll: (offset) => { 682 innerOffsets.push(offset); 683 return false; 684 }, 685 }, 686 Array.from({ length: 10 }, (_, i) => Text(`inner ${i + 1}`)), 687 ), 688 ...Array.from({ length: 20 }, (_, i) => Text(`outer ${i + 1}`)), 689 ], 690 ), 691 ); 692 await waitForRender(); 693 694 term.sendInput("\x1b[<65;4;3M\x1b[<65;4;3M"); 695 await waitForRender(); 696 697 expect(innerOffsets).toEqual([3, 3]); 698 expect(outerOffsets).toEqual([4, 8]); 699 expect(outerOffset).toBe(8); 700 }); 701 702 test("scroll clamps at max offset (no blank space past content)", async () => { 703 const term = setup(20, 5); 704 let scrollOffset = 0; 705 cel.viewport(() => 706 VStack( 707 { 708 width: 20, 709 height: 5, 710 overflow: "scroll", 711 scrollOffset: scrollOffset, 712 onScroll: (offset) => { 713 scrollOffset = offset; 714 }, 715 }, 716 // 8 items in 5-row viewport → max offset = 3 717 Array.from({ length: 8 }, (_, i) => Text(`item ${i + 1}`)), 718 ), 719 ); 720 await waitForRender(); 721 722 // Scroll down 10 times — should clamp at 3 723 for (let i = 0; i < 10; i++) { 724 term.sendInput("\x1b[<65;4;3M"); 725 await waitForRender(); 726 } 727 728 expect(scrollOffset).toBe(3); 729 }); 730 731 test("onScroll receives maxOffset", async () => { 732 const term = setup(20, 12); 733 let scrollOffset = 0; 734 let receivedMax = -1; 735 cel.viewport(() => 736 VStack( 737 { 738 width: 20, 739 height: 12, 740 overflow: "scroll", 741 scrollOffset: scrollOffset, 742 onScroll: (offset, maxOffset) => { 743 scrollOffset = offset; 744 receivedMax = maxOffset; 745 }, 746 }, 747 // 20 items in 12-row viewport → max offset = 8 748 Array.from({ length: 20 }, (_, i) => Text(`item ${i + 1}`)), 749 ), 750 ); 751 await waitForRender(); 752 753 term.sendInput("\x1b[<65;4;3M"); 754 await waitForRender(); 755 756 expect(scrollOffset).toBe(4); 757 expect(receivedMax).toBe(8); 758 }); 759 760 test("scrollStep overrides the adaptive default", async () => { 761 const term = setup(20, 12); 762 let scrollOffset = 0; 763 cel.viewport(() => 764 VStack( 765 { 766 width: 20, 767 height: 12, 768 overflow: "scroll", 769 scrollStep: 6, 770 scrollOffset: scrollOffset, 771 onScroll: (offset) => { 772 scrollOffset = offset; 773 }, 774 }, 775 Array.from({ length: 30 }, (_, i) => Text(`line ${i + 1}`)), 776 ), 777 ); 778 await waitForRender(); 779 780 term.sendInput("\x1b[<65;4;3M"); 781 await waitForRender(); 782 783 expect(scrollOffset).toBe(6); 784 }); 785 786 test("uncontrolled scroll works without onScroll", async () => { 787 const term = setup(20, 12); 788 cel.viewport(() => 789 VStack( 790 { 791 width: 20, 792 height: 12, 793 overflow: "scroll", 794 }, 795 Array.from({ length: 20 }, (_, i) => Text(`line ${i + 1}`)), 796 ), 797 ); 798 await waitForRender(); 799 800 // Before scroll, line 1 should be visible at row 0 801 const buf1 = currentBuffer(); 802 expect(buf1.get(0, 0).char).toBe("l"); // "line 1" 803 804 term.sendInput("\x1b[<65;4;3M"); 805 await waitForRender(); 806 807 // After scrolling by the adaptive step (4), line 5 should now be at row 0 808 const buf2 = currentBuffer(); 809 let row0 = ""; 810 for (let x = 0; x < 6; x++) row0 += buf2.get(x, 0).char; 811 expect(row0).toBe("line 5"); 812 }); 813 814 test("stateKey preserves uncontrolled scroll when a sibling is inserted", async () => { 815 const term = setup(20, 4); 816 let showHeader = false; 817 818 cel.viewport(() => 819 VStack( 820 { width: 20, height: 4 }, 821 [ 822 showHeader ? Text("header") : null, 823 VStack( 824 { 825 stateKey: "messages", 826 width: 20, 827 height: 3, 828 overflow: "scroll", 829 }, 830 Array.from({ length: 6 }, (_, i) => Text(`line ${i + 1}`)), 831 ), 832 ].filter((node) => node !== null), 833 ), 834 ); 835 await waitForRender(); 836 837 term.sendInput("\x1b[<65;4;2M"); 838 await waitForRender(); 839 showHeader = true; 840 cel.render(); 841 await waitForRender(); 842 843 const row = Array.from( 844 { length: 6 }, 845 (_, x) => currentBuffer().get(x, 1).char, 846 ).join(""); 847 expect(row).toBe("line 4"); 848 }); 849 850 test("unmounting a stateKey disposes uncontrolled scroll state", async () => { 851 const term = setup(20, 3); 852 let mounted = true; 853 854 cel.viewport(() => 855 VStack( 856 { width: 20, height: 3 }, 857 mounted 858 ? [ 859 VStack( 860 { 861 stateKey: "messages", 862 width: 20, 863 height: 3, 864 overflow: "scroll", 865 }, 866 Array.from({ length: 6 }, (_, i) => Text(`line ${i + 1}`)), 867 ), 868 ] 869 : [Text("unmounted")], 870 ), 871 ); 872 await waitForRender(); 873 874 term.sendInput("\x1b[<65;4;2M"); 875 await waitForRender(); 876 mounted = false; 877 cel.render(); 878 await waitForRender(); 879 mounted = true; 880 cel.render(); 881 await waitForRender(); 882 883 const row = Array.from( 884 { length: 6 }, 885 (_, x) => currentBuffer().get(x, 0).char, 886 ).join(""); 887 expect(row).toBe("line 1"); 888 }); 889 890 test("uncontrolled scroll updates framework state when onScroll consumes", async () => { 891 const term = setup(20, 3); 892 const offsets: number[] = []; 893 894 cel.viewport(() => 895 VStack( 896 { 897 width: 20, 898 height: 3, 899 overflow: "scroll", 900 onScroll: (offset) => { 901 offsets.push(offset); 902 }, 903 }, 904 Array.from({ length: 6 }, (_, i) => Text(`line ${i + 1}`)), 905 ), 906 ); 907 await waitForRender(); 908 909 term.sendInput("\x1b[<65;4;2M"); 910 await waitForRender(); 911 912 expect(offsets).toEqual([3]); 913 const row0 = Array.from( 914 { length: 6 }, 915 (_, x) => currentBuffer().get(x, 0).char, 916 ).join(""); 917 expect(row0).toBe("line 4"); 918 }); 919 920 test("uncontrolled onScroll returning false leaves state unchanged and bubbles", async () => { 921 const term = setup(20, 6); 922 const innerOffsets: number[] = []; 923 const outerOffsets: number[] = []; 924 925 cel.viewport(() => 926 VStack( 927 { 928 width: 20, 929 height: 6, 930 overflow: "scroll", 931 onScroll: (offset) => { 932 outerOffsets.push(offset); 933 }, 934 }, 935 [ 936 VStack( 937 { 938 height: 3, 939 overflow: "scroll", 940 onScroll: (offset) => { 941 innerOffsets.push(offset); 942 return false; 943 }, 944 }, 945 Array.from({ length: 6 }, (_, i) => Text(`inner ${i + 1}`)), 946 ), 947 ...Array.from({ length: 6 }, (_, i) => Text(`outer ${i + 1}`)), 948 ], 949 ), 950 ); 951 await waitForRender(); 952 953 term.sendInput("\x1b[<65;4;2M"); 954 await waitForRender(); 955 956 expect(innerOffsets).toEqual([3]); 957 expect(outerOffsets).toEqual([3]); 958 const row0 = Array.from( 959 { length: 7 }, 960 (_, x) => currentBuffer().get(x, 0).char, 961 ).join(""); 962 expect(row0).toBe("outer 1"); 963 }); 964 965 test("uncontrolled scroll clamps at max offset", async () => { 966 const term = setup(20, 3); 967 cel.viewport(() => 968 VStack( 969 { 970 width: 20, 971 height: 3, 972 overflow: "scroll", 973 }, 974 // 5 items in 3-row viewport → max offset = 2 975 Array.from({ length: 5 }, (_, i) => Text(`item ${i + 1}`)), 976 ), 977 ); 978 await waitForRender(); 979 980 // Scroll down 10 times — should clamp at 2 981 for (let i = 0; i < 10; i++) { 982 term.sendInput("\x1b[<65;4;2M"); 983 await waitForRender(); 984 } 985 986 // Row 0 should show item 3 (offset=2), row 2 should show item 5 987 const buf = currentBuffer(); 988 let row0 = ""; 989 for (let x = 0; x < 6; x++) row0 += buf.get(x, 0).char; 990 expect(row0).toBe("item 3"); 991 992 let row2 = ""; 993 for (let x = 0; x < 6; x++) row2 += buf.get(x, 2).char; 994 expect(row2).toBe("item 5"); 995 }); 996 997 test("scroll clamps correctly with padding", async () => { 998 const term = setup(20, 7); 999 let scrollOffset = 0; 1000 cel.viewport(() => 1001 VStack( 1002 { 1003 width: 20, 1004 height: 7, 1005 overflow: "scroll", 1006 padding: { y: 1 }, 1007 scrollOffset: scrollOffset, 1008 onScroll: (offset) => { 1009 scrollOffset = offset; 1010 }, 1011 }, 1012 // 7 items, viewport inner height = 5 (7 - 2*1), max offset = 2 1013 Array.from({ length: 7 }, (_, i) => Text(`line ${i}`)), 1014 ), 1015 ); 1016 await waitForRender(); 1017 1018 // Scroll down 10 times — should clamp at 2 (7 items - 5 visible = 2) 1019 for (let i = 0; i < 10; i++) { 1020 term.sendInput("\x1b[<65;4;4M"); 1021 await waitForRender(); 1022 } 1023 1024 expect(scrollOffset).toBe(2); 1025 }); 1026 1027 test("scroll up unsticks from Infinity scrollOffset (sticky-bottom pattern)", async () => { 1028 const term = setup(20, 5); 1029 let scrollOffset = 0; 1030 let stickToBottom = true; 1031 1032 cel.viewport(() => 1033 VStack( 1034 { 1035 width: 20, 1036 height: 5, 1037 overflow: "scroll", 1038 scrollbar: true, 1039 scrollOffset: stickToBottom ? Infinity : scrollOffset, 1040 onScroll: (offset, maxOffset) => { 1041 scrollOffset = offset; 1042 stickToBottom = offset >= maxOffset; 1043 cel.render(); 1044 }, 1045 }, 1046 // 10 items in 5-row viewport → max offset = 5, adaptive wheel step = 3 1047 Array.from({ length: 10 }, (_, i) => Text(`line ${i + 1}`)), 1048 ), 1049 ); 1050 await waitForRender(); 1051 1052 // Initially stuck to bottom (Infinity → clamped to maxOffset=5) 1053 expect(stickToBottom).toBe(true); 1054 1055 // Scroll up — should unstick from bottom 1056 term.sendInput("\x1b[<64;4;3M"); 1057 await waitForRender(); 1058 1059 expect(stickToBottom).toBe(false); 1060 expect(scrollOffset).toBe(2); // maxOffset(5) - 3 = 2 1061 }); 1062 }); 1063 1064 describe("focus system", () => { 1065 test("Tab moves focus to next focusable element", async () => { 1066 const term = setup(20, 5); 1067 const focused: string[] = []; 1068 const blurred: string[] = []; 1069 let focus1 = false; 1070 let focus2 = false; 1071 1072 cel.viewport(() => 1073 VStack({ width: 20, height: 5 }, [ 1074 HStack( 1075 { 1076 onClick: () => {}, 1077 focused: focus1, 1078 onFocus: (event) => { 1079 focus1 = true; 1080 focus2 = false; 1081 focused.push(`btn1:${event.reason}`); 1082 }, 1083 onBlur: (event) => { 1084 focus1 = false; 1085 blurred.push(`btn1:${event.reason}`); 1086 }, 1087 }, 1088 [Text("Btn1")], 1089 ), 1090 HStack( 1091 { 1092 onClick: () => {}, 1093 focused: focus2, 1094 onFocus: (event) => { 1095 focus2 = true; 1096 focus1 = false; 1097 focused.push(`btn2:${event.reason}`); 1098 }, 1099 onBlur: (event) => { 1100 focus2 = false; 1101 blurred.push(`btn2:${event.reason}`); 1102 }, 1103 }, 1104 [Text("Btn2")], 1105 ), 1106 ]), 1107 ); 1108 await waitForRender(); 1109 1110 // Tab to focus first element 1111 term.sendInput(TAB); 1112 await waitForRender(); 1113 expect(focused).toEqual(["btn1:tab"]); 1114 expect(blurred).toEqual([]); 1115 1116 // Tab to focus second element 1117 term.sendInput(TAB); 1118 await waitForRender(); 1119 expect(focused).toEqual(["btn1:tab", "btn2:tab"]); 1120 expect(blurred).toEqual(["btn1:tab"]); 1121 }); 1122 1123 test("Shift+Tab moves focus backwards", async () => { 1124 const term = setup(20, 5); 1125 let focus1 = false; 1126 let focus2 = true; 1127 const focused: string[] = []; 1128 const blurred: string[] = []; 1129 1130 cel.viewport(() => 1131 VStack({ width: 20, height: 5 }, [ 1132 HStack( 1133 { 1134 onClick: () => {}, 1135 focused: focus1, 1136 onFocus: (event) => { 1137 focus1 = true; 1138 focus2 = false; 1139 focused.push(`btn1:${event.reason}`); 1140 }, 1141 onBlur: (event) => { 1142 focus1 = false; 1143 blurred.push(`btn1:${event.reason}`); 1144 }, 1145 }, 1146 [Text("Btn1")], 1147 ), 1148 HStack( 1149 { 1150 onClick: () => {}, 1151 focused: focus2, 1152 onFocus: (event) => { 1153 focus2 = true; 1154 focus1 = false; 1155 focused.push(`btn2:${event.reason}`); 1156 }, 1157 onBlur: (event) => { 1158 focus2 = false; 1159 blurred.push(`btn2:${event.reason}`); 1160 }, 1161 }, 1162 [Text("Btn2")], 1163 ), 1164 ]), 1165 ); 1166 await waitForRender(); 1167 1168 // Shift+Tab from btn2 should focus btn1 1169 term.sendInput(SHIFT_TAB); 1170 await waitForRender(); 1171 expect(focused).toEqual(["btn1:shift+tab"]); 1172 expect(blurred).toEqual(["btn2:shift+tab"]); 1173 }); 1174 1175 test("Escape unfocuses the current element", async () => { 1176 const term = setup(20, 5); 1177 let blurReason = ""; 1178 let btnFocused = true; 1179 1180 cel.viewport(() => 1181 VStack({ width: 20, height: 5 }, [ 1182 HStack( 1183 { 1184 onClick: () => {}, 1185 focused: btnFocused, 1186 onBlur: (event) => { 1187 blurReason = event.reason; 1188 btnFocused = false; 1189 }, 1190 }, 1191 [Text("Btn")], 1192 ), 1193 ]), 1194 ); 1195 await waitForRender(); 1196 1197 term.sendInput(ESCAPE); 1198 await waitForRender(); 1199 expect(blurReason).toBe("escape"); 1200 }); 1201 1202 test("Enter activates focused clickable container", async () => { 1203 const term = setup(20, 5); 1204 let clicked = false; 1205 1206 cel.viewport(() => 1207 VStack({ width: 20, height: 5 }, [ 1208 HStack( 1209 { 1210 onClick: () => { 1211 clicked = true; 1212 }, 1213 focused: true, 1214 }, 1215 [Text("Btn")], 1216 ), 1217 ]), 1218 ); 1219 await waitForRender(); 1220 1221 term.sendInput(ENTER); 1222 await waitForRender(); 1223 expect(clicked).toBe(true); 1224 }); 1225 1226 test("mouse click on focusable element fires onFocus", async () => { 1227 const term = setup(20, 5); 1228 let btnFocused = false; 1229 let focusReason = ""; 1230 1231 cel.viewport(() => 1232 VStack({ width: 20, height: 5 }, [ 1233 HStack( 1234 { 1235 onClick: () => {}, 1236 focused: btnFocused, 1237 onFocus: (event) => { 1238 btnFocused = true; 1239 focusReason = event.reason; 1240 }, 1241 }, 1242 [Text("Btn")], 1243 ), 1244 ]), 1245 ); 1246 await waitForRender(); 1247 1248 // Click on the button at (1, 0) 1249 term.sendInput("\x1b[<0;2;1m"); 1250 await waitForRender(); 1251 expect(btnFocused).toBe(true); 1252 expect(focusReason).toBe("click"); 1253 }); 1254 1255 test("Tab wraps around to first element", async () => { 1256 const term = setup(20, 5); 1257 let focus1 = false; 1258 let focus2 = true; 1259 const focused: string[] = []; 1260 1261 cel.viewport(() => 1262 VStack({ width: 20, height: 5 }, [ 1263 HStack( 1264 { 1265 onClick: () => {}, 1266 focused: focus1, 1267 onFocus: () => { 1268 focus1 = true; 1269 focus2 = false; 1270 focused.push("btn1"); 1271 }, 1272 onBlur: () => { 1273 focus1 = false; 1274 }, 1275 }, 1276 [Text("Btn1")], 1277 ), 1278 HStack( 1279 { 1280 onClick: () => {}, 1281 focused: focus2, 1282 onFocus: () => { 1283 focus2 = true; 1284 focus1 = false; 1285 focused.push("btn2"); 1286 }, 1287 onBlur: () => { 1288 focus2 = false; 1289 }, 1290 }, 1291 [Text("Btn2")], 1292 ), 1293 ]), 1294 ); 1295 await waitForRender(); 1296 1297 // Tab from btn2 (last) should wrap to btn1 1298 term.sendInput(TAB); 1299 await waitForRender(); 1300 expect(focused).toEqual(["btn1"]); 1301 }); 1302 }); 1303 1304 describe("onKeyPress bubbling", () => { 1305 test("void handler consumes key — no further bubbling (backward compat)", async () => { 1306 const term = setup(20, 5); 1307 const received: string[] = []; 1308 1309 cel.viewport(() => 1310 VStack( 1311 { 1312 width: 20, 1313 height: 5, 1314 onKeyPress: (key) => { 1315 received.push(`root:${key}`); 1316 }, 1317 }, 1318 [ 1319 VStack( 1320 { 1321 onKeyPress: (key) => { 1322 received.push(`mid:${key}`); 1323 // void return = consumed 1324 }, 1325 }, 1326 [ 1327 HStack( 1328 { 1329 onClick: () => {}, 1330 focused: true, 1331 }, 1332 [Text("Focused Btn")], 1333 ), 1334 ], 1335 ), 1336 ], 1337 ), 1338 ); 1339 await waitForRender(); 1340 1341 term.sendInput("x"); 1342 await waitForRender(); 1343 1344 // mid handler consumes (void return), root never sees the key 1345 expect(received).toEqual(["mid:x"]); 1346 }); 1347 1348 test("returning false continues bubbling to parent handlers", async () => { 1349 const term = setup(20, 5); 1350 const received: string[] = []; 1351 1352 cel.viewport(() => 1353 VStack( 1354 { 1355 width: 20, 1356 height: 5, 1357 onKeyPress: (key) => { 1358 received.push(`root:${key}`); 1359 }, 1360 }, 1361 [ 1362 VStack( 1363 { 1364 onKeyPress: (key) => { 1365 received.push(`mid:${key}`); 1366 return false; // not consumed — keep bubbling 1367 }, 1368 }, 1369 [ 1370 HStack( 1371 { 1372 onClick: () => {}, 1373 focused: true, 1374 }, 1375 [Text("Focused Btn")], 1376 ), 1377 ], 1378 ), 1379 ], 1380 ), 1381 ); 1382 await waitForRender(); 1383 1384 term.sendInput("x"); 1385 await waitForRender(); 1386 1387 // mid returns false, so key continues to root 1388 expect(received).toEqual(["mid:x", "root:x"]); 1389 }); 1390 1391 test("selective bubbling — consume some keys, pass others", async () => { 1392 const term = setup(20, 5); 1393 const received: string[] = []; 1394 1395 cel.viewport(() => 1396 VStack( 1397 { 1398 width: 20, 1399 height: 5, 1400 onKeyPress: (key) => { 1401 received.push(`root:${key}`); 1402 }, 1403 }, 1404 [ 1405 VStack( 1406 { 1407 onKeyPress: (key) => { 1408 received.push(`mid:${key}`); 1409 // Consume "a" but pass through everything else 1410 if (key === "a") return; // consumed 1411 return false; // not consumed 1412 }, 1413 }, 1414 [ 1415 HStack( 1416 { 1417 onClick: () => {}, 1418 focused: true, 1419 }, 1420 [Text("Focused Btn")], 1421 ), 1422 ], 1423 ), 1424 ], 1425 ), 1426 ); 1427 await waitForRender(); 1428 1429 term.sendInput("a"); 1430 await waitForRender(); 1431 expect(received).toEqual(["mid:a"]); // consumed by mid 1432 1433 received.length = 0; 1434 term.sendInput("b"); 1435 await waitForRender(); 1436 expect(received).toEqual(["mid:b", "root:b"]); // passed through to root 1437 }); 1438 1439 test("all handlers return false — every handler in chain is called", async () => { 1440 const term = setup(20, 5); 1441 const received: string[] = []; 1442 1443 cel.viewport(() => 1444 VStack( 1445 { 1446 width: 20, 1447 height: 5, 1448 onKeyPress: (key) => { 1449 received.push(`root:${key}`); 1450 return false; 1451 }, 1452 }, 1453 [ 1454 VStack( 1455 { 1456 onKeyPress: (key) => { 1457 received.push(`mid:${key}`); 1458 return false; 1459 }, 1460 }, 1461 [ 1462 HStack( 1463 { 1464 onClick: () => {}, 1465 focused: true, 1466 }, 1467 [Text("Focused Btn")], 1468 ), 1469 ], 1470 ), 1471 ], 1472 ), 1473 ); 1474 await waitForRender(); 1475 1476 term.sendInput("x"); 1477 await waitForRender(); 1478 1479 expect(received).toEqual(["mid:x", "root:x"]); 1480 }); 1481 1482 test("ancestor onKeyPress can consume Escape before blur", async () => { 1483 const term = setup(20, 5); 1484 const received: string[] = []; 1485 let btnFocused = true; 1486 let blurred = false; 1487 1488 cel.viewport(() => 1489 VStack( 1490 { 1491 width: 20, 1492 height: 5, 1493 onKeyPress: (key) => { 1494 received.push(key); 1495 }, 1496 }, 1497 [ 1498 HStack( 1499 { 1500 onClick: () => {}, 1501 focused: btnFocused, 1502 onBlur: () => { 1503 blurred = true; 1504 btnFocused = false; 1505 }, 1506 }, 1507 [Text("Btn")], 1508 ), 1509 ], 1510 ), 1511 ); 1512 await waitForRender(); 1513 1514 term.sendInput(ESCAPE); 1515 await waitForRender(); 1516 1517 expect(received).toEqual(["escape"]); 1518 expect(blurred).toBe(false); 1519 expect(btnFocused).toBe(true); 1520 }); 1521 1522 test("Escape blurs after bubbling when ancestor handlers return false", async () => { 1523 const term = setup(20, 5); 1524 const received: string[] = []; 1525 let btnFocused = true; 1526 let blurred = false; 1527 1528 cel.viewport(() => 1529 VStack( 1530 { 1531 width: 20, 1532 height: 5, 1533 onKeyPress: (key) => { 1534 received.push(key); 1535 return false; 1536 }, 1537 }, 1538 [ 1539 HStack( 1540 { 1541 onClick: () => {}, 1542 focused: btnFocused, 1543 onBlur: () => { 1544 blurred = true; 1545 btnFocused = false; 1546 }, 1547 }, 1548 [Text("Btn")], 1549 ), 1550 ], 1551 ), 1552 ); 1553 await waitForRender(); 1554 1555 term.sendInput(ESCAPE); 1556 await waitForRender(); 1557 1558 expect(received).toEqual(["escape"]); 1559 expect(blurred).toBe(true); 1560 expect(btnFocused).toBe(false); 1561 }); 1562 1563 test("key reaches root onKeyPress when no intermediate handlers", async () => { 1564 const term = setup(20, 5); 1565 let rootKey = ""; 1566 1567 cel.viewport(() => 1568 VStack( 1569 { 1570 width: 20, 1571 height: 5, 1572 onKeyPress: (key) => { 1573 rootKey = key; 1574 }, 1575 }, 1576 [ 1577 HStack( 1578 { 1579 onClick: () => {}, 1580 focused: true, 1581 }, 1582 [Text("Btn")], 1583 ), 1584 ], 1585 ), 1586 ); 1587 await waitForRender(); 1588 1589 term.sendInput("y"); 1590 await waitForRender(); 1591 expect(rootKey).toBe("y"); 1592 }); 1593 1594 test("modifier key from focused TextInput bubbles to ancestors", async () => { 1595 const term = setup(20, 5); 1596 let parentKey = ""; 1597 let inputValue = "hello"; 1598 1599 cel.viewport(() => 1600 VStack( 1601 { 1602 width: 20, 1603 height: 5, 1604 onKeyPress: (key) => { 1605 parentKey = key; 1606 }, 1607 }, 1608 [ 1609 TextInput({ 1610 value: inputValue, 1611 onChange: (v) => { 1612 inputValue = v; 1613 }, 1614 focused: true, 1615 }), 1616 ], 1617 ), 1618 ); 1619 await waitForRender(); 1620 1621 // Ctrl+S is not an editing key — should bubble up 1622 term.sendInput(CTRL_S); 1623 await waitForRender(); 1624 expect(parentKey).toBe("ctrl+s"); 1625 }); 1626 1627 test("TextInput onKeyPress returning false prevents Escape blur", async () => { 1628 const term = setup(20, 5); 1629 let parentKey = ""; 1630 let inputKey = ""; 1631 let inputValue = "hello"; 1632 let inputFocused = true; 1633 1634 cel.viewport(() => 1635 VStack( 1636 { 1637 width: 20, 1638 height: 5, 1639 onKeyPress: (key) => { 1640 parentKey = key; 1641 }, 1642 }, 1643 [ 1644 TextInput({ 1645 value: inputValue, 1646 onChange: (v) => { 1647 inputValue = v; 1648 }, 1649 focused: inputFocused, 1650 onBlur: () => { 1651 inputFocused = false; 1652 }, 1653 onKeyPress: (key) => { 1654 inputKey = key; 1655 if (key === "escape") return false; 1656 }, 1657 }), 1658 ], 1659 ), 1660 ); 1661 await waitForRender(); 1662 1663 term.sendInput(ESCAPE); 1664 await waitForRender(); 1665 1666 expect(inputKey).toBe("escape"); 1667 expect(parentKey).toBe(""); 1668 expect(inputFocused).toBe(true); 1669 expect(inputValue).toBe("hello"); 1670 }); 1671 1672 test("unfocused state routes key to root handler", async () => { 1673 const term = setup(20, 5); 1674 let rootKey = ""; 1675 1676 cel.viewport(() => 1677 VStack( 1678 { 1679 width: 20, 1680 height: 5, 1681 onKeyPress: (key) => { 1682 rootKey = key; 1683 }, 1684 }, 1685 [Text("no focus")], 1686 ), 1687 ); 1688 await waitForRender(); 1689 1690 term.sendInput("z"); 1691 await waitForRender(); 1692 expect(rootKey).toBe("z"); 1693 }); 1694 1695 test("unfocused key does not leak through a topmost layer without a root handler", async () => { 1696 const term = setup(20, 5); 1697 let baseLayerKey = ""; 1698 1699 cel.viewport(() => [ 1700 VStack( 1701 { 1702 width: 20, 1703 height: 5, 1704 onKeyPress: (key) => { 1705 baseLayerKey = key; 1706 }, 1707 }, 1708 [Text("base")], 1709 ), 1710 VStack({ width: 20, height: 5 }, [Text("overlay")]), 1711 ]); 1712 await waitForRender(); 1713 1714 term.sendInput("z"); 1715 await waitForRender(); 1716 expect(baseLayerKey).toBe(""); 1717 }); 1718 }); 1719 1720 describe("TextInput Tab handling", () => { 1721 test("Tab inserts \\t when TextInput is focused", async () => { 1722 const term = setup(20, 5); 1723 let value = "hello"; 1724 cel.viewport(() => 1725 VStack({ width: 20, height: 5 }, [ 1726 TextInput({ 1727 value, 1728 focused: true, 1729 onChange: (v) => { 1730 value = v; 1731 }, 1732 }), 1733 ]), 1734 ); 1735 await waitForRender(); 1736 1737 // Tab should insert \t, not traverse focus 1738 term.sendInput(TAB); 1739 await waitForRender(); 1740 expect(value).toBe("hello\t"); 1741 }); 1742 1743 test("Shift+Enter inserts newline in TextInput", async () => { 1744 const term = setup(20, 5); 1745 let value = "hello"; 1746 cel.viewport(() => 1747 VStack({ width: 20, height: 5 }, [ 1748 TextInput({ 1749 value, 1750 focused: true, 1751 onChange: (v) => { 1752 value = v; 1753 }, 1754 }), 1755 ]), 1756 ); 1757 await waitForRender(); 1758 1759 // Shift+Enter should insert a newline at cursor position 1760 term.sendInput(kittyEncode("shift+enter")); 1761 await waitForRender(); 1762 expect(value).toBe("hello\n"); 1763 }); 1764 1765 test("Shift+Enter inserts newline at cursor, not just at end", async () => { 1766 const term = setup(20, 5); 1767 let value = "hello"; 1768 // Stable onChange ref so cursor state persists across re-renders 1769 const onChange = (v: string) => { 1770 value = v; 1771 }; 1772 cel.viewport(() => 1773 VStack({ width: 20, height: 5 }, [ 1774 TextInput({ 1775 value, 1776 focused: true, 1777 onChange, 1778 }), 1779 ]), 1780 ); 1781 await waitForRender(); 1782 1783 // Move cursor left 3 times (to position 2: "he|llo") 1784 term.sendInput(kittyEncode("left")); 1785 await waitForRender(); 1786 term.sendInput(kittyEncode("left")); 1787 await waitForRender(); 1788 term.sendInput(kittyEncode("left")); 1789 await waitForRender(); 1790 1791 // Shift+Enter should insert newline at cursor 1792 term.sendInput(kittyEncode("shift+enter")); 1793 await waitForRender(); 1794 expect(value).toBe("he\nllo"); 1795 }); 1796 1797 test("Shift+Enter inserts newline even when onKeyPress intercepts Enter", async () => { 1798 const term = setup(20, 5); 1799 let value = "hello"; 1800 let submitted = false; 1801 cel.viewport(() => 1802 VStack({ width: 20, height: 5 }, [ 1803 TextInput({ 1804 value, 1805 focused: true, 1806 onChange: (v) => { 1807 value = v; 1808 }, 1809 onKeyPress: (key) => { 1810 if (key === "enter") { 1811 submitted = true; 1812 return false; 1813 } 1814 }, 1815 }), 1816 ]), 1817 ); 1818 await waitForRender(); 1819 1820 // Shift+Enter is a different key — should insert newline, not trigger onKeyPress for "enter" 1821 term.sendInput(kittyEncode("shift+enter")); 1822 await waitForRender(); 1823 expect(submitted).toBe(false); 1824 expect(value).toBe("hello\n"); 1825 }); 1826 1827 test("Shift+Tab does not traverse focus when TextInput is focused", async () => { 1828 const term = setup(20, 5); 1829 let value = "hello"; 1830 let btnFocused = false; 1831 1832 cel.viewport(() => 1833 VStack({ width: 20, height: 5 }, [ 1834 HStack( 1835 { 1836 onClick: () => {}, 1837 focused: btnFocused, 1838 onFocus: () => { 1839 btnFocused = true; 1840 }, 1841 }, 1842 [Text("Btn")], 1843 ), 1844 TextInput({ 1845 value, 1846 focused: true, 1847 onChange: (v) => { 1848 value = v; 1849 }, 1850 }), 1851 ]), 1852 ); 1853 await waitForRender(); 1854 1855 // Shift+Tab should not move focus away from TextInput 1856 term.sendInput(SHIFT_TAB); 1857 await waitForRender(); 1858 expect(btnFocused).toBe(false); 1859 }); 1860 1861 test("Tab traverses focus when TextInput is NOT focused", async () => { 1862 const term = setup(20, 5); 1863 let tiFocused = false; 1864 1865 cel.viewport(() => 1866 VStack({ width: 20, height: 5 }, [ 1867 TextInput({ 1868 value: "text", 1869 focused: tiFocused, 1870 onChange: () => {}, 1871 onFocus: () => { 1872 tiFocused = true; 1873 }, 1874 }), 1875 ]), 1876 ); 1877 await waitForRender(); 1878 1879 // Tab should traverse to the TextInput (it's not focused yet) 1880 term.sendInput(TAB); 1881 await waitForRender(); 1882 expect(tiFocused).toBe(true); 1883 }); 1884 1885 test("Escape then Tab leaves TextInput and traverses", async () => { 1886 const term = setup(20, 5); 1887 let tiFocused = true; 1888 let btnFocused = false; 1889 1890 cel.viewport(() => 1891 VStack({ width: 20, height: 5 }, [ 1892 HStack( 1893 { 1894 onClick: () => {}, 1895 focused: btnFocused, 1896 onFocus: () => { 1897 btnFocused = true; 1898 tiFocused = false; 1899 }, 1900 }, 1901 [Text("Btn")], 1902 ), 1903 TextInput({ 1904 value: "text", 1905 focused: tiFocused, 1906 onChange: () => {}, 1907 onBlur: () => { 1908 tiFocused = false; 1909 }, 1910 }), 1911 ]), 1912 ); 1913 await waitForRender(); 1914 1915 // Escape to leave TextInput 1916 term.sendInput(ESCAPE); 1917 await waitForRender(); 1918 expect(tiFocused).toBe(false); 1919 1920 // Tab should now traverse to the button 1921 term.sendInput(TAB); 1922 await waitForRender(); 1923 expect(btnFocused).toBe(true); 1924 }); 1925 1926 test("Escape then Tab advances past the element that was unfocused", async () => { 1927 const term = setup(20, 5); 1928 let focused: string | null = "ti"; 1929 1930 cel.viewport(() => 1931 VStack({ width: 20, height: 5 }, [ 1932 TextInput({ 1933 value: "text", 1934 focused: focused === "ti", 1935 onChange: () => {}, 1936 onFocus: () => { 1937 focused = "ti"; 1938 }, 1939 onBlur: () => { 1940 if (focused === "ti") focused = null; 1941 }, 1942 }), 1943 HStack( 1944 { 1945 onClick: () => {}, 1946 focused: focused === "btn", 1947 onFocus: () => { 1948 focused = "btn"; 1949 }, 1950 onBlur: () => { 1951 if (focused === "btn") focused = null; 1952 }, 1953 }, 1954 [Text("Btn")], 1955 ), 1956 ]), 1957 ); 1958 await waitForRender(); 1959 expect(focused as string | null).toBe("ti"); 1960 1961 // Escape to unfocus TextInput 1962 term.sendInput(ESCAPE); 1963 await waitForRender(); 1964 expect(focused as string | null).toBe(null); 1965 1966 // Tab should go FORWARD to the button (next after TextInput), 1967 // not back to the TextInput 1968 term.sendInput(TAB); 1969 await waitForRender(); 1970 expect(focused as string | null).toBe("btn"); 1971 }); 1972 1973 test("Escape then Shift+Tab goes backward from the unfocused element", async () => { 1974 const term = setup(20, 5); 1975 let focused: string | null = null; 1976 1977 cel.viewport(() => 1978 VStack({ width: 20, height: 5 }, [ 1979 HStack( 1980 { 1981 onClick: () => {}, 1982 focused: focused === "a", 1983 onFocus: () => { 1984 focused = "a"; 1985 }, 1986 onBlur: () => { 1987 if (focused === "a") focused = null; 1988 }, 1989 }, 1990 [Text("A")], 1991 ), 1992 HStack( 1993 { 1994 onClick: () => {}, 1995 focused: focused === "b", 1996 onFocus: () => { 1997 focused = "b"; 1998 }, 1999 onBlur: () => { 2000 if (focused === "b") focused = null; 2001 }, 2002 }, 2003 [Text("B")], 2004 ), 2005 HStack( 2006 { 2007 onClick: () => {}, 2008 focused: focused === "c", 2009 onFocus: () => { 2010 focused = "c"; 2011 }, 2012 onBlur: () => { 2013 if (focused === "c") focused = null; 2014 }, 2015 }, 2016 [Text("C")], 2017 ), 2018 ]), 2019 ); 2020 await waitForRender(); 2021 2022 // Tab to B 2023 term.sendInput(TAB); // → A 2024 await waitForRender(); 2025 term.sendInput(TAB); // → B 2026 await waitForRender(); 2027 expect(focused as string | null).toBe("b"); 2028 2029 // Escape, then Shift+Tab should go backward to A 2030 term.sendInput(ESCAPE); 2031 await waitForRender(); 2032 expect(focused as string | null).toBe(null); 2033 2034 term.sendInput(SHIFT_TAB); // Shift+Tab 2035 await waitForRender(); 2036 expect(focused as string | null).toBe("a"); 2037 }); 2038 }); 2039 2040 describe("TextInput mouse wheel scroll", () => { 2041 test("mouse wheel scrolls TextInput content", async () => { 2042 const term = setup(20, 12); 2043 let value = Array.from({ length: 20 }, (_, i) => `line${i + 1}`).join( 2044 "\n", 2045 ); 2046 // Stable onChange ref so TextInput scroll state persists across re-renders 2047 const onChange = (v: string) => { 2048 value = v; 2049 }; 2050 2051 cel.viewport(() => 2052 VStack({ width: 20, height: 12 }, [ 2053 TextInput({ 2054 value, 2055 height: 12, 2056 onChange, 2057 }), 2058 ]), 2059 ); 2060 await waitForRender(); 2061 2062 // Initially line1 should be visible 2063 const buf1 = currentBuffer(); 2064 let row0 = ""; 2065 for (let x = 0; x < 5; x++) row0 += buf1.get(x, 0).char; 2066 expect(row0).toBe("line1"); 2067 2068 // 12-row viewport → adaptive wheel step = 4 2069 term.sendInput("\x1b[<65;6;2M"); 2070 await waitForRender(); 2071 2072 const buf2 = currentBuffer(); 2073 let row0After = ""; 2074 for (let x = 0; x < 5; x++) row0After += buf2.get(x, 0).char; 2075 expect(row0After).toBe("line5"); 2076 }); 2077 2078 test("mouse wheel scroll up clamps at 0", async () => { 2079 const term = setup(20, 3); 2080 let value = "line1\nline2\nline3\nline4"; 2081 const onChange = (v: string) => { 2082 value = v; 2083 }; 2084 2085 cel.viewport(() => 2086 VStack({ width: 20, height: 3 }, [ 2087 TextInput({ 2088 value, 2089 height: 3, 2090 onChange, 2091 }), 2092 ]), 2093 ); 2094 await waitForRender(); 2095 2096 // Scroll up (already at top, should clamp to 0) 2097 term.sendInput("\x1b[<64;6;2M"); 2098 await waitForRender(); 2099 2100 // line1 should still be at row 0 2101 const buf = currentBuffer(); 2102 let row0 = ""; 2103 for (let x = 0; x < 5; x++) row0 += buf.get(x, 0).char; 2104 expect(row0).toBe("line1"); 2105 }); 2106 2107 test("mouse wheel prefers innermost TextInput over outer scrollable", async () => { 2108 const term = setup(20, 12); 2109 let outerScrolled = false; 2110 let value = Array.from({ length: 20 }, (_, i) => `line${i + 1}`).join( 2111 "\n", 2112 ); 2113 const onChange = (v: string) => { 2114 value = v; 2115 }; 2116 2117 cel.viewport(() => 2118 VStack( 2119 { 2120 width: 20, 2121 height: 12, 2122 overflow: "scroll", 2123 scrollOffset: 0, 2124 onScroll: () => { 2125 outerScrolled = true; 2126 }, 2127 }, 2128 [ 2129 TextInput({ 2130 value, 2131 height: 12, 2132 onChange, 2133 }), 2134 ], 2135 ), 2136 ); 2137 await waitForRender(); 2138 2139 // Scroll down — should target TextInput, not outer VStack 2140 term.sendInput("\x1b[<65;6;2M"); 2141 await waitForRender(); 2142 2143 expect(outerScrolled).toBe(false); 2144 2145 // Verify TextInput scrolled by the adaptive step (4) 2146 const buf = currentBuffer(); 2147 let row0 = ""; 2148 for (let x = 0; x < 5; x++) row0 += buf.get(x, 0).char; 2149 expect(row0).toBe("line5"); 2150 }); 2151 }); 2152 2153 describe("uncontrolled focus", () => { 2154 test("autoFocus seeds focus once and reports the auto reason", async () => { 2155 const term = setup(20, 5); 2156 const reasons: string[] = []; 2157 let clicks = 0; 2158 let mounted = true; 2159 2160 cel.viewport(() => 2161 VStack( 2162 { width: 20, height: 5 }, 2163 mounted 2164 ? [ 2165 HStack( 2166 { 2167 stateKey: "primary", 2168 autoFocus: true, 2169 onClick: () => clicks++, 2170 onFocus: ({ reason }) => reasons.push(reason), 2171 }, 2172 [Text("Primary")], 2173 ), 2174 ] 2175 : [Text("unmounted")], 2176 ), 2177 ); 2178 await waitForRender(); 2179 2180 term.sendInput(ENTER); 2181 await waitForRender(); 2182 expect(clicks).toBe(1); 2183 expect(reasons).toEqual(["auto"]); 2184 2185 term.sendInput(ESCAPE); 2186 await waitForRender(); 2187 cel.render(); 2188 await waitForRender(); 2189 term.sendInput(ENTER); 2190 await waitForRender(); 2191 2192 expect(clicks).toBe(1); 2193 expect(reasons).toEqual(["auto"]); 2194 2195 mounted = false; 2196 cel.render(); 2197 await waitForRender(); 2198 mounted = true; 2199 cel.render(); 2200 await waitForRender(); 2201 term.sendInput(ENTER); 2202 await waitForRender(); 2203 2204 expect(clicks).toBe(2); 2205 expect(reasons).toEqual(["auto", "auto"]); 2206 }); 2207 2208 test("Tab focuses first clickable element and Enter activates it", async () => { 2209 const term = setup(20, 5); 2210 let clicked = false; 2211 2212 cel.viewport(() => 2213 VStack({ width: 20, height: 5 }, [ 2214 HStack( 2215 { 2216 onClick: () => { 2217 clicked = true; 2218 }, 2219 }, 2220 [Text("Btn")], 2221 ), 2222 ]), 2223 ); 2224 await waitForRender(); 2225 2226 // Tab to focus the button 2227 term.sendInput(TAB); 2228 await waitForRender(); 2229 2230 // Enter to activate 2231 term.sendInput(ENTER); 2232 await waitForRender(); 2233 2234 expect(clicked).toBe(true); 2235 }); 2236 2237 test("Tab cycles through focusable elements in order", async () => { 2238 const term = setup(20, 5); 2239 const clicks: string[] = []; 2240 2241 cel.viewport(() => 2242 VStack({ width: 20, height: 5 }, [ 2243 HStack({ onClick: () => clicks.push("btn1") }, [Text("Btn1")]), 2244 HStack({ onClick: () => clicks.push("btn2") }, [Text("Btn2")]), 2245 ]), 2246 ); 2247 await waitForRender(); 2248 2249 // Tab to first, activate 2250 term.sendInput(TAB); 2251 await waitForRender(); 2252 term.sendInput(ENTER); 2253 await waitForRender(); 2254 expect(clicks).toEqual(["btn1"]); 2255 2256 // Tab to second, activate 2257 term.sendInput(TAB); 2258 await waitForRender(); 2259 term.sendInput(ENTER); 2260 await waitForRender(); 2261 expect(clicks).toEqual(["btn1", "btn2"]); 2262 }); 2263 2264 test("stateKey preserves uncontrolled focus when siblings reorder", async () => { 2265 const term = setup(20, 5); 2266 const clicks: string[] = []; 2267 let reversed = false; 2268 2269 cel.viewport(() => { 2270 const first = HStack( 2271 { stateKey: "first", onClick: () => clicks.push("first") }, 2272 [Text("First")], 2273 ); 2274 const second = HStack( 2275 { stateKey: "second", onClick: () => clicks.push("second") }, 2276 [Text("Second")], 2277 ); 2278 return VStack( 2279 { width: 20, height: 5 }, 2280 reversed ? [second, first] : [first, second], 2281 ); 2282 }); 2283 await waitForRender(); 2284 2285 term.sendInput(TAB); 2286 await waitForRender(); 2287 reversed = true; 2288 cel.render(); 2289 await waitForRender(); 2290 term.sendInput(ENTER); 2291 await waitForRender(); 2292 2293 expect(clicks).toEqual(["first"]); 2294 }); 2295 2296 test("keyed last focus survives reorder after Escape", async () => { 2297 const term = setup(20, 5); 2298 const clicks: string[] = []; 2299 let reversed = false; 2300 2301 cel.viewport(() => { 2302 const first = HStack( 2303 { stateKey: "first", onClick: () => clicks.push("first") }, 2304 [Text("First")], 2305 ); 2306 const second = HStack( 2307 { stateKey: "second", onClick: () => clicks.push("second") }, 2308 [Text("Second")], 2309 ); 2310 return VStack( 2311 { width: 20, height: 5 }, 2312 reversed ? [second, first] : [first, second], 2313 ); 2314 }); 2315 await waitForRender(); 2316 2317 term.sendInput(TAB); 2318 await waitForRender(); 2319 term.sendInput(ESCAPE); 2320 await waitForRender(); 2321 reversed = true; 2322 cel.render(); 2323 await waitForRender(); 2324 term.sendInput(TAB); 2325 await waitForRender(); 2326 term.sendInput(ENTER); 2327 await waitForRender(); 2328 2329 expect(clicks).toEqual(["second"]); 2330 }); 2331 2332 test("removing a keyed focus does not focus its index replacement", async () => { 2333 const term = setup(20, 5); 2334 const clicks: string[] = []; 2335 let showFirst = true; 2336 2337 cel.viewport(() => 2338 VStack({ width: 20, height: 5 }, [ 2339 ...(showFirst 2340 ? [ 2341 HStack( 2342 { 2343 stateKey: "first", 2344 onClick: () => clicks.push("first"), 2345 }, 2346 [Text("First")], 2347 ), 2348 ] 2349 : []), 2350 HStack({ stateKey: "second", onClick: () => clicks.push("second") }, [ 2351 Text("Second"), 2352 ]), 2353 ]), 2354 ); 2355 await waitForRender(); 2356 2357 term.sendInput(TAB); 2358 await waitForRender(); 2359 showFirst = false; 2360 cel.render(); 2361 await waitForRender(); 2362 term.sendInput(ENTER); 2363 await waitForRender(); 2364 2365 expect(clicks).toEqual([]); 2366 }); 2367 2368 test("Shift+Tab moves focus backwards", async () => { 2369 const term = setup(20, 5); 2370 const clicks: string[] = []; 2371 2372 cel.viewport(() => 2373 VStack({ width: 20, height: 5 }, [ 2374 HStack({ onClick: () => clicks.push("btn1") }, [Text("Btn1")]), 2375 HStack({ onClick: () => clicks.push("btn2") }, [Text("Btn2")]), 2376 ]), 2377 ); 2378 await waitForRender(); 2379 2380 // Shift+Tab to focus last element 2381 term.sendInput(SHIFT_TAB); 2382 await waitForRender(); 2383 term.sendInput(ENTER); 2384 await waitForRender(); 2385 expect(clicks).toEqual(["btn2"]); 2386 2387 // Shift+Tab to go backward to first 2388 term.sendInput(SHIFT_TAB); 2389 await waitForRender(); 2390 term.sendInput(ENTER); 2391 await waitForRender(); 2392 expect(clicks).toEqual(["btn2", "btn1"]); 2393 }); 2394 2395 test("Escape unfocuses and Enter does nothing", async () => { 2396 const term = setup(20, 5); 2397 let clicked = false; 2398 2399 cel.viewport(() => 2400 VStack({ width: 20, height: 5 }, [ 2401 HStack( 2402 { 2403 onClick: () => { 2404 clicked = true; 2405 }, 2406 }, 2407 [Text("Btn")], 2408 ), 2409 ]), 2410 ); 2411 await waitForRender(); 2412 2413 // Tab to focus, then Escape to unfocus 2414 term.sendInput(TAB); 2415 await waitForRender(); 2416 term.sendInput(ESCAPE); 2417 await waitForRender(); 2418 2419 // Enter should do nothing — no element is focused 2420 term.sendInput(ENTER); 2421 await waitForRender(); 2422 expect(clicked).toBe(false); 2423 }); 2424 2425 test("mouse click focuses element and Enter activates it", async () => { 2426 const term = setup(20, 5); 2427 let clicked = false; 2428 2429 cel.viewport(() => 2430 VStack({ width: 20, height: 5 }, [ 2431 HStack({}, [Text("not clickable")]), 2432 HStack( 2433 { 2434 onClick: () => { 2435 clicked = true; 2436 }, 2437 }, 2438 [Text("Clickable")], 2439 ), 2440 ]), 2441 ); 2442 await waitForRender(); 2443 2444 // Click on the second element (row 1) 2445 term.sendInput("\x1b[<0;2;2m"); 2446 await waitForRender(); 2447 2448 // Enter should activate the clicked element 2449 term.sendInput(ENTER); 2450 await waitForRender(); 2451 expect(clicked).toBe(true); 2452 }); 2453 2454 test("onFocus fires as notification in uncontrolled mode", async () => { 2455 const term = setup(20, 5); 2456 const events: string[] = []; 2457 2458 cel.viewport(() => 2459 VStack({ width: 20, height: 5 }, [ 2460 HStack( 2461 { 2462 onClick: () => {}, 2463 onFocus: () => events.push("focus"), 2464 }, 2465 [Text("Btn")], 2466 ), 2467 ]), 2468 ); 2469 await waitForRender(); 2470 2471 term.sendInput(TAB); 2472 await waitForRender(); 2473 expect(events).toEqual(["focus"]); 2474 }); 2475 2476 test("onBlur fires as notification in uncontrolled mode", async () => { 2477 const term = setup(20, 5); 2478 const events: string[] = []; 2479 2480 cel.viewport(() => 2481 VStack({ width: 20, height: 5 }, [ 2482 HStack( 2483 { 2484 onClick: () => {}, 2485 onFocus: () => events.push("focus1"), 2486 onBlur: () => events.push("blur1"), 2487 }, 2488 [Text("Btn1")], 2489 ), 2490 HStack( 2491 { 2492 onClick: () => {}, 2493 onFocus: () => events.push("focus2"), 2494 }, 2495 [Text("Btn2")], 2496 ), 2497 ]), 2498 ); 2499 await waitForRender(); 2500 2501 // Tab to first 2502 term.sendInput(TAB); 2503 await waitForRender(); 2504 expect(events).toEqual(["focus1"]); 2505 2506 // Tab to second — first should get onBlur 2507 term.sendInput(TAB); 2508 await waitForRender(); 2509 expect(events).toEqual(["focus1", "blur1", "focus2"]); 2510 }); 2511 2512 test("Tab focuses uncontrolled TextInput and typing works", async () => { 2513 const term = setup(20, 5); 2514 let value = ""; 2515 2516 cel.viewport(() => 2517 VStack({ width: 20, height: 5 }, [ 2518 TextInput({ 2519 value, 2520 onChange: (v) => { 2521 value = v; 2522 }, 2523 }), 2524 ]), 2525 ); 2526 await waitForRender(); 2527 2528 // Tab to focus the TextInput 2529 term.sendInput(TAB); 2530 await waitForRender(); 2531 2532 // Type a character 2533 term.sendInput("x"); 2534 await waitForRender(); 2535 expect(value).toBe("x"); 2536 }); 2537 2538 test("Tab focusing an uncontrolled TextInput shows the native cursor", async () => { 2539 const term = setup(20, 5); 2540 let value = ""; 2541 2542 cel.viewport(() => 2543 VStack({ width: 20, height: 5 }, [ 2544 TextInput({ 2545 value, 2546 onChange: (v) => { 2547 value = v; 2548 }, 2549 }), 2550 ]), 2551 ); 2552 await waitForRender(); 2553 2554 term.clearOutput(); 2555 term.sendInput(TAB); 2556 await waitForRender(); 2557 2558 expect(term.output).toContain("\x1b[?25h"); 2559 }); 2560 2561 test("Tab wraps from last to first element", async () => { 2562 const term = setup(20, 5); 2563 const clicks: string[] = []; 2564 2565 cel.viewport(() => 2566 VStack({ width: 20, height: 5 }, [ 2567 HStack({ onClick: () => clicks.push("btn1") }, [Text("Btn1")]), 2568 HStack({ onClick: () => clicks.push("btn2") }, [Text("Btn2")]), 2569 ]), 2570 ); 2571 await waitForRender(); 2572 2573 // Tab to first, Tab to second, Tab wraps to first 2574 term.sendInput(TAB); 2575 await waitForRender(); 2576 term.sendInput(TAB); 2577 await waitForRender(); 2578 term.sendInput(TAB); 2579 await waitForRender(); 2580 term.sendInput(ENTER); 2581 await waitForRender(); 2582 expect(clicks).toEqual(["btn1"]); 2583 }); 2584 2585 test("mixed controlled and uncontrolled elements", async () => { 2586 const term = setup(20, 5); 2587 const clicks: string[] = []; 2588 let controlledFocused = false; 2589 2590 cel.viewport(() => 2591 VStack({ width: 20, height: 5 }, [ 2592 HStack( 2593 { 2594 onClick: () => clicks.push("controlled"), 2595 focused: controlledFocused, 2596 onFocus: () => { 2597 controlledFocused = true; 2598 }, 2599 onBlur: () => { 2600 controlledFocused = false; 2601 }, 2602 }, 2603 [Text("Controlled")], 2604 ), 2605 HStack({ onClick: () => clicks.push("uncontrolled") }, [ 2606 Text("Uncontrolled"), 2607 ]), 2608 ]), 2609 ); 2610 await waitForRender(); 2611 2612 // Tab to first (controlled) 2613 term.sendInput(TAB); 2614 await waitForRender(); 2615 expect(controlledFocused).toBe(true); 2616 2617 // Tab to second (uncontrolled), Enter to activate 2618 term.sendInput(TAB); 2619 await waitForRender(); 2620 expect(controlledFocused).toBe(false); 2621 term.sendInput(ENTER); 2622 await waitForRender(); 2623 expect(clicks).toEqual(["uncontrolled"]); 2624 }); 2625 2626 test("controlled TextInput as only focusable: Escape+Tab re-focuses it", async () => { 2627 const term = setup(40, 5); 2628 let value = ""; 2629 let inputFocused = false; 2630 2631 cel.viewport(() => 2632 VStack({ width: 40, height: 5 }, [ 2633 TextInput({ 2634 value, 2635 onChange: (v) => { 2636 value = v; 2637 cel.render(); 2638 }, 2639 focused: inputFocused, 2640 onFocus: () => { 2641 inputFocused = true; 2642 cel.render(); 2643 }, 2644 onBlur: () => { 2645 inputFocused = false; 2646 cel.render(); 2647 }, 2648 }), 2649 ]), 2650 ); 2651 await waitForRender(); 2652 2653 // Tab → TextInput focused 2654 term.sendInput(TAB); 2655 await waitForRender(); 2656 expect(inputFocused).toBe(true); 2657 2658 // Escape → unfocused 2659 term.sendInput(ESCAPE); 2660 await waitForRender(); 2661 expect(inputFocused).toBe(false); 2662 2663 // Tab → only one focusable, wraps to it again 2664 term.sendInput(TAB); 2665 await waitForRender(); 2666 expect(inputFocused).toBe(true); 2667 }); 2668 2669 test("controlled TextInput: Escape then Tab advances past it", async () => { 2670 const term = setup(40, 5); 2671 let value = ""; 2672 let inputFocused = false; 2673 const clicks: string[] = []; 2674 2675 cel.viewport(() => 2676 VStack({ width: 40, height: 5 }, [ 2677 TextInput({ 2678 value, 2679 onChange: (v) => { 2680 value = v; 2681 cel.render(); 2682 }, 2683 focused: inputFocused, 2684 onFocus: () => { 2685 inputFocused = true; 2686 cel.render(); 2687 }, 2688 onBlur: () => { 2689 inputFocused = false; 2690 cel.render(); 2691 }, 2692 }), 2693 HStack({ onClick: () => clicks.push("btn") }, [Text("Button")]), 2694 ]), 2695 ); 2696 await waitForRender(); 2697 2698 // Tab focuses the TextInput 2699 term.sendInput(TAB); 2700 await waitForRender(); 2701 expect(inputFocused).toBe(true); 2702 2703 // Tab inserts \t (editing key) while TextInput is focused 2704 term.sendInput(TAB); 2705 await waitForRender(); 2706 expect(value).toBe("\t"); 2707 2708 // Escape unfocuses 2709 term.sendInput(ESCAPE); 2710 await waitForRender(); 2711 expect(inputFocused).toBe(false); 2712 2713 // Tab should advance PAST the TextInput to the Button 2714 term.sendInput(TAB); 2715 await waitForRender(); 2716 2717 // Enter activates the button 2718 term.sendInput(ENTER); 2719 await waitForRender(); 2720 expect(clicks).toEqual(["btn"]); 2721 }); 2722 2723 test("controlled TextInput blur from re-render preserves Tab continuity", async () => { 2724 const term = setup(40, 5); 2725 let value = ""; 2726 let inputFocused = false; 2727 let buttonFocused = false; 2728 2729 cel.viewport(() => 2730 VStack({ width: 40, height: 5 }, [ 2731 TextInput({ 2732 value, 2733 onChange: (v) => { 2734 value = v; 2735 cel.render(); 2736 }, 2737 focused: inputFocused, 2738 onFocus: () => { 2739 inputFocused = true; 2740 buttonFocused = false; 2741 cel.render(); 2742 }, 2743 onBlur: () => { 2744 inputFocused = false; 2745 cel.render(); 2746 }, 2747 onKeyPress: (key) => { 2748 if (key === "enter") { 2749 inputFocused = false; 2750 cel.render(); 2751 return false; 2752 } 2753 }, 2754 }), 2755 HStack( 2756 { 2757 onClick: () => {}, 2758 focused: buttonFocused, 2759 onFocus: () => { 2760 buttonFocused = true; 2761 inputFocused = false; 2762 cel.render(); 2763 }, 2764 onBlur: () => { 2765 buttonFocused = false; 2766 cel.render(); 2767 }, 2768 }, 2769 [Text("Button")], 2770 ), 2771 ]), 2772 ); 2773 await waitForRender(); 2774 2775 term.sendInput(TAB); 2776 await waitForRender(); 2777 expect(inputFocused).toBe(true); 2778 2779 term.sendInput(ENTER); 2780 await waitForRender(); 2781 expect(inputFocused).toBe(false); 2782 2783 term.sendInput(TAB); 2784 await waitForRender(); 2785 expect(buttonFocused).toBe(true); 2786 }); 2787 }); 2788 2789 describe("focusStyle rendering", () => { 2790 test("focusStyle bgColor applied when element is focused via Tab", async () => { 2791 const term = setup(20, 3); 2792 2793 cel.viewport(() => 2794 VStack({ width: 20, height: 3 }, [ 2795 HStack( 2796 { 2797 onClick: () => {}, 2798 bgColor: "color00", 2799 focusStyle: { bgColor: "color06" }, 2800 }, 2801 [Text("Btn")], 2802 ), 2803 ]), 2804 ); 2805 await waitForRender(); 2806 2807 // Before focus — normal bgColor 2808 let buf = currentBuffer(); 2809 expect(buf.get(0, 0).bgColor).toBe("color00"); 2810 2811 // Tab to focus 2812 term.sendInput(TAB); 2813 await waitForRender(); 2814 2815 // After focus — focusStyle bgColor 2816 buf = currentBuffer(); 2817 expect(buf.get(0, 0).bgColor).toBe("color06"); 2818 }); 2819 2820 test("focusStyle removed when focus moves to another element", async () => { 2821 const term = setup(20, 3); 2822 2823 cel.viewport(() => 2824 VStack({ width: 20, height: 3 }, [ 2825 HStack( 2826 { 2827 onClick: () => {}, 2828 bgColor: "color00", 2829 focusStyle: { bgColor: "color06" }, 2830 }, 2831 [Text("Btn1")], 2832 ), 2833 HStack( 2834 { 2835 onClick: () => {}, 2836 bgColor: "color00", 2837 focusStyle: { bgColor: "color03" }, 2838 }, 2839 [Text("Btn2")], 2840 ), 2841 ]), 2842 ); 2843 await waitForRender(); 2844 2845 // Tab to first 2846 term.sendInput(TAB); 2847 await waitForRender(); 2848 let buf = currentBuffer(); 2849 expect(buf.get(0, 0).bgColor).toBe("color06"); 2850 expect(buf.get(0, 1).bgColor).toBe("color00"); 2851 2852 // Tab to second — first loses focusStyle, second gains it 2853 term.sendInput(TAB); 2854 await waitForRender(); 2855 buf = currentBuffer(); 2856 expect(buf.get(0, 0).bgColor).toBe("color00"); 2857 expect(buf.get(0, 1).bgColor).toBe("color03"); 2858 }); 2859 2860 test("focusStyle fgColor inherited by child Text when focused", async () => { 2861 const term = setup(20, 3); 2862 2863 cel.viewport(() => 2864 VStack({ width: 20, height: 3 }, [ 2865 HStack( 2866 { 2867 onClick: () => {}, 2868 fgColor: "color07", 2869 focusStyle: { fgColor: "color00", bgColor: "color06" }, 2870 }, 2871 [Text("Btn")], 2872 ), 2873 ]), 2874 ); 2875 await waitForRender(); 2876 2877 // Before focus — Text inherits white 2878 let buf = currentBuffer(); 2879 expect(buf.get(0, 0).fgColor).toBe("color07"); 2880 2881 // Tab to focus 2882 term.sendInput(TAB); 2883 await waitForRender(); 2884 2885 // After focus — Text inherits black from focusStyle 2886 buf = currentBuffer(); 2887 expect(buf.get(0, 0).fgColor).toBe("color00"); 2888 }); 2889 2890 test("focusStyle with controlled focus", async () => { 2891 const term = setup(20, 3); 2892 let isFocused = true; 2893 2894 cel.viewport(() => 2895 VStack({ width: 20, height: 3 }, [ 2896 HStack( 2897 { 2898 onClick: () => {}, 2899 focused: isFocused, 2900 bgColor: "color00", 2901 focusStyle: { bgColor: "color06" }, 2902 onBlur: () => { 2903 isFocused = false; 2904 }, 2905 }, 2906 [Text("Btn")], 2907 ), 2908 ]), 2909 ); 2910 await waitForRender(); 2911 2912 // Controlled focused=true — focusStyle applies 2913 let buf = currentBuffer(); 2914 expect(buf.get(0, 0).bgColor).toBe("color06"); 2915 2916 // Escape to blur 2917 term.sendInput(ESCAPE); 2918 await waitForRender(); 2919 2920 // focused=false — normal style 2921 buf = currentBuffer(); 2922 expect(buf.get(0, 0).bgColor).toBe("color00"); 2923 }); 2924 }); 2925});