70160
Ce
cel-tui / files
cel-tui / packages / core / src / layout.test.ts
54.4 KB
1import { describe, expect, test } from "bun:test"; 2import { measureContentHeight } from "./index.js"; 3import { type LayoutNode, layout, type Rect } from "./layout.js"; 4import { HStack, VStack } from "./primitives/stacks.js"; 5import { Text } from "./primitives/text.js"; 6import { TextInput } from "./primitives/text-input.js"; 7 8/** Helper to extract just the rect from a layout result. */ 9function rect(ln: LayoutNode): Rect { 10 return ln.rect; 11} 12 13/** Helper to get child rects. */ 14function childRects(ln: LayoutNode): Rect[] { 15 return ln.children.map((c) => c.rect); 16} 17 18function item<T>(items: readonly T[], index: number): T { 19 const value = items[index]; 20 expect(value).toBeDefined(); 21 if (value === undefined) { 22 throw new Error(`Missing item at index ${index}`); 23 } 24 return value; 25} 26 27describe("layout", () => { 28 describe("fixed sizing", () => { 29 test("container with fixed width and height", () => { 30 const node = VStack({ width: 30, height: 10 }, []); 31 const result = layout(node, 80, 24); 32 expect(rect(result)).toEqual({ x: 0, y: 0, width: 30, height: 10 }); 33 }); 34 35 test("child with fixed size inside a container", () => { 36 const node = VStack({ width: 80, height: 24 }, [ 37 VStack({ width: 20, height: 5 }, []), 38 ]); 39 const result = layout(node, 80, 24); 40 expect(childRects(result)).toEqual([ 41 { x: 0, y: 0, width: 20, height: 5 }, 42 ]); 43 }); 44 45 test("root without sizing fills available space", () => { 46 const node = VStack({}, []); 47 const result = layout(node, 80, 24); 48 expect(rect(result)).toEqual({ x: 0, y: 0, width: 80, height: 24 }); 49 }); 50 }); 51 52 describe("intrinsic sizing", () => { 53 test("Text height is 1 for single line", () => { 54 const node = VStack({ width: 80, height: 24 }, [Text("hello")]); 55 const result = layout(node, 80, 24); 56 expect(item(childRects(result), 0).height).toBe(1); 57 }); 58 59 test("Text height follows newlines", () => { 60 const node = VStack({ width: 80, height: 24 }, [ 61 Text("line1\nline2\nline3"), 62 ]); 63 const result = layout(node, 80, 24); 64 expect(item(childRects(result), 0).height).toBe(3); 65 }); 66 67 test("Text width fills parent in VStack", () => { 68 const node = VStack({ width: 40, height: 10 }, [Text("hi")]); 69 const result = layout(node, 80, 24); 70 expect(item(childRects(result), 0).width).toBe(40); 71 }); 72 73 test("VStack without height sizes to children", () => { 74 const node = VStack({ width: 80 }, [ 75 VStack({ height: 3 }, []), 76 VStack({ height: 5 }, []), 77 ]); 78 const result = layout(node, 80, 24); 79 expect(rect(result).height).toBe(8); 80 }); 81 82 test("HStack without width sizes to children", () => { 83 const node = HStack({ height: 10 }, [ 84 VStack({ width: 15 }, []), 85 VStack({ width: 25 }, []), 86 ]); 87 const result = layout(node, 80, 24); 88 expect(rect(result).width).toBe(40); 89 }); 90 }); 91 92 describe("visibleWidth-aware sizing", () => { 93 test("CJK text intrinsic width counts double-width chars", () => { 94 const node = HStack({ height: 10, alignItems: "start" }, [ 95 Text("世界"), // 2 chars, each width 2 = 4 columns 96 ]); 97 const result = layout(node, 80, 24); 98 expect(item(childRects(result), 0).width).toBe(4); 99 }); 100 101 test("CJK text word-wrap height uses visible width", () => { 102 // 6 CJK chars = 12 columns wide, wrap at width 5 → 3 visual lines 103 const node = VStack({ width: 5, height: 24 }, [ 104 Text("世界世界世界", { wrap: "word" }), 105 ]); 106 const result = layout(node, 80, 24); 107 expect(item(childRects(result), 0).height).toBe(3); 108 }); 109 110 test("Text word-wrap height follows visual word wrapping, not hard width wrapping", () => { 111 const node = VStack({ width: 6, height: 24 }, [ 112 Text("foo bar baz", { wrap: "word" }), 113 ]); 114 const result = layout(node, 80, 24); 115 expect(item(childRects(result), 0).height).toBe(3); 116 }); 117 118 test("TextInput intrinsic height follows visual word wrapping", () => { 119 const node = VStack({ width: 6, height: 24 }, [ 120 TextInput({ value: "foo bar baz", onChange: () => {} }), 121 ]); 122 const result = layout(node, 80, 24); 123 expect(item(childRects(result), 0).height).toBe(3); 124 }); 125 126 test("emoji text intrinsic width counts width 2", () => { 127 const node = HStack({ height: 10, alignItems: "start" }, [ 128 Text("😀hi"), // emoji(2) + h(1) + i(1) = 4 129 ]); 130 const result = layout(node, 80, 24); 131 expect(item(childRects(result), 0).width).toBe(4); 132 }); 133 134 test("mixed ASCII and CJK intrinsic width", () => { 135 const node = HStack({ height: 10, alignItems: "start" }, [ 136 Text("hi世界"), // h(1) + i(1) + 世(2) + 界(2) = 6 137 ]); 138 const result = layout(node, 80, 24); 139 expect(item(childRects(result), 0).width).toBe(6); 140 }); 141 }); 142 143 describe("flex sizing", () => { 144 test("single flex child fills remaining space in VStack", () => { 145 const node = VStack({ width: 80, height: 24 }, [ 146 VStack({ height: 4 }, []), 147 VStack({ flex: 1 }, []), 148 ]); 149 const result = layout(node, 80, 24); 150 expect(childRects(result)).toEqual([ 151 { x: 0, y: 0, width: 80, height: 4 }, 152 { x: 0, y: 4, width: 80, height: 20 }, 153 ]); 154 }); 155 156 test("multiple flex children share space proportionally", () => { 157 const node = VStack({ width: 80, height: 24 }, [ 158 VStack({ flex: 1 }, []), 159 VStack({ flex: 2 }, []), 160 ]); 161 const result = layout(node, 80, 24); 162 expect(childRects(result)).toEqual([ 163 { x: 0, y: 0, width: 80, height: 8 }, 164 { x: 0, y: 8, width: 80, height: 16 }, 165 ]); 166 }); 167 168 test("flex in HStack divides width", () => { 169 const node = HStack({ width: 80, height: 24 }, [ 170 VStack({ flex: 1 }, []), 171 VStack({ flex: 3 }, []), 172 ]); 173 const result = layout(node, 80, 24); 174 expect(childRects(result)).toEqual([ 175 { x: 0, y: 0, width: 20, height: 24 }, 176 { x: 20, y: 0, width: 60, height: 24 }, 177 ]); 178 }); 179 180 test("fixed + flex children", () => { 181 const node = VStack({ width: 80, height: 24 }, [ 182 VStack({ height: 1 }, []), 183 VStack({ flex: 1 }, []), 184 VStack({ height: 1 }, []), 185 ]); 186 const result = layout(node, 80, 24); 187 expect(childRects(result)).toEqual([ 188 { x: 0, y: 0, width: 80, height: 1 }, 189 { x: 0, y: 1, width: 80, height: 22 }, 190 { x: 0, y: 23, width: 80, height: 1 }, 191 ]); 192 }); 193 194 test("repeat fill claims remaining width in HStack", () => { 195 const node = HStack({ width: 10, height: 1 }, [ 196 Text("A"), 197 Text("-", { repeat: "fill" }), 198 Text("B"), 199 ]); 200 const result = layout(node, 10, 1); 201 expect(childRects(result)).toEqual([ 202 { x: 0, y: 0, width: 1, height: 1 }, 203 { x: 1, y: 0, width: 8, height: 1 }, 204 { x: 9, y: 0, width: 1, height: 1 }, 205 ]); 206 }); 207 }); 208 209 describe("percentage sizing", () => { 210 test("percentage width relative to parent", () => { 211 const node = VStack({ width: 80, height: 24 }, [ 212 VStack({ width: "50%", height: 10 }, []), 213 ]); 214 const result = layout(node, 80, 24); 215 expect(item(childRects(result), 0).width).toBe(40); 216 }); 217 218 test("percentage height relative to parent", () => { 219 const node = VStack({ width: 80, height: 24 }, [ 220 VStack({ width: 80, height: "50%" }, []), 221 ]); 222 const result = layout(node, 80, 24); 223 expect(item(childRects(result), 0).height).toBe(12); 224 }); 225 }); 226 227 describe("rounding (largest remainder)", () => { 228 test("distributes remainder to children with largest fractional parts", () => { 229 // 80 / 3 = 26.667 each → 27, 27, 26 230 const node = HStack({ width: 80, height: 24 }, [ 231 VStack({ flex: 1 }, []), 232 VStack({ flex: 1 }, []), 233 VStack({ flex: 1 }, []), 234 ]); 235 const result = layout(node, 80, 24); 236 const widths = childRects(result).map((r) => r.width); 237 expect(widths).toEqual([27, 27, 26]); 238 expect(widths.reduce((a, b) => a + b)).toBe(80); 239 }); 240 }); 241 242 describe("constraints", () => { 243 test("minWidth redistributes the remaining flex width", () => { 244 const node = HStack({ width: 80, height: 24 }, [ 245 VStack({ flex: 1, minWidth: 60 }, []), 246 VStack({ flex: 1 }, []), 247 ]); 248 const result = layout(node, 80, 24); 249 const widths = childRects(result).map((child) => child.width); 250 expect(widths).toEqual([60, 20]); 251 expect(widths.reduce((sum, width) => sum + width, 0)).toBe(80); 252 }); 253 254 test("maxHeight redistributes the remaining flex height", () => { 255 const node = VStack({ width: 80, height: 24 }, [ 256 VStack({ flex: 1, maxHeight: 10 }, []), 257 VStack({ flex: 1 }, []), 258 ]); 259 const result = layout(node, 80, 24); 260 const heights = childRects(result).map((child) => child.height); 261 expect(heights).toEqual([10, 14]); 262 expect(heights.reduce((sum, height) => sum + height, 0)).toBe(24); 263 }); 264 265 test("mixed minWidth and maxWidth constraints keep redistributing flex width", () => { 266 const node = HStack({ width: 100, height: 24 }, [ 267 VStack({ flex: 1, minWidth: 80 }, []), 268 VStack({ flex: 1, maxWidth: 10 }, []), 269 ]); 270 const result = layout(node, 100, 24); 271 const widths = childRects(result).map((child) => child.width); 272 expect(widths).toEqual([90, 10]); 273 expect(widths.reduce((sum, width) => sum + width, 0)).toBe(100); 274 }); 275 }); 276 277 describe("gap", () => { 278 test("VStack gap adds spacing between children", () => { 279 const node = VStack({ width: 80, height: 24, gap: 1 }, [ 280 VStack({ height: 3 }, []), 281 VStack({ height: 3 }, []), 282 VStack({ height: 3 }, []), 283 ]); 284 const result = layout(node, 80, 24); 285 const rects = childRects(result); 286 expect(rects[0]).toEqual({ x: 0, y: 0, width: 80, height: 3 }); 287 expect(rects[1]).toEqual({ x: 0, y: 4, width: 80, height: 3 }); 288 expect(rects[2]).toEqual({ x: 0, y: 8, width: 80, height: 3 }); 289 }); 290 291 test("HStack gap adds spacing between children", () => { 292 const node = HStack({ width: 80, height: 24, gap: 2 }, [ 293 VStack({ width: 20 }, []), 294 VStack({ width: 20 }, []), 295 ]); 296 const result = layout(node, 80, 24); 297 const rects = childRects(result); 298 expect(rects[0]).toEqual({ x: 0, y: 0, width: 20, height: 24 }); 299 expect(rects[1]).toEqual({ x: 22, y: 0, width: 20, height: 24 }); 300 }); 301 302 test("gap is subtracted before flex distribution", () => { 303 const node = VStack({ width: 80, height: 24, gap: 1 }, [ 304 VStack({ height: 4 }, []), 305 VStack({ flex: 1 }, []), 306 ]); 307 const result = layout(node, 80, 24); 308 const rects = childRects(result); 309 // 24 - 4 (fixed) - 1 (gap) = 19 for flex 310 expect(rects[0]).toEqual({ x: 0, y: 0, width: 80, height: 4 }); 311 expect(rects[1]).toEqual({ x: 0, y: 5, width: 80, height: 19 }); 312 }); 313 }); 314 315 describe("padding", () => { 316 test("padding reduces available space for children", () => { 317 const node = VStack({ width: 80, height: 24, padding: { x: 2, y: 1 } }, [ 318 VStack({ flex: 1 }, []), 319 ]); 320 const result = layout(node, 80, 24); 321 // Children start at (2, 1) with reduced size 322 expect(childRects(result)[0]).toEqual({ 323 x: 2, 324 y: 1, 325 width: 76, 326 height: 22, 327 }); 328 }); 329 330 test("padding with gap", () => { 331 const node = VStack( 332 { width: 80, height: 24, padding: { x: 1 }, gap: 1 }, 333 [VStack({ height: 3 }, []), VStack({ height: 3 }, [])], 334 ); 335 const result = layout(node, 80, 24); 336 const rects = childRects(result); 337 expect(rects[0]).toEqual({ x: 1, y: 0, width: 78, height: 3 }); 338 expect(rects[1]).toEqual({ x: 1, y: 4, width: 78, height: 3 }); 339 }); 340 }); 341 342 describe("VStack child positioning", () => { 343 test("children stack top to bottom", () => { 344 const node = VStack({ width: 80, height: 24 }, [ 345 VStack({ height: 5 }, []), 346 VStack({ height: 3 }, []), 347 VStack({ height: 7 }, []), 348 ]); 349 const result = layout(node, 80, 24); 350 const rects = childRects(result); 351 expect(item(rects, 0).y).toBe(0); 352 expect(item(rects, 1).y).toBe(5); 353 expect(item(rects, 2).y).toBe(8); 354 }); 355 }); 356 357 describe("HStack child positioning", () => { 358 test("children stack left to right", () => { 359 const node = HStack({ width: 80, height: 24 }, [ 360 VStack({ width: 20 }, []), 361 VStack({ width: 15 }, []), 362 VStack({ width: 30 }, []), 363 ]); 364 const result = layout(node, 80, 24); 365 const rects = childRects(result); 366 expect(item(rects, 0).x).toBe(0); 367 expect(item(rects, 1).x).toBe(20); 368 expect(item(rects, 2).x).toBe(35); 369 }); 370 }); 371 372 describe("cross-axis intrinsic sizing", () => { 373 test("HStack inside VStack uses max child height, not sum of widths", () => { 374 // HStack with 3 Text children: each is 1 line tall. 375 // HStack's intrinsic height should be 1 (max), not sum of widths. 376 const node = VStack({ width: 80, height: 24 }, [ 377 HStack({}, [Text("hello"), Text("world")]), 378 Text("below"), 379 ]); 380 const result = layout(node, 80, 24); 381 const rects = childRects(result); 382 // HStack height = 1 (tallest child is a single-line Text) 383 expect(item(rects, 0).height).toBe(1); 384 // "below" Text starts right after 385 expect(item(rects, 1).y).toBe(1); 386 expect(item(rects, 1).height).toBe(1); 387 }); 388 389 test("HStack intrinsic height with multi-line text child", () => { 390 const node = VStack({ width: 80, height: 24 }, [ 391 HStack({}, [ 392 Text("a"), 393 Text("b\nc\nd"), // 3 lines tall 394 ]), 395 ]); 396 const result = layout(node, 80, 24); 397 // HStack height = max child height = 3 398 expect(item(childRects(result), 0).height).toBe(3); 399 }); 400 401 test("VStack inside HStack uses max child width, not sum of heights", () => { 402 const node = HStack({ width: 80, height: 24 }, [ 403 VStack({}, [VStack({ height: 3 }, []), VStack({ height: 5 }, [])]), 404 VStack({ width: 10 }, []), 405 ]); 406 const result = layout(node, 80, 24); 407 const rects = childRects(result); 408 // First VStack intrinsic width should fill parent (80), not 3+5=8 409 // Actually with no explicit width children, innerCross is used. 410 // The second child starts after the first. 411 // VStack intrinsic width = max of children's widths on cross axis 412 // Its children have no explicit width, so they'd fill available. 413 // Let's just check that it doesn't sum heights as width. 414 expect(item(rects, 0).width).not.toBe(8); 415 }); 416 417 test("hello example pattern: VStack with HStack children stacks tightly", () => { 418 // Mimics the hello.ts layout pattern that was broken. 419 // Key assertion: HStack children get height=1 (max child height), 420 // not the sum of children widths. 421 const node = VStack({ width: 80 }, [ 422 Text("logo line"), 423 Text(""), 424 HStack({ gap: 2 }, [Text("item1"), Text("item2"), Text("item3")]), 425 Text(""), 426 HStack({ gap: 1 }, [Text("a"), Text("b")]), 427 ]); 428 const result = layout(node, 80, 24); 429 const rects = childRects(result); 430 // All children should be height 1 431 expect(item(rects, 0).height).toBe(1); // Text 432 expect(item(rects, 1).height).toBe(1); // Text("") 433 expect(item(rects, 2).height).toBe(1); // HStack (was broken: summed widths) 434 expect(item(rects, 3).height).toBe(1); // Text("") 435 expect(item(rects, 4).height).toBe(1); // HStack 436 // Children stack sequentially with no gaps 437 expect(item(rects, 0).y).toBe(0); 438 expect(item(rects, 1).y).toBe(1); 439 expect(item(rects, 2).y).toBe(2); 440 expect(item(rects, 3).y).toBe(3); 441 expect(item(rects, 4).y).toBe(4); 442 // Total intrinsic height = 5 443 expect(result.rect.height).toBe(5); 444 }); 445 }); 446 447 describe("justifyContent", () => { 448 test("start is the default (no offset)", () => { 449 const node = VStack({ width: 80, height: 24, justifyContent: "start" }, [ 450 VStack({ height: 3 }, []), 451 VStack({ height: 3 }, []), 452 ]); 453 const result = layout(node, 80, 24); 454 const rects = childRects(result); 455 expect(item(rects, 0).y).toBe(0); 456 expect(item(rects, 1).y).toBe(3); 457 }); 458 459 test("end pushes children to the end of the main axis", () => { 460 const node = VStack({ width: 80, height: 24, justifyContent: "end" }, [ 461 VStack({ height: 3 }, []), 462 VStack({ height: 3 }, []), 463 ]); 464 const result = layout(node, 80, 24); 465 const rects = childRects(result); 466 // remaining = 24 - 6 = 18 467 expect(item(rects, 0).y).toBe(18); 468 expect(item(rects, 1).y).toBe(21); 469 }); 470 471 test("center centers children on the main axis", () => { 472 const node = VStack({ width: 80, height: 24, justifyContent: "center" }, [ 473 VStack({ height: 3 }, []), 474 VStack({ height: 3 }, []), 475 ]); 476 const result = layout(node, 80, 24); 477 const rects = childRects(result); 478 // remaining = 24 - 6 = 18, offset = 9 479 expect(item(rects, 0).y).toBe(9); 480 expect(item(rects, 1).y).toBe(12); 481 }); 482 483 test("space-between distributes space between children", () => { 484 const node = VStack( 485 { width: 80, height: 24, justifyContent: "space-between" }, 486 [ 487 VStack({ height: 3 }, []), 488 VStack({ height: 3 }, []), 489 VStack({ height: 3 }, []), 490 ], 491 ); 492 const result = layout(node, 80, 24); 493 const rects = childRects(result); 494 // remaining = 24 - 9 = 15, gaps = 2, space per gap = 7.5 → 8, 7 495 expect(item(rects, 0).y).toBe(0); 496 // gap after first = floor(15/2) or largest-remainder? 497 // 15 / 2 = 7.5 each → 8, 7 via largest remainder 498 expect(item(rects, 1).y).toBe(3 + 8); // 11 499 expect(item(rects, 2).y).toBe(3 + 8 + 3 + 7); // 21 500 }); 501 502 test("space-between with single child acts like start", () => { 503 const node = VStack( 504 { width: 80, height: 24, justifyContent: "space-between" }, 505 [VStack({ height: 3 }, [])], 506 ); 507 const result = layout(node, 80, 24); 508 expect(item(childRects(result), 0).y).toBe(0); 509 }); 510 511 test("space-between ignores main-axis gap in HStack", () => { 512 const node = HStack( 513 { 514 width: 25, 515 height: 1, 516 justifyContent: "space-between", 517 gap: 4, 518 }, 519 [ 520 VStack({ width: 6, height: 1 }, []), 521 VStack({ width: 6, height: 1 }, []), 522 VStack({ width: 6, height: 1 }, []), 523 ], 524 ); 525 const result = layout(node, 25, 1); 526 const rects = childRects(result); 527 528 expect(rects[0]).toEqual({ x: 0, y: 0, width: 6, height: 1 }); 529 expect(rects[1]).toEqual({ x: 10, y: 0, width: 6, height: 1 }); 530 expect(rects[2]).toEqual({ x: 19, y: 0, width: 6, height: 1 }); 531 }); 532 533 test("center in HStack centers horizontally", () => { 534 const node = HStack({ width: 80, height: 24, justifyContent: "center" }, [ 535 VStack({ width: 10, height: 24 }, []), 536 VStack({ width: 10, height: 24 }, []), 537 ]); 538 const result = layout(node, 80, 24); 539 const rects = childRects(result); 540 // remaining = 80 - 20 = 60, offset = 30 541 expect(item(rects, 0).x).toBe(30); 542 expect(item(rects, 1).x).toBe(40); 543 }); 544 545 test("end in HStack pushes children right", () => { 546 const node = HStack({ width: 80, height: 24, justifyContent: "end" }, [ 547 VStack({ width: 20, height: 24 }, []), 548 ]); 549 const result = layout(node, 80, 24); 550 expect(item(childRects(result), 0).x).toBe(60); 551 }); 552 553 test("justifyContent with padding", () => { 554 const node = VStack( 555 { 556 width: 80, 557 height: 24, 558 justifyContent: "center", 559 padding: { y: 2 }, 560 }, 561 [VStack({ height: 4 }, [])], 562 ); 563 const result = layout(node, 80, 24); 564 // inner height = 24 - 4 = 20, remaining = 20 - 4 = 16, offset = 8 565 // child y = padY + offset = 2 + 8 = 10 566 expect(item(childRects(result), 0).y).toBe(10); 567 }); 568 569 test("justifyContent with gap", () => { 570 const node = VStack( 571 { width: 80, height: 24, justifyContent: "end", gap: 2 }, 572 [VStack({ height: 3 }, []), VStack({ height: 3 }, [])], 573 ); 574 const result = layout(node, 80, 24); 575 const rects = childRects(result); 576 // total content = 3 + 2 + 3 = 8, remaining = 24 - 8 = 16 577 expect(item(rects, 0).y).toBe(16); 578 expect(item(rects, 1).y).toBe(21); // 16 + 3 + 2 579 }); 580 }); 581 582 describe("alignItems", () => { 583 test("stretch is the default (children fill cross axis)", () => { 584 const node = VStack({ width: 80, height: 24 }, [ 585 VStack({ height: 3 }, []), 586 ]); 587 const result = layout(node, 80, 24); 588 // cross axis = width, should fill parent 589 expect(item(childRects(result), 0).width).toBe(80); 590 expect(item(childRects(result), 0).x).toBe(0); 591 }); 592 593 test("center centers children on cross axis in VStack", () => { 594 const node = VStack({ width: 80, height: 24, alignItems: "center" }, [ 595 VStack({ width: 20, height: 3 }, []), 596 ]); 597 const result = layout(node, 80, 24); 598 // cross remaining = 80 - 20 = 60, offset = 30 599 expect(item(childRects(result), 0).x).toBe(30); 600 }); 601 602 test("end aligns children to the end of cross axis", () => { 603 const node = VStack({ width: 80, height: 24, alignItems: "end" }, [ 604 VStack({ width: 20, height: 3 }, []), 605 ]); 606 const result = layout(node, 80, 24); 607 // cross remaining = 80 - 20 = 60 608 expect(item(childRects(result), 0).x).toBe(60); 609 }); 610 611 test("start aligns children to the start of cross axis", () => { 612 const node = VStack({ width: 80, height: 24, alignItems: "start" }, [ 613 VStack({ width: 20, height: 3 }, []), 614 ]); 615 const result = layout(node, 80, 24); 616 expect(item(childRects(result), 0).x).toBe(0); 617 }); 618 619 test("center in HStack centers vertically", () => { 620 const node = HStack({ width: 80, height: 24, alignItems: "center" }, [ 621 VStack({ width: 20, height: 6 }, []), 622 ]); 623 const result = layout(node, 80, 24); 624 // cross = height, remaining = 24 - 6 = 18, offset = 9 625 expect(item(childRects(result), 0).y).toBe(9); 626 }); 627 628 test("end in HStack pushes children to the bottom", () => { 629 const node = HStack({ width: 80, height: 24, alignItems: "end" }, [ 630 VStack({ width: 20, height: 6 }, []), 631 ]); 632 const result = layout(node, 80, 24); 633 expect(item(childRects(result), 0).y).toBe(18); 634 }); 635 636 test("alignItems with padding", () => { 637 const node = VStack( 638 { 639 width: 80, 640 height: 24, 641 alignItems: "center", 642 padding: { x: 5 }, 643 }, 644 [VStack({ width: 20, height: 3 }, [])], 645 ); 646 const result = layout(node, 80, 24); 647 // inner width = 80 - 10 = 70, remaining = 70 - 20 = 50, offset = 25 648 // child x = padX + offset = 5 + 25 = 30 649 expect(item(childRects(result), 0).x).toBe(30); 650 }); 651 652 test("alignItems affects each child independently", () => { 653 const node = VStack({ width: 80, height: 24, alignItems: "center" }, [ 654 VStack({ width: 20, height: 3 }, []), 655 VStack({ width: 40, height: 3 }, []), 656 ]); 657 const result = layout(node, 80, 24); 658 const rects = childRects(result); 659 // child 1: (80 - 20) / 2 = 30 660 expect(item(rects, 0).x).toBe(30); 661 // child 2: (80 - 40) / 2 = 20 662 expect(item(rects, 1).x).toBe(20); 663 }); 664 665 test("center uses intrinsic cross size for Text children", () => { 666 // Text("hello") has intrinsic width 5. With alignItems center, 667 // it should be centered in 80 columns, not fill the width. 668 const node = VStack({ width: 80, height: 24, alignItems: "center" }, [ 669 Text("hello"), 670 ]); 671 const result = layout(node, 80, 24); 672 const r = item(childRects(result), 0); 673 // intrinsic width of "hello" = 5, cross = 80 674 // offset = (80 - 5) / 2 = 37 (floored from 37.5) 675 expect(r.x).toBe(37); 676 expect(r.width).toBe(5); 677 }); 678 679 test("center uses intrinsic cross size for HStack without explicit width", () => { 680 const node = VStack({ width: 80, height: 24, alignItems: "center" }, [ 681 HStack({ gap: 1 }, [Text("aa"), Text("bb")]), 682 ]); 683 const result = layout(node, 80, 24); 684 const r = item(childRects(result), 0); 685 // HStack intrinsic width = 2 + 1 + 2 = 5 686 expect(r.width).toBe(5); 687 expect(r.x).toBe(37); 688 }); 689 690 test("end uses intrinsic cross size for children without explicit width", () => { 691 const node = VStack({ width: 80, height: 24, alignItems: "end" }, [ 692 Text("hi"), 693 ]); 694 const result = layout(node, 80, 24); 695 const r = item(childRects(result), 0); 696 // intrinsic width of "hi" = 2 697 expect(r.width).toBe(2); 698 expect(r.x).toBe(78); 699 }); 700 701 test("stretch still fills cross axis (default)", () => { 702 const node = VStack({ width: 80, height: 24, alignItems: "stretch" }, [ 703 Text("hi"), 704 ]); 705 const result = layout(node, 80, 24); 706 expect(item(childRects(result), 0).width).toBe(80); 707 expect(item(childRects(result), 0).x).toBe(0); 708 }); 709 710 test("combined justifyContent center + alignItems center", () => { 711 const node = VStack( 712 { 713 width: 80, 714 height: 24, 715 justifyContent: "center", 716 alignItems: "center", 717 }, 718 [VStack({ width: 20, height: 4 }, [])], 719 ); 720 const result = layout(node, 80, 24); 721 const r = item(childRects(result), 0); 722 // main (y): (24 - 4) / 2 = 10 723 expect(r.y).toBe(10); 724 // cross (x): (80 - 20) / 2 = 30 725 expect(r.x).toBe(30); 726 }); 727 }); 728 729 describe("flexWrap", () => { 730 test("nowrap is the default — children stay on one line", () => { 731 // Three 30-wide children in an 80-wide HStack: total 90, no wrapping 732 const node = HStack({ width: 80, height: 24 }, [ 733 VStack({ width: 30, height: 5 }, []), 734 VStack({ width: 30, height: 5 }, []), 735 VStack({ width: 30, height: 5 }, []), 736 ]); 737 const result = layout(node, 80, 24); 738 const rects = childRects(result); 739 // All on same row (y=0), positioned left-to-right 740 expect(item(rects, 0).y).toBe(0); 741 expect(item(rects, 1).y).toBe(0); 742 expect(item(rects, 2).y).toBe(0); 743 expect(item(rects, 0).x).toBe(0); 744 expect(item(rects, 1).x).toBe(30); 745 expect(item(rects, 2).x).toBe(60); 746 }); 747 748 test("wrap splits children into rows when they exceed width", () => { 749 // Three 30-wide children in an 80-wide HStack with wrap 750 // Row 1: child0 (30) + child1 (30) = 60 ≤ 80 ✓ 751 // Row 2: child2 (30) — adding to row 1 would be 90 > 80 752 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 753 VStack({ width: 30, height: 5 }, []), 754 VStack({ width: 30, height: 5 }, []), 755 VStack({ width: 30, height: 5 }, []), 756 ]); 757 const result = layout(node, 80, 24); 758 const rects = childRects(result); 759 // Row 1: child0 at (0,0), child1 at (30,0) 760 expect(rects[0]).toEqual({ x: 0, y: 0, width: 30, height: 5 }); 761 expect(rects[1]).toEqual({ x: 30, y: 0, width: 30, height: 5 }); 762 // Row 2: child2 at (0,5) 763 expect(rects[2]).toEqual({ x: 0, y: 5, width: 30, height: 5 }); 764 }); 765 766 test("all children fit on one row — no wrapping", () => { 767 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 768 VStack({ width: 20, height: 3 }, []), 769 VStack({ width: 20, height: 3 }, []), 770 VStack({ width: 20, height: 3 }, []), 771 ]); 772 const result = layout(node, 80, 24); 773 const rects = childRects(result); 774 expect(rects[0]).toEqual({ x: 0, y: 0, width: 20, height: 3 }); 775 expect(rects[1]).toEqual({ x: 20, y: 0, width: 20, height: 3 }); 776 expect(rects[2]).toEqual({ x: 40, y: 0, width: 20, height: 3 }); 777 }); 778 779 test("each child on its own row when each exceeds half the width", () => { 780 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 781 VStack({ width: 50, height: 2 }, []), 782 VStack({ width: 50, height: 3 }, []), 783 VStack({ width: 50, height: 4 }, []), 784 ]); 785 const result = layout(node, 80, 24); 786 const rects = childRects(result); 787 expect(rects[0]).toEqual({ x: 0, y: 0, width: 50, height: 2 }); 788 expect(rects[1]).toEqual({ x: 0, y: 2, width: 50, height: 3 }); 789 expect(rects[2]).toEqual({ x: 0, y: 5, width: 50, height: 4 }); 790 }); 791 792 test("row height is the max cross-size of children in that row", () => { 793 // Row 1: height-2 and height-5 → row height 5 794 // Row 2: height-3 → row height 3 795 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 796 VStack({ width: 50, height: 2 }, []), 797 VStack({ width: 20, height: 5 }, []), 798 VStack({ width: 50, height: 3 }, []), 799 ]); 800 const result = layout(node, 80, 24); 801 const rects = childRects(result); 802 // Row 1: both at y=0, row height = max(2,5) = 5 803 expect(rects[0]).toEqual({ x: 0, y: 0, width: 50, height: 2 }); 804 expect(rects[1]).toEqual({ x: 50, y: 0, width: 20, height: 5 }); 805 // Row 2: starts at y=5 806 expect(rects[2]).toEqual({ x: 0, y: 5, width: 50, height: 3 }); 807 }); 808 809 test("gap between items within a row", () => { 810 const node = HStack({ width: 80, height: 24, flexWrap: "wrap", gap: 2 }, [ 811 VStack({ width: 30, height: 3 }, []), 812 VStack({ width: 30, height: 3 }, []), 813 VStack({ width: 30, height: 3 }, []), 814 ]); 815 const result = layout(node, 80, 24); 816 const rects = childRects(result); 817 // Row 1: child0(30) + gap(2) + child1(30) = 62 ≤ 80 ✓ 818 // + gap(2) + child2(30) = 94 > 80 ✗ 819 // Row 2: child2 820 expect(rects[0]).toEqual({ x: 0, y: 0, width: 30, height: 3 }); 821 expect(rects[1]).toEqual({ x: 32, y: 0, width: 30, height: 3 }); 822 // Row 2 starts at y = row1Height(3) + gap(2) = 5 823 expect(rects[2]).toEqual({ x: 0, y: 5, width: 30, height: 3 }); 824 }); 825 826 test("gap between rows", () => { 827 // Every child gets its own row, gap between rows 828 const node = HStack({ width: 80, height: 24, flexWrap: "wrap", gap: 1 }, [ 829 VStack({ width: 80, height: 2 }, []), 830 VStack({ width: 80, height: 3 }, []), 831 ]); 832 const result = layout(node, 80, 24); 833 const rects = childRects(result); 834 expect(rects[0]).toEqual({ x: 0, y: 0, width: 80, height: 2 }); 835 // gap between rows = 1 836 expect(rects[1]).toEqual({ x: 0, y: 3, width: 80, height: 3 }); 837 }); 838 839 test("flex children distribute remaining space within their row", () => { 840 // Row 1: fixed(20) + flex(1) → flex gets 80-20 = 60 841 // Row 2: flex(1) → flex gets 80 842 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 843 VStack({ width: 20, height: 3 }, []), 844 VStack({ flex: 1, height: 3 }, []), 845 VStack({ width: 90, height: 4 }, []), // wider than container, own row 846 ]); 847 const result = layout(node, 80, 24); 848 const rects = childRects(result); 849 // Row 1: fixed + flex 850 expect(rects[0]).toEqual({ x: 0, y: 0, width: 20, height: 3 }); 851 expect(rects[1]).toEqual({ x: 20, y: 0, width: 60, height: 3 }); 852 // Row 2: child wider than container 853 expect(item(rects, 2).y).toBe(3); 854 }); 855 856 test("multiple flex children in one row share space", () => { 857 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 858 VStack({ width: 20, height: 3 }, []), 859 VStack({ flex: 1, height: 3 }, []), 860 VStack({ flex: 1, height: 3 }, []), 861 ]); 862 const result = layout(node, 80, 24); 863 const rects = childRects(result); 864 // All fit in one row. Remaining = 80 - 20 = 60, split 30/30 865 expect(rects[0]).toEqual({ x: 0, y: 0, width: 20, height: 3 }); 866 expect(rects[1]).toEqual({ x: 20, y: 0, width: 30, height: 3 }); 867 expect(rects[2]).toEqual({ x: 50, y: 0, width: 30, height: 3 }); 868 }); 869 870 test("row flex redistribution honors minWidth after clamping", () => { 871 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 872 VStack({ width: 20, height: 3 }, []), 873 VStack({ flex: 1, minWidth: 40, height: 3 }, []), 874 VStack({ flex: 1, height: 3 }, []), 875 ]); 876 const result = layout(node, 80, 24); 877 const widths = childRects(result).map((child) => child.width); 878 expect(widths).toEqual([20, 40, 20]); 879 expect(widths.reduce((sum, width) => sum + width, 0)).toBe(80); 880 }); 881 882 test("row flex redistribution honors maxWidth after clamping", () => { 883 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 884 VStack({ width: 20, height: 3 }, []), 885 VStack({ flex: 1, maxWidth: 10, height: 3 }, []), 886 VStack({ flex: 1, height: 3 }, []), 887 ]); 888 const result = layout(node, 80, 24); 889 const widths = childRects(result).map((child) => child.width); 890 expect(widths).toEqual([20, 10, 50]); 891 expect(widths.reduce((sum, width) => sum + width, 0)).toBe(80); 892 }); 893 894 test("row flex redistribution keeps going after mixed minWidth and maxWidth clamps", () => { 895 const node = HStack({ width: 100, height: 24, flexWrap: "wrap" }, [ 896 VStack({ flex: 1, minWidth: 80, height: 3 }, []), 897 VStack({ flex: 1, maxWidth: 10, height: 3 }, []), 898 ]); 899 const result = layout(node, 100, 24); 900 const widths = childRects(result).map((child) => child.width); 901 expect(widths).toEqual([90, 10]); 902 expect(widths.reduce((sum, width) => sum + width, 0)).toBe(100); 903 }); 904 905 test("padding reduces available width for wrapping", () => { 906 // Inner width = 80 - 2*5 = 70 907 // Row 1: 40 ≤ 70 ✓, 40+40=80 > 70 ✗ 908 // Row 2: 40 909 const node = HStack( 910 { width: 80, height: 24, flexWrap: "wrap", padding: { x: 5, y: 2 } }, 911 [ 912 VStack({ width: 40, height: 3 }, []), 913 VStack({ width: 40, height: 3 }, []), 914 ], 915 ); 916 const result = layout(node, 80, 24); 917 const rects = childRects(result); 918 // Row 1 at padded origin 919 expect(rects[0]).toEqual({ x: 5, y: 2, width: 40, height: 3 }); 920 // Row 2 921 expect(rects[1]).toEqual({ x: 5, y: 5, width: 40, height: 3 }); 922 }); 923 924 test("alignItems stretch makes children fill row height", () => { 925 // Default alignItems is stretch. Children should fill their row's height. 926 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 927 VStack({ width: 50, height: 2 }, []), 928 VStack({ width: 20 }, []), // no explicit height 929 VStack({ width: 50, height: 3 }, []), 930 ]); 931 const result = layout(node, 80, 24); 932 const rects = childRects(result); 933 // Row 1: max height = 2 (first child), second child stretches to 2 934 // But wait — when the second child has no explicit height, under stretch 935 // it should fill the row height, which is determined by the tallest child. 936 // The tallest in row 1 is 2, so stretched child gets 2. 937 expect(item(rects, 0).height).toBe(2); 938 expect(item(rects, 1).height).toBe(2); 939 // Row 2 940 expect(item(rects, 2).height).toBe(3); 941 }); 942 943 test("alignItems center centers children vertically within row", () => { 944 const node = HStack( 945 { width: 80, height: 24, flexWrap: "wrap", alignItems: "center" }, 946 [ 947 VStack({ width: 50, height: 6 }, []), 948 VStack({ width: 20, height: 2 }, []), 949 ], 950 ); 951 const result = layout(node, 80, 24); 952 const rects = childRects(result); 953 // Single row, row height = 6 954 // First child: y=0, height=6 (fills row) 955 expect(rects[0]).toEqual({ x: 0, y: 0, width: 50, height: 6 }); 956 // Second child: centered in 6-high row → (6-2)/2 = 2 957 expect(rects[1]).toEqual({ x: 50, y: 2, width: 20, height: 2 }); 958 }); 959 960 test("alignItems end aligns children to bottom of row", () => { 961 const node = HStack( 962 { width: 80, height: 24, flexWrap: "wrap", alignItems: "end" }, 963 [ 964 VStack({ width: 50, height: 6 }, []), 965 VStack({ width: 20, height: 2 }, []), 966 ], 967 ); 968 const result = layout(node, 80, 24); 969 const rects = childRects(result); 970 // Row height = 6. Second child at bottom: y = 6-2 = 4 971 expect(rects[0]).toEqual({ x: 0, y: 0, width: 50, height: 6 }); 972 expect(rects[1]).toEqual({ x: 50, y: 4, width: 20, height: 2 }); 973 }); 974 975 test("justifyContent center centers items within each row", () => { 976 const node = HStack( 977 { width: 80, height: 24, flexWrap: "wrap", justifyContent: "center" }, 978 [ 979 VStack({ width: 30, height: 3 }, []), 980 VStack({ width: 30, height: 3 }, []), 981 VStack({ width: 30, height: 3 }, []), 982 ], 983 ); 984 const result = layout(node, 80, 24); 985 const rects = childRects(result); 986 // Row 1: child0+child1 = 60, remaining = 20, offset = 10 987 expect(rects[0]).toEqual({ x: 10, y: 0, width: 30, height: 3 }); 988 expect(rects[1]).toEqual({ x: 40, y: 0, width: 30, height: 3 }); 989 // Row 2: child2 = 30, remaining = 50, offset = 25 990 expect(rects[2]).toEqual({ x: 25, y: 3, width: 30, height: 3 }); 991 }); 992 993 test("justifyContent end pushes items to end of each row", () => { 994 const node = HStack( 995 { width: 80, height: 24, flexWrap: "wrap", justifyContent: "end" }, 996 [ 997 VStack({ width: 30, height: 3 }, []), 998 VStack({ width: 30, height: 3 }, []), 999 VStack({ width: 30, height: 3 }, []), 1000 ], 1001 ); 1002 const result = layout(node, 80, 24); 1003 const rects = childRects(result); 1004 // Row 1: 30+30 = 60, remaining = 20, offset = 20 1005 expect(rects[0]).toEqual({ x: 20, y: 0, width: 30, height: 3 }); 1006 expect(rects[1]).toEqual({ x: 50, y: 0, width: 30, height: 3 }); 1007 // Row 2: 30, remaining = 50, offset = 50 1008 expect(rects[2]).toEqual({ x: 50, y: 3, width: 30, height: 3 }); 1009 }); 1010 1011 test("justifyContent space-between distributes within each row", () => { 1012 const node = HStack( 1013 { 1014 width: 80, 1015 height: 24, 1016 flexWrap: "wrap", 1017 justifyContent: "space-between", 1018 }, 1019 [ 1020 VStack({ width: 20, height: 3 }, []), 1021 VStack({ width: 20, height: 3 }, []), 1022 VStack({ width: 20, height: 3 }, []), 1023 VStack({ width: 20, height: 3 }, []), 1024 ], 1025 ); 1026 const result = layout(node, 80, 24); 1027 const rects = childRects(result); 1028 // All 4 fit: 20*4 = 80 — no wrapping needed 1029 // Remaining = 0 with space-between 1030 expect(item(rects, 0).x).toBe(0); 1031 expect(item(rects, 1).x).toBe(20); 1032 expect(item(rects, 2).x).toBe(40); 1033 expect(item(rects, 3).x).toBe(60); 1034 }); 1035 1036 test("wrapping HStack space-between ignores main-axis gap", () => { 1037 const node = HStack( 1038 { 1039 width: 25, 1040 height: 4, 1041 flexWrap: "wrap", 1042 justifyContent: "space-between", 1043 gap: 4, 1044 }, 1045 [ 1046 VStack({ width: 6, height: 1 }, []), 1047 VStack({ width: 6, height: 1 }, []), 1048 VStack({ width: 6, height: 1 }, []), 1049 ], 1050 ); 1051 const result = layout(node, 25, 4); 1052 const rects = childRects(result); 1053 1054 expect(rects[0]).toEqual({ x: 0, y: 0, width: 6, height: 1 }); 1055 expect(rects[1]).toEqual({ x: 10, y: 0, width: 6, height: 1 }); 1056 expect(rects[2]).toEqual({ x: 19, y: 0, width: 6, height: 1 }); 1057 }); 1058 1059 test("intrinsic height of wrapping HStack equals sum of row heights", () => { 1060 // HStack with no explicit height should size to fit all rows 1061 const node = VStack({ width: 80, height: 24 }, [ 1062 HStack({ flexWrap: "wrap" }, [ 1063 VStack({ width: 50, height: 3 }, []), 1064 VStack({ width: 50, height: 4 }, []), 1065 ]), 1066 Text("below"), 1067 ]); 1068 const result = layout(node, 80, 24); 1069 const rects = childRects(result); 1070 // HStack: row1 height=3, row2 height=4, total = 7 1071 expect(item(rects, 0).height).toBe(7); 1072 // Text below starts at y=7 1073 expect(item(rects, 1).y).toBe(7); 1074 }); 1075 1076 test("re-measures flex child height at its allocated row width", () => { 1077 const node = VStack({ width: 10 }, [ 1078 HStack({ flexWrap: "wrap" }, [ 1079 VStack({ width: 5, height: 1 }, []), 1080 VStack({ flex: 1 }, [Text("abcdefghij", { wrap: "word" })]), 1081 ]), 1082 Text("after"), 1083 ]); 1084 1085 const result = layout(node, 10, 24); 1086 const wrappingRow = item(result.children, 0); 1087 const followingText = item(result.children, 1); 1088 const flexChild = item(wrappingRow.children, 1); 1089 1090 expect(result.rect.height).toBe(3); 1091 expect(wrappingRow.rect.height).toBe(2); 1092 expect(flexChild.rect).toEqual({ x: 5, y: 0, width: 5, height: 2 }); 1093 expect(item(flexChild.children, 0).rect.height).toBe(2); 1094 expect(followingText.rect.y).toBe(2); 1095 }); 1096 1097 test("uses constrained flex widths when measuring wrapped row height", () => { 1098 const node = VStack({ width: 12 }, [ 1099 HStack({ flexWrap: "wrap", alignItems: "start" }, [ 1100 VStack({ width: 4, height: 1 }, []), 1101 VStack({ flex: 1, minWidth: 6 }, [ 1102 Text("abcdefghijkl", { wrap: "word" }), 1103 ]), 1104 VStack({ flex: 1, maxWidth: 2 }, [Text("abcdef", { wrap: "word" })]), 1105 ]), 1106 Text("after"), 1107 ]); 1108 1109 const result = layout(node, 12, 24); 1110 const wrappingRow = item(result.children, 0); 1111 1112 expect(wrappingRow.children.map((child) => child.rect.width)).toEqual([ 1113 4, 6, 2, 1114 ]); 1115 expect(wrappingRow.children.map((child) => child.rect.height)).toEqual([ 1116 1, 2, 3, 1117 ]); 1118 expect(wrappingRow.rect.height).toBe(3); 1119 expect(item(result.children, 1).rect.y).toBe(3); 1120 }); 1121 1122 test("uses percentage widths when measuring wrapped row height", () => { 1123 const node = VStack({ width: 10 }, [ 1124 HStack({ flexWrap: "wrap", alignItems: "start" }, [ 1125 VStack({ width: "50%" }, [Text("abcdefghij", { wrap: "word" })]), 1126 VStack({ flex: 1 }, [Text("klmnopqrst", { wrap: "word" })]), 1127 ]), 1128 Text("after"), 1129 ]); 1130 1131 const result = layout(node, 10, 24); 1132 const wrappingRow = item(result.children, 0); 1133 1134 expect(wrappingRow.children.map((child) => child.rect.width)).toEqual([ 1135 5, 5, 1136 ]); 1137 expect(wrappingRow.children.map((child) => child.rect.height)).toEqual([ 1138 2, 2, 1139 ]); 1140 expect(wrappingRow.rect.height).toBe(2); 1141 expect(item(result.children, 1).rect.y).toBe(2); 1142 }); 1143 1144 test("intrinsic height with gap includes gaps between rows", () => { 1145 const node = VStack({ width: 80, height: 24 }, [ 1146 HStack({ flexWrap: "wrap", gap: 1 }, [ 1147 VStack({ width: 80, height: 2 }, []), 1148 VStack({ width: 80, height: 3 }, []), 1149 ]), 1150 Text("below"), 1151 ]); 1152 const result = layout(node, 80, 24); 1153 const rects = childRects(result); 1154 // Two rows: 2 + gap(1) + 3 = 6 1155 expect(item(rects, 0).height).toBe(6); 1156 expect(item(rects, 1).y).toBe(6); 1157 }); 1158 1159 test("Text children wrap based on intrinsic width", () => { 1160 // Text nodes have intrinsic width. They should wrap like other nodes. 1161 const node = HStack({ width: 20, height: 10, flexWrap: "wrap" }, [ 1162 Text("hello"), // width 5 1163 Text("world"), // width 5 1164 Text("this is long text"), // width 17 1165 ]); 1166 const result = layout(node, 20, 10); 1167 const rects = childRects(result); 1168 // Row 1: "hello"(5) + "world"(5) = 10 ≤ 20 ✓ 1169 // + "this is long text"(17) = 27 > 20 ✗ 1170 // Row 2: "this is long text" 1171 expect(item(rects, 0).y).toBe(0); 1172 expect(item(rects, 1).y).toBe(0); 1173 expect(item(rects, 2).y).toBe(1); // second row 1174 expect(item(rects, 0).x).toBe(0); 1175 expect(item(rects, 1).x).toBe(5); 1176 expect(item(rects, 2).x).toBe(0); 1177 }); 1178 1179 test("single child wider than container gets its own row", () => { 1180 const node = HStack({ width: 40, height: 24, flexWrap: "wrap" }, [ 1181 VStack({ width: 20, height: 3 }, []), 1182 VStack({ width: 50, height: 4 }, []), // wider than container 1183 VStack({ width: 20, height: 2 }, []), 1184 ]); 1185 const result = layout(node, 40, 24); 1186 const rects = childRects(result); 1187 // Row 1: child0 (20 ≤ 40) 1188 // child1 (20+50 > 40, starts new row) 1189 // Row 2: child1 (50 > 40, but it's the first item so it goes on this row) 1190 // Row 3: child2 (50+20 > 40, new row) 1191 expect(item(rects, 0).y).toBe(0); 1192 expect(item(rects, 1).y).toBe(3); // after row 1 (height 3) 1193 expect(item(rects, 2).y).toBe(7); // after row 2 (height 4) 1194 }); 1195 1196 test("empty HStack with flexWrap produces no children", () => { 1197 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, []); 1198 const result = layout(node, 80, 24); 1199 expect(result.children).toEqual([]); 1200 }); 1201 1202 test("single child in wrapping HStack", () => { 1203 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 1204 VStack({ width: 30, height: 5 }, []), 1205 ]); 1206 const result = layout(node, 80, 24); 1207 const rects = childRects(result); 1208 expect(rects[0]).toEqual({ x: 0, y: 0, width: 30, height: 5 }); 1209 }); 1210 1211 test("flexWrap on VStack is ignored (treated as nowrap)", () => { 1212 // flexWrap is only meaningful on HStack per the spec 1213 const node = VStack({ width: 80, height: 10, flexWrap: "wrap" }, [ 1214 VStack({ height: 4 }, []), 1215 VStack({ height: 4 }, []), 1216 VStack({ height: 4 }, []), 1217 ]); 1218 const result = layout(node, 80, 10); 1219 const rects = childRects(result); 1220 // Normal VStack behavior — children stack vertically, overflow 1221 expect(item(rects, 0).y).toBe(0); 1222 expect(item(rects, 1).y).toBe(4); 1223 expect(item(rects, 2).y).toBe(8); 1224 }); 1225 1226 test("nested wrapping HStack inside VStack", () => { 1227 const node = VStack({ width: 60, height: 24 }, [ 1228 Text("Title"), 1229 HStack({ flexWrap: "wrap", gap: 1 }, [ 1230 VStack({ width: 25, height: 2 }, []), 1231 VStack({ width: 25, height: 2 }, []), 1232 VStack({ width: 25, height: 2 }, []), 1233 ]), 1234 Text("Footer"), 1235 ]); 1236 const result = layout(node, 60, 24); 1237 const rects = childRects(result); 1238 1239 // Title at y=0, height=1 1240 expect(item(rects, 0).y).toBe(0); 1241 expect(item(rects, 0).height).toBe(1); 1242 1243 // HStack at y=1 1244 // Row 1: 25 + 1 + 25 = 51 ≤ 60, + 1 + 25 = 77 > 60 1245 // Row 2: 25 1246 // HStack height = 2 + 1 + 2 = 5 1247 expect(item(rects, 1).y).toBe(1); 1248 expect(item(rects, 1).height).toBe(5); 1249 1250 // Footer at y=6 1251 expect(item(rects, 2).y).toBe(6); 1252 1253 // Verify HStack's children 1254 const hstackChildren = item(result.children, 1).children; 1255 expect(item(hstackChildren, 0).rect).toEqual({ 1256 x: 0, 1257 y: 1, 1258 width: 25, 1259 height: 2, 1260 }); 1261 expect(item(hstackChildren, 1).rect).toEqual({ 1262 x: 26, 1263 y: 1, 1264 width: 25, 1265 height: 2, 1266 }); 1267 expect(item(hstackChildren, 2).rect).toEqual({ 1268 x: 0, 1269 y: 4, 1270 width: 25, 1271 height: 2, 1272 }); 1273 }); 1274 1275 test("constraints apply to children in wrapped rows", () => { 1276 const node = HStack({ width: 80, height: 24, flexWrap: "wrap" }, [ 1277 VStack({ flex: 1, minWidth: 30, height: 3 }, []), 1278 VStack({ flex: 1, minWidth: 30, height: 3 }, []), 1279 VStack({ flex: 1, minWidth: 30, height: 3 }, []), 1280 ]); 1281 const result = layout(node, 80, 24); 1282 const rects = childRects(result); 1283 // Flex children: need to decide row membership based on min sizes 1284 // Each has minWidth 30. Three at 30 = 90 > 80, so wrapping occurs. 1285 // Row 1: child0 + child1 at minWidth 30 each = 60 ≤ 80 1286 // Remaining = 80 - 0 (no fixed) = 80, shared between 2 flex → 40 each 1287 // Both ≥ 30 ✓ 1288 // Row 2: child2 → gets full 80 1289 expect(item(rects, 0).width).toBe(40); 1290 expect(item(rects, 1).width).toBe(40); 1291 expect(item(rects, 2).y).toBe(3); 1292 expect(item(rects, 2).width).toBe(80); 1293 }); 1294 }); 1295 1296 describe("nested layout", () => { 1297 test("editor-like layout: sidebar + main area", () => { 1298 const node = HStack({ width: 80, height: 24 }, [ 1299 VStack({ width: 20 }, []), 1300 VStack({ flex: 1 }, [ 1301 VStack({ height: 1 }, []), 1302 VStack({ flex: 1 }, []), 1303 VStack({ height: 1 }, []), 1304 ]), 1305 ]); 1306 const result = layout(node, 80, 24); 1307 const rects = childRects(result); 1308 1309 // Sidebar 1310 expect(rects[0]).toEqual({ x: 0, y: 0, width: 20, height: 24 }); 1311 // Main area 1312 expect(rects[1]).toEqual({ x: 20, y: 0, width: 60, height: 24 }); 1313 1314 // Main area children 1315 const mainChildren = item(result.children, 1).children; 1316 expect(item(mainChildren, 0).rect).toEqual({ 1317 x: 20, 1318 y: 0, 1319 width: 60, 1320 height: 1, 1321 }); 1322 expect(item(mainChildren, 1).rect).toEqual({ 1323 x: 20, 1324 y: 1, 1325 width: 60, 1326 height: 22, 1327 }); 1328 expect(item(mainChildren, 2).rect).toEqual({ 1329 x: 20, 1330 y: 23, 1331 width: 60, 1332 height: 1, 1333 }); 1334 }); 1335 }); 1336 1337 describe("cross-axis constraints", () => { 1338 test("maxHeight on child in HStack is respected", () => { 1339 const node = HStack({ width: 40, height: 10 }, [ 1340 VStack({ flex: 1, maxHeight: 3 }, [Text("hello")]), 1341 ]); 1342 const result = layout(node, 80, 24); 1343 expect(childRects(result)).toEqual([ 1344 { x: 0, y: 0, width: 40, height: 3 }, 1345 ]); 1346 }); 1347 1348 test("minHeight on child in HStack is respected", () => { 1349 const node = HStack({ width: 40, height: 10, alignItems: "start" }, [ 1350 VStack({ flex: 1, minHeight: 5 }, [Text("hi")]), 1351 ]); 1352 const result = layout(node, 80, 24); 1353 // Intrinsic height of VStack containing "hi" is 1, but minHeight is 5 1354 expect(item(childRects(result), 0).height).toBe(5); 1355 }); 1356 1357 test("maxWidth on child in VStack is respected", () => { 1358 const node = VStack({ width: 40, height: 10 }, [ 1359 HStack({ flex: 1, maxWidth: 20 }, [Text("x")]), 1360 ]); 1361 const result = layout(node, 80, 24); 1362 expect(item(childRects(result), 0).width).toBe(20); 1363 }); 1364 1365 test("TextInput maxHeight in HStack creates autogrowing input", () => { 1366 // Nest HStack in VStack so HStack gets intrinsic height 1367 // Empty value → height 1 1368 const node1 = VStack({ width: 80, height: 24 }, [ 1369 HStack({}, [ 1370 TextInput({ 1371 flex: 1, 1372 maxHeight: 5, 1373 value: "", 1374 onChange: () => {}, 1375 }), 1376 ]), 1377 Text("after"), 1378 ]); 1379 const r1 = layout(node1, 80, 24); 1380 expect(item(r1.children, 0).rect.height).toBe(1); 1381 expect(item(item(r1.children, 0).children, 0).rect.height).toBe(1); 1382 1383 // Multi-line value → grows to content height 1384 const node2 = VStack({ width: 80, height: 24 }, [ 1385 HStack({}, [ 1386 TextInput({ 1387 flex: 1, 1388 maxHeight: 5, 1389 value: "line1\nline2\nline3", 1390 onChange: () => {}, 1391 }), 1392 ]), 1393 Text("after"), 1394 ]); 1395 const r2 = layout(node2, 80, 24); 1396 expect(item(r2.children, 0).rect.height).toBe(3); 1397 expect(item(item(r2.children, 0).children, 0).rect.height).toBe(3); 1398 1399 // Many lines → capped at maxHeight 1400 const node3 = VStack({ width: 80, height: 24 }, [ 1401 HStack({}, [ 1402 TextInput({ 1403 flex: 1, 1404 maxHeight: 5, 1405 value: "1\n2\n3\n4\n5\n6\n7\n8", 1406 onChange: () => {}, 1407 }), 1408 ]), 1409 Text("after"), 1410 ]); 1411 const r3 = layout(node3, 80, 24); 1412 expect(item(r3.children, 0).rect.height).toBe(5); 1413 expect(item(item(r3.children, 0).children, 0).rect.height).toBe(5); 1414 }); 1415 1416 test("cross-axis constraints in intrinsic container sizing", () => { 1417 // HStack with no explicit height, child has maxHeight: 1418 // HStack intrinsic height should respect child's maxHeight 1419 const node = VStack({ width: 80, height: 24 }, [ 1420 HStack({}, [ 1421 TextInput({ 1422 flex: 1, 1423 maxHeight: 3, 1424 value: "1\n2\n3\n4\n5", 1425 onChange: () => {}, 1426 }), 1427 ]), 1428 Text("after"), 1429 ]); 1430 const result = layout(node, 80, 24); 1431 // HStack should be 3 tall (capped by TextInput's maxHeight) 1432 expect(item(result.children, 0).rect.height).toBe(3); 1433 // Text "after" should be right after it 1434 expect(item(result.children, 1).rect.y).toBe(3); 1435 }); 1436 }); 1437 1438 describe("measureContentHeight", () => { 1439 test("wrapped Text returns the visual line count for the provided width", () => { 1440 // Arrange 1441 const node = Text("foo bar baz", { wrap: "word" }); 1442 1443 // Act 1444 const height = measureContentHeight(node, { width: 6 }); 1445 1446 // Assert 1447 expect(height).toBe(3); 1448 }); 1449 1450 test("padded VStack accounts for its own padding when measuring children", () => { 1451 // Arrange 1452 const node = VStack({ padding: { x: 1, y: 1 }, gap: 1 }, [ 1453 Text("foo bar", { wrap: "word" }), 1454 Text("baz"), 1455 ]); 1456 1457 // Act 1458 const height = measureContentHeight(node, { width: 8 }); 1459 1460 // Assert 1461 expect(height).toBe(6); 1462 }); 1463 1464 test("wrapping HStack returns the sum of row heights plus row gaps", () => { 1465 // Arrange 1466 const node = HStack({ flexWrap: "wrap", gap: 1 }, [ 1467 VStack({ width: 5, height: 2 }, []), 1468 VStack({ width: 5, height: 3 }, []), 1469 VStack({ width: 5, height: 4 }, []), 1470 ]); 1471 1472 // Act 1473 const height = measureContentHeight(node, { width: 11 }); 1474 1475 // Assert 1476 expect(height).toBe(8); 1477 }); 1478 1479 test("TextInput returns wrapped content height including its own padding", () => { 1480 // Arrange 1481 const node = TextInput({ 1482 padding: { x: 1, y: 1 }, 1483 value: "foo bar baz", 1484 onChange: () => {}, 1485 }); 1486 1487 // Act 1488 const height = measureContentHeight(node, { width: 8 }); 1489 1490 // Assert 1491 expect(height).toBe(5); 1492 }); 1493 1494 test("explicit root height does not limit the measured content height", () => { 1495 // Arrange 1496 const node = VStack({ height: 1 }, [ 1497 Text("line 1"), 1498 Text("line 2"), 1499 Text("line 3"), 1500 ]); 1501 1502 // Act 1503 const height = measureContentHeight(node, { width: 10 }); 1504 1505 // Assert 1506 expect(height).toBe(3); 1507 }); 1508 1509 test("caller-provided width overrides the measured root width prop", () => { 1510 // Arrange 1511 const node = VStack({ width: 10 }, [ 1512 Text("foo bar baz", { wrap: "word" }), 1513 ]); 1514 1515 // Act 1516 const height = measureContentHeight(node, { width: 100 }); 1517 1518 // Assert 1519 expect(height).toBe(1); 1520 }); 1521 }); 1522});