superlighttui 0.21.1

Super Light TUI - A lightweight, ergonomic terminal UI library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
#![allow(clippy::print_stderr)]
#![allow(clippy::unwrap_used)]

use super::tree::{default_container_config, ContainerConfig};
use super::*;

#[test]
fn wrap_empty() {
    assert_eq!(wrap_lines("", 10), vec![""]);
}

#[test]
fn wrap_fits() {
    assert_eq!(wrap_lines("hello", 10), vec!["hello"]);
}

#[test]
fn wrap_word_boundary() {
    assert_eq!(wrap_lines("hello world", 7), vec!["hello", "world"]);
}

#[test]
fn wrap_multiple_words() {
    assert_eq!(
        wrap_lines("one two three four", 9),
        vec!["one two", "three", "four"]
    );
}

#[test]
fn wrap_long_word() {
    assert_eq!(wrap_lines("abcdefghij", 4), vec!["abcd", "efgh", "ij"]);
}

#[test]
fn wrap_zero_width() {
    assert_eq!(wrap_lines("hello", 0), vec!["hello"]);
}

#[test]
fn wrap_lines_single_word_exact_fit() {
    // Word width equals max_width: fits on one line.
    assert_eq!(wrap_lines("hello", 5), vec!["hello"]);
}

#[test]
fn wrap_lines_cjk_wide_chars() {
    // Each CJK char has display width 2. At max_width=4 only two chars per line.
    assert_eq!(wrap_lines("日本語文字", 4), vec!["日本", "語文", ""]);
}

#[test]
fn wrap_lines_cjk_with_space() {
    // Break at the space after the CJK chunk when the next word won't fit.
    assert_eq!(wrap_lines("日本 hello", 5), vec!["日本", "hello"]);
}

#[test]
fn wrap_lines_consecutive_spaces_collapse() {
    // Multiple spaces between words collapse to a single separator.
    assert_eq!(wrap_lines("a  b   c", 10), vec!["a b c"]);
}

#[test]
fn wrap_lines_combining_mark_preserved() {
    // Combining marks have width 0 and must stay attached to their base char.
    let input = "e\u{0301}llo";
    assert_eq!(wrap_lines(input, 10), vec![input]);
}

#[test]
fn wrap_lines_single_wide_char_over_width() {
    // A single wide char wider than max_width is emitted on its own line.
    assert_eq!(wrap_lines("", 1), vec![""]);
}

#[test]
fn wrap_lines_only_spaces() {
    // Input of only whitespace produces a single empty output line.
    assert_eq!(wrap_lines("   ", 5), vec![""]);
}

#[test]
fn wrap_lines_hard_break_basic() {
    // Embedded '\n' is a hard break even though "a b" fits within max_width.
    assert_eq!(wrap_lines("a\nb", 10), vec!["a", "b"]);
}

#[test]
fn wrap_lines_hard_break_blank_line() {
    // Consecutive newlines produce a genuine blank line.
    assert_eq!(wrap_lines("a\n\nb", 10), vec!["a", "", "b"]);
}

#[test]
fn wrap_lines_hard_break_leading_newline() {
    assert_eq!(wrap_lines("\nb", 10), vec!["", "b"]);
}

#[test]
fn wrap_lines_hard_break_trailing_newline() {
    // Trailing '\n' opens a fresh empty line — split('\n'), not str::lines().
    assert_eq!(wrap_lines("a\n", 10), vec!["a", ""]);
}

#[test]
fn wrap_lines_hard_break_then_soft_wrap() {
    // Soft wrapping still applies independently within each hard-break paragraph.
    assert_eq!(
        wrap_lines("hello world\nfoo", 7),
        vec!["hello", "world", "foo"]
    );
}

#[test]
fn wrap_lines_hard_break_cjk() {
    assert_eq!(wrap_lines("日本\nhello", 5), vec!["日本", "hello"]);
}

#[test]
fn wrap_lines_crlf_normalized() {
    // "\r\n" collapses to a single break with no stray '\r' in output.
    assert_eq!(wrap_lines("a\r\nb", 10), vec!["a", "b"]);
}

#[test]
fn wrap_lines_no_literal_control_char_in_output() {
    for line in wrap_lines("alpha\nbeta\r\ngamma", 80) {
        assert!(!line.contains('\n') && !line.contains('\r'));
    }
}

#[test]
fn wrap_lines_zero_width_honors_hard_break() {
    // max_width == 0 still splits on '\n' and never emits a literal control char.
    assert_eq!(wrap_lines("a\nb", 0), vec!["a", "b"]);
}

#[test]
fn wrap_segments_empty_returns_single_empty_line() {
    let segs: Vec<(String, Style)> = Vec::new();
    assert_eq!(
        wrap_segments(&segs, 10),
        vec![Vec::<(String, Style)>::new()]
    );
}

#[test]
fn wrap_segments_zero_width_returns_single_empty_line() {
    let segs = vec![("hello".to_string(), Style::new())];
    assert_eq!(wrap_segments(&segs, 0), vec![Vec::<(String, Style)>::new()]);
}

#[test]
fn wrap_segments_single_style_greedy_break() {
    // wrap_segments uses greedy fill + rewind-to-last-space, which differs from
    // wrap_lines. Verify the exact line split here.
    let style = Style::new();
    let segs = vec![("hello world foo bar".to_string(), style)];
    let lines = wrap_segments(&segs, 9);
    let joined: Vec<String> = lines
        .iter()
        .map(|l| l.iter().map(|(t, _)| t.as_str()).collect())
        .collect();
    assert_eq!(joined, vec!["hello", "world", "foo bar"]);
}

#[test]
fn wrap_segments_mixed_styles_preserve_run_boundaries() {
    // Two adjacent runs with different styles should remain separated on the same line.
    let s1 = Style::new();
    let s2 = Style::new().fg(Color::Red);
    let segs = vec![("abc".to_string(), s1), ("def".to_string(), s2)];
    let lines = wrap_segments(&segs, 10);
    assert_eq!(lines.len(), 1);
    assert_eq!(lines[0].len(), 2);
    assert_eq!(lines[0][0].0, "abc");
    assert_eq!(lines[0][1].0, "def");
    assert_eq!(lines[0][0].1, s1);
    assert_eq!(lines[0][1].1, s2);
}

#[test]
fn wrap_segments_trailing_whitespace_trimmed() {
    let s1 = Style::new();
    let s2 = Style::new().fg(Color::Red);
    let segs = vec![("abc ".to_string(), s1), ("   ".to_string(), s2)];
    let lines = wrap_segments(&segs, 20);
    // The entire trailing-whitespace seg is dropped and the first seg is trimmed.
    assert_eq!(lines, vec![vec![("abc".to_string(), s1)]]);
}

#[test]
fn wrap_segments_break_on_space_with_style_change() {
    // A line overflows mid-next-word: rewind to the space and carry the rest forward.
    let s1 = Style::new();
    let s2 = Style::new().fg(Color::Red);
    let segs = vec![("abc ".to_string(), s1), ("defgh".to_string(), s2)];
    let lines = wrap_segments(&segs, 5);
    assert_eq!(lines.len(), 2);
    // Line 1: "abc" (trimmed), line 2: "defgh".
    assert_eq!(lines[0], vec![("abc".to_string(), s1)]);
    assert_eq!(lines[1], vec![("defgh".to_string(), s2)]);
}

#[test]
fn wrap_segments_cjk_with_mixed_styles() {
    let s1 = Style::new();
    let s2 = Style::new().fg(Color::Red);
    let segs = vec![("日本".to_string(), s1), ("".to_string(), s2)];
    let lines = wrap_segments(&segs, 4);
    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0], vec![("日本".to_string(), s1)]);
    assert_eq!(lines[1], vec![("".to_string(), s2)]);
}

#[test]
fn wrap_segments_hard_break_basic() {
    let s = Style::new();
    let segs = vec![("a\nb".to_string(), s)];
    assert_eq!(
        wrap_segments(&segs, 10),
        vec![vec![("a".to_string(), s)], vec![("b".to_string(), s)]]
    );
}

#[test]
fn wrap_segments_hard_break_blank_line() {
    let s = Style::new();
    let segs = vec![("a\n\nb".to_string(), s)];
    assert_eq!(
        wrap_segments(&segs, 10),
        vec![
            vec![("a".to_string(), s)],
            Vec::<(String, Style)>::new(),
            vec![("b".to_string(), s)],
        ]
    );
}

#[test]
fn wrap_segments_hard_break_across_segment_boundary() {
    // A '\n' that ends one styled segment still breaks the line; styles persist.
    let s1 = Style::new();
    let s2 = Style::new().fg(Color::Red);
    let segs = vec![("a\n".to_string(), s1), ("b".to_string(), s2)];
    assert_eq!(
        wrap_segments(&segs, 10),
        vec![vec![("a".to_string(), s1)], vec![("b".to_string(), s2)]]
    );
}

#[test]
fn wrap_segments_hard_break_then_soft_wrap() {
    // Soft word wrap still applies within each hard-break paragraph.
    let s = Style::new();
    let segs = vec![("hello world\nfoo".to_string(), s)];
    let joined: Vec<String> = wrap_segments(&segs, 9)
        .iter()
        .map(|l| l.iter().map(|(t, _)| t.as_str()).collect())
        .collect();
    assert_eq!(joined, vec!["hello", "world", "foo"]);
}

#[test]
fn wrap_segments_crlf_normalized() {
    let s = Style::new();
    let segs = vec![("a\r\nb".to_string(), s)];
    assert_eq!(
        wrap_segments(&segs, 10),
        vec![vec![("a".to_string(), s)], vec![("b".to_string(), s)]]
    );
}

#[test]
fn wrap_segments_no_literal_control_char_in_runs() {
    let s = Style::new();
    let segs = vec![("alpha\nbeta\r\ngamma".to_string(), s)];
    for line in wrap_segments(&segs, 80) {
        for (t, _) in line {
            assert!(!t.contains('\n') && !t.contains('\r'));
        }
    }
}

#[test]
fn diagnostic_demo_layout() {
    use crate::rect::Rect;
    use crate::style::{Align, Border, Constraints, Justify, Margin, Padding, Style};

    let mut root = LayoutNode::container(
        Direction::Column,
        ContainerConfig {
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: None,
            border_sides: BorderSides::all(),
            border_style: Style::new(),
            bg_color: None,
            padding: Padding::default(),
            margin: Margin::default(),
            constraints: Constraints::default(),
            title: None,
            grow: 0,
        },
    );

    let mut outer_container = LayoutNode::container(
        Direction::Column,
        ContainerConfig {
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: Some(Border::Rounded),
            border_sides: BorderSides::all(),
            border_style: Style::new(),
            bg_color: None,
            padding: Padding::all(1),
            margin: Margin::default(),
            constraints: Constraints::default(),
            title: None,
            grow: 1,
        },
    );

    outer_container.children.push(LayoutNode::text(
        "header".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));

    outer_container.children.push(LayoutNode::text(
        "separator".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));

    let mut inner_container = LayoutNode::container(
        Direction::Column,
        ContainerConfig {
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: None,
            border_sides: BorderSides::all(),
            border_style: Style::new(),
            bg_color: None,
            padding: Padding::default(),
            margin: Margin::default(),
            constraints: Constraints::default(),
            title: None,
            grow: 1,
        },
    );

    inner_container.children.push(LayoutNode::text(
        "content1".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));
    inner_container.children.push(LayoutNode::text(
        "content2".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));
    inner_container.children.push(LayoutNode::text(
        "content3".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));

    outer_container.children.push(inner_container);

    outer_container.children.push(LayoutNode::text(
        "separator2".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));

    outer_container.children.push(LayoutNode::text(
        "footer".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));

    root.children.push(outer_container);

    compute(&mut root, Rect::new(0, 0, 80, 50));

    eprintln!("\n=== DIAGNOSTIC LAYOUT TEST ===");
    eprintln!("Root node:");
    eprintln!("  pos: {:?}, size: {:?}", root.pos, root.size);

    let outer = &root.children[0];
    eprintln!("\nOuter bordered container (grow:1):");
    eprintln!("  pos: {:?}, size: {:?}", outer.pos, outer.size);

    let inner = &outer.children[2];
    eprintln!("\nInner container (grow:1, simulates scrollable):");
    eprintln!("  pos: {:?}, size: {:?}", inner.pos, inner.size);

    eprintln!("\nAll children of outer container:");
    for (i, child) in outer.children.iter().enumerate() {
        eprintln!("  [{}] pos: {:?}, size: {:?}", i, child.pos, child.size);
    }

    assert_eq!(root.size, (80, 50));
    assert_eq!(outer.size, (80, 50));

    let expected_inner_height = 50 - 2 - 2 - 4;
    assert_eq!(inner.size.1, expected_inner_height as u32);

    let expected_inner_y = 1 + 1 + 1 + 1;
    assert_eq!(inner.pos.1, expected_inner_y as u32);
}

#[test]
fn collect_focus_rects_from_markers() {
    use Style;

    let mut commands = vec![
        Command::FocusMarker(0),
        Command::Text {
            content: "input1".into(),
            cursor_offset: None,
            style: Style::new(),
            grow: 0,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Default::default(),
            constraints: Default::default(),
        },
        Command::FocusMarker(1),
        Command::Text {
            content: "input2".into(),
            cursor_offset: None,
            style: Style::new(),
            grow: 0,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Default::default(),
            constraints: Default::default(),
        },
    ];

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 40, 10);
    compute(&mut tree, area);

    let mut fd = FrameData::default();
    collect_all(&tree, &mut fd);
    assert_eq!(fd.focus_rects.len(), 2);
    assert_eq!(fd.focus_rects[0].0, 0);
    assert_eq!(fd.focus_rects[1].0, 1);
    assert!(fd.focus_rects[0].1.width > 0);
    assert!(fd.focus_rects[1].1.width > 0);
    assert_ne!(fd.focus_rects[0].1.y, fd.focus_rects[1].1.y);
}

#[test]
fn focus_marker_tags_container() {
    use crate::style::{Border, Style};

    let mut commands = vec![
        Command::FocusMarker(0),
        Command::BeginContainer(Box::new(BeginContainerArgs {
            direction: Direction::Column,
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: Some(Border::Single),
            border_sides: BorderSides::all(),
            border_style: Style::new(),
            bg_color: None,
            padding: Padding::default(),
            margin: Default::default(),
            constraints: Default::default(),
            title: None,
            grow: 0,
            group_name: None,
        })),
        Command::Text {
            content: "inside".into(),
            cursor_offset: None,
            style: Style::new(),
            grow: 0,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Default::default(),
            constraints: Default::default(),
        },
        Command::EndContainer,
    ];

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 40, 10);
    compute(&mut tree, area);

    let mut fd = FrameData::default();
    collect_all(&tree, &mut fd);
    assert_eq!(fd.focus_rects.len(), 1);
    assert_eq!(fd.focus_rects[0].0, 0);
    assert!(fd.focus_rects[0].1.width >= 8);
    assert!(fd.focus_rects[0].1.height >= 3);
}

#[test]
fn wrapped_text_cache_reused_for_same_width() {
    let mut node = LayoutNode::text(
        "alpha beta gamma".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, true, false),
        Margin::default(),
        Constraints::default(),
    );

    let height_a = node.min_height_for_width(6);
    let first_ptr = node
        .text_data()
        .and_then(|t| t.cached_wrapped.as_ref())
        .map(Vec::as_ptr)
        .unwrap();
    let height_b = node.min_height_for_width(6);
    let second_ptr = node
        .text_data()
        .and_then(|t| t.cached_wrapped.as_ref())
        .map(Vec::as_ptr)
        .unwrap();

    assert_eq!(height_a, height_b);
    assert_eq!(first_ptr, second_ptr);
    assert_eq!(node.text_data().and_then(|t| t.cached_wrap_width), Some(6));
}

#[test]
fn collect_all_clips_raw_draw_to_scroll_viewport() {
    // Replaces the prior `collect_all_matches_raw_draw_collection` legacy-vs-
    // current parity test. The legacy `collect_raw_draw_rects` walker (a
    // `#[cfg(test)]` duplicate of the production branch) was removed; this
    // test exercises the production `collect_all` path directly.
    //
    // Setup: scroll container at (0,0) sized 20x4 with `scroll_offset = 2`,
    // containing a 6x3 RawDraw child at layout coordinates (1, 3).
    // Hand trace:
    //   inner viewport (no border/padding) = (0,0)..(20,4)
    //   screen_y     = 3 - 2 = 1            (image top in screen coords)
    //   img_bottom   = 1 + 3 = 4
    //   visible_top  = max(1, 0)     = 1
    //   visible_bot  = min(4, 4)     = 4
    //   visible_h    = 4 - 1         = 3    (entire image fits inside)
    //   top_clip     = max(0 - 1, 0) = 0    (image top sits below vp top)
    //   original_h   = 3                    (drives pixel renderer crop)
    let mut root = LayoutNode::container(Direction::Column, default_container_config());
    let mut scroll = LayoutNode::container(Direction::Column, default_container_config());
    scroll.is_scrollable = true;
    scroll.pos = (0, 0);
    scroll.size = (20, 4);
    scroll.scroll_offset = 2;
    let mut raw = LayoutNode::raw_draw(7, Constraints::default(), 0, Margin::default(), None, None);
    raw.pos = (1, 3);
    raw.size = (6, 3);
    scroll.children.push(raw);
    root.children.push(scroll);

    let mut fd = FrameData::default();
    collect_all(&root, &mut fd);
    let rects: Vec<_> = fd
        .raw_draw_rects
        .into_iter()
        .map(|r| (r.draw_id, r.rect, r.top_clip_rows, r.original_height))
        .collect();

    assert_eq!(
        rects,
        vec![(7, crate::rect::Rect::new(1, 1, 6, 3), 0, 3)],
        "collect_all must clip RawDraw rect into the scroll viewport, \
         leave top_clip_rows = 0 when the image top is already inside \
         the viewport, and report the unclipped original height"
    );
}

#[test]
fn group_names_share_arc_across_focus_descendants() {
    use std::sync::Arc;

    // Build a frame with 50 grouped containers, each holding 3 focusable text
    // children. After the DFS in `collect_all`, every focus slot must carry
    // the group's Arc pointer — and all 3 descendants of the same group must
    // share the *same* allocation (Arc::ptr_eq), proving the reader inherits
    // via a pointer bump rather than allocating a new String per focus hit.
    const N_GROUPS: usize = 50;
    const FOCUSES_PER_GROUP: usize = 3;
    let mut commands: Vec<Command> = Vec::new();
    let mut focus_id = 0usize;
    for i in 0..N_GROUPS {
        commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
            direction: Direction::Column,
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: None,
            border_sides: BorderSides::all(),
            border_style: Style::new(),
            bg_color: None,
            padding: Padding::default(),
            margin: Margin::default(),
            constraints: Constraints::default(),
            title: None,
            grow: 0,
            group_name: Some(Arc::from(format!("group-{i}").as_str())),
        })));
        for _ in 0..FOCUSES_PER_GROUP {
            commands.push(Command::FocusMarker(focus_id));
            commands.push(Command::Text {
                content: format!("row {focus_id}"),
                cursor_offset: None,
                style: Style::new(),
                grow: 0,
                align: Align::Start,
                wrap: false,
                truncate: false,
                margin: Margin::default(),
                constraints: Constraints::default(),
            });
            focus_id += 1;
        }
        commands.push(Command::EndContainer);
    }

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 80, (N_GROUPS * FOCUSES_PER_GROUP) as u32 + 4);
    compute(&mut tree, area);
    let mut fd = FrameData::default();
    collect_all(&tree, &mut fd);

    // All N_GROUPS group rects present with the expected names.
    assert_eq!(
        fd.group_rects.len(),
        N_GROUPS,
        "one group rect per container"
    );
    for (i, (name, _rect)) in fd.group_rects.iter().enumerate() {
        assert_eq!(name.as_ref(), format!("group-{i}"));
    }

    // Every focus slot must carry the correct group, and all focus slots
    // within the same group must share the SAME Arc allocation.
    assert_eq!(
        fd.focus_groups.len(),
        N_GROUPS * FOCUSES_PER_GROUP,
        "one focus slot per focusable"
    );
    for g in 0..N_GROUPS {
        let base = g * FOCUSES_PER_GROUP;
        let first = fd.focus_groups[base]
            .as_ref()
            .unwrap_or_else(|| panic!("focus slot {base} missing group"));
        assert_eq!(first.as_ref(), format!("group-{g}"));
        for k in 1..FOCUSES_PER_GROUP {
            let slot = fd.focus_groups[base + k]
                .as_ref()
                .unwrap_or_else(|| panic!("focus slot {} missing group", base + k));
            assert!(
                Arc::ptr_eq(first, slot),
                "focus slots within group-{g} must share the same Arc allocation"
            );
        }
    }
}

#[test]
fn flexbox_row_many_children_overflow_scratch() {
    // Row with 32 text children (> INLINE_CAP=16) must still lay out correctly,
    // exercising the U32Stack heap-overflow path in layout_row. Semantic equivalence
    // check: positions are strictly increasing and non-overlapping.
    const N: usize = 32;
    let mut commands: Vec<Command> = Vec::new();
    commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
        direction: Direction::Row,
        gap: 0,
        align: Align::Start,
        align_self: None,
        justify: Justify::Start,
        border: None,
        border_sides: BorderSides::all(),
        border_style: Style::new(),
        bg_color: None,
        padding: Padding::default(),
        margin: Margin::default(),
        constraints: Constraints::default(),
        title: None,
        grow: 0,
        group_name: None,
    })));
    for i in 0..N {
        commands.push(Command::Text {
            content: format!("c{i}"),
            cursor_offset: None,
            style: Style::new(),
            grow: 1,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Default::default(),
            constraints: Default::default(),
        });
    }
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    compute(&mut tree, crate::rect::Rect::new(0, 0, 200, 4));

    let row = &tree.children[0];
    assert_eq!(row.children.len(), N);
    // Monotonically increasing x positions, non-overlapping, total width bounded by area.
    let mut prev_end = 0u32;
    for child in &row.children {
        assert!(child.pos.0 >= prev_end, "children must not overlap");
        prev_end = child.pos.0 + child.size.0;
    }
    assert!(prev_end <= 200);
}

#[test]
fn flexbox_column_many_children_overflow_scratch() {
    // Column with 20 fixed-height children exercises layout_column's heap-overflow
    // path in the U32Stack scratch (> INLINE_CAP=16).
    const N: usize = 20;
    let mut commands: Vec<Command> = Vec::new();
    commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
        direction: Direction::Column,
        gap: 0,
        align: Align::Start,
        align_self: None,
        justify: Justify::Start,
        border: None,
        border_sides: BorderSides::all(),
        border_style: Style::new(),
        bg_color: None,
        padding: Padding::default(),
        margin: Margin::default(),
        constraints: Constraints::default(),
        title: None,
        grow: 0,
        group_name: None,
    })));
    for i in 0..N {
        commands.push(Command::Text {
            content: format!("row{i}"),
            cursor_offset: None,
            style: Style::new(),
            grow: 0,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Default::default(),
            constraints: Default::default(),
        });
    }
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    compute(&mut tree, crate::rect::Rect::new(0, 0, 20, 50));

    let col = &tree.children[0];
    assert_eq!(col.children.len(), N);
    let mut prev_end = 0u32;
    for child in &col.children {
        assert!(
            child.pos.1 >= prev_end,
            "children must not overlap vertically"
        );
        prev_end = child.pos.1 + child.size.1;
    }
    assert!(prev_end <= 50);
}

#[test]
fn flexbox_grow_with_max_width_no_gap() {
    // Row: 40px wide, no gap, two grow:1 children.
    // Child 0: grow:1, max_width:10 → rendered 10px, x should advance by 10.
    // Child 1: grow:1, no constraint → starts at x=10, rendered ~30px.
    use crate::style::{Align, Constraints, Justify, Margin, Padding};

    let mut commands = vec![
        Command::BeginContainer(Box::new(BeginContainerArgs {
            direction: Direction::Row,
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: None,
            border_sides: BorderSides::all(),
            border_style: crate::style::Style::new(),
            bg_color: None,
            padding: Padding::default(),
            margin: Margin::default(),
            constraints: Constraints::default(),
            title: None,
            grow: 0,
            group_name: None,
        })),
        Command::Text {
            content: "A".into(),
            cursor_offset: None,
            style: crate::style::Style::new(),
            grow: 1,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Margin::default(),
            constraints: Constraints::default().max_w(10),
        },
        Command::Text {
            content: "B".into(),
            cursor_offset: None,
            style: crate::style::Style::new(),
            grow: 1,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Margin::default(),
            constraints: Constraints::default(),
        },
        Command::EndContainer,
    ];

    let mut tree = build_tree(&mut commands);
    compute(&mut tree, crate::rect::Rect::new(0, 0, 40, 4));

    let row = &tree.children[0];
    assert_eq!(row.children.len(), 2);
    let c0 = &row.children[0];
    let c1 = &row.children[1];

    // Child 0 clamped to max_width=10
    assert_eq!(
        c0.size.0, 10,
        "child 0 width should be clamped to max_width=10"
    );
    // Child 1 must start immediately after child 0 (no gap)
    assert_eq!(
        c1.pos.0,
        c0.pos.0 + c0.size.0,
        "child 1 x must equal child 0 x + child 0 width (no gap)"
    );
}

#[test]
fn flexbox_column_grow_with_max_height_no_gap() {
    // Column: 20px tall, no gap, two grow:1 children.
    // Child 0: grow:1, max_height:5 → rendered 5px, y should advance by 5.
    // Child 1: grow:1, no constraint → starts at y=5.
    //
    // The outer column itself uses grow:1 so the implicit root column built by
    // `build_tree` stretches it to the full 20px area; otherwise the outer
    // column shrinks to its min_height and there is no flex space for c0/c1
    // to grow into.
    use crate::style::{Align, Constraints, Justify, Margin, Padding};

    let mut commands = vec![
        Command::BeginContainer(Box::new(BeginContainerArgs {
            direction: Direction::Column,
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: None,
            border_sides: BorderSides::all(),
            border_style: crate::style::Style::new(),
            bg_color: None,
            padding: Padding::default(),
            margin: Margin::default(),
            constraints: Constraints::default(),
            title: None,
            grow: 1,
            group_name: None,
        })),
        Command::Text {
            content: "A".into(),
            cursor_offset: None,
            style: crate::style::Style::new(),
            grow: 1,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Margin::default(),
            constraints: Constraints::default().max_h(5),
        },
        Command::Text {
            content: "B".into(),
            cursor_offset: None,
            style: crate::style::Style::new(),
            grow: 1,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Margin::default(),
            constraints: Constraints::default(),
        },
        Command::EndContainer,
    ];

    let mut tree = build_tree(&mut commands);
    compute(&mut tree, crate::rect::Rect::new(0, 0, 20, 20));

    let col = &tree.children[0];
    assert_eq!(col.children.len(), 2);
    let c0 = &col.children[0];
    let c1 = &col.children[1];

    // Child 0 clamped to max_height=5
    assert_eq!(
        c0.size.1, 5,
        "child 0 height should be clamped to max_height=5"
    );
    // Child 1 must start immediately after child 0
    assert_eq!(
        c1.pos.1,
        c0.pos.1 + c0.size.1,
        "child 1 y must equal child 0 y + child 0 height (no gap)"
    );
}

// --- Regression tests for v0.19.1 hotfix ---------------------------------

#[test]
fn collect_all_keeps_scroll_invariant_with_nested_scrollables() {
    // Regression for issue #151: `collect_all_inner` historically split the
    // `scroll_infos` push and the `scroll_rects` push into two separate
    // `if node.is_scrollable` branches, making it possible to update one
    // side without the other and silently break the
    // `scroll_infos.len() == scroll_rects.len()` invariant enforced by the
    // outer pipeline. Merging the branches makes drift impossible; this test
    // pins the invariant so a future re-split fails loudly.
    //
    // Build a tree with two nested scrollable containers + one sibling
    // scrollable (3 scrollable nodes total, all visible) and verify both
    // vectors agree in length.
    let mut root = LayoutNode::container(Direction::Column, default_container_config());

    let mut outer = LayoutNode::container(Direction::Column, default_container_config());
    outer.is_scrollable = true;
    outer.pos = (0, 0);
    outer.size = (40, 20);

    let mut inner = LayoutNode::container(Direction::Column, default_container_config());
    inner.is_scrollable = true;
    inner.pos = (1, 1);
    inner.size = (38, 18);
    outer.children.push(inner);

    let mut sibling = LayoutNode::container(Direction::Column, default_container_config());
    sibling.is_scrollable = true;
    sibling.pos = (0, 25);
    sibling.size = (40, 10);

    root.children.push(outer);
    root.children.push(sibling);

    let mut fd = FrameData::default();
    collect_all(&root, &mut fd);

    assert_eq!(
        fd.scroll_infos.len(),
        fd.scroll_rects.len(),
        "scroll_infos and scroll_rects must always have equal length \
         (collect_all_inner branch merge invariant)"
    );
    assert_eq!(
        fd.scroll_infos.len(),
        3,
        "expected 3 scrollable nodes (outer, inner, sibling)"
    );
}

#[test]
fn raw_draw_constructor_matches_inline_literal_shape() {
    // Regression for issue #156: `LayoutNode::raw_draw` must produce a node
    // identical to the prior inline 34-field literal in `build_children`.
    // This pins every field that the constructor sets so a future drift in
    // the constructor or the build_children call site is caught immediately.
    let constraints = Constraints::default().min_w(7).min_h(2);
    let margin = Margin {
        top: 1,
        right: 2,
        bottom: 3,
        left: 4,
    };
    let node = LayoutNode::raw_draw(42, constraints, 5, margin, Some(11), Some(13));

    assert!(matches!(node.kind, NodeKind::RawDraw(42)));
    assert_eq!(node.grow, 5);
    assert_eq!(node.margin.top, 1);
    assert_eq!(node.margin.right, 2);
    assert_eq!(node.margin.bottom, 3);
    assert_eq!(node.margin.left, 4);
    assert_eq!(node.constraints.min_width(), Some(7));
    assert_eq!(node.constraints.min_height(), Some(2));
    assert_eq!(node.size, (7, 2), "size must seed from constraints minima");
    assert_eq!(node.pos, (0, 0));
    assert_eq!(node.focus_id, Some(11));
    assert_eq!(node.interaction_id, Some(13));
    // Defaults — none of these should be populated by the constructor.
    // Non-text nodes have no `text_data`; the text-only fields are
    // unreachable from this variant (issue #153 split them off).
    assert!(node.text_data().is_none());
    assert_eq!(node.align, Align::Start);
    assert!(node.align_self.is_none());
    assert_eq!(node.justify, Justify::Start);
    assert!(!node.wrap);
    assert!(!node.truncate);
    assert_eq!(node.gap, 0);
    assert!(node.border.is_none());
    assert!(node.bg_color.is_none());
    assert!(node.title.is_none());
    assert!(node.children.is_empty());
    assert!(!node.is_scrollable);
    assert_eq!(node.scroll_offset, 0);
    assert_eq!(node.content_height, 0);
    // The wrap caches and segments are inside `text_data`, which is
    // `None` for `RawDraw` nodes.
    assert!(node.link_url.is_none());
    assert!(node.group_name.is_none());
    assert!(node.overlays.is_empty());
}

/// Build a `LayoutNode` chain of the given depth without going through
/// `build_children` (which has its own depth guard at construction time).
/// Used to exercise the depth guards in `compute` / `collect_all_inner` /
/// `render_inner` directly.
fn build_deep_node(depth: usize) -> LayoutNode {
    let mut root = LayoutNode::container(Direction::Column, default_container_config());
    let mut cursor = &mut root;
    for _ in 0..depth {
        cursor.children.push(LayoutNode::container(
            Direction::Column,
            default_container_config(),
        ));
        cursor = cursor.children.last_mut().unwrap();
    }
    root
}

#[test]
#[should_panic(expected = "layout tree depth exceeds 512")]
fn compute_panics_at_depth_guard() {
    // Regression for issue #154: `compute` must panic with the documented
    // diagnostic message when recursion depth exceeds `MAX_LAYOUT_DEPTH`.
    // build_deep_node(514) yields a chain of 515 nested containers (root +
    // 514 children); the inner-most container is reached at depth 514,
    // which is past the limit of 512.
    let mut node = build_deep_node(514);
    compute(&mut node, crate::rect::Rect::new(0, 0, 80, 24));
}

#[test]
#[should_panic(expected = "layout tree depth exceeds 512")]
fn collect_all_panics_at_depth_guard() {
    // Regression for issue #154: `collect_all_inner` must panic with the
    // documented diagnostic message when recursion depth exceeds
    // `MAX_LAYOUT_DEPTH`. Sizes are populated directly (bypassing `compute`,
    // which would also panic) so the DFS reaches the inner-most depth.
    let mut node = build_deep_node(514);
    fn populate_sizes(n: &mut LayoutNode) {
        n.size = (10, 10);
        for c in &mut n.children {
            populate_sizes(c);
        }
    }
    populate_sizes(&mut node);
    let mut fd = FrameData::default();
    collect_all(&node, &mut fd);
}

#[test]
#[should_panic(expected = "layout tree depth exceeds 512")]
fn render_panics_at_depth_guard() {
    // Regression for issue #154: `render_inner` must panic with the
    // documented diagnostic message when recursion depth exceeds
    // `MAX_LAYOUT_DEPTH`. Sizes are populated directly so the renderer does
    // not short-circuit on the size-zero check before reaching the guard.
    let mut node = build_deep_node(514);
    fn populate_sizes(n: &mut LayoutNode) {
        n.size = (10, 10);
        n.pos = (0, 0);
        for c in &mut n.children {
            populate_sizes(c);
        }
    }
    populate_sizes(&mut node);
    let mut buf = crate::buffer::Buffer::empty(crate::rect::Rect::new(0, 0, 80, 24));
    super::render::render(&node, &mut buf);
}

#[test]
fn collect_all_reuses_buffer_without_leaking_prior_frame_data() {
    // Regression for issue #155: `collect_all(&tree, &mut fd)` must call
    // `fd.clear()` before populating, so a recycled `FrameData` does not
    // carry data from a previous frame's tree. The capacity is reused; the
    // contents are not.
    use crate::style::{Constraints, Margin};

    // Frame A: tree with two focusables.
    let mut tree_a = LayoutNode::container(Direction::Column, default_container_config());
    let mut focus_a0 = LayoutNode::container(Direction::Column, default_container_config());
    focus_a0.focus_id = Some(0);
    focus_a0.pos = (0, 0);
    focus_a0.size = (10, 1);
    tree_a.children.push(focus_a0);
    let mut focus_a1 = LayoutNode::container(Direction::Column, default_container_config());
    focus_a1.focus_id = Some(1);
    focus_a1.pos = (0, 1);
    focus_a1.size = (10, 1);
    tree_a.children.push(focus_a1);

    let mut fd = FrameData::default();
    collect_all(&tree_a, &mut fd);
    assert_eq!(fd.focus_rects.len(), 2, "frame A: two focus rects");

    // Frame B: a different tree with a single raw_draw (no focuses, no
    // groups). After collect_all, the recycled `fd` must reflect *only*
    // tree B — not a mix of A and B.
    let mut tree_b = LayoutNode::container(Direction::Column, default_container_config());
    let mut raw =
        LayoutNode::raw_draw(42, Constraints::default(), 0, Margin::default(), None, None);
    raw.pos = (0, 0);
    raw.size = (4, 2);
    tree_b.children.push(raw);

    collect_all(&tree_b, &mut fd);
    assert_eq!(
        fd.focus_rects.len(),
        0,
        "frame B: focus_rects must be cleared before refill"
    );
    assert_eq!(
        fd.raw_draw_rects.len(),
        1,
        "frame B: one raw_draw rect must be present"
    );
    assert_eq!(fd.raw_draw_rects[0].draw_id, 42);
}

#[test]
fn f12_debug_overlay_outlines_overlay_layer() {
    // Regression for issue #201 Part A: `render_debug_overlay` previously
    // walked only `node.children`, leaving any active overlay/modal invisible
    // to the F12 outline pass. Build a root with one base child + one
    // overlay child, and verify the overlay's bounds receive border chars.

    let mut root = LayoutNode::container(Direction::Column, default_container_config());

    // Base layer: a container at (0,0) sized 40×5.
    let mut base = LayoutNode::container(Direction::Column, default_container_config());
    base.pos = (0, 0);
    base.size = (40, 5);
    root.children.push(base);

    // Overlay layer: a container at (10,10) sized 20×4.
    let mut overlay_node = LayoutNode::container(Direction::Column, default_container_config());
    overlay_node.pos = (10, 10);
    overlay_node.size = (20, 4);
    root.overlays.push(super::tree::OverlayLayer {
        node: overlay_node,
        modal: false,
    });

    let mut buf = crate::buffer::Buffer::empty(crate::rect::Rect::new(0, 0, 40, 20));
    super::render::render_debug_overlay(&root, &mut buf, 0, 60.0, crate::DebugLayer::All);

    // The overlay container's top-RIGHT corner should now carry the outline
    // corner char ('┐'). The top-left position is overwritten by the depth
    // label '0', so we sample the right corner instead. Pre-fix the entire
    // overlay rect was untouched.
    let cell_top_right = buf.get(10 + 20 - 1, 10);
    assert_eq!(
        cell_top_right.symbol, "",
        "F12 overlay outline must hit overlay's top-right corner; got {:?}",
        cell_top_right.symbol
    );
    // Bottom-right corner of overlay.
    let cell_bottom_right = buf.get(10 + 20 - 1, 10 + 4 - 1);
    assert_eq!(
        cell_bottom_right.symbol, "",
        "F12 overlay outline must hit overlay's bottom-right corner; got {:?}",
        cell_bottom_right.symbol
    );
}

#[test]
fn count_leaf_widgets_matches_outline_count_with_overlays() {
    // Regression for issue #201 Part C: the status-bar widget count must
    // include overlay nodes so the displayed total reflects what the renderer
    // actually drew.
    let mut root = LayoutNode::container(Direction::Column, default_container_config());

    // 2 base widgets.
    root.children.push(LayoutNode::text(
        "a".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));
    root.children.push(LayoutNode::text(
        "b".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));

    // 1 overlay widget.
    let mut overlay_root = LayoutNode::container(Direction::Column, default_container_config());
    overlay_root.children.push(LayoutNode::text(
        "overlay".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    ));
    root.overlays.push(super::tree::OverlayLayer {
        node: overlay_root,
        modal: false,
    });

    // Render the debug status bar at the bottom. The widget count is the
    // first integer following "| ".
    let mut buf = crate::buffer::Buffer::empty(crate::rect::Rect::new(0, 0, 80, 5));
    super::render::render_debug_overlay(&root, &mut buf, 0, 60.0, crate::DebugLayer::All);

    let mut bottom = String::new();
    for x in 0..80 {
        bottom.push_str(&buf.get(x, 4).symbol);
    }
    // Expected: 2 base widgets + 1 overlay widget = 3 total.
    assert!(
        bottom.contains("3 widgets"),
        "status bar widget count must include overlay nodes; got {bottom:?}"
    );
    // Per-layer breakdown is appended in parens whenever more than one
    // layer family is non-empty (matches the doc update in DEBUGGING.md).
    assert!(
        bottom.contains("2 base") && bottom.contains("1 overlay"),
        "status bar must include per-layer breakdown when multiple layers \
         are populated; got {bottom:?}"
    );
}

#[test]
fn f12_debug_overlay_distinguishes_layers_by_color() {
    // Regression for the #201 follow-up: each layer family (base / overlay /
    // modal) must paint its outlines in a distinct hue so the F12 view stays
    // legible when several layers are stacked. We sample a known cell on
    // each layer's border ring and check that the foreground colors differ
    // meaningfully (different `Color::Rgb` channels — testing equality is
    // sufficient because `debug_color_for_depth` returns concrete RGBs).
    let mut root = LayoutNode::container(Direction::Column, default_container_config());

    // Base container: (2, 2) sized 10x4. Bottom-right corner at (11, 5).
    let mut base = LayoutNode::container(Direction::Column, default_container_config());
    base.pos = (2, 2);
    base.size = (10, 4);
    root.children.push(base);

    // Non-modal overlay: (15, 2) sized 10x4. Bottom-right corner at (24, 5).
    let mut overlay_node = LayoutNode::container(Direction::Column, default_container_config());
    overlay_node.pos = (15, 2);
    overlay_node.size = (10, 4);
    root.overlays.push(super::tree::OverlayLayer {
        node: overlay_node,
        modal: false,
    });

    // Modal overlay: (28, 2) sized 10x4. Bottom-right corner at (37, 5).
    let mut modal_node = LayoutNode::container(Direction::Column, default_container_config());
    modal_node.pos = (28, 2);
    modal_node.size = (10, 4);
    root.overlays.push(super::tree::OverlayLayer {
        node: modal_node,
        modal: true,
    });

    let mut buf = crate::buffer::Buffer::empty(crate::rect::Rect::new(0, 0, 60, 8));
    super::render::render_debug_overlay(&root, &mut buf, 0, 60.0, crate::DebugLayer::All);

    // Sample the bottom-right corner '┘' of each container — that cell is
    // never overwritten by the depth label (which sits at the top-left).
    let base_fg = buf.get(11, 5).style.fg;
    let overlay_fg = buf.get(24, 5).style.fg;
    let modal_fg = buf.get(37, 5).style.fg;

    assert!(
        base_fg.is_some() && overlay_fg.is_some() && modal_fg.is_some(),
        "all three layer outlines must carry a foreground color; \
         got base={base_fg:?} overlay={overlay_fg:?} modal={modal_fg:?}"
    );
    assert_ne!(
        base_fg, overlay_fg,
        "base and overlay outlines must be different colors"
    );
    assert_ne!(
        base_fg, modal_fg,
        "base and modal outlines must be different colors"
    );
    assert_ne!(
        overlay_fg, modal_fg,
        "overlay and modal outlines must be different colors"
    );

    // Sanity-check the hue families: green-dominant for Base, red-dominant
    // for Overlay, blue-dominant for Modal. Catches future palette drift
    // that keeps colors distinct but loses the convention.
    if let Some(crate::style::Color::Rgb(r, g, b)) = base_fg {
        assert!(
            g > r && g > b,
            "Base outline should be green-dominant; got rgb({r},{g},{b})"
        );
    } else {
        panic!("Base outline must be Color::Rgb; got {base_fg:?}");
    }
    if let Some(crate::style::Color::Rgb(r, g, b)) = overlay_fg {
        assert!(
            r > g && r > b,
            "Overlay outline should be red-dominant; got rgb({r},{g},{b})"
        );
    } else {
        panic!("Overlay outline must be Color::Rgb; got {overlay_fg:?}");
    }
    if let Some(crate::style::Color::Rgb(r, g, b)) = modal_fg {
        assert!(
            b > r && b > g,
            "Modal outline should be blue-dominant; got rgb({r},{g},{b})"
        );
    } else {
        panic!("Modal outline must be Color::Rgb; got {modal_fg:?}");
    }
}

#[test]
fn build_tree_drains_in_place_preserving_capacity() {
    // Regression for issue #150: `build_tree` must drain its input Vec via
    // `&mut Vec<Command>` rather than consume by value, so the caller can
    // recycle the allocation across frames. Pre-fix the function took
    // `commands: Vec<Command>` and `into_iter()`d, dropping the buffer at
    // the end of each frame.
    let initial_cap = 64;
    let mut commands: Vec<Command> = Vec::with_capacity(initial_cap);
    commands.push(Command::Text {
        content: "hello".into(),
        cursor_offset: None,
        style: Style::new(),
        grow: 0,
        align: Align::Start,
        wrap: false,
        truncate: false,
        margin: Default::default(),
        constraints: Default::default(),
    });
    commands.push(Command::Spacer { grow: 1 });

    let cap_before = commands.capacity();
    let _tree = build_tree(&mut commands);

    assert_eq!(
        commands.len(),
        0,
        "build_tree must drain the Vec to len = 0"
    );
    assert!(
        commands.capacity() >= cap_before,
        "build_tree must preserve at least the prior capacity (was {cap_before}, now {})",
        commands.capacity()
    );

    // Second pass: same Vec, more commands. Capacity that already covers
    // the new push count must not trigger a reallocation. This is the
    // contract the frame loop relies on for steady-state zero-alloc.
    let cap_after_first = commands.capacity();
    for _ in 0..cap_after_first {
        commands.push(Command::Spacer { grow: 0 });
    }
    let cap_after_pushes = commands.capacity();
    assert_eq!(
        cap_after_first, cap_after_pushes,
        "pushing within the prior capacity must not realloc \
         (was {cap_after_first}, now {cap_after_pushes})"
    );
    let _tree2 = build_tree(&mut commands);
    assert_eq!(commands.len(), 0, "second drain must also leave len = 0");
}

// =====================================================================
// #161 — opt-in proportional flex-shrink
// =====================================================================
//
// Three regression tests cover the spec checklist from #161:
//
//   (a) no shrink   — output identical to pre-#161 (overflow-by-design).
//   (b) all shrink  — every fixed child scales by `available / fixed_total`.
//   (c) mixed       — only flagged children scale; the rest keep their size.

/// Helper: push a Column container holding a single 20-char text.
fn push_textcol_20(commands: &mut Vec<Command>, shrink: bool) {
    if shrink {
        commands.push(Command::ShrinkMarker);
    }
    commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
        direction: Direction::Column,
        gap: 0,
        align: Align::Start,
        align_self: None,
        justify: Justify::Start,
        border: None,
        border_sides: BorderSides::all(),
        border_style: Style::new(),
        bg_color: None,
        padding: Padding::default(),
        margin: Margin::default(),
        constraints: Constraints::default(),
        title: None,
        grow: 0,
        group_name: None,
    })));
    commands.push(Command::Text {
        content: "x".repeat(20),
        cursor_offset: None,
        style: Style::new(),
        grow: 0,
        align: Align::Start,
        wrap: false,
        truncate: false,
        margin: Default::default(),
        constraints: Default::default(),
    });
    commands.push(Command::EndContainer);
}

fn open_row(commands: &mut Vec<Command>) {
    commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
        direction: Direction::Row,
        gap: 0,
        align: Align::Start,
        align_self: None,
        justify: Justify::Start,
        border: None,
        border_sides: BorderSides::all(),
        border_style: Style::new(),
        bg_color: None,
        padding: Padding::default(),
        margin: Margin::default(),
        constraints: Constraints::default(),
        title: None,
        grow: 0,
        group_name: None,
    })));
}

#[test]
fn flex_shrink_default_off_preserves_overflow() {
    let mut commands: Vec<Command> = Vec::new();
    open_row(&mut commands);
    push_textcol_20(&mut commands, false);
    push_textcol_20(&mut commands, false);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 4);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert_eq!(row.children.len(), 2);
    assert!(!row.children[0].shrink);
    assert!(!row.children[1].shrink);
    assert_eq!(row.children[0].size.0, 20);
    assert_eq!(row.children[1].size.0, 20);
}

#[test]
fn flex_shrink_all_children_proportional_distribution() {
    let mut commands: Vec<Command> = Vec::new();
    open_row(&mut commands);
    push_textcol_20(&mut commands, true);
    push_textcol_20(&mut commands, true);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 4);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert_eq!(row.children.len(), 2);
    assert!(row.children[0].shrink);
    assert!(row.children[1].shrink);
    assert_eq!(row.children[0].size.0, 15);
    assert_eq!(row.children[1].size.0, 15);
}

#[test]
fn flex_shrink_mixed_only_flagged_scale() {
    let mut commands: Vec<Command> = Vec::new();
    open_row(&mut commands);
    push_textcol_20(&mut commands, true);
    push_textcol_20(&mut commands, false);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 4);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert_eq!(row.children.len(), 2);
    assert!(row.children[0].shrink);
    assert!(!row.children[1].shrink);
    assert_eq!(row.children[0].size.0, 10);
    assert_eq!(row.children[1].size.0, 20);
}

// =====================================================================
// #258 — flex-wrap (multi-line row) + flex-basis
// =====================================================================
//
// Tree-level tests mirroring the #161 flex_shrink_* style: build a command
// stream with the relevant marker, `build_tree`, `compute` against a fixed
// `Rect`, and assert child `.pos` / `.size`.

/// Push a Column container holding a single `width`-char text. When `basis`
/// is `Some`, a `BasisMarker` precedes the container; when `shrink`, a
/// `ShrinkMarker` precedes it.
fn push_textcol(commands: &mut Vec<Command>, width: usize, shrink: bool, basis: Option<u32>) {
    if shrink {
        commands.push(Command::ShrinkMarker);
    }
    if let Some(b) = basis {
        commands.push(Command::BasisMarker(b));
    }
    commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
        direction: Direction::Column,
        gap: 0,
        align: Align::Start,
        align_self: None,
        justify: Justify::Start,
        border: None,
        border_sides: BorderSides::all(),
        border_style: Style::new(),
        bg_color: None,
        padding: Padding::default(),
        margin: Margin::default(),
        constraints: Constraints::default(),
        title: None,
        grow: 0,
        group_name: None,
    })));
    commands.push(Command::Text {
        content: "x".repeat(width),
        cursor_offset: None,
        style: Style::new(),
        grow: 0,
        align: Align::Start,
        wrap: false,
        truncate: false,
        margin: Default::default(),
        constraints: Default::default(),
    });
    commands.push(Command::EndContainer);
}

/// Open a row, optionally wrapping (with the given cross-axis gap) and/or
/// with a within-line gap. `wrap` pushes a `WrapMarker(cross_gap)` first.
fn open_row_cfg(commands: &mut Vec<Command>, gap: i32, wrap: Option<i32>) {
    if let Some(cross_gap) = wrap {
        commands.push(Command::WrapMarker(cross_gap));
    }
    commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
        direction: Direction::Row,
        gap,
        align: Align::Start,
        align_self: None,
        justify: Justify::Start,
        border: None,
        border_sides: BorderSides::all(),
        border_style: Style::new(),
        bg_color: None,
        padding: Padding::default(),
        margin: Margin::default(),
        constraints: Constraints::default(),
        title: None,
        grow: 0,
        group_name: None,
    })));
}

#[test]
fn flex_wrap_two_lines_on_overflow() {
    // Three 14-wide children in a 30-wide wrapping row (gap 0): two fit on
    // line 0 (14 + 14 = 28 <= 30), the third overflows to line 1.
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, Some(0));
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 8);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert!(row.wrap_children);
    assert_eq!(row.children.len(), 3);
    // Line 0: children 0 and 1 share the top line.
    assert_eq!(row.children[0].pos.1, area.y);
    assert_eq!(row.children[1].pos.1, area.y);
    assert_eq!(row.children[0].pos.0, area.x);
    assert_eq!(row.children[1].pos.0, area.x + 14);
    // Line 1: the overflowing child wraps down and its x resets to area.x.
    assert_eq!(row.children[2].pos.1, area.y + 1);
    assert_eq!(row.children[2].pos.0, area.x);
}

#[test]
fn flex_wrap_off_is_single_line() {
    // Same children, no WrapMarker: all on the top line, overflow preserved.
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, None);
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 8);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert!(!row.wrap_children);
    for child in &row.children {
        assert_eq!(child.pos.1, area.y);
    }
    // Overflow-by-design: monotonic x advance, third child past the right edge.
    assert_eq!(row.children[0].pos.0, area.x);
    assert_eq!(row.children[1].pos.0, area.x + 14);
    assert_eq!(row.children[2].pos.0, area.x + 28);
}

#[test]
fn flex_wrap_row_gap_between_lines() {
    // Cross-axis gap of 2 between lines. Each line is 1 cell tall, so line 1
    // lands at area.y + line0_height (1) + cross_gap (2) = area.y + 3.
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, Some(2));
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 12);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert_eq!(row.cross_gap, 2);
    assert_eq!(row.children[0].pos.1, area.y);
    assert_eq!(row.children[1].pos.1, area.y);
    assert_eq!(row.children[2].pos.1, area.y + 3);
}

#[test]
fn flex_wrap_oversize_child_own_line() {
    // One child wider than the full area occupies its own line, then a normal
    // child follows on the next line — no empty line, no panic.
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, Some(0));
    push_textcol(&mut commands, 50, false, None); // wider than the 30-wide row
    push_textcol(&mut commands, 10, false, None);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 8);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert_eq!(row.children.len(), 2);
    // Oversize child on line 0 (clipped to area width), normal child on line 1.
    assert_eq!(row.children[0].pos.1, area.y);
    assert_eq!(row.children[1].pos.1, area.y + 1);
    assert_eq!(row.children[1].pos.0, area.x);
}

#[test]
fn flex_wrap_min_height_reserves_lines() {
    // A wrapping row inside the (implicit) root column must report a multi-line
    // height so the parent reserves enough rows. Three 14-wide children in a
    // 30-wide row wrap to 2 lines, each 1 cell tall → row height == 2.
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, Some(0));
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    push_textcol(&mut commands, 14, false, None);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 12);
    compute(&mut tree, area);

    let row = &mut tree.children[0];
    // min_height_for_width drives the parent column's reservation.
    assert_eq!(row.min_height_for_width(30), 2);
}

#[test]
fn flex_basis_feeds_grow() {
    // Two children with basis(10) and grow(1) each in a 40-wide row.
    // free = 40 - (10 + 10) = 20, split 10/10 → each grows to 10.
    // (Grow children resolve to their share of flex_space; basis sets the
    // base subtracted from available, so flex_space = 40 - 20 = 20.)
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, None);
    // Use grow on the wrapper containers — push them manually with basis+grow.
    for _ in 0..2 {
        commands.push(Command::BasisMarker(10));
        commands.push(Command::BeginContainer(Box::new(BeginContainerArgs {
            direction: Direction::Column,
            gap: 0,
            align: Align::Start,
            align_self: None,
            justify: Justify::Start,
            border: None,
            border_sides: BorderSides::all(),
            border_style: Style::new(),
            bg_color: None,
            padding: Padding::default(),
            margin: Margin::default(),
            constraints: Constraints::default(),
            title: None,
            grow: 1,
            group_name: None,
        })));
        commands.push(Command::Text {
            content: "x".to_string(),
            cursor_offset: None,
            style: Style::new(),
            grow: 0,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Default::default(),
            constraints: Default::default(),
        });
        commands.push(Command::EndContainer);
    }
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 40, 4);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert_eq!(row.children[0].flex_basis(), Some(10));
    assert_eq!(row.children[1].flex_basis(), Some(10));
    // Grow children split the full available width 40 → 20 each.
    assert_eq!(row.children[0].size.0, 20);
    assert_eq!(row.children[1].size.0, 20);
}

#[test]
fn flex_basis_feeds_shrink() {
    // Two children, basis(20), both shrink, in a 30-wide row. The narrow text
    // (5 chars) makes min_width 5, but basis(20) replaces it as the shrink
    // base: fixed = 40, scale = min(30,40)/40 = 0.75, each → floor(20*0.75) = 15.
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, None);
    push_textcol(&mut commands, 5, true, Some(20));
    push_textcol(&mut commands, 5, true, Some(20));
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 30, 4);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert!(row.children[0].shrink);
    assert_eq!(row.children[0].flex_basis(), Some(20));
    assert_eq!(row.children[0].size.0, 15);
    assert_eq!(row.children[1].size.0, 15);
}

#[test]
fn flex_basis_none_falls_back_to_min_width() {
    // No BasisMarker: base size is the child's min_width (20), identical to
    // the pre-#258 path.
    let mut commands: Vec<Command> = Vec::new();
    open_row_cfg(&mut commands, 0, None);
    push_textcol(&mut commands, 20, false, None);
    push_textcol(&mut commands, 20, false, None);
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    let area = crate::rect::Rect::new(0, 0, 60, 4);
    compute(&mut tree, area);

    let row = &tree.children[0];
    assert_eq!(row.children[0].flex_basis(), None);
    assert_eq!(row.children[0].size.0, 20);
    assert_eq!(row.children[1].size.0, 20);
}

// ---------------------------------------------------------------------------
// Grapheme-cluster segmentation (issue #259)
//
// A break / truncate must never fall inside an extended grapheme cluster.
// Helpers below re-segment the output and assert no output cluster boundary
// was introduced that wasn't already an input cluster boundary.
// ---------------------------------------------------------------------------

/// A regional indicator (U+1F1E6..=U+1F1FF) on its own — i.e. a flag emoji that
/// was split in half. Its presence in a wrapped line is the failure signature.
fn contains_lone_regional_indicator(s: &str) -> bool {
    use unicode_segmentation::UnicodeSegmentation;
    s.graphemes(true).any(|g| {
        let mut chars = g.chars();
        match (chars.next(), chars.next()) {
            // A single regional indicator with no paired partner: a half flag.
            (Some(c), None) => ('\u{1F1E6}'..='\u{1F1FF}').contains(&c),
            _ => false,
        }
    })
}

#[test]
fn wrap_lines_zwj_flag_not_split() {
    // 🇰🇷 and 🇯🇵 are each two regional-indicator scalars forming one cluster.
    // At max_width=2 (one flag is width 2) each flag must stay whole.
    let lines = wrap_lines("🇰🇷🇯🇵", 2);
    for line in &lines {
        assert!(
            !contains_lone_regional_indicator(line),
            "flag split mid-cluster: {line:?}"
        );
    }
    // Re-joined output preserves every flag.
    let joined: String = lines.join("");
    assert!(joined.contains("🇰🇷"));
    assert!(joined.contains("🇯🇵"));
}

#[test]
fn wrap_lines_family_emoji_at_boundary() {
    // Family emoji: 👨‍👩‍👧‍👦 is one cluster joined by ZWJ. Put a width-2 char
    // before it so the family base lands at/over the boundary; the whole
    // cluster must stay intact on one line (no orphaned ZWJ tail).
    let input = "日👨\u{200D}👩\u{200D}👧\u{200D}👦";
    let lines = wrap_lines(input, 2);
    // The family cluster appears whole on exactly one output line.
    let whole = "👨\u{200D}👩\u{200D}👧\u{200D}👦";
    assert!(
        lines.iter().any(|l| l.contains(whole)),
        "family emoji was broken across lines: {lines:?}"
    );
    // No output line carries a dangling ZWJ at an edge of the cluster.
    for line in &lines {
        // A ZWJ must always be flanked by its joined glyphs — re-segmenting
        // must not yield a fragment that starts or ends with a bare ZWJ.
        assert!(
            !line.starts_with('\u{200D}') && !line.ends_with('\u{200D}'),
            "dangling ZWJ in {line:?}"
        );
    }
}

#[test]
fn wrap_lines_devanagari_cluster() {
    // "क्षि" is base + virama + consonant + vowel sign: one (or few) clusters.
    // It must not be broken between a base and its combining marks.
    use unicode_segmentation::UnicodeSegmentation;
    let input = "क्षि";
    let lines = wrap_lines(input, 1);
    let input_clusters: Vec<&str> = input.graphemes(true).collect();
    // Every output line is a whole sequence of input clusters.
    for line in &lines {
        for g in line.graphemes(true) {
            assert!(
                input_clusters.contains(&g),
                "devanagari cluster fragmented: {g:?} not an input cluster"
            );
        }
    }
}

#[test]
fn wrap_lines_thai_cluster() {
    // "กำ" is a Thai consonant + sara am (one cluster). Must stay intact.
    use unicode_segmentation::UnicodeSegmentation;
    let input = "กำ";
    let lines = wrap_lines(input, 1);
    let input_clusters: Vec<&str> = input.graphemes(true).collect();
    for line in &lines {
        for g in line.graphemes(true) {
            assert!(
                input_clusters.contains(&g),
                "thai cluster fragmented: {g:?}"
            );
        }
    }
}

#[test]
fn split_long_word_keeps_clusters() {
    // An over-wide word of concatenated flags chunks only between flags.
    let lines = wrap_lines("🇰🇷🇯🇵🇺🇸", 2);
    for line in &lines {
        assert!(
            !contains_lone_regional_indicator(line),
            "long-word chunk split a flag: {line:?}"
        );
    }
    let joined: String = lines.join("");
    assert!(joined.contains("🇰🇷") && joined.contains("🇯🇵") && joined.contains("🇺🇸"));
}

#[test]
fn wrap_segments_zwj_across_break() {
    // A styled run containing a ZWJ family emoji never splits the cluster
    // across two visual lines.
    let whole = "👨\u{200D}👩\u{200D}👧\u{200D}👦";
    let segs = vec![
        ("日本".to_string(), Style::new()),
        (whole.to_string(), Style::new()),
    ];
    let lines = wrap_segments(&segs, 2);
    // Find the cluster reassembled on a single line.
    let any_whole = lines.iter().any(|line| {
        let joined: String = line.iter().map(|(t, _)| t.as_str()).collect();
        joined.contains(whole)
    });
    assert!(any_whole, "ZWJ cluster split across lines: {lines:?}");
    // No line fragment dangles a ZWJ at an edge.
    for line in &lines {
        for (t, _) in line {
            assert!(
                !t.starts_with('\u{200D}') && !t.ends_with('\u{200D}'),
                "dangling ZWJ in segment {t:?}"
            );
        }
    }
}

#[test]
fn truncate_with_ellipsis_drops_whole_cluster() {
    // "🇰🇷abc" truncated to width 2: the flag is width 2 so it cannot fit
    // alongside the ellipsis (target = 1). It is dropped whole, not halved.
    let out = super::render::truncate_with_ellipsis("🇰🇷abc", 2);
    assert_eq!(out, "\u{2026}");
    assert!(!contains_lone_regional_indicator(&out));
}

#[test]
fn truncate_with_ellipsis_ascii_unchanged() {
    // ASCII regression: identical to scalar behavior.
    assert_eq!(
        super::render::truncate_with_ellipsis("hello", 4),
        "hel\u{2026}"
    );
    assert_eq!(
        super::render::truncate_with_ellipsis("hi", 10),
        "hi\u{2026}"
    );
}

// --- gap_overlap (signed gap) tests (#222) -------------------------------

/// Build a `Direction` container with `gap` (signed) holding `n` fixed-size
/// children, lay it out in `area`, and return the computed tree. Each child
/// is a text node constrained to exactly `child_w` x `child_h` so positions
/// are deterministic regardless of content metrics.
fn gap_overlap_tree(
    direction: Direction,
    gap: i32,
    child_w: u32,
    child_h: u32,
    n: usize,
    area: crate::rect::Rect,
) -> LayoutNode {
    use crate::style::{Align, Constraints, Justify, Margin, Padding};

    let mut commands = vec![Command::BeginContainer(Box::new(BeginContainerArgs {
        direction,
        gap,
        align: Align::Start,
        align_self: None,
        justify: Justify::Start,
        border: None,
        border_sides: BorderSides::all(),
        border_style: crate::style::Style::new(),
        bg_color: None,
        padding: Padding::default(),
        margin: Margin::default(),
        constraints: Constraints::default(),
        title: None,
        grow: 0,
        group_name: None,
    }))];
    for _ in 0..n {
        commands.push(Command::Text {
            content: "x".into(),
            cursor_offset: None,
            style: crate::style::Style::new(),
            grow: 0,
            align: Align::Start,
            wrap: false,
            truncate: false,
            margin: Margin::default(),
            constraints: Constraints::default().w(child_w).h(child_h),
        });
    }
    commands.push(Command::EndContainer);

    let mut tree = build_tree(&mut commands);
    compute(&mut tree, area);
    tree
}

#[test]
fn gap_overlap_1_reduces_total_width() {
    // Row of two w(10) children in a w(25) parent with gap = -1.
    // Total used = 10 + 10 - 1 = 19; child 1 starts at child 0 end - 1.
    let tree = gap_overlap_tree(
        Direction::Row,
        -1,
        10,
        1,
        2,
        crate::rect::Rect::new(0, 0, 25, 4),
    );
    let row = &tree.children[0];
    let c0 = &row.children[0];
    let c1 = &row.children[1];
    assert_eq!(c0.size.0, 10);
    assert_eq!(c1.size.0, 10);
    // Overlap by 1: child 1 starts one column before child 0's end.
    assert_eq!(
        c1.pos.0,
        c0.pos.0 + c0.size.0 - 1,
        "child 1 must overlap child 0 by one column"
    );
    // Total extent from first child start to last child end = 19.
    assert_eq!(c1.pos.0 + c1.size.0 - c0.pos.0, 19);
}

#[test]
fn gap_overlap_zero_is_gap_zero() {
    // gap_overlap(0) (gap = 0) must lay out identically to gap(0).
    let area = crate::rect::Rect::new(0, 0, 40, 4);
    let a = gap_overlap_tree(Direction::Row, 0, 8, 1, 3, area);
    let b = gap_overlap_tree(Direction::Row, 0, 8, 1, 3, area);
    for i in 0..3 {
        assert_eq!(a.children[0].children[i].pos, b.children[0].children[i].pos);
        assert_eq!(
            a.children[0].children[i].size,
            b.children[0].children[i].size
        );
    }
    // And child 1 starts exactly at child 0's end (no overlap, no gap).
    let row = &a.children[0];
    assert_eq!(row.children[1].pos.0, row.children[0].pos.0 + 8);
}

#[test]
fn gap_overlap_large_value_does_not_panic() {
    // gap = -1000 with small children: positions saturate at 0, no panic/wrap.
    let tree = gap_overlap_tree(
        Direction::Row,
        -1000,
        5,
        1,
        3,
        crate::rect::Rect::new(0, 0, 20, 4),
    );
    let row = &tree.children[0];
    assert_eq!(row.children.len(), 3);
    // All children collapse to the same starting column (positions clamped).
    for child in &row.children {
        assert_eq!(child.pos.0, row.children[0].pos.0);
    }
}

#[test]
fn gap_positive_unchanged() {
    // gap = 2 (positive) behaves as before: child 1 starts 2 cols after child 0.
    let tree = gap_overlap_tree(
        Direction::Row,
        2,
        6,
        1,
        2,
        crate::rect::Rect::new(0, 0, 40, 4),
    );
    let row = &tree.children[0];
    let c0 = &row.children[0];
    let c1 = &row.children[1];
    assert_eq!(c1.pos.0, c0.pos.0 + c0.size.0 + 2);
}

#[test]
fn gap_overlap_col_direction() {
    // gap = -1 on a column container overlaps children vertically by one row.
    let tree = gap_overlap_tree(
        Direction::Column,
        -1,
        4,
        3,
        2,
        crate::rect::Rect::new(0, 0, 10, 25),
    );
    let col = &tree.children[0];
    let c0 = &col.children[0];
    let c1 = &col.children[1];
    assert_eq!(c0.size.1, 3);
    assert_eq!(c1.size.1, 3);
    assert_eq!(
        c1.pos.1,
        c0.pos.1 + c0.size.1 - 1,
        "child 1 must overlap child 0 by one row"
    );
}

mod wrap_lines_grapheme_property {
    use super::wrap_lines;
    use proptest::prelude::*;
    use unicode_segmentation::UnicodeSegmentation;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(256))]
        // For arbitrary strings (newline-free so we test the soft-wrap kernel),
        // every cluster on every output line must be a whole input cluster:
        // no output cluster boundary that wasn't an input boundary.
        #[test]
        fn no_partial_cluster(
            s in "[a-c \\x{1F1E6}-\\x{1F1FF}\\x{0301}\\x{0E33}\\x{0E01}]{0,32}",
            w in 1u32..6,
        ) {
            let input_clusters: std::collections::HashSet<String> =
                s.graphemes(true).map(str::to_string).collect();
            for line in wrap_lines(&s, w) {
                for g in line.graphemes(true) {
                    // Spaces are inserted as word separators by the wrapper, so
                    // a bare space is always a legal output cluster.
                    prop_assert!(
                        g == " " || input_clusters.contains(g),
                        "output cluster {:?} was not a whole input cluster (input {:?}, width {})",
                        g, s, w
                    );
                }
            }
        }
    }
}

// ─── #247: horizontal scroll (scroll_row) layout / collect kernel ──────────

/// Build a scrollable row of fixed-width text children for the #247 tests.
///
/// Each child is a 1-row text node sized `cell_w` wide; `gap` is the
/// inter-child main-axis gap. The container is positioned at the origin and
/// marked `is_scrollable`, mirroring what `build_children` produces for
/// `scroll_row`.
#[cfg(test)]
fn scrollable_row(children_widths: &[u32], gap: i32, viewport_w: u32) -> LayoutNode {
    let mut row = LayoutNode::container(Direction::Row, default_container_config());
    row.is_scrollable = true;
    row.gap = gap;
    for (i, &w) in children_widths.iter().enumerate() {
        let mut t = LayoutNode::text(
            format!("C{i}"),
            Style::new(),
            0,
            Align::Start,
            (None, false, false),
            Margin::default(),
            Constraints::default().min_w(w).max_w(w),
        );
        // Seed the intrinsic width so `min_width` reports `w` regardless of the
        // 2-char label.
        t.size = (w, 1);
        row.children.push(t);
    }
    let mut root = LayoutNode::container(Direction::Column, default_container_config());
    root.children.push(row);
    compute(&mut root, crate::rect::Rect::new(0, 0, viewport_w, 4));
    root
}

#[test]
fn scrollable_row_overflow_sets_content_width_and_lays_out_horizontally() {
    // Four 10-wide children + 3 gaps of 1 = 43 natural width into a 20-wide
    // viewport. The scrollable row must lay out into the oversized virtual
    // area (children march left→right past the viewport) and record
    // `content_width == natural_width`.
    let root = scrollable_row(&[10, 10, 10, 10], 1, 20);
    let row = &root.children[0];
    assert!(row.is_scrollable);
    assert_eq!(
        row.content_width, 43,
        "content_width must equal the natural width (4*10 + 3*1)"
    );
    assert_eq!(
        row.content_height, 0,
        "a scrollable row has no content_height"
    );

    // Children laid out left→right at the same y, spanning past the viewport.
    let c = &row.children;
    assert_eq!(c[0].pos, (0, 0));
    assert_eq!(c[1].pos.0, 11, "second child after first(10)+gap(1)");
    assert_eq!(c[2].pos.0, 22);
    assert_eq!(c[3].pos.0, 33);
    for child in c {
        assert_eq!(child.pos.1, 0, "all children share the same row (y)");
    }
    assert!(
        c[3].pos.0 + c[3].size.0 > 20,
        "the far-right child must overflow the 20-wide viewport"
    );
}

#[test]
fn scrollable_row_fits_reports_content_within_viewport() {
    // 3*5 + 2*1 = 17 ≤ 20: no overflow. Mirroring the scrollable-column path,
    // `content_width` records the real content extent (not forced to 0), and
    // because it is ≤ the viewport width the row reports no scrollable slack:
    // `can_scroll_right` will be false.
    let root = scrollable_row(&[5, 5, 5], 1, 20);
    let row = &root.children[0];
    assert_eq!(
        row.content_width, 17,
        "a fitting scrollable row records its real content width (3*5 + 2*1)"
    );
    let mut s = crate::widgets::ScrollState::new();
    s.set_bounds_x(row.content_width, 20);
    assert!(
        !s.can_scroll_right(),
        "content (17) ≤ viewport (20): no horizontal slack"
    );
}

#[test]
fn scrollable_column_reports_zero_content_width() {
    // Regression guard: a vertical scrollable must keep content_width == 0 and
    // a non-zero content_height, so `ScrollState` never sees phantom
    // horizontal overflow (#247 must not disturb the y-axis).
    let mut root = LayoutNode::container(Direction::Column, default_container_config());
    let mut col = LayoutNode::container(Direction::Column, default_container_config());
    col.is_scrollable = true;
    for i in 0..10 {
        let mut t = LayoutNode::text(
            format!("row {i}"),
            Style::new(),
            0,
            Align::Start,
            (None, false, false),
            Margin::default(),
            Constraints::default(),
        );
        t.size = (6, 1);
        col.children.push(t);
    }
    root.children.push(col);
    compute(&mut root, crate::rect::Rect::new(0, 0, 20, 4));
    let col = &root.children[0];
    assert_eq!(
        col.content_width, 0,
        "vertical scrollable: content_width == 0"
    );
    assert!(
        col.content_height > 0,
        "vertical scrollable: content_height > 0"
    );
}

#[test]
fn collect_all_reports_horizontal_axis_for_scrollable_row() {
    // The collect pass must tag a scrollable row with `is_horizontal = true`
    // and report content/viewport *widths*, so `Context::scrollable` binds the
    // x-axis next frame.
    let root = scrollable_row(&[10, 10, 10, 10], 1, 20);
    let mut fd = FrameData::default();
    collect_all(&root, &mut fd);
    assert_eq!(fd.scroll_infos.len(), 1);
    let (content, viewport, is_horizontal) = fd.scroll_infos[0];
    assert!(is_horizontal, "scrollable row must be tagged horizontal");
    assert_eq!(content, 43, "reports content width");
    assert_eq!(viewport, 20, "reports viewport width");
}

#[test]
fn collect_all_keeps_vertical_axis_flag_false_for_scrollable_column() {
    let mut root = LayoutNode::container(Direction::Column, default_container_config());
    let mut col = LayoutNode::container(Direction::Column, default_container_config());
    col.is_scrollable = true;
    col.pos = (0, 0);
    col.size = (20, 4);
    col.content_height = 30;
    root.children.push(col);
    let mut fd = FrameData::default();
    collect_all(&root, &mut fd);
    let (content, viewport, is_horizontal) = fd.scroll_infos[0];
    assert!(!is_horizontal, "scrollable column stays vertical (false)");
    assert_eq!(content, 30);
    assert_eq!(viewport, 4);
}

#[test]
fn collect_all_shifts_child_x_by_scroll_offset_x() {
    // A scrollable row with `scroll_offset_x = 7` must report its child focus
    // rect shifted left by 7 (the on-screen x), mirroring the y-axis offset.
    let mut root = LayoutNode::container(Direction::Column, default_container_config());
    let mut row = LayoutNode::container(Direction::Row, default_container_config());
    row.is_scrollable = true;
    row.pos = (0, 0);
    row.size = (20, 1);
    row.scroll_offset_x = 7;
    let mut child = LayoutNode::text(
        "focusable".to_string(),
        Style::new(),
        0,
        Align::Start,
        (None, false, false),
        Margin::default(),
        Constraints::default(),
    );
    child.pos = (10, 0);
    child.size = (9, 1);
    child.focus_id = Some(3);
    row.children.push(child);
    root.children.push(row);

    let mut fd = FrameData::default();
    collect_all(&root, &mut fd);
    let (_, rect) = fd
        .focus_rects
        .iter()
        .find(|(id, _)| *id == 3)
        .expect("focus rect collected");
    assert_eq!(
        rect.x, 3,
        "child at layout x=10 with x-offset 7 must collect at screen x=3"
    );
}

#[test]
fn scroll_row_renders_left_to_right_not_stacked() {
    // Regression for the core #247 bug: `scroll_row` must NOT silently build a
    // vertical column. Children must increase in x with a constant y.
    let mut tb = crate::test_utils::TestBackend::new(20, 6);
    let mut scroll = crate::widgets::ScrollState::new();
    tb.render(|ui| {
        let _ = ui.scroll_row(&mut scroll, |ui| {
            for i in 0..6 {
                ui.text(format!("COL{i}"));
            }
        });
    });
    // The leftmost columns are visible on the same single line; a vertical
    // column would have stacked them on separate lines instead.
    let line0 = tb.line(0);
    assert!(
        line0.contains("COL0") && line0.contains("COL1"),
        "scroll_row must lay COL0 and COL1 on the SAME line (got {line0:?})"
    );
    assert_eq!(
        tb.line(1),
        "",
        "a horizontal scroll_row must not stack children onto row 1"
    );
}

#[test]
fn scroll_row_clips_far_right_then_reveals_after_scroll() {
    // Wide content into a narrow viewport: a far-right column is clipped until
    // we scroll right, then it appears and the leftmost clips out.
    let mut tb = crate::test_utils::TestBackend::new(16, 4);
    let mut scroll = crate::widgets::ScrollState::new();

    // Frame 1 records the scroll bounds; frame 2's `scrollable()` reads them
    // back via `prev_scroll_infos` and calls `set_bounds_x`, so after frame 2
    // `scroll` knows its content/viewport widths and `scroll_right` can clamp
    // correctly. (Same two-frame bounds lifecycle as vertical scroll.)
    tb.render(|ui| {
        let _ = ui.scroll_row(&mut scroll, |ui| {
            for i in 0..8 {
                ui.text(format!("[item{i:02}]"));
            }
        });
    });
    tb.render(|ui| {
        let _ = ui.scroll_row(&mut scroll, |ui| {
            for i in 0..8 {
                ui.text(format!("[item{i:02}]"));
            }
        });
    });
    tb.assert_contains("[item00]");
    tb.assert_not_contains("[item07]");

    // Now bounds are known: scroll right far enough to reveal the last item.
    scroll.scroll_right(60);
    tb.render(|ui| {
        let _ = ui.scroll_row(&mut scroll, |ui| {
            for i in 0..8 {
                ui.text(format!("[item{i:02}]"));
            }
        });
    });
    tb.assert_contains("[item07]");
    tb.assert_not_contains("[item00]");
}

#[test]
fn scroll_state_horizontal_bounds_clamp_and_predicates() {
    // Drive the bounds/predicate surface directly (no rendering): the x-axis
    // mirror of the vertical clamp/can_scroll tests.
    let mut s = crate::widgets::ScrollState::new();
    s.set_bounds_x(100, 20); // content 100 wide, viewport 20 → max offset 80.

    assert!(!s.can_scroll_left(), "at start, cannot scroll left");
    assert!(s.can_scroll_right(), "content overflows, can scroll right");

    s.scroll_right(1000); // way past the end
    assert_eq!(s.offset_x, 80, "scroll_right clamps to content - viewport");
    assert!(!s.can_scroll_right(), "at end, cannot scroll right");
    assert!(s.can_scroll_left(), "at end, can scroll left");

    s.scroll_left(1000); // way past the start
    assert_eq!(s.offset_x, 0, "scroll_left clamps to 0");
    assert!((s.progress_x() - 0.0).abs() < f64::EPSILON);

    s.scroll_right(40);
    assert!((s.progress_x() - 0.5).abs() < 1e-9, "40/80 == 0.5 progress");
}

#[test]
fn scroll_state_vertical_api_unchanged_by_horizontal_addition() {
    // The vertical API must remain byte-identical: a column scrollable updates
    // `offset` only, never `offset_x`.
    let mut s = crate::widgets::ScrollState::new();
    s.set_bounds(50, 10);
    s.scroll_down(5);
    assert_eq!(s.offset, 5);
    assert_eq!(s.offset_x, 0, "vertical scroll must not touch offset_x");
    assert_eq!(s.content_width(), 0);
    assert_eq!(s.viewport_width(), 0);
}

mod hscroll_proptest {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(128))]
        // Invariant 1: for any child widths and viewport, after layout the
        // scrollable row's `content_width` equals the natural width (sum of
        // child widths + gaps) — the x-axis mirror of the column path, which
        // records the real content extent regardless of overflow.
        //
        // Invariant 2: `ScrollState::scroll_right(offset)` always clamps
        // `offset_x` into `[0, content_width - viewport_width]`, so the
        // rendered viewport never scrolls past the content edge.
        #[test]
        fn content_width_and_offset_clamp(
            widths in prop::collection::vec(1u32..12, 1..8),
            viewport_w in 4u32..30,
            requested_offset in 0usize..200,
        ) {
            let root = scrollable_row(&widths, 1, viewport_w);
            let row = &root.children[0];

            let gap: i64 = 1;
            let gaps = (widths.len() as i64 - 1).max(0) * gap;
            let natural: i64 = widths.iter().map(|&w| w as i64).sum::<i64>() + gaps;
            prop_assert_eq!(row.content_width as i64, natural,
                "scrollable row records the natural content width");

            // Offset clamp invariant via collect (the binding path).
            let mut fd = FrameData::default();
            collect_all(&root, &mut fd);
            let (content, viewport, is_horizontal) = fd.scroll_infos[0];
            prop_assert!(is_horizontal);

            let mut s = crate::widgets::ScrollState::new();
            s.set_bounds_x(content, viewport);
            s.scroll_right(requested_offset);
            let max = content.saturating_sub(viewport) as usize;
            prop_assert!(s.offset_x <= max,
                "offset_x {} must clamp to max {}", s.offset_x, max);
            // No leak past the clip: every visible cell's screen x is within
            // [0, viewport). A child at layout x `cx` renders at `cx - offset_x`;
            // require the visible window to stay within bounds.
            prop_assert!(s.offset_x as u32 <= content,
                "offset_x must never exceed total content width");
        }
    }
}