oxicode-textarea 0.79.0

Atomic-mutation text editor widget with soft-wrap and TextElement atomicity (ported from xai-org/grok-build)
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
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
use super::*;
// crossterm types are intentionally not imported here to avoid unused warnings
use rand::prelude::*;

fn rand_grapheme(rng: &mut rand::rngs::StdRng) -> String {
    let r: u8 = rng.random_range(0..100);
    match r {
        0..=4 => "\n".to_string(),
        5..=12 => " ".to_string(),
        13..=35 => (rng.random_range(b'a'..=b'z') as char).to_string(),
        36..=45 => (rng.random_range(b'A'..=b'Z') as char).to_string(),
        46..=52 => (rng.random_range(b'0'..=b'9') as char).to_string(),
        53..=65 => {
            // Some emoji (wide graphemes)
            let choices = ["👍", "😊", "🐍", "🚀", "🧪", "🌟"];
            choices[rng.random_range(0..choices.len())].to_string()
        }
        66..=75 => {
            // CJK wide characters
            let choices = ["", "", "", "", "", "", "", "", ""];
            choices[rng.random_range(0..choices.len())].to_string()
        }
        76..=85 => {
            // Combining mark sequences
            let base = ["e", "a", "o", "n", "u"][rng.random_range(0..5)];
            let marks = ["\u{0301}", "\u{0308}", "\u{0302}", "\u{0303}"];
            format!("{base}{}", marks[rng.random_range(0..marks.len())])
        }
        86..=92 => {
            // Some non-latin single codepoints (Greek, Cyrillic, Hebrew)
            let choices = ["Ω", "β", "Ж", "ю", "ש", "م", ""];
            choices[rng.random_range(0..choices.len())].to_string()
        }
        _ => {
            // ZWJ sequences (single graphemes but multi-codepoint)
            let choices = [
                "👩\u{200D}💻", // woman technologist
                "👨\u{200D}💻", // man technologist
                "🏳️\u{200D}🌈", // rainbow flag
            ];
            choices[rng.random_range(0..choices.len())].to_string()
        }
    }
}

fn ta_with(text: &str) -> TextArea {
    let mut t = TextArea::new();
    t.insert_str(text);
    t
}

#[test]
fn canonical_adapter_matches_standalone_edit_buffer() {
    let cases = [
        (
            "hello-world",
            "hello-world".len(),
            EditCommand::MoveWordLeft(WordStyle::Small),
        ),
        (
            "hello-world",
            0,
            EditCommand::MoveWordRight(WordStyle::Small),
        ),
        (
            "foo bar",
            "foo bar".len(),
            EditCommand::DeleteWordBackward(WordStyle::Small),
        ),
        (
            "foo bar",
            0,
            EditCommand::DeleteWordForward(WordStyle::Small),
        ),
        ("one\ntwo", 4, EditCommand::MoveLogicalLineStart),
        ("one\ntwo", 3, EditCommand::MoveLogicalLineEnd),
        ("abc", 2, EditCommand::DeleteGraphemeBackward),
        ("abc", 1, EditCommand::DeleteGraphemeForward),
    ];

    for (text, cursor, command) in cases {
        let mut textarea = TextArea::new();
        textarea.set_text(text);
        textarea.clear_history();
        textarea.set_cursor(cursor);
        let mut buffer = EditBuffer::from_parts(text, cursor);

        textarea.apply_classified_command(command);
        let _ = buffer.apply(command);

        assert_eq!(textarea.text(), buffer.text());
        assert_eq!(textarea.cursor(), buffer.cursor_byte());
    }
}

#[test]
fn canonical_adapter_updates_selection_from_applied_delta() {
    let mut textarea = ta_with("abcdef");
    textarea.set_selection(4, 6);
    textarea.replace_range(0..2, "X");
    assert_eq!(textarea.text(), "Xcdef");
    assert_eq!(textarea.selection_range(), Some(3..5));
}

#[test]
fn canonical_adapter_applies_same_byte_metadata_edits_with_history() {
    let mut textarea = TextArea::new();
    let id = textarea.insert_element("TOKEN", ElementKind(1), None);
    textarea.set_selection(0, 5);
    textarea.clear_history();

    textarea.replace_range(0..5, "TOKEN");
    assert_eq!(textarea.text(), "TOKEN");
    assert!(textarea.elements().is_empty());
    assert!(textarea.selection.is_none());
    assert!(textarea.can_undo());

    assert!(textarea.undo());
    assert_eq!(textarea.text(), "TOKEN");
    assert_eq!(textarea.elements().len(), 1);
    assert_eq!(textarea.elements()[0].id, id);
    assert!(textarea.redo());
    assert_eq!(textarea.text(), "TOKEN");
    assert!(textarea.elements().is_empty());
}

#[test]
fn replace_element_forces_cursor_end_and_restores_metadata() {
    let mut before = ta_with("left TOKEN right");
    before.clear_history();
    before.set_cursor(0);
    let id = before.replace_range_with_element(5..10, "NODE", ElementKind(1), None);
    let end = 5 + "NODE".len();
    assert_eq!(before.cursor(), end);
    assert_eq!(before.elements()[0].id, id);

    assert!(before.undo());
    assert_eq!(before.text(), "left TOKEN right");
    assert!(before.elements().is_empty());
    assert_eq!(before.cursor(), 0);
    assert!(before.redo());
    assert_eq!(before.text(), "left NODE right");
    assert_eq!(before.elements()[0].id, id);
    assert_eq!(before.cursor(), end);

    let mut after = ta_with("left TOKEN right");
    after.clear_history();
    after.set_cursor(after.text().len());
    after.replace_range_with_element(5..10, "NODE", ElementKind(1), None);
    assert_eq!(after.cursor(), end);
}

#[test]
fn empty_set_text_invalidates_redo_and_is_undoable() {
    let mut textarea = TextArea::new();
    textarea.insert_str("x");
    assert!(textarea.undo());
    assert!(textarea.can_redo());

    textarea.set_text("");
    assert!(!textarea.can_redo());
    assert!(textarea.can_undo());
    assert_eq!(textarea.cursor(), 0);
}

#[test]
fn set_text_preserves_cursor_clamped_across_grow_and_shrink() {
    let mut grow = ta_with("abcd");
    grow.set_cursor(2);
    grow.set_text("abcdefgh");
    assert_eq!(grow.cursor(), 2);

    let mut shrink = ta_with("abcdefgh");
    shrink.set_cursor(6);
    shrink.set_text("abc");
    assert_eq!(shrink.cursor(), 3);
}

#[test]
fn set_text_restores_zero_length_element_metadata_through_history() {
    let mut textarea = TextArea::new();
    let id = textarea.insert_element("", ElementKind(7), None);
    textarea.clear_history();

    textarea.set_text("");
    assert!(textarea.elements().is_empty());
    assert!(textarea.undo());
    assert_eq!(textarea.text(), "");
    assert_eq!(textarea.elements().len(), 1);
    assert_eq!(textarea.elements()[0].id, id);
    assert_eq!(textarea.elements()[0].range, 0..0);
    assert!(textarea.redo());
    assert!(textarea.elements().is_empty());
}

#[test]
fn rejected_adapter_plan_has_no_side_effects() {
    let mut textarea = TextArea::new();
    let id = textarea.insert_element("TOKEN", ElementKind(1), None);
    textarea.set_selection(0, 5);
    textarea.kill_buffer = "sentinel".to_owned();
    textarea.preferred_col = Some(3);
    textarea.scroll_override = Some(2);
    let _ = textarea.desired_height(20);
    textarea.clear_history();
    let plan = textarea.plan_edit_replacement(0..5, "X");
    let _ = textarea.text.set_cursor_byte(0);

    let result = textarea.try_apply_edit_plan(plan, Some(MutationKind::Replace));
    assert_eq!(result, Err(ApplyEditPlanError::StalePlan));
    assert_eq!(textarea.text(), "TOKEN");
    assert_eq!(textarea.elements().len(), 1);
    assert_eq!(textarea.elements()[0].id, id);
    assert_eq!(textarea.selection_range(), Some(0..5));
    assert_eq!(textarea.kill_buffer, "sentinel");
    assert_eq!(textarea.preferred_col, Some(3));
    assert_eq!(textarea.scroll_override, Some(2));
    assert!(textarea.wrap_cache.borrow().is_some());
    assert!(!textarea.can_undo());
}

#[test]
fn handled_boundary_navigation_clears_vertical_affinity() {
    let mut textarea = ta_with("ab\nwxyz");
    textarea.set_cursor(0);
    textarea.preferred_col = Some(3);
    textarea.scroll_override = Some(2);

    textarea.move_cursor_left();
    assert_eq!(textarea.cursor(), 0);
    assert_eq!(textarea.preferred_col, None);
    assert_eq!(textarea.scroll_override, None);

    textarea.move_cursor_down();
    assert_eq!(textarea.cursor(), 3);
}

#[test]
fn insert_str_at_inside_element_clamps_to_an_atomic_boundary() {
    let mut textarea = TextArea::new();
    textarea.insert_str("a");
    textarea.insert_element("TOKEN", ElementKind(1), None);
    textarea.insert_str("b");
    textarea.clear_history();

    textarea.insert_str_at(3, "X");
    assert_eq!(textarea.text(), "aXTOKENb");
    assert_eq!(textarea.cursor(), 8);
    assert_eq!(textarea.elements()[0].range, 2..7);
    assert!(textarea.undo());
    assert_eq!(textarea.text(), "aTOKENb");
    assert_eq!(textarea.elements()[0].range, 1..6);
}

#[test]
fn canonical_adapter_keeps_elements_atomic_for_motion_and_deletion() {
    let mut backward = TextArea::new();
    backward.insert_str("a");
    let id = backward.insert_element("TOKEN", ElementKind(1), None);
    backward.insert_str("b");
    let range = backward.elements()[0].range.clone();
    backward.set_cursor(range.end);
    backward.move_cursor_left();
    assert_eq!(backward.cursor(), range.start);
    backward.move_cursor_right();
    assert_eq!(backward.cursor(), range.end);
    backward.delete_backward(1);
    assert_eq!(backward.text(), "ab");
    assert!(backward.elements().iter().all(|element| element.id != id));

    let mut forward = TextArea::new();
    forward.insert_str("a");
    forward.insert_element("TOKEN", ElementKind(1), None);
    forward.insert_str("b");
    let range = forward.elements()[0].range.clone();
    forward.set_cursor(range.start);
    forward.delete_forward(1);
    assert_eq!(forward.text(), "ab");
    assert!(forward.elements().is_empty());
}

#[test]
fn canonical_adapter_ignores_element_newlines_and_restores_kills() {
    let mut textarea = TextArea::new();
    textarea.insert_str("a");
    textarea.insert_element("X\nY", ElementKind(1), None);
    textarea.insert_str("b\nc");
    textarea.clear_history();

    textarea.set_cursor(0);
    textarea.move_cursor_to_end_of_line(true);
    assert_eq!(textarea.cursor(), 5);
    textarea.set_cursor(0);
    textarea.kill_to_end_of_line();
    assert_eq!(textarea.text(), "\nc");
    assert_eq!(textarea.kill_buffer, "aX\nYb");

    assert!(textarea.undo());
    assert_eq!(textarea.text(), "aX\nYb\nc");
    assert_eq!(textarea.elements().len(), 1);
    assert!(textarea.redo());
    assert_eq!(textarea.text(), "\nc");
    assert!(textarea.elements().is_empty());
}

#[test]
fn canonical_adapter_preserves_right_affinity_through_undo_redo() {
    let woman = "👩";
    let tail = "👩🏽\u{200d}💻";
    let original = format!("{woman}{tail}");
    let mut textarea = ta_with(&original);
    textarea.clear_history();
    textarea.set_cursor(woman.len());

    textarea.insert_str("\u{200d}");
    assert_eq!(textarea.text().graphemes(true).count(), 1);
    assert_eq!(textarea.cursor(), textarea.text().len());

    assert!(textarea.undo());
    assert_eq!(textarea.text(), original);
    assert_eq!(textarea.cursor(), woman.len());
    assert!(textarea.redo());
    assert_eq!(textarea.text().graphemes(true).count(), 1);
    assert_eq!(textarea.cursor(), textarea.text().len());
}

#[test]
fn is_undo_input_accepts_ctrl_and_cmd_z() {
    assert!(is_undo_input(&KeyEvent::new(
        KeyCode::Char('z'),
        KeyModifiers::CONTROL
    )));
    assert!(is_undo_input(&KeyEvent::new(
        KeyCode::Char('z'),
        KeyModifiers::SUPER
    )));
}

#[test]
fn is_undo_input_rejects_redo_and_plain_z() {
    // Uppercase 'Z' (redo) stays excluded so the guard is disjoint from
    // the redo arm regardless of match order.
    assert!(!is_undo_input(&KeyEvent::new(
        KeyCode::Char('Z'),
        KeyModifiers::CONTROL | KeyModifiers::SHIFT
    )));
    // A bare 'z' (no chord modifier) is plain typing, not undo.
    assert!(!is_undo_input(&KeyEvent::new(
        KeyCode::Char('z'),
        KeyModifiers::NONE
    )));
    assert!(!is_undo_input(&KeyEvent::new(
        KeyCode::Char('z'),
        KeyModifiers::SHIFT
    )));
}

#[test]
fn insert_and_replace_update_cursor_and_text() {
    // insert helpers
    let mut t = ta_with("hello");
    t.set_cursor(5);
    t.insert_str("!");
    assert_eq!(t.text(), "hello!");
    assert_eq!(t.cursor(), 6);

    t.insert_str_at(0, "X");
    assert_eq!(t.text(), "Xhello!");
    assert_eq!(t.cursor(), 7);

    // Insert after the cursor should not move it
    t.set_cursor(1);
    let end = t.text().len();
    t.insert_str_at(end, "Y");
    assert_eq!(t.text(), "Xhello!Y");
    assert_eq!(t.cursor(), 1);

    // replace_range cases
    // 1) cursor before range
    let mut t = ta_with("abcd");
    t.set_cursor(1);
    t.replace_range(2..3, "Z");
    assert_eq!(t.text(), "abZd");
    assert_eq!(t.cursor(), 1);

    // 2) cursor inside range
    let mut t = ta_with("abcd");
    t.set_cursor(2);
    t.replace_range(1..3, "Q");
    assert_eq!(t.text(), "aQd");
    assert_eq!(t.cursor(), 2);

    // 3) cursor after range with shifted by diff
    let mut t = ta_with("abcd");
    t.set_cursor(4);
    t.replace_range(0..1, "AA");
    assert_eq!(t.text(), "AAbcd");
    assert_eq!(t.cursor(), 5);
}

#[test]
fn delete_backward_and_forward_edges() {
    let mut t = ta_with("abc");
    t.set_cursor(1);
    t.delete_backward(1);
    assert_eq!(t.text(), "bc");
    assert_eq!(t.cursor(), 0);

    // deleting backward at start is a no-op
    t.set_cursor(0);
    t.delete_backward(1);
    assert_eq!(t.text(), "bc");
    assert_eq!(t.cursor(), 0);

    // forward delete removes next grapheme
    t.set_cursor(1);
    t.delete_forward(1);
    assert_eq!(t.text(), "b");
    assert_eq!(t.cursor(), 1);

    // forward delete at end is a no-op
    t.set_cursor(t.text().len());
    t.delete_forward(1);
    assert_eq!(t.text(), "b");
}

#[test]
fn delete_backward_word_and_kill_line_variants() {
    // delete backward word at end removes the whole previous word
    let mut t = ta_with("hello   world  ");
    t.set_cursor(t.text().len());
    t.delete_backward_word();
    assert_eq!(t.text(), "hello   ");
    assert_eq!(t.cursor(), 8);

    // From inside a word, delete from word start to cursor
    let mut t = ta_with("foo bar");
    t.set_cursor(6); // inside "bar" (after 'a')
    t.delete_backward_word();
    assert_eq!(t.text(), "foo r");
    assert_eq!(t.cursor(), 4);

    // From end, delete the last word only
    let mut t = ta_with("foo bar");
    t.set_cursor(t.text().len());
    t.delete_backward_word();
    assert_eq!(t.text(), "foo ");
    assert_eq!(t.cursor(), 4);

    let mut t = ta_with("hello-world");
    t.set_cursor(t.text().len());
    t.delete_backward_word();
    assert_eq!(t.text(), "hello-");
    assert_eq!(t.cursor(), "hello-".len());

    // kill_to_end_of_line when not at EOL
    let mut t = ta_with("abc\ndef");
    t.set_cursor(1); // on first line, middle
    t.kill_to_end_of_line();
    assert_eq!(t.text(), "a\ndef");
    assert_eq!(t.cursor(), 1);

    // kill_to_end_of_line when at EOL deletes newline
    let mut t = ta_with("abc\ndef");
    t.set_cursor(3); // EOL of first line
    t.kill_to_end_of_line();
    assert_eq!(t.text(), "abcdef");
    assert_eq!(t.cursor(), 3);

    // kill_to_beginning_of_line from middle of line
    let mut t = ta_with("abc\ndef");
    t.set_cursor(5); // on second line, after 'e'
    t.kill_to_beginning_of_line();
    assert_eq!(t.text(), "abc\nef");

    // kill_to_beginning_of_line at beginning of non-first line removes the previous newline
    let mut t = ta_with("abc\ndef");
    t.set_cursor(4); // beginning of second line
    t.kill_to_beginning_of_line();
    assert_eq!(t.text(), "abcdef");
    assert_eq!(t.cursor(), 3);

    // kill_current_line from middle of single line
    let mut t = ta_with("hello world");
    t.set_cursor(5);
    t.kill_current_line();
    assert_eq!(t.text(), "");
    assert_eq!(t.cursor(), 0);

    // kill_current_line from middle of multiline
    let mut t = ta_with("abc\ndef\nghi");
    t.set_cursor(5);
    t.kill_current_line();
    assert_eq!(t.text(), "abc\n\nghi");
    assert_eq!(t.cursor(), 4);

    // kill_current_line on empty line joins with previous
    let mut t = ta_with("abc\n\nghi");
    t.set_cursor(4);
    t.kill_current_line();
    assert_eq!(t.text(), "abc\nghi");
    assert_eq!(t.cursor(), 3);

    // kill_current_line at beginning of only line
    let mut t = ta_with("hello");
    t.set_cursor(0);
    t.kill_current_line();
    assert_eq!(t.text(), "");
    assert_eq!(t.cursor(), 0);
}

#[test]
fn delete_forward_word_variants() {
    let mut t = ta_with("hello   world ");
    t.set_cursor(0);
    t.delete_forward_word();
    assert_eq!(t.text(), "   world ");
    assert_eq!(t.cursor(), 0);

    let mut t = ta_with("hello   world ");
    t.set_cursor(1);
    t.delete_forward_word();
    assert_eq!(t.text(), "h   world ");
    assert_eq!(t.cursor(), 1);

    let mut t = ta_with("hello   world");
    t.set_cursor(t.text().len());
    t.delete_forward_word();
    assert_eq!(t.text(), "hello   world");
    assert_eq!(t.cursor(), t.text().len());

    let mut t = ta_with("foo   \nbar");
    t.set_cursor(3);
    t.delete_forward_word();
    assert_eq!(t.text(), "foo");
    assert_eq!(t.cursor(), 3);

    let mut t = ta_with("foo\nbar");
    t.set_cursor(3);
    t.delete_forward_word();
    assert_eq!(t.text(), "foo");
    assert_eq!(t.cursor(), 3);

    let mut t = ta_with("hello-world");
    t.set_cursor(0);
    t.delete_forward_word();
    assert_eq!(t.text(), "-world");
    assert_eq!(t.cursor(), 0);

    let mut t = ta_with("hello   world ");
    t.set_cursor(t.text().len() + 10);
    t.delete_forward_word();
    assert_eq!(t.text(), "hello   world ");
    assert_eq!(t.cursor(), t.text().len());
}

#[test]
fn super_right_moves_to_end_of_line() {
    let mut t = ta_with("hello world\nsecond line");
    t.set_cursor(3); // middle of "hello world"
    t.input(KeyEvent::new(KeyCode::Right, KeyModifiers::SUPER));
    assert_eq!(t.cursor(), 11); // end of "hello world" (before \n)

    // Already at end of line → stays there
    t.input(KeyEvent::new(KeyCode::Right, KeyModifiers::SUPER));
    assert_eq!(t.cursor(), 11);
}

#[test]
fn super_left_moves_to_beginning_of_line() {
    let mut t = ta_with("hello world\nsecond line");
    let second_line_start = t.text().find("second").unwrap();
    t.set_cursor(second_line_start + 4); // middle of "second line"
    t.input(KeyEvent::new(KeyCode::Left, KeyModifiers::SUPER));
    assert_eq!(t.cursor(), second_line_start);

    // Already at beginning of line → stays there
    t.input(KeyEvent::new(KeyCode::Left, KeyModifiers::SUPER));
    assert_eq!(t.cursor(), second_line_start);
}

#[test]
fn super_backspace_kills_to_beginning_of_line() {
    let mut t = ta_with("hello world\nsecond line");
    let second_line_start = t.text().find("second").unwrap();
    t.set_cursor(second_line_start + 7); // after "second "
    t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::SUPER));
    assert_eq!(t.text(), "hello world\nline");
    assert_eq!(t.cursor(), second_line_start);
}

#[test]
fn ctrl_u_kills_to_beginning_of_line_keeps_text_after_cursor() {
    let mut t = ta_with("hello world");
    t.set_cursor(5);
    t.input(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL));
    assert_eq!(t.text(), " world");
    assert_eq!(t.cursor(), 0);
}

#[test]
fn delete_forward_word_handles_atomic_elements() {
    let kind = ElementKind(0);

    let mut t = TextArea::new();
    t.insert_element("<element>", kind, None);
    t.insert_str(" tail");

    t.set_cursor(0);
    t.delete_forward_word();
    assert_eq!(t.text(), " tail");
    assert_eq!(t.cursor(), 0);

    let mut t = TextArea::new();
    t.insert_str("   ");
    t.insert_element("<element>", kind, None);
    t.insert_str(" tail");

    t.set_cursor(0);
    t.delete_forward_word();
    assert_eq!(t.text(), " tail");
    assert_eq!(t.cursor(), 0);

    let mut t = TextArea::new();
    t.insert_str("prefix ");
    t.insert_element("<element>", kind, None);
    t.insert_str(" tail");

    // cursor in the middle of the element, delete_forward_word deletes the element
    let elem_range = t.elements()[0].range.clone();
    let _ = t
        .text
        .set_cursor_byte(elem_range.start + (elem_range.len() / 2));
    t.delete_forward_word();
    assert_eq!(t.text(), "prefix  tail");
    assert_eq!(t.cursor(), elem_range.start);
}

// ===== Phase 1: Typed element tests =====

#[test]
fn element_id_is_unique_and_stable() {
    let mut t = TextArea::new();
    let kind = ElementKind(1);

    let id1 = t.insert_element("aaa", kind, None);
    let id2 = t.insert_element("bbb", kind, None);
    assert_ne!(id1, id2);

    // ids survive after deletion of the first element
    t.set_cursor(0);
    t.delete_forward(1); // deletes "aaa" atomically
    assert_eq!(t.elements().len(), 1);
    assert_eq!(t.elements()[0].id, id2);
}

#[test]
fn element_kind_preserved() {
    let mut t = TextArea::new();
    let kind_paste = ElementKind(1);
    let kind_file = ElementKind(2);

    t.insert_element("paste", kind_paste, None);
    t.insert_element("file", kind_file, None);

    assert_eq!(t.elements()[0].kind, kind_paste);
    assert_eq!(t.elements()[1].kind, kind_file);
}

#[test]
fn element_at_cursor_returns_element() {
    let mut t = TextArea::new();
    let kind = ElementKind(0);

    t.insert_str("before ");
    let id = t.insert_element("[paste]", kind, None);
    t.insert_str(" after");

    // Cursor is at end of element after insert_element
    // Move to start of element
    t.set_cursor(7); // "before " is 7 bytes, element starts at 7
    let elem = t.element_at_cursor().expect("should find element");
    assert_eq!(elem.id, id);
    assert_eq!(elem.kind, kind);

    // Cursor before element
    t.set_cursor(0);
    assert!(t.element_at_cursor().is_none());

    // Cursor after element
    t.set_cursor(t.text().len());
    assert!(t.element_at_cursor().is_none());
}

#[test]
fn element_text_returns_buffer_text() {
    let mut t = TextArea::new();
    let id = t.insert_element("raw buffer content", ElementKind(0), None);
    assert_eq!(t.element_text(id), Some("raw buffer content"));

    // Non-existent id returns None
    let fake_id = ElementId(9999);
    assert_eq!(t.element_text(fake_id), None);
}

#[test]
fn element_display_can_be_set_and_updated() {
    let mut t = TextArea::new();
    let display = Line::from("[Pasted 5 lines]");
    let id = t.insert_element("lots of raw text here", ElementKind(1), Some(display));

    // Verify display is set
    let elem = &t.elements()[0];
    assert!(elem.display.is_some());
    assert_eq!(
        elem.display.as_ref().unwrap().to_string(),
        "[Pasted 5 lines]"
    );

    // Update display
    let new_display = Line::from("[Pasted 5 lines, 200 chars]");
    t.set_element_display(id, Some(new_display));
    let elem = &t.elements()[0];
    assert_eq!(
        elem.display.as_ref().unwrap().to_string(),
        "[Pasted 5 lines, 200 chars]"
    );

    // Clear display
    t.set_element_display(id, None);
    assert!(t.elements()[0].display.is_none());

    // Buffer text is unchanged
    assert_eq!(t.element_text(id), Some("lots of raw text here"));
}

#[test]
fn insert_element_returns_id_for_metadata_tracking() {
    let mut t = TextArea::new();
    let mut metadata: std::collections::HashMap<ElementId, String> =
        std::collections::HashMap::new();

    let id1 = t.insert_element("paste1", ElementKind(1), None);
    metadata.insert(id1, "First paste".to_string());

    let id2 = t.insert_element("paste2", ElementKind(1), None);
    metadata.insert(id2, "Second paste".to_string());

    // Verify we can look up metadata by id
    assert_eq!(metadata.get(&id1), Some(&"First paste".to_string()));
    assert_eq!(metadata.get(&id2), Some(&"Second paste".to_string()));

    // Delete first element
    t.set_cursor(0);
    t.delete_forward(1);

    // id2 still valid in our metadata map
    let remaining = &t.elements()[0];
    assert_eq!(remaining.id, id2);
    assert_eq!(
        metadata.get(&remaining.id),
        Some(&"Second paste".to_string())
    );
}

#[test]
fn elements_returns_sorted_slice() {
    let mut t = TextArea::new();
    let kind = ElementKind(0);

    t.insert_str("aaa ");
    t.insert_element("BBB", kind, None);
    t.insert_str(" ccc ");
    t.insert_element("DDD", kind, None);

    let elems = t.elements();
    assert_eq!(elems.len(), 2);
    assert!(elems[0].range.start < elems[1].range.start);
    assert_eq!(&t.text()[elems[0].range.clone()], "BBB");
    assert_eq!(&t.text()[elems[1].range.clone()], "DDD");
}

// ===== Phase 2: Display rendering & truncation tests =====

#[test]
fn render_element_with_display_shows_display_text() {
    use ratatui::style::Stylize;

    let mut t = TextArea::new();
    let display = Line::from("[Pasted]".cyan());
    t.insert_element("raw content here", ElementKind(0), Some(display));

    let area = Rect::new(0, 0, 20, 1);
    let mut buf = Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    // The rendered buffer should show "[Pasted]" not "raw content here"
    let rendered: String = (0..area.width)
        .map(|x| buf.cell((x, 0)).unwrap().symbol().to_string())
        .collect::<String>();
    let rendered = rendered.trim_end();
    assert_eq!(rendered, "[Pasted]");
}

#[test]
fn render_element_without_display_shows_buffer_text_cyan() {
    let mut t = TextArea::new();
    t.insert_element("hello", ElementKind(0), None);

    let area = Rect::new(0, 0, 20, 1);
    let mut buf = Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    // Should show "hello" with cyan foreground
    let cell = buf.cell((0, 0)).unwrap();
    assert_eq!(cell.symbol(), "h");
    assert_eq!(cell.fg, Color::Cyan);
}

#[test]
fn truncate_line_display_no_truncation_needed() {
    let line = Line::from("[Short]");
    let result = truncate_line_display(&line, 20);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    assert_eq!(text, "[Short]");
}

#[test]
fn truncate_line_display_with_bracket_preservation() {
    let line: Line<'static> = Line::from("[Pasted ~100 lines]");
    // Width 12: budget = 12 - 2 (ellipsis + bracket) = 10 chars content
    let result = truncate_line_display(&line, 12);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    assert!(text.ends_with(']'), "should preserve ]: got {text:?}");
    assert!(text.contains(''), "should contain ellipsis: got {text:?}");
    assert_eq!(text, "[Pasted ~1…]");
}

#[test]
fn truncate_line_display_without_bracket() {
    let line: Line<'static> = Line::from("very long display text");
    let result = truncate_line_display(&line, 10);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    assert!(text.contains(''));
    assert!(!text.ends_with(']'));
    // 9 chars content + 1 ellipsis = 10
    assert_eq!(text, "very long…");
}

#[test]
fn truncate_line_display_zero_width() {
    let line: Line<'static> = Line::from("[Pasted]");
    let result = truncate_line_display(&line, 0);
    assert!(result.spans.is_empty() || result.width() == 0);
}

#[test]
fn truncate_line_display_width_1() {
    let line: Line<'static> = Line::from("[Pasted]");
    let result = truncate_line_display(&line, 1);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    // Only room for "…" (the bracket can't fit with content)
    assert_eq!(text, "");
}

#[test]
fn truncate_preserves_multi_span_styles() {
    use ratatui::text::Span;

    let line: Line<'static> = Line::from(vec![
        Span::styled("[", Style::default().fg(Color::Yellow)),
        Span::styled("Pasted ~100 lines", Style::default().fg(Color::Cyan)),
        Span::styled("]", Style::default().fg(Color::Yellow)),
    ]);
    let result = truncate_line_display(&line, 10);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    assert!(text.ends_with(']'));
    assert!(text.contains(''));
    // "[" (1) + content budget (10-2=8) + "…" (1) + "]" (1) = 11? No...
    // Budget: 10 - 2 = 8 for content. "[" is 1, so 7 more chars of "Pasted ~"
    // Result: "[Pasted …]" which is 10 wide
    assert!(
        result.width() <= 10,
        "width should be <= 10, got {}",
        result.width()
    );
}

#[test]
fn render_element_with_prefix_text() {
    let mut t = TextArea::new();
    t.insert_str("hi ");
    let display = Line::from("[P]");
    t.insert_element("raw", ElementKind(0), Some(display));

    let area = Rect::new(0, 0, 20, 1);
    let mut buf = Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    let rendered: String = (0..area.width)
        .map(|x| buf.cell((x, 0)).unwrap().symbol().to_string())
        .collect::<String>();
    let rendered = rendered.trim_end();
    assert_eq!(rendered, "hi [P]");
}

#[test]
fn render_text_after_element_uses_display_width() {
    // User scenario: "foo " + element("Clean build", display="[📎 Pasted 1 line, 11 chars]") + " abcde"
    // Display: "[📎 Pasted 1 line, 11 chars]" = 1+2+1+23+1 = 28 display cols
    // Buffer: "Clean build" = 11 bytes
    // Without fix, text after element renders at buffer x, overlapping with element display.
    let mut t = TextArea::new();
    t.insert_str("foo ");
    let display = Line::from(vec![
        ratatui::text::Span::raw("["),
        ratatui::text::Span::raw("📎 "),
        ratatui::text::Span::raw("Pasted 1 line, 11 chars"),
        ratatui::text::Span::raw("]"),
    ]);
    t.insert_element("Clean build", ElementKind(0), Some(display));
    t.insert_str(" abcde");

    // Area wide enough to fit everything
    let area = Rect::new(0, 0, 80, 1);
    let mut buf = Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    // Verify key cells: text before element, element display, text after element.
    // "foo " occupies cols 0-3
    assert_eq!(buf.cell((0, 0)).unwrap().symbol(), "f");
    assert_eq!(buf.cell((3, 0)).unwrap().symbol(), " ");

    // Element display starts at col 4: "[📎 Pasted 1 line, 11 chars]"
    assert_eq!(buf.cell((4, 0)).unwrap().symbol(), "[");
    assert_eq!(buf.cell((5, 0)).unwrap().symbol(), "📎");
    // col 6 is the wide-char continuation cell
    assert_eq!(buf.cell((7, 0)).unwrap().symbol(), " ");
    assert_eq!(buf.cell((8, 0)).unwrap().symbol(), "P");

    // Element display ends at col 31 ("]")
    assert_eq!(buf.cell((31, 0)).unwrap().symbol(), "]");

    // Text after element: " abcde" starting at col 32
    assert_eq!(buf.cell((32, 0)).unwrap().symbol(), " ");
    assert_eq!(buf.cell((33, 0)).unwrap().symbol(), "a");
    assert_eq!(buf.cell((34, 0)).unwrap().symbol(), "b");
    assert_eq!(buf.cell((35, 0)).unwrap().symbol(), "c");
    assert_eq!(buf.cell((36, 0)).unwrap().symbol(), "d");
    assert_eq!(buf.cell((37, 0)).unwrap().symbol(), "e");
}

#[test]
fn render_text_after_wider_display_element_simple() {
    // Simpler case: element buffer text "x" (1 byte), display "[LONG]" (6 cols)
    // Suffix text "!" should render at column 6, not column 1.
    let mut t = TextArea::new();
    let display = Line::from("[LONG]");
    t.insert_element("x", ElementKind(0), Some(display));
    t.insert_str("!");

    let area = Rect::new(0, 0, 20, 1);
    let mut buf = Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    let rendered: String = (0..area.width)
        .map(|x| buf.cell((x, 0)).unwrap().symbol().to_string())
        .collect::<String>();
    let rendered = rendered.trim_end();
    assert_eq!(rendered, "[LONG]!");
}

// ===== Phase 3: Display projection tests =====

#[test]
fn display_width_of_range_plain_text() {
    let t = ta_with("hello world");
    assert_eq!(t.display_width_of_range(0, 5), 5); // "hello"
    assert_eq!(t.display_width_of_range(0, 11), 11); // "hello world"
    assert_eq!(t.display_width_of_range(6, 11), 5); // "world"
}

#[test]
fn insert_expands_tabs_to_spaces() {
    let mut t = TextArea::new();
    assert_eq!(t.tab_width(), 4);
    t.insert_str("a\tb");
    assert_eq!(t.text(), "a    b");
    assert_eq!(t.cursor(), 6);
    assert_eq!(t.display_width_of_range(0, t.text().len()), 6);

    let mut t2 = TextArea::new();
    t2.set_tab_width(8);
    t2.insert_str("x\ty");
    assert_eq!(t2.text(), "x        y");
    assert_eq!(t2.cursor(), 10);
    assert_eq!(t2.display_width_of_range(0, t2.text().len()), 10);

    let mut t3 = TextArea::new();
    t3.insert_str("\ta");
    assert_eq!(t3.text(), "    a");
    t3.set_text("");
    t3.insert_str("a\t");
    assert_eq!(t3.text(), "a    ");
    assert_eq!(t3.cursor(), 5);
    t3.set_text("");
    t3.insert_str("\t\t");
    assert_eq!(t3.text(), "        ");
    assert_eq!(t3.display_width_of_range(0, 8), 8);
    t3.insert_str("");
    assert_eq!(t3.text(), "        ");
    t3.insert_str_at(0, "z\t");
    assert_eq!(t3.text(), "z            ");
}

#[test]
fn set_text_and_replace_expand_tabs() {
    let mut t = TextArea::new();
    t.set_text("col1\tcol2");
    assert_eq!(t.text(), "col1    col2");

    t.replace_range(4..8, "\t");
    assert_eq!(t.text(), "col1    col2");

    t.replace_range(4..4, "\tx");
    assert_eq!(t.text(), "col1    x    col2");
    // Insert-only replace places cursor at end of inserted expansion (4 spaces + 'x').
    assert_eq!(&t.text()[4..9], "    x");

    let mut t0 = TextArea::new();
    t0.set_tab_width(0);
    t0.insert_str("a\tb");
    assert_eq!(t0.text(), "a\tb");
    t0.set_text("x\ty");
    assert_eq!(t0.text(), "x\ty");
    // Passthrough: display width matches unicode-width (no expansion).
    assert_eq!(
        t0.display_width_of_range(0, t0.text().len()),
        "x\ty".width()
    );
    t0.set_cursor(t0.text().len());
    let area = Rect::new(0, 0, 80, 1);
    let (x, _y) = t0.cursor_pos(area).unwrap();
    assert_eq!(x as usize, "x\ty".width());
}

#[test]
fn remaining_tabs_count_in_display_width_and_cursor() {
    // Simulate leftover tabs without going through expand (tab_width set after set_text
    // would still expand on set_text; inject via tab_width=0 then enable display tabs).
    let mut t = TextArea::new();
    t.set_tab_width(0);
    t.set_text("a\tb\tc");
    assert!(t.text().contains('\t'));
    t.set_tab_width(4);
    // "a" + 4 + "b" + 4 + "c" = 11
    assert_eq!(t.display_width_of_range(0, t.text().len()), 11);
    t.set_cursor(t.text().len());
    let area = Rect::new(0, 0, 80, 1);
    let (x, _y) = t.cursor_pos(area).unwrap();
    assert_eq!(x, 11);
}

#[test]
fn set_tab_width_does_not_rewrite_existing_spaces() {
    let mut t = TextArea::new();
    t.insert_str("a\tb");
    assert_eq!(t.text(), "a    b");
    t.set_tab_width(8);
    assert_eq!(t.text(), "a    b");
    t.insert_str("\tc");
    assert_eq!(t.text(), "a    b        c");
}

#[test]
fn multi_column_paste_tabs_readable() {
    let mut t = TextArea::new();
    t.insert_str("Name\tAge\tCity\nAda\t36\tLondon");
    assert_eq!(t.text(), "Name    Age    City\nAda    36    London");
    assert_eq!(t.cursor(), t.text().len());
    let end = t.text().len();
    let area = Rect::new(0, 0, 80, 3);
    let (x, _y) = t.cursor_pos(area).unwrap();
    // Ada(3) + 4 + 36(2) + 4 + London(6) = 19
    assert_eq!(x, 19);
    let bol = t.text().rfind('\n').map(|i| i + 1).unwrap_or(0);
    assert_eq!(x as usize, t.display_width_of_range(bol, end));
    let last_line = &t.text()[bol..];
    let (paint, paint_w) = paint_plain_for_display(last_line, 80, 4);
    assert_eq!(paint.as_ref(), last_line);
    assert_eq!(paint_w, 19);
}

#[test]
fn insert_element_expands_tabs_and_covers_full_range() {
    let mut t = TextArea::new();
    t.insert_element("a\tb", ElementKind(0), None);
    assert_eq!(t.text(), "a    b");
    assert_eq!(t.elements().len(), 1);
    assert_eq!(t.elements()[0].range, 0..6);
    assert_eq!(t.cursor(), 6);

    let mut t2 = TextArea::new();
    t2.insert_element("a\tb\nc\td", ElementKind(1), Some(Line::from("[P]")));
    assert_eq!(t2.text(), "a    b\nc    d");
    assert_eq!(t2.elements()[0].range, 0..t2.text().len());
    assert_eq!(t2.cursor(), t2.text().len());
    assert!(!t2.text().contains('\t'));
}

#[test]
fn replace_range_with_element_expands_tabs() {
    let mut t = TextArea::new();
    t.insert_str("xx");
    t.replace_range_with_element(0..2, "a\tb", ElementKind(0), None);
    assert_eq!(t.text(), "a    b");
    assert_eq!(t.elements()[0].range, 0..6);
    assert_eq!(t.cursor(), 6);
}

#[test]
fn unicode_plus_tabs_expansion_and_residual() {
    let mut t = TextArea::new();
    t.insert_str("\tAge");
    // 名 is typically width 2; plus 4 spaces + Age
    assert_eq!(t.text(), "名    Age");
    assert_eq!(
        t.display_width_of_range(0, t.text().len()),
        "".width() + 4 + 3
    );
    t.set_cursor(t.text().len());
    let area = Rect::new(0, 0, 80, 1);
    let (x, _) = t.cursor_pos(area).unwrap();
    assert_eq!(x as usize, "".width() + 4 + 3);

    let mut t2 = TextArea::new();
    t2.set_tab_width(0);
    t2.set_text("😀\tb");
    t2.set_tab_width(4);
    let expected = "😀".width() + 4 + 1;
    assert_eq!(t2.display_width_of_range(0, t2.text().len()), expected);
    t2.set_cursor(t2.text().len());
    let (x, _) = t2.cursor_pos(area).unwrap();
    assert_eq!(x as usize, expected);
}

#[test]
fn tab_helpers_clip_and_paint() {
    assert_eq!(expand_tabs_with_width("a\tb", 4).as_ref(), "a    b");
    assert!(matches!(
        expand_tabs_with_width("a\tb", 0),
        std::borrow::Cow::Borrowed("a\tb")
    ));
    assert!(matches!(
        expand_tabs_with_width("ab", 4),
        std::borrow::Cow::Borrowed("ab")
    ));
    assert_eq!(plain_display_width_with_tab("a\tb\tc", 4), 11);
    assert_eq!(
        plain_display_width_with_tab("a\tb\tc", 0),
        "a\tb\tc".width()
    );
    assert_eq!(clip_str_to_display_width_with_tab("a\tb", 3, 4), "a");
    let (paint, w) = paint_plain_for_display("a\tb", 80, 4);
    assert_eq!(paint.as_ref(), "a    b");
    assert_eq!(w, 6);
    let (paint2, w2) = paint_plain_for_display("a\tb", 3, 4);
    assert_eq!(paint2.as_ref(), "a");
    assert_eq!(w2, 1);
}

#[test]
fn display_width_of_range_with_display_element() {
    let mut t = TextArea::new();
    t.insert_str("ab");
    // Element has 100 bytes of buffer text but displays as "[P]" (3 chars)
    let buffer_text = "x".repeat(100);
    let display = Line::from("[P]");
    t.insert_element(&buffer_text, ElementKind(0), Some(display));
    t.insert_str("cd");

    // Range covering just "ab" = 2
    assert_eq!(t.display_width_of_range(0, 2), 2);
    // Range covering "ab" + element = 2 + 3 = 5
    assert_eq!(t.display_width_of_range(0, 102), 5);
    // Range covering "ab" + element + "cd" = 2 + 3 + 2 = 7
    assert_eq!(t.display_width_of_range(0, 104), 7);
    // Range covering just the element = 3
    assert_eq!(t.display_width_of_range(2, 102), 3);
    // Range covering just "cd" = 2
    assert_eq!(t.display_width_of_range(102, 104), 2);
}

#[test]
fn cursor_pos_uses_display_width() {
    let mut t = TextArea::new();
    t.insert_str("ab");
    let buffer_text = "x".repeat(50);
    let display = Line::from("[P]");
    t.insert_element(&buffer_text, ElementKind(0), Some(display));
    t.insert_str("cd");

    // Cursor at end of element (buffer pos 52)
    t.set_cursor(52);
    let area = Rect::new(0, 0, 80, 1);
    let (x, _y) = t.cursor_pos(area).unwrap();
    // Expected: "ab" (2) + "[P]" (3) = column 5
    assert_eq!(x, 5);

    // Cursor at end of text (buffer pos 54)
    t.set_cursor(54);
    let (x, _y) = t.cursor_pos(area).unwrap();
    // Expected: "ab" (2) + "[P]" (3) + "cd" (2) = column 7
    assert_eq!(x, 7);

    // Cursor at start
    t.set_cursor(0);
    let (x, _y) = t.cursor_pos(area).unwrap();
    assert_eq!(x, 0);
}

#[test]
fn display_width_no_elements() {
    let t = ta_with("abc");
    assert_eq!(t.display_width_of_range(0, 3), 3);
    assert_eq!(t.display_width_of_range(1, 2), 1);
    assert_eq!(t.display_width_of_range(3, 3), 0);
}

#[test]
fn display_width_element_without_display() {
    let mut t = TextArea::new();
    t.insert_element("elem", ElementKind(0), None);
    // No display override — width should equal buffer text width
    assert_eq!(t.display_width_of_range(0, 4), 4);
}

// ===== Wide unicode in display text =====

#[test]
fn display_width_with_wide_unicode_display() {
    let mut t = TextArea::new();
    t.insert_str("ab");
    // Display text has emoji (each width 2) and CJK
    let display = Line::from("📎漢字"); // 2 + 2 + 2 = 6 display columns
    t.insert_element("raw", ElementKind(0), Some(display));
    t.insert_str("cd");

    // "ab" = 2, element display = 6, "cd" = 2 → total 10
    assert_eq!(t.display_width_of_range(0, 2), 2);
    assert_eq!(t.display_width_of_range(2, 5), 6); // element "raw" = 3 bytes
    assert_eq!(t.display_width_of_range(0, 7), 10); // "ab" + elem + "cd"
}

#[test]
fn cursor_pos_with_wide_unicode_display() {
    let mut t = TextArea::new();
    t.insert_str("a");
    // Element display is "🚀" (width 2), buffer text is "xyz" (3 bytes)
    let display = Line::from("🚀");
    t.insert_element("xyz", ElementKind(0), Some(display));
    t.insert_str("b");

    let area = Rect::new(0, 0, 40, 1);

    // Cursor after "a" (at element start → element_at_cursor returns it)
    t.set_cursor(1);
    let (x, _) = t.cursor_pos(area).unwrap();
    assert_eq!(x, 1); // "a" = 1 col

    // Cursor after element (buffer pos 4 = 1 + 3)
    t.set_cursor(4);
    let (x, _) = t.cursor_pos(area).unwrap();
    assert_eq!(x, 3); // "a" (1) + "🚀" (2) = 3

    // Cursor at end "b" (buffer pos 5)
    t.set_cursor(5);
    let (x, _) = t.cursor_pos(area).unwrap();
    assert_eq!(x, 4); // "a" (1) + "🚀" (2) + "b" (1) = 4
}

#[test]
fn truncate_display_with_wide_unicode() {
    // Display: "📎paste" = 2+5 = 7 cols, truncate to 5
    let line: Line<'static> = Line::from("📎paste");
    let result = truncate_line_display(&line, 5);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    // Budget = 5 - 1 (ellipsis) = 4 content cols → "📎pa" (2+1+1=4)
    assert!(text.contains(''));
    assert!(
        result.width() <= 5,
        "width should be <= 5, got {}",
        result.width()
    );
}

#[test]
fn clip_str_to_display_width_preserves_zwj_graphemes() {
    let s = "👩\u{200D}💻a";

    assert_eq!(clip_str_to_display_width(s, 0), "");
    assert_eq!(clip_str_to_display_width(s, 1), "");
    assert_eq!(clip_str_to_display_width(s, 2), "👩\u{200D}💻");
    assert_eq!(clip_str_to_display_width(s, 3), s);
}

#[test]
fn truncate_display_preserves_zwj_graphemes() {
    let line: Line<'static> = Line::from("👩\u{200D}💻abc");
    let result = truncate_line_display(&line, 3);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();

    assert_eq!(text, "👩\u{200D}💻…");
    assert_eq!(result.width(), 3);
}

#[test]
fn truncate_display_wide_char_at_boundary() {
    // Display: "ab🚀cd" = 2+2+2 = 6 cols, truncate to 4
    // Budget = 4 - 1 = 3 content cols. "ab" = 2, "🚀" = 2 → doesn't fit → "ab…"
    let line: Line<'static> = Line::from("ab🚀cd");
    let result = truncate_line_display(&line, 4);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    assert_eq!(text, "ab…");
    assert!(result.width() <= 4);
}

#[test]
fn truncate_display_bracket_with_wide_chars() {
    // "[📎 pasted]" = 1+2+1+6+1 = 11 cols, truncate to 7
    // Budget = 7 - 2 (ellipsis + bracket) = 5 content cols → "[📎 p…]"
    let line: Line<'static> = Line::from("[📎 pasted]");
    let result = truncate_line_display(&line, 7);
    let text: String = result.spans.iter().map(|s| s.content.as_ref()).collect();
    assert!(text.ends_with(']'), "should preserve ]: got {text:?}");
    assert!(text.contains(''));
    assert!(result.width() <= 7, "got {}", result.width());
}

#[test]
fn render_element_with_wide_unicode_display() {
    let mut t = TextArea::new();
    let display = Line::from("📎漢");
    t.insert_element("hidden text", ElementKind(0), Some(display));

    let area = Rect::new(0, 0, 20, 1);
    let mut buf = Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    // Should show "📎漢" (4 display cols: 2+2)
    let cell0 = buf.cell((0, 0)).unwrap();
    assert_eq!(cell0.symbol(), "📎");
    let cell2 = buf.cell((2, 0)).unwrap();
    assert_eq!(cell2.symbol(), "");
}

// ===== Element-aware editing behavior (explicit tests) =====

#[test]
fn backspace_at_element_end_deletes_entire_element() {
    let mut t = TextArea::new();
    t.insert_str("before ");
    t.insert_element("[paste]", ElementKind(0), None);
    // Cursor is now at end of element
    assert_eq!(t.cursor(), 14); // "before " (7) + "[paste]" (7)

    t.delete_backward(1);
    assert_eq!(t.text(), "before ");
    assert_eq!(t.cursor(), 7);
    assert!(t.elements().is_empty());
}

#[test]
fn delete_at_element_start_deletes_entire_element() {
    let mut t = TextArea::new();
    t.insert_element("[paste]", ElementKind(0), None);
    t.insert_str(" after");
    t.set_cursor(0);

    t.delete_forward(1);
    assert_eq!(t.text(), " after");
    assert_eq!(t.cursor(), 0);
    assert!(t.elements().is_empty());
}

#[test]
fn left_right_navigation_jumps_over_element() {
    let mut t = TextArea::new();
    t.insert_str("a");
    t.insert_element("[elem]", ElementKind(0), None);
    t.insert_str("b");
    // text = "a[elem]b", element at 1..7

    // Start at end, move left: should jump from 8 → 7 (before 'b'),
    // then 7 → 1 (before element, atomic jump), then 1 → 0
    t.set_cursor(8);
    t.move_cursor_left(); // 8 → 7
    assert_eq!(t.cursor(), 7);
    t.move_cursor_left(); // 7 → 1 (atomic jump over "[elem]")
    assert_eq!(t.cursor(), 1);
    t.move_cursor_left(); // 1 → 0
    assert_eq!(t.cursor(), 0);

    // Now right: 0 → 1, then 1 → 7 (atomic jump), then 7 → 8
    t.move_cursor_right(); // 0 → 1
    assert_eq!(t.cursor(), 1);
    t.move_cursor_right(); // 1 → 7 (atomic jump over "[elem]")
    assert_eq!(t.cursor(), 7);
    t.move_cursor_right(); // 7 → 8
    assert_eq!(t.cursor(), 8);
}

#[test]
fn word_delete_backward_removes_element_atomically() {
    let mut t = TextArea::new();
    t.insert_str("prefix ");
    t.insert_element("[pasted content]", ElementKind(0), None);
    // Cursor at end of element
    assert_eq!(t.cursor(), 23); // 7 + 16

    t.delete_backward_word();
    // Should remove the entire element (it's one "word" unit)
    assert_eq!(t.text(), "prefix ");
    assert!(t.elements().is_empty());
}

#[test]
fn word_delete_forward_removes_element_atomically() {
    let mut t = TextArea::new();
    t.insert_element("[element]", ElementKind(0), None);
    t.insert_str(" suffix");
    t.set_cursor(0);

    t.delete_forward_word();
    assert_eq!(t.text(), " suffix");
    assert!(t.elements().is_empty());
}

#[test]
fn kill_to_eol_removes_element_in_range() {
    let mut t = TextArea::new();
    t.insert_str("start ");
    t.insert_element("[elem]", ElementKind(0), None);
    t.insert_str(" end");
    t.set_cursor(6); // right after "start "

    t.kill_to_end_of_line();
    assert_eq!(t.text(), "start ");
    assert!(t.elements().is_empty());
}

// ===== Element newline skipping in BOL/EOL =====
//
// Elements with multi-line buffer text (e.g. paste blocks) should be
// treated as atomic for line navigation. Newlines inside elements are
// NOT line boundaries.

#[test]
fn ctrl_e_skips_newline_inside_element() {
    // "foo <element:line1\nline2> bar"
    // Ctrl-E from start should go to end of the whole line, not stop
    // at the \n inside the element.
    let mut t = TextArea::new();
    t.insert_str("foo ");
    t.insert_element("line1\nline2", ElementKind(1), None);
    t.insert_str(" bar");
    // buffer = "foo line1\nline2 bar" (19 bytes), element at 4..15

    t.set_cursor(0);
    t.move_cursor_to_end_of_line(false);
    assert_eq!(t.cursor(), t.text().len()); // should reach end of "foo ... bar"
}

#[test]
fn ctrl_a_skips_newline_inside_element() {
    let mut t = TextArea::new();
    t.insert_str("foo ");
    t.insert_element("line1\nline2", ElementKind(1), None);
    t.insert_str(" bar");
    // buffer = "foo line1\nline2 bar" (19 bytes)

    // Set cursor to end
    t.set_cursor(t.text().len());
    t.move_cursor_to_beginning_of_line(false);
    assert_eq!(t.cursor(), 0); // should reach beginning, not stop inside element
}

#[test]
fn ctrl_e_from_element_boundary_skips_to_real_eol() {
    let mut t = TextArea::new();
    t.insert_str("foo ");
    t.insert_element("a\nb\nc", ElementKind(1), None);
    t.insert_str(" bar");
    // element at 4..9

    // Place cursor at element start boundary
    t.set_cursor(4);
    t.move_cursor_to_end_of_line(false);
    assert_eq!(t.cursor(), t.text().len());
}

#[test]
fn ctrl_a_from_after_element_skips_to_real_bol() {
    let mut t = TextArea::new();
    t.insert_str("foo ");
    t.insert_element("a\nb\nc", ElementKind(1), None);
    t.insert_str(" bar");
    // element at 4..9, " bar" at 9..13

    // Place cursor on " bar"
    t.set_cursor(10);
    t.move_cursor_to_beginning_of_line(false);
    assert_eq!(t.cursor(), 0);
}

#[test]
fn kill_to_eol_with_multiline_element() {
    let mut t = TextArea::new();
    t.insert_str("foo ");
    t.insert_element("x\ny\nz", ElementKind(1), None);
    t.insert_str(" bar");
    // buffer = "foo x\ny\nz bar", element at 4..9

    t.set_cursor(0);
    t.kill_to_end_of_line();
    // Should kill everything on the line: "foo " + element + " bar"
    assert_eq!(t.text(), "");
}

#[test]
fn kill_to_bol_with_multiline_element() {
    let mut t = TextArea::new();
    t.insert_str("foo ");
    t.insert_element("x\ny\nz", ElementKind(1), None);
    t.insert_str(" bar");

    t.set_cursor(t.text().len()); // end of " bar"
    t.kill_to_beginning_of_line();
    assert_eq!(t.text(), "");
}

#[test]
fn bol_eol_with_real_newline_and_element() {
    // "hello\nfoo <element:a\nb> bar"
    // Two real lines. Element is on the second line.
    let mut t = TextArea::new();
    t.insert_str("hello\nfoo ");
    t.insert_element("a\nb", ElementKind(1), None);
    t.insert_str(" bar");
    // buffer = "hello\nfoo a\nb bar"
    // Real newline at 5. Element at 10..13. Element's \n at 11 should be skipped.

    // From start of second line (pos 6), Ctrl-E should reach end
    t.set_cursor(6);
    t.move_cursor_to_end_of_line(false);
    assert_eq!(t.cursor(), t.text().len());

    // From end, Ctrl-A should go back to pos 6
    t.move_cursor_to_beginning_of_line(false);
    assert_eq!(t.cursor(), 6);
}

#[test]
fn bol_eol_no_element_unchanged() {
    // Verify the fix doesn't break normal (no-element) behavior.
    let mut t = TextArea::new();
    t.insert_str("line1\nline2\nline3");

    t.set_cursor(6); // start of "line2"
    t.move_cursor_to_end_of_line(false);
    assert_eq!(t.cursor(), 11); // end of "line2" (before \n)

    t.move_cursor_to_beginning_of_line(false);
    assert_eq!(t.cursor(), 6);
}

// ===== Element-display-aware wrapping =====

#[test]
fn wrapping_uses_element_display_width() {
    // Scenario: "foo bar " + element("Clean build", display 28 cols) at width 20.
    // Buffer text: "foo bar Clean build" = 19 buffer cols → textwrap says it fits on one line.
    // Display text: "foo bar [📎 Pasted 1 line, 11 chars]" = 8 + 28 = 36 display cols → should wrap.
    let mut t = TextArea::new();
    t.insert_str("foo bar ");
    let display = Line::from("[📎 Pasted 1 line, 11 chars]"); // 28 display cols
    t.insert_element("Clean build", ElementKind(0), Some(display));
    // buffer = "foo bar Clean build" (19 bytes)

    let lines = t.wrapped_lines(20);
    // The element display (28 cols) doesn't fit after "foo bar " (8 cols) on a 20-col line.
    // But it DOES fit on a fresh 20-col line (28 > 20, so it overflows but gets its own line).
    // Expected: line 1 = "foo bar ", line 2 = element.
    assert!(
        lines.len() >= 2,
        "Expected wrapping to produce at least 2 lines, got {} lines. \
         Line ranges: {:?}",
        lines.len(),
        &*lines,
    );
}

#[test]
fn wrapping_element_fits_on_next_line() {
    // Element display (10 cols) doesn't fit after "hello " (6 cols) on 12-col line,
    // but fits on a fresh line.
    let mut t = TextArea::new();
    t.insert_str("hello ");
    let display = Line::from("[Pasted!]"); // 9 display cols
    t.insert_element("xy", ElementKind(0), Some(display));
    t.insert_str(" z");
    // buffer: "hello xy z" (10 bytes)
    // display: "hello [Pasted!] z" = 6 + 9 + 2 = 17 display cols

    let lines = t.wrapped_lines(12);
    // Line 1: "hello " (6 cols, fits)
    // Line 2: "[Pasted!] z" (9 + 2 = 11 cols, fits in 12)
    assert_eq!(
        lines.len(),
        2,
        "Expected 2 wrapped lines, got {}. Ranges: {:?}",
        lines.len(),
        &*lines,
    );
}

#[test]
fn wrapping_element_without_display_uses_buffer_width() {
    // Element without display override: wrapping should use buffer text width (unchanged behavior).
    let mut t = TextArea::new();
    t.insert_str("hello ");
    t.insert_element("xy", ElementKind(0), None);
    t.insert_str(" z");
    // buffer: "hello xy z" (10 bytes), no display override
    // display = buffer = "hello xy z" = 10 cols

    let lines = t.wrapped_lines(12);
    // 10 cols fits on 12-col line → 1 line
    assert_eq!(lines.len(), 1);
}

#[test]
fn wrapping_element_display_renders_on_correct_lines() {
    // End-to-end: wrapping + rendering with display element.
    // "abc " (4) + element("xy", display="[ELEM]" = 6 cols) + " d" (2)
    // At width 8: "abc " (4) + "[ELEM]" (6) = 10 > 8 → wrap before element
    // Line 1: "abc " (4 cols), Line 2: "[ELEM] d" (8 cols)
    let mut t = TextArea::new();
    t.insert_str("abc ");
    let display = Line::from("[ELEM]");
    t.insert_element("xy", ElementKind(0), Some(display));
    t.insert_str(" d");
    // buffer: "abc xy d" (8 bytes)

    // Check wrapping (drop the Ref before rendering)
    {
        let lines = t.wrapped_lines(8);
        assert_eq!(
            lines.len(),
            2,
            "Should wrap into 2 lines, got {:?}",
            &*lines
        );
    }

    // Render and verify
    let area = Rect::new(0, 0, 8, 2);
    let mut buf = Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    // Line 1 (y=0): "abc " padded to 8
    assert_eq!(buf.cell((0, 0)).unwrap().symbol(), "a");
    assert_eq!(buf.cell((1, 0)).unwrap().symbol(), "b");
    assert_eq!(buf.cell((2, 0)).unwrap().symbol(), "c");
    assert_eq!(buf.cell((3, 0)).unwrap().symbol(), " ");

    // Line 2 (y=1): "[ELEM] d"
    assert_eq!(buf.cell((0, 1)).unwrap().symbol(), "[");
    assert_eq!(buf.cell((1, 1)).unwrap().symbol(), "E");
    assert_eq!(buf.cell((5, 1)).unwrap().symbol(), "]");
    assert_eq!(buf.cell((6, 1)).unwrap().symbol(), " ");
    assert_eq!(buf.cell((7, 1)).unwrap().symbol(), "d");
}

#[test]
fn wrapping_element_with_newlines_stays_single_line() {
    // When an element's buffer text contains \n, wrapping must NOT split at those
    // newlines. The element's display is a single-line chip; the \n is internal.
    // Scenario: "hello " + element("line1\nline2\nline3", display="[paste]") + " world"
    // Buffer: "hello line1\nline2\nline3 world"  (contains \n inside element)
    // Display: "hello [paste] world" = 6 + 7 + 6 = 19 cols
    // At width 40: should be 1 visual line.
    let mut t = TextArea::new();
    t.insert_str("hello ");
    let display = Line::from("[paste]"); // 7 display cols
    t.insert_element("line1\nline2\nline3", ElementKind(0), Some(display));
    t.insert_str(" world");
    // buffer: "hello line1\nline2\nline3 world"

    let lines = t.wrapped_lines(40);
    assert_eq!(
        lines.len(),
        1,
        "Element with internal \\n should NOT create extra visual lines. \
         Got {} lines: {:?}",
        lines.len(),
        &*lines,
    );
}

#[test]
fn cursor_pos_after_multiline_element() {
    // After inserting text after a multiline element, the cursor should be on the
    // same visual line as the element chip, not bumped down by internal newlines.
    let mut t = TextArea::new();
    t.insert_str("hello ");
    let display = Line::from("[paste]"); // 7 display cols
    t.insert_element("line1\nline2", ElementKind(0), Some(display));
    t.insert_str(" world");

    let area = Rect::new(0, 0, 80, 10);
    let pos = t.cursor_pos(area);
    assert_eq!(
        pos,
        Some((19, 0)), // 6 + 7 + 6 = 19, row 0
        "Cursor should be at col 19, row 0 after multiline element. \
         Got {:?}. Buffer: {:?}, cursor byte: {}",
        pos,
        t.text(),
        t.cursor(),
    );
}

#[test]
fn yank_restores_last_kill() {
    let mut t = ta_with("hello");
    t.set_cursor(0);
    t.kill_to_end_of_line();
    assert_eq!(t.text(), "");
    assert_eq!(t.cursor(), 0);

    t.yank();
    assert_eq!(t.text(), "hello");
    assert_eq!(t.cursor(), 5);

    let mut t = ta_with("hello world");
    t.set_cursor(t.text().len());
    t.delete_backward_word();
    assert_eq!(t.text(), "hello ");
    assert_eq!(t.cursor(), 6);

    t.yank();
    assert_eq!(t.text(), "hello world");
    assert_eq!(t.cursor(), 11);

    let mut t = ta_with("hello");
    t.set_cursor(5);
    t.kill_to_beginning_of_line();
    assert_eq!(t.text(), "");
    assert_eq!(t.cursor(), 0);

    t.yank();
    assert_eq!(t.text(), "hello");
    assert_eq!(t.cursor(), 5);
}

#[test]
fn no_op_kill_preserves_the_kill_buffer() {
    let mut textarea = ta_with("hello");
    textarea.set_cursor(0);
    textarea.kill_to_end_of_line();
    assert_eq!(textarea.kill_buffer, "hello");

    textarea.set_text("world");
    textarea.set_cursor(textarea.text().len());
    textarea.kill_to_end_of_line();
    textarea.yank();
    assert_eq!(textarea.text(), "worldhello");
}

#[test]
fn kill_buffer_survives_set_text() {
    // A cut must outlive the buffer reset that send does via set_text("").
    let mut t = ta_with("hello");
    t.set_cursor(0);
    t.kill_to_end_of_line();
    assert_eq!(t.text(), "");

    t.set_text(""); // send resets the prompt
    assert_eq!(t.text(), "");

    t.yank();
    assert_eq!(t.text(), "hello");
    assert_eq!(t.cursor(), 5);
}

#[test]
fn cursor_left_and_right_handle_graphemes() {
    let mut t = ta_with("a👍b");
    t.set_cursor(t.text().len());

    t.move_cursor_left(); // before 'b'
    let after_first_left = t.cursor();
    t.move_cursor_left(); // before '👍'
    let after_second_left = t.cursor();
    t.move_cursor_left(); // before 'a'
    let after_third_left = t.cursor();

    assert!(after_first_left < t.text().len());
    assert!(after_second_left < after_first_left);
    assert!(after_third_left < after_second_left);

    // Move right back to end safely
    t.move_cursor_right();
    t.move_cursor_right();
    t.move_cursor_right();
    assert_eq!(t.cursor(), t.text().len());
}

#[test]
fn control_b_and_f_move_cursor() {
    let mut t = ta_with("abcd");
    t.set_cursor(1);

    t.input(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL));
    assert_eq!(t.cursor(), 2);

    t.input(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    assert_eq!(t.cursor(), 1);
}

#[test]
fn control_b_f_fallback_control_chars_move_cursor() {
    let mut t = ta_with("abcd");
    t.set_cursor(2);

    // Simulate terminals that send C0 control chars without CONTROL modifier.
    // ^B (U+0002) should move left
    t.input(KeyEvent::new(KeyCode::Char('\u{0002}'), KeyModifiers::NONE));
    assert_eq!(t.cursor(), 1);

    // ^F (U+0006) should move right
    t.input(KeyEvent::new(KeyCode::Char('\u{0006}'), KeyModifiers::NONE));
    assert_eq!(t.cursor(), 2);
}

/// Regression (user report): Ctrl+W must rubout to whitespace, not stop
/// at punctuation.
#[test]
fn ctrl_w_unix_word_rubout_deletes_to_whitespace() {
    let mut t = ta_with("git commit -m hello-world");
    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
    assert_eq!(t.text(), "git commit -m ");
    t.input(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL));
    assert_eq!(t.text(), "git commit ");
}

#[test]
fn unix_word_rubout_whitespace_runs_paths_and_edges() {
    let mut t = ta_with("cat path/to/file.rs   ");
    t.set_cursor(t.text().len());
    t.delete_backward_unix_word();
    assert_eq!(t.text(), "cat ");
    assert_eq!(t.cursor(), 4);

    let mut t = ta_with("foo bar-baz");
    t.set_cursor(7); // foo bar|-baz
    t.delete_backward_unix_word();
    assert_eq!(t.text(), "foo -baz");
    assert_eq!(t.cursor(), 4);

    // Newlines are whitespace: rubout crosses line boundaries.
    let mut t = ta_with("line1\nword  ");
    t.set_cursor(t.text().len());
    t.delete_backward_unix_word();
    assert_eq!(t.text(), "line1\n");

    let mut t = ta_with("");
    t.delete_backward_unix_word();
    assert_eq!(t.text(), "");
    let mut t = ta_with("   ");
    t.set_cursor(3);
    t.delete_backward_unix_word();
    assert_eq!(t.text(), "");
}

/// Readline parity: only C-w is whitespace-delimited; M-DEL/C-Backspace
/// stay chunked.
#[test]
fn alt_backspace_keeps_word_chunk_semantics() {
    let mut t = ta_with("hello-world");
    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::ALT));
    assert_eq!(t.text(), "hello-");

    let mut t = ta_with("hello-world");
    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL));
    assert_eq!(t.text(), "hello-");
}

#[test]
fn delete_backward_word_alt_keys() {
    // Test the custom Alt+Ctrl+h binding
    let mut t = ta_with("hello world");
    t.set_cursor(t.text().len()); // cursor at the end
    t.input(KeyEvent::new(
        KeyCode::Char('h'),
        KeyModifiers::CONTROL | KeyModifiers::ALT,
    ));
    assert_eq!(t.text(), "hello ");
    assert_eq!(t.cursor(), 6);

    // Test the standard Alt+Backspace binding
    let mut t = ta_with("hello world");
    t.set_cursor(t.text().len()); // cursor at the end
    t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::ALT));
    assert_eq!(t.text(), "hello ");
    assert_eq!(t.cursor(), 6);
}

#[test]
fn ctrl_backspace_deletes_backward_word() {
    let mut t = ta_with("hello world");
    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL));
    assert_eq!(t.text(), "hello ");
    assert_eq!(t.cursor(), 6);

    // From end of middle word: deletes "bar", leaves surrounding spaces
    let mut t = ta_with("foo bar baz");
    t.set_cursor(7); // after "bar"
    t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL));
    assert_eq!(t.text(), "foo  baz");
    assert_eq!(t.cursor(), 4);
}

#[test]
fn ctrl_delete_deletes_forward_word() {
    // Mirror of ctrl_backspace_deletes_backward_word.
    let mut t = ta_with("hello world");
    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Delete, KeyModifiers::CONTROL));
    assert_eq!(t.text(), " world");
    assert_eq!(t.cursor(), 0);

    // From start of middle word: deletes "bar", leaves surrounding spaces
    let mut t = ta_with("foo bar baz");
    t.set_cursor(4); // before "bar"
    t.input(KeyEvent::new(KeyCode::Delete, KeyModifiers::CONTROL));
    assert_eq!(t.text(), "foo  baz");
    assert_eq!(t.cursor(), 4);
}

#[test]
fn delete_backward_word_handles_narrow_no_break_space() {
    let mut t = ta_with("32\u{202F}AM");
    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::ALT));
    pretty_assertions::assert_eq!(t.text(), "32\u{202F}");
    pretty_assertions::assert_eq!(t.cursor(), t.text().len());
}

#[test]
fn delete_forward_word_with_without_alt_modifier() {
    let mut t = ta_with("hello world");
    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Delete, KeyModifiers::ALT));
    assert_eq!(t.text(), " world");
    assert_eq!(t.cursor(), 0);

    let mut t = ta_with("hello");
    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Delete, KeyModifiers::NONE));
    assert_eq!(t.text(), "ello");
    assert_eq!(t.cursor(), 0);
}

#[test]
fn alt_d_deletes_forward_word() {
    // Alt+D (Meta-d, Emacs) → delete forward word
    let mut t = ta_with("hello world foo");
    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::ALT));
    assert_eq!(t.text(), " world foo");
    assert_eq!(t.cursor(), 0);

    // Alt+D at a word boundary
    let mut t = ta_with("hello world");
    t.set_cursor(5); // cursor right after "hello"
    t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::ALT));
    assert_eq!(t.text(), "hello");
    assert_eq!(t.cursor(), 5);

    // Super+D (Cmd+D on macOS with Kitty protocol) also works
    let mut t = ta_with("hello world");
    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::SUPER));
    assert_eq!(t.text(), " world");
    assert_eq!(t.cursor(), 0);
}

#[test]
fn ctrl_p_moves_cursor_up() {
    let mut t = ta_with("first\nsecond\nthird");
    let second_line_start = 6; // after "first\n"
    t.set_cursor(second_line_start + 2); // middle of "second"
    t.input(KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL));
    // Should be on first line now
    assert!(t.cursor() < second_line_start);
}

#[test]
fn ctrl_n_moves_cursor_down() {
    let mut t = ta_with("first\nsecond\nthird");
    t.set_cursor(2); // middle of "first"
    t.input(KeyEvent::new(KeyCode::Char('n'), KeyModifiers::CONTROL));
    let second_line_start = 6;
    // Should be on second line now
    assert!(t.cursor() >= second_line_start);
}

#[test]
fn control_h_backspace() {
    // Test Ctrl+H as backspace
    let mut t = ta_with("12345");
    t.set_cursor(3); // cursor after '3'
    t.input(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::CONTROL));
    assert_eq!(t.text(), "1245");
    assert_eq!(t.cursor(), 2);

    // Test Ctrl+H at beginning (should be no-op)
    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::CONTROL));
    assert_eq!(t.text(), "1245");
    assert_eq!(t.cursor(), 0);

    // Test Ctrl+H at end
    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::CONTROL));
    assert_eq!(t.text(), "124");
    assert_eq!(t.cursor(), 3);
}
#[test]
fn char_bs_backspace() {
    // Test Char('\x08') (BS) as backspace
    let mut t = ta_with("12345");
    t.set_cursor(3); // cursor after '3'
    t.input(KeyEvent::new(KeyCode::Char('\x08'), KeyModifiers::NONE));
    assert_eq!(t.text(), "1245");
    assert_eq!(t.cursor(), 2);
}

#[test]
fn char_del_deletes_backward() {
    // Char('\x7f') (DEL) should delete backward — on Unix terminals,
    // Backspace sends 0x7F in legacy mode (no Kitty protocol).
    let mut t = ta_with("12345");
    t.set_cursor(2); // cursor after '2'
    t.input(KeyEvent::new(KeyCode::Char('\x7f'), KeyModifiers::NONE));
    assert_eq!(t.text(), "1345");
    assert_eq!(t.cursor(), 1);
}

#[test]
fn raw_delete_chars_ignore_stray_modifiers() {
    for raw in ['\u{0008}', '\u{007f}'] {
        for modifiers in [
            KeyModifiers::ALT,
            KeyModifiers::CONTROL,
            KeyModifiers::SUPER,
            KeyModifiers::ALT | KeyModifiers::CONTROL,
        ] {
            let mut t = ta_with("alpha beta");
            t.input(KeyEvent::new(KeyCode::Char(raw), modifiers));
            assert_eq!(
                t.text(),
                "alpha bet",
                "raw {raw:?} with {modifiers:?} must delete one grapheme",
            );
        }
    }
}

#[test]
fn del_char_treated_as_backspace() {
    // When Kitty keyboard protocol gets silently popped, Backspace can
    // arrive as raw DEL (0x7F) instead of KeyCode::Backspace. Ensure it
    // deletes backward instead of inserting an invisible character.
    let mut t = ta_with("hello");
    t.set_cursor(3);
    t.input(KeyEvent::new(KeyCode::Char('\u{007f}'), KeyModifiers::NONE));
    assert_eq!(t.text(), "helo");
    assert_eq!(t.cursor(), 2);

    // At beginning: no-op
    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Char('\u{007f}'), KeyModifiers::NONE));
    assert_eq!(t.text(), "helo");
    assert_eq!(t.cursor(), 0);

    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(KeyCode::Char('\u{007f}'), KeyModifiers::ALT));
    assert_eq!(t.text(), "hel");
    assert_eq!(t.cursor(), 3);
}

#[test]
fn bs_char_treated_as_backspace() {
    // BS (0x08) arriving as Char without CONTROL modifier should also
    // delete backward (Ctrl-H without the modifier flag).
    let mut t = ta_with("abcde");
    t.set_cursor(4);
    t.input(KeyEvent::new(KeyCode::Char('\u{0008}'), KeyModifiers::NONE));
    assert_eq!(t.text(), "abce");
    assert_eq!(t.cursor(), 3);
}

#[test]
fn del_char_with_selection_deletes_selection() {
    // DEL (0x7F) arriving as Char with an active selection should delete
    // the selection cleanly, not insert an invisible control character.
    let mut t = ta_with("hello world");
    t.set_selection(0, 5);
    t.input(KeyEvent::new(KeyCode::Char('\u{007f}'), KeyModifiers::NONE));
    assert_eq!(t.text(), " world");
    assert_eq!(t.cursor(), 0);
    assert!(t.selection_range().is_none());
}

#[test]
fn cursor_vertical_movement_across_lines_and_bounds() {
    let mut t = ta_with("short\nloooooooooong\nmid");
    // Place cursor on second line, column 5
    let second_line_start = 6; // after first '\n'
    t.set_cursor(second_line_start + 5);

    // Move up: target column preserved, clamped by line length
    t.move_cursor_up();
    assert_eq!(t.cursor(), 5); // first line has len 5

    // Move up again goes to start of text
    t.move_cursor_up();
    assert_eq!(t.cursor(), 0);

    // Move down: from start to target col tracked
    t.move_cursor_down();
    // On first move down, we should land on second line, at col 0 (target col remembered as 0)
    let pos_after_down = t.cursor();
    assert!(pos_after_down >= second_line_start);

    // Move down again to third line; clamp to its length
    t.move_cursor_down();
    let third_line_start = t.text().find("mid").unwrap();
    let third_line_end = third_line_start + 3;
    assert!(t.cursor() >= third_line_start && t.cursor() <= third_line_end);

    // Moving down at last line jumps to end
    t.move_cursor_down();
    assert_eq!(t.cursor(), t.text().len());
}

#[test]
fn home_end_and_emacs_style_home_end() {
    let mut t = ta_with("one\ntwo\nthree");
    // Position at middle of second line
    let second_line_start = t.text().find("two").unwrap();
    t.set_cursor(second_line_start + 1);

    t.move_cursor_to_beginning_of_line(false);
    assert_eq!(t.cursor(), second_line_start);

    // Ctrl-A behavior: if at BOL, go to beginning of previous line
    t.move_cursor_to_beginning_of_line(true);
    assert_eq!(t.cursor(), 0); // beginning of first line

    // Move to EOL of first line
    t.move_cursor_to_end_of_line(false);
    assert_eq!(t.cursor(), 3);

    // Ctrl-E: if at EOL, go to end of next line
    t.move_cursor_to_end_of_line(true);
    // end of second line ("two") is right before its '\n'
    let end_second_nl = t.text().find("\nthree").unwrap();
    assert_eq!(t.cursor(), end_second_nl);
}

#[test]
fn home_end_use_logical_line_when_soft_wrapped() {
    // width 4 → "abcd" | "efgh" | "ij"
    let mut t = ta_with("abcdefghij");
    let _ = t.desired_height(4);
    t.set_cursor(6); // mid second visual row

    t.input(KeyEvent::new(KeyCode::Home, KeyModifiers::NONE));
    assert_eq!(t.cursor(), 0);
    t.input(KeyEvent::new(KeyCode::End, KeyModifiers::NONE));
    assert_eq!(t.cursor(), t.text().len());

    // Super+Left/Right stay on the visual wrap row.
    t.set_cursor(6);
    t.move_cursor_to_beginning_of_line(false);
    assert_eq!(t.cursor(), 4);
    t.move_cursor_to_end_of_line(false);
    assert_eq!(t.cursor(), 7);

    // Multiline: Home/End stay on this logical line, not wrap-row or buffer.
    let mut multi = ta_with("abcdefghij\nxyz");
    let _ = multi.desired_height(4);
    multi.set_cursor(6);
    multi.input(KeyEvent::new(KeyCode::Home, KeyModifiers::NONE));
    assert_eq!(multi.cursor(), 0);
    multi.set_cursor(6);
    multi.input(KeyEvent::new(KeyCode::End, KeyModifiers::NONE));
    assert_eq!(multi.cursor(), "abcdefghij".len());
    multi.set_cursor("abcdefghij\nxy".len());
    multi.input(KeyEvent::new(KeyCode::Home, KeyModifiers::NONE));
    assert_eq!(multi.cursor(), "abcdefghij\n".len());
    multi.input(KeyEvent::new(KeyCode::End, KeyModifiers::NONE));
    assert_eq!(multi.cursor(), multi.text().len());

    // Ctrl+A/E stay logical (and chain across lines).
    t.set_cursor(6);
    t.move_cursor_to_beginning_of_line(true);
    assert_eq!(t.cursor(), 0);
    t.set_cursor(6);
    t.move_cursor_to_end_of_line(true);
    assert_eq!(t.cursor(), t.text().len());
}

#[test]
fn end_of_line_or_down_at_end_of_text() {
    let mut t = ta_with("one\ntwo");
    // Place cursor at absolute end of the text
    t.set_cursor(t.text().len());
    // Should remain at end without panicking
    t.move_cursor_to_end_of_line(true);
    assert_eq!(t.cursor(), t.text().len());

    // Also verify behavior when at EOL of a non-final line:
    let eol_first_line = 3; // index of '\n' in "one\ntwo"
    t.set_cursor(eol_first_line);
    t.move_cursor_to_end_of_line(true);
    assert_eq!(t.cursor(), t.text().len()); // moves to end of next (last) line
}

#[test]
fn word_navigation_helpers() {
    let t = ta_with("  alpha  beta   gamma");
    let mut t = t; // make mutable for set_cursor
    // Put cursor after "alpha"
    let after_alpha = t.text().find("alpha").unwrap() + "alpha".len();
    t.set_cursor(after_alpha);
    assert_eq!(t.beginning_of_previous_word(), 2); // skip initial spaces

    // Put cursor at start of beta
    let beta_start = t.text().find("beta").unwrap();
    t.set_cursor(beta_start);
    assert_eq!(t.end_of_next_word(), beta_start + "beta".len());

    // If at end, end_of_next_word returns len
    t.set_cursor(t.text().len());
    assert_eq!(t.end_of_next_word(), t.text().len());
}

#[test]
fn word_navigation_splits_on_hyphen() {
    let mut t = ta_with("hello-world");
    let hyphen = t.text().find('-').unwrap();
    let after_hyphen = hyphen + 1;

    t.set_cursor(t.text().len());
    assert_eq!(t.beginning_of_previous_word(), after_hyphen);

    t.set_cursor(after_hyphen);
    assert_eq!(t.beginning_of_previous_word(), hyphen);

    t.set_cursor(hyphen);
    assert_eq!(t.beginning_of_previous_word(), 0);

    t.set_cursor(0);
    assert_eq!(t.end_of_next_word(), hyphen);

    t.set_cursor(hyphen);
    assert_eq!(t.end_of_next_word(), after_hyphen);

    t.set_cursor(after_hyphen);
    assert_eq!(t.end_of_next_word(), t.text().len());
}

#[test]
fn alt_arrow_navigation_splits_on_hyphen() {
    let mut t = ta_with("hello-world");
    let hyphen = t.text().find('-').unwrap();
    let after_hyphen = hyphen + 1;
    let end = t.text().len();

    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Right, KeyModifiers::ALT));
    assert_eq!(t.cursor(), hyphen);

    t.input(KeyEvent::new(KeyCode::Right, KeyModifiers::ALT));
    assert_eq!(t.cursor(), after_hyphen);

    t.input(KeyEvent::new(KeyCode::Right, KeyModifiers::ALT));
    assert_eq!(t.cursor(), end);

    t.input(KeyEvent::new(KeyCode::Left, KeyModifiers::ALT));
    assert_eq!(t.cursor(), after_hyphen);

    t.input(KeyEvent::new(KeyCode::Left, KeyModifiers::ALT));
    assert_eq!(t.cursor(), hyphen);

    t.input(KeyEvent::new(KeyCode::Left, KeyModifiers::ALT));
    assert_eq!(t.cursor(), 0);
}

#[test]
fn cursor_at_wrap_boundary_shows_on_next_line() {
    // When typing fills an entire line, the cursor sits at the exact wrap
    // boundary.  It should be reported on the *next* visual line at col 0,
    // not at col == width (which is the invisible right border).

    // Case 1: text exactly fills one line — cursor at text.len()
    let mut t = ta_with("abcde");
    let area = Rect::new(0, 0, 5, 3); // width 5
    t.set_cursor(5); // cursor right after 'e'

    let (x, y) = t.cursor_pos(area).unwrap();
    assert_eq!(x, 0, "cursor x should be 0 (start of virtual next line)");
    assert_eq!(y, 1, "cursor y should be 1 (next line)");

    // Case 2: text wraps — cursor at the boundary between two wrapped lines
    let mut t = ta_with("abcdefgh");
    let area = Rect::new(0, 0, 5, 3); // width 5, wraps after 'e'
    // cursor at position 5 = start of "fgh" = should be col 0, row 1
    t.set_cursor(5);

    let (x, y) = t.cursor_pos(area).unwrap();
    assert_eq!(x, 0, "cursor at wrap point should be col 0 of next line");
    assert_eq!(y, 1, "cursor at wrap point should be on second visual line");
}

#[test]
fn wrapping_and_cursor_positions() {
    let mut t = ta_with("hello world here");
    let area = Rect::new(0, 0, 6, 10); // width 6 -> wraps words
    // desired height counts wrapped lines
    assert!(t.desired_height(area.width) >= 3);

    // Place cursor in "world"
    let world_start = t.text().find("world").unwrap();
    t.set_cursor(world_start + 3);
    let (_x, y) = t.cursor_pos(area).unwrap();
    assert_eq!(y, 1); // world should be on second wrapped line

    // With state and small height, cursor is mapped onto visible row
    let mut state = TextAreaState::default();
    let small_area = Rect::new(0, 0, 6, 1);
    // First call: cursor not visible -> effective scroll ensures it is
    let (_x, y) = t.cursor_pos_with_state(small_area, state).unwrap();
    assert_eq!(y, 0);

    // Render with state to update actual scroll value
    let mut buf = Buffer::empty(small_area);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), small_area, &mut buf, &mut state);
    // After render, state.scroll should be adjusted so cursor row fits
    let effective_lines = t.desired_height(small_area.width);
    assert!(state.scroll < effective_lines);
}

#[test]
fn cursor_pos_with_state_basic_and_scroll_behaviors() {
    // Case 1: No wrapping needed, height fits — scroll ignored, y maps directly.
    let mut t = ta_with("hello world");
    t.set_cursor(3);
    let area = Rect::new(2, 5, 20, 3);
    // Even if an absurd scroll is provided, when content fits the area the
    // effective scroll is 0 and the cursor position matches cursor_pos.
    let bad_state = TextAreaState { scroll: 999 };
    let (x1, y1) = t.cursor_pos(area).unwrap();
    let (x2, y2) = t.cursor_pos_with_state(area, bad_state).unwrap();
    assert_eq!((x2, y2), (x1, y1));

    // Case 2: Cursor below the current window — y should be clamped to the
    // bottom row (area.height - 1) after adjusting effective scroll.
    let mut t = ta_with("one two three four five six");
    // Force wrapping to many visual lines.
    let wrap_width = 4;
    let _ = t.desired_height(wrap_width);
    // Put cursor somewhere near the end so it's definitely below the first window.
    t.set_cursor(t.text().len().saturating_sub(2));
    let small_area = Rect::new(0, 0, wrap_width, 2);
    let state = TextAreaState { scroll: 0 };
    let (_x, y) = t.cursor_pos_with_state(small_area, state).unwrap();
    assert_eq!(y, small_area.y + small_area.height - 1);

    // Case 3: Cursor above the current window — y should be top row (0)
    // when the provided scroll is too large.
    let mut t = ta_with("alpha beta gamma delta epsilon zeta");
    let wrap_width = 5;
    let lines = t.desired_height(wrap_width);
    // Place cursor near start so an excessive scroll moves it to top row.
    t.set_cursor(1);
    let area = Rect::new(0, 0, wrap_width, 3);
    let state = TextAreaState {
        scroll: lines.saturating_mul(2),
    };
    let (_x, y) = t.cursor_pos_with_state(area, state).unwrap();
    assert_eq!(y, area.y);
}

#[test]
fn screen_spans_of_range_single_row() {
    let t = ta_with("xy /model tail");
    let area = Rect::new(2, 1, 40, 3);
    let state = TextAreaState::default();

    // "/model" = bytes 3..9, all on the first visual row.
    let spans = t.screen_spans_of_range(3..9, area, state);
    assert_eq!(spans, vec![Rect::new(5, 1, 6, 1)]);

    // Degenerate ranges yield no spans.
    assert!(t.screen_spans_of_range(4..4, area, state).is_empty());
    assert!(t.screen_spans_of_range(4..999, area, state).is_empty());
}

#[test]
fn screen_spans_of_range_rejects_non_char_boundaries() {
    // 'é' spans bytes 1..3; an endpoint inside it must yield no spans
    // (tolerated like the other invalid-range shapes, never a panic).
    let t = ta_with("héllo");
    let area = Rect::new(0, 0, 10, 2);
    let state = TextAreaState::default();

    assert!(t.screen_spans_of_range(2..5, area, state).is_empty());
    assert!(t.screen_spans_of_range(0..2, area, state).is_empty());
}

#[test]
fn screen_spans_of_range_covers_wrapped_rows() {
    // A token wider than the wrap width must split at the line end and
    // report one span per visual row it lands on.
    let mut t = ta_with("aa /pr-workflow");
    t.show_scrollbar = false;
    let area = Rect::new(0, 0, 8, 4);
    let state = TextAreaState::default();

    // "/pr-workflow" = bytes 3..15, display width 12 > wrap width 8.
    let spans = t.screen_spans_of_range(3..15, area, state);
    assert!(
        spans.len() >= 2,
        "token must cover multiple rows: {spans:?}"
    );
    assert!(spans.iter().all(|r| r.height == 1));
    for pair in spans.windows(2) {
        assert_eq!(pair[1].y, pair[0].y + 1, "rows must be consecutive");
    }
    for r in &spans[1..] {
        assert_eq!(r.x, area.x, "continuation rows start at the left edge");
        assert!(r.right() <= area.x + area.width);
    }
    // Tokens contain no whitespace, so no cell is lost at wrap boundaries:
    // the summed span widths equal the token's display width.
    let total: u16 = spans.iter().map(|r| r.width).sum();
    assert_eq!(total, 12);
}

#[test]
fn screen_spans_of_range_skips_offscreen_rows() {
    // Cursor at the end scrolls the viewport to the tail: the token's
    // first row is above the viewport, but its visible tail must still
    // be reported (screen_position_of on the start would return None).
    let mut t = ta_with("/pr-workflow abc");
    t.show_scrollbar = false;
    let area = Rect::new(0, 0, 8, 2);
    let state = TextAreaState::default();

    let spans = t.screen_spans_of_range(0..12, area, state);
    assert!(!spans.is_empty(), "visible token tail must be reported");
    for r in &spans {
        assert!((area.y..area.y + area.height).contains(&r.y));
        assert!(r.width > 0 && r.right() <= area.x + area.width);
    }
    let total: u16 = spans.iter().map(|r| r.width).sum();
    assert!(
        total < 12,
        "off-screen head must not be reported: {spans:?}"
    );
}

#[test]
fn screen_spans_of_range_uses_display_width() {
    // 2-cell CJK chars: 日本語 (9 bytes, display width 6) at wrap width 4
    // renders as 日本 / 語.
    let mut t = ta_with("日本語");
    t.show_scrollbar = false;
    let area = Rect::new(1, 0, 4, 3);
    let state = TextAreaState::default();

    let spans = t.screen_spans_of_range(0..9, area, state);
    assert_eq!(spans, vec![Rect::new(1, 0, 4, 1), Rect::new(1, 1, 2, 1)]);
}

#[test]
fn screen_spans_of_range_clamps_to_content_edge() {
    // Overflowing content puts the scrollbar up, so content is only
    // `tw = width - 1` columns. Row 0's byte range keeps its trailing
    // wrap spaces ("ab   " measures 5), but the reported span must stop
    // at the content edge (4), never reaching the scrollbar column.
    let mut t = ta_with("ab   cd ef gh");
    t.set_cursor(0);
    let area = Rect::new(0, 0, 5, 2);
    let state = TextAreaState::default();

    let spans = t.screen_spans_of_range(0..5, area, state);
    assert_eq!(spans, vec![Rect::new(0, 0, 4, 1)]);
}

#[test]
fn wrapped_navigation_across_visual_lines() {
    let mut t = ta_with("abcdefghij");
    t.show_scrollbar = false;
    // Force wrapping at width 4: lines -> ["abcd", "efgh", "ij"]
    let _ = t.desired_height(4);

    // From the very start, moving down should go to the start of the next wrapped line (index 4)
    t.set_cursor(0);
    t.move_cursor_down();
    assert_eq!(t.cursor(), 4);

    // Cursor at boundary index 4 should be displayed at start of second wrapped line
    t.set_cursor(4);
    let area = Rect::new(0, 0, 4, 10);
    let (x, y) = t.cursor_pos(area).unwrap();
    assert_eq!((x, y), (0, 1));

    // With state and small height, cursor should be visible at row 0, col 0
    let small_area = Rect::new(0, 0, 4, 1);
    let state = TextAreaState::default();
    let (x, y) = t.cursor_pos_with_state(small_area, state).unwrap();
    assert_eq!((x, y), (0, 0));

    // Place cursor in the middle of the second wrapped line ("efgh"), at 'g'
    t.set_cursor(6);
    // Move up should go to same column on previous wrapped line -> index 2 ('c')
    t.move_cursor_up();
    assert_eq!(t.cursor(), 2);

    // Move down should return to same position on the next wrapped line -> back to index 6 ('g')
    t.move_cursor_down();
    assert_eq!(t.cursor(), 6);

    // Move down again should go to third wrapped line. Target col is 2, but the line has len 2 -> clamp to end
    t.move_cursor_down();
    assert_eq!(t.cursor(), t.text().len());
}

#[test]
fn cursor_pos_with_state_after_movements() {
    let mut t = ta_with("abcdefghij");
    // Wrap width 4 -> visual lines: abcd | efgh | ij
    let _ = t.desired_height(4);
    let area = Rect::new(0, 0, 4, 2);
    let mut state = TextAreaState::default();
    let mut buf = Buffer::empty(area);

    // Start at beginning
    t.set_cursor(0);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), area, &mut buf, &mut state);
    let (x, y) = t.cursor_pos_with_state(area, state).unwrap();
    assert_eq!((x, y), (0, 0));

    // Move down to second visual line; should be at bottom row (row 1) within 2-line viewport
    t.move_cursor_down();
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), area, &mut buf, &mut state);
    let (x, y) = t.cursor_pos_with_state(area, state).unwrap();
    assert_eq!((x, y), (0, 1));

    // Move down to third visual line; viewport scrolls and keeps cursor on bottom row
    t.move_cursor_down();
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), area, &mut buf, &mut state);
    let (x, y) = t.cursor_pos_with_state(area, state).unwrap();
    assert_eq!((x, y), (0, 1));

    // Move up to second visual line; with current scroll, it appears on top row
    t.move_cursor_up();
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), area, &mut buf, &mut state);
    let (x, y) = t.cursor_pos_with_state(area, state).unwrap();
    assert_eq!((x, y), (0, 0));

    // Column preservation across moves: set to col 2 on first line, move down
    t.set_cursor(2);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), area, &mut buf, &mut state);
    let (x0, y0) = t.cursor_pos_with_state(area, state).unwrap();
    assert_eq!((x0, y0), (2, 0));
    t.move_cursor_down();
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), area, &mut buf, &mut state);
    let (x1, y1) = t.cursor_pos_with_state(area, state).unwrap();
    assert_eq!((x1, y1), (2, 1));
}

#[test]
fn wrapped_navigation_with_newlines_and_spaces() {
    // Include spaces and an explicit newline to exercise boundaries
    let mut t = ta_with("word1  word2\nword3");
    // Width 6 will wrap "word1  " and then "word2" before the newline
    let _ = t.desired_height(6);

    // Put cursor on the second wrapped line before the newline, at column 1 of "word2"
    let start_word2 = t.text().find("word2").unwrap();
    t.set_cursor(start_word2 + 1);

    // Up should go to first wrapped line, column 1 -> index 1
    t.move_cursor_up();
    assert_eq!(t.cursor(), 1);

    // Down should return to the same visual column on "word2"
    t.move_cursor_down();
    assert_eq!(t.cursor(), start_word2 + 1);

    // Down again should cross the logical newline to the next visual line ("word3"), clamped to its length if needed
    t.move_cursor_down();
    let start_word3 = t.text().find("word3").unwrap();
    assert!(t.cursor() >= start_word3 && t.cursor() <= start_word3 + "word3".len());
}

#[test]
fn wrapped_navigation_with_wide_graphemes() {
    // Four thumbs up, each of display width 2, with width 3 to force wrapping inside grapheme boundaries
    let mut t = ta_with("👍👍👍👍");
    let _ = t.desired_height(3);

    // Put cursor after the second emoji (which should be on first wrapped line)
    t.set_cursor("👍👍".len());

    // Move down should go to the start of the next wrapped line (same column preserved but clamped)
    t.move_cursor_down();
    // We expect to land somewhere within the third emoji or at the start of it
    let pos_after_down = t.cursor();
    assert!(pos_after_down >= "👍👍".len());

    // Moving up should take us back to the original position
    t.move_cursor_up();
    assert_eq!(t.cursor(), "👍👍".len());
}

#[test]
fn wrapped_navigation_with_zwj_graphemes() {
    let grapheme = "👩\u{200D}💻";
    let mut t = ta_with(&format!("{grapheme}{grapheme}{grapheme}"));
    let _ = t.desired_height(4);

    t.set_cursor(grapheme.len() * 2);

    t.move_cursor_down();
    let pos_after_down = t.cursor();
    assert!(pos_after_down >= grapheme.len() * 2);

    t.move_cursor_up();
    assert_eq!(t.cursor(), grapheme.len() * 2);
}

#[test]
fn element_aware_wrap_ranges_preserve_zwj_graphemes() {
    let grapheme = "👩\u{200D}💻";
    let mut t = TextArea::new();
    t.insert_str(&format!("{grapheme}{grapheme}"));
    t.insert_element("raw", ElementKind(0), Some(Line::from("[P]")));

    let ranges = {
        let lines = t.wrapped_lines(2);
        lines.iter().cloned().collect::<Vec<_>>()
    };

    assert_eq!(ranges.len(), 3);
    assert_eq!(&t.text()[ranges[0].clone()], grapheme);
    assert_eq!(&t.text()[ranges[1].clone()], grapheme);
}

#[test]
fn fuzz_textarea_randomized() {
    // Deterministic seed for reproducibility

    // Seed the RNG based on the current day in UTC. This keeps the fuzz test
    // deterministic within a day while still varying day-to-day to improve
    // coverage. We avoid pulling chrono into our workspace deps by deriving the
    // day index from std::time::SystemTime.
    use std::time::{SystemTime, UNIX_EPOCH};
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0);
    let pst_today_seed: u64 = now / 86_400; // day index since epoch
    let mut rng = rand::rngs::StdRng::seed_from_u64(pst_today_seed);

    for _case in 0..500 {
        let mut ta = TextArea::new();
        let mut state = TextAreaState::default();
        // Track element payloads we insert. Payloads use characters '[' and ']' which
        // are not produced by rand_grapheme(), avoiding accidental collisions.
        let mut elem_texts: Vec<String> = Vec::new();
        let mut next_elem_id: usize = 0;
        // Start with a random base string
        let base_len = rng.random_range(0..30);
        let mut base = String::new();
        for _ in 0..base_len {
            base.push_str(&rand_grapheme(&mut rng));
        }
        ta.set_text(&base);
        // Choose a valid char boundary for initial cursor
        let mut boundaries: Vec<usize> = vec![0];
        boundaries.extend(ta.text().char_indices().map(|(i, _)| i).skip(1));
        boundaries.push(ta.text().len());
        let init = boundaries[rng.random_range(0..boundaries.len())];
        ta.set_cursor(init);

        let mut width: u16 = rng.random_range(1..=12);
        let mut height: u16 = rng.random_range(1..=4);

        for _step in 0..60 {
            // Mostly stable width/height, occasionally change
            if rng.random_bool(0.1) {
                width = rng.random_range(1..=12);
            }
            if rng.random_bool(0.1) {
                height = rng.random_range(1..=4);
            }

            // Pick an operation
            match rng.random_range(0..18) {
                0 => {
                    // insert small random string at cursor
                    let len = rng.random_range(0..6);
                    let mut s = String::new();
                    for _ in 0..len {
                        s.push_str(&rand_grapheme(&mut rng));
                    }
                    ta.insert_str(&s);
                }
                1 => {
                    // Include mid-grapheme char boundaries so normalization stays exercised.
                    let mut b: Vec<usize> = vec![0];
                    b.extend(ta.text().char_indices().map(|(i, _)| i).skip(1));
                    b.push(ta.text().len());
                    let i1 = rng.random_range(0..b.len());
                    let i2 = rng.random_range(0..b.len());
                    let (start, end) = if b[i1] <= b[i2] {
                        (b[i1], b[i2])
                    } else {
                        (b[i2], b[i1])
                    };
                    let insert_len = rng.random_range(0..=4);
                    let mut s = String::new();
                    for _ in 0..insert_len {
                        s.push_str(&rand_grapheme(&mut rng));
                    }
                    let before = ta.text().len();
                    let atomic_ranges = ta.element_ranges();
                    let plan = ta
                        .text
                        .plan_replace_byte_range(start..end, &s, &atomic_ranges);
                    let normalized_len = plan.replaced_byte_range().len();
                    ta.replace_range(start..end, &s);
                    let after = ta.text().len();
                    assert_eq!(
                        after as isize,
                        before as isize + (s.len() as isize) - (normalized_len as isize)
                    );
                }
                2 => ta.delete_backward(rng.random_range(0..=3)),
                3 => ta.delete_forward(rng.random_range(0..=3)),
                4 => ta.delete_backward_word(),
                5 => ta.kill_to_beginning_of_line(),
                6 => ta.kill_to_end_of_line(),
                7 => ta.move_cursor_left(),
                8 => ta.move_cursor_right(),
                9 => ta.move_cursor_up(),
                10 => ta.move_cursor_down(),
                11 => ta.move_cursor_to_beginning_of_line(true),
                12 => ta.move_cursor_to_end_of_line(true),
                13 => {
                    // Insert an element with a unique sentinel payload
                    let payload =
                        format!("[[EL#{}:{}]]", next_elem_id, rng.random_range(1000..9999));
                    next_elem_id += 1;
                    ta.insert_element(&payload, ElementKind(0), None);
                    elem_texts.push(payload);
                }
                14 => {
                    // Try inserting inside an existing element (should clamp to boundary)
                    if let Some(payload) = elem_texts.choose(&mut rng).cloned()
                        && let Some(start) = ta.text().find(&payload)
                    {
                        let end = start + payload.len();
                        if end - start > 2 {
                            let pos = rng.random_range(start + 1..end - 1);
                            let ins = rand_grapheme(&mut rng);
                            ta.insert_str_at(pos, &ins);
                        }
                    }
                }
                15 => {
                    // Replace a range that intersects an element -> whole element should be replaced
                    if let Some(payload) = elem_texts.choose(&mut rng).cloned()
                        && let Some(start) = ta.text().find(&payload)
                    {
                        let end = start + payload.len();
                        // Create an intersecting range [start-δ, end-δ2)
                        let mut s = start.saturating_sub(rng.random_range(0..=2));
                        let mut e = (end + rng.random_range(0..=2)).min(ta.text().len());
                        // Align to char boundaries to satisfy String::replace_range contract
                        let txt = ta.text();
                        while s > 0 && !txt.is_char_boundary(s) {
                            s -= 1;
                        }
                        while e < txt.len() && !txt.is_char_boundary(e) {
                            e += 1;
                        }
                        if s < e {
                            // Small replacement text
                            let mut srep = String::new();
                            for _ in 0..rng.random_range(0..=2) {
                                srep.push_str(&rand_grapheme(&mut rng));
                            }
                            ta.replace_range(s..e, &srep);
                        }
                    }
                }
                16 => {
                    // Try setting the cursor to a position inside an element; it should clamp out
                    if let Some(payload) = elem_texts.choose(&mut rng).cloned()
                        && let Some(start) = ta.text().find(&payload)
                    {
                        let end = start + payload.len();
                        if end - start > 2 {
                            let pos = rng.random_range(start + 1..end - 1);
                            ta.set_cursor(pos);
                        }
                    }
                }
                _ => {
                    // Jump to word boundaries
                    if rng.random_bool(0.5) {
                        let p = ta.beginning_of_previous_word();
                        ta.set_cursor(p);
                    } else {
                        let p = ta.end_of_next_word();
                        ta.set_cursor(p);
                    }
                }
            }

            // Sanity invariants
            assert!(ta.cursor() <= ta.text().len());

            // Element invariants
            for payload in &elem_texts {
                if let Some(start) = ta.text().find(payload) {
                    let end = start + payload.len();
                    // 1) Text inside elements matches the initially set payload
                    assert_eq!(&ta.text()[start..end], payload);
                    // 2) Cursor is never strictly inside an element
                    let c = ta.cursor();
                    assert!(
                        c <= start || c >= end,
                        "cursor inside element: {start}..{end} at {c}"
                    );
                }
            }

            // Render and compute cursor positions; ensure they are in-bounds and do not panic
            let area = Rect::new(0, 0, width, height);
            // Stateless render into an area tall enough for all wrapped lines
            let total_lines = ta.desired_height(width);
            let full_area = Rect::new(0, 0, width, total_lines.max(1));
            let mut buf = Buffer::empty(full_area);
            ratatui::widgets::WidgetRef::render_ref(&(&ta), full_area, &mut buf);

            // cursor_pos: x must be within width when present
            let _ = ta.cursor_pos(area);

            // cursor_pos_with_state: always within viewport rows
            let (_x, _y) = ta
                .cursor_pos_with_state(area, state)
                .unwrap_or((area.x, area.y));

            // Stateful render should not panic, and updates scroll
            let mut sbuf = Buffer::empty(area);
            ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut sbuf, &mut state);

            // After wrapping, desired height equals the number of lines we would render without scroll
            let total_lines = total_lines as usize;
            // state.scroll must not exceed total_lines when content fits within area height
            if (height as usize) >= total_lines {
                assert_eq!(state.scroll, 0);
            }
        }
    }
}

// ── Mouse M1: Screen→Buffer mapping tests ──

#[test]
fn buffer_pos_at_screen_plain_text_start() {
    let t = ta_with("hello");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click at column 0 → pos 0
    assert_eq!(t.buffer_pos_at_screen(0, 0, area, state), Some(0));
}

#[test]
fn buffer_pos_at_screen_plain_text_middle() {
    let t = ta_with("hello");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click at column 3 → pos 3
    assert_eq!(t.buffer_pos_at_screen(3, 0, area, state), Some(3));
}

#[test]
fn buffer_pos_at_screen_past_end_of_line() {
    let t = ta_with("hello");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click at column 10, line only has 5 chars → snap to end of text
    assert_eq!(t.buffer_pos_at_screen(10, 0, area, state), Some(5));
}

#[test]
fn buffer_pos_at_screen_below_text() {
    let t = ta_with("hello");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click on row 3, text only occupies row 0 → end of text
    assert_eq!(t.buffer_pos_at_screen(0, 3, area, state), Some(5));
}

#[test]
fn buffer_pos_at_screen_outside_area() {
    let t = ta_with("hello");
    let area = Rect::new(5, 5, 20, 5);
    let state = TextAreaState::default();
    // Click at (0, 0) which is outside area starting at (5, 5)
    assert_eq!(t.buffer_pos_at_screen(0, 0, area, state), None);
    // Click at (4, 5) — just left of area
    assert_eq!(t.buffer_pos_at_screen(4, 5, area, state), None);
    // Click at (5, 4) — just above area
    assert_eq!(t.buffer_pos_at_screen(5, 4, area, state), None);
}

#[test]
fn buffer_pos_at_screen_with_area_offset() {
    let t = ta_with("hello");
    let area = Rect::new(10, 5, 20, 5);
    let state = TextAreaState::default();
    // Click at screen (13, 5) = column 3 within the area → pos 3
    assert_eq!(t.buffer_pos_at_screen(13, 5, area, state), Some(3));
}

#[test]
fn buffer_pos_at_screen_multiline() {
    let t = ta_with("hello\nworld");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click on row 0, col 2 → "hello" pos 2
    assert_eq!(t.buffer_pos_at_screen(2, 0, area, state), Some(2));
    // Click on row 1, col 1 → "world" pos 6+1 = 7
    assert_eq!(t.buffer_pos_at_screen(1, 1, area, state), Some(7));
}

#[test]
fn buffer_pos_at_screen_wrapped_text() {
    // "abcdefghij" at width 5 wraps into "abcde" (0..5) and "fghij" (5..10)
    let t = ta_with("abcdefghij");
    let area = Rect::new(0, 0, 5, 5);
    let state = TextAreaState::default();
    // Click on row 0, col 2 → pos 2
    assert_eq!(t.buffer_pos_at_screen(2, 0, area, state), Some(2));
    // Click on row 1, col 0 → pos 5 (start of second wrapped line)
    assert_eq!(t.buffer_pos_at_screen(0, 1, area, state), Some(5));
    // Click on row 1, col 3 → pos 8
    assert_eq!(t.buffer_pos_at_screen(3, 1, area, state), Some(8));
}

#[test]
fn buffer_pos_at_screen_scrolled() {
    // 3 lines, area height 2 → first line scrolled off when cursor is at end
    let mut t = ta_with("aaa\nbbb\nccc");
    t.set_cursor(t.text().len()); // cursor at end → scroll to show last lines
    let area = Rect::new(0, 0, 20, 2);
    let mut state = TextAreaState::default();
    // Render to compute scroll
    let mut buf = ratatui::buffer::Buffer::empty(area);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&t), area, &mut buf, &mut state);
    // state.scroll should be 1 (skipping "aaa")
    assert_eq!(state.scroll, 1);
    // Click row 0 = visual row 0 = wrapped line 1 ("bbb"), col 1 → pos 5
    assert_eq!(t.buffer_pos_at_screen(1, 0, area, state), Some(5));
    // Click row 1 = visual row 1 = wrapped line 2 ("ccc"), col 2 → pos 10
    assert_eq!(t.buffer_pos_at_screen(2, 1, area, state), Some(10));
}

#[test]
fn buffer_pos_at_screen_wide_unicode() {
    // "a🦀b" — 🦀 is 2 columns wide (4 bytes)
    let t = ta_with("a🦀b");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // col 0 → 'a' at pos 0
    assert_eq!(t.buffer_pos_at_screen(0, 0, area, state), Some(0));
    // col 1 → first column of 🦀 → pos 1
    assert_eq!(t.buffer_pos_at_screen(1, 0, area, state), Some(1));
    // col 2 → second column of 🦀 → still pos 1 (within the 2-wide grapheme;
    // display_col_to_buffer_pos snaps to start of grapheme since target_col < width_so_far)
    // Actually: width_so_far after 'a' is 1, then 🦀 adds 2 → width_so_far=3 > target_col=2
    // → returns pos 1 (start of 🦀)
    assert_eq!(t.buffer_pos_at_screen(2, 0, area, state), Some(1));
    // col 3 → 'b' at pos 5 (1 + 4 bytes for 🦀)
    assert_eq!(t.buffer_pos_at_screen(3, 0, area, state), Some(5));
}

#[test]
fn buffer_pos_at_screen_element_with_display() {
    // "ab" + element(buffer="raw_text", display="[X]") + "cd"
    // Display: "ab[X]cd" — element is at display cols 2..5
    let mut t = TextArea::new();
    t.insert_str("ab");
    let display = Line::from("[X]");
    t.insert_element("raw_text", ElementKind(0), Some(display));
    t.insert_str("cd");
    // Buffer: "abraw_textcd", element range 2..10
    assert_eq!(t.text(), "abraw_textcd");

    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();

    // col 0 → 'a' at pos 0
    assert_eq!(t.buffer_pos_at_screen(0, 0, area, state), Some(0));
    // col 1 → 'b' at pos 1
    assert_eq!(t.buffer_pos_at_screen(1, 0, area, state), Some(1));
    // col 2 → start of element display "[X]" → snap to element start (pos 2)
    assert_eq!(t.buffer_pos_at_screen(2, 0, area, state), Some(2));
    // col 3 → middle of element display → snap to nearest boundary
    // display width = 3, dist_start = 1, dist_end = 2 → snap to start (pos 2)
    assert_eq!(t.buffer_pos_at_screen(3, 0, area, state), Some(2));
    // col 4 → near end of element display → snap to end (pos 10)
    // dist_start = 2, dist_end = 1 → snap to end
    assert_eq!(t.buffer_pos_at_screen(4, 0, area, state), Some(10));
    // col 5 → 'c' at pos 10
    assert_eq!(t.buffer_pos_at_screen(5, 0, area, state), Some(10));
    // col 6 → 'd' at pos 11
    assert_eq!(t.buffer_pos_at_screen(6, 0, area, state), Some(11));
}

#[test]
fn element_at_screen_hit_and_miss() {
    let mut t = TextArea::new();
    t.insert_str("ab");
    let display = Line::from("[File]");
    let id = t.insert_element("file.rs", ElementKind(1), Some(display));
    t.insert_str("cd");
    // Display: "ab[File]cd"

    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();

    // Click on 'a' (col 0) → no element
    assert!(t.element_at_screen(0, 0, area, state).is_none());
    // Click on 'b' (col 1) → no element
    assert!(t.element_at_screen(1, 0, area, state).is_none());
    // Click on element display (col 2) → element (snaps to start, pos 2 = element start)
    let elem = t.element_at_screen(2, 0, area, state);
    assert!(elem.is_some());
    assert_eq!(elem.unwrap().id, id);
    // Click on element display (col 3) → still the element
    assert_eq!(t.element_at_screen(3, 0, area, state).unwrap().id, id);
    // Click past element (col 8) → 'c' or 'd', no element
    assert!(t.element_at_screen(8, 0, area, state).is_none());
}

#[test]
fn buffer_pos_at_screen_empty_textarea() {
    let t = TextArea::new();
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click on empty textarea → pos 0
    assert_eq!(t.buffer_pos_at_screen(0, 0, area, state), Some(0));
    // Click at col 5 → still pos 0 (end of empty text)
    assert_eq!(t.buffer_pos_at_screen(5, 0, area, state), Some(0));
}

// ── Mouse M2: Selection state + rendering tests ──

#[test]
fn selection_range_normalizes_anchor_head() {
    let mut t = ta_with("hello world");
    // anchor > head → range should be normalized to start..end
    t.set_selection(8, 3);
    let range = t.selection_range().unwrap();
    assert_eq!(range, 3..8);
}

#[test]
fn selection_range_anchor_equals_head_is_none() {
    let mut t = ta_with("hello");
    t.set_selection(3, 3);
    assert!(t.selection_range().is_none());
}

#[test]
fn selected_text_returns_buffer_substring() {
    let mut t = ta_with("hello world");
    t.set_selection(6, 11);
    assert_eq!(t.selected_text().unwrap(), "world");
}

#[test]
fn selection_expands_to_element_boundaries() {
    let mut t = TextArea::new();
    t.insert_str("ab");
    t.insert_element("element_text", ElementKind(0), None);
    t.insert_str("cd");
    // Buffer: "abelement_textcd", element range 2..14
    assert_eq!(t.text(), "abelement_textcd");

    // Select only part of the element (bytes 5..10) → should expand to 2..14
    t.set_selection(5, 10);
    let range = t.selection_range().unwrap();
    assert_eq!(range.start, 2); // expanded to element start
    assert_eq!(range.end, 14); // expanded to element end
    assert_eq!(t.selected_text().unwrap(), "element_text");
}

#[test]
fn clear_selection_clears() {
    let mut t = ta_with("hello");
    t.set_selection(0, 3);
    assert!(t.selection_range().is_some());
    t.clear_selection();
    assert!(t.selection_range().is_none());
}

#[test]
fn take_clipboard_returns_and_clears() {
    let mut t = TextArea::new();
    t.set_clipboard_text("copied text".to_string());
    let text = t.take_clipboard();
    assert_eq!(text, Some("copied text".to_string()));
    // Second take returns None
    assert_eq!(t.take_clipboard(), None);
}

#[test]
fn no_selection_returns_none() {
    let t = ta_with("hello");
    assert!(t.selection_range().is_none());
    assert!(t.selected_text().is_none());
}

#[test]
fn selection_rendering_applies_default_selection_style() {
    let mut t = ta_with("hello");
    t.set_selection(1, 4); // select "ell"

    let area = Rect::new(0, 0, 10, 1);
    let mut buf = ratatui::buffer::Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    let default_bg = Color::Rgb(49, 62, 115);
    let default_fg = Color::Rgb(192, 202, 245);
    // Cells 1, 2, 3 should have the default selection bg + fg
    for col in 1..4u16 {
        let cell = &buf[(col, 0)];
        assert_eq!(
            cell.bg, default_bg,
            "cell at col {col} should have default selection bg"
        );
        assert_eq!(
            cell.fg, default_fg,
            "cell at col {col} should have default selection fg"
        );
    }
    // Cell 0 ('h') and cell 4 ('o') should NOT have selection bg
    assert_ne!(buf[(0, 0)].bg, default_bg);
    assert_ne!(buf[(4, 0)].bg, default_bg);
}

// ── Phase 1: Undo/Redo plumbing tests ──

#[test]
fn undo_insert_chars_one_at_a_time() {
    let mut ta = TextArea::new();
    ta.insert_str("a");
    ta.insert_str("b");
    ta.insert_str("c");
    assert_eq!(ta.text(), "abc");
    assert_eq!(ta.cursor(), 3);

    // Phase 2: consecutive single-char inserts are batched into 1 undo step.
    assert!(ta.undo());
    assert_eq!(ta.text(), "");
    assert_eq!(ta.cursor(), 0);

    // Nothing left to undo.
    assert!(!ta.undo());
}

#[test]
fn redo_after_undo_restores() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    ta.insert_str(" ");
    ta.insert_str("world");
    assert_eq!(ta.text(), "hello world");

    // With word boundary batching: "hello" / " " / "world" = 3 steps.
    ta.undo(); // undo "world"
    assert_eq!(ta.text(), "hello ");
    ta.undo(); // undo " "
    assert_eq!(ta.text(), "hello");
    ta.undo(); // undo "hello"
    assert_eq!(ta.text(), "");

    // Redo walks forward.
    ta.redo();
    assert_eq!(ta.text(), "hello");
    ta.redo();
    assert_eq!(ta.text(), "hello ");
    ta.redo();
    assert_eq!(ta.text(), "hello world");
    assert_eq!(ta.cursor(), 11);
}

#[test]
fn undo_via_super_modifier() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    assert_eq!(ta.text(), "hello");

    // Cmd+Z (SUPER) triggers undo.
    ta.input(KeyEvent::new(KeyCode::Char('z'), KeyModifiers::SUPER));
    assert_eq!(ta.text(), "");
}

#[test]
fn redo_via_super_modifier() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    ta.undo();
    assert_eq!(ta.text(), "");

    // Cmd+Shift+Z (SUPER, reported as uppercase Z) triggers redo.
    ta.input(KeyEvent::new(
        KeyCode::Char('Z'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT,
    ));
    assert_eq!(ta.text(), "hello");
}

#[test]
fn redo_cleared_by_new_mutation() {
    let mut ta = TextArea::new();
    ta.insert_str("abc");

    ta.undo(); // undo "abc" → ""
    assert_eq!(ta.text(), "");
    assert!(ta.can_redo());

    ta.insert_str("x"); // new mutation clears redo
    assert!(!ta.can_redo());
    assert_eq!(ta.text(), "x");
}

#[test]
fn undo_delete_backward_restores_char() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    ta.delete_backward(1); // "hell"
    assert_eq!(ta.text(), "hell");

    ta.undo(); // undo delete → "hello"
    assert_eq!(ta.text(), "hello");
    assert_eq!(ta.cursor(), 5);
}

#[test]
fn undo_redo_preserves_cursor() {
    let mut ta = TextArea::new();
    ta.insert_str("abc");
    // cursor is at 3
    ta.set_cursor(1);
    ta.insert_str("X"); // "aXbc", cursor at 2
    assert_eq!(ta.text(), "aXbc");
    assert_eq!(ta.cursor(), 2);

    ta.undo(); // undo insert "X" → "abc", cursor at 1
    assert_eq!(ta.text(), "abc");
    assert_eq!(ta.cursor(), 1);

    ta.redo(); // redo → "aXbc", cursor at 2
    assert_eq!(ta.text(), "aXbc");
    assert_eq!(ta.cursor(), 2);
}

#[test]
fn can_undo_can_redo_reflect_state() {
    let mut ta = TextArea::new();
    assert!(!ta.can_undo());
    assert!(!ta.can_redo());

    ta.insert_str("a");
    assert!(ta.can_undo());
    assert!(!ta.can_redo());

    ta.undo();
    assert!(!ta.can_undo());
    assert!(ta.can_redo());

    ta.redo();
    assert!(ta.can_undo());
    assert!(!ta.can_redo());
}

#[test]
fn undo_stack_depth_capped() {
    let mut ta = TextArea::new();
    // Override max_depth for testing.
    ta.undo.max_depth = 5;

    // Use set_text (Replace — always discrete) to force separate undo steps.
    for i in 0..10 {
        ta.set_text(&format!("v{i}"));
    }
    assert_eq!(ta.text(), "v9");
    // Stack should be capped at 5.
    assert_eq!(ta.undo.stack.len(), 5);

    // We can undo at most 5 times.
    let mut count = 0;
    while ta.undo() {
        count += 1;
    }
    assert_eq!(count, 5);
    // We've undone 5 set_text calls, landing on the 5th oldest state.
    assert_eq!(ta.text(), "v4");
}

#[test]
fn undo_set_text_restores_previous() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    ta.set_text("new");
    assert_eq!(ta.text(), "new");

    ta.undo(); // undo set_text → "hello"
    assert_eq!(ta.text(), "hello");
}

#[test]
fn undo_redo_multiple_round_trips() {
    let mut ta = TextArea::new();
    // Use separate insert kinds so they don't batch together.
    ta.insert_str("hello");
    ta.delete_backward(2); // "hel" — kind changes Insert→Delete, new undo step
    assert_eq!(ta.text(), "hel");

    ta.undo(); // undo delete → "hello"
    assert_eq!(ta.text(), "hello");

    ta.undo(); // undo insert → ""
    assert_eq!(ta.text(), "");

    ta.redo(); // redo insert → "hello"
    assert_eq!(ta.text(), "hello");

    ta.redo(); // redo delete → "hel"
    assert_eq!(ta.text(), "hel");

    // undo one, insert new → redo cleared
    ta.undo(); // undo delete → "hello"
    assert_eq!(ta.text(), "hello");
    ta.insert_str("z"); // "helloz" — new branch
    assert_eq!(ta.text(), "helloz");
    assert!(!ta.can_redo());

    // undo "z" — but wait, "z" extends the "hello" batch (same kind, consecutive cursor)?
    // No: undo reset last_kind=None, so "z" is a fresh group.
    ta.undo();
    assert_eq!(ta.text(), "hello");
}

// ── Phase 2: Batching tests ──

#[test]
fn batch_consecutive_inserts_into_one_undo_step() {
    // Typing "hello" char by char → batched into 1 undo step.
    let mut ta = TextArea::new();
    ta.insert_str("h");
    ta.insert_str("e");
    ta.insert_str("l");
    ta.insert_str("l");
    ta.insert_str("o");
    assert_eq!(ta.text(), "hello");
    assert_eq!(ta.undo.stack.len(), 1); // single checkpoint

    ta.undo();
    assert_eq!(ta.text(), "");
    assert!(!ta.undo());
}

#[test]
fn multi_grapheme_delete_calls_are_single_undo_steps() {
    for forward in [false, true] {
        let mut ta = ta_with("hello");
        if forward {
            ta.set_cursor(0);
            ta.delete_forward(2);
            assert_eq!(ta.text(), "llo");
        } else {
            ta.delete_backward(2);
            assert_eq!(ta.text(), "hel");
        }
        assert!(ta.undo());
        assert_eq!(ta.text(), "hello");
    }
}

#[test]
fn multi_count_deletes_cross_atomic_element_boundaries() {
    let mut backward = TextArea::new();
    backward.insert_str("a");
    backward.insert_element("TOKEN", ElementKind(1), None);
    backward.insert_str("b");
    backward.delete_backward(2);
    assert_eq!(backward.text(), "a");
    assert!(backward.elements().is_empty());
    assert!(backward.undo());
    assert_eq!(backward.text(), "aTOKENb");

    let mut forward = TextArea::new();
    forward.insert_str("a");
    forward.insert_element("TOKEN", ElementKind(1), None);
    forward.insert_str("b");
    forward.set_cursor(0);
    forward.delete_forward(2);
    assert_eq!(forward.text(), "b");
    assert!(forward.elements().is_empty());
    assert!(forward.undo());
    assert_eq!(forward.text(), "aTOKENb");
}

#[test]
fn batch_consecutive_deletes_into_one_undo_step() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    // 5 backspaces — all Delete kind, consecutive cursor
    ta.delete_backward(1); // o
    ta.delete_backward(1); // l
    ta.delete_backward(1); // l
    ta.delete_backward(1); // e
    ta.delete_backward(1); // h
    assert_eq!(ta.text(), "");

    // 2 undo steps: 1 for insert batch, 1 for delete batch
    ta.undo(); // undo all deletes
    assert_eq!(ta.text(), "hello");

    ta.undo(); // undo insert
    assert_eq!(ta.text(), "");
    assert!(!ta.undo());
}

#[test]
fn kind_change_breaks_batch() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    ta.delete_backward(1); // "hell" — kind changes → new step
    assert_eq!(ta.text(), "hell");

    // 2 undo steps
    ta.undo(); // undo delete
    assert_eq!(ta.text(), "hello");
    ta.undo(); // undo insert
    assert_eq!(ta.text(), "");
}

#[test]
fn cursor_jump_breaks_insert_batch() {
    let mut ta = TextArea::new();
    ta.insert_str("he"); // cursor at 2
    ta.set_cursor(0); // move cursor to 0 (no mutation, just movement)
    ta.insert_str("X"); // cursor was at 0, last_cursor was 2 → jump → new step
    assert_eq!(ta.text(), "Xhe");

    // 2 undo steps
    ta.undo(); // undo "X"
    assert_eq!(ta.text(), "he");
    ta.undo(); // undo "he"
    assert_eq!(ta.text(), "");
}

#[test]
fn kill_always_discrete() {
    let mut ta = TextArea::new();
    ta.insert_str("hello world");
    ta.set_cursor(5);
    ta.kill_to_end_of_line(); // kills " world"
    assert_eq!(ta.text(), "hello");
    ta.kill_to_end_of_line(); // kills nothing (already at EOL with no newline... wait)

    // Second kill at EOL does nothing (text.len() == cursor_pos).
    // So only 1 kill undo step.
    ta.undo(); // undo kill
    assert_eq!(ta.text(), "hello world");
}

#[test]
fn kill_consecutive_each_own_step() {
    // Two kill operations back-to-back should be separate undo steps.
    let mut ta = TextArea::new();
    ta.insert_str("aaa bbb ccc");
    ta.set_cursor(7); // after "aaa bbb"
    ta.kill_to_end_of_line(); // kills " ccc" → "aaa bbb"
    assert_eq!(ta.text(), "aaa bbb");
    ta.set_cursor(3);
    ta.kill_to_end_of_line(); // kills " bbb" → "aaa"
    assert_eq!(ta.text(), "aaa");

    ta.undo(); // undo second kill
    assert_eq!(ta.text(), "aaa bbb");
    ta.undo(); // undo first kill
    assert_eq!(ta.text(), "aaa bbb ccc");
}

#[test]
fn insert_str_multi_char_is_one_step() {
    // A single insert_str("hello world") call is 1 undo step.
    let mut ta = TextArea::new();
    ta.insert_str("hello world");
    assert_eq!(ta.undo.stack.len(), 1);

    ta.undo();
    assert_eq!(ta.text(), "");
}

#[test]
fn set_text_always_discrete() {
    let mut ta = TextArea::new();
    ta.set_text("first");
    ta.set_text("second");
    assert_eq!(ta.text(), "second");

    // Each set_text is its own undo step (Replace is always discrete)
    ta.undo();
    assert_eq!(ta.text(), "first");
    ta.undo();
    assert_eq!(ta.text(), "");
}

#[test]
fn insert_then_undo_then_insert_fresh_batch() {
    // After undo, last_kind is reset, so new inserts start a fresh batch.
    let mut ta = TextArea::new();
    ta.insert_str("ab");
    ta.undo(); // → ""
    ta.insert_str("cd");
    ta.insert_str("ef"); // should batch with "cd"
    assert_eq!(ta.text(), "cdef");

    ta.undo(); // undo "cdef" batch
    assert_eq!(ta.text(), "");
}

#[test]
fn delete_forward_batches() {
    let mut ta = TextArea::new();
    ta.insert_str("abcde");
    ta.set_cursor(0);
    ta.delete_forward(1); // "bcde"
    ta.delete_forward(1); // "cde"
    ta.delete_forward(1); // "de"
    assert_eq!(ta.text(), "de");

    // All delete_forward calls batch into 1 step
    ta.undo(); // undo all deletes
    assert_eq!(ta.text(), "abcde");
}

#[test]
fn word_boundary_breaks_insert_batch() {
    // Typing "foo bar" char by char: ws↔non-ws transitions create checkpoints.
    let mut ta = TextArea::new();
    // "foo" — all non-ws, batches into 1 step
    ta.insert_str("f");
    ta.insert_str("o");
    ta.insert_str("o");
    // " " — whitespace, class change → new step
    ta.insert_str(" ");
    // "bar" — non-ws, class change → new step
    ta.insert_str("b");
    ta.insert_str("a");
    ta.insert_str("r");
    assert_eq!(ta.text(), "foo bar");

    ta.undo(); // undo "bar"
    assert_eq!(ta.text(), "foo ");
    ta.undo(); // undo " "
    assert_eq!(ta.text(), "foo");
    ta.undo(); // undo "foo"
    assert_eq!(ta.text(), "");
    assert!(!ta.undo());
}

#[test]
fn word_boundary_whitespace_runs_batch_together() {
    // Multiple consecutive whitespace chars batch into one step.
    let mut ta = TextArea::new();
    ta.insert_str("a");
    ta.insert_str(" ");
    ta.insert_str(" ");
    ta.insert_str(" ");
    ta.insert_str("b");
    assert_eq!(ta.text(), "a   b");

    ta.undo(); // undo "b"
    assert_eq!(ta.text(), "a   ");
    ta.undo(); // undo "   "
    assert_eq!(ta.text(), "a");
    ta.undo(); // undo "a"
    assert_eq!(ta.text(), "");
}

#[test]
fn word_boundary_newlines_are_whitespace() {
    // Newlines are whitespace — they batch with spaces, break from words.
    let mut ta = TextArea::new();
    ta.insert_str("foo");
    ta.insert_str("\n");
    ta.insert_str("\n");
    ta.insert_str(" ");
    ta.insert_str(" ");
    ta.insert_str("bar");
    assert_eq!(ta.text(), "foo\n\n  bar");

    ta.undo(); // undo "bar"
    assert_eq!(ta.text(), "foo\n\n  ");
    ta.undo(); // undo "\n\n  " (all whitespace batched)
    assert_eq!(ta.text(), "foo");
    ta.undo(); // undo "foo"
    assert_eq!(ta.text(), "");
}

#[test]
fn word_boundary_multi_char_insert_str_is_one_step() {
    // A single insert_str("hello world") call is still 1 undo step,
    // even though it contains a space. Boundary check only applies
    // between separate insert_str calls.
    let mut ta = TextArea::new();
    ta.insert_str("hello world");
    assert_eq!(ta.undo.stack.len(), 1);

    ta.undo();
    assert_eq!(ta.text(), "");
}

#[test]
fn word_boundary_after_undo_starts_fresh() {
    // After undo, last_kind is reset — no stale boundary check.
    let mut ta = TextArea::new();
    ta.insert_str("abc");
    ta.insert_str(" ");
    ta.undo(); // undo " "
    assert_eq!(ta.text(), "abc");
    // Now insert non-ws — should start a fresh batch, no boundary check against stale state.
    ta.insert_str("d");
    ta.insert_str("e");
    assert_eq!(ta.text(), "abcde");

    ta.undo(); // undo "de"
    assert_eq!(ta.text(), "abc");
}

#[test]
fn element_insert_always_discrete() {
    let mut ta = TextArea::new();
    ta.insert_str("hi ");
    ta.insert_element("@file.rs", ElementKind(0), None);
    // Element should be its own undo step, not batched with the insert.
    assert_eq!(ta.text(), "hi @file.rs");

    ta.undo(); // undo element
    assert_eq!(ta.text(), "hi ");
    assert!(ta.elements().is_empty());

    ta.undo(); // undo "hi "
    assert_eq!(ta.text(), "");
}

// ── Phase 3: Element undo/redo tests ──

#[test]
fn undo_insert_element_redo_preserves_element_id() {
    let mut ta = TextArea::new();
    let id = ta.insert_element("@foo", ElementKind(1), None);
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.elements()[0].id, id);
    assert_eq!(ta.cursor(), "@foo".len());

    ta.undo(); // remove element
    assert!(ta.elements().is_empty());
    assert_eq!(ta.text(), "");
    assert_eq!(ta.cursor(), 0);

    ta.redo(); // restore element — same ElementId
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.elements()[0].id, id);
    assert_eq!(ta.text(), "@foo");
    assert_eq!(ta.cursor(), "@foo".len());
}

#[test]
fn undo_redo_zero_length_element_preserves_metadata_and_cursor() {
    let mut ta = TextArea::new();
    let id = ta.insert_element("", ElementKind(9), None);
    assert_eq!(ta.cursor(), 0);
    assert_eq!(ta.elements()[0].range, 0..0);

    assert!(ta.undo());
    assert!(ta.elements().is_empty());
    assert_eq!(ta.cursor(), 0);

    assert!(ta.redo());
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.elements()[0].id, id);
    assert_eq!(ta.elements()[0].range, 0..0);
    assert_eq!(ta.cursor(), 0);
}

#[test]
fn undo_replace_range_with_element_restores_original() {
    let mut ta = TextArea::new();
    ta.insert_str("hello @foo world");
    // Replace "@foo" (6..10) with an element
    let id = ta.replace_range_with_element(6..10, "@bar.rs", ElementKind(2), None);
    assert_eq!(ta.text(), "hello @bar.rs world");
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.elements()[0].id, id);

    ta.undo(); // undo replace → original text, no elements
    assert_eq!(ta.text(), "hello @foo world");
    assert!(ta.elements().is_empty());

    ta.redo(); // redo → element back
    assert_eq!(ta.text(), "hello @bar.rs world");
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.elements()[0].id, id);
}

#[test]
fn undo_element_display_preserved() {
    let mut ta = TextArea::new();
    let display = Line::from(vec![
        ratatui::text::Span::styled("[", Style::default().fg(Color::Green)),
        ratatui::text::Span::raw("file.rs"),
        ratatui::text::Span::styled("]", Style::default().fg(Color::Green)),
    ]);
    let id = ta.insert_element("@file.rs", ElementKind(0), Some(display));
    assert!(ta.elements()[0].display.is_some());

    ta.undo();
    assert!(ta.elements().is_empty());

    ta.redo();
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.elements()[0].id, id);
    // Display should be restored from the snapshot clone
    let restored = ta.elements()[0].display.as_ref().unwrap();
    assert_eq!(restored.spans.len(), 3);
    let text: String = restored.spans.iter().map(|s| s.content.as_ref()).collect();
    assert_eq!(text, "[file.rs]");
}

#[test]
fn next_element_id_never_decreases_after_undo() {
    let mut ta = TextArea::new();
    let id1 = ta.insert_element("a", ElementKind(0), None);
    let id2 = ta.insert_element("b", ElementKind(0), None);

    ta.undo(); // undo element "b"
    ta.undo(); // undo element "a"
    assert!(ta.elements().is_empty());

    // New element after undo should get a fresh ID, never reuse id1 or id2.
    let id3 = ta.insert_element("c", ElementKind(0), None);
    assert_ne!(id3, id1);
    assert_ne!(id3, id2);
    // IDs are monotonically increasing
    assert!(id3.0 > id2.0);
}

#[test]
fn backspace_on_element_undo_restores_element() {
    let mut ta = TextArea::new();
    ta.insert_str("before ");
    let id = ta.insert_element("[paste]", ElementKind(0), None);
    assert_eq!(ta.text(), "before [paste]");
    assert_eq!(ta.cursor(), 14);

    // Backspace at element end → deletes entire element atomically
    ta.delete_backward(1);
    assert_eq!(ta.text(), "before ");
    assert!(ta.elements().is_empty());

    // Undo → element restored with same ID
    ta.undo();
    assert_eq!(ta.text(), "before [paste]");
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.elements()[0].id, id);
    assert_eq!(ta.elements()[0].range, 7..14);
}

// ── Phase 4: Undo group tests ──

#[test]
fn undo_group_collapses_multiple_mutations() {
    // Autocomplete scenario: replace trigger + insert trailing space = 1 undo step.
    let mut ta = TextArea::new();
    ta.insert_str("hello @fo");
    assert_eq!(ta.text(), "hello @fo");

    ta.begin_undo_group();
    ta.replace_range_with_element(6..9, "@foo.rs", ElementKind(1), None);
    ta.insert_str(" "); // trailing space after element
    ta.end_undo_group();

    assert_eq!(ta.text(), "hello @foo.rs ");
    assert_eq!(ta.elements().len(), 1);

    // Single undo undoes the entire autocomplete operation.
    ta.undo();
    assert_eq!(ta.text(), "hello @fo");
    assert!(ta.elements().is_empty());
}

#[test]
fn cancel_undo_group_restores_original() {
    // Line-select cancel: enter → N live-updates → cancel = 0 undo entries.
    let mut ta = TextArea::new();
    ta.insert_str("original");
    let stack_before = ta.undo.stack.len();

    ta.begin_undo_group();
    ta.set_text("modified once");
    ta.set_text("modified twice");
    ta.cancel_undo_group();

    // State restored to before the group.
    assert_eq!(ta.text(), "original");
    // No new undo entries created by the group.
    assert_eq!(ta.undo.stack.len(), stack_before);
}

#[test]
fn nested_groups_only_outermost_pushes() {
    let mut ta = TextArea::new();
    ta.insert_str("start");

    ta.begin_undo_group(); // depth 1
    ta.insert_str(" A");
    ta.begin_undo_group(); // depth 2
    ta.insert_str(" B");
    ta.end_undo_group(); // depth 1 (inner end — no push)
    assert_eq!(ta.text(), "start A B");
    ta.insert_str(" C");
    ta.end_undo_group(); // depth 0 (outermost end — push)

    assert_eq!(ta.text(), "start A B C");

    // Single undo undoes everything in the group.
    ta.undo();
    assert_eq!(ta.text(), "start");
}

#[test]
fn group_with_no_mutations_creates_no_entry() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    let stack_len = ta.undo.stack.len();

    ta.begin_undo_group();
    // No mutations inside the group.
    ta.end_undo_group();

    // Stack unchanged — no empty undo entry created.
    assert_eq!(ta.undo.stack.len(), stack_len);
}

#[test]
fn redo_cleared_by_end_undo_group() {
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    ta.undo(); // undo → ""
    assert!(ta.can_redo());

    ta.begin_undo_group();
    ta.insert_str("world");
    ta.end_undo_group();

    // Redo from the previous undo should be cleared.
    assert!(!ta.can_redo());
    assert_eq!(ta.text(), "world");
}

#[test]
fn cancel_nested_group_restores_outermost() {
    // Even if deeply nested, cancel restores to the outermost group snapshot.
    let mut ta = TextArea::new();
    ta.insert_str("original");

    ta.begin_undo_group();
    ta.insert_str(" X");
    ta.begin_undo_group();
    ta.insert_str(" Y");
    // Cancel from inner level — should still restore to outermost snapshot.
    ta.cancel_undo_group();

    assert_eq!(ta.text(), "original");
    assert_eq!(ta.undo.group_depth, 0);
}

#[test]
fn mutations_after_group_work_normally() {
    // After a group ends, normal batching resumes.
    let mut ta = TextArea::new();

    ta.begin_undo_group();
    ta.insert_str("grouped");
    ta.end_undo_group();

    // Normal insert after group — should be its own batch.
    ta.insert_str("X");
    ta.insert_str("Y"); // batches with X

    ta.undo(); // undo "XY"
    assert_eq!(ta.text(), "grouped");

    ta.undo(); // undo group
    assert_eq!(ta.text(), "");
}

// ── M3: Click-to-place cursor tests ──

/// Helper to create a MouseEvent for testing.
fn mouse_down(col: u16, row: u16) -> MouseEvent {
    MouseEvent {
        kind: MouseEventKind::Down(MouseButton::Left),
        column: col,
        row,
        modifiers: KeyModifiers::NONE,
    }
}

fn mouse_up(col: u16, row: u16) -> MouseEvent {
    MouseEvent {
        kind: MouseEventKind::Up(MouseButton::Left),
        column: col,
        row,
        modifiers: KeyModifiers::NONE,
    }
}

#[test]
fn click_places_cursor_at_correct_position() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click at column 3 → cursor at byte 3
    let action = ta.handle_mouse(mouse_down(3, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    assert_eq!(ta.cursor(), 3);

    // Click at column 0 → cursor at byte 0
    let action = ta.handle_mouse(mouse_down(0, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    assert_eq!(ta.cursor(), 0);
}

#[test]
fn click_on_element_returns_clicked_element() {
    let mut ta = TextArea::new();
    ta.insert_str("hi ");
    let id = ta.insert_element("elem", ElementKind(0), None);
    ta.insert_str(" bye");

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Element occupies cols 3..7, click at col 4
    let action = ta.handle_mouse(mouse_down(4, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    let ev = ta.poll_element_event().expect("should emit element click");
    assert_eq!(ev.id, id);
    assert_eq!(ev.kind, TextElementEventKind::Click);
}

#[test]
fn click_past_end_of_line_snaps_to_line_end() {
    let mut ta = ta_with("hi");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click far past end of "hi" (col 20)
    let action = ta.handle_mouse(mouse_down(20, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    assert_eq!(ta.cursor(), 2); // end of "hi"
}

#[test]
fn click_below_text_snaps_to_text_end() {
    let mut ta = ta_with("hello");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click on row 3 (only 1 row of text)
    let action = ta.handle_mouse(mouse_down(0, 3), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    assert_eq!(ta.cursor(), 5); // text.len()
}

#[test]
fn click_clears_existing_selection() {
    let mut ta = ta_with("hello world");
    ta.set_selection(0, 5);
    assert!(ta.selection_range().is_some());

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(8, 0), area, state);
    assert!(ta.selection_range().is_none());
}

#[test]
fn click_outside_area_returns_nothing() {
    let mut ta = ta_with("hello");
    let area = Rect::new(5, 5, 20, 3);
    let state = TextAreaState::default();

    // Click outside the area
    let action = ta.handle_mouse(mouse_down(0, 0), area, state);
    assert_eq!(action, MouseAction::Nothing);
}

#[test]
fn mouse_up_clears_down_pos() {
    let mut ta = ta_with("hello");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(2, 0), area, state);
    assert!(ta.mouse_down_pos.is_some());

    ta.handle_mouse(mouse_up(2, 0), area, state);
    assert!(ta.mouse_down_pos.is_none());
}

#[test]
fn click_on_second_line_multiline_text() {
    let mut ta = ta_with("hello\nworld");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click on row 1, col 2 → "world" starts at byte 6, so byte 8 = 'r'
    let action = ta.handle_mouse(mouse_down(2, 1), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    assert_eq!(ta.cursor(), 8); // "hello\nwo" = 8 bytes → cursor at 'r'
}

// ── M4: Drag selection tests ──

fn mouse_drag(col: u16, row: u16) -> MouseEvent {
    MouseEvent {
        kind: MouseEventKind::Drag(MouseButton::Left),
        column: col,
        row,
        modifiers: KeyModifiers::NONE,
    }
}

#[test]
fn drag_selects_text() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Mouse down at col 0, drag to col 5
    ta.handle_mouse(mouse_down(0, 0), area, state);
    let action = ta.handle_mouse(mouse_drag(5, 0), area, state);
    assert_eq!(action, MouseAction::SelectionUpdated);
    assert_eq!(ta.selection_range(), Some(0..5));
    assert_eq!(ta.selected_text(), Some("hello".to_string()));
}

#[test]
fn drag_across_element_expands_to_element_boundaries() {
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    let mut ta = TextArea::new();
    ta.insert_str("ab");
    ta.insert_element("ELEM", ElementKind(0), None);
    ta.insert_str("cd");
    // buffer: "abELEMcd"
    // element range: 2..6
    // display cols: a(0) b(1) E(2) L(3) E(4) M(5) c(6) d(7)

    // Drag from col 1 ("b") to col 7 ("d") — fully crosses the element.
    // Raw selection: anchor=1, head=7. Element at 2..6 is fully inside.
    ta.handle_mouse(mouse_down(1, 0), area, state);
    ta.handle_mouse(mouse_drag(7, 0), area, state);
    let range = ta.selection_range().unwrap();
    assert_eq!(range, 1..7);

    let mut ta = TextArea::new();
    ta.insert_str("ab");
    ta.insert_element("ELEM", ElementKind(0), None);
    ta.insert_str("cd");

    // Now test partial overlap: drag from col 0 to col 3 (into the element).
    // display_col_to_buffer_pos snaps col 3 to element start (2) since dist
    // to start (1) < dist to end (3). Raw selection 0..2 → but element at
    // 2..6 is NOT overlapped, so no expansion.
    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(3, 0), area, state);
    let range = ta.selection_range().unwrap();
    assert_eq!(range, 0..2);

    let mut ta = TextArea::new();
    ta.insert_str("ab");
    ta.insert_element("ELEM", ElementKind(0), None);
    ta.insert_str("cd");

    // Drag from col 0 to col 5 — past element midpoint, so snaps to end (6).
    // Raw selection 0..6 → element fully covered.
    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(5, 0), area, state);
    let range = ta.selection_range().unwrap();
    assert_eq!(range, 0..6);
}

#[test]
fn mouse_up_after_drag_copies_to_clipboard() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(6, 0), area, state);
    ta.handle_mouse(mouse_drag(11, 0), area, state);
    let action = ta.handle_mouse(mouse_up(11, 0), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);

    // Clipboard should contain "world"
    assert_eq!(ta.take_clipboard(), Some("world".to_string()));
    // take_clipboard clears it
    assert_eq!(ta.take_clipboard(), None);
}

#[test]
fn selection_persists_after_mouseup_by_default() {
    let mut ta = ta_with("hello");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(3, 0), area, state);
    ta.handle_mouse(mouse_up(3, 0), area, state);

    // Default: keep_selection_after_mouseup = true
    assert!(ta.selection_range().is_some());
    assert_eq!(ta.selected_text(), Some("hel".to_string()));
}

#[test]
fn selection_clears_after_mouseup_when_configured() {
    let mut ta = ta_with("hello");
    ta.keep_selection_after_mouseup = false;
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(3, 0), area, state);
    ta.handle_mouse(mouse_up(3, 0), area, state);

    // Clipboard was still set
    assert_eq!(ta.take_clipboard(), Some("hel".to_string()));
    // But selection is cleared
    assert!(ta.selection_range().is_none());
}

#[test]
fn backspace_deletes_selection_only() {
    let mut ta = ta_with("hello world");
    ta.set_selection(0, 5);
    assert_eq!(ta.selected_text(), Some("hello".to_string()));

    // Backspace should delete "hello", not an extra char.
    ta.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE));
    assert_eq!(ta.text(), " world");
    assert_eq!(ta.cursor(), 0);
    assert!(ta.selection_range().is_none());
}

#[test]
fn typing_replaces_selection() {
    let mut ta = ta_with("hello world");
    ta.set_selection(0, 5);

    // Typing 'X' should replace "hello" with "X".
    ta.input(KeyEvent::new(KeyCode::Char('X'), KeyModifiers::NONE));
    assert_eq!(ta.text(), "X world");
    assert_eq!(ta.cursor(), 1);
    assert!(ta.selection_range().is_none());
}

#[test]
fn arrow_clears_selection() {
    let mut ta = ta_with("hello world");
    ta.set_selection(0, 5);
    assert!(ta.selection_range().is_some());

    ta.input(KeyEvent::new(KeyCode::Right, KeyModifiers::NONE));
    assert!(ta.selection_range().is_none());
}

#[test]
fn undo_after_delete_selection_restores() {
    let mut ta = ta_with("hello world");
    ta.set_cursor(ta.text().len());
    ta.set_selection(0, 5);

    ta.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE));
    assert_eq!(ta.text(), " world");
    assert_eq!(ta.cursor(), 0);
    assert_eq!(ta.undo.last_cursor, 0);

    ta.undo();
    assert_eq!(ta.text(), "hello world");
}

#[test]
fn undo_after_type_replace_selection_restores() {
    let mut ta = ta_with("hello world");
    ta.set_selection(0, 5);

    ta.input(KeyEvent::new(KeyCode::Char('X'), KeyModifiers::NONE));
    assert_eq!(ta.text(), "X world");

    // Single undo should restore to pre-replacement state (undo group).
    ta.undo();
    assert_eq!(ta.text(), "hello world");
}

#[test]
fn backspace_works_with_zero_width_selection() {
    // Regression: a zero-width selection (anchor == head, from mouse
    // jitter) caused Backspace/Delete to be silently swallowed because
    // delete_selection() returned false but input() still returned early.
    let mut ta = ta_with("hello");
    ta.set_cursor(5);
    // Simulate a zero-width selection (anchor == head at cursor).
    ta.set_selection(5, 5);
    assert!(ta.selection_range().is_none()); // zero-width → no range

    // Backspace must still delete the char before cursor.
    ta.input(KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE));
    assert_eq!(ta.text(), "hell");
    assert_eq!(ta.cursor(), 4);
    // Selection should be cleared.
    assert!(ta.selection.is_none());
}

#[test]
fn delete_forward_works_with_zero_width_selection() {
    let mut ta = ta_with("hello");
    ta.set_cursor(2);
    ta.set_selection(2, 2);

    ta.input(KeyEvent::new(KeyCode::Delete, KeyModifiers::NONE));
    assert_eq!(ta.text(), "helo");
    assert_eq!(ta.cursor(), 2);
    assert!(ta.selection.is_none());
}

#[test]
fn ctrl_x_with_zero_width_selection_falls_through() {
    let mut ta = ta_with("hello");
    ta.set_cursor(5);
    ta.set_selection(5, 5);

    // Ctrl-X on zero-width selection shouldn't eat the key.
    // It should clear selection and fall through to normal handling
    // (which for Ctrl-X without selection is a no-op, but the selection
    // must be cleared).
    ta.input(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL));
    assert!(ta.selection.is_none());
}

#[test]
fn mouse_up_discards_zero_width_drag_selection() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click at col 3 then drag to same position (zero distance).
    ta.handle_mouse(mouse_down(3, 0), area, state);
    let action = ta.handle_mouse(mouse_drag(3, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    let action = ta.handle_mouse(mouse_up(3, 0), area, state);
    assert_eq!(action, MouseAction::Nothing);

    // Zero-width drag should not leave a selection behind.
    assert!(ta.selection.is_none());
}

#[test]
fn drag_backward_selects_correctly() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click at col 8, drag back to col 3 (backward selection)
    ta.handle_mouse(mouse_down(8, 0), area, state);
    ta.handle_mouse(mouse_drag(3, 0), area, state);
    // selection_range() normalizes anchor/head
    assert_eq!(ta.selection_range(), Some(3..8));
    assert_eq!(ta.selected_text(), Some("lo wo".to_string()));
}

#[test]
fn click_after_drag_clears_selection() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Drag to select
    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(5, 0), area, state);
    ta.handle_mouse(mouse_up(5, 0), area, state);
    assert!(ta.selection_range().is_some());

    // New click clears the old selection
    ta.handle_mouse(mouse_down(8, 0), area, state);
    assert!(ta.selection_range().is_none());
}

#[test]
fn set_text_clears_selection_and_drag_state() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(5, 0), area, state);
    assert!(ta.selection_range().is_some());
    assert!(ta.drag_anchor.is_some());
    assert!(ta.drag_active);
    assert!(ta.mouse_down_pos.is_some());

    ta.set_text("reset");

    assert!(ta.selection.is_none());
    assert!(ta.selection_range().is_none());
    assert!(ta.drag_anchor.is_none());
    assert!(!ta.drag_active);
    assert!(ta.mouse_down_pos.is_none());
    assert!(ta.pending_drag_scroll.is_none());
}

#[test]
fn same_cell_drag_does_not_create_selection() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(3, 0), area, state);
    let action = ta.handle_mouse(mouse_drag(3, 0), area, state);

    assert_eq!(action, MouseAction::CursorPlaced);
    assert!(ta.selection.is_none());
    assert!(ta.selection_range().is_none());
    assert!(!ta.drag_active);
}

#[test]
fn typing_with_zero_width_selection_inserts_character() {
    let mut ta = ta_with("hello");
    ta.set_cursor(5);
    ta.set_selection(5, 5);

    ta.input(KeyEvent::new(KeyCode::Char('!'), KeyModifiers::SHIFT));

    assert_eq!(ta.text(), "hello!");
    assert_eq!(ta.cursor(), 6);
    assert!(ta.selection.is_none());
}

#[test]
fn mouse_up_after_drag_clears_drag_anchor() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(5, 0), area, state);
    assert!(ta.drag_anchor.is_some());

    ta.handle_mouse(mouse_up(5, 0), area, state);

    assert!(ta.drag_anchor.is_none());
    assert!(!ta.drag_active);
}

// ── M5: Double/triple click tests ──

#[test]
fn double_click_selects_word() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on "hello" (col 2)
    ta.handle_mouse(mouse_down(2, 0), area, state);
    let action = ta.handle_mouse(mouse_down(2, 0), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);
    assert_eq!(ta.selection_range(), Some(0..5));
    assert_eq!(ta.selected_text(), Some("hello".to_string()));
    assert_eq!(ta.take_clipboard(), Some("hello".to_string()));
}

#[test]
fn double_click_cursor_on_last_char() {
    // Neovim places cursor on the last character of the selection,
    // not one past the end.
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on "hello"
    ta.handle_mouse(mouse_down(2, 0), area, state);
    ta.handle_mouse(mouse_down(2, 0), area, state);

    assert_eq!(ta.selection_range(), Some(0..5));
    // Cursor should be on 'o' (byte 4), not on ' ' (byte 5)
    assert_eq!(
        ta.cursor(),
        4,
        "double-click cursor should be on last char 'o' (byte 4), got {}",
        ta.cursor()
    );
}

#[test]
fn double_click_cursor_on_last_char_unicode() {
    // Test with multi-byte characters — cursor must land on a valid char boundary
    let mut ta = ta_with("café bar");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on "café" (col 1 = 'a')
    ta.handle_mouse(mouse_down(1, 0), area, state);
    ta.handle_mouse(mouse_down(1, 0), area, state);

    assert_eq!(ta.selected_text(), Some("café".to_string()));
    // 'é' is 2 bytes (0xC3 0xA9), so "café" = [c(0), a(1), f(2), é(3,4)]
    // Last char 'é' starts at byte 3
    assert_eq!(
        ta.cursor(),
        3,
        "cursor should be on 'é' (byte 3), got {}",
        ta.cursor()
    );
}

#[test]
fn double_click_on_second_word() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on "world" (col 8)
    ta.handle_mouse(mouse_down(8, 0), area, state);
    let action = ta.handle_mouse(mouse_down(8, 0), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);
    assert_eq!(ta.selection_range(), Some(6..11));
    assert_eq!(ta.selected_text(), Some("world".to_string()));
}

#[test]
fn double_click_stops_at_punctuation() {
    // "hello, world," — double-click on 'h' should select "hello", not "hello,"
    let mut ta = ta_with("hello, world,");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on "hello" (col 2 = 'l')
    ta.handle_mouse(mouse_down(2, 0), area, state);
    let action = ta.handle_mouse(mouse_down(2, 0), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);
    assert_eq!(
        ta.selected_text(),
        Some("hello".to_string()),
        "should select only 'hello', not include trailing comma"
    );
    assert_eq!(ta.selection_range(), Some(0..5));
}

#[test]
fn double_click_on_punctuation_selects_punctuation_run() {
    // Double-click on punctuation selects the contiguous punctuation
    let mut ta = ta_with("hello... world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on "..." (col 6 = first '.')
    ta.handle_mouse(mouse_down(6, 0), area, state);
    let action = ta.handle_mouse(mouse_down(6, 0), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);
    assert_eq!(
        ta.selected_text(),
        Some("...".to_string()),
        "double-click on punctuation should select the punctuation run"
    );
}

#[test]
fn double_click_word_with_underscore() {
    // Underscores are part of a word (like vim's iskeyword)
    let mut ta = ta_with("hello_world foo");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on "hello_world" (col 3)
    ta.handle_mouse(mouse_down(3, 0), area, state);
    let action = ta.handle_mouse(mouse_down(3, 0), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);
    assert_eq!(
        ta.selected_text(),
        Some("hello_world".to_string()),
        "underscore should be part of the word"
    );
}

#[test]
fn double_click_on_element_snaps_like_single_click() {
    // Word-selecting an element would copy its hidden buffer text to
    // the clipboard; a double-click must instead snap to the element
    // start and re-emit Click so the host decides what it means.
    let mut ta = TextArea::new();
    ta.insert_str("hi ");
    let display = Line::from("[chip]");
    let id = ta.insert_element("hidden\ntext", ElementKind(0), Some(display));
    ta.insert_str(" bye");
    // Buffer: "hi hidden\ntext bye", element at 3..14, display "[chip]".

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // First click on the display (col 4) emits its own Click event.
    ta.handle_mouse(mouse_down(4, 0), area, state);
    assert!(ta.poll_element_event().is_some());

    let action = ta.handle_mouse(mouse_down(4, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    assert_eq!(ta.cursor(), 3); // element start
    assert!(ta.selection_range().is_none());
    assert_eq!(ta.take_clipboard(), None);
    let ev = ta
        .poll_element_event()
        .expect("double-click re-emits Click");
    assert_eq!(ev.id, id);
    assert_eq!(ev.kind, TextElementEventKind::Click);
}

#[test]
fn triple_click_selects_line() {
    let mut ta = ta_with("hello world\nsecond line\nthird");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Triple-click on first line (col 3)
    ta.handle_mouse(mouse_down(3, 0), area, state);
    ta.handle_mouse(mouse_down(3, 0), area, state);
    let action = ta.handle_mouse(mouse_down(3, 0), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);
    // Should select "hello world\n" (including the newline)
    assert_eq!(ta.selection_range(), Some(0..12));
    assert_eq!(ta.selected_text(), Some("hello world\n".to_string()));
}

#[test]
fn triple_click_on_last_line_selects_to_end() {
    let mut ta = ta_with("hello\nworld");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Triple-click on "world" (row 1, col 2)
    ta.handle_mouse(mouse_down(2, 1), area, state);
    ta.handle_mouse(mouse_down(2, 1), area, state);
    let action = ta.handle_mouse(mouse_down(2, 1), area, state);
    assert_eq!(action, MouseAction::SelectionFinished);
    // Last line has no trailing \n — selects to text.len()
    assert_eq!(ta.selection_range(), Some(6..11));
    assert_eq!(ta.selected_text(), Some("world".to_string()));
}

#[test]
fn triple_click_cursor_stays_at_click_pos() {
    // Triple-click should select the whole line but keep the cursor
    // at the click position, not at the end of the selection.
    let mut ta = ta_with("hello world\nsecond line\nthird");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Triple-click on first line at col 3 (byte 3 = 'l')
    ta.handle_mouse(mouse_down(3, 0), area, state);
    ta.handle_mouse(mouse_down(3, 0), area, state);
    ta.handle_mouse(mouse_down(3, 0), area, state);

    // Selection covers the full line "hello world\n"
    assert_eq!(ta.selection_range(), Some(0..12));

    // Cursor should be at the click position (byte 3), not at line_end (12)
    assert_eq!(
        ta.cursor(),
        3,
        "triple-click cursor should stay at click pos (3), got {}",
        ta.cursor()
    );
}

#[test]
fn selection_uses_custom_style_override() {
    let mut t = ta_with("hello");
    t.selection_style = Style::default().bg(Color::Blue);
    t.set_selection(1, 4); // select "ell"

    let area = Rect::new(0, 0, 10, 1);
    let mut buf = ratatui::buffer::Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&t), area, &mut buf);

    // Cells 1, 2, 3 should have Blue background (custom selection style)
    for col in 1..4u16 {
        let cell = &buf[(col, 0)];
        assert_eq!(
            cell.bg,
            Color::Blue,
            "cell at col {col} should have Blue bg"
        );
    }
    // Cell 0 ('h') and cell 4 ('o') should NOT have Blue bg
    assert_ne!(buf[(0, 0)].bg, Color::Blue);
    assert_ne!(buf[(4, 0)].bg, Color::Blue);
}

#[test]
fn double_click_on_whitespace_places_cursor() {
    let mut ta = ta_with("hello   world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Double-click on whitespace (col 6)
    ta.handle_mouse(mouse_down(6, 0), area, state);
    let action = ta.handle_mouse(mouse_down(6, 0), area, state);
    // Whitespace has no word → just places cursor
    assert_eq!(action, MouseAction::CursorPlaced);
    assert!(ta.selection_range().is_none());
}

#[test]
fn click_tracker_resets_on_position_change() {
    let mut ta = ta_with("hello world");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click at col 2, then at col 8 → not a double-click
    ta.handle_mouse(mouse_down(2, 0), area, state);
    let action = ta.handle_mouse(mouse_down(8, 0), area, state);
    // Should be a single click, not a double-click
    assert_eq!(action, MouseAction::CursorPlaced);
    assert!(ta.selection_range().is_none());
}

// ── Drag-to-scroll tests ──

#[test]
fn drag_below_area_scrolls_down_and_extends_selection() {
    // Text with 5 lines, visible area is only 3 rows tall.
    let mut ta = ta_with("aaa\nbbb\nccc\nddd\neee");
    // Place cursor at start so scroll=0.
    ta.set_cursor(0);
    let area = Rect::new(0, 0, 40, 3);
    let state = TextAreaState::default();

    // Click on first line.
    ta.handle_mouse(mouse_down(0, 0), area, state);
    assert_eq!(ta.cursor(), 0);

    // Drag below the visible area (row 5, past area.height=3).
    let action = ta.handle_mouse(mouse_drag(0, 5), area, state);
    assert_eq!(action, MouseAction::SelectionUpdated);

    // Cursor should have moved past the visible area.
    // With scroll=0 and height=3, visible lines are 0,1,2 (aaa,bbb,ccc).
    // Dragging below → target_line = visible_end = 3 → "ddd" starts at byte 12.
    // At col 0, cursor should be at byte 12 (start of "ddd").
    assert!(ta.cursor() >= 12);

    // Selection should extend from anchor (0) to the new cursor position.
    let range = ta.selection_range().unwrap();
    assert_eq!(range.start, 0);
    assert!(range.end >= 12);
}

#[test]
fn drag_above_area_scrolls_up_and_extends_selection() {
    // Text with 5 lines, start with cursor on the last line.
    let mut ta = ta_with("aaa\nbbb\nccc\nddd\neee");
    let area = Rect::new(0, 0, 40, 3);

    // Place cursor at the end first (to ensure scroll is at the bottom).
    ta.set_cursor(ta.text().len());
    let state = TextAreaState { scroll: 2 };

    // Click on bottom visible line (row 2).
    ta.handle_mouse(mouse_down(1, 2), area, state);

    // Drag above the visible area (row is before area.y).
    // Since area.y = 0, dragging to row=0 when scroll=2 means the row
    // is at the top edge. We need a row *above* the area. With area.y=0,
    // we can't go negative, but we can use an area with area.y > 0.
    let area2 = Rect::new(0, 5, 40, 3); // area starts at row 5
    ta.handle_mouse(mouse_down(1, 7), area2, state); // click at row 7 (visible)

    // Drag above: row 3 (above area2.y=5)
    let action = ta.handle_mouse(mouse_drag(0, 3), area2, state);
    assert_eq!(action, MouseAction::SelectionUpdated);

    // Cursor should have moved to a line above the visible region.
    // The exact position depends on how many lines we scroll per drag.
    let range = ta.selection_range().unwrap();
    assert!(range.start < range.end);
}

#[test]
fn drag_below_area_moves_cursor_past_last_visible_line() {
    // 10 short lines, area shows only 2.
    let text = "L0\nL1\nL2\nL3\nL4\nL5\nL6\nL7\nL8\nL9";
    let mut ta = ta_with(text);
    // Place cursor at start so scroll=0.
    ta.set_cursor(0);
    let area = Rect::new(0, 0, 40, 2);
    let state = TextAreaState::default();

    // Click at start.
    ta.handle_mouse(mouse_down(0, 0), area, state);
    assert_eq!(ta.cursor(), 0);

    // Drag below area (row 10, way below the 2-row area).
    let action = ta.handle_mouse(mouse_drag(1, 10), area, state);
    assert_eq!(action, MouseAction::SelectionUpdated);

    // With scroll=0 and height=2, visible lines are 0,1 (L0,L1).
    // Dragging below → target_line = 2 → "L2" starts at byte 6.
    // Cursor should be at col 1 of L2 → byte 7.
    assert!(ta.cursor() >= 6, "cursor={} should be >= 6", ta.cursor());
}

#[test]
fn drag_above_wide_column_still_scrolls_up() {
    // Bug: when dragging above the area with a column wider than the
    // target line, display_col_to_buffer_pos returns line_end which
    // equals the *next* line's start.  wrapped_line_index_by_start
    // then resolves to the next line, so effective_scroll sees the
    // cursor as still within the viewport and doesn't scroll.
    //
    // Scenario: 10 short lines ("ab"), area is 3 rows tall with
    // area.y = 2 (so we can drag above).  Scroll starts at line 5.
    // We drag to row 1 (above area.y = 2) at column 50 (way past
    // each 3-byte line).  The cursor must land ON the target line
    // (line 4), not spill over to line 5.
    let text = "ab\nab\nab\nab\nab\nab\nab\nab\nab\nab";
    let mut ta = ta_with(text);
    let area = Rect::new(0, 2, 40, 3); // area starts at row 2
    let state = TextAreaState { scroll: 5 };

    // Place cursor on wrapped line 6 (within viewport at scroll=5).
    // "ab\n" is 3 bytes per line, so line 6 starts at byte 18.
    ta.set_cursor(18);

    // Click inside the area (row 3 = area.y + 1).
    ta.handle_mouse(mouse_down(1, 3), area, state);

    // Drag above the area: row 1 (< area.y=2), column 50 (far right).
    let action = ta.handle_mouse(mouse_drag(50, 1), area, state);
    assert_eq!(action, MouseAction::SelectionUpdated);

    // target_line = scroll(5) - 1 = 4.  Line 4 spans bytes 12..15.
    // The cursor MUST be within line 4's range [12, 14], NOT at 15
    // (which is line 5's start).
    let cursor = ta.cursor();
    assert!(
        (12..15).contains(&cursor),
        "cursor={cursor} should be in [12, 15) (on line 4), \
         not at 15 (line 5 start)"
    );
}

#[test]
fn drag_below_wide_column_still_scrolls_down() {
    // Same bug but for scrolling down: dragging below the area with
    // a wide column should place the cursor on the target line, not
    // spill over to the next line.
    let text = "ab\nab\nab\nab\nab\nab\nab\nab\nab\nab";
    let mut ta = ta_with(text);
    let area = Rect::new(0, 0, 40, 3);
    let state = TextAreaState::default(); // scroll=0

    // Place cursor on first line.
    ta.set_cursor(0);

    // Click inside the area.
    ta.handle_mouse(mouse_down(1, 0), area, state);

    // Drag below the area: row 5 (>= area.y + height=3), column 50.
    let action = ta.handle_mouse(mouse_drag(50, 5), area, state);
    assert_eq!(action, MouseAction::SelectionUpdated);

    // visible_end = 0 + 3 = 3.  dist = 5 - 3 + 1 = 3.
    // n = drag_scroll_lines_for_distance(3) = 2.
    // target_line = (3 + 2 - 1) = 4.  Line 4 spans bytes 12..15.
    // Cursor must be within [12, 14], not at 15.
    let cursor = ta.cursor();
    assert!(
        (12..15).contains(&cursor),
        "cursor={cursor} should be in [12, 15) (on line 4), \
         not at 15 (line 5 start)"
    );
}

#[test]
fn drag_above_with_multibyte_line_end_does_not_panic() {
    // Regression: clamp_to_line used `line_end - 1` which can land inside
    // a multi-byte character (e.g. '│' = 3 bytes).
    let text = "aaa│\nbbb│\nccc│\nddd│\neee│\nfff│\nggg│";
    let mut ta = ta_with(text);
    let area = Rect::new(0, 0, 40, 3);
    // Start scrolled down so we can drag above.
    let state = TextAreaState { scroll: 3 };
    ta.set_cursor(20); // somewhere in the middle

    // Click inside the area.
    ta.handle_mouse(mouse_down(1, 1), area, state);

    // Drag above the area at a wide column (beyond line width).
    let action = ta.handle_mouse(mouse_drag(50, 0), area, state);
    // Should not panic — cursor should be on a valid char boundary.
    assert!(
        matches!(action, MouseAction::SelectionUpdated),
        "drag above should create selection, got {action:?}"
    );
    // Verify cursor is at a valid char boundary by reading from it.
    let cursor = ta.cursor();
    assert!(
        ta.text().is_char_boundary(cursor),
        "cursor at byte {cursor} is not a char boundary"
    );
}

#[test]
fn selection_across_element_with_multibyte_chars_does_not_panic() {
    // Regression: display_col_to_buffer_pos used `line_end + 1` to skip
    // past elements, but `line_end + 1` can land inside a multi-byte
    // character (e.g. '│' = 3 bytes).
    let mut ta = TextArea::new();
    ta.insert_str("before ");
    // Create an element whose backing text contains multi-byte '│' chars
    // across multiple lines — this triggers wrapping mid-element.
    let backing = "│  Ctrl+Shift+Z/Y  redo  │\n│  Ctrl+C  clear  │";
    ta.insert_element(backing, ElementKind(0), None);
    ta.insert_str(" after");

    let area = Rect::new(0, 0, 30, 5); // narrow so wrapping is forced

    // Select across the entire text (from start to end).
    ta.set_selection(0, ta.text().len());

    // Render should not panic.
    let mut buf = ratatui::buffer::Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&ta), area, &mut buf);
}

#[test]
fn click_on_text_with_multibyte_chars_does_not_panic() {
    // Plain text with '│' — clicking anywhere should not panic.
    let text = "│  Ctrl+Shift+Z/Y  redo  │\n│  Ctrl+C  clear  │";
    let mut ta = ta_with(text);
    let area = Rect::new(0, 0, 30, 5);
    let state = TextAreaState::default();

    // Click at various columns — should not panic.
    for col in 0..25u16 {
        ta.handle_mouse(mouse_down(col, 0), area, state);
    }
    // Double-click should also be safe.
    ta.handle_mouse(mouse_down(5, 0), area, state);
    ta.handle_mouse(mouse_down(5, 0), area, state);
}

#[test]
fn selecting_wrapped_line_ending_with_multibyte_char_does_not_panic() {
    // Regression: when a line wraps and '│' (3-byte char) ends up right
    // at the wrap boundary, the wrapping code (or rendering) can produce
    // a byte position inside the multi-byte character.
    //
    // Reproduce: enough spaces so '│' is pushed to the next wrapped line.
    let text = format!("{}", " ".repeat(29)); // 29 spaces + '│' = 30 display cols
    let mut ta = ta_with(&text);
    let area = Rect::new(0, 0, 30, 5); // width 30 → '│' wraps to next line
    let _state = TextAreaState::default();

    // Select across the wrap boundary.
    ta.set_selection(0, ta.text().len());

    // Render should not panic.
    let mut buf = ratatui::buffer::Buffer::empty(area);
    ratatui::widgets::WidgetRef::render_ref(&(&ta), area, &mut buf);
}

#[test]
fn clicking_on_wrapped_multibyte_line_does_not_panic() {
    // Same as above but triggered via click/drag rather than render.
    for extra_spaces in 28..33 {
        let text = format!("{}│end", " ".repeat(extra_spaces));
        let mut ta = ta_with(&text);
        let area = Rect::new(0, 0, 30, 5);
        let state = TextAreaState::default();

        // Click on every column of both rows.
        for row in 0..2u16 {
            for col in 0..30u16 {
                ta.handle_mouse(mouse_down(col, row), area, state);
            }
        }
    }
}

// ── Inline element tests ──

#[test]
fn inline_element_replaces_element_with_text() {
    let mut ta = TextArea::new();
    ta.insert_str("before ");
    let id = ta.insert_element("pasted\ncontent\nhere", ElementKind(1), None);
    ta.insert_str(" after");
    // Buffer: "before pasted\ncontent\nhere after"
    // Element at "pasted\ncontent\nhere" (bytes 7..26)

    let inlined = ta.inline_element(id);
    assert!(inlined);

    // Text should remain the same (the element's buffer text is kept).
    assert_eq!(ta.text(), "before pasted\ncontent\nhere after");
    // But the element should be gone.
    assert!(ta.elements().is_empty());
    // Cursor should be at the end of the inlined text.
    assert_eq!(ta.cursor(), 26); // end of "pasted\ncontent\nhere"
}

#[test]
fn inline_element_is_undoable() {
    let mut ta = TextArea::new();
    ta.insert_str("A ");
    let id = ta.insert_element("multi\nline", ElementKind(1), None);
    ta.insert_str(" B");
    // Buffer: "A multi\nline B", element at 2..12

    assert_eq!(ta.elements().len(), 1);

    ta.inline_element(id);
    assert!(ta.elements().is_empty());
    assert_eq!(ta.text(), "A multi\nline B");

    // Undo should restore the element.
    assert!(ta.undo());
    assert_eq!(ta.elements().len(), 1);
    assert_eq!(ta.element_text(id), Some("multi\nline"));
}

#[test]
fn inline_nonexistent_element_returns_false() {
    let mut ta = ta_with("hello");
    let fake_id = ElementId(9999);
    assert!(!ta.inline_element(fake_id));
}

#[test]
fn inline_element_cursor_at_element_start() {
    let mut ta = TextArea::new();
    let id = ta.insert_element("elem", ElementKind(0), None);
    ta.insert_str(" tail");
    // Cursor is after " tail" → at end.
    // Move cursor to element start.
    ta.set_cursor(0);

    ta.inline_element(id);
    // Element removed, text unchanged.
    assert!(ta.elements().is_empty());
    // Cursor at end of inlined region.
    assert_eq!(ta.cursor(), 4);
}

// ── Click-on-element edge cases ──

#[test]
fn click_on_element_second_half_snaps_to_start() {
    // Element with a wide display: clicking on the right half should still
    // snap cursor to element start and emit a Click element event.
    let mut ta = TextArea::new();
    ta.insert_str("ab");
    // Element display is "ELEM" (4 chars). Buffer text is "xy".
    let display = Line::from("ELEM");
    let id = ta.insert_element("xy", ElementKind(0), Some(display));
    ta.insert_str("cd");
    // Buffer: "abxycd", element at 2..4, display "ELEM" (4 wide)
    // Visual: a b E L E M c d
    //         0 1 2 3 4 5 6 7

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();
    ta.set_cursor(0);

    // Click on col 5 → second half of "ELEM" display.
    // display_col_to_buffer_pos should return elem_end=4 (closer to end).
    // handle_mouse should detect this as on-element and snap to start.
    let action = ta.handle_mouse(mouse_down(5, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    let ev = ta.poll_element_event().expect("should emit element click");
    assert_eq!(ev.id, id);
    assert_eq!(ev.kind, TextElementEventKind::Click);
    assert_eq!(ta.cursor(), 2); // element start
}

#[test]
fn click_on_element_first_half_snaps_to_start() {
    let mut ta = TextArea::new();
    ta.insert_str("ab");
    let display = Line::from("ELEM");
    let id = ta.insert_element("xy", ElementKind(0), Some(display));
    ta.insert_str("cd");
    ta.set_cursor(0);

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click on col 2 → first half of "ELEM" display.
    let action = ta.handle_mouse(mouse_down(2, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    let ev = ta.poll_element_event().expect("should emit element click");
    assert_eq!(ev.id, id);
    assert_eq!(ev.kind, TextElementEventKind::Click);
    assert_eq!(ta.cursor(), 2);
}

#[test]
fn click_after_element_places_cursor_not_element() {
    let mut ta = TextArea::new();
    ta.insert_str("ab");
    let display = Line::from("EL");
    ta.insert_element("xy", ElementKind(0), Some(display));
    ta.insert_str("cd");
    ta.set_cursor(0);
    // Visual: a b E L c d
    //         0 1 2 3 4 5

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click on col 4 → 'c' (after element).
    let action = ta.handle_mouse(mouse_down(4, 0), area, state);
    assert_eq!(action, MouseAction::CursorPlaced);
    assert_eq!(ta.cursor(), 4); // byte 4 = 'c'
}

// ── Mouse wheel tests ──

fn mouse_scroll_down(col: u16, row: u16) -> MouseEvent {
    MouseEvent {
        kind: MouseEventKind::ScrollDown,
        column: col,
        row,
        modifiers: KeyModifiers::NONE,
    }
}

fn mouse_scroll_up(col: u16, row: u16) -> MouseEvent {
    MouseEvent {
        kind: MouseEventKind::ScrollUp,
        column: col,
        row,
        modifiers: KeyModifiers::NONE,
    }
}

#[test]
fn scroll_down_returns_scrolled() {
    let mut ta = ta_with("aaa\nbbb\nccc\nddd\neee");
    ta.set_cursor(0);
    let area = Rect::new(0, 0, 40, 3);
    let state = TextAreaState::default();

    let action = ta.handle_mouse(mouse_scroll_down(5, 1), area, state);
    assert_eq!(action, MouseAction::Scrolled);
}

#[test]
fn scroll_up_returns_scrolled() {
    let mut ta = ta_with("aaa\nbbb\nccc\nddd\neee");
    ta.set_cursor(ta.text().len());
    let area = Rect::new(0, 0, 40, 3);
    let state = TextAreaState { scroll: 2 };

    let action = ta.handle_mouse(mouse_scroll_up(5, 1), area, state);
    assert_eq!(action, MouseAction::Scrolled);
}

#[test]
fn scroll_down_when_content_fits_returns_nothing() {
    let mut ta = ta_with("short");
    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    let action = ta.handle_mouse(mouse_scroll_down(5, 1), area, state);
    assert_eq!(action, MouseAction::Nothing);
}

#[test]
fn mousewheel_scrolls_viewport_not_cursor() {
    // Mousewheel should scroll the viewport without moving the cursor.
    // The cursor should stay at its current buffer position.
    let text = (0..20)
        .map(|i| format!("line {i}"))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    let area = Rect::new(0, 0, 40, 5); // only 5 lines visible
    let state = TextAreaState::default();

    // Place cursor on "line 2"
    let line2_start = text.find("line 2").unwrap();
    ta.set_cursor(line2_start);
    let cursor_before = ta.cursor();

    // Scroll down
    ta.handle_mouse(mouse_scroll_down(0, 0), area, state);

    // Cursor must NOT have moved
    assert_eq!(
        ta.cursor(),
        cursor_before,
        "mousewheel should not move cursor"
    );
}

#[test]
fn click_after_scroll_places_cursor_at_clicked_line() {
    // After scrolling the viewport away from the cursor via mousewheel,
    // clicking on a visible line should place the cursor on THAT line —
    // not jump to some other position based on the old cursor location.
    let text = (0..40)
        .map(|i| format!("line {:02}", i))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    // height=20 → scroll_lines_for_height returns 3 lines/tick
    let area = Rect::new(0, 0, 40, 20);
    let mut state = TextAreaState::default();

    // Cursor starts at line 0.
    ta.set_cursor(0);

    // Render to initialize state.scroll.
    let mut buf = Buffer::empty(area);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);

    // Scroll down 3 ticks (3 lines × 3 = 9 lines).
    for _ in 0..3 {
        ta.handle_mouse(mouse_scroll_down(0, 0), area, state);
        ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);
    }

    // Viewport should now start around line 9.
    assert!(
        state.scroll >= 9,
        "viewport should have scrolled; scroll={}",
        state.scroll
    );

    // Click on visual row 0 (which is now "line 09" or similar).
    ta.handle_mouse(mouse_down(0, 0), area, state);

    // The cursor should now be on a line that was VISIBLE.
    let cursor = ta.cursor();
    let lines = ta.wrapped_lines(area.width);
    let cursor_line = TextArea::wrapped_line_index_by_start(&lines, cursor).unwrap();
    assert!(
        cursor_line >= state.scroll as usize && cursor_line < (state.scroll + area.height) as usize,
        "click on visible row 0 should place cursor on a visible line; \
         cursor_line={cursor_line}, scroll={}, visible=[{}..{})",
        state.scroll,
        state.scroll,
        state.scroll as usize + area.height as usize,
    );
}

#[test]
fn drag_select_after_scroll_selects_visible_text() {
    // After scrolling, drag-selecting should work on the visible text,
    // not jump the viewport back.
    let text = (0..20)
        .map(|i| format!("line {:02}", i))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    let area = Rect::new(0, 0, 40, 5);
    let mut state = TextAreaState::default();

    ta.set_cursor(0);

    // Render + scroll down.
    let mut buf = Buffer::empty(area);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);
    for _ in 0..3 {
        ta.handle_mouse(mouse_scroll_down(0, 0), area, state);
        ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);
    }
    let scroll_after = state.scroll;

    // Click-down on row 1, then drag to row 3.
    ta.handle_mouse(mouse_down(0, 1), area, state);
    // Re-render so state.scroll updates after the click.
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);
    ta.handle_mouse(mouse_drag(5, 3), area, state);

    // Selection should exist.
    let sel = ta.selection_range().expect("drag should create selection");

    // The selected region should be within the visible range, not at line 0.
    let lines = ta.wrapped_lines(area.width);
    let sel_start_line = TextArea::wrapped_line_index_by_start(&lines, sel.start).unwrap();
    assert!(
        sel_start_line >= scroll_after as usize,
        "selection start should be in scrolled region; \
         sel_start_line={sel_start_line}, scroll={scroll_after}"
    );
}

#[test]
fn drag_outside_after_mousewheel_still_scrolls() {
    // After mousewheel scrolling during a drag, dragging outside the
    // textarea area should continue to auto-scroll the viewport.
    let text = (0..30)
        .map(|i| format!("line {:02}", i))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    let area = Rect::new(0, 0, 40, 5);
    let mut state = TextAreaState::default();

    ta.set_cursor(0);

    // Render to initialize state.
    let mut buf = Buffer::empty(area);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);

    // Start drag at row 0.
    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(0, 1), area, state);
    assert!(ta.selection_range().is_some());

    // Mousewheel scroll down during drag.
    ta.handle_mouse(mouse_scroll_down(0, 0), area, state);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);
    let scroll_after_wheel = state.scroll;

    // Now drag below the area (row = area.y + area.height = 5).
    // This should auto-scroll the viewport further down.
    // We need to bypass throttle, so reset the timer.
    ta.last_drag_scroll = None;
    ta.drag_scroll_steps = 0;
    ta.handle_mouse(mouse_drag(0, area.y + area.height), area, state);
    ratatui::widgets::StatefulWidgetRef::render_ref(&(&ta), area, &mut buf, &mut state);

    assert!(
        state.scroll > scroll_after_wheel,
        "drag-below after mousewheel should continue scrolling; \
         scroll={}, expected > {scroll_after_wheel}",
        state.scroll
    );
}

#[test]
fn scroll_during_drag_preserves_selection_anchor() {
    // Start drag at "bbb", scroll down — selection should extend,
    // anchor stays at original position.
    let mut ta = ta_with("aaa\nbbb\nccc\nddd\neee\nfff\nggg");
    ta.set_cursor(0); // put cursor at start so scroll=0 is consistent
    let area = Rect::new(0, 0, 40, 3);
    let state = TextAreaState::default();

    // Click-down on "bbb" (row 1, col 1 → byte 5 = second 'b')
    ta.handle_mouse(mouse_down(1, 1), area, state);
    let anchor = ta.cursor();
    assert_eq!(
        anchor, 5,
        "click on bbb col 1 should place cursor at byte 5"
    );

    // Start drag → creates selection
    ta.handle_mouse(mouse_drag(2, 1), area, state);
    assert!(
        ta.selection_range().is_some(),
        "drag should create selection"
    );

    // Now scroll down while dragging
    let action = ta.handle_mouse(mouse_scroll_down(1, 1), area, state);
    assert_eq!(action, MouseAction::Scrolled);

    // Selection should still exist and anchor should not have moved
    let sel = ta
        .selection_range()
        .expect("selection should survive scroll");
    assert!(
        sel.contains(&anchor),
        "anchor byte {anchor} should still be inside selection {sel:?}"
    );
}

#[test]
fn scroll_during_drag_extends_selection_head() {
    // Scroll down during drag should move the selection head forward.
    let mut ta = ta_with("aaa\nbbb\nccc\nddd\neee\nfff\nggg");
    ta.set_cursor(0);
    let area = Rect::new(0, 0, 40, 3);
    let state = TextAreaState::default();

    // Click-down on "aaa" (row 0, col 1)
    ta.handle_mouse(mouse_down(1, 0), area, state);
    // Start drag
    ta.handle_mouse(mouse_drag(2, 0), area, state);
    let sel_before = ta.selection_range().unwrap();

    // Scroll down
    ta.handle_mouse(mouse_scroll_down(1, 1), area, state);
    let sel_after = ta.selection_range().unwrap();

    // Selection should have grown (head moved forward)
    assert!(
        sel_after.end > sel_before.end,
        "scroll-down during drag should extend selection: before={sel_before:?} after={sel_after:?}"
    );
    // Anchor should not have moved
    assert_eq!(sel_after.start, sel_before.start);
}

#[test]
fn down_during_active_drag_does_not_reset_anchor() {
    // Some terminals re-emit Down(Left) after a scroll event even though
    // the button was held the whole time.  When `drag_active` is true,
    // a Down should be treated as a drag continuation, not a new click.
    let mut ta = ta_with("aaa\nbbb\nccc\nddd\neee\nfff\nggg");
    ta.set_cursor(0);
    let area = Rect::new(0, 0, 40, 3);
    let state = TextAreaState::default();

    // Click on "aaa" (row 0, col 1), then drag to start selection.
    ta.handle_mouse(mouse_down(1, 0), area, state);
    ta.handle_mouse(mouse_drag(2, 0), area, state);
    let anchor_before = ta.selection_range().unwrap().start;

    // Scroll down while dragging.
    ta.handle_mouse(mouse_scroll_down(1, 1), area, state);
    assert!(
        ta.selection_range().is_some(),
        "selection must survive scroll"
    );

    // Simulate terminal re-emitting Down(Left) at a different row.
    ta.handle_mouse(mouse_down(1, 2), area, state);

    // Selection should still exist and anchor should NOT have moved.
    let sel = ta
        .selection_range()
        .expect("Down during drag must not kill selection");
    assert_eq!(
        sel.start, anchor_before,
        "anchor must not reset: expected {anchor_before}, got {}",
        sel.start
    );
}

// ── Drag-scroll acceleration / distance helpers ──

#[test]
fn drag_scroll_interval_ramps_up() {
    // Step 0 → 80ms, step 1 → 60ms, step 2+ → 40ms.
    assert_eq!(TextArea::drag_scroll_interval(0), 80);
    assert_eq!(TextArea::drag_scroll_interval(1), 60);
    assert_eq!(TextArea::drag_scroll_interval(2), 40);
    assert_eq!(TextArea::drag_scroll_interval(100), 40);
}

#[test]
fn drag_scroll_lines_for_distance_tiers() {
    // Close: 1 line, farther: more lines.
    assert_eq!(TextArea::drag_scroll_lines_for_distance(1), 1);
    assert_eq!(TextArea::drag_scroll_lines_for_distance(2), 1);
    assert_eq!(TextArea::drag_scroll_lines_for_distance(3), 2);
    assert_eq!(TextArea::drag_scroll_lines_for_distance(5), 2);
    assert_eq!(TextArea::drag_scroll_lines_for_distance(6), 3);
    assert_eq!(TextArea::drag_scroll_lines_for_distance(10), 3);
    assert_eq!(TextArea::drag_scroll_lines_for_distance(20), 5);
}

// ── Scrollbar tests ──

#[test]
fn scrollbar_not_shown_when_content_fits() {
    // 3 lines of text in a 5-row viewport → no scrollbar needed.
    let mut ta = TextArea::new();
    ta.insert_str("aaa\nbbb\nccc");
    let area = Rect::new(0, 0, 20, 5);
    let (cw, needs) = ta.content_width(area.width, area.height);
    assert!(!needs, "should not need scrollbar when content fits");
    assert_eq!(cw, 20, "full width when no scrollbar");
}

#[test]
fn scrollbar_shown_when_content_overflows() {
    // 10 lines of text in a 5-row viewport → scrollbar needed.
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let (cw, needs) = ta.content_width(area.width, area.height);
    assert!(needs, "should need scrollbar when content overflows");
    assert_eq!(cw, 19, "width reduced by 1 for scrollbar");
}

#[test]
fn scrollbar_respects_show_scrollbar_false() {
    let mut ta = TextArea::new();
    ta.show_scrollbar = false;
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let (cw, needs) = ta.content_width(area.width, area.height);
    assert!(!needs, "scrollbar disabled");
    assert_eq!(cw, 20, "full width when scrollbar disabled");
}

#[test]
fn scrollbar_wrapping_uses_narrower_width() {
    // A line that fits in 20 cols but not in 19 should wrap differently
    // when scrollbar is present.
    let mut ta = TextArea::new();
    // 19 'a's → fits in 19 cols (no wrap).
    // Then enough other lines to overflow the viewport.
    ta.insert_str(&format!("{}\n2\n3\n4\n5\n6", "a".repeat(19)));
    let area = Rect::new(0, 0, 20, 5);
    let (cw, needs) = ta.content_width(area.width, area.height);
    assert!(needs, "overflows");
    assert_eq!(cw, 19);
    // The 19-char line should NOT wrap at width 19 — it fits exactly.
    let lines = ta.wrapped_lines(cw);
    // First wrapped line should contain all 19 chars.
    assert_eq!(&ta.text()[lines[0].clone()], &"a".repeat(19));
}

#[test]
fn click_on_scrollbar_column_scrolls() {
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    // Move cursor to start so we can verify it doesn't move.
    let _ = ta.text.set_cursor_byte(0);
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click on the scrollbar column (rightmost column = 19),
    // at the bottom row of the viewport → should scroll to end.
    let action = ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 19,
            row: 4,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    assert_eq!(action, MouseAction::Scrolled);
    // Cursor should not have moved — scrollbar click doesn't place cursor.
    assert_eq!(ta.cursor(), 0);
    // scroll_override should be set.
    assert!(ta.scroll_override.is_some());
}

#[test]
fn click_on_scrollbar_top_scrolls_to_top() {
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Scroll to the bottom first so the top of the track is NOT the thumb.
    ta.scroll_override = Some(5);
    // Click at top of scrollbar (row 0) — should be track, jump to top.
    ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 19,
            row: 0,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    assert_eq!(ta.scroll_override, Some(0));
}

#[test]
fn click_on_text_area_does_not_trigger_scrollbar() {
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Click on column 18 (text area, not scrollbar column 19).
    let action = ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 18,
            row: 0,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    // Should place cursor, not scroll.
    assert_eq!(action, MouseAction::CursorPlaced);
    assert!(!ta.scrollbar_dragging);
}

#[test]
fn drag_on_scrollbar_scrolls_proportionally() {
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    // Scroll to bottom so the thumb is at the bottom, then click
    // on the track at row 0 to start a track-based drag.
    ta.scroll_override = Some(5);
    ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 19,
            row: 0,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    assert!(ta.scrollbar_dragging);
    let scroll_at_top = ta.scroll_override.unwrap();
    assert_eq!(scroll_at_top, 0, "track click at top should jump to 0");
    // Drag to middle of scrollbar track.
    ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Drag(MouseButton::Left),
            column: 19,
            row: 2,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    let scroll_at_mid = ta.scroll_override.unwrap();
    assert!(
        scroll_at_mid > scroll_at_top,
        "dragging down should scroll further"
    );
    // Drag to bottom.
    ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Drag(MouseButton::Left),
            column: 19,
            row: 4,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    let scroll_at_bottom = ta.scroll_override.unwrap();
    assert!(
        scroll_at_bottom > scroll_at_mid,
        "dragging to bottom should scroll to max"
    );
    // Mouse up ends drag.
    ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Up(MouseButton::Left),
            column: 19,
            row: 4,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    assert!(!ta.scrollbar_dragging);
}

#[test]
fn scrollbar_render_produces_track_and_thumb() {
    // Render a textarea with overflow and verify the scrollbar column
    // has non-default styled cells.
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let mut buf = Buffer::empty(area);
    let mut state = TextAreaState::default();
    StatefulWidgetRef::render_ref(&&ta, area, &mut buf, &mut state);

    let sb_col = 19u16;
    // All cells in the scrollbar column should have the track bg color.
    let mut has_thumb = false;
    for row in 0..5u16 {
        let cell = &buf[(sb_col, row)];
        // Track bg is Rgb(45,45,55); check bg is set.
        assert!(cell.style().bg.is_some(), "scrollbar cell should have bg");
        if cell.symbol() != " " {
            has_thumb = true;
        }
    }
    assert!(has_thumb, "should have at least one thumb cell");
}

#[test]
fn no_scrollbar_column_when_content_fits() {
    // When content fits, the rightmost column should not have
    // scrollbar styling.
    let mut ta = TextArea::new();
    ta.insert_str("hello");
    let area = Rect::new(0, 0, 20, 5);
    let mut buf = Buffer::empty(area);
    let mut state = TextAreaState::default();
    StatefulWidgetRef::render_ref(&&ta, area, &mut buf, &mut state);

    let last_col = 19u16;
    let cell = &buf[(last_col, 0u16)];
    // Should be default (empty space), not scrollbar styled.
    assert!(
        cell.style().bg.is_none() || !matches!(cell.style().bg, Some(Color::Rgb(32, 35, 53))),
        "should not have scrollbar bg when content fits"
    );
}

#[test]
fn cursor_pos_accounts_for_scrollbar_width() {
    // When scrollbar is shown, cursor position should use content width,
    // not full area width.
    let mut ta = TextArea::new();
    // Fill 18 chars + enough lines to overflow.
    ta.insert_str(&format!("{}\n2\n3\n4\n5\n6", "x".repeat(18)));
    let _ = ta.text.set_cursor_byte(0); // at start
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();
    let pos = ta.cursor_pos_with_state(area, state);
    // Cursor at pos 0 should be at (0, 0).
    assert_eq!(pos, Some((0, 0)));
}

#[test]
fn click_on_scrollbar_thumb_does_not_jump() {
    // With 10 lines in a 5-row viewport, the thumb is near the top
    // when scroll is at 0.  Clicking on the thumb should NOT jump —
    // it should just start a drag from the current position.
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();

    // Scroll is at 0 — thumb should be at the top of the track.
    // Click on row 0 (top of track = on the thumb).
    let action = ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 19,
            row: 0,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    assert_eq!(action, MouseAction::Scrolled);
    assert!(ta.scrollbar_dragging);
    // The scroll should NOT have changed — thumb click = no jump.
    assert!(
        ta.scroll_override.is_none() || ta.scroll_override == Some(0),
        "thumb click should not jump: {:?}",
        ta.scroll_override,
    );
}

#[test]
fn click_on_scrollbar_track_jumps() {
    // Clicking on the track (outside the thumb) should jump.
    let mut ta = TextArea::new();
    ta.insert_str("1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
    let area = Rect::new(0, 0, 20, 5);
    let state = TextAreaState::default();

    // Scroll at 0, thumb near top.  Click at bottom of track (row 4)
    // which should be on the track, not the thumb.
    let action = ta.handle_mouse(
        MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 19,
            row: 4,
            modifiers: KeyModifiers::NONE,
        },
        area,
        state,
    );
    assert_eq!(action, MouseAction::Scrolled);
    // Should have jumped to a non-zero scroll position.
    assert!(
        ta.scroll_override.unwrap_or(0) > 0,
        "track click should jump"
    );
}

// ── Clipboard provider tests ──

#[test]
fn default_clipboard_provider_round_trips() {
    let mut ta = TextArea::new();
    ta.insert_str("hello world");
    // Select all via set_selection and cut
    ta.set_selection(0, 5);
    ta.input(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL));
    // take_clipboard returns the cut text
    assert_eq!(ta.take_clipboard(), Some("hello".to_string()));
    // Ctrl-V pastes it back
    ta.input(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL));
    assert_eq!(ta.text(), "hello world");
}

#[test]
fn custom_clipboard_provider() {
    #[derive(Debug)]
    struct TestClip {
        stored: Option<String>,
    }
    impl ClipboardProvider for TestClip {
        fn get(&mut self) -> Option<String> {
            self.stored.clone()
        }
        fn set(&mut self, text: &str) {
            self.stored = Some(format!("CUSTOM:{text}"));
        }
    }

    let mut ta = TextArea::new();
    ta.set_clipboard_provider(Box::new(TestClip { stored: None }));
    ta.insert_str("abc");
    ta.set_selection(0, 3);
    // Ctrl-X should call provider.set with "abc"
    ta.input(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL));
    // Ctrl-V should paste from provider.get → "CUSTOM:abc"
    ta.input(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL));
    assert_eq!(ta.text(), "CUSTOM:abc");
}

#[test]
fn ctrl_v_pastes_from_provider() {
    #[derive(Debug)]
    struct PreloadedClip;
    impl ClipboardProvider for PreloadedClip {
        fn get(&mut self) -> Option<String> {
            Some("pasted!".to_string())
        }
        fn set(&mut self, _text: &str) {}
    }

    let mut ta = TextArea::new();
    ta.set_clipboard_provider(Box::new(PreloadedClip));
    ta.input(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL));
    assert_eq!(ta.text(), "pasted!");
}

#[test]
fn copy_on_selection_finalized_sets_provider() {
    // Drag-select → mouse up should call provider.set
    #[derive(Debug)]
    struct RecordingClip {
        last_set: Option<String>,
    }
    impl ClipboardProvider for RecordingClip {
        fn get(&mut self) -> Option<String> {
            self.last_set.clone()
        }
        fn set(&mut self, text: &str) {
            self.last_set = Some(text.to_string());
        }
    }

    let mut ta = TextArea::new();
    ta.set_clipboard_provider(Box::new(RecordingClip { last_set: None }));
    ta.insert_str("hello");
    ta.set_cursor(0);

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Click at 0, drag to 5, release
    ta.handle_mouse(mouse_down(0, 0), area, state);
    ta.handle_mouse(mouse_drag(5, 0), area, state);
    ta.handle_mouse(mouse_up(5, 0), area, state);

    // Now Ctrl-V should paste "hello" (from provider)
    ta.set_cursor(5);
    ta.input(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL));
    assert_eq!(ta.text(), "hellohello");
}

// ── Hover / element event tests ──

fn mouse_moved(col: u16, row: u16) -> MouseEvent {
    MouseEvent {
        kind: MouseEventKind::Moved,
        column: col,
        row,
        modifiers: KeyModifiers::NONE,
    }
}

#[test]
fn hover_enter_on_element() {
    let mut ta = TextArea::new();
    ta.insert_str("hi ");
    let id = ta.insert_element("elem", ElementKind(0), None);
    ta.insert_str(" bye");

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Move over plain text — no event
    ta.handle_mouse(mouse_moved(0, 0), area, state);
    assert!(ta.poll_element_event().is_none());

    // Move over element (col 3)
    ta.handle_mouse(mouse_moved(3, 0), area, state);
    let ev = ta.poll_element_event().expect("should emit HoverEnter");
    assert_eq!(ev.id, id);
    assert_eq!(ev.kind, TextElementEventKind::HoverEnter);
}

#[test]
fn hover_leave_on_element() {
    let mut ta = TextArea::new();
    ta.insert_str("hi ");
    let id = ta.insert_element("elem", ElementKind(0), None);
    ta.insert_str(" bye");

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Enter the element
    ta.handle_mouse(mouse_moved(3, 0), area, state);
    ta.poll_element_event(); // consume

    // Leave the element
    ta.handle_mouse(mouse_moved(0, 0), area, state);
    let ev = ta.poll_element_event().expect("should emit HoverLeave");
    assert_eq!(ev.id, id);
    assert_eq!(ev.kind, TextElementEventKind::HoverLeave);
}

#[test]
fn hover_stays_on_same_element_no_event() {
    let mut ta = TextArea::new();
    ta.insert_str("hi ");
    ta.insert_element("elem", ElementKind(0), None);
    ta.insert_str(" bye");

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Enter the element (col 3)
    ta.handle_mouse(mouse_moved(3, 0), area, state);
    ta.poll_element_event(); // consume enter

    // Move within the element (col 4) — no new event
    ta.handle_mouse(mouse_moved(4, 0), area, state);
    assert!(ta.poll_element_event().is_none());
}

#[test]
fn hover_between_two_elements() {
    let mut ta = TextArea::new();
    let id1 = ta.insert_element("AA", ElementKind(0), None);
    ta.insert_str(" ");
    let id2 = ta.insert_element("BB", ElementKind(0), None);
    // Visual: A A   B B
    //         0 1 2 3 4

    let area = Rect::new(0, 0, 40, 5);
    let state = TextAreaState::default();

    // Hover element 1
    ta.handle_mouse(mouse_moved(0, 0), area, state);
    let ev = ta.poll_element_event().unwrap();
    assert_eq!(ev.id, id1);
    assert_eq!(ev.kind, TextElementEventKind::HoverEnter);

    // Move to element 2 — should emit enter for id2
    // (HoverLeave for id1 gets overwritten by HoverEnter for id2)
    ta.handle_mouse(mouse_moved(3, 0), area, state);
    let ev = ta.poll_element_event().unwrap();
    assert_eq!(ev.id, id2);
    assert_eq!(ev.kind, TextElementEventKind::HoverEnter);
}

// ── set_scroll_override / scroll_override tests ────────────────────

#[test]
fn scroll_override_getter_setter() {
    let mut ta = TextArea::new();
    assert_eq!(ta.scroll_override(), None);
    ta.set_scroll_override(Some(5));
    assert_eq!(ta.scroll_override(), Some(5));
    ta.set_scroll_override(None);
    assert_eq!(ta.scroll_override(), None);
}

/// Helper: stateful render (saves typing the full trait path).
fn render_stateful(ta: &TextArea, area: Rect, buf: &mut Buffer, state: &mut TextAreaState) {
    ratatui::widgets::StatefulWidgetRef::render_ref(&ta, area, buf, state);
}

#[test]
fn scroll_override_forces_viewport_ignoring_cursor() {
    // With 20 lines, cursor at end, and viewport of 5 rows,
    // effective_scroll normally follows the cursor to the bottom.
    // set_scroll_override(Some(0)) should force viewport to top.
    let text = (0..20)
        .map(|i| format!("line {i}"))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    ta.set_cursor(ta.text().len()); // cursor at end
    let area = Rect::new(0, 0, 40, 5);
    let mut state = TextAreaState::default();
    let mut buf = Buffer::empty(area);

    // Render without override: cursor-follow scrolls to bottom.
    render_stateful(&ta, area, &mut buf, &mut state);
    assert!(state.scroll > 0, "should scroll to show cursor at end");
    let normal_scroll = state.scroll;

    // Set override to 0 and render: viewport at top despite cursor at end.
    ta.set_scroll_override(Some(0));
    render_stateful(&ta, area, &mut buf, &mut state);
    assert_eq!(state.scroll, 0, "override should force scroll to 0");

    // Clear override: cursor-follow resumes.
    ta.set_scroll_override(None);
    render_stateful(&ta, area, &mut buf, &mut state);
    assert_eq!(
        state.scroll, normal_scroll,
        "clearing override should resume cursor-follow"
    );
}

#[test]
fn scroll_override_clamped_to_max() {
    // Override value larger than max_scroll should be clamped.
    let text = "line 0\nline 1\nline 2"; // 3 lines
    let mut ta = ta_with(text);
    ta.set_cursor(0);
    let area = Rect::new(0, 0, 40, 2); // 2 rows visible, max_scroll = 1
    let mut state = TextAreaState::default();
    let mut buf = Buffer::empty(area);

    ta.set_scroll_override(Some(999));
    render_stateful(&ta, area, &mut buf, &mut state);
    assert_eq!(state.scroll, 1, "override should be clamped to max_scroll");
}

#[test]
fn scroll_override_survives_render_cycles() {
    // The override should persist across multiple renders (unlike
    // mousewheel override which clears on cursor movement).
    let text = (0..20)
        .map(|i| format!("line {i}"))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    ta.set_cursor(ta.text().len());
    let area = Rect::new(0, 0, 40, 5);
    let mut state = TextAreaState::default();
    let mut buf = Buffer::empty(area);

    ta.set_scroll_override(Some(3));
    for _ in 0..5 {
        render_stateful(&ta, area, &mut buf, &mut state);
        assert_eq!(state.scroll, 3, "override should persist across renders");
    }
}

#[test]
fn scroll_override_save_restore_round_trip() {
    // Simulates the collapsed-prompt pattern:
    // 1. Render normally (cursor-follow)
    // 2. Save state.scroll + scroll_override
    // 3. Override to 0, render collapsed
    // 4. Restore both → next render shows original viewport
    let text = (0..30)
        .map(|i| format!("line {i}"))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    ta.set_cursor(ta.text().len()); // cursor at end
    let area = Rect::new(0, 0, 40, 5);
    let mut state = TextAreaState::default();
    let mut buf = Buffer::empty(area);

    // 1. Initial render — cursor-follow scrolls to bottom.
    render_stateful(&ta, area, &mut buf, &mut state);
    let original_scroll = state.scroll;
    let original_override = ta.scroll_override();
    assert!(original_scroll > 0);
    assert_eq!(original_override, None);

    // 2. "Collapse": override to 0, render a few frames.
    ta.set_scroll_override(Some(0));
    for _ in 0..3 {
        render_stateful(&ta, area, &mut buf, &mut state);
        assert_eq!(state.scroll, 0);
    }

    // 3. Restore both.
    ta.set_scroll_override(original_override);
    state.scroll = original_scroll;

    // 4. Render "uncollapsed" — should show original position.
    render_stateful(&ta, area, &mut buf, &mut state);
    assert_eq!(
        state.scroll, original_scroll,
        "restored scroll should match original"
    );
}

#[test]
fn scroll_override_save_restore_with_mousewheel() {
    // Same as above but the user had mousewheel-scrolled away from cursor
    // before collapse. Both state.scroll and scroll_override must be
    // saved/restored for the viewport to return to its pre-collapse position.
    let text = (0..30)
        .map(|i| format!("line {i}"))
        .collect::<Vec<_>>()
        .join("\n");
    let mut ta = ta_with(&text);
    ta.set_cursor(0); // cursor at start
    let area = Rect::new(0, 0, 40, 5);
    let mut state = TextAreaState::default();
    let mut buf = Buffer::empty(area);

    // Render at start, then mousewheel to scroll viewport away from cursor.
    render_stateful(&ta, area, &mut buf, &mut state);
    assert_eq!(state.scroll, 0);
    for _ in 0..5 {
        ta.handle_mouse(mouse_scroll_down(0, 0), area, state);
        render_stateful(&ta, area, &mut buf, &mut state);
    }
    let mousewheel_scroll = state.scroll;
    let mousewheel_override = ta.scroll_override();
    assert!(mousewheel_scroll > 0, "should have scrolled away");
    assert!(mousewheel_override.is_some(), "mousewheel sets override");

    // "Collapse": save both, override to 0.
    let saved_scroll = state.scroll;
    let saved_override = ta.scroll_override();
    ta.set_scroll_override(Some(0));
    for _ in 0..3 {
        render_stateful(&ta, area, &mut buf, &mut state);
        assert_eq!(state.scroll, 0);
    }

    // Restore both.
    ta.set_scroll_override(saved_override);
    state.scroll = saved_scroll;

    // Render "uncollapsed" — viewport should be at the mousewheel position,
    // NOT snapped to cursor (which is at line 0).
    render_stateful(&ta, area, &mut buf, &mut state);
    assert_eq!(
        state.scroll, mousewheel_scroll,
        "viewport should restore to mousewheel position, not snap to cursor"
    );
}

#[test]
fn shifted_character_classification_only_uppercases_letters() {
    for (input, expected) in [
        ('a', 'A'),
        ('z', 'Z'),
        ('A', 'A'),
        ('7', '7'),
        ('/', '/'),
        (';', ';'),
    ] {
        assert_eq!(
            classify_key_event(&KeyEvent::new(KeyCode::Char(input), KeyModifiers::SHIFT)),
            Some(EditCommand::Insert(expected))
        );
    }
}

#[test]
fn modified_delete_and_arrow_keys_keep_word_semantics() {
    for modifiers in [
        KeyModifiers::ALT,
        KeyModifiers::CONTROL,
        KeyModifiers::ALT | KeyModifiers::CONTROL,
    ] {
        assert_eq!(
            classify_key_event(&KeyEvent::new(KeyCode::Delete, modifiers)),
            Some(EditCommand::DeleteWordForward(WordStyle::Small)),
        );
        assert_eq!(
            classify_key_event(&KeyEvent::new(KeyCode::Left, modifiers)),
            Some(EditCommand::MoveWordLeft(WordStyle::Small)),
        );
        assert_eq!(
            classify_key_event(&KeyEvent::new(KeyCode::Right, modifiers)),
            Some(EditCommand::MoveWordRight(WordStyle::Small)),
        );
    }
    for modifiers in [KeyModifiers::ALT, KeyModifiers::SUPER] {
        assert_eq!(
            classify_key_event(&KeyEvent::new(KeyCode::Char('d'), modifiers)),
            Some(EditCommand::DeleteWordForward(WordStyle::Small)),
        );
    }
}

#[test]
fn modifier_keys_do_not_insert_text() {
    let mut t = ta_with("hello");
    let len = t.text().len();
    t.input(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL));
    assert_eq!(t.text().len(), len);
}

#[test]
fn alt_word_nav_preserved() {
    let mut t = ta_with("hello world");
    t.set_cursor(t.text().len());
    let text = t.text().to_owned();
    t.input(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::ALT));
    assert_eq!(t.text(), text);
    assert!(t.cursor() < text.len());

    t.set_cursor(0);
    t.input(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::ALT));
    assert_eq!(t.text(), text);
    assert!(t.cursor() > 0);
}

#[test]
fn ctrl_alt_h_deletes_word() {
    let mut t = ta_with("hello world");
    t.set_cursor(t.text().len());
    t.input(KeyEvent::new(
        KeyCode::Char('h'),
        KeyModifiers::CONTROL | KeyModifiers::ALT,
    ));
    assert_eq!(t.text(), "hello ");
}

#[test]
fn plain_and_shifted_chars_insert() {
    let mut t = TextArea::new();
    for c in ['a', 'z', '1', '/', '@', '{', '!', '~'] {
        t.input(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    }
    assert_eq!(t.text(), "az1/@{!~");

    let mut t = TextArea::new();
    t.input(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::SHIFT));
    t.input(KeyEvent::new(KeyCode::Char('z'), KeyModifiers::SHIFT));
    assert_eq!(t.text(), "AZ");
}

#[test]
fn altgr_char_insertion_platform_dependent() {
    let mut t = TextArea::new();
    t.input(KeyEvent::new(
        KeyCode::Char('@'),
        KeyModifiers::CONTROL | KeyModifiers::ALT,
    ));
    if cfg!(target_os = "windows") {
        assert_eq!(t.text(), "@");
    } else {
        assert_eq!(t.text(), "");
    }
}

#[test]
fn shift_number_trusts_terminal_character() {
    // QWERTZ: terminal sends Char('/') + SHIFT for Shift+7.
    let mut t = TextArea::new();
    t.input(KeyEvent::new(KeyCode::Char('/'), KeyModifiers::SHIFT));
    assert_eq!(t.text(), "/");
}

// ===== Hangul Compatibility Jamo width regression =====
//
// omp 18.0.4 fixed a cursor-drift bug in its Orca renderer where
// Hangul Compatibility Jamo consonants were reported as 1 cell wide;
// modern fixed-width terminals render them as 2 cells. Under our
// pinned unicode-width 0.2.2, the 93 Hangul Compatibility Jamo
// consonants (e.g. Kiyeok U+3131, Ssangkiyeok U+3132, Rieul U+314F)
// already return width 2 — only the two filler codepoints at the
// ends of the block (U+3130, U+318F) and the filler U+3164 are 1/0.
// This test pins the consonants so a future bump in unicode-width
// or a regression in our own cursor math is caught immediately.
#[test]
fn hangul_compatibility_jamo_width_is_two_cells() {
    // Spot-check three consonants spanning the block: Kiyeok, Ssangkiyeok, Rieul.
    assert_eq!(unicode_width::UnicodeWidthChar::width('\u{3131}'), Some(2));
    assert_eq!(unicode_width::UnicodeWidthChar::width('\u{3132}'), Some(2));
    assert_eq!(unicode_width::UnicodeWidthChar::width('\u{314F}'), Some(2));
}