70160
Ce
cel-tui / files
cel-tui / packages / core / src / paint.test.ts
37.7 KB
1import { describe, expect, test } from "bun:test"; 2import { CellBuffer } from "./cell-buffer.js"; 3import { layout } from "./layout.js"; 4import { 5 getTextInputCursor, 6 getTextInputCursorScreenPos, 7 getTextInputScroll, 8 paint, 9 setTextInputCursor, 10 setTextInputScroll, 11} from "./paint.js"; 12import { HStack, VStack } from "./primitives/stacks.js"; 13import { Text } from "./primitives/text.js"; 14import { TextInput } from "./primitives/text-input.js"; 15 16/** Read a row from the buffer as a string (trimming trailing spaces). */ 17function readRow(buf: CellBuffer, y: number): string { 18 let row = ""; 19 for (let x = 0; x < buf.width; x++) { 20 row += buf.get(x, y).char; 21 } 22 return row.trimEnd(); 23} 24 25/** Read a row from the buffer without trimming trailing spaces. */ 26function readRawRow(buf: CellBuffer, y: number): string { 27 let row = ""; 28 for (let x = 0; x < buf.width; x++) { 29 row += buf.get(x, y).char; 30 } 31 return row; 32} 33 34describe("paint", () => { 35 describe("Text rendering", () => { 36 test("paints text at correct position", () => { 37 const node = VStack({ width: 20, height: 3 }, [Text("Hello")]); 38 const ln = layout(node, 20, 3); 39 const buf = new CellBuffer(20, 3); 40 paint(ln, buf); 41 expect(readRow(buf, 0)).toBe("Hello"); 42 }); 43 44 test("clips text at rect boundary", () => { 45 const node = VStack({ width: 5, height: 1 }, [Text("Hello, world!")]); 46 const ln = layout(node, 5, 1); 47 const buf = new CellBuffer(5, 1); 48 paint(ln, buf); 49 expect(readRow(buf, 0)).toBe("Hello"); 50 }); 51 52 test("paints multiline text from newlines", () => { 53 const node = VStack({ width: 20, height: 5 }, [ 54 Text("line1\nline2\nline3"), 55 ]); 56 const ln = layout(node, 20, 5); 57 const buf = new CellBuffer(20, 5); 58 paint(ln, buf); 59 expect(readRow(buf, 0)).toBe("line1"); 60 expect(readRow(buf, 1)).toBe("line2"); 61 expect(readRow(buf, 2)).toBe("line3"); 62 }); 63 64 test("does not paint embedded ANSI control sequences as text", () => { 65 const node = VStack({ width: 10, height: 1 }, [ 66 Text("\x1b[31mred\x1b[0m"), 67 ]); 68 const ln = layout(node, 10, 1); 69 const buf = new CellBuffer(10, 1); 70 71 paint(ln, buf); 72 73 expect(readRow(buf, 0)).toBe("red"); 74 }); 75 76 test("repeat fill fills the width", () => { 77 const node = VStack({ width: 10, height: 1 }, [ 78 Text("-", { repeat: "fill" }), 79 ]); 80 const ln = layout(node, 10, 1); 81 const buf = new CellBuffer(10, 1); 82 paint(ln, buf); 83 expect(readRow(buf, 0)).toBe("----------"); 84 }); 85 86 test("repeat number repeats exact count", () => { 87 const node = VStack({ width: 20, height: 1 }, [ 88 Text("ab", { repeat: 3 }), 89 ]); 90 const ln = layout(node, 20, 1); 91 const buf = new CellBuffer(20, 1); 92 paint(ln, buf); 93 expect(readRow(buf, 0)).toBe("ababab"); 94 }); 95 96 test("applies foreground color", () => { 97 const node = VStack({ width: 10, height: 1 }, [ 98 Text("Hi", { fgColor: "color01" }), 99 ]); 100 const ln = layout(node, 10, 1); 101 const buf = new CellBuffer(10, 1); 102 paint(ln, buf); 103 expect(buf.get(0, 0).fgColor).toBe("color01"); 104 expect(buf.get(1, 0).fgColor).toBe("color01"); 105 }); 106 107 test("applies background color", () => { 108 const node = VStack({ width: 10, height: 1 }, [ 109 Text("Hi", { bgColor: "color04" }), 110 ]); 111 const ln = layout(node, 10, 1); 112 const buf = new CellBuffer(10, 1); 113 paint(ln, buf); 114 expect(buf.get(0, 0).bgColor).toBe("color04"); 115 }); 116 117 test("applies bold/italic/underline", () => { 118 const node = VStack({ width: 10, height: 1 }, [ 119 Text("X", { bold: true, italic: true, underline: true }), 120 ]); 121 const ln = layout(node, 10, 1); 122 const buf = new CellBuffer(10, 1); 123 paint(ln, buf); 124 const cell = buf.get(0, 0); 125 expect(cell.bold).toBe(true); 126 expect(cell.italic).toBe(true); 127 expect(cell.underline).toBe(true); 128 }); 129 130 test("word-wrapped text preserves whitespace at soft wrap boundaries", () => { 131 const node = VStack({ width: 6, height: 3 }, [ 132 Text("foo bar baz", { wrap: "word" }), 133 ]); 134 const ln = layout(node, 6, 3); 135 const buf = new CellBuffer(6, 3); 136 paint(ln, buf); 137 138 expect(readRawRow(buf, 0)).toBe("foo "); 139 expect(readRawRow(buf, 1)).toBe("bar "); 140 expect(readRawRow(buf, 2)).toBe("baz "); 141 }); 142 143 test("tabs paint as spaces instead of disappearing", () => { 144 const node = VStack({ width: 6, height: 2 }, [ 145 Text("\tfoo", { wrap: "word" }), 146 ]); 147 const ln = layout(node, 6, 2); 148 const buf = new CellBuffer(6, 2); 149 paint(ln, buf); 150 151 expect(readRawRow(buf, 0)).toBe(" "); 152 expect(readRawRow(buf, 1)).toBe("foo "); 153 }); 154 155 test("TextInput cursor screen position follows visual word wrapping", () => { 156 const onChange = () => {}; 157 const props = { 158 value: "foo bar baz", 159 onChange, 160 focused: true, 161 }; 162 163 setTextInputCursor(props, 5); 164 165 expect( 166 getTextInputCursorScreenPos(props, { x: 0, y: 0, width: 6, height: 3 }), 167 ).toEqual({ x: 1, y: 1 }); 168 }); 169 170 test("focused TextInput clamps stale scroll before projecting the cursor", () => { 171 const onChange = () => {}; 172 const props = { 173 value: "foo bar baz", 174 onChange, 175 focused: true, 176 }; 177 178 setTextInputCursor(props, 5); 179 setTextInputScroll(props, 99); 180 181 expect( 182 getTextInputCursorScreenPos(props, { x: 0, y: 0, width: 6, height: 3 }), 183 ).toEqual({ x: 1, y: 1 }); 184 expect(getTextInputScroll(props)).toBe(0); 185 }); 186 187 test("focused TextInput preserves a wide glyph under the painted cursor", () => { 188 const props = { 189 value: "界A", 190 onChange: () => {}, 191 focused: true, 192 }; 193 setTextInputCursor(props, 0); 194 const node = VStack({ width: 4, height: 1 }, [TextInput(props)]); 195 const buf = new CellBuffer(4, 1); 196 197 paint(layout(node, 4, 1), buf); 198 199 expect(buf.get(0, 0).char).toBe("界"); 200 expect(buf.get(1, 0).char).toBe(""); 201 expect(buf.get(2, 0).char).toBe("A"); 202 expect(buf.get(1, 0).fgColor).toBe(buf.get(0, 0).fgColor); 203 expect(buf.get(1, 0).bgColor).toBe(buf.get(0, 0).bgColor); 204 }); 205 206 test("TextInput uses an inverted block cursor by default", () => { 207 const node = VStack({ width: 4, height: 1 }, [ 208 TextInput({ 209 value: "ab", 210 cursor: 1, 211 onChange: () => {}, 212 focused: true, 213 fgColor: "color02", 214 bgColor: "color04", 215 }), 216 ]); 217 const buf = new CellBuffer(4, 1); 218 219 paint(layout(node, 4, 1), buf); 220 221 expect(buf.get(1, 0)).toMatchObject({ 222 char: "b", 223 fgColor: "color04", 224 bgColor: "color02", 225 }); 226 }); 227 228 test("TextInput paints a bar fallback cursor", () => { 229 const node = VStack({ width: 4, height: 1 }, [ 230 TextInput({ 231 value: "ab", 232 cursor: 1, 233 cursorStyle: "bar", 234 onChange: () => {}, 235 focused: true, 236 }), 237 ]); 238 const buf = new CellBuffer(4, 1); 239 240 paint(layout(node, 4, 1), buf); 241 242 expect(buf.get(1, 0).char).toBe("│"); 243 }); 244 245 test("TextInput paints an underline fallback cursor", () => { 246 const node = VStack({ width: 4, height: 1 }, [ 247 TextInput({ 248 value: "ab", 249 cursor: 1, 250 cursorStyle: "underline", 251 onChange: () => {}, 252 focused: true, 253 }), 254 ]); 255 const buf = new CellBuffer(4, 1); 256 257 paint(layout(node, 4, 1), buf); 258 259 expect(buf.get(1, 0).char).toBe("b"); 260 expect(buf.get(1, 0).underline).toBe(true); 261 }); 262 263 test("stateKey isolates TextInput state when inputs share onChange", () => { 264 const onChange = () => {}; 265 const first = { stateKey: "first", value: "abc", onChange }; 266 const second = { stateKey: "second", value: "xyz", onChange }; 267 268 setTextInputCursor(first, 0); 269 setTextInputCursor(second, 2); 270 271 expect(getTextInputCursor(first)).toBe(0); 272 expect(getTextInputCursor(second)).toBe(2); 273 }); 274 275 test("unfocused TextInput clamps stale scroll during paint", () => { 276 const onChange = () => {}; 277 const props = { 278 value: "line1\nline2", 279 onChange, 280 height: 2, 281 }; 282 setTextInputScroll(props, 99); 283 284 const node = VStack({ width: 6, height: 2 }, [TextInput(props)]); 285 const ln = layout(node, 6, 2); 286 const buf = new CellBuffer(6, 2); 287 paint(ln, buf); 288 289 expect(getTextInputScroll(props)).toBe(0); 290 }); 291 }); 292 293 describe("layout-driven positioning", () => { 294 test("VStack children paint at correct vertical positions", () => { 295 const node = VStack({ width: 20, height: 10 }, [ 296 Text("Header"), 297 Text("Body"), 298 ]); 299 const ln = layout(node, 20, 10); 300 const buf = new CellBuffer(20, 10); 301 paint(ln, buf); 302 expect(readRow(buf, 0)).toBe("Header"); 303 expect(readRow(buf, 1)).toBe("Body"); 304 }); 305 306 test("HStack children paint at correct horizontal positions", () => { 307 const node = HStack({ width: 20, height: 1 }, [ 308 VStack({ width: 8 }, [Text("Left")]), 309 VStack({ width: 12 }, [Text("Right")]), 310 ]); 311 const ln = layout(node, 20, 1); 312 const buf = new CellBuffer(20, 1); 313 paint(ln, buf); 314 expect(readRow(buf, 0)).toBe("Left Right"); 315 }); 316 317 test("flex children fill space correctly", () => { 318 const node = HStack({ width: 20, height: 1 }, [ 319 VStack({ width: 5 }, [Text("A")]), 320 VStack({ flex: 1 }, [Text("B")]), 321 ]); 322 const ln = layout(node, 20, 1); 323 const buf = new CellBuffer(20, 1); 324 paint(ln, buf); 325 expect(buf.get(0, 0).char).toBe("A"); 326 expect(buf.get(5, 0).char).toBe("B"); 327 }); 328 329 test("repeat fill renders inside HStack", () => { 330 const node = HStack({ width: 10, height: 1 }, [ 331 Text("A"), 332 Text("-", { repeat: "fill" }), 333 Text("B"), 334 ]); 335 const ln = layout(node, 10, 1); 336 const buf = new CellBuffer(10, 1); 337 paint(ln, buf); 338 expect(readRawRow(buf, 0)).toBe("A--------B"); 339 }); 340 341 test("padding offsets children", () => { 342 const node = VStack({ width: 20, height: 5, padding: { x: 2, y: 1 } }, [ 343 Text("Hi"), 344 ]); 345 const ln = layout(node, 20, 5); 346 const buf = new CellBuffer(20, 5); 347 paint(ln, buf); 348 // Row 0 should be empty (padding), text starts at row 1, col 2 349 expect(readRow(buf, 0)).toBe(""); 350 expect(buf.get(2, 1).char).toBe("H"); 351 expect(buf.get(3, 1).char).toBe("i"); 352 }); 353 }); 354 355 describe("wide character rendering", () => { 356 test("CJK characters occupy 2 cells each", () => { 357 const node = VStack({ width: 20, height: 1 }, [Text("\u4e16\u754c")]); 358 const ln = layout(node, 20, 1); 359 const buf = new CellBuffer(20, 1); 360 paint(ln, buf); 361 // \u4e16 at col 0 (wide), continuation at col 1, \u754c at col 2 (wide), continuation at col 3 362 expect(buf.get(0, 0).char).toBe("\u4e16"); 363 expect(buf.get(1, 0).char).toBe(""); // continuation marker 364 expect(buf.get(2, 0).char).toBe("\u754c"); 365 expect(buf.get(3, 0).char).toBe(""); // continuation marker 366 }); 367 368 test("emoji occupies 2 cells", () => { 369 const node = VStack({ width: 20, height: 1 }, [Text("\ud83d\ude00x")]); 370 const ln = layout(node, 20, 1); 371 const buf = new CellBuffer(20, 1); 372 paint(ln, buf); 373 // emoji at col 0 (wide), continuation at col 1, x at col 2 374 expect(buf.get(0, 0).char).toBe("\ud83d\ude00"); 375 expect(buf.get(1, 0).char).toBe(""); // continuation marker 376 expect(buf.get(2, 0).char).toBe("x"); 377 }); 378 379 test("repeat fill with CJK char fills by visible width", () => { 380 const node = VStack({ width: 6, height: 1 }, [ 381 Text("\u4e16", { repeat: "fill" }), // width 2, fills 6 cols = 3 repeats 382 ]); 383 const ln = layout(node, 6, 1); 384 const buf = new CellBuffer(6, 1); 385 paint(ln, buf); 386 expect(buf.get(0, 0).char).toBe("\u4e16"); 387 expect(buf.get(2, 0).char).toBe("\u4e16"); 388 expect(buf.get(4, 0).char).toBe("\u4e16"); 389 }); 390 391 test("mixed ASCII and CJK paints at correct positions", () => { 392 const node = VStack({ width: 20, height: 1 }, [Text("hi\u4e16\u754c")]); 393 const ln = layout(node, 20, 1); 394 const buf = new CellBuffer(20, 1); 395 paint(ln, buf); 396 expect(buf.get(0, 0).char).toBe("h"); 397 expect(buf.get(1, 0).char).toBe("i"); 398 expect(buf.get(2, 0).char).toBe("\u4e16"); 399 expect(buf.get(4, 0).char).toBe("\u754c"); 400 }); 401 402 test("CJK text clips at rect boundary", () => { 403 // \u4e16(2) + \u754c(2) = 4, but rect is 3 wide \u2192 clip \u754c 404 const node = VStack({ width: 3, height: 1 }, [Text("\u4e16\u754c")]); 405 const ln = layout(node, 3, 1); 406 const buf = new CellBuffer(3, 1); 407 paint(ln, buf); 408 expect(buf.get(0, 0).char).toBe("\u4e16"); 409 // col 2 should be empty - \u754c doesn't fit (needs 2 cols, only 1 remains) 410 expect(buf.get(2, 0).char).toBe(" "); 411 }); 412 413 test("does not leave a wide continuation when its lead is clipped", () => { 414 const node = HStack( 415 { 416 width: 2, 417 height: 1, 418 overflow: "scroll", 419 scrollOffset: 1, 420 }, 421 [Text("😀A")], 422 ); 423 const ln = layout(node, 2, 1); 424 const buf = new CellBuffer(2, 1); 425 426 paint(ln, buf); 427 428 expect(buf.get(0, 0).char).toBe(" "); 429 expect(buf.get(1, 0).char).toBe("A"); 430 }); 431 432 test("does not paint a wide lead when its continuation is clipped", () => { 433 const node = VStack({ width: 1, height: 1 }, [ 434 VStack({ width: 2, height: 1 }, [Text("😀")]), 435 ]); 436 const ln = layout(node, 1, 1); 437 const buf = new CellBuffer(1, 1); 438 439 paint(ln, buf); 440 441 expect(buf.get(0, 0).char).toBe(" "); 442 }); 443 }); 444 445 describe("overflow clipping", () => { 446 test("text extending beyond container width is clipped", () => { 447 // Container is 5 wide, child text is longer 448 const node = VStack({ width: 5, height: 3 }, [ 449 VStack({ width: 5, height: 1 }, [Text("HelloWorld")]), 450 ]); 451 const ln = layout(node, 10, 3); 452 const buf = new CellBuffer(10, 3); 453 paint(ln, buf); 454 // Only "Hello" should appear, "World" clipped by container 455 expect(readRow(buf, 0)).toBe("Hello"); 456 }); 457 458 test("child positioned outside parent bounds is clipped", () => { 459 // Parent is 10 wide, 3 tall. Two children each 2 tall = 4 total. 460 // Second child overflows vertically. 461 const node = VStack({ width: 10, height: 3 }, [ 462 VStack({ height: 2 }, [Text("AA")]), 463 VStack({ height: 2 }, [Text("BB")]), 464 ]); 465 const ln = layout(node, 10, 3); 466 const buf = new CellBuffer(10, 5); 467 paint(ln, buf); 468 expect(readRow(buf, 0)).toBe("AA"); 469 expect(readRow(buf, 2)).toBe("BB"); 470 // Row 3 is outside the parent (height=3), second line of "BB" child 471 // would be at y=3 but should be clipped 472 expect(readRow(buf, 3)).toBe(""); 473 }); 474 475 test("nested containers clip to innermost parent", () => { 476 // Outer 10x3, inner 5x2 at position (0,0) 477 // Text inside inner is long 478 const node = VStack({ width: 10, height: 3 }, [ 479 VStack({ width: 5, height: 2 }, [Text("LongTextHere")]), 480 ]); 481 const ln = layout(node, 10, 3); 482 const buf = new CellBuffer(10, 3); 483 paint(ln, buf); 484 // Clipped to inner container width of 5 485 expect(readRow(buf, 0)).toBe("LongT"); 486 }); 487 488 test("HStack children clipped horizontally by parent", () => { 489 const node = HStack({ width: 8, height: 1 }, [ 490 VStack({ width: 5 }, [Text("AAAAA")]), 491 VStack({ width: 5 }, [Text("BBBBB")]), 492 ]); 493 const ln = layout(node, 10, 1); 494 const buf = new CellBuffer(10, 1); 495 paint(ln, buf); 496 // First child fills 0-4 ("AAAAA"), second starts at 5 497 // But parent is only 8 wide, so second child clipped at col 8 498 expect(readRow(buf, 0)).toBe("AAAAABBB"); 499 }); 500 501 test("deeply nested clipping", () => { 502 const node = VStack({ width: 6, height: 2 }, [ 503 HStack({}, [VStack({ width: 10 }, [Text("TooWide!!!")])]), 504 ]); 505 const ln = layout(node, 10, 2); 506 const buf = new CellBuffer(10, 2); 507 paint(ln, buf); 508 // Should be clipped to outermost container width of 6 509 expect(readRow(buf, 0)).toBe("TooWid"); 510 }); 511 }); 512 513 describe("scroll rendering", () => { 514 test("VStack with overflow scroll and scrollOffset offsets children vertically", () => { 515 const node = VStack( 516 { width: 10, height: 3, overflow: "scroll", scrollOffset: 1 }, 517 [ 518 Text("line0"), 519 Text("line1"), 520 Text("line2"), 521 Text("line3"), 522 Text("line4"), 523 ], 524 ); 525 const ln = layout(node, 10, 3); 526 const buf = new CellBuffer(10, 5); 527 paint(ln, buf); 528 // scrollOffset=1 means first visible line is line1 529 expect(readRow(buf, 0)).toBe("line1"); 530 expect(readRow(buf, 1)).toBe("line2"); 531 expect(readRow(buf, 2)).toBe("line3"); 532 // Row 3 should be empty (outside container) 533 expect(readRow(buf, 3)).toBe(""); 534 }); 535 536 test("scrollOffset 0 shows from the top", () => { 537 const node = VStack( 538 { width: 10, height: 2, overflow: "scroll", scrollOffset: 0 }, 539 [Text("first"), Text("second"), Text("third")], 540 ); 541 const ln = layout(node, 10, 2); 542 const buf = new CellBuffer(10, 3); 543 paint(ln, buf); 544 expect(readRow(buf, 0)).toBe("first"); 545 expect(readRow(buf, 1)).toBe("second"); 546 expect(readRow(buf, 2)).toBe(""); 547 }); 548 549 test("HStack with overflow scroll and scrollOffset offsets children horizontally", () => { 550 const node = HStack( 551 { width: 5, height: 1, overflow: "scroll", scrollOffset: 3 }, 552 [ 553 VStack({ width: 4 }, [Text("AAAA")]), 554 VStack({ width: 4 }, [Text("BBBB")]), 555 VStack({ width: 4 }, [Text("CCCC")]), 556 ], 557 ); 558 const ln = layout(node, 10, 1); 559 const buf = new CellBuffer(10, 1); 560 paint(ln, buf); 561 // Total content width = 12, scrollOffset=3, viewport=5 562 // Visible: cols 3-7 of content = "A" + "BBBB" → "ABBBB" but clipped to 5 563 expect(readRow(buf, 0)).toBe("ABBBB"); 564 }); 565 566 test("nested vertical and horizontal scroll offsets compose", () => { 567 const node = VStack( 568 { 569 width: 4, 570 height: 1, 571 overflow: "scroll", 572 scrollOffset: 1, 573 }, 574 [ 575 Text("top"), 576 HStack( 577 { 578 width: 4, 579 height: 1, 580 overflow: "scroll", 581 scrollOffset: 1, 582 }, 583 [ 584 VStack({ width: 2 }, [Text("AB")]), 585 VStack({ width: 4 }, [Text("CDEF")]), 586 ], 587 ), 588 ], 589 ); 590 const ln = layout(node, 4, 1); 591 const buf = new CellBuffer(4, 1); 592 593 paint(ln, buf); 594 595 expect(readRawRow(buf, 0)).toBe("BCDE"); 596 }); 597 598 test("scroll with scrollbar shows indicator", () => { 599 const node = VStack( 600 { 601 width: 10, 602 height: 4, 603 overflow: "scroll", 604 scrollOffset: 0, 605 scrollbar: true, 606 }, 607 [ 608 Text("line0"), 609 Text("line1"), 610 Text("line2"), 611 Text("line3"), 612 Text("line4"), 613 Text("line5"), 614 Text("line6"), 615 Text("line7"), 616 ], 617 ); 618 const ln = layout(node, 10, 4); 619 const buf = new CellBuffer(10, 4); 620 paint(ln, buf); 621 // Scrollbar should be in the last column (col 9) 622 // Content height = 8, viewport = 4, scrollOffset = 0 623 // Scrollbar thumb should be at top rows 624 const lastCol = Array.from({ length: 4 }, (_, y) => buf.get(9, y).char); 625 // At least one cell should have the scrollbar character 626 expect(lastCol.some((c) => c !== " ")).toBe(true); 627 }); 628 629 test("scrollbarStyle customizes vertical thumb and track cells", () => { 630 const node = VStack( 631 { 632 width: 5, 633 height: 3, 634 overflow: "scroll", 635 scrollbar: true, 636 scrollbarStyle: { 637 thumb: { char: "#", fgColor: "color06", bold: true }, 638 track: { char: ".", fgColor: "color03" }, 639 }, 640 }, 641 Array.from({ length: 6 }, (_, i) => Text(`L${i}`)), 642 ); 643 const buf = new CellBuffer(5, 3); 644 645 paint(layout(node, 5, 3), buf); 646 647 expect(buf.get(4, 0)).toMatchObject({ 648 char: "#", 649 fgColor: "color06", 650 bold: true, 651 }); 652 expect(buf.get(4, 2)).toMatchObject({ 653 char: ".", 654 fgColor: "color03", 655 }); 656 }); 657 658 test("scrollbarStyle rejects characters wider than one cell", () => { 659 const node = VStack( 660 { 661 width: 5, 662 height: 2, 663 overflow: "scroll", 664 scrollbar: true, 665 scrollbarStyle: { thumb: { char: "界" } }, 666 }, 667 [Text("a"), Text("b"), Text("c")], 668 ); 669 670 expect(() => paint(layout(node, 5, 2), new CellBuffer(5, 2))).toThrow( 671 "Scrollbar characters must occupy exactly one terminal column", 672 ); 673 }); 674 675 test("explicit overflow hidden clips children", () => { 676 const node = VStack({ width: 3, height: 1, overflow: "hidden" }, [ 677 Text("abcdef"), 678 ]); 679 const buf = new CellBuffer(6, 1); 680 681 paint(layout(node, 3, 1), buf); 682 683 expect(readRawRow(buf, 0)).toBe("abc "); 684 }); 685 686 test("scrollbar thumb position accounts for vertical padding", () => { 687 const node = VStack( 688 { 689 width: 6, 690 height: 7, 691 overflow: "scroll", 692 scrollOffset: 1, 693 scrollbar: true, 694 padding: { y: 1 }, 695 }, 696 Array.from({ length: 7 }, (_, i) => Text(`line${i}`)), 697 ); 698 const ln = layout(node, 6, 7); 699 const buf = new CellBuffer(6, 7); 700 paint(ln, buf); 701 702 const lastCol = Array.from({ length: 7 }, (_, y) => buf.get(5, y).char); 703 expect(lastCol.join("")).toBe("│┃┃┃┃┃│"); 704 }); 705 706 test("scroll clamps to max offset", () => { 707 const node = VStack( 708 { width: 10, height: 3, overflow: "scroll", scrollOffset: 99 }, 709 [Text("line0"), Text("line1"), Text("line2"), Text("line3")], 710 ); 711 const ln = layout(node, 10, 5); 712 const buf = new CellBuffer(10, 5); 713 paint(ln, buf); 714 // 4 items in 3-row viewport → max offset = 1 715 // scrollOffset=99 is clamped to 1, so line1–line3 visible 716 expect(readRow(buf, 0)).toBe("line1"); 717 expect(readRow(buf, 1)).toBe("line2"); 718 expect(readRow(buf, 2)).toBe("line3"); 719 }); 720 721 test("scroll clamps to 0 when content fits", () => { 722 const node = VStack( 723 { width: 10, height: 5, overflow: "scroll", scrollOffset: 10 }, 724 [Text("line0"), Text("line1")], 725 ); 726 const ln = layout(node, 10, 5); 727 const buf = new CellBuffer(10, 5); 728 paint(ln, buf); 729 // 2 items in 5-row viewport → max offset = 0 730 // scrollOffset=10 clamped to 0 731 expect(readRow(buf, 0)).toBe("line0"); 732 expect(readRow(buf, 1)).toBe("line1"); 733 }); 734 }); 735 736 describe("flexWrap rendering", () => { 737 test("wrapping HStack paints children on multiple rows", () => { 738 const node = HStack({ width: 10, height: 5, flexWrap: "wrap" }, [ 739 VStack({ width: 6 }, [Text("AAAAAA")]), 740 VStack({ width: 6 }, [Text("BBBBBB")]), 741 ]); 742 const ln = layout(node, 10, 5); 743 const buf = new CellBuffer(10, 5); 744 paint(ln, buf); 745 // Row 1: "AAAAAA" at y=0 746 expect(readRow(buf, 0)).toBe("AAAAAA"); 747 // Row 2: "BBBBBB" at y=1 748 expect(readRow(buf, 1)).toBe("BBBBBB"); 749 }); 750 751 test("wrapping HStack with gap paints correctly", () => { 752 const node = HStack({ width: 12, height: 5, flexWrap: "wrap", gap: 1 }, [ 753 VStack({ width: 5 }, [Text("AAA")]), 754 VStack({ width: 5 }, [Text("BBB")]), 755 VStack({ width: 5 }, [Text("CCC")]), 756 ]); 757 const ln = layout(node, 12, 5); 758 const buf = new CellBuffer(12, 5); 759 paint(ln, buf); 760 // Row 1: "AAA" at x=0, "BBB" at x=6 (5+gap1) 761 // 5 + 1 + 5 = 11 ≤ 12, + 1 + 5 = 17 > 12 762 expect(buf.get(0, 0).char).toBe("A"); 763 expect(buf.get(6, 0).char).toBe("B"); 764 // Row 2: "CCC" at x=0, y=1+gap(1)=2... or y depends on row height 765 // Row height = 1 (Text is 1 line), gap between rows = 1 766 // So row 2 at y=2 767 expect(buf.get(0, 2).char).toBe("C"); 768 }); 769 770 test("wrapping HStack with bgColor fills all rows", () => { 771 const node = HStack( 772 { width: 10, height: 6, flexWrap: "wrap", bgColor: "color04" }, 773 [ 774 VStack({ width: 6, height: 2 }, []), 775 VStack({ width: 6, height: 2 }, []), 776 ], 777 ); 778 const ln = layout(node, 10, 6); 779 const buf = new CellBuffer(10, 6); 780 paint(ln, buf); 781 // Background should fill the entire HStack rect 782 expect(buf.get(0, 0).bgColor).toBe("color04"); 783 expect(buf.get(9, 3).bgColor).toBe("color04"); 784 }); 785 786 test("wrapping HStack clips children to container bounds", () => { 787 // Container is 10x3, wrapping creates rows beyond height 788 const node = HStack({ width: 10, height: 3, flexWrap: "wrap" }, [ 789 VStack({ width: 10, height: 2 }, [Text("Row1")]), 790 VStack({ width: 10, height: 2 }, [Text("Row2")]), 791 ]); 792 const ln = layout(node, 10, 3); 793 const buf = new CellBuffer(10, 4); 794 paint(ln, buf); 795 // Row 1 at y=0, visible 796 expect(readRow(buf, 0)).toBe("Row1"); 797 // Row 2 at y=2, first line visible (y=2 < height=3) 798 expect(readRow(buf, 2)).toBe("Row2"); 799 // Row 2 second line at y=3, clipped (y=3 ≥ height=3) 800 expect(readRow(buf, 3)).toBe(""); 801 }); 802 }); 803 804 describe("nested layout", () => { 805 test("editor-like layout paints correctly", () => { 806 const node = HStack({ width: 30, height: 5 }, [ 807 VStack({ width: 10 }, [Text("sidebar")]), 808 VStack({ flex: 1 }, [ 809 VStack({ height: 1 }, [Text("tab bar")]), 810 VStack({ flex: 1 }, [Text("editor")]), 811 VStack({ height: 1 }, [Text("status")]), 812 ]), 813 ]); 814 const ln = layout(node, 30, 5); 815 const buf = new CellBuffer(30, 5); 816 paint(ln, buf); 817 818 // Sidebar at col 0 819 expect(readRow(buf, 0).startsWith("sidebar")).toBe(true); 820 // Tab bar at col 10, row 0 821 expect(buf.get(10, 0).char).toBe("t"); 822 // Editor at col 10, row 1 823 expect(buf.get(10, 1).char).toBe("e"); 824 // Status at col 10, row 4 825 expect(buf.get(10, 4).char).toBe("s"); 826 }); 827 }); 828 829 describe("container background", () => { 830 test("bgColor fills the container rect", () => { 831 const node = VStack({ width: 5, height: 3, bgColor: "color04" }, []); 832 const ln = layout(node, 5, 3); 833 const buf = new CellBuffer(5, 3); 834 paint(ln, buf); 835 836 for (let y = 0; y < 3; y++) { 837 for (let x = 0; x < 5; x++) { 838 expect(buf.get(x, y).bgColor).toBe("color04"); 839 } 840 } 841 }); 842 843 test("bgColor fills behind child text", () => { 844 const node = VStack({ width: 10, height: 3, bgColor: "color04" }, [ 845 Text("Hi"), 846 ]); 847 const ln = layout(node, 10, 3); 848 const buf = new CellBuffer(10, 3); 849 paint(ln, buf); 850 851 // Text cells have the container background 852 expect(buf.get(0, 0).char).toBe("H"); 853 expect(buf.get(0, 0).bgColor).toBe("color04"); 854 // Empty cells in the container also have the background 855 expect(buf.get(5, 0).bgColor).toBe("color04"); 856 expect(buf.get(0, 2).bgColor).toBe("color04"); 857 }); 858 859 test("nested container bgColor only fills inner rect", () => { 860 const node = VStack({ width: 10, height: 5 }, [ 861 VStack({ width: 6, height: 2, bgColor: "color01" }, [Text("X")]), 862 ]); 863 const ln = layout(node, 10, 5); 864 const buf = new CellBuffer(10, 5); 865 paint(ln, buf); 866 867 // Inner rect (0,0)-(5,1) has red background 868 expect(buf.get(0, 0).bgColor).toBe("color01"); 869 expect(buf.get(5, 1).bgColor).toBe("color01"); 870 // Outside inner rect has no background 871 expect(buf.get(7, 0).bgColor).toBeNull(); 872 expect(buf.get(0, 3).bgColor).toBeNull(); 873 }); 874 875 test("container without bgColor has no background fill", () => { 876 const node = VStack({ width: 5, height: 3 }, [Text("Hi")]); 877 const ln = layout(node, 5, 3); 878 const buf = new CellBuffer(5, 3); 879 paint(ln, buf); 880 881 // Empty cells should have no bgColor 882 expect(buf.get(4, 2).bgColor).toBeNull(); 883 }); 884 885 test("HStack bgColor fills its rect", () => { 886 const node = HStack({ width: 8, height: 2, bgColor: "color02" }, [ 887 VStack({ width: 4 }, [Text("AB")]), 888 ]); 889 const ln = layout(node, 8, 2); 890 const buf = new CellBuffer(8, 2); 891 paint(ln, buf); 892 893 // All cells in the HStack rect have green background 894 expect(buf.get(0, 0).bgColor).toBe("color02"); 895 expect(buf.get(7, 1).bgColor).toBe("color02"); 896 }); 897 }); 898 899 describe("style inheritance", () => { 900 test("Text inherits fgColor from parent container", () => { 901 const node = VStack({ width: 10, height: 1, fgColor: "color01" }, [ 902 Text("hello"), 903 ]); 904 const ln = layout(node, 10, 1); 905 const buf = new CellBuffer(10, 1); 906 paint(ln, buf); 907 908 expect(buf.get(0, 0).char).toBe("h"); 909 expect(buf.get(0, 0).fgColor).toBe("color01"); 910 }); 911 912 test("Text inherits bgColor from parent container", () => { 913 const node = VStack({ width: 10, height: 1, bgColor: "color04" }, [ 914 Text("hello"), 915 ]); 916 const ln = layout(node, 10, 1); 917 const buf = new CellBuffer(10, 1); 918 paint(ln, buf); 919 920 expect(buf.get(0, 0).char).toBe("h"); 921 expect(buf.get(0, 0).bgColor).toBe("color04"); 922 }); 923 924 test("Text explicit fgColor overrides inherited", () => { 925 const node = VStack({ width: 10, height: 1, fgColor: "color01" }, [ 926 Text("hello", { fgColor: "color02" }), 927 ]); 928 const ln = layout(node, 10, 1); 929 const buf = new CellBuffer(10, 1); 930 paint(ln, buf); 931 932 expect(buf.get(0, 0).fgColor).toBe("color02"); 933 }); 934 935 test("Text explicit bgColor overrides inherited", () => { 936 const node = VStack({ width: 10, height: 1, bgColor: "color04" }, [ 937 Text("hello", { bgColor: "color01" }), 938 ]); 939 const ln = layout(node, 10, 1); 940 const buf = new CellBuffer(10, 1); 941 paint(ln, buf); 942 943 expect(buf.get(0, 0).bgColor).toBe("color01"); 944 }); 945 946 test("nested container overrides outer fgColor", () => { 947 const node = VStack({ width: 10, height: 2, fgColor: "color01" }, [ 948 Text("outer"), 949 VStack({ fgColor: "color06" }, [Text("inner")]), 950 ]); 951 const ln = layout(node, 10, 2); 952 const buf = new CellBuffer(10, 2); 953 paint(ln, buf); 954 955 expect(buf.get(0, 0).fgColor).toBe("color01"); 956 expect(buf.get(0, 1).fgColor).toBe("color06"); 957 }); 958 959 test("Text inherits bold from parent container", () => { 960 const node = VStack({ width: 10, height: 1, bold: true }, [ 961 Text("hello"), 962 ]); 963 const ln = layout(node, 10, 1); 964 const buf = new CellBuffer(10, 1); 965 paint(ln, buf); 966 967 expect(buf.get(0, 0).bold).toBe(true); 968 }); 969 970 test("inheritance crosses multiple nesting levels", () => { 971 const node = VStack({ width: 10, height: 1, fgColor: "color01" }, [ 972 HStack({}, [VStack({}, [Text("deep")])]), 973 ]); 974 const ln = layout(node, 10, 1); 975 const buf = new CellBuffer(10, 1); 976 paint(ln, buf); 977 978 expect(buf.get(0, 0).fgColor).toBe("color01"); 979 }); 980 981 test("HStack inherits from parent VStack and propagates to children", () => { 982 const node = VStack({ width: 20, height: 1, fgColor: "color03" }, [ 983 HStack({}, [ 984 VStack({ width: 5 }, [Text("A")]), 985 VStack({ width: 5 }, [Text("B", { fgColor: "color04" })]), 986 ]), 987 ]); 988 const ln = layout(node, 20, 1); 989 const buf = new CellBuffer(20, 1); 990 paint(ln, buf); 991 992 // A inherits yellow from root 993 expect(buf.get(0, 0).fgColor).toBe("color03"); 994 // B overrides with blue 995 expect(buf.get(5, 0).fgColor).toBe("color04"); 996 }); 997 998 test("repeat fill text inherits styles from container", () => { 999 const node = VStack( 1000 { width: 10, height: 1, fgColor: "color01", bgColor: "color04" }, 1001 [Text("-", { repeat: "fill" })], 1002 ); 1003 const ln = layout(node, 10, 1); 1004 const buf = new CellBuffer(10, 1); 1005 paint(ln, buf); 1006 1007 expect(buf.get(0, 0).char).toBe("-"); 1008 expect(buf.get(0, 0).fgColor).toBe("color01"); 1009 expect(buf.get(0, 0).bgColor).toBe("color04"); 1010 }); 1011 1012 test("TextInput inherits styles from parent container", () => { 1013 const node = VStack({ width: 10, height: 1, fgColor: "color06" }, [ 1014 TextInput({ value: "hi", onChange: () => {} }), 1015 ]); 1016 const ln = layout(node, 10, 1); 1017 const buf = new CellBuffer(10, 1); 1018 paint(ln, buf); 1019 1020 expect(buf.get(0, 0).char).toBe("h"); 1021 expect(buf.get(0, 0).fgColor).toBe("color06"); 1022 }); 1023 1024 test("TextInput placeholder inherits styles from parent container", () => { 1025 const node = VStack({ width: 10, height: 1, fgColor: "color02" }, [ 1026 TextInput({ 1027 value: "", 1028 onChange: () => {}, 1029 placeholder: Text("type..."), 1030 }), 1031 ]); 1032 const ln = layout(node, 10, 1); 1033 const buf = new CellBuffer(10, 1); 1034 paint(ln, buf); 1035 1036 // Placeholder "type..." should inherit fgColor: "color02" from parent VStack 1037 expect(buf.get(0, 0).char).toBe("t"); 1038 expect(buf.get(0, 0).fgColor).toBe("color02"); 1039 }); 1040 1041 test("TextInput placeholder explicit style overrides inherited", () => { 1042 const node = VStack({ width: 10, height: 1, fgColor: "color02" }, [ 1043 TextInput({ 1044 value: "", 1045 onChange: () => {}, 1046 placeholder: Text("type...", { fgColor: "color08" }), 1047 }), 1048 ]); 1049 const ln = layout(node, 10, 1); 1050 const buf = new CellBuffer(10, 1); 1051 paint(ln, buf); 1052 1053 // Explicit fgColor on placeholder should win over inherited 1054 expect(buf.get(0, 0).char).toBe("t"); 1055 expect(buf.get(0, 0).fgColor).toBe("color08"); 1056 }); 1057 }); 1058 1059 describe("focusStyle", () => { 1060 test("focused TextInput focusStyle overrides its normal style", () => { 1061 const node = TextInput({ 1062 width: 4, 1063 height: 1, 1064 value: "X", 1065 onChange: () => {}, 1066 focused: true, 1067 fgColor: "color01", 1068 bgColor: "color02", 1069 bold: false, 1070 italic: false, 1071 underline: false, 1072 focusStyle: { 1073 fgColor: "color03", 1074 bgColor: "color04", 1075 bold: true, 1076 italic: true, 1077 underline: true, 1078 }, 1079 }); 1080 const ln = layout(node, 4, 1); 1081 const buf = new CellBuffer(4, 1); 1082 1083 paint(ln, buf); 1084 1085 const cell = buf.get(0, 0); 1086 expect(cell.fgColor).toBe("color03"); 1087 expect(cell.bgColor).toBe("color04"); 1088 expect(cell.bold).toBe(true); 1089 expect(cell.italic).toBe(true); 1090 expect(cell.underline).toBe(true); 1091 }); 1092 1093 test("container with focused=true applies focusStyle bgColor", () => { 1094 const node = VStack({ width: 10, height: 3 }, [ 1095 HStack( 1096 { 1097 onClick: () => {}, 1098 focused: true, 1099 bgColor: "color00", 1100 focusStyle: { bgColor: "color06" }, 1101 }, 1102 [Text("Btn")], 1103 ), 1104 ]); 1105 const ln = layout(node, 10, 3); 1106 const buf = new CellBuffer(10, 3); 1107 paint(ln, buf); 1108 1109 // The focused element's bgColor should be overridden to cyan 1110 expect(buf.get(0, 0).bgColor).toBe("color06"); 1111 }); 1112 1113 test("focusStyle fgColor is inherited by child Text", () => { 1114 const node = VStack({ width: 10, height: 1 }, [ 1115 HStack( 1116 { 1117 onClick: () => {}, 1118 focused: true, 1119 fgColor: "color07", 1120 focusStyle: { fgColor: "color00" }, 1121 }, 1122 [Text("Btn")], 1123 ), 1124 ]); 1125 const ln = layout(node, 10, 1); 1126 const buf = new CellBuffer(10, 1); 1127 paint(ln, buf); 1128 1129 // Text should inherit the focusStyle fgColor 1130 expect(buf.get(0, 0).char).toBe("B"); 1131 expect(buf.get(0, 0).fgColor).toBe("color00"); 1132 }); 1133 1134 test("container with focused=false uses normal style", () => { 1135 const node = VStack({ width: 10, height: 1 }, [ 1136 HStack( 1137 { 1138 onClick: () => {}, 1139 focused: false, 1140 bgColor: "color00", 1141 fgColor: "color07", 1142 focusStyle: { bgColor: "color06", fgColor: "color00" }, 1143 }, 1144 [Text("Btn")], 1145 ), 1146 ]); 1147 const ln = layout(node, 10, 1); 1148 const buf = new CellBuffer(10, 1); 1149 paint(ln, buf); 1150 1151 // Not focused — normal styles apply 1152 expect(buf.get(0, 0).bgColor).toBe("color00"); 1153 expect(buf.get(0, 0).fgColor).toBe("color07"); 1154 }); 1155 1156 test("Text explicit fgColor overrides focusStyle inheritance", () => { 1157 const node = VStack({ width: 10, height: 1 }, [ 1158 HStack( 1159 { 1160 onClick: () => {}, 1161 focused: true, 1162 fgColor: "color07", 1163 focusStyle: { fgColor: "color00" }, 1164 }, 1165 [Text("Btn", { fgColor: "color01" })], 1166 ), 1167 ]); 1168 const ln = layout(node, 10, 1); 1169 const buf = new CellBuffer(10, 1); 1170 paint(ln, buf); 1171 1172 // Text's explicit fgColor wins over focusStyle inheritance 1173 expect(buf.get(0, 0).fgColor).toBe("color01"); 1174 }); 1175 1176 test("focusStyle with no focused prop uses normal style", () => { 1177 const node = VStack({ width: 10, height: 1 }, [ 1178 HStack( 1179 { 1180 onClick: () => {}, 1181 bgColor: "color00", 1182 focusStyle: { bgColor: "color06" }, 1183 }, 1184 [Text("Btn")], 1185 ), 1186 ]); 1187 const ln = layout(node, 10, 1); 1188 const buf = new CellBuffer(10, 1); 1189 paint(ln, buf); 1190 1191 // No focused prop — normal style applies 1192 expect(buf.get(0, 0).bgColor).toBe("color00"); 1193 }); 1194 }); 1195});