konoma 0.26.5

Terminal file browser built for AI pair-programming — full-screen previews (Markdown, images, PDF, CSV), a git suite (jj/Jujutsu in preview), and an agent-watch mode that follows your AI's edits (macOS and Linux)
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
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
//! E2E-style behavior simulations (NOT coverage-oriented unit tests).
//!
//! Each test replays a real user session: keystrokes go through `handle_key` (the same entry
//! point as the run loop) and every key is followed by a full `ui::render` draw on a
//! `TestBackend`, so state that is built at render time (markdown items, git rows, viewport
//! sizes, clamps) participates exactly as it does live. Assertions read both the App state and
//! the drawn screen text.
//!
//! Policy (user-directed, 2026-07-08): tests are written against the **documented expected
//! behavior** (help/KEYMAP/docs). Where reality disagrees, the test is kept, marked
//! `#[ignore = "FINDING-.."]`, and the mismatch is reported in the findings list instead of
//! being silently "fixed" to match the bug.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::backend::TestBackend;
use ratatui::Terminal;

use crate::app::{App, Mode};
use crate::config::Config;
use crate::test_support::unique_tmp;
use crate::{handle_key, ui};

/// One simulated konoma session: an App plus a terminal, with a draw after every key
/// (mirroring the run loop's key→redraw cycle).
struct Sim {
    app: App,
    term: Terminal<TestBackend>,
    /// Set when a key handler requested quit (like the run loop breaking).
    quit: bool,
    /// Receiver for background file operations, once `with_async_file_ops` opted in (the run loop
    /// owns this channel live). `None` = the synchronous fallback in `start_file_op` is used.
    fileop_rx: Option<std::sync::mpsc::Receiver<crate::app::FileOpResult>>,
    /// Receiver for background git writes, once `with_async_git_ops` opted in (the run loop owns
    /// this channel live). `None` = the synchronous fallback in `start_git_op` is used.
    /// Gated on the `git` feature: without it there is no git write to run in the background.
    #[cfg(feature = "git")]
    gitop_rx: Option<std::sync::mpsc::Receiver<crate::app::GitOpResult>>,
    /// Receiver for background inline-Markdown-image decodes (`with_media`). Drained by `drain_md_images`.
    md_img_rx: Option<std::sync::mpsc::Receiver<crate::app::MdImageResult>>,
    /// Receiver for the background inline-image encode worker (`with_media`). Drained by `drain_md_encodes`.
    md_enc_rx: Option<std::sync::mpsc::Receiver<crate::app::MdEncodeResult>>,
    /// Receiver for full-screen media loads — SVG/GIF/PDF/video/standalone-mermaid/fullscreen-fence
    /// (`with_media`). Drained by `drain_media`.
    media_rx: Option<std::sync::mpsc::Receiver<crate::app::MediaResult>>,
    /// Receiver for background remote (http/https) Markdown image fetches (`with_media`). Drained by `drain_remote`.
    md_remote_rx: Option<std::sync::mpsc::Receiver<crate::app::RemoteFetch>>,
}

impl Sim {
    fn new(root: &std::path::Path) -> Sim {
        Sim::with_config(root, Config::default())
    }

    fn with_config(root: &std::path::Path, cfg: Config) -> Sim {
        Sim::with_config_sized(root, cfg, 90, 26)
    }

    /// Same as `with_config`, but with an explicit terminal size instead of the standard 90x26.
    /// Needed by tests that must clear `mermaid_fit_rows`'s viewport clamp (`preview_viewport - 2`)
    /// to observe a **large** `ui.mermaid_rows` actually growing the diagram — at 90x26 the
    /// viewport (22 rows for a single-fence document, confirmed by direct measurement) already
    /// clamps the *default* 24 down to 20, so any `mermaid_rows` >= 22 would clamp to the exact same
    /// 20 and the "large value changes something" contrast would be unobservable, not merely small.
    fn with_config_sized(root: &std::path::Path, cfg: Config, w: u16, h: u16) -> Sim {
        let app = App::new(root.to_path_buf(), cfg).expect("App::new");
        let term = Terminal::new(TestBackend::new(w, h)).expect("terminal");
        let mut sim = Sim {
            app,
            term,
            quit: false,
            fileop_rx: None,
            #[cfg(feature = "git")]
            gitop_rx: None,
            md_img_rx: None,
            md_enc_rx: None,
            media_rx: None,
            md_remote_rx: None,
        };
        sim.draw();
        sim
    }

    /// Opt in to the **real background path** for file operations (copy/move/duplicate/delete):
    /// attach a runner channel just like `main` does, so `start_file_op` spawns a worker instead of
    /// falling back to running the operation inline. Results are applied by `drain_file_ops`.
    fn with_async_file_ops(mut self) -> Sim {
        let (tx, rx) = std::sync::mpsc::channel();
        self.app.attach_fileop_runner(tx);
        self.fileop_rx = Some(rx);
        self
    }

    /// Opt in to the **real background path** for git writes (stage/unstage/discard/commit/
    /// checkout/branch/worktree): attach a runner channel just like `main` does, so `start_git_op`
    /// spawns a worker instead of running `git` inline. Results are applied by `drain_git_ops`.
    #[cfg(feature = "git")]
    fn with_async_git_ops(mut self) -> Sim {
        let (tx, rx) = std::sync::mpsc::channel();
        self.app.attach_gitop_runner(tx);
        self.gitop_rx = Some(rx);
        self
    }

    /// Attach a bare image backend — a deterministic, no-real-terminal-needed `Picker::halfblocks()`
    /// — WITHOUT attaching any of the async decode/encode worker channels (media_tx / md_img_tx /
    /// md_enc_tx). This is enough to unlock every picker-gated code path that only needs
    /// `picker.font_size()` (still-image decode, and the Markdown mermaid-fence / math-expression
    /// classification and placement sizing), because those all have a synchronous fallback when no
    /// worker tx is attached (`ensure_mermaid_fence_render` / `ensure_math_render` / `load_image`).
    /// What stays out of reach without a real encode-worker thread is the actual **pixel content**
    /// of a rendered raster (e.g. comparing colors for `math_color`/`mermaid_theme`) — that needs
    /// `md_enc_tx` + a running `md_encode_worker`, which this helper does not set up.
    fn with_picker(mut self) -> Sim {
        let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
        std::mem::forget(rx); // never drained: nothing sends without media_tx/md_img_tx attached
        self.app
            .attach_image_backend(ratatui_image::picker::Picker::halfblocks(), tx);
        self
    }

    /// Attach the **full async media pipeline** — the same background workers `main` spawns
    /// (`md_encode_worker` running on a cloned `Picker::halfblocks()`, plus the raw channels behind
    /// `attach_media_loader` / `attach_md_image_loader` / `attach_remote_md_loader`) — so a
    /// keystroke-driven test exercises the real decode(thread) → apply → re-render → encode(thread)
    /// → apply pipeline, instead of `with_picker()`'s synchronous no-loader-tx fallback. This is what
    /// reaches `ensure_md_image` / `ensure_mermaid_fence_render`'s and `ensure_math_render`'s
    /// `if let Some(tx) = ...` thread-spawning branches (previously uncovered — every existing test
    /// that touched inline images/mermaid/math went through the synchronous else-branch instead).
    /// The receivers are kept on `Sim` and drained one result at a time by `drain_md_images` /
    /// `drain_md_encodes` / `drain_media` / `drain_remote` — mirrors `with_async_file_ops` +
    /// `drain_file_ops`, and the run loop's per-tick `while let Ok(r) = rx.try_recv()` loops in `main.rs`.
    fn with_media(self) -> Sim {
        self.with_media_on(ratatui_image::picker::Picker::halfblocks())
    }

    /// `with_media` on a `Picker` forced to **kitty graphics**, so a test can drive the branch a
    /// Ghostty/kitty user actually takes: konoma's own compressed transmit on a fixed image id,
    /// rather than ratatui-image's protocol. Note that the drawn buffer then carries kitty escape
    /// sequences instead of halfblock glyphs — read it with `transmitted_kitty_ids`, not `see`.
    fn with_media_kitty(self) -> Sim {
        let mut picker = ratatui_image::picker::Picker::halfblocks();
        picker.set_protocol_type(ratatui_image::picker::ProtocolType::Kitty);
        self.with_media_on(picker)
    }

    fn with_media_on(mut self, picker: ratatui_image::picker::Picker) -> Sim {
        let (resize_tx, resize_rx) = tokio::sync::mpsc::unbounded_channel();
        std::mem::forget(resize_rx); // the kitty zoom/pan resize path isn't exercised here
        self.app.attach_image_backend(picker.clone(), resize_tx);

        let (media_tx, media_rx) = std::sync::mpsc::channel();
        self.app.attach_media_loader(media_tx);
        self.media_rx = Some(media_rx);

        let (md_img_tx, md_img_rx) = std::sync::mpsc::channel();
        self.app.attach_md_image_loader(md_img_tx);
        self.md_img_rx = Some(md_img_rx);

        let (md_remote_tx, md_remote_rx) = std::sync::mpsc::channel();
        self.app.attach_remote_md_loader(md_remote_tx);
        self.md_remote_rx = Some(md_remote_rx);

        // The inline-image encode worker is a real thread, exactly like `main` spawns it (a cloned
        // Picker, requests in, results out). It exits on its own once `md_enc_tx` is dropped (App
        // teardown at the end of the test).
        let (md_enc_tx, md_enc_worker_rx) = std::sync::mpsc::channel();
        let (md_enc_res_tx, md_enc_res_rx) = std::sync::mpsc::channel();
        self.app.attach_md_encoder(md_enc_tx);
        std::thread::spawn(move || {
            crate::app::md_encode_worker(picker, md_enc_worker_rx, md_enc_res_tx)
        });
        self.md_enc_rx = Some(md_enc_res_rx);

        self
    }

    /// Wait for the next inline-Markdown-image decode result and apply it (the run loop's
    /// `rx.md_img.try_recv()` step), then redraw — which is what feeds a decoded image back into
    /// `ensure_md_image`'s "request an encode" branch on the next frame.
    #[track_caller]
    fn drain_md_images(&mut self) {
        let rx = self
            .md_img_rx
            .as_ref()
            .expect("with_media() を呼んでいない");
        let res = rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("decode worker が結果を返す");
        assert!(self.app.apply_md_image(res), "現世代の結果は適用される");
        self.draw();
    }

    /// Wait for the next inline-image encode result and apply it (the run loop's
    /// `rx.md_enc.try_recv()` step), then redraw.
    #[track_caller]
    fn drain_md_encodes(&mut self) {
        let rx = self
            .md_enc_rx
            .as_ref()
            .expect("with_media() を呼んでいない");
        let res = rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("encode worker が結果を返す");
        assert!(self.app.apply_md_encode(res), "現世代の結果は適用される");
        self.draw();
    }

    /// Wait for the next full-screen media-load result (SVG/GIF/PDF/video/standalone-mermaid/
    /// fullscreen-fence) and apply it (the run loop's `rx.media.try_recv()` step), then redraw.
    #[track_caller]
    fn drain_media(&mut self) {
        let rx = self.media_rx.as_ref().expect("with_media() を呼んでいない");
        let res = rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("media loader が結果を返す");
        assert!(self.app.apply_media(res), "現世代の結果は適用される");
        self.draw();
    }

    /// Wait for the next remote (http/https) Markdown image fetch's completion and apply it (the
    /// run loop's `rx.md_remote.try_recv()` step), then redraw.
    #[track_caller]
    fn drain_remote(&mut self) {
        let rx = self
            .md_remote_rx
            .as_ref()
            .expect("with_media() を呼んでいない");
        let res = rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("remote fetch worker が結果を返す");
        assert!(self.app.apply_remote_fetch(res), "常に再描画を要求する");
        self.draw();
    }

    /// Wait for the in-flight file operation's result and apply it (the run loop's
    /// `while let Ok(result) = rx.fileop.try_recv()` step), then redraw.
    #[track_caller]
    fn drain_file_ops(&mut self) {
        let rx = self
            .fileop_rx
            .as_ref()
            .expect("with_async_file_ops() を呼んでいない");
        let res = rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("ワーカーが結果を返す");
        assert!(self.app.apply_file_op(res), "現世代の結果は適用される");
        self.draw();
    }

    /// Wait for the in-flight git write's result and apply it (the run loop's
    /// `while let Ok(result) = rx.gitop.try_recv()` step), then redraw.
    #[cfg(feature = "git")]
    #[track_caller]
    fn drain_git_ops(&mut self) {
        let rx = self
            .gitop_rx
            .as_ref()
            .expect("with_async_git_ops() を呼んでいない");
        let res = rx
            .recv_timeout(std::time::Duration::from_secs(30))
            .expect("ワーカーが結果を返す");
        assert!(self.app.apply_git_op(res), "現世代の結果は適用される");
        self.draw();
    }

    fn draw(&mut self) {
        let app = &mut self.app;
        self.term.draw(|f| ui::render(f, app)).expect("draw");
    }

    fn press(&mut self, code: KeyCode, mods: KeyModifiers) {
        let res = handle_key(&mut self.app, KeyEvent::new(code, mods));
        // Same as the run loop, a recoverable Err becomes a flash (a simplified equivalent of resolve_key_result).
        match res {
            Ok(true) => self.quit = true,
            Ok(false) => {}
            Err(e) => self.app.flash = Some(format!("error: {e:#}")),
        }
        self.draw();
    }

    fn key(&mut self, c: char) {
        self.press(KeyCode::Char(c), KeyModifiers::NONE);
    }

    /// Type a sequence of plain character keys.
    fn keys(&mut self, s: &str) {
        for c in s.chars() {
            self.key(c);
        }
    }

    fn ctrl(&mut self, c: char) {
        self.press(KeyCode::Char(c), KeyModifiers::CONTROL);
    }

    fn enter(&mut self) {
        self.press(KeyCode::Enter, KeyModifiers::NONE);
    }

    fn esc(&mut self) {
        self.press(KeyCode::Esc, KeyModifiers::NONE);
    }

    fn tab(&mut self) {
        self.press(KeyCode::Tab, KeyModifiers::NONE);
    }

    /// The full screen as one string (rows joined by \n).
    fn screen(&self) -> String {
        let buf = self.term.backend().buffer();
        let w = buf.area.width as usize;
        let mut out = String::new();
        for (i, cell) in buf.content().iter().enumerate() {
            out.push_str(cell.symbol());
            if (i + 1) % w == 0 {
                out.push('\n');
            }
        }
        out
    }

    #[track_caller]
    fn see(&self, needle: &str) {
        assert!(
            self.screen().contains(needle),
            "画面に「{needle}」が見えるはず:\n{}",
            self.screen()
        );
    }

    #[track_caller]
    fn dont_see(&self, needle: &str) {
        assert!(
            !self.screen().contains(needle),
            "画面に「{needle}」は見えないはず:\n{}",
            self.screen()
        );
    }

    /// Move the tree cursor onto the entry whose path ends with `suffix`.
    #[track_caller]
    fn select(&mut self, suffix: &str) {
        let i = self
            .app
            .tab
            .entries
            .iter()
            .position(|e| e.path.to_string_lossy().ends_with(suffix))
            .unwrap_or_else(|| panic!("entry not found: {suffix}"));
        self.app.tab.selected = i;
        self.draw();
    }

    // ---- Style inspection (H2: konoma leans on "sentinel styles" — REVERSED focus, colored
    // gutters/markers — that plain `screen()` string matching cannot see. These helpers read the
    // drawn buffer's per-cell `Style` so a test can assert on it directly.) ----

    /// The style of the cell at (row, col) in the last drawn frame.
    fn cell_style(&self, row: u16, col: u16) -> ratatui::style::Style {
        let buf = self.term.backend().buffer();
        buf[(col, row)].style()
    }

    /// The (row, col) of the first cell where `needle` starts, searching row by row from the top.
    /// Only matches within a single row (konoma's screen rows are independent grids; a needle that
    /// wraps across rows will not be found — none of this harness's needles do). Multi-byte glyphs
    /// (CJK, icons) are handled the same way `screen()` renders them: one cell's `symbol()` may be
    /// several bytes, and the byte→column mapping below accounts for that.
    fn find_text(&self, needle: &str) -> Option<(u16, u16)> {
        let buf = self.term.backend().buffer();
        let w = buf.area.width;
        let h = buf.area.height;
        for row in 0..h {
            let mut row_str = String::new();
            let mut col_at_byte: Vec<u16> = Vec::new();
            for col in 0..w {
                let sym = buf[(col, row)].symbol();
                for _ in 0..sym.len() {
                    col_at_byte.push(col);
                }
                row_str.push_str(sym);
            }
            if let Some(byte_idx) = row_str.find(needle) {
                let col = col_at_byte.get(byte_idx).copied().unwrap_or(0);
                return Some((row, col));
            }
        }
        None
    }

    /// The style of the first cell of `needle`'s first on-screen occurrence, if any.
    fn style_of(&self, needle: &str) -> Option<ratatui::style::Style> {
        self.find_text(needle).map(|(r, c)| self.cell_style(r, c))
    }

    /// Assert `needle` is on screen and its (first cell's) style satisfies `pred`. Fails loudly —
    /// both when `needle` cannot be found at all, and when it's found but styled wrong — with the
    /// full screen dumped either way, so a failure is diagnosable without rerunning under a debugger.
    #[track_caller]
    fn see_styled(&self, needle: &str, pred: impl Fn(&ratatui::style::Style) -> bool, what: &str) {
        let Some((row, col)) = self.find_text(needle) else {
            panic!(
                "画面に「{needle}」が見えるはず({what}を確認できない):\n{}",
                self.screen()
            );
        };
        let style = self.cell_style(row, col);
        assert!(
            pred(&style),
            "「{needle}」のスタイルが {what} を満たさない(実際: {style:?}, 位置=({row},{col})):\n{}",
            self.screen()
        );
    }

    /// How many distinct foreground colors appear in row `row` (e.g. to confirm a rainbow-colored
    /// table row actually alternates colors instead of being uniformly styled).
    fn distinct_fg_in_row(&self, row: u16) -> usize {
        let buf = self.term.backend().buffer();
        let w = buf.area.width;
        let mut set = std::collections::HashSet::new();
        for col in 0..w {
            set.insert(buf[(col, row)].fg);
        }
        set.len()
    }

    /// Every (row, col) currently drawn with the REVERSED modifier.
    fn reversed_cells(&self) -> Vec<(u16, u16)> {
        use ratatui::style::Modifier;
        let buf = self.term.backend().buffer();
        let w = buf.area.width;
        let h = buf.area.height;
        let mut out = Vec::new();
        for row in 0..h {
            for col in 0..w {
                if buf[(col, row)].modifier.contains(Modifier::REVERSED) {
                    out.push((row, col));
                }
            }
        }
        out
    }
}

/// Fresh, process-unique sandbox dir under the OS temp dir (recreated per call). The name is
/// suffixed with the PID + a process-global counter — the same pattern already used by
/// `app/tests.rs`'s `unique_tmp` and `git.rs`'s `unique_tmp` ("a fixed path would collide across
/// parallel test runs"). A bare `name`-keyed path let two *concurrently running processes* that
/// both execute this same test race on `remove_dir_all()` + `create_dir_all()` + file writes in
/// the identical shared directory (e.g. an overlapping/orphaned `cargo test` invocation, a rerun
/// while a previous run was still in flight, or parallel CI jobs sharing a temp mount): one
/// process's sandbox reset can delete a fixture file out from under another process's still-running
/// test. Root-caused via `e2e_ui_inline_gif_animates_through_real_decode_worker`'s intermittent
/// "encode worker が結果を返す: Timeout" — two processes both looping that single test reproduced it
/// reliably (~4% of runs): `resolve_md_image_path`'s `Path::is_file()` transiently observed
/// `anim.gif` as missing mid-recreation-by-the-other-process, so `ensure_md_image` silently
/// returned without ever queuing the second encode request, and `drain_md_encodes()` then waited
/// on a message nobody had sent.
fn sandbox(name: &str) -> std::path::PathBuf {
    let dir = crate::test_support::unique_tmp(&format!("konoma_e2e_{name}"));
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

/// Standard file set for browsing scenarios.
fn seed_files(dir: &std::path::Path) {
    std::fs::create_dir_all(dir.join("src")).unwrap();
    std::fs::write(dir.join("src/lib.rs"), "pub fn hi() {}\n").unwrap();
    std::fs::write(
        dir.join("readme.md"),
        "# Title\n\nhello [go](./notes.txt)\n\n- [ ] task one\n",
    )
    .unwrap();
    std::fs::write(dir.join("notes.txt"), "alpha\nbeta\ngamma\ndelta\n").unwrap();
    std::fs::write(dir.join("data.csv"), "a,b\n1,2\n3,4\n").unwrap();
    std::fs::write(dir.join(".hidden.txt"), "secret\n").unwrap();
}

fn canon(dir: &std::path::Path) -> std::path::PathBuf {
    dir.canonicalize().unwrap()
}

/// Resolves a fixture bundled under the repo's `samples/` directory, anchored at
/// `CARGO_MANIFEST_DIR` (baked in at compile time) rather than a bare relative path. A bare
/// `"samples/…"` resolves against the test binary's **cwd**, which is only the crate root by
/// convention (`cargo test` run from elsewhere, e.g. `cd /tmp && cargo test --manifest-path …`, is
/// a real, supported invocation) — the tests that used to build one directly (`std::fs::copy`
/// straight off a bare relative path, with no existence check at all) would then hard-fail with an
/// IO error from a cwd that has nothing to do with whether the fixture is actually missing.
/// Tolerant of the one case where the fixture is legitimately absent — `samples/` is excluded from
/// the published crate (`Cargo.toml`'s `exclude`) — by returning `None` so the caller can skip,
/// same as the rest of this codebase's samples-gated tests, but saying so loudly (`eprintln!`,
/// visible with `--nocapture` or in the captured-output dump whenever the process later exits
/// non-zero for any reason) instead of silently passing zero assertions.
fn sample_path_or_skip(name: &str) -> Option<std::path::PathBuf> {
    let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("samples")
        .join(name);
    if p.exists() {
        Some(p)
    } else {
        eprintln!(
            "SKIP: samples/{name} not found (excluded from the published crate) — this test verifies nothing this run"
        );
        None
    }
}

/// A config with `code_bg = "none"` — the real setting that broke the first bg-based code
/// detection (inline code / code blocks then have no background, only `fg(White)` / the `▎` gutter).
/// Tests must exercise this so a bg-only skip can never regress unnoticed again.
#[cfg(test)]
fn cfg_code_bg_none() -> Config {
    let mut cfg = Config::default();
    cfg.ui.theme.code_bg = "none".into();
    cfg
}

/// Write `name.md` with `body` in a fresh sandbox, open it in a Sim under `cfg` (tree → preview),
/// and return the Sim (at the Markdown preview) plus the sandbox dir. Runs the full render pipeline,
/// so `md_link_targets()` and the drawn screen reflect autolink/emoji/alerts exactly as live.
#[cfg(test)]
fn md_preview(cfg: Config, name: &str, body: &str) -> (Sim, std::path::PathBuf) {
    let dir = sandbox(name);
    std::fs::write(dir.join(format!("{name}.md")), body).unwrap();
    let root = canon(&dir);
    let mut s = Sim::with_config(&root, cfg);
    s.select(&format!("{name}.md"));
    s.enter();
    (s, dir)
}

// =============================================================================
// Tree: movement, filtering, hidden files, sort
// =============================================================================

#[test]
fn e2e_tree_navigation_and_ends() {
    let dir = sandbox("tree_nav");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.see("TREE");
    let first = s.app.tab.selected;
    s.key('j');
    assert_eq!(s.app.tab.selected, first + 1, "j で1つ下へ");
    s.key('k');
    assert_eq!(s.app.tab.selected, first, "k で戻る");
    s.key('G');
    assert_eq!(s.app.tab.selected, s.app.tab.entries.len() - 1, "G で末尾");
    s.key('g');
    assert_eq!(s.app.tab.selected, 0, "g で先頭");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_tree_filter_narrows_and_esc_clears() {
    let dir = sandbox("tree_filter");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    let all = s.app.tab.entries.len();
    s.key('/');
    s.keys("csv");
    s.see("data.csv");
    s.dont_see("readme.md");
    assert!(s.app.tab.entries.len() < all, "絞り込みで件数が減る");
    s.enter(); // confirm
    s.see("data.csv");
    s.esc(); // clear
    assert_eq!(s.app.tab.entries.len(), all, "Esc で全件に戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_filter_over_budget_stays_usable_and_fills_in_from_the_worker() {
    // The same journey as `e2e_tree_filter_narrows_and_esc_clears`, but through the **hand-off**
    // path: the scan's budget is forced to zero, so `/` returns with only part of the tree
    // collected and the rest arrives from a worker while the user is already typing.
    //
    // Driven entirely by real key input through `handle_key`, which is what makes it worth having
    // on top of the unit tests: it proves the budgeted start, the worker result and the redraw
    // compose end to end, not just that each piece behaves in isolation.
    crate::app::set_filter_scan_budget(Some(Some(std::time::Duration::ZERO)));
    let dir = sandbox("filter_budget");
    seed_files(&dir);
    // Enough directories that a one-directory-at-a-time walk really does get interrupted.
    for i in 0..6 {
        let sub = dir.join(format!("pkg{i}"));
        std::fs::create_dir_all(&sub).unwrap();
        std::fs::write(sub.join(format!("deep{i}.csv")), "a,b\n").unwrap();
    }
    let root = canon(&dir);
    let mut s = Sim::new(&root);
    let (tx, rx) = std::sync::mpsc::channel();
    s.app.attach_filter_pool_loader(tx);

    s.key('/');
    s.keys("deep");
    // The key press returned and the UI is live even though the tree is not fully collected yet.
    s.see("/deep");

    // Drain the worker the way the run loop does, then redraw.
    let res = rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap();
    assert!(s.app.apply_filter_pool(res), "ワーカーの結果が適用される");
    assert!(
        rx.try_recv().is_err(),
        "受け渡しは1回で完結する(残りは期限なしで完走するので追加の往復は起きない)"
    );
    s.draw();

    for i in 0..6 {
        s.see(&format!("deep{i}.csv"));
    }
    s.dont_see("readme.md");
    s.esc();
    s.see("readme.md");
    crate::app::set_filter_scan_budget(None);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_filter_cursor_keeps_its_file_when_the_worker_result_reorders_the_list() {
    // The window this closes, driven by real keys: `/` on a big tree hands the tail to a worker and
    // the user is *expected* to be picking a target out of the partial list while it finishes.
    // `filter_mode = "fuzzy"` (the default) ranks by score, so the arrival renumbers the list — and
    // a cursor that was only range-clamped then pointed at a different file, silently retargeting
    // the very next `Enter` / `y` / `Space→d`. Every step below goes through `handle_key`.
    crate::app::set_filter_scan_budget(Some(Some(std::time::Duration::ZERO)));
    let dir = sandbox("filter_reorder");
    for i in 0..3 {
        std::fs::write(dir.join(format!("x_a_b_c_{i}.txt")), b"weak\n").unwrap();
    }
    // In a subdirectory, so the zero budget leaves it for the worker: a far better match for "abc"
    // than anything the synchronous half collected, which is what makes the arrival reorder.
    std::fs::create_dir_all(dir.join("pkg")).unwrap();
    std::fs::write(dir.join("pkg/abc.txt"), b"strong\n").unwrap();
    let root = canon(&dir);
    let mut s = Sim::new(&root);
    let (tx, rx) = std::sync::mpsc::channel();
    s.app.attach_filter_pool_loader(tx);

    s.key('/');
    s.keys("abc");
    s.enter(); // commit the query — from here the results are navigated normally
    s.key('j'); // move off the first row, so "reset to 0" would fail too
    let before = s.app.tab.selected;
    assert_eq!(before, 1, "土台: カーソルは 2 行目");
    let target = s.app.tab.entries[before].path.clone();
    let target_name = target.file_name().unwrap().to_string_lossy().into_owned();
    assert!(target_name.starts_with("x_a_b_c_"), "土台: {target_name}");

    // The run loop applying the worker's result, then redrawing.
    let res = rx.recv_timeout(std::time::Duration::from_secs(10)).unwrap();
    assert!(s.app.apply_filter_pool(res), "ワーカーの結果が適用される");
    s.draw();

    s.see("abc.txt");
    let now = s
        .app
        .tab
        .entries
        .iter()
        .position(|e| e.path == target)
        .expect("対象は今も一致する");
    assert_ne!(
        now, before,
        "土台: 到着で順位が入れ替わっていないとこのテストは何も検出しない"
    );
    assert_eq!(
        s.app.tab.entries[s.app.tab.selected].path, target,
        "カーソルは同じファイルを指し続ける"
    );

    // 1) Enter previews the file the user was looking at, not whatever took its row.
    s.enter();
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert_eq!(
        s.app.tab.preview_path.as_deref(),
        Some(target.as_path()),
        "Enter が開くのはカーソル下のファイル"
    );
    s.see("weak");
    s.key('q');
    assert_eq!(
        s.app.tab.entries[s.app.tab.selected].path, target,
        "戻ってもカーソルは同じファイル"
    );

    // 2) `y` → `f` copies that file's path. Assert the *value* via the clipboard-independent
    // getter (copy_string_for = what copy_path would write) — headless CI has no clipboard, so
    // reading `flash` (which reports the round-trip's success/failure) is flaky there. The key
    // wiring itself (leader opens, then is consumed) is still exercised via real keys below.
    s.key('y');
    assert_eq!(
        s.app.pending_leader,
        Some(crate::keymap::LeaderId::Copy),
        "y でコピーリーダーが開く"
    );
    let copied = s
        .app
        .copy_string_for(crate::app::CopyKind::Full)
        .expect("コピー対象が存在する");
    s.key('f');
    assert_eq!(s.app.pending_leader, None, "y f でリーダーが消費される");
    assert_eq!(
        copied,
        target.display().to_string(),
        "y f がコピーするのはカーソル下のファイル"
    );
    assert!(
        !copied.contains("abc.txt"),
        "先頭を奪った別ファイルではない: {copied}"
    );

    // 3) `Space` → `d` → `!` deletes that file and no other. The decisive one: an off-by-one here
    //    is unrecoverable.
    s.key(' ');
    s.key('d');
    s.key('!');
    assert!(!target.exists(), "消えたのはカーソル下のファイル");
    assert!(
        dir.join("pkg/abc.txt").exists(),
        "順位を奪った別ファイルは無傷"
    );

    crate::app::set_filter_scan_budget(None);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_tree_hidden_toggle() {
    let dir = sandbox("tree_hidden");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.dont_see(".hidden.txt");
    s.key('.');
    s.see(".hidden.txt");
    s.key('.');
    s.dont_see(".hidden.txt");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_tree_descend_and_parent() {
    let dir = sandbox("tree_descend");
    seed_files(&dir);
    let root = canon(&dir);
    let mut s = Sim::new(&root);
    s.select("src");
    s.key('l');
    assert!(
        s.app.tab.root.ends_with("src"),
        "l でディレクトリへ潜る(root 変更)"
    );
    s.see("lib.rs");
    s.key('h');
    assert_eq!(s.app.tab.root, root, "h で親へ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_sort_menu_switches_order() {
    let dir = sandbox("sort");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('s');
    s.see("sort"); // the sort menu's footer/display
    s.key('s'); // by size
                // The menu closes and the order has changed (directories still stay first).
    assert!(!s.app.is_sort_menu());
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// File operations: create, rename, delete (cancel and permanent delete)
// =============================================================================

#[test]
fn e2e_file_create_via_dialog() {
    let dir = sandbox("file_create");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    // The creation target is "the directory at the cursor position" (op_base_dir). To create directly
    // under root, put the cursor on a file directly under root (its parent = root becomes the creation target).
    s.select("notes.txt");
    s.key(' ');
    s.key('n'); // Space→n create
    s.keys("fresh.txt");
    s.enter();
    assert!(
        dir.join("fresh.txt").exists(),
        "作成したファイルがディスクに存在"
    );
    s.see("fresh.txt");

    // Putting the cursor on a directory creates it inside that directory (as op_base_dir is designed to).
    s.select("src");
    s.key(' ');
    s.key('n');
    s.keys("inner.txt");
    s.enter();
    assert!(
        dir.join("src/inner.txt").exists(),
        "ディレクトリ選択時はその中に作成される"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Creating a file that already exists must flash in whichever language the UI is configured for
/// (not a fixed language regardless of `ui.lang`) — the bug this whole i18n pass fixes.
#[test]
fn e2e_file_create_existing_name_flashes_in_ui_language() {
    let dir_en = sandbox("file_create_existing_name_en");
    seed_files(&dir_en);
    let mut cfg_en = Config::default();
    cfg_en.ui.lang = "en".into();
    let mut s = Sim::with_config(&canon(&dir_en), cfg_en);
    s.select("notes.txt");
    s.key(' ');
    s.key('n'); // Space→n create
    s.keys("notes.txt"); // op_base_dir = notes.txt's parent (root), which already has notes.txt
    s.enter();
    let flash_en = s.app.flash.clone().unwrap_or_default();
    assert!(
        flash_en.contains("already exists: "),
        "英語 UI では英語で: {flash_en:?}"
    );
    assert!(
        !flash_en.contains("既に存在"),
        "英語 UI に日本語が混ざってはならない: {flash_en:?}"
    );
    assert!(
        !dir_en.join("notes.txt.tmp").exists(),
        "衝突時は何も作られない"
    ); // sanity: the collision really was rejected, not silently overwritten

    let dir_jp = sandbox("file_create_existing_name_jp");
    seed_files(&dir_jp);
    let mut cfg_jp = Config::default();
    cfg_jp.ui.lang = "jp".into();
    let mut s2 = Sim::with_config(&canon(&dir_jp), cfg_jp);
    s2.select("notes.txt");
    s2.key(' ');
    s2.key('n');
    s2.keys("notes.txt");
    s2.enter();
    let flash_jp = s2.app.flash.clone().unwrap_or_default();
    assert!(
        flash_jp.contains("既に存在します: "),
        "日本語 UI では日本語で: {flash_jp:?}"
    );

    std::fs::remove_dir_all(&dir_en).ok();
    std::fs::remove_dir_all(&dir_jp).ok();
}

#[test]
fn e2e_file_duplicate_in_place() {
    let dir = sandbox("file_duplicate");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    // Cursor on notes.txt → Space→D to duplicate → notes copy.txt is created in place.
    s.select("notes.txt");
    s.key(' ');
    s.key('D');
    assert!(
        dir.join("notes copy.txt").exists(),
        "複製 notes copy.txt がディスクに存在"
    );
    assert_eq!(
        std::fs::read(dir.join("notes copy.txt")).unwrap(),
        std::fs::read(dir.join("notes.txt")).unwrap(),
        "複製の内容が元と同一"
    );
    s.see("notes copy.txt"); // it appears in the tree (refresh+reveal), selected
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_file_rename_via_dialog() {
    let dir = sandbox("file_rename");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.key(' ');
    s.key('r');
    // Expects the existing name to be prefilled → clear it all and type the new name.
    for _ in 0..30 {
        s.press(KeyCode::Backspace, KeyModifiers::NONE);
    }
    s.keys("renamed.txt");
    s.enter();
    assert!(dir.join("renamed.txt").exists(), "新名で存在");
    assert!(!dir.join("notes.txt").exists(), "旧名は消える");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_file_delete_cancel_and_permanent() {
    let dir = sandbox("file_delete");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.key(' ');
    s.key('d');
    // A confirmation dialog appears → n to cancel (the file remains).
    s.key('n');
    assert!(dir.join("notes.txt").exists(), "取消で残る");
    // Once more → `!` to permanently delete (doesn't touch the trash).
    s.key(' ');
    s.key('d');
    s.key('!');
    assert!(!dir.join("notes.txt").exists(), "完全削除で消える");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Preview: text/Markdown/links/checkboxes/CSV/search
// =============================================================================

#[test]
fn e2e_preview_text_roundtrip() {
    let dir = sandbox("preview_text");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.enter();
    assert_eq!(s.app.tab.mode, Mode::Preview);
    s.see("PREVIEW");
    s.see("alpha");
    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree, "q でツリーへ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

// A file with (almost) no newlines — minified JS, a one-line JSON log — must behave like any other
// text preview: the draw returns, scrolling works, and `q` goes back. `read_lines` runs inside
// `terminal.draw`, so the per-line cap is what keeps that true; the allocation bound below is what
// gives this test teeth (uncapped, opening a 4 MiB one-line file allocated ~21 MB in the frame).
#[test]
fn e2e_preview_one_huge_line_file_stays_responsive() {
    let dir = sandbox("preview_huge_line");
    seed_files(&dir);
    let huge = "x".repeat(4 * 1024 * 1024);
    std::fs::write(
        dir.join("min.txt"),
        format!("{huge}\nsecond line\nlast line\n"),
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    // Warm up on an ordinary file first, so one-time process-global init isn't attributed below.
    s.select("notes.txt");
    s.enter();
    s.key('q');

    s.select("min.txt");
    let open_alloc = crate::mem_tests::allocated_by(|| s.enter());
    assert_eq!(s.app.tab.mode, Mode::Preview, "巨大1行でもプレビューに入る");
    s.see("PREVIEW");
    s.see("xxxxxxxx");
    assert!(
        open_alloc < 2 * 1024 * 1024,
        "4MiB の1行ファイルを開く1フレームが {open_alloc} バイト確保した(行を丸ごと実体化?)"
    );
    // Scrolling (the window re-reads on every step, so an uncapped read would be paid again here).
    let scroll_alloc = crate::mem_tests::allocated_by(|| {
        s.key('j');
        s.key('k');
    });
    assert!(
        scroll_alloc < 2 * 1024 * 1024,
        "巨大1行のスクロール2回が {scroll_alloc} バイト確保した"
    );
    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree, "q でツリーへ戻る");

    // Again with `wrap = false` (the horizontal-scroll setting): now one logical line is one row,
    // so the lines after the huge one are reachable and `l` actually moves along it.
    let mut cfg = Config::default();
    cfg.ui.wrap = false;
    let mut s = Sim::with_config(&canon(&dir), cfg);
    s.select("min.txt");
    s.enter();
    s.see("xxxxxxxx");
    s.key('G');
    s.see("last line");
    s.key('g');
    s.see("xxxxxxxx");
    // `l` walks the 2D caret along the line and the unwrapped view follows it horizontally; `$`
    // goes to its last character. Read the column through the visual selection (the public view of
    // the caret): on a 4 MiB line it must land inside the readable prefix, not at column ~4,194,303.
    s.key('v'); // charwise visual, anchored at column 0
    s.keys("lll");
    s.press(KeyCode::Char('$'), KeyModifiers::NONE);
    let end_col = match s.app.preview_selection() {
        crate::app::PreviewSelection::Char { start, end } => {
            assert_eq!(start.1, 0, "アンカーは行頭");
            end.1
        }
        other => panic!("charwise 選択のはず: {other:?}"),
    };
    assert!(end_col > 2, "l/$ でキャレットが行に沿って進む: {end_col}");
    assert!(
        end_col < 100_000,
        "$ のキャレット列が行長に比例している(上限が効いていない?): {end_col}"
    );
    s.esc(); // leave visual
    s.see("xxxxxxxx");
    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree, "q でツリーへ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_link_follow_and_back() {
    let dir = sandbox("md_link");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("readme.md");
    s.enter();
    s.see("Title");
    s.tab(); // focus the link "go"
    s.enter(); // open ./notes.txt
    s.see("alpha");
    assert!(
        s.app
            .tab
            .preview_path
            .as_deref()
            .is_some_and(|p| p.ends_with("notes.txt")),
        "リンク先へ遷移"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_checkbox_space_toggles_file() {
    let dir = sandbox("md_checkbox");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("readme.md");
    s.enter();
    s.tab(); // link
    s.tab(); // checkbox
    s.key(' ');
    let src = std::fs::read_to_string(dir.join("readme.md")).unwrap();
    assert!(
        src.contains("- [x] task one"),
        "Space でファイルに書き戻る: {src}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_csv_table_cell_navigation() {
    let dir = sandbox("csv_nav");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("data.csv");
    s.enter();
    s.see("TABLE");
    let r0 = s.app.table_cursor();
    s.key('j');
    s.key('l');
    let r1 = s.app.table_cursor();
    assert_ne!(r0, r1, "hjkl でセルカーソルが動く");
    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_zip_archive_opens_as_table() {
    let dir = sandbox("zip_archive");
    {
        use std::io::Write as _;
        let f = std::fs::File::create(dir.join("bundle.zip")).unwrap();
        let mut zw = zip::ZipWriter::new(f);
        let opts = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        zw.start_file("one.txt", opts).unwrap();
        zw.write_all(b"hello").unwrap();
        zw.start_file("two.txt", opts).unwrap();
        zw.write_all(b"world").unwrap();
        zw.finish().unwrap();
    }
    let mut s = Sim::new(&canon(&dir));
    s.select("bundle.zip");
    s.enter();
    s.see("TABLE");
    s.see("one.txt");
    s.see("two.txt");
    assert!(s.app.is_table_preview());
    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree);
    std::fs::remove_dir_all(&dir).ok();
}

/// `Enter`'s full-text popup: the grid truncates at column width (40), but the popup shows the raw
/// full text. A continuous run far past the truncation cap (39 consecutive + `…`) being visible on screen proves there is no truncation.
#[test]
fn e2e_table_cell_popup_shows_full_text_not_truncated() {
    let dir = sandbox("cellpopup_long");
    let long = "x".repeat(80);
    std::fs::write(dir.join("wide.csv"), format!("h1,h2\nshort,{long}\n")).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("wide.csv");
    s.enter();
    s.see("TABLE");
    s.dont_see(&"x".repeat(40)); // the grid's truncation cap is 39 consecutive + "…".
    s.key('l'); // move to the long cell (h2 column).
    s.enter(); // open the popup.
    s.see("CELL");
    s.see(&"x".repeat(45)); // a continuous run far past the truncation cap is visible.
    assert_eq!(
        s.app.table_cell_view().unwrap().text,
        long,
        "ポップアップの中身は生の全文(切り詰め無し)"
    );
    s.key('q');
    s.see("TABLE");
    s.dont_see("CELL");
    assert_eq!(
        s.app.tab.mode,
        Mode::Preview,
        "q はポップアップだけ閉じる(テーブルへ留まる・ツリーへは戻らない)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// A newline inside CSV quotes (e.g. a multi-line address field) stays intact (as a real newline) in
/// the popup's content. Also confirms it can be closed with Esc too (not just `q`).
#[test]
fn e2e_table_cell_popup_shows_multiline_cell_and_esc_closes() {
    let dir = sandbox("cellpopup_multiline");
    std::fs::write(
        dir.join("addr.csv"),
        "name,address\nDoe,\"123 Main St\nSuite 4\nCity\"\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("addr.csv");
    s.enter();
    s.see("TABLE");
    s.key('l'); // move to the address column (contains a newline).
    s.enter();
    s.see("CELL");
    assert_eq!(
        s.app.table_cell_view().unwrap().text,
        "123 Main St\nSuite 4\nCity",
        "引用符内の実改行がそのまま保持される"
    );
    s.esc();
    s.dont_see("CELL");
    assert_eq!(s.app.tab.mode, Mode::Preview, "Esc もツリーへは戻らない");
    std::fs::remove_dir_all(&dir).ok();
}

/// The archive listing also goes through the same `TableData`, so the cell popup works via real keystrokes too.
#[test]
fn e2e_zip_archive_cell_popup_shows_full_name_via_enter() {
    let dir = sandbox("cellpopup_zip");
    let long_name = format!("{}.txt", "x".repeat(90));
    {
        use std::io::Write as _;
        let f = std::fs::File::create(dir.join("bundle.zip")).unwrap();
        let mut zw = zip::ZipWriter::new(f);
        let opts = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        zw.start_file(&long_name, opts).unwrap();
        zw.write_all(b"hi").unwrap();
        zw.finish().unwrap();
    }
    let mut s = Sim::new(&canon(&dir));
    s.select("bundle.zip");
    s.enter();
    s.see("TABLE");
    s.enter(); // cursor is on the first cell (Name column) → open the popup.
    s.see("CELL");
    let view = s.app.table_cell_view().unwrap();
    assert_eq!(view.header, "Name");
    assert_eq!(view.text, long_name, "長いエントリ名も切り詰め無しで取れる");
    s.key('q');
    s.see("TABLE");
    std::fs::remove_dir_all(&dir).ok();
}

/// Route decorated Markdown search **through key input**. While staying in decorated display (not
/// switching to raw), scroll to the matching line, and move to the next match with n. Must not break on CJK body text either.
#[test]
fn e2e_decorated_markdown_search() {
    let dir = sandbox("md_search");
    let mut src = String::from("# Guide\n\n");
    for i in 0..30 {
        src.push_str(&format!("padding paragraph {i}\n\n"));
    }
    src.push_str("the **haystack** contains a needle here\n\n");
    for i in 30..60 {
        src.push_str(&format!("padding paragraph {i}\n\n"));
    }
    src.push_str("日本語の行にも needle があります\n");
    std::fs::write(dir.join("guide.md"), &src).unwrap();

    let mut s = Sim::new(&canon(&dir));
    s.select("guide.md");
    s.enter();
    s.key('/');
    s.keys("needle");
    s.enter();
    assert_eq!(
        s.app.search_status(),
        Some((1, 2)),
        "装飾 md で 2 件見つかる"
    );
    assert!(!s.app.is_raw_source(), "装飾表示のまま検索できる");
    s.see("needle");

    s.key('n');
    assert_eq!(s.app.search_status(), Some((2, 2)), "n で 2 件目へ");
    s.see("needle"); // a match on the CJK line also shows up on screen

    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree);
    std::fs::remove_dir_all(&dir).ok();
}

/// Route table search **through key input** too (`/` → query → Enter → n/N). Unit tests hit App's
/// methods directly, so a missing keymap binding or an input surface hijacking the key can only be caught here.
#[test]
fn e2e_table_search_jumps_to_matching_cell() {
    let dir = sandbox("table_search");
    std::fs::write(
        dir.join("data.csv"),
        "name,city\nalice,kyoto\nbob,osaka\ncarol,kyoto\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("data.csv");
    s.enter();
    s.see("TABLE");
    assert_eq!(s.app.table_cursor(), (0, 0));

    s.key('/');
    s.keys("kyoto");
    s.enter();
    assert_eq!(s.app.table_cursor(), (0, 1), "最初の一致セルへ");
    assert_eq!(s.app.search_status(), Some((1, 2)), "2 件中 1 件目");

    s.key('n');
    assert_eq!(s.app.table_cursor(), (2, 1), "n で次の一致セルへ");
    s.key('n');
    assert_eq!(s.app.table_cursor(), (0, 1), "wrap して先頭へ");

    // Esc is "clear search → once more to the tree" (the same convention as the text preview).
    // A real-terminal bug where Esc did nothing = the highlight couldn't be cleared was found, so pin it down here.
    s.esc();
    assert_eq!(s.app.preview_search_query(), None, "Esc で検索が解除される");
    assert!(!s.app.table_cell_is_hit(0, 1), "一致の強調も消える");
    assert_eq!(s.app.tab.mode, Mode::Preview, "1回目の Esc では表に留まる");
    s.esc();
    assert_eq!(s.app.tab.mode, Mode::Tree, "2回目の Esc でツリーへ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_preview_search_moves_between_matches() {
    let dir = sandbox("preview_search");
    let big: String = (0..300)
        .map(|i| {
            if i % 100 == 0 {
                format!("needle line {i}\n")
            } else {
                format!("filler {i}\n")
            }
        })
        .collect();
    std::fs::write(dir.join("big.txt"), &big).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("big.txt");
    s.enter();
    s.key('/');
    s.keys("needle");
    s.enter();
    s.see("needle line 0"); // to the first match position
    s.key('n'); // to the next match → the screen moves to the match at line 100
    s.see("needle line 100");
    s.key('N'); // back to the previous
    s.see("needle line 0");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Tabs, tab list
// =============================================================================

#[test]
fn e2e_tabs_full_lifecycle() {
    let dir = sandbox("tabs_life");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.keys("tt"); // 3 tabs
    assert_eq!(s.app.tab_count(), 3);
    s.see("3:");
    s.key('1');
    assert_eq!(s.app.active_tab_index(), 0, "数字で直行");
    s.key(']');
    assert_eq!(s.app.active_tab_index(), 1, "] で次");
    s.key('T');
    assert!(s.app.is_tab_list());
    s.see("Tabs");
    s.key('j');
    s.enter();
    assert!(!s.app.is_tab_list(), "Enter で切替+一覧が閉じる");
    assert_eq!(s.app.active_tab_index(), 2);
    s.key('T');
    s.key('d');
    assert_eq!(s.app.tab_count(), 2, "一覧の d で選択タブを閉じる");
    s.key('q'); // close the list
    assert!(!s.app.is_tab_list());
    // w does nothing by default.
    s.key('w');
    assert_eq!(s.app.tab_count(), 2, "w は未割当");
    // The tree's q closes the tab.
    s.key('q');
    assert_eq!(s.app.tab_count(), 1, "tree の q でタブが閉じる");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ctrl_t_opens_selected_in_new_tab() {
    // In the tree, Ctrl-t = open what's under the cursor in a separate tab. File → preview / directory → new tab's root.
    // The original tab is preserved.
    let dir = sandbox("newtab_open");
    std::fs::write(dir.join("note.txt"), "HELLO_NEWTAB body\n").unwrap();
    std::fs::create_dir_all(dir.join("sub")).unwrap();
    std::fs::write(dir.join("sub/inside.txt"), "INSIDE_MARK\n").unwrap();
    let mut s = Sim::new(&canon(&dir));

    // File: Ctrl-t previews it in a new tab.
    s.select("note.txt");
    assert_eq!(s.app.tab_count(), 1);
    s.ctrl('t');
    assert_eq!(s.app.tab_count(), 2, "Ctrl-t で新規タブができる");
    assert_eq!(s.app.active_tab_index(), 1, "新規タブがアクティブ");
    s.see("HELLO_NEWTAB"); // the file is previewed in the new tab

    // The original tab is preserved: going back with [ shows note.txt in the tree (the preview content isn't visible).
    s.key('[');
    assert_eq!(s.app.active_tab_index(), 0);
    s.see("note.txt");
    s.dont_see("HELLO_NEWTAB");

    // Directory: Ctrl-t makes the new tab's root that folder.
    s.select("sub");
    s.ctrl('t');
    assert_eq!(s.app.tab_count(), 3);
    s.see("inside.txt"); // the new tab shows sub's contents
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Bookmarks
// =============================================================================

#[test]
fn e2e_bookmarks_set_list_jump() {
    let root = sandbox("bm_flow");
    let proj = root.join("proj");
    std::fs::create_dir_all(&proj).unwrap();
    seed_files(&proj);
    let proj = canon(&proj);
    let mut s = Sim::new(&proj);
    s.app.bookmarks = crate::bookmarks::Bookmarks::with_base(root.join("cfg"), &proj);
    s.select("notes.txt");
    s.keys("mb"); // register to b
    s.see("bookmarked");
    s.key('\'');
    s.see("Bookmarks");
    s.key('b'); // a letter jumps directly → the file is previewed
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert!(s
        .app
        .tab
        .preview_path
        .as_deref()
        .is_some_and(|p| p.ends_with("notes.txt")));
    // m while previewing registers the file currently on display.
    s.keys("mz");
    assert!(
        s.app
            .bookmarks
            .get('z')
            .is_some_and(|p| p.ends_with("notes.txt")),
        "プレビュー中の登録対象は表示中ファイル"
    );
    // Open the list and delete the selected one with Ctrl-d (confirms the count drops by one, regardless of order).
    s.key('\'');
    s.see("Bookmarks");
    let before =
        s.app.bookmarks.get('b').is_some() as u8 + s.app.bookmarks.get('z').is_some() as u8;
    assert_eq!(before, 2, "削除前は b/z の2件");
    s.ctrl('d');
    let after = s.app.bookmarks.get('b').is_some() as u8 + s.app.bookmarks.get('z').is_some() as u8;
    assert_eq!(after, 1, "Ctrl-d で選択中の1件が削除される");
    std::fs::remove_dir_all(&root).ok();
}

#[test]
fn e2e_bookmark_overwrite_prompts_confirm_then_applies() {
    // Default (confirm_bookmark_overwrite=true): registering a different path to an already-used key shows a confirmation dialog.
    // n = cancel, stays as-is / y = overwrite, moves to the new path.
    let root = sandbox("bm_overwrite");
    let proj = root.join("proj");
    std::fs::create_dir_all(&proj).unwrap();
    seed_files(&proj);
    let proj = canon(&proj);
    let mut s = Sim::new(&proj);
    s.app.bookmarks = crate::bookmarks::Bookmarks::with_base(root.join("cfg"), &proj);

    // Register notes.txt to 'b' (an unused key = no confirmation).
    s.select("notes.txt");
    s.keys("mb");
    s.see("bookmarked");
    let notes = s.app.bookmarks.get('b').expect("b set");

    // Register data.csv to the same 'b' → a confirmation dialog. Don't overwrite yet.
    s.select("data.csv");
    s.keys("mb");
    s.see("OVERWRITE?");
    s.see("Overwrite bookmark");
    assert_eq!(
        s.app.bookmarks.get('b'),
        Some(notes.clone()),
        "確認前は元のまま"
    );

    // n = cancel.
    s.key('n');
    s.see("canceled");
    assert_eq!(
        s.app.bookmarks.get('b'),
        Some(notes.clone()),
        "取消で notes.txt のまま"
    );

    // Show it again and y = overwrite.
    s.select("data.csv");
    s.keys("mb");
    s.see("OVERWRITE?");
    s.key('y');
    assert!(
        s.app
            .bookmarks
            .get('b')
            .is_some_and(|p| p.ends_with("data.csv")),
        "y で data.csv へ上書き"
    );
    std::fs::remove_dir_all(&root).ok();
}

#[test]
fn e2e_bookmark_overwrite_off_applies_silently() {
    // confirm_bookmark_overwrite=false: even a different path overwrites immediately with no confirmation.
    let root = sandbox("bm_overwrite_off");
    let proj = root.join("proj");
    std::fs::create_dir_all(&proj).unwrap();
    seed_files(&proj);
    let proj = canon(&proj);
    let mut cfg = Config::default();
    cfg.ui.confirm_bookmark_overwrite = false;
    let mut s = Sim::with_config(&proj, cfg);
    s.app.bookmarks = crate::bookmarks::Bookmarks::with_base(root.join("cfg"), &proj);

    s.select("notes.txt");
    s.keys("mb");
    s.select("data.csv");
    s.keys("mb");
    s.dont_see("OVERWRITE?");
    assert!(
        s.app
            .bookmarks
            .get('b')
            .is_some_and(|p| p.ends_with("data.csv")),
        "confirm オフは即上書き"
    );
    std::fs::remove_dir_all(&root).ok();
}

// =============================================================================
// git: hub, stage/unstage, diff, changed filter, follow
// =============================================================================

#[cfg(feature = "git")]
fn seed_repo(dir: &std::path::Path) {
    let run = |args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(dir)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    run(&["init", "-q", "."]);
    run(&["config", "user.email", "t@t"]);
    run(&["config", "user.name", "t"]);
    std::fs::write(dir.join("a.rs"), "fn a() {}\n").unwrap();
    std::fs::write(dir.join("b.rs"), "fn b() {}\n").unwrap();
    run(&["add", "-A"]);
    // Keep the subject "init" as-is (existing tests stay unchanged) + a 2-paragraph body. Multiple -m
    // flags become blank-line-separated paragraphs. Lets the E2E confirm commit details preserve the body's newlines.
    run(&[
        "commit",
        "-q",
        "-m",
        "init",
        "-m",
        "First body paragraph.",
        "-m",
        "Second body paragraph.",
    ]);
    // One non-current branch (for validating the branches list/graph panel). Points at the same commit.
    run(&["branch", "feature-x"]);
    // Set up uncommitted changes.
    std::fs::write(dir.join("a.rs"), "fn a() { let _x = 1; }\n").unwrap();
    std::fs::write(dir.join("new.txt"), "untracked\n").unwrap();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_hub_stage_and_diff() {
    let dir = sandbox("git_hub");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.see("a.rs");
    s.see("new.txt");
    // stage → the display changes (a staged mark). unstage reverts it.
    s.key('s');
    s.key('u');
    // Enter opens the diff; q returns to the hub. `s.see("diff")` alone has no detection power
    // here: the hub's own footer legend (`StGitHubKeys`) always contains the literal substring
    // "diff" (the "Enter:diff" hint), so it would still pass even if `Enter` were a complete
    // no-op that left the hub showing. Assert the actual state transition via `is_git_diff_preview`
    // instead — the same accessor used elsewhere in this file for the same purpose.
    s.enter();
    assert!(
        s.app.is_git_diff_preview(),
        "Enter で全画面 diff プレビューに入る"
    );
    s.see("diff");
    s.key('q');
    assert!(s.app.is_git_view(), "diff の q でハブへ戻る");
    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree);
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_diff_from_tree_and_cycle_files() {
    let dir = sandbox("git_diff_cycle");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("a.rs");
    s.key('d');
    s.see("diff");
    s.see("(");
    // n moves to the next changed file (wraps).
    let before = s.screen();
    s.key('n');
    assert_ne!(before, s.screen(), "n で別ファイルの diff へ");
    s.key('q');
    assert_eq!(s.app.tab.mode, Mode::Tree);
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_changed_filter_and_jumps() {
    let dir = sandbox("changed_filter");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('C');
    s.see("CHANGED");
    assert!(s.app.changed_filter());
    // The 2 changes (a.rs, new.txt) are in the list.
    s.see("a.rs");
    s.see("new.txt");
    s.key('C');
    assert!(!s.app.changed_filter(), "C 再押下で解除");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_follow_mode_chip_toggles() {
    let dir = sandbox("follow_chip");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('F');
    assert!(s.app.follow_enabled());
    s.see("FOLLOW");
    // Maximally sticky (2026-07-23): a normal key does not break follow.
    s.key('j');
    assert!(s.app.follow_enabled(), "j のような通常キーではフォロー維持");
    s.see("FOLLOW");
    // Only broken by pressing F again (via toggle_follow).
    s.key('F');
    assert!(!s.app.follow_enabled(), "F の再押下で解除");
    s.dont_see("FOLLOW");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Help, info, quit
// =============================================================================

#[test]
fn e2e_help_open_close() {
    let dir = sandbox("help");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('?');
    assert!(s.app.show_help);
    s.key('q');
    assert!(!s.app.show_help, "q でヘルプを閉じる");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_quit_confirm_flow() {
    let dir = sandbox("quit");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('Q');
    assert!(!s.quit, "確認ダイアログで即終了しない");
    s.key('n');
    assert!(!s.quit, "n で取消");
    s.key('Q');
    s.key('y');
    assert!(s.quit, "y で終了");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_info_popup() {
    let dir = sandbox("info");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.key('i');
    assert!(s.app.is_info());
    s.see("notes.txt");
    s.key('q');
    assert!(!s.app.is_info());
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Focused simulation of recently-changed complex paths
// (custom task states, focus round-trip, closing the last tab, global BM absolute display,
//  visual selection, copy menu, reversed sort, anchor)
// =============================================================================

#[test]
fn e2e_markdown_task_cycles_custom_states() {
    // Setting ui.md_task_states to [" ", "!", "x"] makes Space cycle through 3 states,
    // writing just 1 character back to the file at each step (v0.6.0's variable checkbox).
    let dir = sandbox("md_task_cycle");
    seed_files(&dir);
    let mut cfg = Config::default();
    cfg.ui.md_task_states = vec![" ".into(), "!".into(), "x".into()];
    let mut s = Sim::with_config(&canon(&dir), cfg);
    s.select("readme.md");
    s.enter();
    s.tab(); // link
    s.tab(); // checkbox
    let read = || std::fs::read_to_string(dir.join("readme.md")).unwrap();
    s.key(' ');
    assert!(
        read().contains("- [!] task one"),
        "1回目: [ ]→[!]: {}",
        read()
    );
    s.key(' ');
    assert!(
        read().contains("- [x] task one"),
        "2回目: [!]→[x]: {}",
        read()
    );
    s.key(' ');
    assert!(
        read().contains("- [ ] task one"),
        "3回目: [x]→[ ] へ巡回: {}",
        read()
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_focus_forward_then_back() {
    // Tab moves focus to the next item, Shift-Tab to the previous, round-tripping.
    let dir = sandbox("md_focus_back");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("readme.md");
    s.enter();
    assert_eq!(s.app.focused_item(), None, "初期はフォーカス無し");
    s.tab();
    let first = s.app.focused_item();
    assert!(first.is_some(), "Tab で最初のアイテムへ");
    s.tab();
    let second = s.app.focused_item();
    assert!(second > first, "2回目の Tab で次のアイテムへ");
    s.press(KeyCode::BackTab, KeyModifiers::SHIFT);
    assert_eq!(
        s.app.focused_item(),
        first,
        "Shift-Tab で前のアイテムへ戻る"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_last_tab_q_quits_when_confirm_off() {
    // With confirm_quit=false and a single tab, the tree's q quits the app (CloseTabOrQuit).
    let dir = sandbox("last_tab_quit");
    seed_files(&dir);
    let mut cfg = Config::default();
    cfg.ui.confirm_quit = false;
    let mut s = Sim::with_config(&canon(&dir), cfg);
    assert_eq!(s.app.tab_count(), 1);
    s.key('q');
    assert!(s.quit, "最後のタブでの q は終了する");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_last_tab_q_confirms_when_confirm_on() {
    // With confirm_quit=ON (default), the last tab's q shows a confirmation dialog and doesn't quit yet.
    let dir = sandbox("last_tab_confirm");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('q');
    assert!(!s.quit, "確認ダイアログでまだ終了しない");
    assert!(s.app.is_dialog(), "終了確認ダイアログが開く");
    s.key('q'); // qq confirms quitting
    assert!(s.quit, "qq で終了");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_global_bookmark_shows_absolute_path() {
    // An uppercase mark = global. The list display shows an absolute path (~ shortened) (v0.7.0).
    let root = sandbox("bm_global");
    let proj = canon(&root);
    seed_files(&proj);
    let mut s = Sim::new(&proj);
    s.app.bookmarks = crate::bookmarks::Bookmarks::with_base(root.join("cfg"), &proj);
    s.select("notes.txt");
    s.keys("mB"); // uppercase = global registration
    s.key('\'');
    let items = s.app.bookmark_list_items();
    let entry = items
        .iter()
        .find(|(_, k, _)| *k == 'B')
        .expect("global bookmark B");
    assert!(!entry.0, "大文字はグローバル(is_local=false)");
    let disp = s.app.bookmark_display_path(entry.0, &entry.2);
    assert!(
        disp.starts_with('/') || disp.starts_with('~'),
        "グローバルは絶対パス表示: {disp}"
    );
    std::fs::remove_dir_all(&root).ok();
}

#[test]
fn e2e_preview_visual_selection_and_cancel() {
    // In a windowed preview, v = charwise / V = linewise selection begins, and Esc cancels it.
    let dir = sandbox("visual_sel");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.enter();
    assert!(s.app.is_windowed(), "テキストは windowed プレビュー");
    s.key('v');
    assert!(s.app.is_preview_visual(), "v で選択開始");
    assert!(!s.app.preview_visual_linewise(), "v は charwise");
    s.key('l'); // move the caret right → the selection grows
    assert!(matches!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char { .. }
    ));
    s.esc();
    assert!(!s.app.is_preview_visual(), "Esc で選択解除");
    // V=linewise
    s.key('V');
    assert!(
        s.app.is_preview_visual() && s.app.preview_visual_linewise(),
        "V は linewise"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_copy_leader_opens_and_cancels() {
    // y opens the copy which-key leader. Esc closes it (copies nothing).
    let dir = sandbox("copy_leader");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.key('y');
    assert_eq!(
        s.app.pending_leader,
        Some(crate::keymap::LeaderId::Copy),
        "y でコピーリーダー待ち"
    );
    s.esc();
    assert_eq!(s.app.pending_leader, None, "Esc でリーダー解除");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_sort_reverse_flips_order() {
    // s→r reverses the order (directories still stay first, but the files' order reverses).
    let dir = sandbox("sort_rev");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    let before: Vec<_> = s.app.tab.entries.iter().map(|e| e.path.clone()).collect();
    s.key('s');
    s.key('r'); // reverse toggle (toggle-type items keep the menu open so several can be switched — as designed)
    assert!(
        s.app.is_sort_menu(),
        "r/. はトグルなのでメニューは開いたまま"
    );
    let after: Vec<_> = s.app.tab.entries.iter().map(|e| e.path.clone()).collect();
    assert_ne!(before, after, "反転で並びが変わる");
    s.esc(); // close the menu
    assert!(!s.app.is_sort_menu(), "Esc でメニューが閉じる");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_anchor_reanchor_and_reset() {
    // After descending into a subdirectory and changing root, a = re-anchor to the current location, A = return to the launch position.
    // Both flash and don't crash (switching the basis for relative-path display).
    let dir = sandbox("anchor");
    seed_files(&dir);
    let root = canon(&dir);
    let mut s = Sim::new(&root);
    s.select("src");
    s.key('l'); // descend into src (root changes)
    assert!(s.app.tab.root.ends_with("src"));
    s.key('a'); // re-anchor
    assert!(s.app.flash.is_some(), "a で通知");
    s.key('A'); // to the launch position
    assert!(s.app.flash.is_some(), "A で通知");
    // Anchoring leaves the tree structure/root unchanged (only the display basis changes).
    assert!(
        s.app.tab.root.ends_with("src"),
        "アンカーは root を動かさない"
    );
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// git: log / graph / branches / commit detail / base pin
// =============================================================================

#[cfg(feature = "git")]
#[test]
fn e2e_git_hub_to_log_shows_commit_subject() {
    // Changes hub (o) → log (l). seed_repo's subject "init" shows up in the list.
    let dir = sandbox("git_log_view");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    assert!(s.app.is_git_view(), "o で変更ハブが開く");
    s.key('l'); // in the hub, l = log
    assert!(s.app.is_git_log(), "l で log ビューへ");
    s.see("Git log"); // render_log's literal title
    s.see("init"); // the commit subject (summary)
    s.key('q'); // log's q returns to the hub
    assert!(s.app.is_git_view(), "log の q でハブへ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_graph_shows_commit_nodes() {
    // Changes hub (o) → graph (g). The angular-line renderer's node ● shows up.
    let dir = sandbox("git_graph_view");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('g'); // in the hub, g = graph
    assert!(s.app.is_git_graph(), "g でグラフへ");
    s.see("Git graph"); // render_graph's literal title
    s.see(""); // the commit/working-tree node glyph
    s.key('q');
    assert!(s.app.is_git_view(), "グラフの q でハブへ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_graph_set_and_clear_base() {
    // In the graph, s = pin the base / x = clear it. `⌖ base:` appears/disappears in the title.
    let dir = sandbox("git_graph_base");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('g');
    s.dont_see("⌖ base:"); // default config has graph_base_branches=[] → no base initially
    s.key('G'); // to the bottom row = the oldest commit line (avoids s being unusable on the working-tree row at the top)
    s.key('s'); // pin the selected commit as base
    s.see("⌖ base:"); // base appended in the title
    s.key('x'); // clear the base
    s.dont_see("⌖ base:");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_branches_view_lists_branches() {
    // Changes hub (o) → branches (b). Branch names and the current-branch marker * show up.
    let dir = sandbox("git_branches_view");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('b'); // in the hub, b = branches
    assert!(s.app.is_git_branches(), "b でブランチ一覧へ");
    s.see("Git branches"); // render_branches's literal title
    s.see("feature-x"); // the non-current branch that seed_repo creates
    s.see("*"); // the current-branch marker
    s.key('q');
    assert!(s.app.is_git_view(), "branches の q でハブへ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktrees_view_lists_and_enter_switches_root() {
    // Changes hub (o) → worktrees (w). Both the main and a linked worktree show up; `Enter` on the
    // linked one switches this tab's root **and** open_dir (so `y @`/`Y` references anchor to the
    // new location, not the one the tab launched from).
    let dir = sandbox("git_worktrees_view");
    seed_repo(&dir);
    let root = canon(&dir);
    let linked = unique_tmp("konoma_e2e_git_worktrees_view_linked");
    let _ = std::fs::remove_dir_all(&linked);
    let out = std::process::Command::new("git")
        .current_dir(&root)
        .args([
            "worktree",
            "add",
            "-q",
            "-b",
            "wt-feature",
            linked.to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert!(out.status.success(), "git worktree add: {out:?}");
    let linked = linked.canonicalize().unwrap();

    let mut s = Sim::new(&root);
    s.key('o');
    s.key('w'); // in the hub, w = worktrees
    assert!(s.app.is_git_worktrees(), "w でワークツリー一覧へ");
    // The outer mode chip must promote to GIT here, same as the hub/log/graph/branches/detail
    // views (`in_git_view()` — regression coverage for a spot where the worktree list was missing
    // from that predicate and the chip stayed on TREE).
    s.see("GIT");
    s.see("WORKTREES");
    s.see("Git worktrees");
    s.see("wt-feature");
    s.see("*"); // current-worktree marker
    assert_eq!(
        s.app.git_worktree_sel(),
        0,
        "cursor はカレント(main)から始まる"
    );

    s.key('j'); // move onto the linked worktree
    s.enter();
    assert!(!s.app.is_git_worktrees(), "Enter で一覧が閉じる");
    assert!(!s.app.is_git_view(), "ハブへも戻らず Tree に着地する");
    assert_eq!(s.app.tab.root, linked, "root が切り替わる");
    assert_eq!(s.app.tab.open_dir, linked, "open_dir も切り替わる");
    assert!(matches!(s.app.tab.mode, Mode::Tree), "切替後は Tree 表示");

    std::fs::remove_dir_all(&linked).ok();
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktrees_ctrl_t_opens_in_new_tab_leaving_current_tab_untouched() {
    let dir = sandbox("git_worktrees_new_tab");
    seed_repo(&dir);
    let root = canon(&dir);
    let linked = unique_tmp("konoma_e2e_git_worktrees_new_tab_linked");
    let _ = std::fs::remove_dir_all(&linked);
    let out = std::process::Command::new("git")
        .current_dir(&root)
        .args([
            "worktree",
            "add",
            "-q",
            "-b",
            "wt-feature",
            linked.to_str().unwrap(),
        ])
        .output()
        .unwrap();
    assert!(out.status.success(), "git worktree add: {out:?}");
    let linked = linked.canonicalize().unwrap();

    let mut s = Sim::new(&root);
    s.key('o');
    s.key('w');
    s.key('j'); // move onto the linked worktree
    s.ctrl('t');
    assert_eq!(s.app.tab_count(), 2, "新規タブが開く");
    assert_eq!(s.app.tab.root, linked, "新タブの root が対象ワークツリー");
    assert_eq!(s.app.tab.open_dir, linked);
    assert!(matches!(s.app.tab.mode, Mode::Tree));
    assert!(
        !s.app.is_git_worktrees(),
        "新タブは素の Tree(一覧を引き継がない)"
    );

    // The original tab's own worktree list is left open (this action doesn't own it).
    s.key('[');
    assert_eq!(s.app.tab.root, root, "元タブの root は不変");
    assert!(
        s.app.is_git_worktrees(),
        "元タブのワークツリー一覧は開いたまま"
    );

    std::fs::remove_dir_all(&linked).ok();
    std::fs::remove_dir_all(&dir).ok();
}

/// `o` `w` `n`: type a branch name into the input dialog, submit → `git worktree add` creates it
/// next to the main worktree (default `[git] worktree_dir = "../"`) and this tab switches into it
/// (both `root` and `open_dir`, mirroring `worktree_goto`'s `Enter`).
#[cfg(feature = "git")]
#[test]
fn e2e_git_worktree_create_switches_into_it() {
    let dir = sandbox("git_worktree_create");
    seed_repo(&dir);
    let root = canon(&dir);
    // The default worktree_dir ("../") places the new worktree as a **sibling of the sandbox
    // dir** (`worktree_create_target`: `main.join(worktree_dir).join(branch_name)`) — and
    // `sandbox()` puts `dir` directly under the shared OS temp dir, so that sibling also lands
    // directly under the shared temp dir with the *branch name* as its only unique component.
    // A literal branch name here would therefore collide with a concurrently-running instance of
    // this very test even though `dir` itself is `unique_tmp`-safe — so the branch name itself
    // must be the unique part; `expected` is then derived from the real placement formula
    // (`root`'s parent + that branch name), matching `worktree_create_target`.
    let branch_name = unique_tmp("e2e-wt-branch")
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    let expected = root.parent().unwrap().join(&branch_name);
    let _ = std::fs::remove_dir_all(&expected);

    let mut s = Sim::new(&root);
    s.key('o');
    s.key('w');
    assert!(s.app.is_git_worktrees());
    s.key('n');
    assert!(s.app.is_dialog(), "n で入力面に入る");
    s.keys(&branch_name);
    s.enter();

    assert!(!s.app.is_git_worktrees(), "作成後は一覧を閉じる");
    let new_root = s.app.tab.root.clone();
    assert_ne!(new_root, root, "root が新しいワークツリーへ切替わる");
    assert_eq!(s.app.tab.open_dir, new_root, "open_dir も切り替わる");
    assert_eq!(new_root, expected.canonicalize().unwrap());
    assert!(matches!(s.app.tab.mode, Mode::Tree), "切替後は Tree 表示");
    assert!(
        crate::git::branch_tip(&root, &branch_name).is_some(),
        "新規ブランチが作られる"
    );

    std::fs::remove_dir_all(&expected).ok();
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktrees_enter_on_current_worktree_flashes_and_returns_to_hub() {
    // The safety guard for "already here": Enter on the currently-active worktree must not touch
    // root, and must close back to the changes hub (not silently land in Tree) — a regression here
    // (guard removed) would instead re-run jump_to_dir on the same path and skip open_git_view.
    let dir = sandbox("git_worktrees_already_current");
    seed_repo(&dir);
    let root = canon(&dir);
    let mut s = Sim::new(&root);
    s.key('o');
    s.key('w');
    assert!(s.app.is_git_worktrees());
    s.enter(); // cursor starts on the current (main) worktree
    s.see("already in this worktree");
    assert!(!s.app.is_git_worktrees(), "閉じる");
    assert!(
        s.app.is_git_view(),
        "ハブへ戻る(root は動かないので Tree ではない)"
    );
    assert_eq!(s.app.tab.root, root, "root は不変");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktrees_refuses_bare_and_prunable_targets() {
    // Safety guards: a bare main worktree (no checkout) and a prunable/missing one both refuse with
    // a flash, and the list stays open (unlike a successful switch, which closes it).
    let src = sandbox("git_worktrees_refuse_src");
    seed_repo(&src);
    let src = canon(&src);
    let bare = unique_tmp("konoma_e2e_git_worktrees_refuse_bare.git");
    let wt = unique_tmp("konoma_e2e_git_worktrees_refuse_wt");
    let gone = unique_tmp("konoma_e2e_git_worktrees_refuse_gone");
    for p in [&bare, &wt, &gone] {
        let _ = std::fs::remove_dir_all(p);
    }
    let sh = |cwd: &std::path::Path, args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(cwd)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    sh(
        &std::env::temp_dir(),
        &[
            "clone",
            "-q",
            "--bare",
            src.to_str().unwrap(),
            bare.to_str().unwrap(),
        ],
    );
    sh(
        &bare,
        &["worktree", "add", "-q", wt.to_str().unwrap(), "master"],
    );
    sh(
        &bare,
        &[
            "worktree",
            "add",
            "-q",
            "-b",
            "gone-branch",
            gone.to_str().unwrap(),
        ],
    );
    let wt = wt.canonicalize().unwrap();
    std::fs::remove_dir_all(&gone).unwrap(); // now prunable (checkout gone, entry still listed)

    let mut s = Sim::new(&wt);
    s.key('o');
    s.key('w');
    assert!(s.app.is_git_worktrees());
    s.see("bare");

    // Locate rows by content rather than assuming a fixed order: git's listing order for
    // non-main entries is not creation order (confirmed empirically), only "main is always
    // first" is guaranteed.
    let view = s.app.git_worktree_view();
    let cur_idx = view.iter().position(|w| w.is_current).expect("current");
    let bare_idx = view.iter().position(|w| w.is_bare).expect("bare");
    let gone_idx = view.iter().position(|w| w.prunable).expect("prunable");
    assert_eq!(
        s.app.git_worktree_sel(),
        cur_idx,
        "cursor はカレント(wt)から始まる"
    );

    // Move onto the bare main worktree's row: refused.
    let step = |s: &mut Sim, from: usize, to: usize| {
        let delta = to as i32 - from as i32;
        for _ in 0..delta.unsigned_abs() {
            s.key(if delta > 0 { 'j' } else { 'k' });
        }
    };
    step(&mut s, cur_idx, bare_idx);
    s.enter();
    assert!(s.app.is_git_worktrees(), "bare は拒否されて一覧のまま");
    assert_eq!(s.app.tab.root, wt, "root は不変");
    s.see("bare repository");

    // Move onto the removed/prunable worktree's row: also refused.
    step(&mut s, bare_idx, gone_idx);
    s.enter();
    assert!(s.app.is_git_worktrees(), "prunable は拒否されて一覧のまま");
    assert_eq!(s.app.tab.root, wt, "root は不変");
    s.see("missing");

    std::fs::remove_dir_all(&wt).ok();
    std::fs::remove_dir_all(&bare).ok();
    std::fs::remove_dir_all(&src).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktree_show_changes_opens_detail_with_title_and_returns_to_list() {
    // o → w → d on a **linked** worktree that has both committed and uncommitted work on top of
    // the base: the detail shows both (committed+uncommitted, not just one), the title names the
    // worktree's branch and the base it's diffed against, and q returns to the worktree list
    // (not the hub, not the tree) — the list is still open underneath, never closed by this action.
    let dir = sandbox("git_worktree_show_changes");
    seed_repo(&dir);
    let root = canon(&dir);
    let base = crate::git::branch(&root).expect("sanity: has a branch after seed_repo's commit");

    let linked = unique_tmp("konoma_e2e_git_worktree_show_changes_linked");
    let _ = std::fs::remove_dir_all(&linked);
    let sh = |cwd: &std::path::Path, args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(cwd)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    sh(
        &root,
        &[
            "worktree",
            "add",
            "-q",
            "-b",
            "wt-since-feature",
            linked.to_str().unwrap(),
        ],
    );
    let linked = linked.canonicalize().unwrap();

    // ① Something committed *inside the worktree* (what an agent typically does mid-task).
    std::fs::write(linked.join("agent_landed.txt"), "AGENT_LANDED_MARKER\n").unwrap();
    sh(&linked, &["add", "-A"]);
    sh(&linked, &["commit", "-q", "-m", "agent commit"]);
    // ② Something still uncommitted on top of that.
    std::fs::write(linked.join("agent_pending.txt"), "AGENT_PENDING_MARKER\n").unwrap();

    let mut cfg = Config::default();
    cfg.ui.graph_base_branches = vec![base.clone()];
    let mut s = Sim::with_config(&root, cfg);
    s.key('o');
    s.key('w');
    assert!(s.app.is_git_worktrees());

    // Move the cursor onto the linked worktree's row (position by content, not assumed order —
    // git's non-main listing order isn't creation order, confirmed empirically elsewhere in this
    // file).
    let view = s.app.git_worktree_view();
    let cur = s.app.git_worktree_sel();
    let target = view
        .iter()
        .position(|w| w.path == linked)
        .expect("linked worktree row");
    for _ in 0..target.abs_diff(cur) {
        s.key(if target > cur { 'j' } else { 'k' });
    }

    s.key('d');
    assert!(
        s.app.is_git_detail(),
        "d でこのワークツリーの diff が detail として開く"
    );
    s.see(&format!("wt-since-feature ← {base}"));
    s.see("agent_landed.txt");
    s.see("AGENT_LANDED_MARKER");
    s.see("agent_pending.txt");
    s.see("AGENT_PENDING_MARKER");

    s.key('q');
    assert!(!s.app.is_git_detail(), "q で detail のみ閉じる");
    assert!(
        s.app.is_git_worktrees(),
        "detail の裏にあったワークツリー一覧へ戻る(ハブでもツリーでもない)"
    );
    assert!(!s.app.is_git_view());

    std::fs::remove_dir_all(&linked).ok();
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktree_show_changes_works_on_the_currently_active_worktree() {
    // Unlike `worktree_goto`'s "already here" guard, `d` does NOT refuse the worktree the tab is
    // presently inside — viewing its own diff is perfectly legitimate. The cursor starts there
    // (per the worktree list's documented behavior), so pressing `d` immediately exercises this.
    let dir = sandbox("git_worktree_show_changes_current");
    seed_repo(&dir); // leaves uncommitted changes on a.rs + an untracked new.txt
    let root = canon(&dir);
    let mut s = Sim::new(&root); // default config: graph_base_branches = [] → falls back to the
                                 // main worktree's own branch, which is exactly the one it's on
    s.key('o');
    s.key('w');
    assert_eq!(
        s.app.git_worktree_sel(),
        s.app
            .git_worktree_view()
            .iter()
            .position(|w| w.is_current)
            .unwrap(),
        "cursor はカレントから始まる"
    );
    s.key('d');
    assert!(
        s.app.is_git_detail(),
        "現在地のワークツリーでも d は拒否されない(AlreadyCurrent は worktree_goto 専用のガード)"
    );
    s.see("new.txt"); // the uncommitted untracked file from seed_repo
    s.see("_x = 1"); // the uncommitted edit to a.rs from seed_repo

    s.key('q');
    assert!(s.app.is_git_worktrees(), "一覧へ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktree_show_changes_refuses_bare_worktree() {
    // Same guard `worktree_goto` uses for a bare main worktree (no checkout to diff at all): `d`
    // refuses with a flash and leaves the list open, rather than crashing or showing nothing.
    let src = sandbox("git_worktree_show_changes_refuse_bare_src");
    seed_repo(&src);
    let src = canon(&src);
    let bare = unique_tmp("konoma_e2e_git_worktree_show_changes_refuse_bare.git");
    let wt = unique_tmp("konoma_e2e_git_worktree_show_changes_refuse_wt");
    for p in [&bare, &wt] {
        let _ = std::fs::remove_dir_all(p);
    }
    let sh = |cwd: &std::path::Path, args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(cwd)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    sh(
        &std::env::temp_dir(),
        &[
            "clone",
            "-q",
            "--bare",
            src.to_str().unwrap(),
            bare.to_str().unwrap(),
        ],
    );
    sh(
        &bare,
        &["worktree", "add", "-q", wt.to_str().unwrap(), "master"],
    );
    let wt = wt.canonicalize().unwrap();

    let mut s = Sim::new(&wt);
    s.key('o');
    s.key('w');
    let view = s.app.git_worktree_view();
    let cur = s.app.git_worktree_sel();
    let bare_idx = view.iter().position(|w| w.is_bare).expect("bare row");
    for _ in 0..bare_idx.abs_diff(cur) {
        s.key(if bare_idx > cur { 'j' } else { 'k' });
    }

    s.key('d');
    assert!(
        !s.app.is_git_detail(),
        "bare は拒否されて detail は開かない"
    );
    assert!(s.app.is_git_worktrees(), "一覧は開いたまま");
    s.see("bare repository");

    std::fs::remove_dir_all(&wt).ok();
    std::fs::remove_dir_all(&bare).ok();
    std::fs::remove_dir_all(&src).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_commit_detail_shows_full_message() {
    // log → Enter opens commit detail. The full message (body newlines preserved) shows up in the header.
    let dir = sandbox("git_commit_detail");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('l');
    s.enter(); // detail of the selected commit (init)
    assert!(s.app.is_git_detail(), "Enter で詳細へ");
    s.see("First body paragraph."); // body paragraph 1 (newlines preserved)
    s.see("Second body paragraph."); // body paragraph 2
    s.key('q'); // detail's q returns to the log behind it (the log isn't closed)
    assert!(s.app.is_git_log(), "詳細の q で log へ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_graph_branch_picker_toggles() {
    // b in the graph opens the branch-display panel, listing branch names. q cancels and closes it.
    let dir = sandbox("git_graph_picker");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('g');
    s.key('b'); // in the graph, b = the branch-display panel
    assert!(s.app.is_git_graph_picker(), "b でパネルが開く");
    s.see("feature-x"); // the branch name in a panel row
    s.key('q'); // cancel (git_graph_picker_cancel)
    assert!(!s.app.is_git_graph_picker(), "q でパネルを閉じる");
    assert!(s.app.is_git_graph(), "パネルを閉じるとグラフへ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// jj: the read-only gate (`Action::writes_repository()` + `dispatch_action`'s check in
// src/main.rs), and jj-specific key behavior (R sync, w worktrees, a graph range).
//
// konoma never writes to a jj repository (see `crate::vcs::jj`'s module docs): every write-shaped
// `Action` is refused before it runs whenever `crate::vcs::caps(root).write` is false, which is
// jj's answer unconditionally. This section is what pins that down — before the 2026-08 audit that
// added it, the gate had no test coverage at all.
// =============================================================================

/// A throwaway jj workspace with no colocated `.git` — the jj analog of `seed_repo`, with one
/// bookmark (so the branch list has something to open) and one uncommitted change (so the changed
/// list, the diff view, and the working-copy status are non-empty). None when this machine has no
/// jj, mirroring `app/tests.rs`'s `jj_scratch` — the suite must stay green without it.
#[cfg(feature = "git")]
fn jj_seed_repo(name: &str) -> Option<std::path::PathBuf> {
    if !crate::vcs::jj::available() {
        return None;
    }
    let dir = sandbox(name);
    let jj = |args: &[&str]| -> bool {
        std::process::Command::new("jj")
            .current_dir(&dir)
            .env("HOME", &dir) // never touch the running machine's own jj config
            .env("JJ_USER", "konoma test")
            .env("JJ_EMAIL", "test@example.invalid")
            .args(args)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    };
    if !jj(&["git", "init", "--no-colocate", "."]) {
        return None;
    }
    std::fs::write(dir.join("a.txt"), b"one\n").ok()?;
    if !jj(&["commit", "-m", "seed"]) {
        return None;
    }
    // A bookmark, so `open_git_branches` finds something to show — without one, the branch list
    // never opens at all (`NoBranches`), and `BranchCheckout`/`BranchCreate`/`BranchDelete` could
    // never be reached to prove their gate holds.
    if !jj(&["bookmark", "create", "main", "-r", "@-"]) {
        return None;
    }
    std::fs::write(dir.join("a.txt"), b"two\n").ok()?;
    Some(dir)
}

/// A **colocated** repository — both `.git` and `.jj` — with `main`/`master` already a bookmark
/// (jj auto-imports it from the colocated git branch), and one real uncommitted change. Built by
/// following jj's own documented on-ramp for an existing git repository (`jj git init --colocate`
/// inside it); verified working in a scratch directory before use here. None when this machine has
/// no jj.
#[cfg(feature = "git")]
fn jj_colocated_seed_repo(name: &str) -> Option<std::path::PathBuf> {
    if !crate::vcs::jj::available() {
        return None;
    }
    let dir = sandbox(name);
    let git = |args: &[&str]| -> bool {
        std::process::Command::new("git")
            .current_dir(&dir)
            .args(args)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    };
    let jj = |args: &[&str]| -> bool {
        std::process::Command::new("jj")
            .current_dir(&dir)
            .env("HOME", &dir)
            .env("JJ_USER", "konoma test")
            .env("JJ_EMAIL", "test@example.invalid")
            .args(args)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    };
    if !git(&["init", "-q", "."])
        || !git(&["config", "user.email", "t@t"])
        || !git(&["config", "user.name", "t"])
    {
        return None;
    }
    std::fs::write(dir.join("a.txt"), b"one\n").ok()?;
    if !git(&["add", "-A"]) || !git(&["commit", "-q", "-m", "init"]) {
        return None;
    }
    // The documented way to add jj on top of an existing git repository.
    if !jj(&["git", "init", "--colocate", "."]) {
        return None;
    }
    // A real uncommitted change `git status --porcelain` can show a diff against (measured in a
    // scratch directory: `.jj` itself never shows up in `git status`, colocated or not, so this is
    // the only source of "before" state the test needs).
    std::fs::write(dir.join("a.txt"), b"two\n").ok()?;
    Some(dir)
}

/// Walks every key that reaches a write-shaped `Action` (`Action::writes_repository()` in
/// `src/keymap.rs`) in a jj repository, and confirms each one is refused — the `VcsReadOnly` flash,
/// no dialog opened, no navigation away from wherever the key was pressed. `writes_repository()`
/// has no enumerable list of variants to iterate (`Action` derives no `EnumIter`-style trait, unlike
/// `i18n::Msg`'s `ALL_MSGS`), so the (surface, key) pairs below are transcribed by hand from
/// `src/keymap.rs`'s per-surface maps rather than generated — see this test's sibling
/// `writes_repository_set_is_pinned...` (`app/tests.rs`) for the trip-wire that catches this list
/// (and `writes_repository()` itself) drifting apart.
///
/// `WorktreeCreate` is deliberately not exercised here: `w` never reaches `Surface::GitWorktrees` in
/// a jj repository at all (`open_git_worktrees` redirects to the `JjWorkspacesUnlisted` flash
/// first — see `e2e_jj_worktrees_key_flashes_instead_of_opening_the_git_list` below), so the key
/// that would fire it cannot be pressed through real navigation. It is covered separately in
/// `app/tests.rs`, by forcing the surface open directly.
#[cfg(feature = "git")]
#[test]
fn e2e_jj_write_actions_are_gated_by_the_read_only_backend() {
    let Some(dir) = jj_seed_repo("jj_write_gate") else {
        return;
    };
    let root = canon(&dir);
    let mut s = Sim::new(&root);
    assert!(
        !crate::vcs::caps(&s.app.tab.root).write,
        "sanity: this fixture must resolve to the read-only jj backend, or the test proves nothing"
    );

    fn refused(s: &Sim, key: char) {
        assert!(
            s.app.flash.as_deref().is_some_and(
                |f| f.contains(crate::i18n::tr(s.app.lang, crate::i18n::Msg::VcsReadOnly))
            ),
            "key {key:?}: expected the VcsReadOnly flash, got {:?}",
            s.app.flash
        );
        assert!(
            !s.app.is_dialog(),
            "key {key:?}: a refused write must not open a dialog"
        );
    }

    // --- The changes hub: stage / unstage / stage_all / unstage_all / discard / commit ---
    s.key('o');
    assert!(s.app.is_git_view(), "o must still open the (read-only) hub");
    for key in ['s', 'u', 'S', 'U', 'x', 'c'] {
        s.key(key);
        refused(&s, key);
        assert!(
            s.app.is_git_view(),
            "key {key:?}: a refused write must not leave the hub"
        );
    }

    // --- The branch list: checkout / create / delete ---
    s.key('b');
    assert!(
        s.app.is_git_branches(),
        "the bookmark fixture must open the branch list"
    );
    for key in ['l', 'n', 'd'] {
        s.key(key);
        refused(&s, key);
        assert!(
            s.app.is_git_branches(),
            "key {key:?}: a refused write must not leave the branch list"
        );
    }
    s.key('q'); // branches -> hub
    s.key('q'); // hub -> tree
    assert_eq!(
        s.app.tab.mode,
        Mode::Tree,
        "both q's must have returned to the tree"
    );

    // --- The full-screen diff: discard-from-diff ---
    s.select("a.txt");
    s.key('d');
    assert!(
        s.app.is_git_diff_preview(),
        "d must still open the (read-only) diff"
    );
    s.key('x');
    refused(&s, 'x');
    assert!(
        s.app.is_git_diff_preview(),
        "a refused GitDiffDiscard must not leave the diff view"
    );

    std::fs::remove_dir_all(&dir).ok();
}

/// Pins `[external] vcs = "jj"` for **this thread only**.
///
/// The real preference is process-wide, because the background status scan has to detect the same
/// backend the UI thread does. That makes it useless to a test: every `App::new` in this binary
/// writes it, so a parallel test starting up flips it back mid-run -- observed directly, as a
/// colocated fixture reading back `caps().write == true` right after being pinned to jj. Writing
/// the global from here would leak the pin into those tests in return. `set_preference_for_test`
/// is the thread-local override that avoids both directions; reset it with `unpin_vcs`.
#[cfg(feature = "git")]
fn pin_vcs_to_jj() {
    crate::vcs::set_preference_for_test(Some(crate::vcs::Preference::Jj));
}

/// Hands the thread back to the process-wide preference. Paired with [`pin_vcs_to_jj`].
#[cfg(feature = "git")]
fn unpin_vcs() {
    crate::vcs::set_preference_for_test(None);
}

/// The worst case the jj write-safety audit (2026-08) flagged: a **colocated** repository (both
/// `.git` and `.jj`) with `[external] vcs = "jj"` set explicitly, so a real, writable `.git` sits
/// right there while konoma is pinned to read jj instead. If the gate in `dispatch_action` ever
/// regressed to asking the wrong backend, konoma would run `git add`/`git commit`/`git switch`
/// against a repository the user explicitly chose to only read through jj. Checking the flash alone
/// would not catch a gate that *looked* closed but let the write through anyway, so this reads
/// `git status --porcelain` before and after every key and requires it to be byte-for-byte
/// identical.
///
/// The preference is pinned for this thread only; see [`pin_vcs_to_jj`].
#[cfg(feature = "git")]
#[test]
fn e2e_jj_write_gate_holds_in_a_colocated_repository_pinned_to_jj() {
    let Some(dir) = jj_colocated_seed_repo("jj_colocated_gate") else {
        return;
    };
    let root = canon(&dir);
    let git_status = || -> String {
        let out = std::process::Command::new("git")
            .current_dir(&root)
            .args(["status", "--porcelain"])
            .output()
            .unwrap();
        assert!(out.status.success(), "git status --porcelain: {out:?}");
        String::from_utf8(out.stdout).unwrap()
    };
    let before = git_status();
    assert!(
        !before.is_empty(),
        "the fixture must leave real uncommitted git state, or a broken gate has nothing to prove itself against"
    );

    let mut cfg = Config::default();
    cfg.external.vcs = "jj".into();
    pin_vcs_to_jj();
    let mut s = Sim::with_config(&root, cfg);
    assert!(
        !crate::vcs::caps(&s.app.tab.root).write,
        "sanity: [external] vcs = \"jj\" must resolve this colocated repo to the read-only jj backend"
    );

    s.key('o');
    assert!(s.app.is_git_view(), "o must still open the (read-only) hub");
    for key in ['s', 'u', 'S', 'U', 'x', 'c'] {
        s.key(key);
        assert!(
            s.app.flash.as_deref().is_some_and(
                |f| f.contains(crate::i18n::tr(s.app.lang, crate::i18n::Msg::VcsReadOnly))
            ),
            "key {key:?}: expected the VcsReadOnly flash, got {:?}",
            s.app.flash
        );
        assert!(
            !s.app.is_dialog(),
            "key {key:?}: a refused write must not open a dialog"
        );
    }

    s.key('b');
    assert!(
        s.app.is_git_branches(),
        "the colocated repo's auto-imported bookmark must open the branch list"
    );
    for key in ['l', 'n', 'd'] {
        s.key(key);
        assert!(
            s.app.flash.as_deref().is_some_and(
                |f| f.contains(crate::i18n::tr(s.app.lang, crate::i18n::Msg::VcsReadOnly))
            ),
            "key {key:?}: expected the VcsReadOnly flash, got {:?}",
            s.app.flash
        );
        assert!(
            !s.app.is_dialog(),
            "key {key:?}: a refused write must not open a dialog"
        );
    }

    unpin_vcs();
    let after = git_status();
    assert_eq!(
        before, after,
        "the read-only gate must leave the real .git untouched, byte for byte"
    );

    std::fs::remove_dir_all(&dir).ok();
}

/// `R` (`jj_start_sync`) on a repository that is not jj — this needs no jj binary at all: the check
/// that refuses it runs before jj is ever invoked.
#[cfg(feature = "git")]
#[test]
fn e2e_jj_sync_key_on_a_git_repo_flashes_and_opens_nothing() {
    let dir = sandbox("jj_sync_not_jj");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('R');
    assert!(
        s.app.flash.as_deref().is_some_and(|f| f.contains(
            crate::i18n::tr(s.app.lang, crate::i18n::Msg::JjSyncNotJj)
        )),
        "expected the JjSyncNotJj flash, got {:?}",
        s.app.flash
    );
    assert!(
        !s.app.is_dialog(),
        "R on a non-jj repository must not open the sync confirmation"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// The full `R` round trip on an actual jj repository: ask (`confirm_jj_sync` defaults on) → `y`
/// confirms → `jj_run_sync` actually runs → the flash reports what happened. This is the one write
/// konoma makes to a jj repository, by design (`crate::vcs::jj`'s module docs) — `dialog_confirm`'s
/// `y`/Enter key is a **fixed key**, resolved before `dispatch_action`'s `Action`-based gate ever
/// sees it (`Surface::DialogConfirmDelete` in `src/main.rs`'s `handle_key`), so nothing in this path
/// is `writes_repository()`-gated; it relies entirely on `jj_start_sync`'s own
/// `detect() != VcsKind::Jj` check having already been true when the dialog was opened.
#[cfg(feature = "git")]
#[test]
fn e2e_jj_sync_full_round_trip_confirms_and_reports_completion() {
    let Some(dir) = jj_seed_repo("jj_sync_round_trip") else {
        return;
    };
    let mut s = Sim::new(&canon(&dir)); // confirm_jj_sync defaults on
    s.key('o');
    s.key('R');
    assert!(s.app.is_dialog(), "R must ask before letting jj snapshot");
    s.key('y');
    assert!(!s.app.is_dialog(), "confirming must close the dialog");
    assert!(
        s.app.flash.as_deref().is_some_and(|f| {
            f.contains(crate::i18n::tr(s.app.lang, crate::i18n::Msg::JjSyncDone))
                || f.contains(crate::i18n::tr(s.app.lang, crate::i18n::Msg::JjSyncFailed))
        }),
        "the confirmed sync must report completion or failure, got {:?}",
        s.app.flash
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `w` (worktree list) in a jj repository: konoma does not list jj workspaces yet, and a colocated
/// repository's git worktree list would describe a checkout the user is not working in — so it
/// flashes an explanation instead of opening the (git-only) list.
#[cfg(feature = "git")]
#[test]
fn e2e_jj_worktrees_key_flashes_instead_of_opening_the_git_list() {
    let Some(dir) = jj_seed_repo("jj_worktrees_unlisted") else {
        return;
    };
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    assert!(s.app.is_git_view());
    s.key('w');
    assert!(
        s.app
            .flash
            .as_deref()
            .is_some_and(|f| f.contains(crate::i18n::tr(
                s.app.lang,
                crate::i18n::Msg::JjWorkspacesUnlisted
            ))),
        "expected the JjWorkspacesUnlisted flash, got {:?}",
        s.app.flash
    );
    assert!(
        !s.app.is_git_worktrees(),
        "the git worktree list must not open for a jj repository"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `a` (`GitGraphToggleAll`) means different things per backend: git's graph is already every
/// reachable commit, so it just says so; jj's graph defaults to a narrower range (it rewrites
/// commits as you work), so `a` really does widen/narrow it.
#[cfg(feature = "git")]
#[test]
fn e2e_git_graph_toggle_all_differs_between_git_and_jj() {
    let git_dir = sandbox("graph_toggle_git");
    seed_repo(&git_dir);
    let mut sg = Sim::new(&canon(&git_dir));
    sg.key('o');
    sg.key('g');
    assert!(sg.app.is_git_graph(), "g must open the graph");
    sg.key('a');
    assert!(
        sg.app
            .flash
            .as_deref()
            .is_some_and(|f| f.contains(crate::i18n::tr(
                sg.app.lang,
                crate::i18n::Msg::GraphAlreadyAll
            ))),
        "git's graph must say it already shows everything rather than toggle a range, got {:?}",
        sg.app.flash
    );
    std::fs::remove_dir_all(&git_dir).ok();

    let Some(jj_dir) = jj_seed_repo("graph_toggle_jj") else {
        return;
    };
    let mut sj = Sim::new(&canon(&jj_dir));
    sj.key('o');
    sj.key('g');
    assert!(sj.app.is_git_graph(), "g must open the graph");
    sj.key('a');
    assert!(
        sj.app
            .flash
            .as_deref()
            .is_some_and(|f| f.contains(crate::i18n::tr(
                sj.app.lang,
                crate::i18n::Msg::GraphShowingAll
            ))),
        "jj's graph must widen to every revision, got {:?}",
        sj.app.flash
    );
    sj.key('a');
    assert!(
        sj.app
            .flash
            .as_deref()
            .is_some_and(|f| f.contains(crate::i18n::tr(
                sj.app.lang,
                crate::i18n::Msg::GraphShowingDefault
            ))),
        "pressing a again must narrow back to the default range, got {:?}",
        sj.app.flash
    );
    std::fs::remove_dir_all(&jj_dir).ok();
}

// =============================================================================
// jj cross-cutting: konoma's git-agnostic features (tab switching / follow / changed filter / the
// change gutter) driven by a **jj** backend, not just git. Before the 2026-08 audit that added this
// section, none of these had ever been exercised through a real jj repository — `App::git_vcs` (the
// single App-level "which backend answers" slot, re-detected on every root change) had no coverage
// of a real tab-switch round trip between a git repo and a jj workspace, and follow/`C`/`n`/`N`/the
// gutter had only ever been driven by `seed_repo` (git).
// =============================================================================

/// Writes the same seed recipe as `jj_seed_repo`, but **into an existing directory** rather than
/// allocating its own sandbox — so a jj workspace can sit as a sibling of a git repository and real
/// tree navigation (`h`/`l`) can move between the two, which
/// `e2e_tab_switch_between_git_and_jj_settles_the_backend_each_time` needs. Returns `false` (and
/// creates nothing further) when this machine has no jj.
#[cfg(feature = "git")]
fn jj_init_in(dir: &std::path::Path) -> bool {
    if !crate::vcs::jj::available() {
        return false;
    }
    let jj = |args: &[&str]| -> bool {
        std::process::Command::new("jj")
            .current_dir(dir)
            .env("HOME", dir) // never touch the running machine's own jj config
            .env("JJ_USER", "konoma test")
            .env("JJ_EMAIL", "test@example.invalid")
            .args(args)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    };
    if !jj(&["git", "init", "--no-colocate", "."]) {
        return false;
    }
    if std::fs::write(dir.join("j.txt"), b"one\n").is_err() {
        return false;
    }
    if !jj(&["commit", "-m", "seed"]) {
        return false;
    }
    std::fs::write(dir.join("j.txt"), b"two\n").is_ok()
}

/// `App::git_vcs` lives directly on `App`, not per-tab — it is re-detected every time the active
/// root changes (`kick_status_refresh`). This walks the **real key-driven path** that has to keep it
/// honest across a tab switch: two sibling repositories (one git, one jj) under a common parent,
/// reached by real tree descend/ascend (`l`/`h`) and real tab operations (`t`/`[`/`]`) — not by
/// assigning `tab.root` directly the way
/// `moving_to_another_repository_settles_the_backend_before_the_scan_lands` (`app/tests.rs`) does.
/// A regression where `git_status_dirty = true` stopped being set on tab switch (see
/// `load_active`'s doc comment) would leave a stale backend answering for the newly active tab —
/// this is what would catch it.
#[cfg(feature = "git")]
#[test]
fn e2e_tab_switch_between_git_and_jj_settles_the_backend_each_time() {
    let parent = sandbox("tab_git_jj_switch");
    let git_dir = parent.join("repo_git");
    let jj_dir = parent.join("repo_jj");
    std::fs::create_dir_all(&git_dir).unwrap();
    std::fs::create_dir_all(&jj_dir).unwrap();
    seed_repo(&git_dir);
    if !jj_init_in(&jj_dir) {
        std::fs::remove_dir_all(&parent).ok();
        return; // no jj on this machine
    }

    let mut s = Sim::new(&canon(&parent));

    // Tab 1: descend (`l`) into the git repository.
    s.select("repo_git");
    s.key('l');
    assert!(
        s.app.tab.root.ends_with("repo_git"),
        "タブ1は git リポジトリへ降りる"
    );
    assert_eq!(
        s.app.git_vcs,
        crate::vcs::VcsKind::Git,
        "git リポジトリへ降りたら git_vcs=Git"
    );

    // Open a second tab (starts at the same root = repo_git), then move *that* tab into the jj
    // workspace via real navigation (`h` up to the parent, select, `l` down).
    s.key('t');
    assert_eq!(s.app.tab_count(), 2, "タブが2つ");
    assert_eq!(s.app.active_tab_index(), 1, "新タブがアクティブ");
    s.key('h');
    s.select("repo_jj");
    s.key('l');
    assert!(
        s.app.tab.root.ends_with("repo_jj"),
        "タブ2は jj ワークスペースへ降りる"
    );
    assert_eq!(
        s.app.git_vcs,
        crate::vcs::VcsKind::Jj,
        "jj ワークスペースへ降りたら git_vcs=Jj"
    );

    // Switch back and forth through the real tab-switch keys (`[`=tab_prev / `]`=tab_next), and
    // confirm the backend settles correctly *every* time, not just on the first crossing.
    s.key('['); // -> tab 1 (git)
    assert_eq!(s.app.active_tab_index(), 0);
    assert!(s.app.tab.root.ends_with("repo_git"));
    assert_eq!(
        s.app.git_vcs,
        crate::vcs::VcsKind::Git,
        "タブ1に戻ったら git_vcs は Git に戻る"
    );

    s.key(']'); // -> tab 2 (jj)
    assert_eq!(s.app.active_tab_index(), 1);
    assert!(s.app.tab.root.ends_with("repo_jj"));
    assert_eq!(
        s.app.git_vcs,
        crate::vcs::VcsKind::Jj,
        "タブ2に戻ったら git_vcs は Jj に戻る"
    );

    s.key('['); // -> tab 1 (git) again: a second round trip, not just the first crossing
    assert_eq!(
        s.app.git_vcs,
        crate::vcs::VcsKind::Git,
        "2周目のタブ切替でも git_vcs は正しく戻る"
    );

    std::fs::remove_dir_all(&parent).ok();
}

/// The jj-backed sibling of `e2e_follow_opens_full_screen_diff`: same scenario, but the repository
/// behind it is a jj workspace. `follow_jump`/`compute_gitdiff_lines` read through
/// `crate::vcs::file_diff`/`follow_baseline_diff` (backend-agnostic once the file is already dirty at
/// follow-start, which it is here), so this pins that follow was never only proven against git.
#[cfg(feature = "git")]
#[test]
fn e2e_jj_follow_opens_full_screen_diff() {
    let Some(dir) = jj_seed_repo("jj_follow_diff") else {
        return;
    };
    let mut s = Sim::new(&canon(&dir)); // a.txt is already dirty ("one" -> "two") from the fixture
    s.key('F');
    assert!(s.app.follow_enabled(), "F でフォロー ON");

    let a = s.app.tab.root.join("a.txt");
    // Equivalent to a real-world AI edit landing after F: a second, external write.
    std::fs::write(&a, "three\n").unwrap();
    assert!(s.app.follow_note_change(&a), "変更ファイルは有効な追尾対象");
    s.app.follow_jump(&a);
    s.draw();

    assert!(
        s.app.is_git_diff_preview(),
        "jj バックエンドでも追尾先は全画面 diff で開く"
    );
    assert_eq!(s.app.tab.mode, Mode::Preview);
    s.see("diff");
    assert_eq!(
        s.app.diff_change_position(),
        Some((1, 1)),
        "セッションに 1 ファイル → (1/1)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// A jj workspace like `jj_seed_repo`, but with **three tracked files** so a test can leave one
/// untouched (a control that must not show as changed) while editing the other two after the seed
/// commit — enough for `C`'s changed-only filter to genuinely narrow, and for `n`/`N` to have more
/// than one changed file to cycle between.
///
/// mtime note (flagged by the 2026-08 audit that added this section): unlike `jj_seed_repo`, this
/// sleeps for over a second **before** the seed commit. `newer_than`'s check (`mtime_secs >=
/// snapshot_epoch`, both second-granularity) is inclusive, so a file created in the very same
/// wall-clock second as the commit could alias into looking "changed" too, purely by timing luck —
/// without the sleep, `c.txt` (meant to stay a clean control) could not be trusted to read as
/// unchanged deterministically. `crate::vcs::jj`'s own `scratch_repo` test fixture uses the same
/// sleep for the same reason. The two post-commit edits below need no equivalent sleep: they always
/// land strictly after `jj commit` returns, so `newer_than` is unambiguously true for them regardless
/// of same-second timing.
#[cfg(feature = "git")]
fn jj_seed_repo_multi(name: &str) -> Option<std::path::PathBuf> {
    if !crate::vcs::jj::available() {
        return None;
    }
    let dir = sandbox(name);
    let jj = |args: &[&str]| -> bool {
        std::process::Command::new("jj")
            .current_dir(&dir)
            .env("HOME", &dir)
            .env("JJ_USER", "konoma test")
            .env("JJ_EMAIL", "test@example.invalid")
            .args(args)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false)
    };
    if !jj(&["git", "init", "--no-colocate", "."]) {
        return None;
    }
    std::fs::write(dir.join("a.txt"), b"one\n").ok()?;
    std::fs::write(dir.join("b.txt"), b"one\n").ok()?;
    std::fs::write(dir.join("c.txt"), b"one\n").ok()?; // stays untouched: the "unchanged" control
    std::thread::sleep(std::time::Duration::from_millis(1100));
    if !jj(&["commit", "-m", "seed"]) {
        return None;
    }
    std::fs::write(dir.join("a.txt"), b"two\n").ok()?;
    std::fs::write(dir.join("b.txt"), b"two\n").ok()?;
    Some(dir)
}

/// The jj-backed sibling of `e2e_changed_filter_and_jumps` + `e2e_git_changed_filter_n_jumps_between_changed_files`,
/// combined: `C`/`n`/`N` all read through `crate::vcs::statuses`/`changed_paths`, which are
/// backend-agnostic — pin that a jj workspace drives the changed-only filter and cross-file jumps the
/// same way git does, including that `c.txt` (untouched since the seed commit) is correctly excluded.
#[cfg(feature = "git")]
#[test]
fn e2e_jj_changed_filter_narrows_and_n_jumps_between_changed_files() {
    let Some(dir) = jj_seed_repo_multi("jj_changed_filter") else {
        return;
    };
    let mut s = Sim::new(&canon(&dir));
    let all = s.app.tab.entries.len();
    assert_eq!(all, 3, "前提: a.txt/b.txt/c.txt の3枚(fixture の検証)");

    s.key('C');
    assert!(s.app.changed_filter());
    assert_eq!(
        s.app.tab.entries.len(),
        2,
        "C で a.txt/b.txt だけに絞られる(未変更の c.txt は除外される)"
    );
    s.see("a.txt");
    s.see("b.txt");
    s.dont_see("c.txt");

    s.select("a.txt");
    assert!(s.app.tab.entries[s.app.tab.selected]
        .path
        .ends_with("a.txt"));
    s.key('n');
    assert!(
        s.app.tab.entries[s.app.tab.selected]
            .path
            .ends_with("b.txt"),
        "n で次の変更ファイル(b.txt)へ"
    );
    s.key('n');
    assert!(
        s.app.tab.entries[s.app.tab.selected]
            .path
            .ends_with("a.txt"),
        "n はラップして a.txt へ戻る"
    );
    s.key('N');
    assert!(
        s.app.tab.entries[s.app.tab.selected]
            .path
            .ends_with("b.txt"),
        "N は逆方向に b.txt へ"
    );

    s.key('C');
    assert!(!s.app.changed_filter(), "C 再押下で解除");
    assert_eq!(s.app.tab.entries.len(), all, "解除で全件(3枚)に戻る");

    std::fs::remove_dir_all(&dir).ok();
}

/// The jj-backed sibling of `e2e_ui_git_gutter_toggle_shows_or_hides_change_marker`: same scenario,
/// jj-backed repository. `git_gutter_marks` reads through `crate::vcs::file_diff`
/// (backend-agnostic), so this pins that the left-edge change gutter (▌) was never only proven
/// against git.
#[cfg(feature = "git")]
#[test]
fn e2e_ui_git_gutter_toggle_shows_or_hides_change_marker_jj() {
    let Some(dir) = jj_seed_repo("ui_git_gutter_jj") else {
        return;
    };
    let dir = canon(&dir);

    let mut cfg_on = Config::default();
    cfg_on.ui.syntax_highlight = false; // determinism: don't depend on cold/warm grammar state
    let mut s_on = Sim::with_config(&dir, cfg_on);
    s_on.select("a.txt");
    s_on.enter();
    s_on.see(""); // default git_gutter=true: jj's diff also feeds the same gutter marker

    let mut cfg_off = Config::default();
    cfg_off.ui.syntax_highlight = false;
    cfg_off.ui.git_gutter = false;
    let mut s_off = Sim::with_config(&dir, cfg_off);
    s_off.select("a.txt");
    s_off.enter();
    s_off.dont_see("");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Verifying "actual values" for copy (clipboard-independent — verified via computation functions/getters)
// =============================================================================

#[test]
fn e2e_copy_path_strings_from_tree() {
    // Verify, without relying on the clipboard, that y→'s various copy kinds (n/r/f/p/@) produce the
    // expected strings relative to "where it was opened" (open_dir), via copy_string_for (= copy_text).
    // - @ (AtRef) is **strictly relative** to open_dir (doesn't prefix the launch dir name) = Claude Code's @ syntax.
    // - r (Relative) prefixes the launch dir name (the same basis as the title display = format_path).
    let dir = sandbox("copy_strings");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    let path = s.app.tab.entries[s.app.tab.selected].path.clone();

    assert_eq!(
        s.app.copy_string_for(crate::app::CopyKind::Name).as_deref(),
        Some("notes.txt")
    );
    assert_eq!(
        s.app.copy_string_for(crate::app::CopyKind::Full),
        Some(path.display().to_string())
    );
    assert_eq!(
        s.app.copy_string_for(crate::app::CopyKind::Parent),
        Some(path.parent().unwrap().display().to_string())
    );
    // @ reference is strictly relative, without prefixing the launch dir name.
    assert_eq!(
        s.app
            .copy_string_for(crate::app::CopyKind::AtRef)
            .as_deref(),
        Some("@notes.txt")
    );
    // Relative is the same as the title display (format_path), prefixing the launch dir name (distinct from @).
    let rel = s
        .app
        .copy_string_for(crate::app::CopyKind::Relative)
        .unwrap();
    assert_eq!(
        rel,
        s.app.format_path(&path),
        "相対コピー=タイトル表示と同一基準"
    );
    assert!(
        rel.ends_with("/notes.txt") && rel != "notes.txt" && rel != "@notes.txt",
        "相対は起動dir名を先頭に付ける(@ の厳密相対とは異なる): {rel}"
    );

    // y→@ key wiring: the copy leader opens and is consumed by @ (pending_leader returns to None).
    s.key('y');
    assert_eq!(s.app.pending_leader, Some(crate::keymap::LeaderId::Copy));
    s.key('@');
    assert_eq!(s.app.pending_leader, None, "y→@ でリーダーが消費される");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_copy_atref_is_open_dir_relative_when_descended() {
    // The @ reference is relative to the "launch (open) directory". Even after descending into a
    // subdirectory changes root, open_dir stays at the launch position, so it's strictly relative from there, e.g. @src/lib.rs.
    let dir = sandbox("atref_descend");
    seed_files(&dir);
    let root = canon(&dir);
    let mut s = Sim::new(&root);
    s.select("src");
    s.key('l'); // descend into src (root=src; open_dir stays at the launch position)
    assert!(s.app.tab.root.ends_with("src"));
    s.select("lib.rs");
    assert_eq!(
        s.app
            .copy_string_for(crate::app::CopyKind::AtRef)
            .as_deref(),
        Some("@src/lib.rs"),
        "@ 参照は起動位置からの厳密相対(現在の root ではない)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_preview_at_ref_line_reference() {
    // Preview's Y = @path#L reference. Caret line = single #L, v/V selection = range #L..-... The value
    // is verified via the clipboard-independent getter (selection_ref_string=preview_selection_ref_text).
    let dir = sandbox("preview_atref");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.enter();
    assert!(s.app.is_windowed(), "テキストは windowed(2D キャレット)");
    // The caret is on the first line → #L1.
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@notes.txt#L1")
    );
    s.key('j'); // to line 2 (windowed = line-caret movement)
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@notes.txt#L2")
    );
    // A charwise selection spanning lines 2-3 → a range reference.
    s.key('v');
    s.key('j'); // move the caret to line 3 (the anchor is line 2)
    assert!(matches!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char { .. }
    ));
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@notes.txt#L2-3")
    );
    // Y copies the reference and exits the selection (the value goes through the clipboard = environment-dependent, so only confirm that it exited).
    s.key('Y');
    assert!(!s.app.is_preview_visual(), "Y で参照コピー後に選択解除");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_commit_copy_values() {
    // In git log, y→'s commit-info copy (short/full hash, subject, full text, author, date).
    // git_copy(kind) just copies current_commit_meta into each field. In GitLog,
    // current_commit_meta = commit_meta(root, git_log_selected_id()), so verify by reproducing the
    // same meta via a pub path (no getter needed).
    let dir = sandbox("git_commit_copy");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o'); // changes hub
    s.key('l'); // → log (synchronous load)
    s.see("init");
    assert_eq!(s.app.surface(), crate::keymap::Surface::GitLog);

    let id = s.app.git_log_selected_id().expect("選択コミットの id");
    let meta = crate::git::commit_meta(&s.app.tab.root, &id).expect("commit_meta");
    assert_eq!(meta.message.lines().next(), Some("init"), "Subject=件名");
    assert_eq!(meta.author, "t", "Author=user.name");
    assert_eq!(meta.short.len(), 7, "ShortHash=7桁");
    assert_eq!(meta.id.len(), 40, "FullHash=40桁");
    assert!(
        meta.id.starts_with(&meta.short),
        "短ハッシュは完全ハッシュの接頭辞"
    );
    assert!(!meta.date.is_empty(), "Date は非空");

    // y→ wiring: the GitCopy leader opens and is consumed by t (subject).
    s.key('y');
    assert_eq!(
        s.app.pending_leader,
        Some(crate::keymap::LeaderId::GitCopy),
        "log の y は GitCopy リーダー(パスコピーの Copy ではない)"
    );
    s.key('t');
    assert_eq!(s.app.pending_leader, None, "y→t でリーダー消費");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Follow mode's diff display and n/N cycling
// [Note] Since Sim doesn't run the run loop/notify, what the run loop does on an FS event —
//   follow_note_change(&p) → follow_jump(&p) — is replaced with a **direct pub call**.
//   The pipeline FS watch → pending_follow → dwell (1s) → jump (debounce/latest-wins) is
//   not verified here — this verifies follow's "logic" (wiring/dwell is a separate layer).
// =============================================================================

#[cfg(feature = "git")]
#[test]
fn e2e_follow_opens_full_screen_diff() {
    // With follow ON, an external change opens its diff as a full-screen git diff (ui.follow_view default "diff").
    let dir = sandbox("follow_diff");
    seed_repo(&dir); // a.rs = modified, new.txt = untracked (both have a file_diff)
    let mut s = Sim::new(&canon(&dir));
    s.key('F');
    assert!(s.app.follow_enabled(), "F でフォロー ON");

    let a = s.app.tab.root.join("a.rs");
    // The baseline diff shows changes "since F" → edit a.rs after F (real-world: an AI's edit).
    std::fs::write(&a, "fn a() { let _x = 2; }\n").unwrap();
    // Equivalent to the run loop: record the change event, then jump (both are pub).
    assert!(s.app.follow_note_change(&a), "変更ファイルは有効な追尾対象");
    s.app.follow_jump(&a);
    s.draw();

    assert!(
        s.app.is_git_diff_preview(),
        "追尾先は全画面 git diff で開く"
    );
    assert_eq!(s.app.tab.mode, Mode::Preview);
    s.see("diff");
    // The follow-originated diff's cycling target = "files changed during this session". Currently 1.
    assert_eq!(
        s.app.diff_change_position(),
        Some((1, 1)),
        "セッションに 1 ファイル → (1/1)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_follow_diff_cycles_session_files() {
    // n/N inside a follow-originated diff wrap-cycle through only "files changed during the follow
    // session", and the title's position indicator (diff_change_position) changes.
    let dir = sandbox("follow_cycle");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('F');

    let a = s.app.tab.root.join("a.rs");
    let nt = s.app.tab.root.join("new.txt");
    // The baseline diff shows changes "since F" → edit both files after F.
    std::fs::write(&a, "fn a() { let _x = 2; }\n").unwrap();
    std::fs::write(&nt, "untracked\nAFTER\n").unwrap();
    // 2 change events accumulate in the session (record order = cycle order).
    assert!(s.app.follow_note_change(&a));
    assert!(s.app.follow_note_change(&nt));
    s.app.follow_jump(&a); // open a.rs's diff first
    s.draw();
    assert!(s.app.is_git_diff_preview());
    assert_eq!(
        s.app.diff_change_position(),
        Some((1, 2)),
        "a.rs = 2 件中 1 番目"
    );

    // n = to the next changed file's diff (session-scoped only). Maximally sticky (2026-07-23) = n
    // doesn't resolve to PreviewBack, so it cycles while follow stays on (diff_follow_scope + follow_session).
    s.key('n');
    assert!(
        s.app.follow_enabled(),
        "n(変更ファイル回遊)でもフォロー維持(最大粘着)"
    );
    assert_eq!(
        s.app.diff_change_position(),
        Some((2, 2)),
        "n で 2 番目(new.txt)へ"
    );
    s.key('N'); // to the previous (wraps)
    assert_eq!(
        s.app.diff_change_position(),
        Some((1, 2)),
        "N で 1 番目へ戻る"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_f_toggles_follow_diff_scope_without_breaking_follow() {
    // Regression: f (ToggleFollowDiffScope) is a display option that switches the follow-originated
    // diff's scope (since-start ⇄ full), not a takeover of manual operation → it must not break follow.
    // Before the fix, the "anything but F is follow_break" check didn't include f, so pressing it turned FOLLOW OFF.
    let dir = sandbox("follow_f_scope");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('F');

    let a = s.app.tab.root.join("a.rs");
    std::fs::write(&a, "fn a() { let _x = 2; }\n").unwrap();
    assert!(s.app.follow_note_change(&a));
    s.app.follow_jump(&a);
    s.draw();
    assert!(s.app.is_git_diff_preview(), "追尾先は全画面 diff で開く");
    assert!(s.app.follow_enabled(), "f を押す前は follow が ON");

    s.key('f');
    assert!(
        s.app.follow_enabled(),
        "f はフォロー表示オプション=follow を解除してはいけない(回帰)"
    );
    assert_eq!(
        s.app.follow_diff_scope_msg(),
        Some(crate::i18n::Msg::FollowShowFull),
        "1 回目の f で範囲がフルへ切り替わる"
    );

    s.key('f');
    assert!(
        s.app.follow_enabled(),
        "2 回目の f でも follow は ON のまま"
    );
    assert_eq!(
        s.app.follow_diff_scope_msg(),
        Some(crate::i18n::Msg::FollowShowSince),
        "2 回目の f で範囲が開始以降へ戻る"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_follow_survives_scroll_and_cycle_breaks_only_on_q() {
    // Regression (2026-07-23, user's choice = maximally sticky): inside a follow-originated diff,
    // every key except q (which exits the diff via PreviewBack) — scroll/horizontal scroll/n's
    // changed-file cycling — keeps follow on. Before the fix, any key other than F/f broke it (hands-off/Zed-style).
    let dir = sandbox("follow_sticky");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('F');
    assert!(s.app.follow_enabled(), "F でフォロー ON");

    let a = s.app.tab.root.join("a.rs");
    std::fs::write(&a, "fn a() { let _x = 2; }\n").unwrap();
    assert!(s.app.follow_note_change(&a));
    s.app.follow_jump(&a);
    s.draw();
    assert!(s.app.is_git_diff_preview(), "追尾先は全画面 diff で開く");
    assert!(s.app.follow_enabled(), "diff を開いた直後は follow ON");

    s.key('j');
    assert!(s.app.follow_enabled(), "スクロールでフォロー維持");
    s.key('h');
    assert!(s.app.follow_enabled(), "横スクロールでフォロー維持");
    s.key('n');
    assert!(
        s.app.follow_enabled(),
        "n(変更ファイル回遊)でもフォロー維持"
    );
    s.key('q');
    assert!(!s.app.follow_enabled(), "q(diff を出る)でだけフォロー解除");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Markdown syntax render verification + wrapped-focus following
// (strictly glyph-only verification — color/reversal can't be read from screen())
// =============================================================================

/// Display column (== screen char index, since screen() emits exactly one char per cell:
/// a wide CJK char is stored in one cell and its trailing cell resets to " ") of every
/// `target` glyph in a row string. Lets us compare table-column alignment across rows that
/// contain CJK of differing widths.
fn glyph_cols(row: &str, target: char) -> Vec<usize> {
    row.chars()
        .enumerate()
        .filter(|(_, c)| *c == target)
        .map(|(i, _)| i)
        .collect()
}

#[test]
fn e2e_md_table_renders_box_and_aligns_cjk() {
    // A GFM table is drawn with line-drawing glyphs (┌┬┐ │ ├┼┤ └┴┘), and columns align even with CJK mixed in.
    let dir = sandbox("md_table_grid");
    seed_files(&dir);
    std::fs::write(
        dir.join("grid.md"),
        "| Tool | 名称 |\n|------|------|\n| alpharow | 設定ファイル |\n| betarow | 日本語 |\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("grid.md");
    s.enter();
    // T-shaped/cross glyphs unique to tables (the preview frame only uses ┌┐└┘─│, so these are proof of the table).
    s.see("");
    s.see("");
    s.see("");
    s.see("");
    s.see("");
    // Column alignment: the vertical bar │'s display column matches across the header row and both data rows (proof CJK width is handled correctly).
    let scr = s.screen();
    let rows: Vec<&str> = scr.lines().collect();
    let bars = |needle: &str| -> Vec<usize> {
        let row = rows
            .iter()
            .find(|l| l.contains(needle))
            .unwrap_or_else(|| panic!("row not found: {needle}\n{scr}"));
        glyph_cols(row, '')
    };
    let head = bars("Tool");
    let d1 = bars("alpharow");
    let d2 = bars("betarow");
    assert!(head.len() >= 4, "外枠+列区切りの │ が並ぶ: {head:?}");
    assert_eq!(head, d1, "ヘッダとデータ行1で列が揃う(CJK 桁): {scr}");
    assert_eq!(head, d2, "ヘッダとデータ行2で列が揃う(CJK 桁): {scr}");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_md_table_cell_link_shows_label_hides_url() {
    // [label](url) inside a table cell only shows the label (the URL is a hidden target, gone from the screen).
    // Can be focused with Tab, and Enter opens the target (./notes.txt) (the link lands in md_items).
    let dir = sandbox("md_table_link");
    seed_files(&dir); // includes notes.txt ("alpha\n...")
    std::fs::write(
        dir.join("tbl.md"),
        "| name | doc |\n|---|---|\n| konoma | [OpenDoc](./notes.txt) |\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("tbl.md");
    s.enter();
    s.see("OpenDoc"); // the label is displayed
    s.dont_see("[OpenDoc]"); // the raw Markdown syntax isn't shown
    s.dont_see("./notes.txt"); // the URL is removed by collapse_links, hidden
    s.tab(); // focus the in-table link
    assert_eq!(
        s.app.focused_item(),
        Some(0),
        "表内リンクが md_items の先頭"
    );
    s.enter(); // open the link target
    assert!(
        s.app
            .tab
            .preview_path
            .as_deref()
            .is_some_and(|p| p.ends_with("notes.txt")),
        "表内リンクの Enter で ./notes.txt を開く"
    );
    s.see("alpha"); // notes.txt's content
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_md_html_details_collapse_and_toggle() {
    // <details> (without open) is collapsed by default: summary shows but the body is hidden. Tab
    // focuses summary → Space/Enter expands it, revealing the body. The tags are stripped.
    let dir = sandbox("md_html_details");
    seed_files(&dir);
    std::fs::write(
        dir.join("html.md"),
        "before text\n\n<details>\n<summary>Summary text</summary>\n\nhidden body line\n\n</details>\n\nafter text\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("html.md");
    s.enter();
    s.see("Summary text"); // <summary> always shows
    s.dont_see("hidden body line"); // default is collapsed = the body is hidden
    s.dont_see("<summary>"); // the tag is stripped
    s.dont_see("<details>");
    // Tab focuses summary → Space expands it.
    s.tab();
    assert_eq!(s.app.focused_item(), Some(0), "summary が Tab アイテム");
    s.key(' ');
    s.see("hidden body line"); // expanded, the body shows
                               // Toggling again collapses it.
    s.enter();
    s.dont_see("hidden body line");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_md_thematic_break_becomes_rule_and_fenced_dashes_kept() {
    // `---` (thematic break) becomes a full-width ─ rule. --- inside a code fence stays literal, unconverted.
    let dir = sandbox("md_hr");
    seed_files(&dir);
    // Don't place a heading (avoids the H1/H2 underline rule mixing with ─).
    std::fs::write(
        dir.join("hr.md"),
        "above paragraph\n\n---\n\nbelow paragraph\n\n```text\nraw --- text\n```\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("hr.md");
    s.enter();
    s.see("above paragraph");
    s.see("below paragraph");
    // A thematic-break row: within the preview frame (leading │), a row exists where the rest is all ─.
    // (The frame's top/bottom border is excluded since it starts with ┌/└.) The *right* border is
    // either the frame's `│` or the scroll indicator's `█` — the indicator is drawn on top of that
    // column — so both close the row here; what this asserts is the rule's own width, not the frame.
    let has_rule = s.screen().lines().any(|l| {
        l.starts_with('')
            && (l.ends_with('') || l.ends_with(''))
            && l.chars().filter(|&c| c == '').count() >= 60
    });
    assert!(has_rule, "--- が全幅 ─ 罫線になる:\n{}", s.screen());
    // --- inside a fence stays literal (since extras run after the code path, there's no false trigger).
    s.see("raw --- text");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Markdown web parity Phase 1: bare-URL autolinking / GitHub Alerts / emoji
// (route every path through both code_bg="none" and the default = real rendering, not synthetic spans)
// =============================================================================

#[test]
fn e2e_markdown_autolink_and_emoji_full_pipeline() {
    // The full source → decorated spans → postprocess_md path. Prose URLs/emails/www become
    // focusable links and shortcodes convert; anything inside inline code or a code fence stays
    // verbatim — under BOTH code_bg settings (this is the regression the first, synthetic-span
    // unit tests missed: they never ran under code_bg="none" nor through the real render).
    let body = concat!(
        "Prose url https://prose.example here.\n\n",
        "Prose mail me@prose.example here.\n\n",
        "Prose www www.prose.example here.\n\n",
        "Prose emoji :rocket: here.\n\n",
        "Inline `https://in-code.example` and `:sparkles:` stay literal.\n\n",
        "```sh\ncurl https://in-fence.example  # :tada:\n```\n",
    );
    for (label, cfg) in [
        ("code_bg_set", Config::default()),
        ("code_bg_none", cfg_code_bg_none()),
    ] {
        let (mut s, dir) = md_preview(cfg, "autolink_emoji", body);
        // Exactly the three prose links, in document order — never the in-code / in-fence URLs.
        assert_eq!(
            s.app.md_link_targets(),
            vec![
                "https://prose.example".to_string(),
                "mailto:me@prose.example".to_string(),
                "http://www.prose.example".to_string(),
            ],
            "[{label}] only prose url/email/www linked; code URLs excluded"
        );
        // They are actually Tab-navigable (the point of autolinking).
        s.tab();
        assert_eq!(
            s.app.focused_item(),
            Some(0),
            "[{label}] Tab reaches the first link"
        );
        // Prose emoji converted; every code shortcode stays literal.
        s.see("🚀");
        s.dont_see(":rocket:");
        s.see(":sparkles:");
        s.see(":tada:");
        // In-code / in-fence URLs remain visible as plain text (just not links, asserted above).
        s.see("https://in-code.example");
        s.see("https://in-fence.example");
        std::fs::remove_dir_all(&dir).ok();
    }
}

#[test]
fn e2e_markdown_alert_callout_and_nested_code_fence() {
    // Alerts render as a labeled callout (not the raw `[!NOTE]`). The body's prose URL links, but a
    // code fence INSIDE the alert keeps its URL/shortcode verbatim (Finding 1: alert body lines are
    // bar-prefixed, so the code gutter is the 2nd span — is_code_line must scan all spans).
    let body = concat!(
        "> [!NOTE]\n",
        "> Docs https://alert-prose.example here.\n",
        "> ```sh\n",
        "> curl https://alert-fence.example # :tada:\n",
        "> ```\n",
    );
    for (label, cfg) in [
        ("code_bg_set", Config::default()),
        ("code_bg_none", cfg_code_bg_none()),
    ] {
        let (s, dir) = md_preview(cfg, "alert_code", body);
        s.see("Note"); // callout label
        s.dont_see("[!NOTE]"); // raw marker gone
        assert_eq!(
            s.app.md_link_targets(),
            vec!["https://alert-prose.example".to_string()],
            "[{label}] only the alert prose URL links, not the fenced one"
        );
        s.see(":tada:"); // fenced shortcode literal
        s.see("https://alert-fence.example"); // visible as text, not a link
        std::fs::remove_dir_all(&dir).ok();
    }
}

#[test]
fn e2e_markdown_alert_all_five_types_and_aliases() {
    // All five GitHub types plus an alias render a callout label; none leak the raw marker.
    let body = concat!(
        "> [!NOTE]\n> n\n\n",
        "> [!TIP]\n> t\n\n",
        "> [!IMPORTANT]\n> i\n\n",
        "> [!WARNING]\n> w\n\n",
        "> [!CAUTION]\n> c\n\n",
        "> [!danger] Aliased\n> d\n",
    );
    let (s, dir) = md_preview(Config::default(), "alert_types", body);
    for label in ["Note", "Tip", "Important", "Warning", "Caution"] {
        s.see(label);
    }
    s.see("Caution — Aliased"); // danger alias → Caution, with its inline title
    s.dont_see("[!");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_autolink_emoji_alerts_toggle_off() {
    // With the three toggles off: bare URL stays plain, shortcode literal, alert stays a blockquote.
    let body = concat!(
        "> [!WARNING]\n> careful\n\n",
        "Prose https://x.example :rocket: here.\n",
    );
    let mut cfg = cfg_code_bg_none();
    cfg.ui.md_autolink = false;
    cfg.ui.md_emoji = false;
    cfg.ui.md_alerts = false;
    let (s, dir) = md_preview(cfg, "md_toggles_off", body);
    assert!(s.app.md_link_targets().is_empty(), "autolink off: no links");
    s.see(":rocket:"); // emoji off: literal
    s.see("https://x.example"); // not a link
    s.see("[!WARNING]"); // alerts off: raw marker kept
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_inline_html_renders() {
    let body = "Press <kbd>Ctrl</kbd>. H<sub>2</sub>O. <del>old</del> new.\n";
    let (s, dir) = md_preview(Config::default(), "inline_html", body);
    s.see("Ctrl"); // <kbd> content, rendered as an inline-code keycap
    s.see("H₂O"); // <sub> → Unicode subscript
    s.see("new"); // surrounding text intact
    s.dont_see("<kbd>"); // raw tags gone
    s.dont_see("<sub>");
    s.dont_see("<del>");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_footnotes_render_superscript_and_section() {
    let body = "A claim.[^src] More text.\n\n[^src]: The evidence.\n";
    let (s, dir) = md_preview(Config::default(), "footnotes", body);
    s.see("¹"); // superscript reference marker
    s.dont_see("[^src]"); // raw reference and definition are gone
    s.see("The evidence."); // definition text in the footnotes section
    std::fs::remove_dir_all(&dir).ok();
}

/// Documentation *of* a syntax must survive the preview. Both preprocessing passes run in the app
/// layer (`md_render.rs`), not inside `render_markdown_tasks`, so the unit tests over the passes
/// alone never touch this path — this scenario drives the real one, from a key press to the drawn
/// screen, under the user's `code_bg = "none"` theme (the setting that has hidden code-detection
/// bugs before).
///
/// Before the shared code-span primitive: `` `[^1]` `` became `` `¹` `` and `` `<kbd>Ctrl</kbd>` ``
/// became ``` ``Ctrl`` ``` — a *doubled* backtick pair that broke the span outright — so a document
/// explaining either syntax could not show it.
#[test]
fn e2e_markdown_code_span_examples_are_not_rewritten() {
    let body = concat!(
        "Write `<kbd>Ctrl</kbd>` to get a keycap, like <kbd>Ctrl</kbd>.\n\n",
        "Write `[^1]` for a reference, like this one.[^1]\n\n",
        "A line break tag is `<br>`.\n\n",
        "[^1]: The note.\n",
    );
    let (s, dir) = md_preview(cfg_code_bg_none(), "code_span_examples", body);
    // The examples survive verbatim…
    s.see("<kbd>Ctrl</kbd>");
    s.see("[^1]");
    s.see("<br>");
    // …and the doubled-backtick corruption never appears.
    s.dont_see("``");
    // …while the real markup on the same lines is still converted.
    s.see("¹"); // the reference outside the span became a superscript
    s.see("The note."); // and its definition moved into the footnotes section
    s.see("Ctrl"); // the <kbd> outside the span became a keycap
    std::fs::remove_dir_all(&dir).ok();
}

/// The same invariant as the test above, but for CommonMark's *other* kind of code block — a
/// **fenced** block and an **indented** (4-column) block, in the same document, one right after
/// the other — under the real app pipeline (key press → drawn screen), with the user's
/// `code_bg = "none"` theme (2026-08: `literal_code_mask` folds `code_block_mask` into `fence_mask`
/// so both kinds are protected the same way; this drives that union through `md_render.rs`, not
/// just the string-rewriting passes in isolation).
#[test]
fn e2e_markdown_indented_and_fenced_code_blocks_protect_the_same_notations() {
    let body = concat!(
        "Fenced example:\n\n",
        "```\n[^1] <kbd>K</kbd>\n```\n\n",
        "Indented example:\n\n",
        "    [^2] <kbd>J</kbd>\n\n",
        "Outside: real[^1] and <kbd>Ctrl</kbd>, and another[^2].\n\n",
        "[^1]: The note.\n[^2]: The other note.\n",
    );
    let (s, dir) = md_preview(cfg_code_bg_none(), "fence_and_indent_protect", body);
    // Both blocks' examples survive verbatim — neither the reference nor the tag was converted.
    s.see("[^1] <kbd>K</kbd>"); // fenced block
    s.see("[^2] <kbd>J</kbd>"); // indented block
                                // …while the identical notations outside either block still convert as usual.
    s.dont_see("<kbd>Ctrl</kbd>"); // converted to a keycap, tag gone
    s.see("¹"); // real[^1] outside → superscript 1
    s.see("²"); // another[^2] outside → superscript 2
    s.see("The note.");
    s.see("The other note.");
    std::fs::remove_dir_all(&dir).ok();
}

/// `<br>` inside either kind of code block used to inject a real newline *into* the block, splitting
/// it into two — the most visible way this class of bug breaks a document (a code block's header
/// draws around only the first half, and the second half loses its gutter/fence entirely). This
/// checks the on-screen row layout directly: the block's content stays on one row; the identical
/// tag outside a block still becomes a real hard line break, landing "before"/"after" on two rows.
#[test]
fn e2e_markdown_br_inside_a_code_block_does_not_split_it() {
    let body = concat!(
        "Fenced example:\n\n",
        "```\nleft<br>right\n```\n\n",
        "Indented example:\n\n",
        "    leftind<br>rightind\n\n",
        "Outside: before<br>after.\n",
    );
    let (s, dir) = md_preview(cfg_code_bg_none(), "br_code_block_integrity", body);
    // Inside either block, the tag and both halves of the line stay together — one screen row.
    s.see("left<br>right");
    s.see("leftind<br>rightind");
    // Outside, the same tag becomes a real break: "before" and "after" land on different rows.
    let (before_row, _) = s.find_text("before").expect("before is drawn");
    let (after_row, _) = s.find_text("after.").expect("after is drawn");
    assert!(
        after_row > before_row,
        "<br> outside a code block still injects a real line break: before_row={before_row}, \
         after_row={after_row}\n{}",
        s.screen()
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Root cause (2026-08): rewriting `<br>` unconditionally into `"  \n"` injects a line that carries
/// none of the prefix its enclosing block requires. `split_alerts` captures a callout body by
/// consuming *consecutive* `>`-prefixed lines, so the box ended at the injected line, everything
/// after it escaped, and a fence written inside the alert leaked onto the screen as literal
/// `` > ```rust `` text instead of being drawn as a code block.
///
/// The unit tests for this measure the renderer's `Line`s. This drives the same document through the
/// app — key press → `md_render.rs` preprocessing → drawn screen — and asserts on the screen rows,
/// under the user's real `code_bg = "none"` theme. Both spellings are present because they take
/// different branches: a mid-line `<br>` injects a prefixed continuation, while a trailing one must
/// emit the `"  "` marker and **no** newline (a newline there would make an empty quote line, which
/// splits the callout's paragraph instead of breaking a line).
#[test]
fn e2e_markdown_br_in_an_alert_keeps_the_callout_and_its_fence_whole() {
    let body = concat!(
        "> [!NOTE]\n",
        "> line one<br>line two\n",
        "> tail line<br>\n",
        "> after the break\n",
        ">\n",
        "> ```rust\n",
        "> fn inside_the_note() {}\n",
        "> ```\n",
        "\nAfter the note.\n",
    );
    let (mut s, dir) = md_preview(cfg_code_bg_none(), "br_alert_callout", body);

    // The row holding each piece of the body also carries the callout's own bar — i.e. the text is
    // still *inside* the box, not text that escaped it.
    for needle in [
        "line one",
        "line two",
        "tail line",
        "after the break",
        "fn inside_the_note",
    ] {
        let (row, _) = s
            .find_text(needle)
            .unwrap_or_else(|| panic!("「{needle}」が画面にない:\n{}", s.screen()));
        let screen = s.screen();
        let text = screen.lines().nth(row as usize).unwrap_or_default();
        assert!(
            text.contains(''),
            "「{needle}」の行が callout の外に逃げている: {text:?}\n{screen}"
        );
    }
    // The fence inside the alert is drawn as a code block (gutter present), and its raw markers
    // never reach the screen — leaked markup is exactly what the bug produced.
    s.see("");
    s.dont_see("```");
    s.dont_see("> fn inside_the_note");
    // …and it is a real, copyable code block: the count guard passed, so `Tab` reaches it and the
    // copy resolves to the fence's own source with the `>` markers stripped, as drawn.
    s.tab();
    assert!(
        s.app.md_focused_code(),
        "alert 内の fence に Tab でフォーカスできない:\n{}",
        s.screen()
    );
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("fn inside_the_note() {}"),
        "alert 内 fence のコピー内容"
    );
    // A line break, not a paragraph break. The trailing `<br>` must emit only the `"  "` marker:
    // adding a newline as well would inject an empty `> ` line, which reads as a blank quote line
    // and splits the callout's paragraph — a stray `▌` row on screen. The document has exactly one
    // genuinely empty quote line of its own (the `>` between the prose and the fence), so the count
    // is the test, not the presence.
    let screen = s.screen();
    // `█` joins the trim set alongside the frame's `│`: the scroll indicator is drawn on top of the
    // right border column, so a row that is "nothing but the callout bar" can end in either glyph.
    let blank_callout_rows = screen
        .lines()
        .filter(|r| r.trim_matches(|c| c == '' || c == '' || c == ' ') == "")
        .count();
    assert_eq!(
        blank_callout_rows, 1,
        "callout 本文の空行数が想定外(行末 <br> が段落区切りになっている):\n{screen}"
    );
    // The document continues normally below the callout.
    s.see("After the note.");
    std::fs::remove_dir_all(&dir).ok();
}

/// `$…$` inside an indented code block must not be lifted out onto its own image row — the same
/// "is this line literal code" question `process_footnotes`/`process_inline_html` ask, but for
/// `split_math`, which has no string output to eyeball on screen the way the other two do. A picker
/// is required: without one, `math` never enters image mode at all (`md_images()` would stay empty
/// regardless of whether the indented block was protected), so this would pass vacuously.
#[test]
fn e2e_markdown_math_inside_an_indented_code_block_is_not_lifted_as_an_image() {
    let body = concat!(
        "Indented code with a dollar sign, not math:\n\n",
        "    literal $x$ stays text\n\n",
        "Outside this is real math: $y$ done.\n",
    );
    let dir = sandbox("math_indented_code");
    std::fs::write(dir.join("d.md"), body).unwrap();
    let root = canon(&dir);
    let mut s = Sim::with_config(&root, cfg_code_bg_none()).with_picker();
    s.select("d.md");
    s.enter();
    assert_eq!(
        s.app.md_images().len(),
        1,
        "the only image placement must be the outside $y$ — the indented block's $x$ must not be \
         lifted out as a second one"
    );
    s.see("literal $x$ stays text"); // the indented block's dollar sign stays literal, on screen
    std::fs::remove_dir_all(&dir).ok();
}

/// The one case in `literal_code_mask`'s own doc comment where `fence_mask`, not `code_block_mask`,
/// is the one doing the protecting: a fence with no blank line between it and a preceding
/// `<summary>…</summary>` line, inside a `<details>` block. `md_details = "open"` forces the block
/// visible (the default "auto" would collapse it, since there is no `open` attribute here — that
/// collapse behavior is covered elsewhere; this test is only about what's inside once it's open).
#[test]
fn e2e_markdown_details_summary_then_a_fence_with_no_blank_line_stays_literal() {
    let mut cfg = cfg_code_bg_none();
    cfg.ui.md_details = "open".into();
    let body = concat!(
        "<details>\n",
        "<summary>s</summary>\n",
        "```rust\n[^1] <kbd>K</kbd>\n```\n",
        "</details>\n\n",
        "Outside: real[^1] and <kbd>Ctrl</kbd>.\n\n",
        "[^1]: The note.\n",
    );
    let (s, dir) = md_preview(cfg, "details_fence_no_blank_e2e", body);
    s.see("[^1] <kbd>K</kbd>"); // the fence right after <summary>, no blank line, stays literal
    s.dont_see("<kbd>Ctrl</kbd>"); // the identical tag outside the details block still converts
    s.see("¹");
    s.see("The note.");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_footnotes_off_stays_literal() {
    let body = "A claim.[^src]\n\n[^src]: The evidence.\n";
    let mut cfg = Config::default();
    cfg.ui.md_footnotes = false;
    let (s, dir) = md_preview(cfg, "footnotes_off", body);
    s.see("[^src]"); // literal, unprocessed
    std::fs::remove_dir_all(&dir).ok();
}

/// Shared by the on/off front-matter pair below so the two configs render **the same source** —
/// otherwise a needle that merely differs between two unrelated documents proves nothing about the
/// config flag itself.
const FRONT_MATTER_BODY: &str = concat!(
    "---\n",
    "title: My Doc\n",
    "author: Me\n",
    "---\n",
    "[to section](#real-heading)\n\n",
    "# Real Heading\n\n",
    "body text\n",
);

#[test]
fn e2e_markdown_front_matter_renders_as_metadata() {
    // Leading `---`…`---` is shown as a metadata block; the body (and its heading anchors) render
    // normally after it. Anchor jump on a heading defined below the front matter still resolves.
    let (mut s, dir) = md_preview(Config::default(), "frontmatter", FRONT_MATTER_BODY);
    s.see("title"); // metadata key
    s.see("My Doc"); // metadata value
    s.see("author");
    s.see("Real Heading"); // body heading still renders
                           // The needle above ("title" as plain text) is not enough by itself: with `md_frontmatter =
                           // false` the exact same source still shows the literal word "title" on screen too (tui-markdown
                           // renders the un-stripped `---`...`---` block itself — see the `off` test below), so the two
                           // configs are indistinguishable by text alone. What differs is *how* it's styled: konoma's own
                           // `render_front_matter` colors the key Cyan+DIM, closing the block with a rule. Dumped and
                           // confirmed by hand (`cargo test -- --nocapture`) before picking this needle.
    s.see_styled(
        "title",
        |st| {
            st.fg == Some(ratatui::style::Color::Cyan)
                && st.add_modifier.contains(ratatui::style::Modifier::DIM)
        },
        "konoma 自身のフロントマター key 色(Cyan+DIM) — off でも「title」というテキスト自体は\
         画面に出る(tui-markdown 自身が生の---ブロックを描く)ので、テキストだけでは on/off を\
         区別できない",
    );
    // The anchor (computed from the body heading, past the front matter offset) resolves.
    assert_eq!(
        s.app.md_link_targets(),
        vec!["#real-heading".to_string()],
        "the in-page anchor link is present"
    );
    s.tab();
    s.enter();
    assert!(
        s.app.flash.is_none(),
        "anchor resolves (no 'not found' flash)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_front_matter_off_leaves_body_intact() {
    // Same source as the `on` test above — only the config flag differs, so any on/off difference
    // in what follows is attributable to `md_frontmatter` and nothing else.
    let mut cfg = Config::default();
    cfg.ui.md_frontmatter = false;
    let (s, dir) = md_preview(cfg, "frontmatter_off", FRONT_MATTER_BODY);
    // Recognition off: the title line and body both still render (no crash, old behavior). This much
    // is true in *both* configs (see the comment on the `on` test), so on its own it does not prove
    // recognition is off.
    s.see("title");
    s.see("body text");
    // What actually proves it's off: konoma's own pre-pass never ran, so the raw `---`...`---` block
    // reaches tui-markdown unstripped. tui-markdown's parser unconditionally recognizes YAML-style
    // metadata blocks (`ParseOptions::ENABLE_YAML_STYLE_METADATA_BLOCKS`, always on — see
    // `tui_markdown_parse_options` in preview/markdown.rs) and renders the *whole* block with its own
    // `metadata_block()` style — plain LightYellow, no separate key/value coloring. Confirmed by hand
    // (`cargo test -- --nocapture`, dumping `style_of("title")`) that this is exactly the opposite of
    // the `on` test's Cyan+DIM key style, so the two needles are mutually exclusive.
    s.see_styled(
        "title",
        |st| st.fg == Some(ratatui::style::Color::LightYellow),
        "tui-markdown 自身のネイティブ YAML front-matter 色(LightYellow) — konoma 側の\
         Cyan+DIM key 色にはならない(pre-pass が走っていない証拠)",
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_outline_lists_headings_and_jumps() {
    // `o` opens a heading outline; Enter on a heading scrolls the preview there.
    let mut body = String::from("# Top Heading\n\nintro\n\n");
    for i in 0..40 {
        body.push_str(&format!("filler line {i}\n\n"));
    }
    body.push_str("## Deep Section\n\ndeep body text\n");
    let (mut s, dir) = md_preview(Config::default(), "outline_jump", &body);
    assert_eq!(s.app.tab.preview_scroll, 0);
    s.key('o');
    assert!(s.app.is_outline(), "o opens the outline");
    s.see("Top Heading");
    s.see("Deep Section");
    // On open the selection is the current section (Top, at scroll 0); j → the deep heading.
    assert_eq!(s.app.outline_sel(), 0);
    s.key('j');
    assert_eq!(s.app.outline_sel(), 1);
    s.enter();
    assert!(!s.app.is_outline(), "Enter closes the outline");
    assert!(
        s.app.tab.preview_scroll > 0,
        "jumped down to the deep heading"
    );
    s.see("Deep Section");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_outline_toggle_and_esc_close() {
    let (mut s, dir) = md_preview(
        Config::default(),
        "outline_toggle",
        "# A\n\nx\n\n## B\n\ny\n",
    );
    s.key('o');
    assert!(s.app.is_outline());
    s.key('o'); // o again closes
    assert!(!s.app.is_outline());
    s.key('o');
    assert!(s.app.is_outline());
    s.esc(); // Esc also closes
    assert!(!s.app.is_outline());
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_outline_closes_on_new_tab() {
    // The outline overlay is app-global; opening a new tab (`t`, inherited from global while the
    // overlay is up) must close it, or an empty overlay would linger over the fresh tab.
    let (mut s, dir) = md_preview(
        Config::default(),
        "outline_newtab",
        "# A\n\nx\n\n## B\n\ny\n",
    );
    s.key('o');
    assert!(s.app.is_outline());
    s.key('t'); // global new tab
    assert!(!s.app.is_outline(), "a new tab closes the outline overlay");
    assert_eq!(s.app.tab.mode, Mode::Tree, "the new tab is a fresh tree");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_outline_empty_flashes_on_plain_text() {
    // A plain-text (windowed, non-Markdown) preview has no heading outline: `o` flashes, no overlay.
    let dir = sandbox("outline_empty");
    std::fs::write(dir.join("f.txt"), "# not a markdown heading\njust text\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('o');
    assert!(
        !s.app.is_outline(),
        "no overlay opens for a non-Markdown preview"
    );
    assert!(s.app.flash.is_some(), "a 'no headings' flash is shown");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_anchor_jump_scrolls_to_heading() {
    // An in-page anchor `[x](#slug)` scrolls the decorated preview to the matching heading (it used
    // to flash "not supported"). Filler pushes the target well below the fold.
    let mut body = String::from("[go to target](#the-target)\n\n");
    for i in 0..40 {
        body.push_str(&format!("line {i}\n\n"));
    }
    body.push_str("## The Target\n\ndestination text\n");
    let (mut s, dir) = md_preview(Config::default(), "anchor_jump", &body);
    assert_eq!(s.app.tab.preview_scroll, 0, "starts at the top");
    s.tab(); // focus the anchor link
    assert_eq!(s.app.focused_item(), Some(0));
    s.enter(); // jump
    assert!(
        s.app.tab.preview_scroll > 0,
        "scrolled down toward the heading (was {})",
        s.app.tab.preview_scroll
    );
    s.see("The Target");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_anchor_jump_unknown_slug_flashes() {
    let body = "[bad](#nope)\n\n# Real Heading\n\nbody\n";
    let (mut s, dir) = md_preview(Config::default(), "anchor_bad", body);
    s.tab();
    s.enter();
    // Unknown anchor: a "no heading" flash, and no scroll.
    assert!(
        s.screen().contains("nope") || s.app.flash.is_some(),
        "unknown anchor flashes"
    );
    assert_eq!(
        s.app.tab.preview_scroll, 0,
        "no scroll for a missing anchor"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_autolink_cjk_and_trailing_punctuation() {
    // Byte-boundary safety: a bare URL right after CJK, and trailing sentence punctuation trimmed.
    let body = "見るhttps://cjk.example、と (https://paren.example) を確認。\n";
    let (s, dir) = md_preview(cfg_code_bg_none(), "autolink_cjk", body);
    assert_eq!(
        s.app.md_link_targets(),
        vec![
            "https://cjk.example".to_string(),
            "https://paren.example".to_string(),
        ],
        "CJK boundary + paren-wrapped URL both link, punctuation trimmed"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_md_task_markers_render_ascii_or_nf_by_icons() {
    // Tasks `- [ ]` / `- [x]`: ASCII brackets when ui.icons=false, NF glyphs when true.
    let dir = sandbox("md_tasks");
    seed_files(&dir);
    std::fs::write(dir.join("tasks.md"), "- [ ] todo item\n- [x] done item\n").unwrap();
    let root = canon(&dir);

    // icons=false: ASCII "[ ]" / "[x]".
    let mut cfg = Config::default();
    cfg.ui.icons = false;
    let mut s = Sim::with_config(&root, cfg);
    s.select("tasks.md");
    s.enter();
    s.see("[ ] todo item");
    s.see("[x] done item");

    // icons=true (default): Nerd Font glyphs (square_o / check_square_o); ASCII brackets don't appear.
    let mut s2 = Sim::new(&root); // Config::default() = icons true
    s2.select("tasks.md");
    s2.enter();
    s2.see(&crate::ui::icons::task_icon(false).to_string()); // '\u{f096}'
    s2.see(&crate::ui::icons::task_icon(true).to_string()); //  '\u{f046}'
    s2.dont_see("[ ]");
    s2.dont_see("[x]");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_md_raw_source_toggle() {
    // R switches Markdown between decorated display ⇄ raw source (windowed, "· raw source" in the title).
    let dir = sandbox("md_raw");
    seed_files(&dir);
    std::fs::write(
        dir.join("guide.md"),
        "# Heading\n\nsome **bold** text and a [link](./notes.txt)\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("guide.md");
    s.enter();
    // Note: the footer always shows the "R raw source" hint even while decorated (a toggle notice).
    // The title's raw marker is "· raw source" (with a middle dot), so use it for the mode check.
    assert!(!s.app.is_raw_source(), "初期は装飾表示");
    s.dont_see("· raw source"); // no raw marker in the title (distinct from the hint's "raw source")
    s.key('R');
    assert!(s.app.is_raw_source(), "R で raw ソース表示");
    s.see("· raw source"); // the title's "· raw source" (short enough not to be cut, being a relative path)
    s.key('R');
    assert!(!s.app.is_raw_source(), "R で装飾表示へ戻る");
    s.dont_see("· raw source");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_edit_opens_near_scroll_position() {
    // Scrolling decorated Markdown and pressing `e` opens the editor at an approximate source line
    // corresponding to the current display position (approximate, not exact, because of reflow). At
    // the top it's line 1; scrolling down advances it. Since Sim draws the UI after every key, md_view_rows (the total wrapped row count) is set just as in real usage.
    let dir = sandbox("md_edit_line");
    let mut content = String::from("# Top\n\n");
    for i in 1..=25 {
        content.push_str(&format!(
            "Intro prose line {i:02} with several words to read on it here.\n"
        ));
    }
    content.push_str("\n## Section Two\n\n");
    for i in 26..=50 {
        content.push_str(&format!(
            "Section prose line {i:02} with several words to read on it here.\n"
        ));
    }
    std::fs::write(dir.join("doc.md"), content).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("doc.md");
    s.enter(); // decorated Markdown preview
    assert!(!s.app.is_windowed(), "装飾 md は非 windowed");

    // At the top, it opens at line 1.
    s.key('e');
    let (_p, top) = s.app.take_pending_edit().expect("edit requested");
    assert_eq!(top, Some(1), "先頭スクロールは 1 行目");

    // Scroll down far enough, then `e`: it doesn't stick to the top, and points at a later source line.
    for _ in 0..40 {
        s.key('j');
    }
    s.key('e');
    let (_p, deep) = s.app.take_pending_edit().expect("edit requested");
    let deep = deep.expect("装飾 md は描画後に近似行を渡す");
    assert!(
        deep > 10,
        "スクロール後は後方のソース行(>10)を指すべき: {deep}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_edit_content_anchor_hits_exact_line() {
    // Place a unique heading after a long, wrapping intro. Proportional estimation alone would drift
    // due to wrapping, but confirm that content-anchor searches the source for the screen-top text and lands on the heading's "exact" source line.
    let dir = sandbox("md_anchor");
    let mut content = String::from("# Title\n\n");
    for i in 1..=15 {
        content.push_str(&format!(
            "Intro paragraph {i:02} with a deliberately long sentence that wraps across several display rows in a narrow preview here.\n"
        ));
    }
    content.push_str("\n## ANCHORZZZ Section\n\n");
    // Stack enough following lines so the heading can be pushed up to the screen top (row 2).
    for i in 1..=60 {
        content.push_str(&format!("Trailing line {i:02} after the anchor.\n"));
    }
    // The heading's source line (1-based).
    let heading_line = content
        .lines()
        .position(|l| l.contains("ANCHORZZZ"))
        .expect("見出しがある")
        + 1;
    std::fs::write(dir.join("doc.md"), &content).unwrap();

    let mut s = Sim::new(&canon(&dir));
    s.select("doc.md");
    s.enter(); // decorated Markdown preview
    assert!(!s.app.is_windowed(), "装飾 md は非 windowed");

    // Scroll one line at a time until the heading reaches the screen top (frame's row 1 = buffer's row 3).
    let mut scrolled = 0;
    while scrolled < 300 {
        let top = s.screen().lines().nth(2).unwrap_or("").to_string();
        if top.contains("ANCHORZZZ") {
            break;
        }
        s.key('j');
        scrolled += 1;
    }
    s.see("ANCHORZZZ"); // the heading is showing on screen

    s.key('e');
    let (_p, line) = s.app.take_pending_edit().expect("edit requested");
    assert_eq!(
        line,
        Some(heading_line),
        "content-anchor は見出しの正確なソース行 {heading_line} に着地すべき"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_markdown_edit_lands_on_tab_focused_item() {
    // If Tab has focused an item (task/link), `e` opens at that focused line instead of the screen
    // top. Advancing Tab moves the focus, and the opened line follows along.
    let dir = sandbox("md_focus_edit");
    let content = "# Title\n\nIntro paragraph one.\n\n- [ ] UNIQUETASK finish the report\n\nMiddle paragraph.\n\nSee [UNIQUELINK](./target.txt) here.\n";
    std::fs::write(dir.join("doc.md"), content).unwrap();
    std::fs::write(dir.join("target.txt"), "x\n").unwrap();
    let task_line = content
        .lines()
        .position(|l| l.contains("UNIQUETASK"))
        .expect("task line")
        + 1;
    let link_line = content
        .lines()
        .position(|l| l.contains("UNIQUELINK"))
        .expect("link line")
        + 1;

    let mut s = Sim::new(&canon(&dir));
    s.select("doc.md");
    s.enter(); // decorated Markdown preview
    assert!(!s.app.is_windowed(), "装飾 md は非 windowed");

    // Tab → focuses the first item (the task). e → opens at the task's source line.
    s.tab();
    s.key('e');
    let (_p, line) = s.app.take_pending_edit().expect("edit requested");
    assert_eq!(
        line,
        Some(task_line),
        "Tab フォーカスのタスク行 {task_line}"
    );

    // Tab again → to the link. e → opens at the link's source line.
    s.tab();
    s.key('e');
    let (_p, line) = s.app.take_pending_edit().expect("edit requested");
    assert_eq!(
        line,
        Some(link_line),
        "Tab フォーカスのリンク行 {link_line}"
    );

    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_md_code_block_tab_focus_and_copy() {
    // Tab cycles link → code block → task in document order, and while a code block is focused,
    // `y` opens the copy menu where the `c` that appears there copies its raw source (the value is
    // verified via a clipboard-independent getter). Enter does nothing on a code block.
    let dir = sandbox("md_code_copy");
    seed_files(&dir);
    std::fs::write(
        dir.join("snip.md"),
        "see [doc](./notes.txt)\n\n```rust\nfn main() {}\nlet y = 2;\n```\n\n- [ ] todo\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("snip.md");
    s.enter();
    s.see("rust"); // the code block's language header
    s.tab(); // the link
    assert!(!s.app.md_focused_code());
    s.tab(); // the code block
    assert!(s.app.md_focused_code(), "2番目=コードブロックにフォーカス");
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("fn main() {}\nlet y = 2;"),
        "生ソースをコピー対象に"
    );
    // Enter is a no-op on a code block (no which-key, no flash either).
    s.enter();
    assert!(s.app.flash.is_none(), "Enter はコードブロックで何もしない");
    // `y` doesn't hijack it — it opens the copy menu (which-key). While focused, `c:code block` appears.
    s.key('y');
    assert_eq!(
        s.app.pending_leader,
        Some(crate::keymap::LeaderId::Copy),
        "y はコピーリーダー(which-key)を開く"
    );
    s.see("code block"); // the code-block copy entry shows up in the menu
                         // `c` copies (since the clipboard is environment-dependent, only confirm the flash fires).
    s.key('c');
    assert!(s.app.pending_leader.is_none(), "c で確定=リーダー閉じる");
    assert!(s.app.flash.is_some(), "y c でコピー通知が出る");
    s.tab(); // the task
    assert!(!s.app.md_focused_code());
    std::fs::remove_dir_all(&dir).ok();
}

/// Root cause (indented code blocks, 2026-08): a document containing a 4+-column indented code
/// block refused `y c` for **every** code block in it (the count guard is document-wide), so a real
/// fence right next to a perfectly ordinary indented example also stopped copying. Drives Tab → `y`
/// `c` with real keystrokes across a document mixing a fence and an indented block, and confirms
/// each one copies its *own* content — not its neighbor's, which an off-by-one in the ordinal
/// mapping between the two block kinds could otherwise produce silently.
#[test]
fn e2e_md_indented_code_block_tab_focus_and_copy() {
    let dir = sandbox("md_indented_code_copy");
    seed_files(&dir);
    std::fs::write(
        dir.join("snip.md"),
        "Intro paragraph.\n\n```rust\nfn fenced() {}\n```\n\nMore text.\n\n    indented one\n    indented two\n\nTail.\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("snip.md");
    s.enter();
    s.see("rust"); // the fence's language header

    // 1st focusable item: the fenced block.
    s.tab();
    assert!(s.app.md_focused_code(), "1番目=フェンスコードにフォーカス");
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("fn fenced() {}"),
        "フェンス側の内容が取得できる(コピー拒否されない)"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "フェンス側のコピー通知が出る");

    // 2nd focusable item: the indented block. Must resolve to its own content, not the fence's.
    s.tab();
    assert!(s.app.md_focused_code(), "2番目=字下げコードにフォーカス");
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("indented one\nindented two"),
        "字下げ側の内容が正しく取得できる(フェンス側を誤って取ってこない)"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "字下げ側のコピー通知が出る");

    std::fs::remove_dir_all(&dir).ok();
}

/// Root cause (the document-level code mask, 2026-08 — reported by a user): an indented code block
/// whose **first line looked like an HTML tag** was not drawn as a code block at all.
/// `split_html_blocks` asked `fence_mask`, which has no notion of indentation, so the block was
/// rescued as an HTML block instead: its tags were stripped, its gutter was gone, and
/// `"para\n\n    <code>\n"` rendered as *nothing*. Because it never reached the renderer as code it
/// was also not Tab-focusable and not copyable — so the whole reason a reader indents a snippet (to
/// show markup literally, then copy it) failed silently.
///
/// The unit tests for this measure the renderer directly. This drives the shape a user actually hits
/// — key press → `md_render.rs` preprocessing → drawn screen → `Tab` → `y` `c` — under **both**
/// themes, because `code_bg = "none"` (the reporting user's real setting) removes the background
/// that some of this module's code detection has leaned on before, and a bg-only answer must not be
/// able to regress unnoticed again.
#[test]
fn e2e_md_indented_code_block_whose_content_is_a_tag_is_focusable_and_copyable() {
    for (label, cfg) in [
        ("default", Config::default()),
        ("code_bg=none", cfg_code_bg_none()),
    ] {
        let body = concat!(
            "Intro paragraph.\n\n",
            "    <code>\n",
            "    still code\n\n",
            "Tail with a real <kbd>Ctrl</kbd> outside.\n",
        );
        let (mut s, dir) = md_preview(cfg, "indented_tag_code", body);
        // Drawn as a code block: the content is on screen verbatim, behind the `▎` gutter. Under
        // the bug the tag was stripped and this row was empty.
        //
        // Both content lines land on the *same* row (`▎ <code>still code`) — tui-markdown runs an
        // indented block's adjacent lines together, unlike a fenced one. That is upstream behavior
        // rather than anything this fix does, and it is pinned directly in
        // `markdown.rs::tui_markdown_runs_an_indented_blocks_adjacent_lines_together`; asserted here
        // only as "both are present", so this test fails for the reason it is about.
        s.see("▎ <code>");
        s.see("still code");
        // …and the pass that would have rewritten it is demonstrably still running elsewhere, so
        // this is not "conversion stopped everywhere".
        s.dont_see("<kbd>Ctrl</kbd>");

        // Tab reaches it, and `y` `c` copies its exact raw source (not the screen's decorated form).
        s.tab();
        assert!(
            s.app.md_focused_code(),
            "{label}: 字下げコードブロックに Tab でフォーカスできない"
        );
        assert_eq!(
            s.app.focused_code_text().as_deref(),
            Some("<code>\nstill code"),
            "{label}: コピー対象が生ソースと一致しない"
        );
        s.key('y');
        assert_eq!(
            s.app.pending_leader,
            Some(crate::keymap::LeaderId::Copy),
            "{label}: y はコピーリーダーを開く"
        );
        s.see("code block");
        s.key('c');
        assert!(
            s.app.pending_leader.is_none(),
            "{label}: c で確定=リーダーが閉じる"
        );
        assert!(s.app.flash.is_some(), "{label}: y c でコピー通知が出る");
        std::fs::remove_dir_all(&dir).ok();
    }
}

/// The other half of the same pair, through the app pipeline: a centered `<div align="center">`
/// badge banner — the shape at the top of a great many crate READMEs — must draw **no** code gutter.
///
/// This is what makes the fix above hard rather than obvious. `split_block_parts` lifts the `<img>`
/// lines out before the block splitters run, and what is left (`    </a>` / `    <a href="…">`) is
/// byte-for-byte an indented code block when read on its own. The naive fix — point
/// `split_html_blocks` at the indentation-aware mask — turns every banner like this into a `▎ …`
/// block full of raw markup, which is exactly what three registry READMEs did while it was tried.
/// So this test and the one above have to pass **together**; either alone is satisfiable by a wrong
/// answer.
#[test]
fn e2e_md_centered_banner_draws_no_code_gutter() {
    let body = concat!(
        "<p align=\"center\">\n",
        "    <a href=\"https://crates.io/crates/demo\">\n",
        "      <img src=\"https://img.example/v.svg\" alt=\"crates.io badge\">\n",
        "    </a>\n",
        "    <br>\n",
        "    <a href=\"LICENSE-MIT\">\n",
        "      <img src=\"https://img.example/mit.svg\" alt=\"MIT badge\">\n",
        "    </a>\n",
        "</p>\n",
        "\nOrdinary prose after the banner.\n",
    );
    let (s, dir) = md_preview(cfg_code_bg_none(), "banner_no_gutter", body);
    // No code block anywhere on screen, and no raw markup leaked into one.
    s.dont_see("");
    s.dont_see("</a>");
    // The badges survived as images (so the `<img>` extraction that creates the ambiguity really
    // happened) and the prose below the banner was not swallowed.
    s.see("crates.io badge");
    s.see("MIT badge");
    s.see("Ordinary prose after the banner.");
    std::fs::remove_dir_all(&dir).ok();
}

/// The same banner **one container deep**, through the app pipeline.
///
/// Root cause (2026-08): `structure_mask` reported only the outermost construct, so inside
/// `<details>` (or a blockquote) every line of the banner came back as the container rather than as
/// HTML-block body. The lone `    <br>` therefore took the hard-break rewrite, `"    " + "  "` is a
/// blank line to CommonMark, the inner `<p align="center">` ended there, and the 4-column-indented
/// lines below it became a real indented code block — so the markup drew as `▎ …` **and the second
/// badge stopped being extracted at all**.
///
/// The top-level form above is exercised by three tests; this is the level none of them reached, and
/// the measurement that caught it needed both containers: `<details>` shows the `▎` gutter, while
/// the quoted form loses its badge while drawing no konoma code gutter whatsoever.
///
/// The `<details>` case keeps the real README shape (`<img>` on its own line, so `split_block_parts`
/// leaves `    </a>` fragments behind); the quoted case puts the `<img>` inside the `<a>` line so
/// nothing is left over, because a *quoted* leftover fragment still draws as markup for an unrelated,
/// pre-existing reason — see `banner_quoted_multiline_still_leaks_its_leftover_fragments`. Both
/// shapes lost a badge before the fix, which is what this asserts.
#[test]
fn e2e_md_banner_nested_in_a_container_draws_no_code_gutter() {
    for (label, body) in [
        (
            "details",
            concat!(
                "<details open>\n",
                "<summary>Badges</summary>\n",
                "\n",
                "<p align=\"center\">\n",
                "    <a href=\"https://crates.io/crates/demo\">\n",
                "      <img src=\"https://img.example/v.svg\" alt=\"crates.io badge\">\n",
                "    </a>\n",
                "    <br>\n",
                "    <a href=\"LICENSE-MIT\">\n",
                "      <img src=\"https://img.example/mit.svg\" alt=\"MIT badge\">\n",
                "    </a>\n",
                "</p>\n",
                "\n",
                "</details>\n",
                "\nOrdinary prose after the banner.\n",
            ),
        ),
        (
            "blockquote",
            concat!(
                "> <p align=\"center\">\n",
                ">     <a href=\"https://crates.io/crates/demo\"><img src=\"https://img.example/v.svg\" alt=\"crates.io badge\"></a>\n",
                ">     <br>\n",
                ">     <a href=\"LICENSE-MIT\"><img src=\"https://img.example/mit.svg\" alt=\"MIT badge\"></a>\n",
                "> </p>\n",
                "\nOrdinary prose after the banner.\n",
            ),
        ),
    ] {
        let (s, dir) = md_preview(cfg_code_bg_none(), "banner_nested_no_gutter", body);
        s.dont_see("");
        s.dont_see("</a>");
        // Both badges survived the nesting — the second one is the one that used to vanish.
        s.see("crates.io badge");
        s.see("MIT badge");
        s.see("Ordinary prose after the banner.");
        assert!(
            !s.screen().contains("```"),
            "{label}: 生のフェンスが漏れている"
        );
        std::fs::remove_dir_all(&dir).ok();
    }
}

/// Root cause (container-relative indentation, 2026-08 — reported by a user on the released build):
/// CommonMark measures a block's indentation from the container it sits in, so a fence indented 4
/// columns under `1. ` is a real fence, while a paragraph indented 6 columns under `   4. ` is not
/// code at all. The write-back scanner measured absolute columns and got both backwards, tripping
/// the document-wide count guard — `y c` answered "cannot copy code block" for *every* block in the
/// file. Both shapes are ordinary prose (a numbered install guide; the Apache license text), and 26
/// of 2,182 real `.md` files in the crate registry hit one of them.
///
/// Drives real keystrokes through a document containing both, plus an unrelated top-level fence that
/// used to be collaterally refused, and checks each block copies **its own** content.
#[test]
fn e2e_md_code_block_in_a_list_item_is_copyable() {
    let dir = sandbox("md_list_indented_fence");
    seed_files(&dir);
    std::fs::write(
        dir.join("snip.md"),
        // 1. a fence indented 4 columns inside an ordered item (a real fence)
        // 2. a 6-column continuation paragraph under `   4. ` (not code)
        // 3. an unrelated top-level fence
        "## Install\n\n1. Fork it:\n\n    ```sh\n    git clone x\n    ```\n\n\
         2. Build it.\n\n\
            4. Redistribution. You may reproduce and distribute copies of the\n\n\
         \x20     (b) You must cause any modified files to carry prominent notices\n\n\
         ```rust\nfn top_level() {}\n```\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("snip.md");
    s.enter();

    // 1st focusable code block: the one indented inside the ordered item. Its content comes back
    // flush-left (the item's content column is stripped, exactly as the screen shows it).
    s.tab();
    assert!(
        s.app.md_focused_code(),
        "1番目=リスト項目内のフェンスにフォーカス"
    );
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("git clone x"),
        "リスト項目内のフェンスがコピーできる(以前は文書全体が拒否された)"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "コピー通知が出る");

    // 2nd: the unrelated top-level fence — the continuation paragraph in between is not a code
    // block, so it must not consume an ordinal or shift this one.
    s.tab();
    assert!(s.app.md_focused_code(), "2番目=トップレベルのフェンス");
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("fn top_level() {}"),
        "ライセンス文体の字下げ段落をコードと数えず、序数がずれない"
    );

    std::fs::remove_dir_all(&dir).ok();
}

/// The same class of bug, but proven the other way around: an indented block that comes *before*
/// several real fences in the document must not throw off every later block's ordinal.
#[test]
fn e2e_md_indented_code_block_before_fences_keeps_ordinals_correct() {
    let dir = sandbox("md_indented_before_fences");
    seed_files(&dir);
    std::fs::write(
        dir.join("snip.md"),
        "Para.\n\n    first indented\n\nMid.\n\n```\nsecond fenced\n```\n\n```\nthird fenced\n```\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("snip.md");
    s.enter();

    s.tab(); // 1st: indented
    assert_eq!(s.app.focused_code_text().as_deref(), Some("first indented"),);
    s.tab(); // 2nd: fenced
    assert_eq!(s.app.focused_code_text().as_deref(), Some("second fenced"));
    s.tab(); // 3rd: fenced
    assert_eq!(s.app.focused_code_text().as_deref(), Some("third fenced"));

    std::fs::remove_dir_all(&dir).ok();
}

/// Root cause (heading/thematic-break/setext gate, 2026-08): an indented code block right after a
/// heading, with no blank line between them, used to go unrecognized by the write-back scanner (the
/// renderer draws it fine — only a paragraph gates a following indented block, and a heading isn't
/// one). The mismatch cancelled `y c` for the *whole document*, so an unrelated real fence right
/// after it (`npm test`, below) was collaterally refused too. Drives real keys through both blocks.
#[test]
fn e2e_md_indented_code_after_heading_no_blank_keeps_fence_copyable() {
    let dir = sandbox("md_heading_then_indented");
    seed_files(&dir);
    std::fs::write(
        dir.join("snip.md"),
        "## Usage\n    npm install foo\n\n```bash\nnpm test\n```\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("snip.md");
    s.enter();
    s.see("Usage");

    // 1st focusable item: the indented block right under the heading (no blank line).
    s.tab();
    assert!(
        s.app.md_focused_code(),
        "1番目=見出し直後(空行なし)の字下げコードにフォーカス"
    );
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("npm install foo"),
        "見出し直後の字下げコードの内容が取得できる(以前は文書全体のコピーが拒否されていた)"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "字下げ側のコピー通知が出る");

    // 2nd focusable item: the real fence right after it — must not be collaterally refused.
    s.tab();
    assert!(s.app.md_focused_code(), "2番目=フェンスコードにフォーカス");
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("npm test"),
        "無関係な実フェンスまで巻き添えで拒否されない"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "フェンス側のコピー通知も出る");

    std::fs::remove_dir_all(&dir).ok();
}

/// Root cause (container-closed fences, 2026-08): a fence is closed by a delimiter line **or** by the
/// end of the container it was opened in. The mask gating the block splitters knew only the first
/// rule, so a fence in a list item whose "closing" line carries trailing text — `` ``` ([#2642](…)) ``,
/// nix's CHANGELOG.md line 110 — was read as running to the end of the *document*, and every
/// construct below it was skipped as "inside code". Drives real keys, and asserts on the drawn
/// screen: the GFM table below renders with borders instead of one line of raw pipes, and the fence
/// itself is still a copyable code block (its own extent unchanged).
#[test]
fn e2e_md_fence_closed_by_its_list_item_still_renders_the_table_below() {
    let dir = sandbox("md_container_closed_fence");
    seed_files(&dir);
    std::fs::write(
        dir.join("snip.md"),
        "- Removed `Eq`, for example:\n\n  ```\n  let h = 1;\n  ``` ([#2642](https://example.com/pull/2642))\n\n\
         Implemented I/O safety:\n\n| Original Type | New Type |\n| ------------- | -------- |\n| AsRawFd | AsFd |\n\n\
         ```sh\nafter\n```\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("snip.md");
    s.enter();

    // The table below the fence is drawn by konoma's own table renderer (borders), not collapsed by
    // tui-markdown into `| Original Type | New Type | | ---…` on one line.
    // `┬` only ever comes from konoma's table renderer (the preview frame itself draws `┌`/`│`).
    s.see("");
    s.see("│ Original Type │ New Type │");
    s.see("│ AsRawFd");
    s.dont_see("| Original Type | New Type | |");

    // The fence's own extent is unchanged: it is still the 1st focusable code block, and the real
    // fence after the table is still the 2nd (the count guard covers the whole document, so a
    // disagreement here would refuse `y c` everywhere in the file).
    s.tab();
    assert!(
        s.app.md_focused_code(),
        "1番目=リスト項目内のコードブロック"
    );
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("let h = 1;\n``` ([#2642](https://example.com/pull/2642))\n"),
        "末尾テキスト付きの行は閉じないので、その行もブロックの中身のまま"
    );
    s.tab();
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("after"),
        "表の後ろの実フェンスが2番目として拾える"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "コピーが拒否されない");

    std::fs::remove_dir_all(&dir).ok();
}

/// The other axis the same gate was blind to: an **indented** code block (4+ columns), whose contents
/// are a literal transcript. A GFM table or a `> [!NOTE]` written inside one used to be carved out and
/// drawn as a real table / callout, tearing the code block apart. Runs under the user's real
/// `code_bg = "none"`, where a code block is only identifiable by its `▎` gutter.
#[test]
fn e2e_md_indented_code_block_keeps_table_and_alert_literal() {
    let (s, dir) = md_preview(
        cfg_code_bg_none(),
        "md_indented_literal",
        "Example table markup:\n\n    | a | b |\n    |---|---|\n    | 1 | 2 |\n\nExample alert markup:\n\n    > [!NOTE]\n    > body\n",
    );
    // Literal, verbatim — not carved into a bordered table or a colored callout box. `┬` is unique
    // to the table renderer and `▌` to the alert box (the preview frame draws neither); `▎` is the
    // code-block gutter, the only mark of a code block when `code_bg = "none"`.
    s.see("| a | b |");
    s.see("> [!NOTE]");
    s.dont_see("");
    s.dont_see("");
    s.see("");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_md_wrapped_focus_follows_offscreen_item() {
    // When wrapped: moving with Tab to a link after a paragraph taller than the screen makes preview_scroll follow.
    // When not wrapped: the paragraph collapses to 1 line and all fits on screen = no scroll needed (confirm the behavior differs both ways).
    let dir = sandbox("md_wrap_focus");
    seed_files(&dir);
    // A long paragraph (1 logical line) that wraps to ~34 display rows at width 88 + a link/task after it, apart.
    let para = "lorem ".repeat(500); // 3000 chars ≈ ~34 display rows at width 88 > viewport 22
    std::fs::write(
        dir.join("torture.md"),
        format!("{para}\n\n[ZZLINK](./notes.txt)\n\n- [ ] ZZTASK\n"),
    )
    .unwrap();
    let root = canon(&dir);

    // --- wrap=true (default): scrolls to follow ---
    let mut on = Sim::new(&root);
    on.select("torture.md");
    on.enter();
    assert!(on.app.cfg.ui.wrap, "前提: 既定は折返しON");
    assert_eq!(on.app.tab.preview_scroll, 0, "初期スクロール 0");
    on.dont_see("ZZLINK"); // the paragraph occupies the screen = the link is off-screen
    on.tab(); // focus the link past the paragraph (~34 display rows in)
    assert!(
        on.app.tab.preview_scroll > 5,
        "折返しでフォーカスに追従してスクロール: scroll={}",
        on.app.tab.preview_scroll
    );
    on.see("ZZLINK"); // after following, it's on screen

    // --- wrap=false: the paragraph is 1 line = everything fits on screen, so no scroll ---
    let mut cfg = Config::default();
    cfg.ui.wrap = false;
    let mut off = Sim::with_config(&root, cfg);
    off.select("torture.md");
    off.enter();
    off.see("ZZLINK"); // paragraph is 1 line = the link is on screen from the start
    off.tab();
    assert_eq!(
        off.app.tab.preview_scroll, 0,
        "非折返しは追従スクロール不要(scroll 0 のまま)"
    );
    off.see("ZZLINK");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// File operations: copy/cut → paste, batch rename, visual selection
// (op_base_dir = the directory at the cursor. Inside it while a directory is selected / its parent for a file)
// =============================================================================

#[test]
fn e2e_file_copy_then_paste_duplicates() {
    // Space→c copies the file at the cursor, then moving the cursor to the destination directory and
    // Space→p duplicates it (the original remains). The paste target = op_base_dir (inside it while a directory is selected).
    let dir = sandbox("copy_paste");
    std::fs::write(dir.join("orig.txt"), "hello copy\n").unwrap();
    std::fs::create_dir_all(dir.join("dest")).unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);

    s.select("orig.txt"); // cursor on the copy target (op_targets = the file at the cursor)
    s.key(' ');
    s.key('c'); // Space→c = FileCopy
    s.select("dest"); // the paste-destination directory (is_dir, so op_base_dir = dest itself)
    s.key(' ');
    s.key('p'); // Space→p = FilePaste

    assert!(
        dir.join("dest/orig.txt").exists(),
        "コピーがディレクトリ内に複製される"
    );
    assert!(dir.join("orig.txt").exists(), "コピーは元を残す");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_file_copy_then_paste_runs_in_background() {
    // Route the same operation as the synchronous version above through **the actual background path** (attaching a runner).
    // Space→c copies the directory, move the cursor to the paste target, then Space→p. Right after
    // paste, it's still running (shows up in the busy indicator), and only reflects once the worker's result arrives.
    let dir = sandbox("copy_paste_async");
    std::fs::create_dir_all(dir.join("pkg/inner")).unwrap();
    std::fs::write(dir.join("pkg/one.txt"), "1\n").unwrap();
    std::fs::write(dir.join("pkg/inner/two.txt"), "2\n").unwrap();
    std::fs::create_dir_all(dir.join("dest")).unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir).with_async_file_ops();

    s.select("pkg");
    s.key(' ');
    s.key('c'); // Space→c = FileCopy
    s.select("dest");
    s.key(' ');
    s.key('p'); // Space→p = FilePaste (dispatched to the worker)

    assert!(
        s.app.busy_jobs().contains(&crate::i18n::Msg::BusyFileOp),
        "貼付直後はバックグラウンド実行中(busy インジケーターに出る)"
    );

    s.drain_file_ops();

    assert!(
        dir.join("dest/pkg/one.txt").is_file() && dir.join("dest/pkg/inner/two.txt").is_file(),
        "ディレクトリごとコピーされている"
    );
    assert!(dir.join("pkg/one.txt").is_file(), "コピー元は残る");
    assert!(
        s.app
            .tab
            .entries
            .iter()
            .any(|e| e.path == dir.join("dest/pkg")),
        "貼付先が展開され、コピーがツリーに現れる"
    );
    s.see("pkg");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_file_cut_then_paste_moves() {
    // Space→x cuts, then Space→p in another directory → the original disappears and it's created at the destination (a move).
    let dir = sandbox("cut_paste");
    std::fs::write(dir.join("orig.txt"), "hello move\n").unwrap();
    std::fs::create_dir_all(dir.join("dest")).unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);

    s.select("orig.txt");
    s.key(' ');
    s.key('x'); // Space→x = FileCut
    s.select("dest");
    s.key(' ');
    s.key('p'); // Space→p = FilePaste (cut is move)

    assert!(dir.join("dest/orig.txt").exists(), "移動先に出来る");
    assert!(!dir.join("orig.txt").exists(), "移動元は消える");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_batch_rename_numbers_selection_via_preview() {
    // Multi-select (V×2) → Space→r for batch rename. A {n} template gives sequential numbers; going
    // through the preview dialog and applying with y → sequentially renamed (the extension is kept automatically).
    let dir = sandbox("batch_rename");
    std::fs::write(dir.join("alpha.log"), "a\n").unwrap();
    std::fs::write(dir.join("beta.log"), "b\n").unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);

    // V=ToggleSelect: selects the cursor and moves down by one. Twice selects the first 2 files.
    s.key('V');
    s.key('V');
    assert!(s.app.has_selection(), "複数選択が入っている");

    s.key(' ');
    s.key('r'); // Space→r. Since there's a selection, start_batch_rename runs (a template-entry dialog).
    assert!(s.app.is_dialog(), "一括リネームの入力ダイアログが開く");
    s.keys("renamed-{n}"); // {n} = the sequence-number token
    s.enter(); // build_rename_plan → transitions to the preview (old → new)
    assert!(s.app.dialog_is_preview(), "リネームプレビューが挟まる");
    s.see("renamed-1");
    s.key('y'); // the preview's y = apply (dialog_preview_apply)

    assert!(dir.join("renamed-1.log").exists(), "1件目が連番リネーム");
    assert!(dir.join("renamed-2.log").exists(), "2件目が連番リネーム");
    assert!(!dir.join("alpha.log").exists(), "旧名は消える");
    assert!(!dir.join("beta.log").exists(), "旧名は消える");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_tree_visual_selection_grows_then_commits() {
    // v = start range selection → j grows the range (marked_count increases) → v = commit, folding it into selection.
    let dir = sandbox("tree_visual");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));

    s.key('v'); // EnterVisual (Surface=Visual)
    assert!(s.app.is_visual(), "v で範囲選択開始");
    assert_eq!(s.app.marked_count(), 1, "開始直後はアンカー1行");
    s.key('j'); // in the Visual surface, Down → the range grows to [anchor, anchor+1]
    assert_eq!(s.app.marked_count(), 2, "j で範囲が2行に広がる");
    s.key('v'); // in the Visual surface, v=VisualCommit → commit the range into selection and exit
    assert!(!s.app.is_visual(), "v で範囲確定して visual を抜ける");
    assert!(s.app.has_selection(), "確定した集合が残る");
    assert_eq!(s.app.marked_count(), 2, "確定後は選択2件");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Config reflection: details columns / path style cycling / csv_rainbow / busy indicator
// =============================================================================

#[test]
fn e2e_details_columns_show_size_and_modified() {
    // With ui.details=["size","modified"], each row's right edge shows size/modified columns.
    // The expected value is computed via the same quick_meta as the renderer (no hardcoded time/size).
    let dir = sandbox("details");
    seed_files(&dir);
    let dir = canon(&dir);
    let notes = dir.join("notes.txt");
    let meta = crate::fileops::quick_meta(&notes).expect("quick_meta");
    let size_cell = crate::fileops::human_size(meta.size);
    let mod_cell = crate::fileops::format_epoch_short(meta.mtime.expect("mtime"));

    // Control: without details, the date column doesn't appear (a false-positive guard).
    let plain = Sim::new(&dir);
    plain.dont_see(&mod_cell);

    let mut cfg = Config::default();
    cfg.ui.details = vec!["size".into(), "modified".into()];
    let s = Sim::with_config(&dir, cfg);
    s.see(&size_cell); // e.g. "23 B" (appears in the right-aligned column)
    s.see(&mod_cell); // e.g. "2026-07-08 14:23" (the 16-column modified-time column)
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_returning_from_a_preview_drops_a_row_whose_file_vanished() {
    // The reported bug, at the seam where it became visible: while one file is previewed another
    // file in the same directory disappears (an agent, or an external `rm`), and `q` brings back a
    // listing that still carries its row — with a freshly computed, entirely fictional `0 B` in the
    // size column, because returning to the tree re-derived the detail cells but not the rows.
    //
    // Deliberately watcher-free (and git-free): this covers the *self-repair* half of the fix, so
    // it holds no matter which fs events were delivered or missed. The watcher half — never
    // skipping a create/remove/rename — is covered in `main.rs` and `app::tests`.
    let dir = sandbox("stale_row_on_return");
    std::fs::write(dir.join("kept.txt"), "kept-kept-kept\n").unwrap();
    std::fs::write(dir.join("gone.txt"), "gone\n").unwrap();
    let dir = canon(&dir);

    let mut cfg = Config::default();
    cfg.ui.details = vec!["size".into()];
    let mut s = Sim::with_config(&dir, cfg);
    s.see("gone.txt");
    s.see("5 B"); // gone.txt's real size, before it disappears
    s.dont_see("0 B");

    s.select("kept.txt");
    s.enter(); // full-screen preview
    assert!(s.app.tab.preview_path.is_some(), "プレビューに入っている");

    // Disappears while the preview is up — exactly what an agent deleting a build artifact does.
    std::fs::remove_file(dir.join("gone.txt")).unwrap();

    s.key('q'); // back to the tree
    assert!(
        !s.app
            .tab
            .entries
            .iter()
            .any(|e| e.path.ends_with("gone.txt")),
        "消えたファイルの行が entries に残っている(ツリーを再検証していない)"
    );
    s.dont_see("gone.txt");
    // The specific lie: a row for a file that no longer exists, sized 0 B.
    s.dont_see("0 B");
    s.see("kept.txt"); // …without throwing away the rows that are still real
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_returning_from_a_preview_keeps_the_active_filter() {
    // Guards the risk the seam re-verification introduces: `rebuild_tree` resets `entries` to the
    // whole tree, so re-deriving the listing on the way back from a preview must go through the
    // filter-preserving path. Getting this wrong shows the query in the title while listing
    // everything under it — the same inconsistency a previous fix (tab switching) had to repair.
    let dir = sandbox("filter_survives_preview");
    seed_files(&dir);
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);

    s.key('/');
    s.keys("csv");
    s.enter(); // confirm the filter (the list stays filtered)
    let filtered = s.app.tab.entries.len();
    assert!(filtered >= 1, "csv に一致する行がある");
    s.see("data.csv");
    s.dont_see("readme.md");

    s.select("data.csv");
    s.enter(); // preview
    assert!(s.app.tab.preview_path.is_some(), "プレビューに入っている");
    s.key('q'); // back to the tree — re-derives the listing

    assert_eq!(
        s.app.tab.entries.len(),
        filtered,
        "プレビューから戻っても絞り込みが効いたまま(全件に戻らない)"
    );
    s.see("data.csv");
    s.dont_see("readme.md");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_path_style_cycles() {
    // p=CyclePathStyle cycles Relative→Home→Full→Relative. The path: display at the top follows along too.
    use crate::app::PathStyle;
    let dir = sandbox("path_style");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));

    assert_eq!(s.app.path_style, PathStyle::Relative, "既定は relative");
    s.see("path:rel"); // status's path label (assumes the English locale = same as existing tests)
    s.key('p');
    assert_eq!(s.app.path_style, PathStyle::Home, "p で ~ 表示へ");
    s.see("path:~");
    s.key('p');
    assert_eq!(s.app.path_style, PathStyle::Full, "p で絶対へ");
    s.see("path:abs");
    s.key('p');
    assert_eq!(s.app.path_style, PathStyle::Relative, "巡回して先頭へ戻る");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_csv_table_renders_and_navigates_without_rainbow() {
    // Even with ui.csv_rainbow=false, cell content still shows as an aligned table (color can't be verified — glyphs only).
    // hjkl/0/$ move the cell cursor (a different angle than the existing test — also checks jumping to a column edge).
    let dir = sandbox("csv_norainbow");
    std::fs::write(
        dir.join("fruit.csv"),
        "fruit,color\napple,red\nkiwi,green\n",
    )
    .unwrap();
    let mut cfg = Config::default();
    cfg.ui.csv_rainbow = false;
    let mut s = Sim::with_config(&canon(&dir), cfg);

    s.select("fruit.csv");
    s.enter();
    s.see("TABLE");
    s.see("fruit"); // header (glyphs show even in monochrome)
    s.see("apple"); // data cell
    assert_eq!(s.app.table_cursor(), (0, 0), "初期カーソルは先頭セル");
    s.key('l'); // to the right column
    assert_eq!(s.app.table_cursor(), (0, 1), "l で1列右へ");
    s.key('l'); // clamps at the last column (ncols=2)
    assert_eq!(s.app.table_cursor(), (0, 1), "末列でクランプ");
    s.key('0'); // to the first column
    assert_eq!(s.app.table_cursor(), (0, 0), "0 で先頭列");
    s.key('$'); // to the last column
    assert_eq!(s.app.table_cursor(), (0, 1), "$ で末列");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_tabbar_overflow_markers_keep_active_visible() {
    // Opening many tabs and overflowing at a narrow terminal width (90) shows the overflow markers
    // ‹n / n›, while the active tab stays visible (visible_range's active-centered visible window).
    let dir = sandbox("tabbar_overflow");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));

    s.keys("tttttttt"); // t×8 → 9 tabs total (each t switches to the new tab)
    assert_eq!(s.app.tab_count(), 9);
    s.key('5'); // to tab 5 (index 4 = center). Hidden tabs appear on both sides.
    assert_eq!(s.app.active_tab_index(), 4);

    s.see(""); // left overflow marker (s>0)
    s.see(""); // right overflow marker (e<n)
    let label = s.app.tab_label(4);
    s.see(&format!("5:{label}")); // the active tab's chip stays visible
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_busy_indicator_absent_when_idle() {
    // When idle (no job running in the background), the busy display doesn't show. Disabling via config makes it always inactive.
    // Note: since the synchronous harness can't actually trigger an async job, only the idle invariant is verified (an honest scope).
    let dir = sandbox("busy_idle");
    seed_files(&dir);
    let dir = canon(&dir);

    let s = Sim::new(&dir);
    assert!(s.app.busy_jobs().is_empty(), "アイドルでジョブ無し");
    assert!(
        !s.app.busy_indicator_active(),
        "アイドルでインジケーター非アクティブ"
    );
    s.dont_see("loading media");
    s.dont_see("git scan");
    s.dont_see("loading images");

    let mut cfg = Config::default();
    cfg.ui.busy_indicator = false;
    let s2 = Sim::with_config(&dir, cfg);
    assert!(
        !s2.app.busy_indicator_active(),
        "busy_indicator=false は常に非アクティブ"
    );
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Stale-listing indicator (the context bar's STALE chip)
// =============================================================================

/// English wording for the chip regardless of the machine's locale (`lang = "auto"` follows the OS,
/// and these tests match on "STALE").
fn cfg_en() -> Config {
    let mut cfg = Config::default();
    cfg.ui.lang = "en".into();
    cfg
}

/// A failed tree rebuild deliberately keeps the last good listing, so a stale tree looks exactly
/// like a healthy one — files that no longer exist sit there with nothing to doubt them by. The
/// flash that announces the moment it happens is wiped by the very next keypress, which is the
/// whole reason the *level* needs a chip: it has to still be there ten keys later.
///
/// Pins all three halves — it appears, it survives keypresses (while the flash does not), and it
/// disappears once a rebuild succeeds — plus the negative (nothing at all while healthy), so an
/// "always show the chip" implementation cannot pass either.
#[test]
fn e2e_stale_listing_chip_stays_up_until_the_listing_recovers() {
    let dir = sandbox("stale_chip_level");
    seed_files(&dir);
    let root = canon(&dir);
    let mut s = Sim::with_config(&root, cfg_en());

    // Healthy: no chip at all.
    s.see("notes.txt");
    s.dont_see("STALE");
    assert!(!s.app.tree_stale());

    // The root disappears out from under us (an agent removing a directory konoma happens to be
    // showing). This is the fs-watch path the run loop drives, which cannot propagate a failure.
    std::fs::remove_dir_all(&root).unwrap();
    s.app.refresh_fs_watched(false, &[]);
    s.draw();
    s.see("STALE");
    s.see_styled(
        "STALE",
        |st| st.bg == Some(ratatui::style::Color::Yellow),
        "警告色(黄)の背景",
    );
    // The listing itself is still there — which is exactly why the chip has to be.
    s.see("notes.txt");
    // …and the one-shot announcement fired for this frame.
    let flash = s.app.flash.clone().expect("立ち上がりで一度は伝える");
    assert!(flash.contains("out of date"), "{flash}");

    // Keypresses clear the flash. The chip must outlive every one of them — that is the difference
    // this test exists to pin.
    for k in ['j', 'k', 'g', 'G', 'j', 'j', 'k'] {
        s.key(k);
        assert_eq!(s.app.flash, None, "flash はキー入力で消える");
        s.see("STALE");
    }

    // Recovery takes it down again (nothing keeps announcing "stale" over a healthy listing).
    std::fs::create_dir_all(&root).unwrap();
    std::fs::write(root.join("back.txt"), b"y").unwrap();
    s.app.refresh_fs_watched(false, &[]);
    s.draw();
    assert!(!s.app.tree_stale());
    s.dont_see("STALE");
    s.see("back.txt");
    std::fs::remove_dir_all(&root).ok();
}

/// The chip states something about **the listing you are looking at**, so a broken tab sitting in
/// the background must not put it on screen while a healthy tab is in front. Every tab activation
/// ends in a rebuild (`load_active` → `refresh_fs_after_tab_switch`), which is what keeps the
/// app-wide flag describing the visible tab; this pins that, in both directions and repeatedly.
#[test]
fn e2e_stale_listing_chip_follows_the_tab_you_are_looking_at() {
    let dir = sandbox("stale_chip_tabs");
    let base = canon(&dir);
    for sub in ["alpha", "beta"] {
        std::fs::create_dir_all(base.join(sub)).unwrap();
    }
    std::fs::write(base.join("alpha/in-alpha.txt"), b"a\n").unwrap();
    std::fs::write(base.join("beta/in-beta.txt"), b"b\n").unwrap();
    let mut s = Sim::with_config(&base, cfg_en());

    // tab2 = alpha, tab3 = beta (Ctrl-t on a directory roots a new tab there).
    s.select("alpha");
    s.ctrl('t');
    s.key('['); // back to tab1 to pick the other directory
    s.select("beta");
    s.ctrl('t');
    assert_eq!(s.app.tab_count(), 3);
    s.see("in-beta.txt");
    s.dont_see("STALE");

    // tab2's root vanishes while tab3 is in front. Nothing is on screen about it yet — correct,
    // that tab is not the one being shown.
    std::fs::remove_dir_all(base.join("alpha")).unwrap();
    s.dont_see("STALE");

    // Switching to it says so, and keeps saying so.
    s.key('[');
    assert_eq!(s.app.active_tab_index(), 1);
    s.see("STALE");
    s.see("in-alpha.txt"); // the stale listing it is warning about

    // Switching to the healthy tab takes it back down…
    s.key(']');
    assert_eq!(s.app.active_tab_index(), 2);
    s.dont_see("STALE");
    // …and coming back brings it up again (not a one-shot that got used up).
    s.key('[');
    s.see("STALE");
    std::fs::remove_dir_all(&base).ok();
}

/// The context bar is drawn in every mode, so the chip must be too: a stale listing does not stop
/// being stale because you are looking at a file. (The Git view is covered by
/// `stale_listing_chip_shows_in_the_git_view` in `app/tests.rs`, which needs a real repository.)
#[test]
fn e2e_stale_listing_chip_shows_in_tree_and_preview() {
    let dir = sandbox("stale_chip_modes");
    seed_files(&dir);
    let root = canon(&dir);
    let mut s = Sim::with_config(&root, cfg_en());

    // Open a preview first, so the root can then be pulled out from under an open file.
    s.select("notes.txt");
    s.enter();
    assert!(matches!(s.app.tab.mode, Mode::Preview));
    s.dont_see("STALE");

    std::fs::remove_dir_all(&root).unwrap();
    s.app.refresh_fs_watched(false, &[]);
    s.draw();
    assert!(matches!(s.app.tab.mode, Mode::Preview), "プレビューのまま");
    s.see("STALE");

    // Back to the tree: still stale, still shown (the same flag, a shared context bar).
    s.key('q');
    assert!(matches!(s.app.tab.mode, Mode::Tree));
    s.see("STALE");
    std::fs::remove_dir_all(&root).ok();
}

/// The tab bar shares its row with the context bar and is sized by subtracting
/// `status::context_width` (v0.9.0 shipped a bug where it planned across the full width and got
/// painted under the right-hand side). One more chip widens that subtraction, so this pins that the
/// tab bar still behaves with the chip up: the active tab and the overflow markers stay visible,
/// and the chip sits entirely inside the reserved right-hand region rather than on top of a tab.
#[test]
fn e2e_stale_listing_chip_does_not_break_the_tab_bar() {
    let dir = sandbox("stale_chip_tabbar");
    seed_files(&dir);
    let root = canon(&dir);
    let mut s = Sim::with_config(&root, cfg_en());

    s.keys("tttttttt"); // 9 tabs → the bar overflows at width 90
    assert_eq!(s.app.tab_count(), 9);
    s.key('5');
    assert_eq!(s.app.active_tab_index(), 4);

    std::fs::remove_dir_all(&root).unwrap();
    s.app.refresh_fs_watched(false, &[]);
    s.draw();

    s.see("STALE");
    // The overflow window still works: markers on both sides, active tab still visible.
    s.see("");
    s.see("");
    let label = s.app.tab_label(4);
    s.see(&format!("5:{label}"));

    // Both live on the top row, and the chip is inside the region the tab bar was told to keep
    // clear — if the tab bar had planned across the full width, a tab chip would be sitting where
    // the context bar then painted over it.
    let width = s.term.backend().buffer().area.width as usize;
    let ctx = crate::ui::status::context_width(&s.app) as usize;
    let (chip_row, chip_col) = s.find_text("STALE").expect("チップが描かれている");
    assert_eq!(chip_row, 0, "チップは上段(タブバーと同じ行)");
    assert!(
        chip_col as usize >= width - ctx,
        "チップがタブバー側にはみ出している (col={chip_col}, 予約境界={})",
        width - ctx
    );
    let (tab_row, tab_col) = s.find_text(&format!("5:{label}")).expect("アクティブタブ");
    assert_eq!(tab_row, 0);
    assert!(
        (tab_col as usize) < width - ctx,
        "タブがコンテキスト側に食い込んでいる (col={tab_col})"
    );
    std::fs::remove_dir_all(&root).ok();
}

// =============================================================================
// Paste-jump (P): clipboard path/GitHub link → jumps to that location
// =============================================================================

#[test]
fn e2e_paste_jump_local_path_with_line() {
    // Passing a local relative path + `:line` jumps to that location, opens the preview, and scrolls
    // to the given line (the top marker goes off-screen). A nonexistent path flashes a notice, no crash.
    let dir = sandbox("paste_jump_local");
    seed_files(&dir);
    std::fs::create_dir_all(dir.join("src")).unwrap();
    // Plain text (windowed, no highlight-pending wait = deterministic in the synchronous harness). The line-jump path is the same as Code's.
    let mut body = String::new();
    for i in 1..=60 {
        if i == 1 {
            body.push_str("LINE_ONE_MARKER\n");
        } else if i == 30 {
            body.push_str("LINE_THIRTY_MARKER\n");
        } else {
            body.push_str(&format!("plain line {i}\n"));
        }
    }
    std::fs::write(dir.join("src/deep.txt"), body).unwrap();
    let dir = canon(&dir);

    let mut s = Sim::new(&dir);
    // Paste-jump from the tree (no preview open). paste_jump_from is a clipboard-independent pub entry point.
    s.app.paste_jump_from("src/deep.txt:30");
    s.draw();
    assert_eq!(s.app.tab.mode, Mode::Preview, "ファイルはプレビューで開く");
    assert!(
        s.app
            .tab
            .preview_path
            .as_deref()
            .map(|p| p.ends_with("src/deep.txt"))
            .unwrap_or(false),
        "deep.txt がプレビュー対象: {:?}",
        s.app.tab.preview_path
    );
    s.see("LINE_THIRTY_MARKER"); // scrolled down to line 30
    s.dont_see("LINE_ONE_MARKER"); // the top is off-screen = it actually scrolled

    // A path that isn't found flashes a notice, without crashing (principle #3).
    s.app.paste_jump_from("does/not/exist.rs");
    s.draw();
    assert!(
        s.app
            .flash
            .as_deref()
            .map(|f| f.contains("exist.rs"))
            .unwrap_or(false),
        "不在パスは flash 通知: {:?}",
        s.app.flash
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_paste_jump_github_url_switches_root() {
    // With konoma opened at a subdirectory of the repo, pasting a GitHub blob URL pointing at a
    // different directory within that same repo: ① drops the URL's owner/repo/blob/ref and resolves
    // the file relative to the local repo (workdir); ② since the target is outside the current root, switches root to the repo (workdir) and reveals + previews.
    let dir = sandbox("paste_jump_url");
    std::fs::create_dir_all(dir.join("src")).unwrap();
    std::fs::create_dir_all(dir.join("docs")).unwrap();
    std::fs::write(dir.join("src/lib.rs"), "pub fn hi() {}\n").unwrap();
    std::fs::write(dir.join("docs/guide.md"), "# GUIDE_MARKER\n\nbody\n").unwrap();
    let run = |args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(&dir)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    run(&["init", "-q", "."]);
    run(&["config", "user.email", "t@t"]);
    run(&["config", "user.name", "t"]);
    run(&["add", "-A"]);
    run(&["commit", "-q", "-m", "init"]);

    let repo = canon(&dir);
    let subdir = repo.join("src");
    // Launch konoma at the subdirectory (src). docs/guide.md is outside this root.
    let mut s = Sim::new(&subdir);
    assert_eq!(s.app.tab.root, subdir, "起動 root は src サブディレクトリ");

    // Even when the repository name (owner/name) differs from the local one, it can open as long as the trailing suffix docs/guide.md actually exists.
    s.app
        .paste_jump_from("https://github.com/some-owner/some-name/blob/main/docs/guide.md");
    s.draw();

    assert_eq!(s.app.tab.root, repo, "root が repo(workdir)へ切替わる");
    assert_eq!(s.app.tab.mode, Mode::Preview, "guide.md をプレビューで開く");
    assert!(
        s.app
            .tab
            .preview_path
            .as_deref()
            .map(|p| p.ends_with("docs/guide.md"))
            .unwrap_or(false),
        "guide.md がプレビュー対象: {:?}",
        s.app.tab.preview_path
    );
    s.see("GUIDE_MARKER");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_paste_jump_from_visual_exits_visual_and_lands_in_preview() {
    // Bug regression: pressing `P` (PasteJump) while in the tree's Visual (range selection) used to
    // transition to a full-screen preview without the selection being committed/discarded. Since
    // `is_visual()` doesn't check `tab.mode` and only checks whether `tab.visual_anchor` exists,
    // `surface()` kept returning Visual even while the preview was displayed, causing an accident
    // where keys (`j` etc.) flowed into the tree's Visual map even though what's visible is the preview.
    let dir = sandbox("paste_jump_visual");
    std::fs::write(dir.join("target.txt"), "PASTE_JUMP_VISUAL_BODY\n").unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);

    // Via a real key: v enters the tree's Visual.
    s.select("target.txt");
    s.key('v');
    assert!(s.app.is_visual(), "v でツリー Visual に入る");
    assert_eq!(s.app.surface(), crate::keymap::Surface::Visual);

    // Dispatch P (PasteJump). Why call `crate::dispatch_action` directly instead of a real key:
    // this test doesn't want to touch the real machine's system clipboard (its content is
    // environment-dependent = the test becomes flaky. paste_jump()'s own resolution/crash resilience
    // is separately verified elsewhere, e.g. e2e_paste_jump_local_path_with_line, via the
    // clipboard-independent `paste_jump_from`). dispatch_action IS main.rs's central dispatch, and
    // passes exactly the same Action/Surface arguments that the real key `P` resolves to, so as a
    // path it's identical to going through a real key. As long as main.rs's dispatch routes through
    // `commit_visual_if_needed` (as with the other Space→ actions), Visual is guaranteed to end
    // regardless of the clipboard's content/whether reading it succeeds (commit_visual_if_needed
    // runs **before** calling app.paste_jump()).
    let sfc = s.app.surface();
    assert_eq!(sfc, crate::keymap::Surface::Visual);
    crate::dispatch_action(&mut s.app, crate::keymap::Action::PasteJump, sfc)
        .expect("dispatch_action(PasteJump)");
    s.draw();
    assert!(
        !s.app.is_visual(),
        "PasteJump を dispatch した後は(ジャンプの成否によらず)ツリーの Visual 選択が終わっている"
    );

    // Independently verify enter_preview's own defense: paste_jump_from is a clipboard-independent
    // pub entry point (the same style as the existing paste-jump e2e). Deliberately re-enter Visual
    // to simulate a hypothetical caller that doesn't go through commit_visual_if_needed, and confirm
    // the invariant enter_preview itself must guarantee (entering the full-screen preview = the
    // tree's range selection has ended).
    s.app.enter_visual();
    assert!(s.app.is_visual(), "enter_visual で Visual に入り直す");
    s.app.paste_jump_from("target.txt");
    s.draw();
    assert!(
        !s.app.is_visual(),
        "enter_preview は(呼び出し元が畳み忘れていても)ツリーの Visual を必ず終える"
    );
    assert_ne!(
        s.app.surface(),
        crate::keymap::Surface::Visual,
        "surface() はもう Visual を返さない"
    );
    assert_eq!(s.app.tab.mode, Mode::Preview, "プレビューに入っている");
    s.see("PASTE_JUMP_VISUAL_BODY");

    // Proof that keys aren't flowing into the tree's Visual map while viewing the preview: j doesn't
    // move the tree's cursor/Visual range (at the time of the bug, j was extending the tree's Visual range).
    let cursor_before = s.app.tab.selected;
    s.key('j');
    assert_eq!(
        s.app.tab.selected, cursor_before,
        "j はもうツリーの Visual カーソルを動かさない(プレビュー中)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Open Markdown links in a new tab (Ctrl-t)
// =============================================================================

#[test]
fn e2e_md_link_ctrl_t_opens_in_new_tab() {
    // Focusing a local Markdown link and pressing Ctrl-t opens the target in a **separate tab**.
    // The original document's tab remains, reachable with [. Enter (same tab) works as before.
    let dir = sandbox("md_link_newtab");
    seed_files(&dir);
    std::fs::write(dir.join("target.txt"), "TARGET_FILE_MARKER\nmore lines\n").unwrap();
    std::fs::write(
        dir.join("doc.md"),
        "# DOC_HEADING\n\nsee [go](./target.txt) here.\n",
    )
    .unwrap();
    let dir = canon(&dir);

    let mut s = Sim::new(&dir);
    s.select("doc.md");
    s.enter();
    s.see("DOC_HEADING");
    assert_eq!(s.app.tab_count(), 1, "最初は1タブ");

    s.tab(); // focus the link "go"
    assert!(s.app.focused_item().is_some(), "リンクにフォーカス");

    s.ctrl('t'); // open in a new tab
    assert_eq!(s.app.tab_count(), 2, "別タブが増える");
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert!(
        s.app
            .tab
            .preview_path
            .as_deref()
            .map(|p| p.ends_with("target.txt"))
            .unwrap_or(false),
        "新タブは target.txt を開く: {:?}",
        s.app.tab.preview_path
    );
    s.see("TARGET_FILE_MARKER");

    // The original document's tab remains: going back with [ (tab_prev) still shows doc.md.
    s.key('[');
    assert!(
        s.app
            .tab
            .preview_path
            .as_deref()
            .map(|p| p.ends_with("doc.md"))
            .unwrap_or(false),
        "元タブは doc.md のまま: {:?}",
        s.app.tab.preview_path
    );
    s.see("DOC_HEADING");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Reload from disk the tab that became active on a tab switch
// =============================================================================

#[test]
fn e2e_tab_switch_reloads_tree_from_disk() {
    // File watching only sees the active root, so an external change to a backgrounded tab stays as a stale snapshot.
    // Switching to that tab triggers a refresh_fs reload, and files created in the background appear in the tree.
    let dir = sandbox("tab_switch_reload");
    seed_files(&dir);
    let dir = canon(&dir);

    let mut s = Sim::new(&dir);
    s.dont_see("ZZ_NEW_FILE.txt");

    // Create a new tab (same root) and make it active (tab 1 goes to the background).
    s.key('t');
    assert_eq!(s.app.tab_count(), 2, "タブが2つ");

    // While tab 1 is in the background, create a file externally.
    std::fs::write(dir.join("ZZ_NEW_FILE.txt"), "hi\n").unwrap();

    // Return to tab 1 ([ = tab_prev). The switch-time reload should reveal the new file.
    s.key('[');
    assert_eq!(s.app.active_tab_index(), 0, "タブ1 に戻る");
    assert!(
        s.app
            .tab
            .entries
            .iter()
            .any(|e| e.path.ends_with("ZZ_NEW_FILE.txt")),
        "タブ切替でツリーが再読み込みされ新規ファイルが現れる"
    );
    s.see("ZZ_NEW_FILE.txt");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_preview_file_paging_ctrl_n_p() {
    // Ctrl-n/Ctrl-p = paging through files while previewing: tree display order, directories skipped,
    // wraps at the ends, also traverses files inside an expanded subfolder, the tree cursor follows.
    let dir = unique_tmp("konoma_e2e_file_paging");
    let _ = std::fs::remove_dir_all(&dir);
    std::fs::create_dir_all(dir.join("mid")).unwrap();
    std::fs::write(dir.join("a.txt"), b"alpha content\n").unwrap();
    std::fs::write(dir.join("z.txt"), b"zulu content\n").unwrap();
    std::fs::write(dir.join("mid/inner.txt"), b"inner content\n").unwrap();
    let mut s = Sim::new(&dir);

    // Expand mid (with the dirs_first default, tree order is [mid, mid/inner.txt, a.txt, z.txt]).
    s.enter();
    s.key('j'); // inner.txt
    s.key('j'); // a.txt
    s.enter(); // preview a.txt
    assert!(matches!(s.app.tab.mode, Mode::Preview));
    s.see("alpha content");

    // Next file: a.txt → z.txt.
    s.ctrl('n');
    s.see("zulu content");
    assert!(s.app.tab.preview_path.as_ref().unwrap().ends_with("z.txt"));

    // Next again: wraps from the end, and skips mid (a directory) at the top to reach inner.txt.
    s.ctrl('n');
    s.see("inner content");
    assert!(s
        .app
        .tab
        .preview_path
        .as_ref()
        .unwrap()
        .ends_with("inner.txt"));

    // Previous file: before inner.txt is mid (skipped) → wraps to z.txt.
    s.ctrl('p');
    s.see("zulu content");
    assert!(s.app.tab.preview_path.as_ref().unwrap().ends_with("z.txt"));

    // The tree cursor followed along (q returns to being on z.txt).
    s.key('q');
    assert!(matches!(s.app.tab.mode, Mode::Tree));
    assert!(s.app.tab.entries[s.app.tab.selected]
        .path
        .ends_with("z.txt"));
    std::fs::remove_dir_all(&dir).ok();
}

/// `[ui] restore_tabs`: the tab session is saved per launch directory, and on "restart" the tab
/// configuration (count, active tab, cursor, preview surface) is restored as-is.
#[test]
fn e2e_session_restore_reopens_previous_tabs() {
    let dir = sandbox("session_restore");
    std::fs::write(dir.join("a.txt"), "alpha body\n").unwrap();
    std::fs::write(dir.join("b.txt"), "beta body\n").unwrap();
    let base = unique_tmp("konoma_e2e_session_restore_base");
    let _ = std::fs::remove_dir_all(&base);

    // Session 1: preview a.txt → t for a new tab (tree) → cursor on b.txt.
    let mut s = Sim::new(&dir);
    s.app
        .attach_session_store(crate::session::SessionStore::with_base(base.clone(), &dir));
    s.select("a.txt");
    s.press(KeyCode::Enter, KeyModifiers::NONE);
    assert!(matches!(s.app.tab.mode, Mode::Preview));
    s.key('t');
    s.select("b.txt");
    s.app.save_session(); // equivalent to the on-quit save (main calls this after run)
    drop(s);

    // "Restart": attach the store to a fresh Sim of the same launch dir and restore (same order as main's launch).
    let mut s2 = Sim::new(&dir);
    assert_eq!(s2.app.tab_count(), 1, "復元前は素の1タブ");
    s2.app
        .attach_session_store(crate::session::SessionStore::with_base(base.clone(), &dir));
    s2.app.restore_session();
    s2.draw();
    assert_eq!(s2.app.tab_count(), 2, "タブ数を復元");
    assert_eq!(s2.app.active_tab_index(), 1, "アクティブタブを復元");
    assert!(matches!(s2.app.tab.mode, Mode::Tree));
    assert!(
        s2.app.tab.entries[s2.app.tab.selected]
            .path
            .ends_with("b.txt"),
        "ツリーカーソルを復元"
    );
    s2.see("a.txt"); // the tab bar shows the preview tab's (tab 1's) label
                     // Switching to tab 1 = a.txt's preview reopens (the content shows on screen).
    s2.key('[');
    assert!(matches!(s2.app.tab.mode, Mode::Preview));
    s2.see("alpha body");

    std::fs::remove_dir_all(&dir).ok();
    std::fs::remove_dir_all(&base).ok();
}

// =============================================================================
// Additional E2E batch (2026-07-18): fills in a large number of thin/missing coverage areas.
// Routes the whole source→real-render path through the Sim harness, verified via clipboard-independent pub accessors.
// =============================================================================

/// Write `file` with `body` in a fresh sandbox `dir_name`, open it (tree → preview) under `cfg`,
/// and return the Sim plus the sandbox dir. Like `md_preview` but for any single file.
fn text_preview(cfg: Config, dir_name: &str, file: &str, body: &str) -> (Sim, std::path::PathBuf) {
    let dir = sandbox(dir_name);
    std::fs::write(dir.join(file), body).unwrap();
    let root = canon(&dir);
    let mut s = Sim::with_config(&root, cfg);
    s.select(file);
    s.enter();
    (s, dir)
}

// ---- In-preview text search (`/` starts it, n/N move, case-insensitive, no match, Esc, CJK) ----

#[test]
fn e2e_search_opens_input_shows_prompt_and_query() {
    let dir = sandbox("search_open_input");
    std::fs::write(dir.join("f.txt"), "needle at top\nplain line\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    assert!(s.app.is_windowed());
    s.key('/');
    assert!(s.app.is_searching());
    assert_eq!(s.app.search_input(), Some(""));
    s.keys("needle");
    assert_eq!(s.app.search_input(), Some("needle"));
    s.see("/needle");
    s.enter();
    assert!(!s.app.is_searching());
    assert_eq!(s.app.preview_search_query(), Some("needle"));
    assert_eq!(s.app.search_status(), Some((1, 1)));
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_backspace_edits_query() {
    let dir = sandbox("search_backspace");
    std::fs::write(dir.join("f.txt"), "needle here\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("need");
    assert_eq!(s.app.search_input(), Some("need"));
    s.press(KeyCode::Backspace, KeyModifiers::NONE);
    assert_eq!(s.app.search_input(), Some("nee"));
    s.keys("dle");
    assert_eq!(s.app.search_input(), Some("needle"));
    s.esc();
    assert!(!s.app.is_searching());
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_esc_during_input_cancels_no_search() {
    let dir = sandbox("search_esc_input");
    std::fs::write(dir.join("f.txt"), "needle here\nmore\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("needle");
    assert!(s.app.is_searching());
    s.esc();
    assert!(!s.app.is_searching());
    assert_eq!(s.app.preview_search_query(), None);
    assert_eq!(s.app.search_status(), None);
    assert_eq!(s.app.tab.mode, Mode::Preview);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_commit_populates_status_and_jumps_to_first() {
    let dir = sandbox("search_commit");
    let body = "x MARK a\nfiller one\nfiller two\ny MARK b\nfiller three\nz MARK c\n";
    std::fs::write(dir.join("f.txt"), body).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("MARK");
    s.enter();
    assert!(!s.app.is_searching());
    assert_eq!(s.app.preview_search_query(), Some("MARK"));
    assert_eq!(s.app.search_status(), Some((1, 3)));
    s.see("x MARK a");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_next_prev_cycle_with_wrap() {
    let dir = sandbox("search_cycle");
    let body = "a MARK a\nfiller\nfiller\nb MARK b\nfiller\nc MARK c\n";
    std::fs::write(dir.join("f.txt"), body).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("MARK");
    s.enter();
    assert_eq!(s.app.search_status(), Some((1, 3)));
    s.key('n');
    assert_eq!(s.app.search_status(), Some((2, 3)));
    s.key('n');
    assert_eq!(s.app.search_status(), Some((3, 3)));
    s.key('n');
    assert_eq!(s.app.search_status(), Some((1, 3)));
    s.key('N');
    assert_eq!(s.app.search_status(), Some((3, 3)));
    s.key('N');
    assert_eq!(s.app.search_status(), Some((2, 3)));
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_scroll_follows_current_match() {
    let dir = sandbox("search_follow");
    let mut body = String::new();
    for i in 0..300 {
        if i == 10 || i == 150 || i == 290 {
            body.push_str(&format!("MARK line {i}\n"));
        } else {
            body.push_str(&format!("filler {i}\n"));
        }
    }
    std::fs::write(dir.join("big.txt"), &body).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("big.txt");
    s.enter();
    s.key('/');
    s.keys("MARK");
    s.enter();
    s.see("MARK line 10");
    s.dont_see("MARK line 150");
    s.key('n');
    s.see("MARK line 150");
    s.dont_see("MARK line 10");
    s.key('n');
    s.see("MARK line 290");
    s.key('n');
    s.see("MARK line 10");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_case_insensitive_matches() {
    let dir = sandbox("search_ci");
    std::fs::write(
        dir.join("f.txt"),
        "hello line\nWORLD marker here\nbye line\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("world");
    s.enter();
    assert_eq!(s.app.search_status(), Some((1, 1)));
    s.see("WORLD marker here");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_no_match_flash_and_empty_status() {
    let dir = sandbox("search_no_match");
    std::fs::write(dir.join("f.txt"), "alpha\nbeta\ngamma\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("zzzznope");
    s.enter();
    assert_eq!(s.app.preview_search_query(), Some("zzzznope"));
    assert_eq!(s.app.search_status(), None);
    assert!(s
        .app
        .flash
        .as_deref()
        .is_some_and(|m| m.contains("no match")));
    s.key('n');
    assert_eq!(s.app.search_status(), None);
    s.key('N');
    assert_eq!(s.app.search_status(), None);
    s.esc();
    assert_eq!(s.app.preview_search_query(), None);
    assert_eq!(s.app.tab.mode, Mode::Preview);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_esc_clears_then_second_esc_returns_to_tree() {
    let dir = sandbox("search_esc_two");
    std::fs::write(dir.join("f.txt"), "alpha\nbeta marker\ngamma\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("marker");
    s.enter();
    assert_eq!(s.app.search_status(), Some((1, 1)));
    s.esc();
    assert_eq!(s.app.preview_search_query(), None);
    assert_eq!(s.app.tab.mode, Mode::Preview);
    s.esc();
    assert_eq!(s.app.tab.mode, Mode::Tree);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_counts_multiple_occurrences_per_line() {
    let dir = sandbox("search_multi_occ");
    std::fs::write(
        dir.join("f.txt"),
        "one TARGET two TARGET end\nfiller line\nTARGET solo\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("TARGET");
    s.enter();
    assert_eq!(s.app.search_status(), Some((1, 3)));
    s.key('n');
    assert_eq!(s.app.search_status(), Some((2, 3)));
    s.see("one TARGET two TARGET end");
    s.key('n');
    assert_eq!(s.app.search_status(), Some((3, 3)));
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_works_in_code_preview() {
    let dir = sandbox("search_code");
    std::fs::write(
        dir.join("code.rs"),
        "fn alpha() {}\nfn beta() {}\nlet x = 1;\n",
    )
    .unwrap();
    // Turn off highlighting to avoid depending on execution order: with a cold grammar, the preview
    // shows "loading…" and this search test's result would change depending on "whether another
    // test had warmed .rs" (it failed when run alone). Syntax coloring isn't needed to verify search, so run with a setting that deterministically shows the body.
    let mut cfg = Config::default();
    cfg.ui.syntax_highlight = false;
    let mut s = Sim::with_config(&canon(&dir), cfg);
    s.select("code.rs");
    s.enter();
    assert!(s.app.is_windowed());
    s.key('/');
    s.keys("fn");
    s.enter();
    assert_eq!(s.app.search_status(), Some((1, 2)));
    s.see("fn alpha()");
    std::fs::remove_dir_all(&dir).ok();
}

/// `/` works on decorated Markdown too (it used to be rejected as "code/text only").
/// Since the search target is the decorated lines, matches can be highlighted without switching to `R`'s raw source.
#[test]
fn e2e_search_works_on_decorated_markdown_preview() {
    let (mut s, dir) = md_preview(
        Config::default(),
        "doc",
        "# Title\n\nsome needle text here\n",
    );
    assert!(!s.app.is_windowed(), "装飾 md は窓読みではない");
    s.key('/');
    assert!(s.app.is_searching(), "装飾 md でも検索入力に入れる");
    s.keys("needle");
    s.enter();
    assert_eq!(s.app.preview_search_query(), Some("needle"));
    assert_eq!(s.app.search_status(), Some((1, 1)));
    assert!(!s.app.is_raw_source(), "raw ソースに切替わらない");
    std::fs::remove_dir_all(&dir).ok();
}

/// A surface without a search model (images, etc.) still rejects it and notifies via flash, as before.
#[test]
fn e2e_search_rejected_on_media_preview() {
    let Some(fixture) = sample_path_or_skip("sample.svg") else {
        return;
    };
    let dir = sandbox("search_media");
    std::fs::copy(&fixture, dir.join("pic.svg")).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("pic.svg");
    s.enter();
    s.key('/');
    assert!(!s.app.is_searching(), "画像プレビューでは検索に入らない");
    assert_eq!(s.app.preview_search_query(), None);
    assert!(s.app.flash.is_some(), "拒否理由を flash で知らせる");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_empty_query_enter_is_noop() {
    let dir = sandbox("search_empty");
    std::fs::write(dir.join("f.txt"), "alpha\nbeta\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    assert!(s.app.is_searching());
    s.enter();
    assert!(!s.app.is_searching());
    assert_eq!(s.app.preview_search_query(), None);
    assert_eq!(s.app.search_status(), None);
    assert_eq!(s.app.tab.mode, Mode::Preview);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_cjk_query_matches_without_crash() {
    let dir = sandbox("search_cjk");
    std::fs::write(
        dir.join("jp.txt"),
        "リンゴ みかん\nぶどう みかん\nバナナ だけ\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("jp.txt");
    s.enter();
    assert!(s.app.is_windowed());
    s.key('/');
    s.keys("みかん");
    s.enter();
    // NOTE: the on-screen text isn't asserted here — the TestBackend renders each full-width CJK
    // glyph as a cell plus a blank continuation cell, so `screen()` inserts spaces between the
    // characters. The functional path (match found, `n` cycles) is what matters and is CJK-safe.
    assert_eq!(s.app.search_status(), Some((1, 2)));
    s.key('n');
    assert_eq!(s.app.search_status(), Some((2, 2)));
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_search_second_query_replaces_first() {
    let dir = sandbox("search_replace");
    std::fs::write(
        dir.join("f.txt"),
        "MARK aaa\nfiller\nMARK bbb\nSPOT ccc\nfiller\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("f.txt");
    s.enter();
    s.key('/');
    s.keys("MARK");
    s.enter();
    assert_eq!(s.app.search_status(), Some((1, 2)));
    assert_eq!(s.app.preview_search_query(), Some("MARK"));
    s.key('/');
    assert!(s.app.is_searching());
    s.keys("SPOT");
    s.enter();
    assert_eq!(s.app.preview_search_query(), Some("SPOT"));
    assert_eq!(s.app.search_status(), Some((1, 1)));
    s.see("SPOT ccc");
    std::fs::remove_dir_all(&dir).ok();
}

// ---- Windowed page/half-page scroll: real keystrokes (both key schemes) ----
// Regression: after paging *down* through a windowed (Code/Text) preview, the caret sits on the
// last visible row, so the very next page/half-page *up* moved the caret back to `top` — still
// on screen — and `follow_cursor` saw nothing to do: the window didn't move (the first upward
// page/half-page after a downward one was silently swallowed). Fixed by `App::windowed_page_scroll`,
// which moves the window directly and carries the caret along. These drive real key input through
// `handle_key` (not the App method directly) for both the vim (Ctrl-f/b/d/u) and less (f/b/d/u)
// key schemes, and cross-check the two schemes land on identical window positions.

/// 400 lines "line001".."line400", one per line — enough room to page through repeatedly without
/// hitting the top/bottom clamp.
fn paging_body() -> String {
    let mut body = String::new();
    for i in 1..=400 {
        body.push_str(&format!("line{i:03}\n"));
    }
    body
}

#[test]
fn e2e_preview_page_vim_scheme_first_page_up_after_paging_down_moves_the_window() {
    let (mut s, dir) = text_preview(Config::default(), "wpage_vim", "big.txt", &paging_body());
    assert!(s.app.is_windowed());
    let page = (s.app.tab.preview_viewport as i64 - 1).max(1);

    s.ctrl('f');
    s.ctrl('f');
    s.ctrl('f');
    let top_after_downs = s.app.preview_top_line();
    assert_eq!(top_after_downs as i64, 3 * page, "Ctrl-f×3 後の窓位置");

    s.ctrl('b');
    assert_ne!(
        s.app.preview_top_line(),
        top_after_downs,
        "vim: Ctrl-f の直後、最初の Ctrl-b で窓が動かない(バグの再現)"
    );
    assert_eq!(
        s.app.preview_top_line() as i64,
        2 * page,
        "Ctrl-b は 1 ページ分だけ戻るはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_preview_half_page_vim_scheme_first_half_up_after_half_paging_down_moves_the_window() {
    let (mut s, dir) = text_preview(Config::default(), "whalf_vim", "big.txt", &paging_body());
    assert!(s.app.is_windowed());
    let half = (s.app.tab.preview_viewport as i64 / 2).max(1);

    s.ctrl('d');
    s.ctrl('d');
    let top_after_downs = s.app.preview_top_line();
    assert_eq!(top_after_downs as i64, 2 * half, "Ctrl-d×2 後の窓位置");

    s.ctrl('u');
    assert_ne!(
        s.app.preview_top_line(),
        top_after_downs,
        "vim: Ctrl-d の直後、最初の Ctrl-u で窓が動かない(バグの再現)"
    );
    assert_eq!(
        s.app.preview_top_line() as i64,
        half,
        "Ctrl-u は半ページ分だけ戻るはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_preview_page_less_scheme_first_page_up_after_paging_down_moves_the_window() {
    let mut cfg = Config::default();
    cfg.ui.keys = "less".into();
    let (mut s, dir) = text_preview(cfg, "wpage_less", "big.txt", &paging_body());
    assert!(s.app.is_windowed());
    let page = (s.app.tab.preview_viewport as i64 - 1).max(1);

    // less: Space also pages down (in addition to `f`) — exercise both.
    s.key(' ');
    s.key('f');
    s.key('f');
    let top_after_downs = s.app.preview_top_line();
    assert_eq!(top_after_downs as i64, 3 * page, "Space,f,f 後の窓位置");

    s.key('b');
    assert_ne!(
        s.app.preview_top_line(),
        top_after_downs,
        "less: f の直後、最初の b で窓が動かない(バグの再現)"
    );
    assert_eq!(
        s.app.preview_top_line() as i64,
        2 * page,
        "b は 1 ページ分だけ戻るはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_preview_half_page_less_scheme_first_half_up_after_half_paging_down_moves_the_window() {
    let mut cfg = Config::default();
    cfg.ui.keys = "less".into();
    let (mut s, dir) = text_preview(cfg, "whalf_less", "big.txt", &paging_body());
    assert!(s.app.is_windowed());
    let half = (s.app.tab.preview_viewport as i64 / 2).max(1);

    s.key('d');
    s.key('d');
    let top_after_downs = s.app.preview_top_line();
    assert_eq!(top_after_downs as i64, 2 * half, "d×2 後の窓位置");

    s.key('u');
    assert_ne!(
        s.app.preview_top_line(),
        top_after_downs,
        "less: d の直後、最初の u で窓が動かない(バグの再現)"
    );
    assert_eq!(
        s.app.preview_top_line() as i64,
        half,
        "u は半ページ分だけ戻るはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_preview_paging_vim_and_less_schemes_land_on_identical_positions() {
    // Both schemes route the same keys to the same `Action::Navigate(Motion::Page*/Half*)`, so a
    // vim session (Ctrl-f/b/d/u) and a less session (f/b/d/u) run through the identical scenario
    // must end up with the identical window/caret state.
    fn drive(scheme: &str) -> (usize, usize, usize, usize) {
        let mut cfg = Config::default();
        cfg.ui.keys = scheme.into();
        let (mut s, dir) = text_preview(
            cfg,
            &format!("wpage_agree_{scheme}"),
            "big.txt",
            &paging_body(),
        );
        assert!(s.app.is_windowed(), "{scheme}");
        if scheme == "less" {
            s.key('f');
            s.key('f');
            s.key('f');
        } else {
            s.ctrl('f');
            s.ctrl('f');
            s.ctrl('f');
        }
        let down3 = s.app.preview_top_line();
        if scheme == "less" {
            s.key('b');
        } else {
            s.ctrl('b');
        }
        let up1 = s.app.preview_top_line();
        if scheme == "less" {
            s.key('d');
            s.key('d');
        } else {
            s.ctrl('d');
            s.ctrl('d');
        }
        let halfdown2 = s.app.preview_top_line();
        if scheme == "less" {
            s.key('u');
        } else {
            s.ctrl('u');
        }
        let halfup1 = s.app.preview_top_line();
        std::fs::remove_dir_all(&dir).ok();
        (down3, up1, halfdown2, halfup1)
    }

    let vim = drive("vim");
    let less = drive("less");
    assert_eq!(
        vim, less,
        "vim/less スキームで同じキー操作の結果が一致しない"
    );
    // Also pin the regression fix down in this combined scenario: none of the "first up after
    // paging down" steps left the window where it was.
    assert_ne!(vim.0, vim.1);
    assert_ne!(vim.2, vim.3);
}

// ---- preview 2D caret + visual selection (v/V) + Y reference ----
// The caret line is verified via selection_ref_string() (@path#L{n}), the caret column via preview_selection().

#[test]
fn e2e_visual_block_caret_drawn_and_moves() {
    use ratatui::style::Modifier;
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_caret_cell",
        "caret.txt",
        "alpha\nbeta\ngamma\n",
    );
    assert!(s.app.is_windowed());
    s.see("alpha");
    let reversed = |s: &Sim| -> Vec<(usize, usize)> {
        let buf = s.term.backend().buffer();
        let w = buf.area.width as usize;
        buf.content()
            .iter()
            .enumerate()
            .filter(|(_, c)| c.modifier.contains(Modifier::REVERSED))
            .map(|(i, _)| (i % w, i / w))
            .collect()
    };
    let before = reversed(&s);
    assert_eq!(before.len(), 1, "one block-caret cell: {before:?}");
    s.key('j');
    let after = reversed(&s);
    assert_eq!(after.len(), 1);
    assert_eq!(after[0].0, before[0].0);
    assert_eq!(after[0].1, before[0].1 + 1);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_caret_line_tracks_jk_and_gg() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_line_nav",
        "nav.txt",
        "alpha\nbeta\ngamma\n",
    );
    let refstr = |s: &Sim| s.app.selection_ref_string().unwrap();
    assert_eq!(refstr(&s), "@nav.txt#L1");
    s.key('j');
    assert_eq!(refstr(&s), "@nav.txt#L2");
    s.key('j');
    assert_eq!(refstr(&s), "@nav.txt#L3");
    s.key('j');
    assert_eq!(refstr(&s), "@nav.txt#L3");
    s.key('k');
    assert_eq!(refstr(&s), "@nav.txt#L2");
    s.key('G');
    assert_eq!(refstr(&s), "@nav.txt#L3");
    s.key('g');
    assert_eq!(refstr(&s), "@nav.txt#L1");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_v_enters_charwise_state() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_v_char",
        "v.txt",
        "alpha\nbeta\ngamma\n",
    );
    assert!(!s.app.is_preview_visual());
    s.key('v');
    assert!(s.app.is_preview_visual());
    assert!(!s.app.preview_visual_linewise());
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 0)
        }
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_bigv_enters_linewise_state() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_V_line",
        "vv.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('V');
    assert!(s.app.is_preview_visual());
    assert!(s.app.preview_visual_linewise());
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Line { lo: 0, hi: 0 }
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_charwise_range_grows_with_l() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_grow_l",
        "gl.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('v');
    s.key('l');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 1)
        }
    );
    s.key('l');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 2)
        }
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_charwise_range_grows_across_lines() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_grow_j",
        "gj.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('v');
    s.key('j');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (1, 0)
        }
    );
    s.key('j');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (2, 0)
        }
    );
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@gj.txt#L1-3")
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_linewise_range_grows_with_j() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_lw_grow",
        "lw.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('V');
    s.key('j');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Line { lo: 0, hi: 1 }
    );
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@lw.txt#L1-2")
    );
    s.key('j');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Line { lo: 0, hi: 2 }
    );
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@lw.txt#L1-3")
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_col_home_and_end_bound_selection() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_home_end",
        "he.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('v');
    s.key('$');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 4)
        }
    );
    s.key('0');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 0)
        }
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_gg_extend_linewise_to_ends() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_gg_line",
        "gg.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('V');
    s.key('G');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Line { lo: 0, hi: 2 }
    );
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@gg.txt#L1-3")
    );
    s.key('g');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Line { lo: 0, hi: 0 }
    );
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@gg.txt#L1"));
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_cancel_with_esc() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_esc",
        "esc.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('v');
    s.key('l');
    assert!(s.app.is_preview_visual());
    s.esc();
    assert!(!s.app.is_preview_visual());
    assert_eq!(s.app.tab.mode, Mode::Preview);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_cancel_with_v_bigv_q_toggles() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_toggles",
        "tg.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('v');
    assert!(s.app.is_preview_visual());
    s.key('v');
    assert!(!s.app.is_preview_visual());
    assert_eq!(s.app.tab.mode, Mode::Preview);
    s.key('v');
    assert!(s.app.is_preview_visual());
    s.key('V');
    assert!(!s.app.is_preview_visual());
    assert_eq!(s.app.tab.mode, Mode::Preview);
    s.key('v');
    assert!(s.app.is_preview_visual());
    s.key('q');
    assert!(!s.app.is_preview_visual());
    assert_eq!(s.app.tab.mode, Mode::Preview);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_ref_caret_line_and_charwise_span() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_ref_char",
        "rc.txt",
        "alpha\nbeta\ngamma\n",
    );
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@rc.txt#L1"));
    s.key('j');
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@rc.txt#L2"));
    s.key('v');
    s.key('j');
    assert!(matches!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char { .. }
    ));
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@rc.txt#L2-3")
    );
    s.key('Y');
    assert!(!s.app.is_preview_visual());
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_ref_linewise_span() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_ref_line",
        "rl.txt",
        "alpha\nbeta\ngamma\n",
    );
    s.key('V');
    s.key('j');
    s.key('j');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Line { lo: 0, hi: 2 }
    );
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@rl.txt#L1-3")
    );
    s.key('Y');
    assert!(!s.app.is_preview_visual());
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_selection_in_code_preview() {
    let mut cfg = Config::default();
    cfg.ui.syntax_highlight = false;
    let (mut s, dir) = text_preview(cfg, "visual_code", "code.rs", "fn one() {}\nfn two() {}\n");
    assert!(s.app.is_windowed());
    s.see("fn one");
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@code.rs#L1"));
    s.key('j');
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@code.rs#L2"));
    s.key('k');
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@code.rs#L1"));
    s.key('v');
    assert!(s.app.is_preview_visual() && !s.app.preview_visual_linewise());
    s.key('l');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 1)
        }
    );
    s.esc();
    assert!(!s.app.is_preview_visual());
    assert_eq!(s.app.tab.mode, Mode::Preview);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_selection_in_raw_markdown() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_rawmd",
        "doc.md",
        "# Heading\nalpha line\nbeta line\n",
    );
    assert!(!s.app.is_windowed());
    assert!(!s.app.is_md_raw());
    s.key('v');
    assert!(!s.app.is_preview_visual());
    s.key('R');
    assert!(s.app.is_windowed());
    assert!(s.app.is_md_raw() && s.app.is_raw_source());
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@doc.md#L1"));
    s.key('v');
    assert!(s.app.is_preview_visual());
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 0)
        }
    );
    s.key('j');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (1, 0)
        }
    );
    assert_eq!(
        s.app.selection_ref_string().as_deref(),
        Some("@doc.md#L1-2")
    );
    s.esc();
    assert!(!s.app.is_preview_visual());
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_visual_cjk_charwise_no_panic() {
    let (mut s, dir) = text_preview(
        Config::default(),
        "visual_cjk",
        "cjk.txt",
        "あいう\nかきくけ\n",
    );
    assert!(s.app.is_windowed());
    assert_eq!(s.app.selection_ref_string().as_deref(), Some("@cjk.txt#L1"));
    s.key('v');
    s.key('l');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 1)
        }
    );
    s.key('l');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 2)
        }
    );
    s.key('$');
    assert_eq!(
        s.app.preview_selection(),
        crate::app::PreviewSelection::Char {
            start: (0, 0),
            end: (0, 2)
        }
    );
    s.key('y');
    assert!(!s.app.is_preview_visual());
    std::fs::remove_dir_all(&dir).ok();
}

// ---- git: commit / stage/unstage / diff layout / log/graph/branches / copy / changed n/N ----
// All under #[cfg(feature = "git")]. Repository creation follows the existing seed_repo pattern.

#[cfg(feature = "git")]
fn seed_repo_history(dir: &std::path::Path) {
    let run = |args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(dir)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    run(&["init", "-q", "."]);
    run(&["config", "user.email", "t@t"]);
    run(&["config", "user.name", "t"]);
    std::fs::write(dir.join("a.rs"), "fn a() {}\n").unwrap();
    run(&["add", "-A"]);
    run(&["commit", "-q", "-m", "alphacommit"]);
    std::fs::write(dir.join("a.rs"), "fn a() { 1 }\n").unwrap();
    run(&["add", "-A"]);
    run(&["commit", "-q", "-m", "betacommit"]);
    std::fs::write(dir.join("a.rs"), "fn a() { 22 }\n").unwrap();
    run(&["add", "-A"]);
    run(&["commit", "-q", "-m", "gammacommit"]);
    run(&["branch", "feature-x"]);
}

#[cfg(feature = "git")]
fn staged_of(app: &App, name: &str) -> Option<bool> {
    app.git_view_entries()
        .iter()
        .find(|e| e.path.ends_with(name))
        .map(|e| e.staged)
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_stage_then_commit_appears_in_log() {
    let dir = sandbox("git_commit_flow");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    assert!(s.app.is_git_view());
    assert_eq!(staged_of(&s.app, "a.rs"), Some(false));
    s.key('s');
    assert_eq!(staged_of(&s.app, "a.rs"), Some(true));
    s.key('c');
    assert!(s.app.is_dialog());
    s.keys("ZZCOMMITSUBJECT");
    s.enter();
    let commits = crate::git::log(&s.app.tab.root, 200);
    assert!(
        commits.iter().any(|c| c.summary == "ZZCOMMITSUBJECT"),
        "commit in log: {:?}",
        commits
            .iter()
            .map(|c| c.summary.clone())
            .collect::<Vec<_>>()
    );
    assert!(s.app.is_git_view());
    assert_eq!(staged_of(&s.app, "a.rs"), None);
    assert!(staged_of(&s.app, "new.txt").is_some());
    s.key('l');
    assert!(s.app.is_git_log());
    s.see("ZZCOMMITSUBJECT");
    s.see("init");
    std::fs::remove_dir_all(&dir).ok();
}

// --- Background git writes through real key input (design principle #4) -----------------------

/// Install a `pre-commit` hook that parks until the test removes `$GIT_DIR/BLOCK`, so a commit's
/// duration is controlled by the test rather than by a timer.
///
/// The `max` bound is a safety net, not a timing assumption: it exists so that a regression which
/// puts the write back on the UI thread **fails** (the commit eventually lands, and the "no commit
/// yet" assertion below breaks) instead of hanging the suite forever.
#[cfg(feature = "git")]
fn install_blocking_pre_commit_hook(dir: &std::path::Path) {
    let hooks = dir.join(".git").join("hooks");
    std::fs::create_dir_all(&hooks).unwrap();
    let hook = hooks.join("pre-commit");
    std::fs::write(
        &hook,
        "#!/bin/sh\nn=0\nwhile [ -f \"$(git rev-parse --git-dir)/BLOCK\" ] && [ $n -lt 1000 ]; do\n  sleep 0.02\n  n=$((n+1))\ndone\nexit 0\n",
    )
    .unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&hook, std::fs::Permissions::from_mode(0o755)).unwrap();
    }
}

/// Stage and commit through the real key path with the background runner attached: `o` `s` `c`
/// <message> Enter, then the run loop's drain step. The keys are identical to the synchronous
/// version (`e2e_git_stage_then_commit_appears_in_log`) — moving the write off-thread must not
/// change what the user types or sees afterwards.
#[cfg(feature = "git")]
#[test]
fn e2e_git_stage_and_commit_through_the_background_runner() {
    let dir = sandbox("git_async_commit");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir)).with_async_git_ops();
    s.key('o');
    assert!(s.app.is_git_view());
    assert_eq!(staged_of(&s.app, "a.rs"), Some(false));

    s.key('s');
    assert!(s.app.git_op_running(), "ステージは裏で走る");
    s.drain_git_ops();
    assert_eq!(
        staged_of(&s.app, "a.rs"),
        Some(true),
        "適用後に staged になる"
    );

    s.key('c');
    assert!(s.app.is_dialog());
    s.keys("ASYNCCOMMITSUBJECT");
    s.enter();
    assert!(s.app.git_op_running(), "コミットも裏で走る");
    s.drain_git_ops();

    let commits = crate::git::log(&s.app.tab.root, 200);
    assert!(
        commits.iter().any(|c| c.summary == "ASYNCCOMMITSUBJECT"),
        "コミットが log に載る: {:?}",
        commits
            .iter()
            .map(|c| c.summary.clone())
            .collect::<Vec<_>>()
    );
    assert!(s.app.is_git_view(), "完了後も Git ビューに居る");
    assert_eq!(
        staged_of(&s.app, "a.rs"),
        None,
        "コミット済みは一覧から消える"
    );
    s.key('l');
    assert!(s.app.is_git_log());
    s.see("ASYNCCOMMITSUBJECT");
    std::fs::remove_dir_all(&dir).ok();
}

/// **The point of D3, verified structurally rather than by a clock.** While `git commit` is parked
/// inside a `pre-commit` hook, konoma must keep handling keys and drawing.
///
/// No wall-clock assertion is involved: the hook holds until this test deletes `BLOCK`, so
/// "`git log` still has only the seed commits" is a fact the test controls, not a race. The
/// combination of "the commit has demonstrably not finished" and "these keys were processed and
/// these frames rendered" is what proves the write is not on the UI thread. Before the fix,
/// `s.enter()` never returned until the hook gave up, so the first assertion below would fail.
#[cfg(feature = "git")]
#[test]
fn e2e_ui_keeps_responding_while_a_git_commit_is_blocked() {
    let dir = sandbox("git_blocked_commit");
    seed_repo(&dir);
    install_blocking_pre_commit_hook(&dir);
    let root = canon(&dir);
    let git_dir = root.join(".git");
    std::fs::write(git_dir.join("BLOCK"), b"").unwrap();

    // Stage outside the harness, so the commit is the *only* git op this test dispatches and the
    // "still running" assertion below is the first thing a regression trips over.
    crate::git::stage_all(&root).unwrap();
    let before = crate::git::log(&root, 200).len();
    let mut s = Sim::new(&root).with_async_git_ops();
    s.key('o');
    s.key('c');
    s.keys("BLOCKEDCOMMIT");
    s.enter(); // dispatches the commit; the hook is parked

    assert!(
        s.app.git_op_running(),
        "コミットは実行中(まだ完了していない)"
    );
    assert_eq!(
        crate::git::log(&root, 200).len(),
        before,
        "フックが握っている間はコミットが作られていない"
    );

    // Keys keep working, and every one of them redraws (Sim::press draws after each key, exactly
    // like the run loop) — so this also proves rendering isn't stuck behind the write.
    s.key('q'); // leave the Git view
    assert!(!s.app.is_git_view(), "git 実行中でも Git ビューを出られる");
    let start = s.app.tab.selected;
    for _ in 0..3 {
        s.key('j');
    }
    assert!(
        s.app.tab.selected > start,
        "git 実行中でもツリーのカーソルが動く"
    );
    s.key('k');
    s.see("a.rs"); // a frame really was produced
                   // Still blocked, and still nothing committed: the keys above did not secretly wait for git.
    assert!(s.app.git_op_running(), "ここまでキーを叩いてもまだ実行中");
    assert_eq!(
        crate::git::log(&root, 200).len(),
        before,
        "キー処理の間もコミットは作られていない"
    );

    // Release the hook and let the result land.
    std::fs::remove_file(git_dir.join("BLOCK")).unwrap();
    s.drain_git_ops();
    assert!(!s.app.git_op_running(), "適用で実行中フラグが解ける");
    let commits = crate::git::log(&root, 200);
    assert!(
        commits.iter().any(|c| c.summary == "BLOCKEDCOMMIT"),
        "解放後にコミットが完了する: {:?}",
        commits
            .iter()
            .map(|c| c.summary.clone())
            .collect::<Vec<_>>()
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `Q` while a git write is in flight must warn before quitting, even with `confirm_quit = false`:
/// leaving mid-`commit` orphans a `git` that is still holding `.git/index.lock`.
#[cfg(feature = "git")]
#[test]
fn e2e_quit_during_a_git_write_asks_for_confirmation() {
    let dir = sandbox("git_quit_during_write");
    seed_repo(&dir);
    install_blocking_pre_commit_hook(&dir);
    let root = canon(&dir);
    std::fs::write(root.join(".git").join("BLOCK"), b"").unwrap();

    let mut cfg = Config::default();
    cfg.ui.confirm_quit = false;
    crate::git::stage_all(&root).unwrap();
    let mut s = Sim::with_config(&root, cfg).with_async_git_ops();
    s.key('o');
    s.key('c');
    s.keys("WILLQUIT");
    s.enter();
    assert!(s.app.git_op_running(), "コミットは裏で実行中");

    s.key('Q');
    assert!(!s.quit, "確認を出すのでこの時点では終了しない");
    assert!(s.app.is_dialog(), "確認ダイアログが出る");
    s.see(crate::i18n::tr(
        s.app.lang,
        crate::i18n::Msg::QuitWhileGitOp,
    ));

    // The warning only warns: `y` still leaves.
    s.key('y');
    assert!(s.quit, "y なら終了できる(引き止めない)");

    std::fs::remove_file(root.join(".git").join("BLOCK")).ok();
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_stage_unstage_roundtrip() {
    let dir = sandbox("git_stage_unstage");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    assert_eq!(staged_of(&s.app, "a.rs"), Some(false));
    s.key('s');
    assert_eq!(staged_of(&s.app, "a.rs"), Some(true));
    s.key('u');
    assert_eq!(staged_of(&s.app, "a.rs"), Some(false));
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_stage_all_then_unstage_all() {
    let dir = sandbox("git_stage_all");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    assert!(!s.app.git_view_entries().is_empty());
    assert!(s.app.git_view_entries().iter().any(|e| !e.staged));
    s.key('S');
    assert!(s.app.git_view_entries().iter().all(|e| e.staged));
    s.key('U');
    assert!(s.app.git_view_entries().iter().all(|e| !e.staged));
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_diff_layout_toggle_cycles_in_preview() {
    let dir = sandbox("git_diff_layout");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("a.rs");
    s.key('d');
    assert!(s.app.is_git_diff_preview());
    s.see("diff");
    assert!(!s.app.diff_is_split(200));
    s.key('s');
    s.see("side-by-side");
    assert!(s.app.diff_is_split(1) && s.app.diff_is_split(200));
    s.key('s');
    s.see("diff: auto");
    assert!(!s.app.diff_is_split(50) && s.app.diff_is_split(200));
    s.key('s');
    s.see("diff: unified");
    assert!(!s.app.diff_is_split(200));
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// The windowed-diff perf fix: `render_gitdiff` must highlight only what's on screen.
//
// `windowed_range_only_highlights_visible_rows_{unified,side_by_side}` in gitdiff.rs prove
// `diff_lines_range`/`diff_lines_side_by_side_range` themselves are windowed — called directly, not
// through `render_gitdiff`/`ui::render`. That leaves the wiring unverified: nothing stops
// `render_gitdiff` from going back to passing `(0, diff.len())` (the whole document) while the
// windowed functions it calls stay correct in isolation — and the 16 tests that existed before this
// one all stayed green when that exact regression was tried (see this file's own history). These
// tests drive a real keystroke → `handle_key` → `ui::render` pass (the same path the run loop takes)
// against a diff of 2,400+ rows and assert the highlight counter — which `diff_line_to_line`/
// `render_half` bump on every actual `highlight_line_by_ext` call — stays in the viewport's order of
// magnitude, not the diff's.
// =============================================================================

/// A repo with one file whose working-tree diff is far larger than any screen: every one of
/// `n` lines is rewritten, so nothing folds into "unchanged" context — the diff is ~2n rows
/// (n Removed + n Added), all real output from an actual `git diff` invocation (not a synthetic
/// `Vec<DiffLine>` built in-process), so `App::git_diff_lines()` → `file_diff` → `render_gitdiff`'s
/// real wiring runs end to end.
#[cfg(feature = "git")]
fn seed_huge_diff_repo(dir: &std::path::Path) {
    let run = |args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(dir)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    run(&["init", "-q", "."]);
    run(&["config", "user.email", "t@t"]);
    run(&["config", "user.name", "t"]);
    let n = 1200;
    let original: String = (0..n).map(|i| format!("line {i}\n")).collect();
    std::fs::write(dir.join("big.txt"), &original).unwrap();
    run(&["add", "-A"]);
    run(&["commit", "-q", "-m", "init"]);
    // Rewrite every single line: nothing is left unchanged, so the working-tree diff has ~2n rows
    // (n Removed immediately followed by n Added), far larger than any terminal's screen.
    let changed: String = (0..n).map(|i| format!("line {i} changed\n")).collect();
    std::fs::write(dir.join("big.txt"), &changed).unwrap();
}

#[cfg(feature = "git")]
#[test]
fn e2e_gitdiff_unified_render_highlights_only_the_visible_rows() {
    let dir = sandbox("gitdiff_unified_windowed");
    seed_huge_diff_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("big.txt");
    s.key('d');
    assert!(s.app.is_git_diff_preview(), "d で big.txt の diff を開く");
    assert!(
        !s.app.diff_is_split(200),
        "既定は unified(diff.layout=\"unified\")"
    );

    // The diff itself must actually be far larger than the screen, or a small highlight count
    // would prove nothing (it could just be a small diff).
    let total_rows = s.app.git_diff_lines().len();
    assert!(
        total_rows > 2000,
        "全1200行を書き換えたので diff は2000行超のはず(実際 {total_rows})"
    );

    // `s.select`/`s.key('d')` above already triggered draws while the highlight counter was
    // running (it's already been counting since Sim::new's very first draw) — reset it and take
    // exactly one more render pass to measure, mirroring the unit tests in gitdiff.rs.
    crate::preview::gitdiff::reset_highlight_calls_for_test();
    s.draw();
    let calls = crate::preview::gitdiff::highlight_calls_for_test();
    let vh = s.app.tab.preview_viewport as usize;
    assert!(vh > 0, "diff プレビューの高さが描画で設定されているはず");
    assert!(
        calls > 0 && calls <= vh * 3,
        "可視範囲(高さ{vh})を大きく超えてハイライトしている(全{total_rows}行中{calls}回): \
         render_gitdiff が diff_lines_range に全行を渡す退行が起きていないか"
    );

    // Scrolling must not break the windowing: after `G` (to the very end), the count must stay in
    // the same order of magnitude, not jump toward `total_rows`.
    s.key('G');
    crate::preview::gitdiff::reset_highlight_calls_for_test();
    s.draw();
    let calls_at_bottom = crate::preview::gitdiff::highlight_calls_for_test();
    assert!(
        calls_at_bottom > 0 && calls_at_bottom <= vh * 3,
        "末尾(G)へスクロールしても画面オーダーのはず(全{total_rows}行中{calls_at_bottom}回)"
    );

    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_gitdiff_side_by_side_render_highlights_only_the_visible_rows() {
    let dir = sandbox("gitdiff_split_windowed");
    seed_huge_diff_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("big.txt");
    s.key('d');
    s.key('s'); // unified → side-by-side (unconditional at any width, unlike Auto)
    assert!(
        s.app.diff_is_split(1) && s.app.diff_is_split(200),
        "s 一回で side-by-side に固定されるはず"
    );
    s.see("side-by-side");

    let diff = s.app.git_diff_lines();
    let ext = s.app.current_preview_ext().to_string();
    let total_rows = crate::preview::gitdiff::side_by_side_row_count(&diff, &ext);
    assert!(
        total_rows > 1000,
        "side-by-side の行数も画面よりずっと大きいはず(実際 {total_rows})"
    );

    // Each side-by-side row can highlight up to two halves (old + new) — same 2x headroom the
    // unit tests in gitdiff.rs use.
    crate::preview::gitdiff::reset_highlight_calls_for_test();
    s.draw();
    let calls = crate::preview::gitdiff::highlight_calls_for_test();
    let vh = s.app.tab.preview_viewport as usize;
    assert!(vh > 0);
    assert!(
        calls > 0 && calls <= vh * 6,
        "side-by-side でも可視範囲(高さ{vh})を大きく超えてハイライトしている(全{total_rows}行中{calls}回)"
    );

    // Scrolling in side-by-side layout must not break the windowing either.
    s.key('G');
    crate::preview::gitdiff::reset_highlight_calls_for_test();
    s.draw();
    let calls_at_bottom = crate::preview::gitdiff::highlight_calls_for_test();
    assert!(
        calls_at_bottom > 0 && calls_at_bottom <= vh * 6,
        "side-by-side で末尾(G)へスクロールしても画面オーダーのはず(全{total_rows}行中{calls_at_bottom}回)"
    );

    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_worktree_diff_toggle_layout_and_back() {
    let dir = sandbox("git_worktree_detail");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('d');
    assert!(s.app.is_git_detail());
    s.see("Uncommitted changes");
    s.dont_see("");
    s.key('s');
    s.see("side-by-side");
    s.see("");
    s.key('q');
    assert!(!s.app.is_git_detail());
    assert!(s.app.is_git_view());
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_log_navigate_open_detail_and_back() {
    let dir = sandbox("git_log_nav");
    seed_repo_history(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('l');
    assert!(s.app.is_git_log());
    s.see("gammacommit");
    s.see("betacommit");
    s.see("alphacommit");
    let top = s.app.git_log_selected_id().expect("top id");
    s.key('j');
    let second = s.app.git_log_selected_id().expect("second id");
    assert_ne!(top, second);
    // Derive the selected commit's subject rather than assuming a specific ordering.
    let second_subj = crate::git::commit_meta(&s.app.tab.root, &second)
        .and_then(|m| m.message.lines().next().map(str::to_string))
        .expect("second subject");
    s.enter();
    assert!(s.app.is_git_detail());
    s.see(&second_subj); // Enter opens the selected commit's detail
    s.key('q');
    assert!(s.app.is_git_log());
    assert_eq!(
        s.app.git_log_selected_id().as_deref(),
        Some(second.as_str())
    );
    s.key('l');
    assert!(s.app.is_git_detail());
    s.see(&second_subj); // `l` also opens the same commit's detail
    s.key('q');
    assert!(s.app.is_git_log());
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_graph_navigates_commit_rows() {
    let dir = sandbox("git_graph_nav");
    seed_repo_history(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('g');
    assert!(s.app.is_git_graph());
    s.see("");
    s.key('g');
    let top = s
        .app
        .git_graph_selected_row()
        .and_then(|r| r.commit.clone())
        .expect("top commit");
    s.key('G');
    let bottom = s
        .app
        .git_graph_selected_row()
        .and_then(|r| r.commit.clone())
        .expect("bottom commit");
    assert_ne!(top, bottom);
    s.key('g');
    s.key('j');
    let after_j = s
        .app
        .git_graph_selected_row()
        .and_then(|r| r.commit.clone());
    assert!(
        after_j.is_some() && after_j.as_deref() != Some(top.as_str()),
        "j: {after_j:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_graph_set_base_keeps_selection_then_clear() {
    let dir = sandbox("git_graph_base2");
    seed_repo_history(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('g');
    s.dont_see("⌖ base:");
    assert!(s.app.git_graph_base_label().is_none());
    s.key('g');
    let sel_before = s
        .app
        .git_graph_selected_row()
        .and_then(|r| r.commit.clone())
        .expect("commit");
    s.key('s');
    s.see("⌖ base:");
    assert!(s.app.git_graph_base_label().is_some());
    assert_eq!(
        s.app
            .git_graph_selected_row()
            .and_then(|r| r.commit.clone()),
        Some(sel_before)
    );
    s.key('x');
    s.dont_see("⌖ base:");
    assert!(s.app.git_graph_base_label().is_none());
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_graph_picker_current_only_then_all() {
    let dir = sandbox("git_graph_picker2");
    seed_repo_history(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('g');
    s.key('b');
    assert!(s.app.is_git_graph_picker());
    let feature_on = |app: &App| -> Option<bool> {
        app.git_graph_picker_items()
            .into_iter()
            .find(|(name, _, _)| name == "feature-x")
            .map(|(_, _, on)| on)
    };
    assert_eq!(feature_on(&s.app), Some(true));
    s.key('n');
    assert_eq!(feature_on(&s.app), Some(false));
    s.key('a');
    assert_eq!(feature_on(&s.app), Some(true));
    s.key('q');
    assert!(!s.app.is_git_graph_picker());
    assert!(s.app.is_git_graph());
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_branches_filter_narrows_and_clears() {
    let dir = sandbox("git_branches_filter");
    seed_repo_history(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('b');
    assert!(s.app.is_git_branches());
    assert_eq!(s.app.git_branch_view().len(), 2);
    s.key('/');
    assert!(s.app.git_branch_filtering());
    s.keys("feature");
    let view = s.app.git_branch_view();
    assert_eq!(view.len(), 1);
    assert_eq!(view[0].name, "feature-x");
    s.see("feature-x");
    s.esc();
    assert!(!s.app.git_branch_filtering());
    assert_eq!(s.app.git_branch_view().len(), 2);
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_branch_create_switches_to_it() {
    let dir = sandbox("git_branch_create");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('b');
    s.key('n');
    assert!(s.app.is_dialog());
    s.keys("createdbr");
    s.enter();
    let branches = crate::git::branches(&s.app.tab.root);
    let created = branches.iter().find(|b| b.name == "createdbr");
    assert!(created.is_some());
    assert!(created.unwrap().is_current);
    assert!(s.app.is_git_view());
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_branch_delete_removes_feature_branch() {
    let dir = sandbox("git_branch_delete");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('b');
    s.key('g');
    assert_eq!(
        s.app.git_branch_view()[s.app.git_branch_sel()].name,
        "feature-x"
    );
    s.key('d');
    assert!(s.app.is_dialog());
    s.key('y');
    assert!(!crate::git::branches(&s.app.tab.root)
        .iter()
        .any(|b| b.name == "feature-x"));
    assert!(!s
        .app
        .git_branch_view()
        .iter()
        .any(|b| b.name == "feature-x"));
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_copy_leader_all_subkeys_consume_from_log() {
    let dir = sandbox("git_copy_subkeys");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('l');
    assert_eq!(s.app.surface(), crate::keymap::Surface::GitLog);
    s.see("init");
    let id = s.app.git_log_selected_id().expect("selected id");
    let meta = crate::git::commit_meta(&s.app.tab.root, &id).expect("commit_meta");
    assert_eq!(meta.message.lines().next(), Some("init"));
    assert_eq!(meta.author, "t");
    assert_eq!(meta.short.len(), 7);
    assert_eq!(meta.id.len(), 40);
    assert!(meta.id.starts_with(&meta.short));
    assert!(!meta.date.is_empty());
    for sub in ['s', 'h', 't', 'm', 'a', 'd'] {
        s.key('y');
        assert_eq!(
            s.app.pending_leader,
            Some(crate::keymap::LeaderId::GitCopy),
            "sub={sub}"
        );
        s.key(sub);
        assert_eq!(s.app.pending_leader, None, "y->{sub}");
    }
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_copy_leader_opens_in_graph_and_detail() {
    let dir = sandbox("git_copy_surfaces");
    seed_repo_history(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('o');
    s.key('g');
    s.key('g');
    assert_eq!(s.app.surface(), crate::keymap::Surface::GitGraph);
    s.key('y');
    assert_eq!(s.app.pending_leader, Some(crate::keymap::LeaderId::GitCopy));
    s.esc();
    assert_eq!(s.app.pending_leader, None);
    s.enter();
    assert_eq!(s.app.surface(), crate::keymap::Surface::GitDetail);
    s.key('y');
    assert_eq!(s.app.pending_leader, Some(crate::keymap::LeaderId::GitCopy));
    s.esc();
    assert_eq!(s.app.pending_leader, None);
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_git_changed_filter_n_jumps_between_changed_files() {
    let dir = sandbox("git_changed_nN");
    seed_repo(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.key('C');
    assert!(s.app.changed_filter());
    s.see("a.rs");
    s.see("new.txt");
    s.select("a.rs");
    assert!(s.app.tab.entries[s.app.tab.selected].path.ends_with("a.rs"));
    s.key('n');
    assert!(s.app.tab.entries[s.app.tab.selected]
        .path
        .ends_with("new.txt"));
    s.key('n');
    assert!(s.app.tab.entries[s.app.tab.selected].path.ends_with("a.rs"));
    s.key('N');
    assert!(s.app.tab.entries[s.app.tab.selected]
        .path
        .ends_with("new.txt"));
    s.key('C');
    assert!(!s.app.changed_filter());
    std::fs::remove_dir_all(&dir).ok();
}

// ---- Tree sort (size/modified/ext) + filtering edge cases ----

#[test]
fn e2e_sort_by_size_orders_ascending() {
    let dir = sandbox("sort_size");
    std::fs::write(dir.join("aaa.txt"), vec![b'a'; 300]).unwrap();
    std::fs::write(dir.join("bbb.txt"), vec![b'b'; 30]).unwrap();
    std::fs::write(dir.join("ccc.txt"), vec![b'c'; 150]).unwrap();
    let mut s = Sim::new(&canon(&dir));
    let names: Vec<&str> = s
        .app
        .tab
        .entries
        .iter()
        .map(|e| e.path.file_name().and_then(|n| n.to_str()).unwrap_or(""))
        .collect();
    assert_eq!(names, vec!["aaa.txt", "bbb.txt", "ccc.txt"]);
    s.key('s');
    s.key('s');
    assert!(!s.app.is_sort_menu());
    let names: Vec<&str> = s
        .app
        .tab
        .entries
        .iter()
        .map(|e| e.path.file_name().and_then(|n| n.to_str()).unwrap_or(""))
        .collect();
    assert_eq!(names, vec!["bbb.txt", "ccc.txt", "aaa.txt"]);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_sort_by_modified_orders_ascending() {
    let dir = sandbox("sort_mod");
    fn set_mtime(p: &std::path::Path, secs: u64) {
        std::fs::write(p, b"x\n").unwrap();
        let f = std::fs::OpenOptions::new().write(true).open(p).unwrap();
        f.set_modified(std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs))
            .unwrap();
    }
    set_mtime(&dir.join("m_a.txt"), 2_000_000_000);
    set_mtime(&dir.join("m_b.txt"), 1_000_000_000);
    set_mtime(&dir.join("m_c.txt"), 1_500_000_000);
    let mut s = Sim::new(&canon(&dir));
    s.key('s');
    s.key('m');
    assert!(!s.app.is_sort_menu());
    let names: Vec<&str> = s
        .app
        .tab
        .entries
        .iter()
        .map(|e| e.path.file_name().and_then(|n| n.to_str()).unwrap_or(""))
        .collect();
    assert_eq!(names, vec!["m_b.txt", "m_c.txt", "m_a.txt"]);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_sort_by_ext_orders_ascending() {
    let dir = sandbox("sort_ext");
    std::fs::write(dir.join("zzz.aaa"), b"1\n").unwrap();
    std::fs::write(dir.join("aaa.zzz"), b"2\n").unwrap();
    std::fs::write(dir.join("mmm.mmm"), b"3\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    let names: Vec<&str> = s
        .app
        .tab
        .entries
        .iter()
        .map(|e| e.path.file_name().and_then(|n| n.to_str()).unwrap_or(""))
        .collect();
    assert_eq!(names, vec!["aaa.zzz", "mmm.mmm", "zzz.aaa"]);
    s.key('s');
    s.key('e');
    assert!(!s.app.is_sort_menu());
    let names: Vec<&str> = s
        .app
        .tab
        .entries
        .iter()
        .map(|e| e.path.file_name().and_then(|n| n.to_str()).unwrap_or(""))
        .collect();
    assert_eq!(names, vec!["zzz.aaa", "mmm.mmm", "aaa.zzz"]);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_tree_filter_no_matches_shows_empty_list() {
    let dir = sandbox("filter_nomatch");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    let all = s.app.tab.entries.len();
    s.key('/');
    s.keys("qzqzqznope");
    assert!(s.app.is_filtering());
    assert!(s.app.tab.entries.is_empty(), "{}", s.screen());
    s.dont_see("notes.txt");
    s.enter();
    assert!(s.app.tab.entries.is_empty());
    s.esc();
    assert_eq!(s.app.tab.entries.len(), all);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_tree_filter_is_case_insensitive() {
    let dir = sandbox("filter_case");
    seed_files(&dir);
    std::fs::write(dir.join("UPPER.TXT"), "X\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.key('/');
    s.keys("README");
    s.see("readme.md");
    assert!(s
        .app
        .tab
        .entries
        .iter()
        .any(|e| e.path.ends_with("readme.md")));
    assert!(!s
        .app
        .tab
        .entries
        .iter()
        .any(|e| e.path.ends_with("UPPER.TXT")));
    for _ in 0..8 {
        s.press(KeyCode::Backspace, KeyModifiers::NONE);
    }
    s.keys("upper");
    s.see("UPPER.TXT");
    assert!(s
        .app
        .tab
        .entries
        .iter()
        .any(|e| e.path.ends_with("UPPER.TXT")));
    std::fs::remove_dir_all(&dir).ok();
}

// ---- CSV/table copy (y→c/r/C/f) ----

#[test]
fn e2e_table_copy_leader_sub_keys_consume_and_flash() {
    let dir = sandbox("table_copy_wiring");
    std::fs::write(dir.join("data.csv"), "name,qty\napple,3\nkiwi,7\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("data.csv");
    s.enter();
    s.see("TABLE");
    s.key('j');
    s.key('l');
    assert_eq!(s.app.table_cursor(), (1, 1));
    for sub in ['c', 'r', 'C'] {
        s.app.flash = None;
        s.key('y');
        assert_eq!(
            s.app.pending_leader,
            Some(crate::keymap::LeaderId::TableCopy)
        );
        s.key(sub);
        assert_eq!(s.app.pending_leader, None, "sub={sub}");
        assert!(s.app.flash.is_some(), "sub={sub}");
    }
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_table_copy_full_path_value() {
    let dir = sandbox("table_copy_path");
    std::fs::write(dir.join("data.csv"), "name,qty\napple,3\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("data.csv");
    s.enter();
    s.see("TABLE");
    let csv = s.app.tab.preview_path.clone().expect("preview path");
    assert_eq!(
        s.app.copy_string_for(crate::app::CopyKind::Full),
        Some(csv.display().to_string())
    );
    s.key('y');
    assert_eq!(
        s.app.pending_leader,
        Some(crate::keymap::LeaderId::TableCopy)
    );
    s.key('f');
    assert_eq!(s.app.pending_leader, None);
    std::fs::remove_dir_all(&dir).ok();
}

// ---- Bookmarking from the preview (m / ') ----

#[test]
fn e2e_bookmark_set_from_preview_targets_shown_file() {
    let root = sandbox("bm_preview_target");
    let proj = root.join("proj");
    std::fs::create_dir_all(&proj).unwrap();
    seed_files(&proj);
    let proj = canon(&proj);
    let mut s = Sim::new(&proj);
    s.app.bookmarks = crate::bookmarks::Bookmarks::with_base(root.join("cfg"), &proj);
    s.select("notes.txt");
    s.keys("ma");
    s.see("bookmarked");
    s.select("readme.md");
    s.key('\'');
    s.see("Bookmarks");
    s.key('a');
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert!(s
        .app
        .tab
        .preview_path
        .as_deref()
        .is_some_and(|p| p.ends_with("notes.txt")));
    assert!(s.app.tab.entries[s.app.tab.selected]
        .path
        .ends_with("readme.md"));
    s.keys("mx");
    assert!(
        s.app
            .bookmarks
            .get('x')
            .is_some_and(|p| p.ends_with("notes.txt")),
        "{:?}",
        s.app.bookmarks.get('x')
    );
    assert!(!s
        .app
        .bookmarks
        .get('x')
        .is_some_and(|p| p.ends_with("readme.md")));
    std::fs::remove_dir_all(&root).ok();
}

#[test]
fn e2e_bookmark_list_enter_jumps_to_file() {
    let root = sandbox("bm_list_enter");
    let proj = root.join("proj");
    std::fs::create_dir_all(&proj).unwrap();
    std::fs::write(proj.join("a.txt"), "ALPHA_BODY\n").unwrap();
    std::fs::write(proj.join("b.txt"), "BETA_BODY\n").unwrap();
    let proj = canon(&proj);
    let mut s = Sim::new(&proj);
    s.app.bookmarks = crate::bookmarks::Bookmarks::with_base(root.join("cfg"), &proj);
    s.select("a.txt");
    s.keys("ma");
    s.select("b.txt");
    s.keys("mb");
    s.select("b.txt");
    s.enter();
    assert_eq!(s.app.tab.mode, Mode::Preview);
    s.see("BETA_BODY");
    s.key('\'');
    s.see("Bookmarks");
    assert_eq!(s.app.bookmark_list_sel(), 0);
    s.enter();
    assert!(!s.app.is_bookmark_list());
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert!(
        s.app
            .tab
            .preview_path
            .as_deref()
            .is_some_and(|p| p.ends_with("a.txt")),
        "{:?}",
        s.app.tab.preview_path
    );
    s.see("ALPHA_BODY");
    std::fs::remove_dir_all(&root).ok();
}

// ---- Paste-jump (P) edge cases ----

#[test]
fn e2e_pastejump_hash_line_fragment_scrolls() {
    let dir = sandbox("pastejump_hash");
    let mut body = String::new();
    for i in 1..=60 {
        if i == 1 {
            body.push_str("LINE_ONE_ZZ\n");
        } else if i == 40 {
            body.push_str("LINE_FORTY_ZZ\n");
        } else {
            body.push_str(&format!("plain line {i}\n"));
        }
    }
    std::fs::write(dir.join("deep.txt"), body).unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);
    s.app.paste_jump_from("deep.txt#L40");
    s.draw();
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert!(s
        .app
        .tab
        .preview_path
        .as_deref()
        .is_some_and(|p| p.ends_with("deep.txt")));
    s.see("LINE_FORTY_ZZ");
    s.dont_see("LINE_ONE_ZZ");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_pastejump_at_ref_relative_opens() {
    let dir = sandbox("pastejump_atref");
    std::fs::create_dir_all(dir.join("sub")).unwrap();
    std::fs::write(dir.join("sub/target.txt"), "AT_REF_BODY\nsecond line\n").unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);
    s.app.paste_jump_from("@sub/target.txt");
    s.draw();
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert!(s
        .app
        .tab
        .preview_path
        .as_deref()
        .is_some_and(|p| p.ends_with("sub/target.txt")));
    s.see("AT_REF_BODY");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_pastejump_no_line_opens_at_top() {
    let dir = sandbox("pastejump_top");
    let mut body = String::new();
    for i in 1..=60 {
        if i == 1 {
            body.push_str("TOP_MARKER_LINE\n");
        } else if i == 50 {
            body.push_str("DEEP_MARKER_LINE\n");
        } else {
            body.push_str(&format!("filler line {i}\n"));
        }
    }
    std::fs::write(dir.join("top.txt"), body).unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);
    s.app.paste_jump_from("top.txt");
    s.draw();
    assert_eq!(s.app.tab.mode, Mode::Preview);
    assert!(s
        .app
        .tab
        .preview_path
        .as_deref()
        .is_some_and(|p| p.ends_with("top.txt")));
    s.see("TOP_MARKER_LINE");
    s.dont_see("DEEP_MARKER_LINE");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_pastejump_non_github_url_unrecognized() {
    let dir = sandbox("pastejump_nongithub");
    seed_files(&dir);
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);
    assert_eq!(s.app.tab.mode, Mode::Tree);
    assert!(s.app.tab.preview_path.is_none());
    s.app.paste_jump_from("https://example.com/some/file.rs");
    s.draw();
    assert_eq!(s.app.tab.mode, Mode::Tree);
    assert!(s.app.tab.preview_path.is_none());
    // Not just "some flash appeared" — specifically the *unrecognized* classification
    // (`Msg::PasteJumpUnrecognized`), so this can't silently regress into reporting "not found" or
    // "no clipboard" instead (see `parse_paste_target` / `paste_jump_from` in `paste_jump.rs`).
    assert_eq!(
        s.app.flash.as_deref(),
        Some(crate::i18n::tr(
            s.app.lang,
            crate::i18n::Msg::PasteJumpUnrecognized
        )),
        "非 GitHub URL は unrecognized として分類されるはず: {:?}",
        s.app.flash
    );
    std::fs::remove_dir_all(&dir).ok();
}

// ---- File operations edge cases ----

#[cfg(target_os = "macos")]
#[test]
fn e2e_fileop_delete_trash_removes_file() {
    let dir = sandbox("fileop_trash");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.key(' ');
    s.key('d');
    assert!(s.app.is_dialog());
    s.key('y');
    assert!(!dir.join("notes.txt").exists());
    assert!(!s.app.is_dialog());
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_fileop_rename_to_existing_name_fails() {
    let dir = sandbox("fileop_rename_exist");
    seed_files(&dir);
    let mut s = Sim::new(&canon(&dir));
    s.select("notes.txt");
    s.key(' ');
    s.key('r');
    for _ in 0..40 {
        s.press(KeyCode::Backspace, KeyModifiers::NONE);
    }
    s.keys("data.csv");
    s.enter();
    assert!(dir.join("notes.txt").exists());
    assert!(dir.join("data.csv").exists());
    assert!(!s.app.is_dialog());
    // Not just "some flash appeared" — specifically *why* it was refused: `AlreadyExists` (the
    // typed `fileops::FileOpError` translated by `App::describe_error`, added in v0.23.1). Without
    // this, the rename could start silently succeeding, or start failing for an unrelated reason
    // (e.g. a permissions error), and this test would still pass.
    assert!(
        s.app.flash.as_deref().is_some_and(
            |f| f.contains(crate::i18n::tr(s.app.lang, crate::i18n::Msg::AlreadyExists))
        ),
        "既に存在するので AlreadyExists の flash が出るはず: {:?}",
        s.app.flash
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_fileop_create_targets_cursor_file_parent() {
    let dir = sandbox("fileop_create_subdir");
    std::fs::create_dir_all(dir.join("sub")).unwrap();
    std::fs::write(dir.join("sub/inner.txt"), "x\n").unwrap();
    std::fs::write(dir.join("root_file.txt"), "y\n").unwrap();
    let dir = canon(&dir);
    let mut s = Sim::new(&dir);
    s.select("sub");
    s.enter();
    s.select("sub/inner.txt");
    s.key(' ');
    s.key('n');
    s.keys("made.txt");
    s.enter();
    assert!(dir.join("sub/made.txt").exists());
    assert!(!dir.join("made.txt").exists());
    std::fs::remove_dir_all(&dir).ok();
}

/// The user-facing shape of the alert regression: a document that mixes plain checkboxes with a
/// checklist inside a `> [!NOTE]`, driven by **real keystrokes**. Before the fix every Space in this
/// document — including on the plain checkboxes outside the alert — was refused (flash
/// "couldn't toggle checkbox — reloaded") because the write-back scanner never saw the alert's tasks
/// and so disagreed with the screen about how many there were.
#[test]
fn e2e_task_toggle_works_in_a_document_containing_an_alert() {
    let dir = sandbox("md_task_alert");
    let body = "# Checklist\n\n- [ ] plain one\n\n> [!NOTE]\n> - [ ] inside alert\n> - [ ] second in alert\n\n- [ ] after alert\n";
    std::fs::write(dir.join("todo.md"), body).unwrap();
    let mut s = Sim::with_config(&canon(&dir), Config::default());
    s.select("todo.md");
    s.enter();
    let read = || std::fs::read_to_string(dir.join("todo.md")).unwrap();

    // The 1st (outside the alert).
    s.tab();
    s.key(' ');
    assert!(read().contains("- [x] plain one"), "外側1: {}", read());
    s.dont_see("couldn't toggle checkbox");

    // The 2nd and 3rd (inside the alert) — this used to not work at all.
    s.tab();
    s.key(' ');
    assert!(
        read().contains("> - [x] inside alert"),
        "アラート内1: {}",
        read()
    );
    s.tab();
    s.key(' ');
    assert!(
        read().contains("> - [x] second in alert"),
        "アラート内2: {}",
        read()
    );

    // The 4th (after the alert) — the ordinal doesn't drift crossing the alert.
    s.tab();
    s.key(' ');
    assert!(read().contains("- [x] after alert"), "外側2: {}", read());
    s.dont_see("couldn't toggle checkbox");

    // Other lines aren't broken (the heading and alert header are unchanged).
    let out = read();
    assert!(
        out.contains("# Checklist") && out.contains("> [!NOTE]"),
        "{out}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Custom `ui.md_task_states` must cycle inside an alert too — the state char is written back through
/// the same path, and the alert's `>` prefix shifts every column on the line.
#[test]
fn e2e_task_custom_states_cycle_inside_an_alert() {
    let dir = sandbox("md_task_alert_states");
    std::fs::write(dir.join("todo.md"), "> [!TIP]\n> - [ ] cycle me\n").unwrap();
    let mut cfg = Config::default();
    cfg.ui.md_task_states = vec![" ".into(), "/".into(), "x".into()];
    let mut s = Sim::with_config(&canon(&dir), cfg);
    s.select("todo.md");
    s.enter();
    let read = || std::fs::read_to_string(dir.join("todo.md")).unwrap();
    s.tab();
    for want in ["> - [/] cycle me", "> - [x] cycle me", "> - [ ] cycle me"] {
        s.key(' ');
        assert!(read().contains(want), "期待 {want}: {}", read());
    }
    std::fs::remove_dir_all(&dir).ok();
}

/// A collapsed `<details>` hides its checkboxes, so Space on the visible ones must still work: the
/// scanner must not count the hidden ones (it used to, which cancelled every toggle in the file).
#[test]
fn e2e_task_toggle_works_with_a_collapsed_details_block() {
    let dir = sandbox("md_task_details");
    std::fs::write(
        dir.join("todo.md"),
        "- [ ] visible\n\n<details>\n<summary>Hidden</summary>\n\n- [ ] collapsed\n\n</details>\n",
    )
    .unwrap();
    let mut s = Sim::with_config(&canon(&dir), Config::default());
    s.select("todo.md");
    s.enter();
    let read = || std::fs::read_to_string(dir.join("todo.md")).unwrap();

    // Advance focus onto the visible checkbox. Tab cycles links/tasks/details in document order;
    // a closed `<details>` renders only its summary as a focusable item (the hidden checkbox inside
    // never becomes one), so with this document the only two Tab stops are the visible task and the
    // details heading. Bounded to a couple of full cycles (2 items ⇒ 2 tabs is enough) so that if a
    // future change ever drops the task item entirely, this fails loudly with a clear message
    // instead of degrading into the old "either" assertion that passed even when nothing moved.
    let mut on_task = false;
    for _ in 0..4 {
        s.tab();
        if s.app.md_focused_task() {
            on_task = true;
            break;
        }
    }
    assert!(
        on_task,
        "Tab で可視タスクへフォーカスできるはず(focused_item={:?}, md_focused_details={:?})",
        s.app.focused_item(),
        s.app.md_focused_details()
    );

    s.key(' ');
    let out = read();
    // Both assertions matter: the visible task must actually flip (not "either this or that stayed
    // the same" — that OR let a no-op write-back pass), and the hidden one must be left alone (it's
    // not even visible, so Space must never have reached it).
    assert!(
        out.contains("- [x] visible"),
        "可視タスクが実際にトグルされる(全キャンセルされない): {out}"
    );
    assert!(
        out.contains("- [ ] collapsed"),
        "隠れたタスクは変化しない: {out}"
    );
    s.dont_see("couldn't toggle checkbox");
    std::fs::remove_dir_all(&dir).ok();
}

/// Link spans and their targets are produced by two passes — `collapse_links` rewrites the rendered
/// spans, and the item list zips the Nth link span with the Nth collected target. If the two ever fall
/// out of step **every** link in the document points somewhere else (Enter opens the wrong file), and
/// nothing on screen looks wrong. This walks a corpus of every link form konoma renders and asserts the
/// Nth focusable link carries exactly the Nth URL in document order.
#[test]
fn e2e_link_targets_stay_aligned_with_document_order() {
    let cases: &[(&str, &str, &[&str])] = &[
        (
            "inline",
            "[a](./a.md) and [b](./b.md)\n",
            &["./a.md", "./b.md"],
        ),
        (
            "with title",
            "[a](./a.md \"T\") [b](./b.md)\n",
            &["./a.md", "./b.md"],
        ),
        (
            "reference style",
            "[a][r1] [b][r2]\n\n[r1]: ./a.md\n[r2]: ./b.md\n",
            &["./a.md", "./b.md"],
        ),
        (
            "autolink angle",
            "<https://x.example/1> then [b](./b.md)\n",
            &["https://x.example/1", "./b.md"],
        ),
        (
            "bare url autolinked",
            "see https://x.example/2 and [b](./b.md)\n",
            &["https://x.example/2", "./b.md"],
        ),
        (
            "url inside code is not a link",
            "`https://x.example/no` but [b](./b.md)\n",
            &["./b.md"],
        ),
        (
            "url inside a fence is not a link",
            "```\nhttps://x.example/no\n```\n\n[b](./b.md)\n",
            &["./b.md"],
        ),
        (
            "link inside a table cell",
            "| a | b |\n|---|---|\n| [x](./x.md) | [y](./y.md) |\n",
            &["./x.md", "./y.md"],
        ),
        (
            "link inside an alert",
            "> [!NOTE]\n> [in](./in.md)\n\n[out](./out.md)\n",
            &["./in.md", "./out.md"],
        ),
        (
            "heading anchor plus file",
            "[h](#section) [f](./f.md)\n",
            &["#section", "./f.md"],
        ),
        (
            "cjk label",
            "[日本語のリンク](./ja.md) [b](./b.md)\n",
            &["./ja.md", "./b.md"],
        ),
        (
            "emphasis around link",
            "**[bold](./x.md)** and *[em](./y.md)*\n",
            &["./x.md", "./y.md"],
        ),
    ];
    for (name, body, want) in cases {
        let (s, dir) = md_preview(cfg_code_bg_none(), "links", body);
        assert_eq!(
            s.app.md_link_targets(),
            want.iter().map(|t| t.to_string()).collect::<Vec<_>>(),
            "{name}: リンクの並び/対応がソース順とズレている(Enter が別の宛先を開く)\n--- src ---\n{body}"
        );
        std::fs::remove_dir_all(&dir).ok();
    }
}

/// Tab focus must bring the **whole** focused block into view, not just the line the focus marker sits
/// on. A code block's marker is its header and a `<details>`' marker is its summary, so ensuring only
/// that line was visible parked the block against the bottom edge with its contents cut off — the user
/// had to scroll manually after every Tab. Diagrams already did this; everything multi-line now does.
#[test]
fn e2e_tab_scrolls_whole_block_into_view() {
    // Place a block smaller than the screen right at the bottom edge.
    let mut body = String::from("# Doc\n\n");
    for i in 0..20 {
        body.push_str(&format!("filler line {i}\n\n"));
    }
    body.push_str("```rust\n");
    for i in 0..8 {
        body.push_str(&format!("let line_{i} = {i};\n"));
    }
    body.push_str("```\n\nafter\n");

    let (mut s, dir) = md_preview(cfg_code_bg_none(), "blocks", &body);
    let vh = s.app.preview_viewport_for_test().max(1) as usize;
    s.tab(); // the sole Tab target = the code block
    let scroll = s.app.tab.preview_scroll as usize;
    let focused = s.app.focused_item().expect("コードブロックにフォーカス");
    let start = s.app.md_item_line_for_test(focused);
    let end = s.app.md_item_block_end_for_test(focused);
    assert!(
        end > start,
        "コードブロックは複数行にわたる (start={start} end={end})"
    );

    let (top, _) = s.app.md_visual_span_for_test(start);
    let (btop, bh) = s.app.md_visual_span_for_test(end);
    let block_bottom = btop + bh;
    assert!(
        top >= scroll,
        "ブロック先頭が上に見切れている: top={top} scroll={scroll}"
    );
    assert!(
        block_bottom <= scroll + vh,
        "ブロック末尾が下に見切れている: bottom={block_bottom} scroll={scroll} viewport={vh}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// The same guarantee for an **open** `<details>`: focusing its summary must reveal the body under it.
#[test]
fn e2e_tab_scrolls_open_details_body_into_view() {
    let mut body = String::from("# Doc\n\n");
    for i in 0..20 {
        body.push_str(&format!("filler line {i}\n\n"));
    }
    body.push_str("<details open>\n<summary>Show me</summary>\n\n");
    for i in 0..8 {
        body.push_str(&format!("- body line {i}\n"));
    }
    body.push_str("\n</details>\n");

    let (mut s, dir) = md_preview(cfg_code_bg_none(), "detblocks", &body);
    let vh = s.app.preview_viewport_for_test().max(1) as usize;
    s.tab();
    let focused = s.app.focused_item().expect("details にフォーカス");
    let start = s.app.md_item_line_for_test(focused);
    let end = s.app.md_item_block_end_for_test(focused);
    assert!(
        end > start,
        "開いた details は本文を持つ (start={start} end={end})"
    );
    let scroll = s.app.tab.preview_scroll as usize;
    let (btop, bh) = s.app.md_visual_span_for_test(end);
    assert!(
        btop + bh <= scroll + vh,
        "details 本文の末尾が見切れている: bottom={} scroll={scroll} viewport={vh}",
        btop + bh
    );
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// External command delegation (`[[preview.rules]] command = "..."`)
// =============================================================================

/// A real key press (`Enter` on a file matched by a `command = "..."` rule) shows the delegated
/// command's captured output through the ordinary windowed text reader. This is the actual
/// regression this module exists to prevent: before implementation, the preview showed the raw
/// `{:?}` Debug dump of `PreviewKind::Command` (`render_as=None, detached=false`) instead of any
/// real content — `command.rs`'s own tests cover `run_capture`/`build_argv` in isolation, and
/// `app/tests.rs` covers `MediaJob::Command` and the render-side fallback directly, but only this
/// full key→render path proves the delegation is actually wired end to end. Image-mode delegation
/// isn't exercised here — `Sim` has no real image backend/picker, so `App::is_image_preview()` can
/// never become true in this harness; `MediaJob::Command`'s `as_image` branch is covered directly
/// in `app/tests.rs` instead.
#[cfg(unix)]
#[test]
fn e2e_delegated_command_text_preview_shows_captured_output() {
    let dir = sandbox("cmd_delegate_text");
    std::fs::write(dir.join("app.log"), "line one\nline two\n").unwrap();
    let root = canon(&dir);

    let mut cfg = Config::default();
    // Put it first so it wins over the builtin rules (which don't match *.log anyway, but this
    // mirrors how a user rule is meant to take priority).
    cfg.preview.rules.insert(
        0,
        crate::config::Rule {
            glob: Some("*.log".into()),
            command: Some("cat {path}".into()),
            ..Default::default()
        },
    );
    let mut s = Sim::with_config(&root, cfg);
    s.select("app.log");
    s.enter();
    s.see("line one");
    s.see("line two");
    // The title still names the original file — the generated temp path never leaks into the UI.
    s.see("app.log");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// H2/H3 (docs/archive/AUDIT-TESTS-2026-08.md): style-attribute assertions + config→keybinding wiring.
//
// The audit found two "breakable with zero red tests" gaps: (1) `keymap::scheme_from_str` (the
// `ui.keys` string → paging key scheme) had no test that goes config string → real keystrokes →
// screen — only unit tests that construct a `KeyScheme` directly. (2) `Sim::screen()` only ever
// returned glyphs, never `Style`, so konoma's "sentinel style" focus markers (REVERSED on Tab
// focus) were unverifiable at the e2e layer even though `grep -rn REVERSED src` shows they're
// central to the UI. Both are demonstrated broken-with-red-tests below by literally breaking the
// implementation locally and re-running (see the task report for the failure output); these tests
// are what stayed red.
// =============================================================================

/// `ui.keys = "less"`: plain `f`/Space page down, plain `b` pages up — and `Ctrl-f`/`Ctrl-b`,
/// which are the *vim* scheme's keys, are simply unbound in this surface (no fallback, no-op).
/// Breaking `scheme_from_str` to always return `KeyScheme::Vim` makes `f`/Space do nothing here
/// (verified: this test goes red with "left: 0 right: <n>"-style failures when that's done).
#[test]
fn e2e_ui_keys_less_scheme_uses_plain_letters_not_ctrl() {
    let mut cfg = Config::default();
    cfg.ui.keys = "less".into();
    let (mut s, dir) = text_preview(cfg, "keys_less", "big.txt", &paging_body());
    assert!(s.app.is_windowed());
    let top0 = s.app.preview_top_line();

    // Ctrl-f is the *vim* scheme's page-down key — unbound here, so nothing happens.
    s.ctrl('f');
    assert_eq!(
        s.app.preview_top_line(),
        top0,
        "less スキームでは Ctrl-f はバインドされていないはず"
    );

    // Plain `f` pages down (the less scheme's own key).
    s.key('f');
    let top1 = s.app.preview_top_line();
    assert!(top1 > top0, "less: 素の f がページ送りになるはず");

    // Space also pages down (less has both).
    s.key(' ');
    let top2 = s.app.preview_top_line();
    assert!(top2 > top1, "less: Space もページ送りになるはず");

    // Plain `b` pages back up.
    s.key('b');
    let top3 = s.app.preview_top_line();
    assert!(top3 < top2, "less: 素の b がページ戻しになるはず");

    std::fs::remove_dir_all(&dir).ok();
}

/// `ui.keys = "vim"` (also the default): `Ctrl-f`/`Ctrl-b` page, and the *less* scheme's plain
/// `f`/`b`/Space are simply unbound (no-op) in this surface.
#[test]
fn e2e_ui_keys_vim_scheme_uses_ctrl_not_plain_letters() {
    let mut cfg = Config::default();
    cfg.ui.keys = "vim".into();
    let (mut s, dir) = text_preview(cfg, "keys_vim", "big.txt", &paging_body());
    assert!(s.app.is_windowed());
    let top0 = s.app.preview_top_line();

    // Plain f/Space/b are the *less* scheme's keys — unbound here.
    s.key('f');
    assert_eq!(
        s.app.preview_top_line(),
        top0,
        "vim スキームでは素の f はバインドされていないはず"
    );
    s.key(' ');
    assert_eq!(
        s.app.preview_top_line(),
        top0,
        "vim スキームでは Space もバインドされていないはず"
    );

    // Ctrl-f/Ctrl-b (vim's own keys) work.
    s.ctrl('f');
    let top1 = s.app.preview_top_line();
    assert!(top1 > top0, "vim: Ctrl-f がページ送りになるはず");
    s.ctrl('b');
    let top2 = s.app.preview_top_line();
    assert!(top2 < top1, "vim: Ctrl-b がページ戻しになるはず");

    std::fs::remove_dir_all(&dir).ok();
}

/// An unrecognized `ui.keys` value (anything but `"less"`) falls back to vim — pinning down
/// `scheme_from_str`'s documented contract ("anything but 'less' is vim") through real keystrokes,
/// not just by constructing `KeyScheme::Vim` directly.
#[test]
fn e2e_ui_keys_unknown_value_falls_back_to_vim_scheme() {
    let mut cfg = Config::default();
    cfg.ui.keys = "emacs".into(); // not a recognized scheme name
    let (mut s, dir) = text_preview(cfg, "keys_unknown", "big.txt", &paging_body());
    assert!(s.app.is_windowed());
    let top0 = s.app.preview_top_line();

    // Falls back to vim: plain f is unbound...
    s.key('f');
    assert_eq!(
        s.app.preview_top_line(),
        top0,
        "不明な ui.keys は vim にフォールバックするはず(素の f は無効)"
    );
    // ...but Ctrl-f (vim's key) works.
    s.ctrl('f');
    assert!(
        s.app.preview_top_line() > top0,
        "不明な ui.keys は vim にフォールバックするはず(Ctrl-f が有効)"
    );

    std::fs::remove_dir_all(&dir).ok();
}

/// Tab-focus reversal, verified through the actual render buffer's `Style` (not internal indices)
/// — one test per `MdItemKind` that's reachable without an image `Picker` (`Sim` never attaches
/// one; see the mermaid note at the end). Each item is checked both while focused (REVERSED) and
/// while *not* focused (not REVERSED), so this also proves the effect is exclusive to the current
/// focus rather than sticking around. Breaking `invert_focused_line` into a no-op (returning the
/// line unchanged) turns every `reversed(...)` assertion below red (verified: see the task report).
#[test]
fn e2e_markdown_tab_focus_reverses_only_the_focused_marker_style() {
    let mut cfg = Config::default();
    cfg.ui.icons = false; // plain "[ ]" checkbox / no link-icon prefix glyph to worry about
    let (mut s, dir) = md_preview(
        cfg,
        "focus_styles",
        "Read the [guide](https://example.com/guide) first.\n\n\
         - [ ] pending task\n\n\
         ```rust\n\
         fn main() {}\n\
         ```\n\n\
         <details>\n\
         <summary>Extra Info</summary>\n\n\
         Some hidden body text.\n\n\
         </details>\n",
    );

    fn reversed(s: &Sim, needle: &str) -> bool {
        s.style_of(needle)
            .is_some_and(|st| st.add_modifier.contains(ratatui::style::Modifier::REVERSED))
    }

    // Nothing is focused before the first Tab: no marker is reversed, and in fact the buffer holds
    // no REVERSED cells at all (decorated Markdown has no separate block caret the way windowed
    // text/code previews do).
    assert!(
        s.reversed_cells().is_empty(),
        "フォーカス前は反転セルが1つも無いはず: {:?}",
        s.reversed_cells()
    );
    assert!(!reversed(&s, "guide"));
    assert!(!reversed(&s, "[ ]"));
    assert!(!reversed(&s, "rust"));
    assert!(!reversed(&s, ""));

    // 1st Tab → the link (document order: link, task, code block, details).
    s.tab();
    s.see_styled(
        "guide",
        |st| st.add_modifier.contains(ratatui::style::Modifier::REVERSED),
        "フォーカス中のリンク=反転",
    );
    assert!(
        !reversed(&s, "[ ]"),
        "リンクにフォーカス中はチェックボックスは反転しない"
    );
    assert!(
        !reversed(&s, "rust"),
        "リンクにフォーカス中はコードブロックは反転しない"
    );
    assert!(
        !reversed(&s, ""),
        "リンクにフォーカス中は details は反転しない"
    );

    // 2nd Tab → the checkbox marker.
    s.tab();
    assert!(
        !reversed(&s, "guide"),
        "フォーカスが移ればリンクは反転が解ける"
    );
    s.see_styled(
        "[ ]",
        |st| st.add_modifier.contains(ratatui::style::Modifier::REVERSED),
        "フォーカス中のチェックボックス=反転",
    );
    assert!(!reversed(&s, "rust"));
    assert!(!reversed(&s, ""));

    // 3rd Tab → the code block header (the *whole header line* inverts, per `invert_focused_line`'s
    // `whole_line` handling for `MdItemKind::CodeBlock`).
    s.tab();
    assert!(!reversed(&s, "[ ]"));
    s.see_styled(
        "rust",
        |st| st.add_modifier.contains(ratatui::style::Modifier::REVERSED),
        "フォーカス中のコードブロック=ヘッダ行が反転",
    );
    assert!(!reversed(&s, ""));

    // 4th Tab → the <details> summary marker (▸ = closed, the default with no `open` attribute).
    s.tab();
    assert!(!reversed(&s, "rust"));
    s.see_styled(
        "",
        |st| st.add_modifier.contains(ratatui::style::Modifier::REVERSED),
        "フォーカス中の <details>=▸ が反転",
    );

    // 5th Tab wraps back around to the link.
    s.tab();
    s.see_styled(
        "guide",
        |st| st.add_modifier.contains(ratatui::style::Modifier::REVERSED),
        "巡回して先頭(リンク)へ戻る",
    );
    assert!(!reversed(&s, ""));

    // A mermaid fence is not covered here: `Sim` never attaches an image `Picker` (that's a
    // separate, larger change — H1 in docs/archive/AUDIT-TESTS-2026-08.md, out of scope for this task), so
    // `mermaid_image_mode()` is false and a ```mermaid fence degrades to the plain-text
    // box-drawing diagram — it never becomes an `MdItemKind::MermaidFence` Tab item at all.
    // Confirmed by hand: opening a mermaid-only doc through this same harness, `focused_item()`
    // stays `None` through every `Tab` press (there is nothing to cycle to), so there is no
    // reachable "focused mermaid caption" state to assert on here.

    std::fs::remove_dir_all(&dir).ok();
}

/// `distinct_fg_in_row`: a CSV/TSV preview's rainbow column coloring (`ui.csv_rainbow`, default on)
/// actually uses multiple distinct foreground colors across one row, not a single uniform color.
#[test]
fn e2e_csv_rainbow_columns_use_distinct_foreground_colors() {
    let dir = sandbox("csv_rainbow_style");
    std::fs::write(dir.join("wide.csv"), "colA,colB,colC,colD\n1,2,3,4\n").unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("wide.csv");
    s.enter();
    s.see("TABLE");
    let (header_row, _) = s.find_text("colA").expect("header row is drawn");
    assert!(
        s.distinct_fg_in_row(header_row) >= 3,
        "4列のヘッダ行は3色以上の前景色が混在するはず(レインボー)"
    );
    s.key('q');
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// H3 (docs/archive/AUDIT-TESTS-2026-08.md): full `[ui]` config-field coverage table.
//
// Layer (c) from the audit: does setting a field to a NON-default value, through `App::new`,
// actually change observable behavior (screen text/style) or app-state through real keystrokes —
// as opposed to (a) TOML parses into the right default (config/parity_tests.rs) or (b) calling the
// logic directly with an argument (app/tests.rs)? `ui.keys` was the first gap found (fixed by the
// three `e2e_ui_keys_*` tests above); this table makes sure the other 41 fields aren't hiding the
// same kind of gap, and the completeness test below means a newly added field can't be silently
// left out (mirrors i18n's `ALL_MSGS` exhaustiveness check).
// =============================================================================

/// How a `[ui]` field's role is demonstrated at this layer.
#[derive(Debug, Clone, Copy)]
enum Coverage {
    /// A `Sim` test in this file sets the field to a non-default value and asserts the screen/style/
    /// app-state differs from the default — written specifically for this table (possibly alongside
    /// a pre-existing test). `.0` names the covering test(s).
    Covered(&'static str),
    /// A pre-existing `Sim` test (written before this table, for a related feature) already exercises
    /// the field with a genuine default-vs-non-default contrast. `.0` names it.
    CoveredElsewhere(&'static str),
    /// A dedicated unit/integration test outside the `Sim`/keystroke harness already drives
    /// `App::new(path, cfg)` end to end with real file I/O (so it satisfies layer (c) without going
    /// through `handle_key`). `.0` names it.
    ///
    /// (There used to be a fourth `NotObservable` category here for a field genuinely not wired up
    /// to anything observable — `show_hidden` was its only occupant, tracking the dead-config-field
    /// bug fixed in 2026-08. Every field is `Covered`/`CoveredElsewhere`/`CoveredByUnitTest` now, so
    /// the category was removed rather than left with no variant to construct it.)
    CoveredByUnitTest(&'static str),
}

const UI_CONFIG_COVERAGE: &[(&str, Coverage)] = &[
    (
        "show_hidden",
        Coverage::Covered("e2e_ui_show_hidden_config_starts_with_dotfiles_visible"),
    ),
    (
        "filter_mode",
        Coverage::Covered("e2e_ui_filter_mode_fuzzy_vs_substring_noncontiguous_query"),
    ),
    (
        "tabbar",
        Coverage::Covered("e2e_ui_tabbar_always_vs_auto_with_a_single_tab"),
    ),
    (
        "icons",
        Coverage::CoveredElsewhere("e2e_md_task_markers_render_ascii_or_nf_by_icons"),
    ),
    (
        "wrap",
        Coverage::CoveredElsewhere("e2e_md_wrapped_focus_follows_offscreen_item"),
    ),
    (
        "line_numbers",
        Coverage::Covered("e2e_ui_line_numbers_toggle_shows_gutter"),
    ),
    (
        "git_gutter",
        Coverage::Covered("e2e_ui_git_gutter_toggle_shows_or_hides_change_marker"),
    ),
    (
        "tab_width",
        Coverage::Covered(
            "e2e_ui_tab_width_changes_expansion_columns (plain text) / \
             e2e_ui_tab_width_changes_expansion_in_markdown_code_block (Markdown fence + \
             extreme values 0/usize::MAX)",
        ),
    ),
    (
        "syntax_highlight",
        Coverage::Covered("e2e_ui_syntax_highlight_toggle_changes_color_diversity"),
    ),
    (
        "preview_loading",
        Coverage::Covered("e2e_ui_preview_loading_indicator_vs_progressive_for_cold_language"),
    ),
    (
        "path_style",
        Coverage::Covered("e2e_ui_path_style_config_sets_initial_style"),
    ),
    (
        "keys",
        Coverage::CoveredElsewhere(
            "e2e_ui_keys_less_scheme_uses_plain_letters_not_ctrl / e2e_ui_keys_vim_scheme_uses_ctrl_not_plain_letters / e2e_ui_keys_unknown_value_falls_back_to_vim_scheme",
        ),
    ),
    (
        "lang",
        Coverage::Covered("e2e_ui_lang_toggle_switches_footer_translation"),
    ),
    (
        "statusbar",
        Coverage::Covered("e2e_ui_statusbar_layout_split_vs_bottom_moves_context_row"),
    ),
    (
        "theme",
        // `ThemeConfig` has 5 sub-fields; `code_bg` was the only one with a real e2e contrast
        // before the 2026-08-17 pre-release sweep added one test per remaining sub-field.
        Coverage::Covered(
            "e2e_ui_theme_code_bg_toggle_changes_code_block_background (code_bg) / \
             e2e_ui_theme_code_theme_changes_markdown_code_block_highlight_colors (code_theme) / \
             e2e_ui_theme_code_label_align_left_vs_right_moves_badge_in_markdown (code_label_align) / \
             e2e_ui_theme_code_label_bg_configurable_in_markdown (code_label_bg) / \
             e2e_ui_theme_bg_paints_the_whole_frame_background (bg)",
        ),
    ),
    (
        "image_render_scale",
        Coverage::Covered("e2e_ui_image_render_scale_shrinks_display_rect"),
    ),
    (
        "svg_max_px",
        Coverage::Covered("e2e_ui_svg_max_px_changes_apparent_display_size"),
    ),
    (
        "sort",
        Coverage::Covered("e2e_ui_sort_config_sets_initial_order_without_pressing_s"),
    ),
    (
        "details",
        Coverage::CoveredElsewhere("e2e_details_columns_show_size_and_modified"),
    ),
    (
        "graph_max_branches",
        Coverage::Covered("e2e_ui_graph_max_branches_caps_legend_entries"),
    ),
    (
        "graph_base_branches",
        Coverage::Covered("e2e_ui_graph_base_branches_config_auto_selects_base"),
    ),
    (
        "commit_meta_align",
        Coverage::Covered("e2e_ui_commit_meta_align_right_vs_inline_moves_author_column"),
    ),
    (
        "confirm_quit",
        Coverage::CoveredElsewhere(
            "e2e_last_tab_q_quits_when_confirm_off / e2e_last_tab_q_confirms_when_confirm_on",
        ),
    ),
    (
        "confirm_jj_sync",
        Coverage::CoveredByUnitTest("ui_confirm_jj_sync_gates_the_only_write"),
    ),
    (
        "confirm_bookmark_overwrite",
        Coverage::CoveredElsewhere(
            "e2e_bookmark_overwrite_prompts_confirm_then_applies / e2e_bookmark_overwrite_off_applies_silently",
        ),
    ),
    (
        "follow_view",
        Coverage::Covered(
            "e2e_follow_opens_full_screen_diff (default) / e2e_ui_follow_view_file_shows_content_preview_not_diff",
        ),
    ),
    (
        "csv_rainbow",
        Coverage::CoveredElsewhere(
            "e2e_csv_rainbow_columns_use_distinct_foreground_colors / e2e_csv_table_renders_and_navigates_without_rainbow",
        ),
    ),
    (
        "md_task_states",
        Coverage::CoveredElsewhere("e2e_markdown_task_cycles_custom_states"),
    ),
    (
        "md_autolink",
        Coverage::CoveredElsewhere(
            "e2e_markdown_autolink_and_emoji_full_pipeline (on) / e2e_markdown_autolink_emoji_alerts_toggle_off (off)",
        ),
    ),
    (
        "md_alerts",
        Coverage::CoveredElsewhere(
            "e2e_markdown_alert_all_five_types_and_aliases (on) / e2e_markdown_autolink_emoji_alerts_toggle_off (off)",
        ),
    ),
    (
        "md_emoji",
        Coverage::CoveredElsewhere(
            "e2e_markdown_autolink_and_emoji_full_pipeline (on) / e2e_markdown_autolink_emoji_alerts_toggle_off (off)",
        ),
    ),
    (
        "md_frontmatter",
        Coverage::CoveredElsewhere(
            "e2e_markdown_front_matter_renders_as_metadata (on) / e2e_markdown_front_matter_off_leaves_body_intact (off)",
        ),
    ),
    (
        "md_footnotes",
        Coverage::CoveredElsewhere(
            "e2e_markdown_footnotes_render_superscript_and_section (on) / e2e_markdown_footnotes_off_stays_literal (off)",
        ),
    ),
    (
        "md_inline_html",
        Coverage::Covered(
            "e2e_markdown_inline_html_renders (on) / e2e_ui_md_inline_html_off_keeps_raw_tags",
        ),
    ),
    (
        "md_details",
        Coverage::Covered(
            "e2e_md_html_details_collapse_and_toggle (auto) / e2e_ui_md_details_config_forces_open_despite_missing_attribute",
        ),
    ),
    (
        "math",
        Coverage::Covered("e2e_ui_math_image_mode_lifts_expression_text_mode_keeps_it_inline"),
    ),
    (
        "math_color",
        Coverage::Covered("e2e_ui_math_color_changes_rendered_pixel_hue"),
    ),
    (
        "busy_indicator",
        Coverage::Covered(
            "e2e_busy_indicator_absent_when_idle (idle invariant) / e2e_ui_busy_indicator_shows_while_media_loads_hides_when_disabled",
        ),
    ),
    (
        "mermaid",
        Coverage::Covered("e2e_ui_mermaid_image_mode_shows_caption_text_mode_does_not"),
    ),
    (
        "mermaid_theme",
        Coverage::Covered("e2e_ui_mermaid_theme_changes_rendered_pixel_brightness"),
    ),
    (
        "mermaid_rows",
        Coverage::Covered("e2e_ui_mermaid_rows_changes_reserved_diagram_height"),
    ),
    (
        "restore_tabs",
        Coverage::CoveredElsewhere(
            "e2e_session_restore_reopens_previous_tabs (on, via Sim) / session_actions::restore_tabs_off_neither_saves_nor_restores (off, via App::new)",
        ),
    ),
    (
        "restore_single_tab",
        Coverage::CoveredByUnitTest(
            "session_actions::restore_single_tab_true_saves_a_lone_tab / restore_single_tab_false_deletes_a_lone_tab_session / restore_single_tab_false_still_saves_multi_tab / restore_single_tab_false_skips_and_deletes_stale_single_tab_file",
        ),
    ),
    (
        "tree_cursor",
        Coverage::CoveredByUnitTest(
            "ascend_puts_the_cursor_on_the_directory_just_left (default \"origin\") / ascend_with_tree_cursor_top_goes_back_to_the_first_row (\"top\") / ascend_falls_back_to_the_top_when_the_directory_it_left_is_not_listed / descend_still_starts_at_the_top_of_the_new_directory (l unaffected) / ascend_treats_an_unknown_tree_cursor_value_as_origin",
        ),
    ),
];

/// Extract `UiConfig`'s field names directly from `config/mod.rs`'s own source text: locate
/// `pub struct UiConfig {`, brace-match to the closing `}`, then pull every `pub <name>:` line in
/// between. Mirrors i18n's `ALL_MSGS` exhaustiveness pattern — a newly added `[ui]` field fails
/// `ui_config_coverage_table_is_complete` below until it gets a table entry.
fn extract_ui_config_field_names() -> Vec<String> {
    let src = include_str!("config/mod.rs");
    let marker = "pub struct UiConfig {";
    let start = src
        .find(marker)
        .expect("UiConfig struct not found in config/mod.rs — did it move or get renamed?");
    let body_start = start + marker.len() - 1; // the opening '{' itself
    let bytes = src.as_bytes();
    let mut depth = 0i32;
    let mut end = body_start;
    for (i, &b) in bytes[body_start..].iter().enumerate() {
        match b {
            b'{' => depth += 1,
            b'}' => {
                depth -= 1;
                if depth == 0 {
                    end = body_start + i;
                    break;
                }
            }
            _ => {}
        }
    }
    assert!(end > body_start, "matching closing brace not found");
    let body = &src[body_start..end];
    let mut names = Vec::new();
    for line in body.lines() {
        let t = line.trim_start();
        if let Some(rest) = t.strip_prefix("pub ") {
            if let Some(colon) = rest.find(':') {
                let name = rest[..colon].trim();
                if !name.is_empty() {
                    names.push(name.to_string());
                }
            }
        }
    }
    names
}

/// Safety valve for the extractor above: if the brace/regex-ish scan above breaks (e.g. someone
/// reformats the struct in a way the simple line scan can't follow), it must fail LOUD by finding
/// too few fields — not silently return an empty/tiny list that would make
/// `ui_config_coverage_table_is_complete` vacuously pass. `UiConfig` currently has 42 fields; 35 is
/// a conservative floor that still catches "extraction basically broke."
#[test]
fn ui_config_field_extraction_finds_at_least_35_fields() {
    let names = extract_ui_config_field_names();
    assert!(
        names.len() >= 35,
        "抽出数が少なすぎる(安全弁): {} 件 — パーサが壊れている可能性: {:?}",
        names.len(),
        names
    );
}

/// Completeness: every field `UiConfig` actually has must appear in `UI_CONFIG_COVERAGE`, and vice
/// versa (no stale entries for fields that got removed/renamed). Add a field to `UiConfig` without
/// adding a table entry → this test fails and names exactly which field is missing.
#[test]
fn ui_config_coverage_table_is_complete() {
    let extracted = extract_ui_config_field_names();
    assert!(
        extracted.len() >= 35,
        "抽出数が少なすぎる(安全弁): {} 件",
        extracted.len()
    );
    let extracted: std::collections::BTreeSet<&str> =
        extracted.iter().map(|s| s.as_str()).collect();
    let table: std::collections::BTreeSet<&str> =
        UI_CONFIG_COVERAGE.iter().map(|(name, _)| *name).collect();
    let missing_from_table: Vec<&&str> = extracted.difference(&table).collect();
    let stale_in_table: Vec<&&str> = table.difference(&extracted).collect();
    assert!(
        missing_from_table.is_empty() && stale_in_table.is_empty(),
        "UiConfig のフィールドと UI_CONFIG_COVERAGE テーブルが不一致。\n\
         テーブルに無い(新規フィールド?): {missing_from_table:?}\n\
         構造体に無い(削除/改名?): {stale_in_table:?}"
    );
    // No duplicate entries either (a copy-paste of an existing field name would hide a gap).
    assert_eq!(
        table.len(),
        UI_CONFIG_COVERAGE.len(),
        "UI_CONFIG_COVERAGE に重複キーがある"
    );
}

/// Print the coverage breakdown by category (also gives every entry's payload string a real read,
/// so the enum variants document genuine data, not accidental waste). Run with `-- --nocapture` to
/// see the table; the assertions just guard against an accidentally-empty test-name string.
#[test]
fn ui_config_coverage_table_breakdown() {
    let mut covered = 0usize;
    let mut covered_elsewhere = 0usize;
    let mut covered_by_unit_test = 0usize;
    for (field, cov) in UI_CONFIG_COVERAGE {
        match cov {
            Coverage::Covered(test) => {
                assert!(!test.is_empty(), "{field}: Covered のテスト名が空");
                covered += 1;
            }
            Coverage::CoveredElsewhere(test) => {
                assert!(!test.is_empty(), "{field}: CoveredElsewhere のテスト名が空");
                covered_elsewhere += 1;
            }
            Coverage::CoveredByUnitTest(test) => {
                assert!(
                    !test.is_empty(),
                    "{field}: CoveredByUnitTest のテスト名が空"
                );
                covered_by_unit_test += 1;
            }
        }
    }
    eprintln!(
        "UI_CONFIG_COVERAGE breakdown: total={} Covered={covered} CoveredElsewhere={covered_elsewhere} CoveredByUnitTest={covered_by_unit_test}",
        UI_CONFIG_COVERAGE.len(),
    );
}

// =============================================================================
// H3: the field-specific tests referenced by UI_CONFIG_COVERAGE above (new coverage only —
// pre-existing tests referenced as CoveredElsewhere/CoveredByUnitTest live elsewhere in this file
// or in src/app/session_actions.rs).
// =============================================================================

/// FIXED (2026-08, was FINDING-2026-08): `[ui] show_hidden` used to parse fine
/// (config/parity_tests.rs) but was **never read anywhere in app/*.rs** — `tab.show_hidden` was
/// unconditionally initialized to `false` in the `PerTab` default and only ever flipped via the
/// `.` keypress (`app.rs`, `toggle_hidden`) or session restore (`session_actions.rs`), never from
/// config at `App::new`. `App::new` now captures `cfg.ui.show_hidden` before `cfg` is moved into
/// `Self` and feeds it into the fresh tab's `PerTab` (before the first `rebuild_tree`, so the very
/// first draw already reflects it). `tab_new` (`t`/`Ctrl-t`) resets to the same config value rather
/// than silently inheriting whatever the source tab's `.` toggle last left it at; session restore
/// (`apply_saved_tab`) still overwrites it afterwards, so a restored tab's saved visibility wins
/// over the config default, matching its documented persistence.
#[test]
fn e2e_ui_show_hidden_config_starts_with_dotfiles_visible() {
    let dir = sandbox("ui_show_hidden_cfg");
    seed_files(&dir);
    let root = canon(&dir);

    let s_default = Sim::new(&root); // show_hidden=false
    s_default.dont_see(".hidden.txt");

    let mut cfg = Config::default();
    cfg.ui.show_hidden = true;
    let s_on = Sim::with_config(&root, cfg);
    s_on.see(".hidden.txt"); // visible from the very first draw, no `.` key needed
    std::fs::remove_dir_all(&dir).ok();
}

/// A fresh tab (`t` → `tab_new`) starts hidden-file visibility from `[ui] show_hidden` again — it
/// does not silently inherit whatever the source tab's `.` toggle last left it at. Exercised in
/// both directions (config-on overriding a toggled-off source tab, and config-off overriding a
/// toggled-on one) so either "always inherits the previous tab" or "always resets to false" would
/// fail this.
#[test]
fn e2e_ui_show_hidden_new_tab_resets_to_config_default() {
    // config on: toggle off in tab0, then `t` — tab1 should come back on.
    let dir_on = sandbox("ui_show_hidden_new_tab_on");
    seed_files(&dir_on);
    let root_on = canon(&dir_on);
    let mut cfg_on = Config::default();
    cfg_on.ui.show_hidden = true;
    let mut s_on = Sim::with_config(&root_on, cfg_on);
    s_on.see(".hidden.txt");
    s_on.key('.'); // toggle off in tab0
    s_on.dont_see(".hidden.txt");
    s_on.key('t'); // new tab
    s_on.see(".hidden.txt"); // config default (on), not tab0's toggled-off state
    std::fs::remove_dir_all(&dir_on).ok();

    // config off (default): toggle on in tab0, then `t` — tab1 should come back off.
    let dir_off = sandbox("ui_show_hidden_new_tab_off");
    seed_files(&dir_off);
    let root_off = canon(&dir_off);
    let mut s_off = Sim::new(&root_off);
    s_off.dont_see(".hidden.txt");
    s_off.key('.'); // toggle on in tab0
    s_off.see(".hidden.txt");
    s_off.key('t'); // new tab
    s_off.dont_see(".hidden.txt"); // config default (off), not tab0's toggled-on state
    std::fs::remove_dir_all(&dir_off).ok();
}

/// The `.` toggle flips correctly from both possible starting points (config on and config off) —
/// a regression that always flips to a hardcoded state (e.g. always ending up `false`) would pass
/// the two config-default tests above (they only toggle once) but fail this.
#[test]
fn e2e_ui_show_hidden_dot_key_toggles_from_either_config_default() {
    let dir_on = sandbox("ui_show_hidden_toggle_from_on");
    seed_files(&dir_on);
    let root_on = canon(&dir_on);
    let mut cfg_on = Config::default();
    cfg_on.ui.show_hidden = true;
    let mut s_on = Sim::with_config(&root_on, cfg_on);
    s_on.see(".hidden.txt");
    s_on.key('.');
    s_on.dont_see(".hidden.txt");
    s_on.key('.'); // toggle back
    s_on.see(".hidden.txt");
    std::fs::remove_dir_all(&dir_on).ok();

    let dir_off = sandbox("ui_show_hidden_toggle_from_off");
    seed_files(&dir_off);
    let root_off = canon(&dir_off);
    let mut s_off = Sim::new(&root_off); // show_hidden=false
    s_off.dont_see(".hidden.txt");
    s_off.key('.');
    s_off.see(".hidden.txt");
    s_off.key('.'); // toggle back
    s_off.dont_see(".hidden.txt");
    std::fs::remove_dir_all(&dir_off).ok();
}

#[test]
fn e2e_ui_filter_mode_fuzzy_vs_substring_noncontiguous_query() {
    let dir = sandbox("ui_filter_mode_cfg");
    std::fs::write(dir.join("app_resolver.rs"), "x").unwrap();
    let root = canon(&dir);

    // default "fuzzy": a non-contiguous subsequence query still matches (fzf-style).
    let mut s_fuzzy = Sim::new(&root);
    s_fuzzy.key('/');
    s_fuzzy.keys("aprs");
    s_fuzzy.see("app_resolver.rs");

    // "substring": the same query is not a literal substring, so nothing matches.
    let mut cfg = Config::default();
    cfg.ui.filter_mode = "substring".into();
    let mut s_sub = Sim::with_config(&root, cfg);
    s_sub.key('/');
    s_sub.keys("aprs");
    s_sub.dont_see("app_resolver.rs");
    assert_eq!(
        s_sub.app.tab.entries.len(),
        0,
        "substring は非連続クエリに一致しないはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_tabbar_always_vs_auto_with_a_single_tab() {
    let dir = sandbox("ui_tabbar_cfg");
    seed_files(&dir);
    let root = canon(&dir);

    let s_auto = Sim::new(&root); // default "auto": 1 tab → tab bar hidden
    let label = s_auto.app.tab_label(0);
    let chip = format!("1:{label}");
    s_auto.dont_see(&chip);

    let mut cfg = Config::default();
    cfg.ui.tabbar = "always".into();
    let s_always = Sim::with_config(&root, cfg);
    s_always.see(&chip); // shown even with a single tab
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_line_numbers_toggle_shows_gutter() {
    let dir = sandbox("ui_line_numbers_cfg");
    std::fs::write(dir.join("f.txt"), "alpha\nbeta\ngamma\n").unwrap();
    let root = canon(&dir);

    let mut s_off = Sim::new(&root); // default false
    s_off.select("f.txt");
    s_off.enter();
    s_off.dont_see("  1 alpha");

    let mut cfg = Config::default();
    cfg.ui.line_numbers = true;
    let mut s_on = Sim::with_config(&root, cfg);
    s_on.select("f.txt");
    s_on.enter();
    s_on.see("  1 alpha");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_ui_git_gutter_toggle_shows_or_hides_change_marker() {
    let dir = sandbox("ui_git_gutter_cfg");
    seed_repo(&dir);
    let dir = canon(&dir);
    std::fs::write(dir.join("a.rs"), "fn a() {}\nfn a2() {}\n").unwrap(); // an uncommitted addition

    let mut cfg_on = Config::default();
    cfg_on.ui.syntax_highlight = false; // determinism: don't depend on cold/warm grammar state
    let mut s_on = Sim::with_config(&dir, cfg_on);
    s_on.select("a.rs");
    s_on.enter();
    s_on.see(""); // default git_gutter=true: the added-line marker shows

    let mut cfg_off = Config::default();
    cfg_off.ui.syntax_highlight = false;
    cfg_off.ui.git_gutter = false;
    let mut s_off = Sim::with_config(&dir, cfg_off);
    s_off.select("a.rs");
    s_off.enter();
    s_off.dont_see("");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_tab_width_changes_expansion_columns() {
    let dir = sandbox("ui_tab_width_cfg");
    std::fs::write(dir.join("f.txt"), "\tX\n").unwrap();
    let root = canon(&dir);

    let mut s4 = Sim::new(&root); // default tab_width=4: marker + 3 spaces = 4 cols
    s4.select("f.txt");
    s4.enter();
    s4.see("→   X");

    let mut cfg = Config::default();
    cfg.ui.tab_width = 8;
    let mut s8 = Sim::with_config(&root, cfg); // marker + 7 spaces = 8 cols
    s8.select("f.txt");
    s8.enter();
    s8.see("→       X");
    s8.dont_see("→   X");
    std::fs::remove_dir_all(&dir).ok();
}

/// `tab_width`, inside a MARKDOWN FENCED CODE BLOCK specifically — the test just above only ever
/// exercises a plain `.txt` file; `CodeStyle.tab_width` is threaded through
/// `render_markdown_with_images` as its own argument, separate from the standalone-code preview
/// path, so this is a genuinely different call site worth its own keystroke-level check. Also covers
/// the "極端値" (extreme value) axis this config's own field explicitly promises: `0` disables
/// expansion (raw tab char kept, no `→` marker at all) and an absurd out-of-range value safely
/// clamps back to the default (4) rather than allocating a huge buffer or hanging
/// (`config::clamp_tab_width`).
#[test]
fn e2e_ui_tab_width_changes_expansion_in_markdown_code_block() {
    let dir = sandbox("ui_tab_width_md_cfg");
    std::fs::write(dir.join("d.md"), "```rust\n\tX\n```\n").unwrap();
    let root = canon(&dir);

    let mut s4 = Sim::new(&root); // default tab_width=4: marker + 3 spaces = 4 cols
    s4.select("d.md");
    s4.enter();
    s4.see("→   X");

    let mut cfg8 = Config::default();
    cfg8.ui.tab_width = 8;
    let mut s8 = Sim::with_config(&root, cfg8); // marker + 7 spaces = 8 cols
    s8.select("d.md");
    s8.enter();
    s8.see("→       X");
    s8.dont_see("→   X");

    // 0 = expansion disabled: the raw tab character survives, no marker drawn at all.
    let mut cfg0 = Config::default();
    cfg0.ui.tab_width = 0;
    let mut s0 = Sim::with_config(&root, cfg0);
    s0.select("d.md");
    s0.enter();
    s0.dont_see("");

    // An absurd out-of-range value must not crash/hang/allocate huge — it clamps back to the
    // default (4), the same as `tab_width` unset.
    let mut cfg_huge = Config::default();
    cfg_huge.ui.tab_width = 9_223_372_036_854_775_807; // usize::MAX on a 64-bit target
    let mut s_huge = Sim::with_config(&root, cfg_huge);
    s_huge.select("d.md");
    s_huge.enter();
    s_huge.see("→   X"); // clamped to the default, not a huge allocation
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_syntax_highlight_toggle_changes_color_diversity() {
    let dir = sandbox("ui_syntax_hl_cfg");
    let path = dir.join("code.rs");
    std::fs::write(&path, "fn main() { let x = 1; }\n").unwrap();
    // Deterministic: warm the grammar ourselves so the very first render already shows real
    // syntax coloring rather than a "loading" placeholder (whose state is otherwise shared/global
    // across the whole test binary — see the comment on e2e_search_works_in_code_preview).
    crate::preview::code::warm_file("rs", &path);
    let root = canon(&dir);

    let mut s_on = Sim::new(&root); // default: syntax_highlight=true
    s_on.select("code.rs");
    s_on.enter();
    let (row_on, _) = s_on.find_text("fn main").expect("code line drawn");
    let variety_on = s_on.distinct_fg_in_row(row_on);

    let mut cfg = Config::default();
    cfg.ui.syntax_highlight = false;
    let mut s_off = Sim::with_config(&root, cfg);
    s_off.select("code.rs");
    s_off.enter();
    let (row_off, _) = s_off.find_text("fn main").expect("code line drawn");
    let variety_off = s_off.distinct_fg_in_row(row_off);

    assert!(
        variety_on > variety_off,
        "既定はハイライトで複数色/off は単色のはず: on={variety_on} off={variety_off}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_preview_loading_indicator_vs_progressive_for_cold_language() {
    // Note: the sandbox name must not contain "loading" — it would leak into the title bar path
    // and false-positive the `dont_see("loading")` check below.
    let dir = sandbox("ui_cold_lang_cfg");
    // A fabricated extension registered as `code` via a custom rule, so it is both (a) guaranteed
    // never warmed by any other test in this binary and (b) still classified as `PreviewKind::Code`
    // (an unrecognized extension with no custom rule would fall through to Text, where
    // syntax-highlight warm-up — and so `preview_loading` — never applies).
    std::fs::write(dir.join("f.zzzcoldlang"), "UNIQUEBODYTEXT123\n").unwrap();
    let root = canon(&dir);
    let rule = crate::config::Rule {
        glob: Some("*.zzzcoldlang".into()),
        builtin: Some("code".into()),
        ..Default::default()
    };

    // default "indicator": a cold grammar shows a centered loading message, not the body.
    let mut cfg_ind = Config::default();
    cfg_ind.preview.rules.insert(0, rule.clone());
    let mut s_ind = Sim::with_config(&root, cfg_ind);
    s_ind.select("f.zzzcoldlang");
    s_ind.enter();
    assert!(
        s_ind.app.is_highlight_pending(),
        "登録したての拡張子はコールドのはず"
    );
    s_ind.see("loading"); // i18n Msg::Loading = "loading…"
    s_ind.dont_see("UNIQUEBODYTEXT123");

    // "progressive": the same cold grammar shows the plain body immediately instead.
    let mut cfg_prog = Config::default();
    cfg_prog.preview.rules.insert(0, rule);
    cfg_prog.ui.preview_loading = "progressive".into();
    let mut s_prog = Sim::with_config(&root, cfg_prog);
    s_prog.select("f.zzzcoldlang");
    s_prog.enter();
    assert!(s_prog.app.is_highlight_pending());
    s_prog.see("UNIQUEBODYTEXT123");
    s_prog.dont_see("loading");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_path_style_config_sets_initial_style() {
    use crate::app::PathStyle;
    let dir = sandbox("ui_path_style_cfg");
    seed_files(&dir);
    let root = canon(&dir);
    let mut cfg = Config::default();
    cfg.ui.path_style = "full".into();
    let s = Sim::with_config(&root, cfg);
    assert_eq!(
        s.app.path_style,
        PathStyle::Full,
        "config で起動時から full のはず(既定は relative — e2e_path_style_cycles参照)"
    );
    s.see("path:abs");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_lang_toggle_switches_footer_translation() {
    let dir = sandbox("ui_lang_cfg");
    seed_files(&dir);
    let root = canon(&dir);

    let mut cfg_en = Config::default();
    cfg_en.ui.lang = "en".into();
    let s_en = Sim::with_config(&root, cfg_en);
    s_en.see("quit");

    let mut cfg_jp = Config::default();
    cfg_jp.ui.lang = "jp".into();
    let s_jp = Sim::with_config(&root, cfg_jp);
    s_jp.dont_see("quit");
    s_jp.see(""); // "終了" (quit)
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_statusbar_layout_split_vs_bottom_moves_context_row() {
    let dir = sandbox("ui_statusbar_cfg");
    seed_files(&dir);
    let root = canon(&dir);

    let s_split = Sim::new(&root); // default "split": context chip on the top row
    let (row_split, _) = s_split.find_text("TREE").expect("TREE chip shown");
    assert_eq!(row_split, 0, "split: 上段(0行目)に TREE チップ");

    let mut cfg = Config::default();
    cfg.ui.statusbar = "bottom".into();
    let s_bottom = Sim::with_config(&root, cfg);
    let (row_bottom, _) = s_bottom
        .find_text("TREE")
        .expect("TREE chip shown (combined bottom line)");
    assert_ne!(
        row_bottom, 0,
        "bottom: 上段は消え最終行にまとめて表示されるはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_theme_code_bg_toggle_changes_code_block_background() {
    let body = "```rust\nfn a(){}\n```\n";
    let (s_on, dir1) = md_preview(Config::default(), "ui_theme_bg_on", body);
    let bg_on = s_on.style_of("fn a").map(|st| st.bg);
    std::fs::remove_dir_all(&dir1).ok();

    let (s_off, dir2) = md_preview(cfg_code_bg_none(), "ui_theme_bg_off", body);
    let bg_off = s_off.style_of("fn a").map(|st| st.bg);
    std::fs::remove_dir_all(&dir2).ok();

    assert!(
        matches!(bg_on, Some(Some(ratatui::style::Color::Rgb(..)))),
        "既定のコード背景は実色のはず: {bg_on:?}"
    );
    assert_eq!(
        bg_off,
        Some(Some(ratatui::style::Color::Reset)),
        "code_bg=none は背景なし(Reset)のはず: {bg_off:?}"
    );
    assert_ne!(bg_on, bg_off);
}

/// `image_render_scale`: `App::prepare_image` (the function `ui/preview.rs` calls right before
/// drawing) shrinks the on-screen target rect proportionally when < 1.0. Calling it directly with a
/// fixed `inner` rect avoids needing to inspect drawn pixels. A picker is required (`with_picker`)
/// only for `picker.font_size()`/`is_some()` — no async worker needed since still-image decode
/// (`load_image`) has a synchronous fallback.
#[test]
fn e2e_ui_image_render_scale_shrinks_display_rect() {
    let dir = sandbox("ui_image_render_scale_cfg");
    let img = image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(
        800,
        600,
        image::Rgb([200, 50, 50]),
    ));
    img.save(dir.join("p.png")).unwrap();
    let root = canon(&dir);
    let inner = ratatui::layout::Rect::new(0, 0, 80, 20);

    let mut s_default = Sim::new(&root).with_picker(); // image_render_scale=1.0
    s_default.select("p.png");
    s_default.enter();
    let target_default = s_default
        .app
        .prepare_image(inner)
        .expect("画像がデコードされ target rect が計算されるはず");

    let mut cfg = Config::default();
    cfg.ui.image_render_scale = 0.3;
    let mut s_small = Sim::with_config(&root, cfg).with_picker();
    s_small.select("p.png");
    s_small.enter();
    let target_small = s_small
        .app
        .prepare_image(inner)
        .expect("画像がデコードされ target rect が計算されるはず");

    assert!(
        target_small.width < target_default.width && target_small.height < target_default.height,
        "image_render_scale=0.3 は既定1.0より小さい表示矩形になるはず: small={target_small:?} default={target_default:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `svg_max_px`: a lower rasterization cap produces a raster with fewer pixels, which
/// `image_layout`'s natural-size calculation (pixels ÷ font size) maps to a smaller apparent
/// on-screen cell size for the very first display of a vector image.
#[test]
fn e2e_ui_svg_max_px_changes_apparent_display_size() {
    let Some(fixture) = sample_path_or_skip("sample.svg") else {
        return;
    };
    let dir = sandbox("ui_svg_max_px_cfg");
    std::fs::copy(&fixture, dir.join("p.svg")).unwrap();
    let root = canon(&dir);
    let inner = ratatui::layout::Rect::new(0, 0, 80, 20);

    let mut s_default = Sim::new(&root).with_picker(); // svg_max_px=800
    s_default.select("p.svg");
    s_default.enter();
    let target_default = s_default
        .app
        .prepare_image(inner)
        .expect("SVG がラスタライズされ target rect が計算されるはず");

    let mut cfg = Config::default();
    cfg.ui.svg_max_px = 40;
    let mut s_small = Sim::with_config(&root, cfg).with_picker();
    s_small.select("p.svg");
    s_small.enter();
    let target_small = s_small
        .app
        .prepare_image(inner)
        .expect("SVG がラスタライズされ target rect が計算されるはず");

    assert!(
        target_small.width < target_default.width || target_small.height < target_default.height,
        "svg_max_px=40 は既定800より小さい見かけサイズになるはず: small={target_small:?} default={target_default:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_sort_config_sets_initial_order_without_pressing_s() {
    let dir = sandbox("ui_sort_cfg");
    std::fs::write(dir.join("aaa.txt"), vec![b'a'; 300]).unwrap();
    std::fs::write(dir.join("bbb.txt"), vec![b'b'; 30]).unwrap();
    std::fs::write(dir.join("ccc.txt"), vec![b'c'; 150]).unwrap();
    let root = canon(&dir);

    fn names(s: &Sim) -> Vec<&str> {
        s.app
            .tab
            .entries
            .iter()
            .map(|e| e.path.file_name().and_then(|n| n.to_str()).unwrap_or(""))
            .collect()
    }

    let s_default = Sim::new(&root); // default sort.key="name"
    assert_eq!(names(&s_default), vec!["aaa.txt", "bbb.txt", "ccc.txt"]);

    let mut cfg = Config::default();
    cfg.ui.sort.key = "size".into();
    let s_cfg = Sim::with_config(&root, cfg);
    assert_eq!(
        names(&s_cfg),
        vec!["bbb.txt", "ccc.txt", "aaa.txt"],
        "sort.key=\"size\" は起動時からサイズ順のはず(sメニュー不要)"
    );
    assert!(
        !s_cfg.app.is_sort_menu(),
        "メニューを開かず既に反映済みのはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_ui_graph_max_branches_caps_legend_entries() {
    let dir = sandbox("ui_graph_max_branches_cfg");
    seed_repo_history(&dir); // main + feature-x = 2 branches already
    let dir = canon(&dir);
    let run = |args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(&dir)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    run(&["branch", "b1"]);
    run(&["branch", "b2"]);
    run(&["branch", "b3"]); // now 5 branches total

    let mut cfg = Config::default();
    cfg.ui.graph_max_branches = 2;
    let mut s = Sim::with_config(&dir, cfg);
    s.key('o');
    s.key('g');
    s.see("hidden)");

    let mut cfg2 = Config::default();
    cfg2.ui.graph_max_branches = 0; // 0 = unlimited
    let mut s2 = Sim::with_config(&dir, cfg2);
    s2.key('o');
    s2.key('g');
    s2.dont_see("hidden)");
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_ui_graph_base_branches_config_auto_selects_base() {
    let dir = sandbox("ui_graph_base_branches_cfg");
    seed_repo(&dir);
    let root = canon(&dir);
    let base = crate::git::branch(&root).expect("has a branch after seed_repo's commit");

    let mut s_default = Sim::new(&root); // default graph_base_branches=[]: no auto base
    s_default.key('o');
    s_default.key('g');
    s_default.dont_see("⌖ base:");

    let mut cfg = Config::default();
    cfg.ui.graph_base_branches = vec![base];
    let mut s_cfg = Sim::with_config(&root, cfg);
    s_cfg.key('o');
    s_cfg.key('g');
    s_cfg.see("⌖ base:"); // auto-pinned without pressing `s`
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_ui_commit_meta_align_right_vs_inline_moves_author_column() {
    let dir = sandbox("ui_commit_meta_align_cfg");
    std::fs::create_dir_all(&dir).unwrap();
    let run = |args: &[&str]| {
        let out = std::process::Command::new("git")
            .current_dir(&dir)
            .args(args)
            .output()
            .unwrap();
        assert!(out.status.success(), "git {args:?}: {out:?}");
    };
    run(&["init", "-q", "."]);
    run(&["config", "user.email", "t@t"]);
    run(&["config", "user.name", "AuthorZZZ"]);
    std::fs::write(dir.join("a.rs"), "fn a(){}\n").unwrap();
    run(&["add", "-A"]);
    run(&["commit", "-q", "-m", "shortsubj"]);
    let dir = canon(&dir);

    let mut s_right = Sim::new(&dir); // default "right": a padded column near the right edge
    s_right.key('o');
    s_right.key('l');
    let (_, col_right) = s_right
        .find_text("AuthorZZZ")
        .expect("author shown in the log row");

    let mut cfg = Config::default();
    cfg.ui.commit_meta_align = "inline".into();
    let mut s_inline = Sim::with_config(&dir, cfg); // right after the (short) subject
    s_inline.key('o');
    s_inline.key('l');
    let (_, col_inline) = s_inline
        .find_text("AuthorZZZ")
        .expect("author shown in the log row");

    assert!(
        col_right > col_inline,
        "right は列揃え(右寄り)/inline は subject 直後(左寄り)のはず: right_col={col_right} inline_col={col_inline}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[cfg(feature = "git")]
#[test]
fn e2e_ui_follow_view_file_shows_content_preview_not_diff() {
    let dir = sandbox("ui_follow_view_file_cfg");
    seed_repo(&dir); // a.rs is tracked
    let mut cfg = Config::default();
    cfg.ui.follow_view = "file".into();
    let mut s = Sim::with_config(&canon(&dir), cfg);
    s.key('F');
    assert!(s.app.follow_enabled(), "F でフォロー ON");

    let a = s.app.tab.root.join("a.rs");
    std::fs::write(&a, "fn a() { let _x = 2; }\n").unwrap();
    assert!(s.app.follow_note_change(&a));
    s.app.follow_jump(&a);
    s.draw();

    assert!(
        !s.app.is_git_diff_preview(),
        "follow_view=\"file\" は diff でなく通常プレビューのはず(既定は diff — e2e_follow_opens_full_screen_diff参照)"
    );
    assert_eq!(s.app.tab.mode, Mode::Preview);
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_md_inline_html_off_keeps_raw_tags() {
    // The doc comment on `md_inline_html` is precise about what "off" means: "false leaves all
    // these tags to be stripped" — i.e. off doesn't restore the literal `<sub>...</sub>` tags (both
    // on and off strip them; tui-markdown always drops unrecognized tags), it just stops converting
    // the surviving text to Unicode. So the contrast is glyph-level ("2" vs "₂"), not tag-visibility.
    let body = "H<sub>2</sub>O\n";
    let mut cfg = Config::default();
    cfg.ui.md_inline_html = false;
    let (s, dir) = md_preview(cfg, "ui_inline_html_off", body);
    s.see("H2O"); // literal "2" — not converted to a Unicode subscript
    s.dont_see("H₂O");
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_md_details_config_forces_open_despite_missing_attribute() {
    let dir = sandbox("ui_md_details_cfg");
    std::fs::write(
        dir.join("d.md"),
        "<details>\n<summary>Sum</summary>\n\nBODYMARKER\n\n</details>\n",
    )
    .unwrap();
    let root = canon(&dir);

    // No `open` attribute → "auto" (default) honors it and collapses.
    let mut s_auto = Sim::new(&root);
    s_auto.select("d.md");
    s_auto.enter();
    s_auto.dont_see("BODYMARKER");

    // md_details="open" forces it open despite the missing attribute.
    let mut cfg = Config::default();
    cfg.ui.md_details = "open".into();
    let mut s_open = Sim::with_config(&root, cfg);
    s_open.select("d.md");
    s_open.enter();
    s_open.see("BODYMARKER");
    std::fs::remove_dir_all(&dir).ok();
}

/// `math`: with a picker attached (only needed for `picker.font_size()` — decode itself is
/// synchronous without `md_img_tx`), the default "image" mode lifts an inline `$...$` expression
/// onto its own reserved image placement (so the literal LaTeX text is NOT shown inline); "text"
/// mode disables extraction entirely, so the literal source stays in the paragraph text.
#[test]
fn e2e_ui_math_image_mode_lifts_expression_text_mode_keeps_it_inline() {
    let dir = sandbox("ui_math_mode_cfg");
    std::fs::write(dir.join("d.md"), "before $E=mc^2$ after\n").unwrap();
    let root = canon(&dir);

    let mut s_image = Sim::new(&root).with_picker(); // default math="image"
    s_image.select("d.md");
    s_image.enter();
    assert_eq!(
        s_image.app.md_images().len(),
        1,
        "画像モードは math 用のプレースメントを1つ持つはず"
    );
    s_image.dont_see("E=mc^2"); // lifted onto its own image row, not shown as literal text

    let mut cfg = Config::default();
    cfg.ui.math = "text".into();
    let mut s_text = Sim::with_config(&root, cfg).with_picker();
    s_text.select("d.md");
    s_text.enter();
    assert!(
        s_text.app.md_images().is_empty(),
        "text モードは画像プレースメント無しのはず"
    );
    s_text.see("$E=mc^2$");
    std::fs::remove_dir_all(&dir).ok();
}

/// `math_color`: drives the real `md_encode_worker` (the same worker `main` spawns) so the
/// rendered raster's pixel colors actually reach a halfblocks-protocol drawn cell, then reads the
/// foreground RGB channels off the drawn buffer. The default light-gray (#d0d0d0) paints
/// approximately-equal r/g/b; a saturated red (#ff0000) paints g≈b≈0 with r>0 — a signal that
/// can't be produced by anything except the glyph color itself.
#[test]
fn e2e_ui_math_color_changes_rendered_pixel_hue() {
    let dir = sandbox("ui_math_color_cfg");
    std::fs::write(dir.join("d.md"), "$E=mc^2$\n").unwrap();
    let root = canon(&dir);

    fn render_and_get_rgbs(cfg: Config, dir: &std::path::Path) -> Vec<(u8, u8, u8)> {
        let mut s = Sim::with_config(dir, cfg).with_picker();
        let (enc_tx, enc_worker_rx) = std::sync::mpsc::channel();
        let (enc_res_tx, enc_res_rx) = std::sync::mpsc::channel();
        s.app.attach_md_encoder(enc_tx);
        let picker = ratatui_image::picker::Picker::halfblocks();
        std::thread::spawn(move || crate::app::md_encode_worker(picker, enc_worker_rx, enc_res_tx));
        s.select("d.md");
        s.enter(); // decode (sync, no md_img_tx) + kick an encode request
        let res = enc_res_rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("encode worker returns a result");
        assert!(s.app.apply_md_encode(res), "現世代の結果は適用されるはず");
        s.draw(); // now the protocol is Some → the Image widget actually draws pixels
        let buf = s.term.backend().buffer();
        let mut out = Vec::new();
        for cell in buf.content().iter() {
            if let Some(ratatui::style::Color::Rgb(r, g, b)) = cell.style().fg {
                out.push((r, g, b));
            }
        }
        out
    }

    let rgbs_default = render_and_get_rgbs(Config::default(), &root); // #d0d0d0
    assert!(
        !rgbs_default.is_empty(),
        "既定色でも実ピクセルが描かれるはず"
    );
    assert!(
        rgbs_default
            .iter()
            .all(|&(r, g, b)| r.abs_diff(g) <= 2 && g.abs_diff(b) <= 2),
        "既定(#d0d0d0)は無彩色(r≈g≈b)のはず: {rgbs_default:?}"
    );

    let mut cfg_red = Config::default();
    cfg_red.ui.math_color = "#ff0000".into();
    let rgbs_red = render_and_get_rgbs(cfg_red, &root);
    assert!(!rgbs_red.is_empty(), "赤色設定でも実ピクセルが描かれるはず");
    assert!(
        rgbs_red.iter().any(|&(r, g, b)| r > 20 && g == 0 && b == 0),
        "math_color=#ff0000 は赤チャンネルのみのはず: {rgbs_red:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `mermaid`: same reasoning as `math` above — a picker unlocks classification/sizing without
/// needing the encode worker. Default "image" shows the sentinel caption and a reserved image
/// placement; "text" disables the whole image path (legacy Unicode box-drawing renders instead,
/// with no caption and no placement).
#[test]
fn e2e_ui_mermaid_image_mode_shows_caption_text_mode_does_not() {
    let dir = sandbox("ui_mermaid_mode_cfg");
    std::fs::write(
        dir.join("d.md"),
        "# Doc\n\n```mermaid\nflowchart TD\nA-->B\n```\n\nafter\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s_image = Sim::new(&root).with_picker(); // default mermaid="image"
    s_image.select("d.md");
    s_image.enter();
    s_image.see("◇ mermaid");
    assert_eq!(s_image.app.md_images().len(), 1);

    let mut cfg = Config::default();
    cfg.ui.mermaid = "text".into();
    let mut s_text = Sim::with_config(&root, cfg).with_picker();
    s_text.select("d.md");
    s_text.enter();
    s_text.dont_see("◇ mermaid");
    assert!(s_text.app.md_images().is_empty());
    std::fs::remove_dir_all(&dir).ok();
}

/// `mermaid_theme`: same driven-encode-worker technique as `math_color`. "dark" paints near-black
/// low RGB values; "neutral" paints near-white high RGB values — a difference confirmed directly
/// against `mermaid_to_svg`+`rasterize_bytes` output (204,204,204 vs 212,212,212 at a sample pixel)
/// before this test was written, then verified to actually reach the drawn buffer once the diagram
/// is sized small enough to stay fully inside the viewport (a too-tall diagram gets clipped and its
/// visible band alone doesn't reach the encode step in this harness — see mermaid_rows below).
#[test]
fn e2e_ui_mermaid_theme_changes_rendered_pixel_brightness() {
    let dir = sandbox("ui_mermaid_theme_cfg");
    std::fs::write(dir.join("d.md"), "```mermaid\nflowchart TD\nA-->B\n```\n").unwrap();
    let root = canon(&dir);

    fn render_and_get_fgs(cfg: Config, dir: &std::path::Path) -> Vec<(u8, u8, u8)> {
        let mut s = Sim::with_config(dir, cfg).with_picker();
        let (enc_tx, enc_worker_rx) = std::sync::mpsc::channel();
        let (enc_res_tx, enc_res_rx) = std::sync::mpsc::channel();
        s.app.attach_md_encoder(enc_tx);
        let picker = ratatui_image::picker::Picker::halfblocks();
        std::thread::spawn(move || crate::app::md_encode_worker(picker, enc_worker_rx, enc_res_tx));
        s.select("d.md");
        s.enter();
        let res = enc_res_rx
            .recv_timeout(std::time::Duration::from_secs(5))
            .expect("encode worker returns a result");
        assert!(s.app.apply_md_encode(res));
        s.draw();
        let buf = s.term.backend().buffer();
        let mut out = Vec::new();
        for cell in buf.content().iter() {
            if let Some(ratatui::style::Color::Rgb(r, g, b)) = cell.style().fg {
                out.push((r, g, b));
            }
        }
        out
    }

    let mut cfg_dark = Config::default();
    cfg_dark.ui.mermaid_theme = "dark".into();
    cfg_dark.ui.mermaid_rows = 6; // keep the whole block inside the 26-row test terminal
    let fg_dark = render_and_get_fgs(cfg_dark, &root);
    assert!(
        !fg_dark.is_empty(),
        "dark テーマでも実ピクセルが描かれるはず"
    );

    let mut cfg_neutral = Config::default();
    cfg_neutral.ui.mermaid_theme = "neutral".into();
    cfg_neutral.ui.mermaid_rows = 6;
    let fg_neutral = render_and_get_fgs(cfg_neutral, &root);
    assert!(
        !fg_neutral.is_empty(),
        "neutral テーマでも実ピクセルが描かれるはず"
    );

    let avg = |v: &[(u8, u8, u8)]| -> f64 {
        v.iter()
            .map(|&(r, g, b)| (r as f64 + g as f64 + b as f64) / 3.0)
            .sum::<f64>()
            / v.len() as f64
    };
    let avg_dark = avg(&fg_dark);
    let avg_neutral = avg(&fg_neutral);
    assert!(
        avg_dark < avg_neutral,
        "dark は neutral より暗い(低RGB)はず: dark_avg={avg_dark:.1} neutral_avg={avg_neutral:.1}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `mermaid_rows`: the reserved cell box (`ImagePlacement.rows`) scales directly with the target
/// row count, independent of any encode/rendering — a picker alone (for `font_size()`) is enough.
#[test]
fn e2e_ui_mermaid_rows_changes_reserved_diagram_height() {
    let dir = sandbox("ui_mermaid_rows_cfg");
    std::fs::write(
        dir.join("d.md"),
        "```mermaid\nflowchart TD\nA-->B-->C-->D-->E-->F\n```\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s_default = Sim::new(&root).with_picker(); // default mermaid_rows=24
    s_default.select("d.md");
    s_default.enter();
    let rows_default = s_default.app.md_images()[0].rows;

    let mut cfg = Config::default();
    cfg.ui.mermaid_rows = 4;
    let mut s_small = Sim::with_config(&root, cfg).with_picker();
    s_small.select("d.md");
    s_small.enter();
    let rows_small = s_small.app.md_images()[0].rows;

    assert!(
        rows_small < rows_default,
        "mermaid_rows=4 は既定24より小さい高さになるはず: small={rows_small} default={rows_default}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

#[test]
fn e2e_ui_busy_indicator_shows_while_media_loads_hides_when_disabled() {
    let Some(fixture) = sample_path_or_skip("sample.svg") else {
        return;
    };
    let dir = sandbox("ui_busy_indicator_cfg");
    std::fs::copy(&fixture, dir.join("p.svg")).unwrap();
    let root = canon(&dir);

    // default busy_indicator=true: while media is loading, the spinner+label shows.
    let (tx_on, rx_on) = std::sync::mpsc::channel();
    let mut s_on = Sim::new(&root).with_picker();
    s_on.app.attach_media_loader(tx_on);
    std::mem::forget(rx_on); // never drained: media_loading stays true for this frame
    s_on.select("p.svg");
    s_on.enter();
    assert!(s_on.app.is_media_loading());
    s_on.see("loading media");

    // busy_indicator=false: the same in-flight state, but the indicator is suppressed.
    let (tx_off, rx_off) = std::sync::mpsc::channel();
    let mut cfg = Config::default();
    cfg.ui.busy_indicator = false;
    let mut s_off = Sim::with_config(&root, cfg).with_picker();
    s_off.app.attach_media_loader(tx_off);
    std::mem::forget(rx_off);
    s_off.select("p.svg");
    s_off.enter();
    assert!(s_off.app.is_media_loading());
    s_off.dont_see("loading media");
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// H1: real-keystroke-driven async media pipeline (`Sim::with_media`).
//
// Every test above that touches an inline image / mermaid fence / math expression uses
// `with_picker()`, which attaches a `Picker` but no loader channels — so `ensure_md_image` /
// `ensure_mermaid_fence_render` / `ensure_math_render` all take their **synchronous, no-tx**
// fallback branch (decode happens inline, on the test's own thread, before the first draw even
// finishes). That's fine for testing classification/sizing/config, but it never reaches the
// `if let Some(tx) = ...` branches that spawn a **real background thread** and hand the result
// back through a channel — the exact code path `main` uses, and the one where two real bugs (a
// synthetic-URL key going unrecognized, and RaTeX's invisible-black glyphs) shipped in v0.16.0
// without a single test — unit or e2e — ever exercising it.
//
// `with_media()` attaches the real channels (`attach_media_loader` / `attach_md_image_loader` /
// `attach_remote_md_loader` / `attach_md_encoder` + a real `md_encode_worker` thread), so the
// tests below drive that pipeline from actual keystrokes (`Sim::key`/`enter`/`tab`) end to end:
// decode on a worker thread → `drain_md_images` applies it and redraws → the redraw's renderer
// asks for an encode → `drain_md_encodes` applies that and redraws → real pixels reach the drawn
// terminal buffer, read back exactly like `e2e_ui_math_color_changes_rendered_pixel_hue` already
// does for the encode half alone.
// =============================================================================

/// Every `Color::Rgb` currently painted into the drawn buffer's cell foregrounds. Real
/// image/diagram/equation rasters (halfblocks protocol) paint actual pixel colors this way; plain
/// text/UI chrome uses named `Color` variants, never `Rgb`. Used to tell "a real raster reached the
/// screen" apart from "still a placeholder/blank" without needing to inspect `App`'s private cache.
fn drawn_rgb_fgs(term: &Terminal<TestBackend>) -> Vec<(u8, u8, u8)> {
    let buf = term.backend().buffer();
    let mut out = Vec::new();
    for cell in buf.content().iter() {
        if let Some(ratatui::style::Color::Rgb(r, g, b)) = cell.style().fg {
            out.push((r, g, b));
        }
    }
    out
}

/// A **local** inline Markdown image, decoded and encoded through the real background threads.
/// The placement (cols/rows) is available immediately after `enter()` (it's sized straight off the
/// file's on-disk dimensions, synchronously — no worker needed for that part), but the raster only
/// exists once `ensure_md_image`'s real decode thread reports back through `md_img_rx`, and a
/// drawable protocol only exists once the real `md_encode_worker` thread reports back through
/// `md_enc_rx`. Confirms both by reading real pixel colors off the drawn buffer, not just "a
/// `Protocol` object exists" (which a bug in the color pipeline could satisfy while still being
/// visually wrong).
#[test]
fn e2e_ui_inline_local_image_decodes_and_encodes_through_real_worker_threads() {
    let dir = sandbox("inline_local_image_real_workers");
    // A solid, distinctive color: any pixel that reaches the screen unblended is unambiguous proof
    // this specific raster (not some other placeholder/UI chrome) was drawn.
    image::DynamicImage::ImageRgb8(image::RgbImage::from_pixel(
        300,
        120,
        image::Rgb([10, 210, 10]),
    ))
    .save(dir.join("pic.png"))
    .unwrap();
    std::fs::write(
        dir.join("d.md"),
        "before\n\n![a green square](pic.png)\n\nafter\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();
    let placements = s.app.md_images();
    assert_eq!(
        placements.len(),
        1,
        "サイズはファイルの寸法から即座に(デコード前でも)確定するはず"
    );
    let (url, cols, rows) = (
        placements[0].url.clone(),
        placements[0].cols,
        placements[0].rows,
    );
    assert!(cols > 0 && rows > 0);
    assert!(
        s.app.md_image_proto(&url, cols, rows, 0, rows).is_none(),
        "デコードが終わる前はまだ protocol が無いはず"
    );

    s.drain_md_images(); // real decode thread applies the raster + redraws (which requests an encode)
    s.drain_md_encodes(); // real encode-worker thread turns it into a drawable protocol

    assert!(
        s.app.md_image_proto(&url, cols, rows, 0, rows).is_some(),
        "エンコードが終われば protocol が立つはず"
    );
    let greens: Vec<_> = drawn_rgb_fgs(&s.term)
        .into_iter()
        .filter(|&(r, g, b)| g > 150 && r < 60 && b < 60)
        .collect();
    assert!(
        !greens.is_empty(),
        "実ピクセル(緑)が描画バッファに届いているはず: {:?}",
        drawn_rgb_fgs(&s.term)
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Every kitty image id **transmitted** into the drawn terminal buffer (`i=<id>,a=T` heads a
/// transmit escape). That is the quantity a kitty terminal charges its image-storage budget
/// against, so it is the honest way to ask "is konoma filling the terminal up?".
fn transmitted_kitty_ids(term: &Terminal<TestBackend>) -> Vec<u32> {
    let dump: String = term
        .backend()
        .buffer()
        .content()
        .iter()
        .map(|c| c.symbol())
        .collect();
    dump.split("i=")
        .skip(1)
        .filter_map(|part| part.split_once(",a=T"))
        .filter_map(|(id, _)| id.parse::<u32>().ok())
        .collect()
}

/// Write a `frames`-frame animated GIF, each frame a distinct solid color.
fn write_animated_gif(path: &std::path::Path, colors: &[[u8; 3]]) {
    use image::codecs::gif::GifEncoder;
    use image::{Delay, Frame, Rgba, RgbaImage};
    let file = std::fs::File::create(path).unwrap();
    let mut enc = GifEncoder::new(file);
    enc.set_repeat(image::codecs::gif::Repeat::Infinite)
        .unwrap();
    for c in colors {
        let img = RgbaImage::from_pixel(120, 60, Rgba([c[0], c[1], c[2], 255]));
        enc.encode_frame(Frame::from_parts(
            img,
            0,
            0,
            Delay::from_numer_denom_ms(10, 1),
        ))
        .unwrap();
    }
}

/// **The regression the whole fix exists for**, driven through the real renderer on a kitty
/// terminal: open a Markdown document with an animated GIF in it and let the animation run several
/// laps, exactly as the run loop does (`advance_md_gifs_if_due` → redraw → the redraw asks for the
/// new frame's encode → apply → redraw).
///
/// Before the fix every frame was transmitted under a fresh `rand::random()` id, so the terminal
/// accumulated one whole image per frame (~66 MB/min measured) until it hit its storage budget and
/// evicted a still image that had scrolled out of view — which konoma would never re-transmit, so
/// the still came back blank after a few minutes. The observable is therefore not bytes or timing
/// but the *number of distinct ids the terminal is handed*, read straight off the drawn buffer.
///
/// The unit-level sibling (`inline_gif_animation_never_transmits_a_second_kitty_image_id`) pins the
/// same invariant one layer down; this one additionally covers `overlay_inline_images`, i.e. that
/// the real draw path is what emits those transmits.
#[test]
fn e2e_ui_animated_inline_gif_reuses_one_kitty_image_id_across_laps() {
    let dir = sandbox("inline_gif_kitty_id");
    let colors = [[220, 20, 20], [20, 220, 20], [20, 20, 220]];
    write_animated_gif(&dir.join("anim.gif"), &colors);
    std::fs::write(dir.join("d.md"), "before\n\n![anim](anim.gif)\n\nafter\n").unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media_kitty();
    s.select("d.md");
    s.enter();
    s.drain_md_images(); // the decode thread hands back all frames
    s.drain_md_encodes(); // the encode worker turns frame 0 into a drawable image

    let placements = s.app.md_images();
    assert_eq!(placements.len(), 1, "GIF が1枚配置される");
    let (url, cols, rows) = (
        placements[0].url.clone(),
        placements[0].cols,
        placements[0].rows,
    );
    assert!(
        s.app
            .md_image_proto(&url, cols, rows, 0, rows)
            .is_some_and(|i| i.is_kitty()),
        "kitty 端末では konoma 自前の転送経路を通る"
    );

    let mut ids: std::collections::BTreeSet<u32> =
        transmitted_kitty_ids(&s.term).into_iter().collect();
    assert_eq!(ids.len(), 1, "最初の描画で1枚だけ転送される");
    let mut transmits = ids.len();

    // Three laps of a three-frame animation, each step the run loop's own sequence.
    for _ in 0..(colors.len() * 3) {
        while !s.app.advance_md_gifs_if_due() {
            // The very first tick only starts the frame timer; the 10ms delays then elapse on
            // their own. Looping (rather than sleeping a fixed amount) keeps this independent of
            // how fast the machine is.
            std::hint::spin_loop();
        }
        s.draw(); // the redraw notices the new frame and requests its encode
        s.drain_md_encodes(); // ...which the worker returns, and drain redraws with it
        let frame_ids = transmitted_kitty_ids(&s.term);
        transmits += frame_ids.len();
        ids.extend(frame_ids);
    }

    // Vacuity guard: without real re-transmits the assertion below would hold trivially.
    assert!(
        transmits >= colors.len() * 3,
        "コマごとに転送が起きている: {transmits}"
    );
    assert_eq!(
        ids.len(),
        1,
        "何周させても端末に載る画像 ID は1つのまま(= 画像ストレージが埋まらない): {ids:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Two ```mermaid fences, both rendered on real background threads: Tab-focuses the caption line
/// (only reachable once the fence has actually decoded to an `Image` slot — while `Loading`, a
/// fence produces no `MdItemKind::MermaidFence` Tab item at all, so this also closes the
/// previously-noted "mermaid focus is unreachable without a picker" gap), opens it full-screen with
/// Enter, and confirms the **correct** fence opened by its source ordinal — not by draw order or
/// completion order (the two real decode threads can finish in either order; `fence_ord` is
/// assigned during the synchronous, single-pass source parse, so it's stable regardless). `q`
/// restores the exact scroll/focus that were active before opening the diagram.
#[test]
fn e2e_ui_mermaid_fence_tab_enter_opens_correct_ordinal_fullscreen_q_returns() {
    let dir = sandbox("mermaid_fence_real_async");
    std::fs::write(
        dir.join("d.md"),
        "# Doc\n\n```mermaid\nflowchart TD\nA-->B\n```\n\nmiddle text\n\n```mermaid\nflowchart TD\nC-->D\n```\n\ntail\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter(); // both fences kick off a real decode thread (Loading — no image placement yet)
    assert!(
        s.app.md_images().is_empty(),
        "デコード前はまだ画像プレースメント無し"
    );
    assert_eq!(
        s.app.focused_item(),
        None,
        "Loading中はまだ Tab で辿れるアイテムが無いはず"
    );

    // Both fences finish decoding (the two threads' completion order is not guaranteed, but two
    // drains always land both entries in md_image_cache, and fence_ord is source-order regardless).
    s.drain_md_images();
    s.drain_md_images();
    let placements = s.app.md_images();
    assert_eq!(
        placements.len(),
        2,
        "両方の図が画像プレースメントを持つはず"
    );

    // Tab to the first fence (ordinal 0) and open it.
    s.tab();
    assert_eq!(s.app.focused_item(), Some(0));
    let scroll_before = s.app.tab.preview_scroll;
    let focus_before = s.app.focused_item();
    s.enter();
    assert!(
        matches!(
            s.app.tab.preview_kind,
            Some(crate::preview::PreviewKind::MermaidFence(0))
        ),
        "1つ目のTabアイテムは序数0の図を開くはず: {:?}",
        s.app.tab.preview_kind
    );
    s.drain_media(); // the fullscreen re-render (MediaJob::MermaidSrc) is a real thread too
    assert!(
        s.app
            .prepare_image(ratatui::layout::Rect::new(0, 0, 80, 20))
            .is_some(),
        "フルスクリーン描画用のラスタが実際にデコードされているはず"
    );

    s.key('q');
    assert!(
        matches!(
            s.app.tab.preview_kind,
            Some(crate::preview::PreviewKind::Markdown(_))
        ),
        "q でMarkdownへ戻るはず: {:?}",
        s.app.tab.preview_kind
    );
    assert_eq!(
        s.app.tab.preview_scroll, scroll_before,
        "スクロール位置が復元されるはず"
    );
    assert_eq!(
        s.app.focused_item(),
        focus_before,
        "フォーカスが復元されるはず"
    );

    // Tab to the second fence (ordinal 1) and open it — the actual "correct ordinal" regression
    // check: a draw-order/completion-order-based ordinal would also happen to say "1" here by
    // coincidence with exactly two fences, but this exercises the same `open_mermaid_fence` path
    // that re-extracts the fence by its **source** ordinal. `q` above restored focus to item 0, so
    // a single Tab now reaches item 1.
    s.tab();
    assert_eq!(s.app.focused_item(), Some(1));
    s.enter();
    assert!(
        matches!(
            s.app.tab.preview_kind,
            Some(crate::preview::PreviewKind::MermaidFence(1))
        ),
        "2つ目のTabアイテムは序数1の図を開くはず: {:?}",
        s.app.tab.preview_kind
    );
    s.drain_media();
    assert!(s
        .app
        .prepare_image(ratatui::layout::Rect::new(0, 0, 80, 20))
        .is_some());
    std::fs::remove_dir_all(&dir).ok();
}

/// A math expression, decoded and encoded through the real background threads — exactly the path
/// where v0.16.0 shipped two real bugs invisible to every test that existed at the time: (1) the
/// synthetic `math://` key wasn't recognized by the "should this URL be resolved on disk" check, so
/// an encode was never even requested (the reserved row stayed permanently blank); (2) RaTeX paints
/// every glyph pure black, invisible on a dark terminal. Both regressions are pinned here by
/// actually going through `ensure_math_render`'s real-thread branch (previously unreached) and
/// reading real pixel colors — a `Some(url)` synthetic key alone (bug 1) or a `Some(protocol)`
/// alone (bug 2, since black paints a technically-valid protocol) would not have caught either.
#[test]
fn e2e_ui_math_expression_decodes_through_real_worker_and_reserves_image_row() {
    let dir = sandbox("math_real_worker");
    std::fs::write(dir.join("d.md"), "before $E=mc^2$ after\n").unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();
    // While loading: no placement yet, and the literal LaTeX is not shown as plain text (it's on
    // its own reserved "loading" row, not inline with "before"/"after").
    assert!(s.app.md_images().is_empty());
    s.dont_see("E=mc^2");
    s.see("loading");

    s.drain_md_images(); // real background render (ensure_math_render's Some(tx) branch)
    let placements = s.app.md_images();
    assert_eq!(
        placements.len(),
        1,
        "デコード完了後に画像プレースメントが立つはず"
    );
    let (url, cols, rows) = (
        placements[0].url.clone(),
        placements[0].cols,
        placements[0].rows,
    );
    assert!(
        crate::preview::markdown::is_math_url(&url),
        "合成キー math:// が使われているはず: {url}"
    );

    // Bug 1's regression: with the synthetic key unrecognized, `ensure_md_image` would try to
    // resolve `math://...` as a real file path, fail, and never even ask for an encode — this
    // `drain_md_encodes` would then hang (5s timeout → test failure) instead of completing fast.
    s.drain_md_encodes();

    assert!(
        s.app.md_image_proto(&url, cols, rows, 0, rows).is_some(),
        "エンコード完了後は protocol が立つはず"
    );
    // Bug 2's regression: RaTeX's default glyph color is pure black (0,0,0) — invisible on
    // konoma's dark terminal. `recolor` repaints it (default `math_color` #d0d0d0), so real,
    // non-black ink must reach the drawn buffer.
    let visible_ink: Vec<_> = drawn_rgb_fgs(&s.term)
        .into_iter()
        .filter(|&(r, g, b)| r > 20 || g > 20 || b > 20)
        .collect();
    assert!(
        !visible_ink.is_empty(),
        "純黒(不可視)でない実インクが描かれるはず: {:?}",
        drawn_rgb_fgs(&s.term)
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Failure path (design principle #3): when the real background render fails — a mermaid fence
/// mermaid-rs-renderer can't lay out, and a math expression RaTeX can't parse — the async result
/// still arrives (never silently drops) and must degrade safely to the raw source text: never stuck
/// on "loading…" forever, and never a crash. Both failures go through the very same
/// `md_img_tx`/`apply_md_image` real-thread pipeline as the success-path tests above; only the
/// renderer's `Result` differs.
#[test]
fn e2e_ui_mermaid_and_math_render_failure_degrades_safely_to_raw_source() {
    let dir = sandbox("mermaid_math_failure_real_worker");
    std::fs::write(
        dir.join("d.md"),
        "```mermaid\ndefinitely not a diagram !!!\n```\n\n$\\frac{$\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();
    s.see("loading"); // both start out as loading placeholders

    // Both async renders finish (with failures). Each `drain_md_images` also redraws, which is
    // what turns a `failed` cache entry back into the text fallback (`apply_md_image` invalidates
    // `md_cache` on both success *and* failure, so the very next render re-decorates).
    s.drain_md_images();
    s.drain_md_images();

    assert!(
        s.app.md_images().is_empty(),
        "失敗した図/式は画像プレースメントを持たないはず"
    );
    s.dont_see("loading…");
    // mermaid: mermaid-rs-renderer fails on garbage, and so does the legacy text renderer,
    // degrading all the way to the raw fence source (`fallback_raw`).
    s.see("definitely not a diagram");
    // math: RaTeX fails on unbalanced LaTeX, degrading to the raw `$...$` text.
    s.see("\\frac{");
    std::fs::remove_dir_all(&dir).ok();
}

/// An animated GIF embedded inline in Markdown, decoded through the real background decode thread
/// (`decode_gif_inline`, keeping every frame — the inline-specific budget, separate from the
/// full-screen GIF path) and cycled via `advance_md_gifs_if_due`/`md_gif_poll_timeout` (the inline
/// analog of the full-screen `App::advance_gif_if_due`/`gif_poll_timeout`, previously never
/// exercised through a real decode). Two solid, distinctly-colored frames make the actual frame
/// swap unambiguous from the drawn buffer's pixel colors, not just an internal frame-index bump.
#[test]
fn e2e_ui_inline_gif_animates_through_real_decode_worker() {
    let dir = sandbox("inline_gif_real_worker");
    {
        use image::codecs::gif::GifEncoder;
        use image::{Delay, Frame, Rgba, RgbaImage};
        let out = std::fs::File::create(dir.join("anim.gif")).unwrap();
        let mut enc = GifEncoder::new(out);
        let frames = [Rgba([220, 20, 20, 255]), Rgba([20, 20, 220, 255])] // red, then blue
            .into_iter()
            .map(|color| {
                Frame::from_parts(
                    RgbaImage::from_pixel(120, 80, color),
                    0,
                    0,
                    Delay::from_numer_denom_ms(100, 1),
                )
            });
        enc.encode_frames(frames).unwrap();
    }
    std::fs::write(dir.join("d.md"), "![anim](anim.gif)\n").unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();
    let placements = s.app.md_images();
    assert_eq!(placements.len(), 1);
    let (url, cols, rows) = (
        placements[0].url.clone(),
        placements[0].cols,
        placements[0].rows,
    );

    s.drain_md_images(); // real decode thread: decode_gif_inline finds 2 frames
    assert!(
        s.app.md_gif_poll_timeout().is_some(),
        "アニメGIFは巡回のポーリング待ちを要求するはず"
    );
    s.drain_md_encodes(); // frame 0 (red)'s protocol

    assert!(s.app.md_image_proto(&url, cols, rows, 0, rows).is_some());
    let reds: Vec<_> = drawn_rgb_fgs(&s.term)
        .into_iter()
        .filter(|&(r, g, b)| r > 150 && g < 60 && b < 60)
        .collect();
    assert!(
        !reds.is_empty(),
        "1コマ目(赤)の実ピクセルが描かれるはず: {:?}",
        drawn_rgb_fgs(&s.term)
    );

    assert!(
        !s.app.advance_md_gifs_if_due(),
        "最初の呼び出しはタイマーを起動するだけで、まだ進めないはず"
    );
    std::thread::sleep(std::time::Duration::from_millis(150)); // > the 100ms per-frame delay
    assert!(
        s.app.advance_md_gifs_if_due(),
        "遅延経過後は次フレームへ進むはず"
    );
    s.draw(); // the frame swap invalidated proto_size → the renderer requests a fresh encode
    s.drain_md_encodes(); // frame 1 (blue)'s protocol

    let blues: Vec<_> = drawn_rgb_fgs(&s.term)
        .into_iter()
        .filter(|&(r, g, b)| b > 150 && r < 60 && g < 60)
        .collect();
    assert!(
        !blues.is_empty(),
        "2コマ目(青)へ切り替わった実ピクセルが描かれるはず: {:?}",
        drawn_rgb_fgs(&s.term)
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// RAII guard: pins `NO_PROXY` to `*` for its lifetime and restores whatever the ambient
/// environment had (or clears it) when dropped — including on an early return via a failed
/// `assert!`/panic, so a test failure never leaves the process-global var permanently mutated for
/// whatever else runs in the same test binary afterward. See `NoProxyGuard::new`'s doc for *why*.
struct NoProxyGuard(Option<std::ffi::OsString>);

impl NoProxyGuard {
    /// Isolates the real fetch (`fetch_remote_bytes_capped` in app.rs) from whatever proxy the
    /// *ambient* environment happens to configure. `ureq`'s `Config::default()` — which
    /// `fetch_remote_bytes_capped` goes through unconditionally, it never calls `.proxy(..)` itself
    /// — bakes in `Proxy::try_from_env()` at agent-construction time: if
    /// `ALL_PROXY`/`HTTPS_PROXY`/`HTTP_PROXY` (any casing) is set, a request meant to stay on
    /// loopback is actually routed through that proxy instead of connecting directly, and
    /// connection-refused-in-~1ms turns into "however long the real proxy takes to answer or time
    /// out" — up to `fetch_remote_bytes_capped`'s 20s
    /// `timeout_global`, so a proxied environment would flake or hang the calling test, and worse,
    /// a *reachable* proxy could make a "without ever reaching a real network" test actually reach
    /// one. `NO_PROXY=*` (checked by `ureq` before any of the proxy vars are consulted — see
    /// `Proxy::is_no_proxy`) makes every host bypass the proxy unconditionally, restoring the
    /// loopback-only guarantee regardless of what the environment running the suite has configured.
    fn new() -> Self {
        let saved = std::env::var_os("NO_PROXY");
        std::env::set_var("NO_PROXY", "*");
        Self(saved)
    }
}

impl Drop for NoProxyGuard {
    fn drop(&mut self) {
        match self.0.take() {
            Some(v) => std::env::set_var("NO_PROXY", v),
            None => std::env::remove_var("NO_PROXY"),
        }
    }
}

/// A remote (http/https) inline Markdown image, fetched by the real background thread
/// (`attach_remote_md_loader`) — **without ever reaching a real network**: `http://127.0.0.1:1/...`
/// fails via connection-refused near-instantly (confirmed separately: ~1ms, well under the
/// `recv_timeout`), since nothing can bind privileged port 1, and the request never leaves the
/// loopback interface. Confirms loading → failure → text-placeholder degrade (principle #3)
/// through actual keystrokes and the real fetch thread, not the config-level synchronous-failure
/// path (`[external] remote_images = false`) unit tests already cover. `NoProxyGuard` (above)
/// makes that "never reaching a real network" claim hold regardless of the ambient proxy env.
#[test]
fn e2e_ui_remote_image_fetch_failure_without_network_degrades_to_placeholder() {
    let _no_proxy = NoProxyGuard::new();

    let dir = sandbox("remote_image_no_network");
    std::fs::write(
        dir.join("d.md"),
        "![unreachable](http://127.0.0.1:1/nope.png)\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();
    s.see("loading");
    assert!(s.app.md_images().is_empty());

    s.drain_remote(); // real fetch thread: connection refused instantly, ok=false

    s.dont_see("loading");
    s.see("http://127.0.0.1:1/nope.png"); // degraded to the text-fallback line (image_text_fallback)
    assert!(
        s.app.md_images().is_empty(),
        "失敗した画像はプレースメントを持たないはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// The reported bug, end to end through real key input: a document shaped like `rand_chacha`'s
/// README — a footnote definition wrapped onto a second line — plus an ordinary fenced code block.
/// On the released build `y c` was refused for the *whole* document, because the phantom code block
/// the unclosed footnote left behind made the screen and the source scanner disagree about how many
/// code blocks there were. The payload is the one the reporting user was actually trying to copy.
#[test]
fn e2e_yc_copies_code_in_a_document_with_a_multiline_footnote() {
    let payload = "/groundwork <仕様書> <NotionURL> <資料dir>";
    let doc = format!(
        "Intro[^1] text.\n\n```sh\n{payload}\n```\n\n[^1]: [label](\n      https://example.com/x)\n"
    );
    let (mut s, dir) = md_preview(cfg_code_bg_none(), "fnote", &doc);

    // Tab to the code block (the only focusable code item in the document).
    let mut found = false;
    for _ in 0..8 {
        s.tab();
        if s.app.md_focused_code() {
            found = true;
            break;
        }
    }
    assert!(found, "コードブロックにフォーカスできる\n{}", s.screen());
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some(payload),
        "`y c` が拒否されず、ファイルどおりの中身を返す"
    );

    // And through the copy leader exactly as a user types it: `y` then `c`.
    s.key('y');
    s.see("code block");
    s.key('c');
    assert!(
        !s.app
            .flash
            .as_deref()
            .unwrap_or_default()
            .contains("couldn't"),
        "コピーが拒否されていない(flash={:?})",
        s.app.flash
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// The other half of the same bug: the phantom code block itself. A wrapped footnote definition used
/// to leave its continuation line behind in the body, where the indentation turned it into an
/// indented code block on screen, and the footnote section showed a truncated, unclosed link. Both
/// the stray block and the broken link must be gone — the continuation belongs to the footnote.
#[test]
fn e2e_multiline_footnote_leaves_no_phantom_code_block() {
    let doc = "Intro[^1] text.\n\n[^1]: [label](\n      https://example.com/x)\n";
    let (s, dir) = md_preview(cfg_code_bg_none(), "phantom", doc);
    let screen = s.screen();
    assert!(
        !screen.contains(''),
        "本文に幽霊コードブロックが出ていない\n{screen}"
    );
    // The definition is in the footnotes section as a *working* link. Links render label-only (the
    // URL is collapsed out and recovered into the item), so the proof is the recovered target: on
    // the released build the wrapped URL was stranded in the body and the link was left unclosed,
    // so no target like this existed at all.
    assert!(
        screen.contains("label"),
        "脚注の節に定義が入っている\n{screen}"
    );
    assert_eq!(
        s.app.md_link_targets(),
        vec!["https://example.com/x".to_string()],
        "折り返された URL まで含めて1本のリンクとして復元される\n{screen}"
    );
    // Nothing focusable is a code block any more (there is no code in this document at all).
    let mut s = s;
    for _ in 0..6 {
        s.tab();
        assert!(
            !s.app.md_focused_code(),
            "コードブロックとして巡回対象になっていない\n{}",
            s.screen()
        );
    }
    std::fs::remove_dir_all(&dir).ok();
}

/// `y c` must keep working on a document the pre-passes give a code block the *file* does not have.
/// `<del></del>` alone on a line rewrites to `~~~~`, a tilde fence opener — so the screen shows a
/// code block that scanning the raw file cannot find. This is the same class as the reported
/// multi-line-footnote bug but survives that fix, which is why the copy scanner reads the text the
/// renderer parsed instead of re-reading the file.
#[test]
fn e2e_yc_copies_when_preprocessing_creates_the_code_block() {
    let doc = "before\n\n<del></del>\n\nafter\n";
    let (mut s, dir) = md_preview(cfg_code_bg_none(), "delfence", doc);
    let mut found = false;
    for _ in 0..6 {
        s.tab();
        if s.app.md_focused_code() {
            found = true;
            break;
        }
    }
    assert!(
        found,
        "前処理が作ったコードブロックにフォーカスできる\n{}",
        s.screen()
    );
    assert!(
        s.app.focused_code_text().is_some(),
        "`y c` が拒否されない(生ファイルを読むと 0 個に見えて全拒否になる)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Report 1: an inline animated GIF keeps ticking forever after leaving Preview. `md_image_cache` is
/// a flat, path-keyed cache that `enter_preview` only clears when switching to a genuinely
/// *different* file (`if !same_file`, `src/app.rs`) — nothing clears it just because the mode moved
/// back to Tree via `q` (`back_to_tree`, also in `src/app.rs`, never touches it). Since
/// `md_gif_poll_timeout`/`advance_md_gifs_if_due` (both in `src/app/md_media.rs`) scan every cache
/// entry unconditionally, the run loop keeps waking up (≤100ms) and redrawing the *tree* forever even
/// though nothing on screen is animating. Drives the exact same real-thread GIF pipeline as
/// `e2e_ui_inline_gif_animates_through_real_decode_worker`, then leaves Preview with the real `q`
/// keystroke (the same entry point the run loop uses).
#[test]
fn e2e_ui_inline_gif_stops_polling_after_leaving_preview() {
    let dir = sandbox("inline_gif_stops_on_tree");
    {
        use image::codecs::gif::GifEncoder;
        use image::{Delay, Frame, Rgba, RgbaImage};
        let out = std::fs::File::create(dir.join("anim.gif")).unwrap();
        let mut enc = GifEncoder::new(out);
        let frames = [Rgba([220, 20, 20, 255]), Rgba([20, 20, 220, 255])]
            .into_iter()
            .map(|color| {
                Frame::from_parts(
                    RgbaImage::from_pixel(120, 80, color),
                    0,
                    0,
                    Delay::from_numer_denom_ms(100, 1),
                )
            });
        enc.encode_frames(frames).unwrap();
    }
    std::fs::write(dir.join("d.md"), "![anim](anim.gif)\n").unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();
    s.drain_md_images(); // real decode thread: decode_gif_inline finds 2 frames
    s.drain_md_encodes(); // frame 0's protocol
    assert!(
        s.app.md_gif_poll_timeout().is_some(),
        "プレビュー中はアニメGIFのポーリング待ちが立つはず"
    );

    s.key('q'); // Action::PreviewBack -> back_to_tree, the same key the run loop dispatches
    assert_eq!(s.app.tab.mode, Mode::Tree, "q でツリーへ戻るはず");
    assert!(
        s.app.md_gif_poll_timeout().is_none(),
        "ツリーへ戻った後は GIF ポーリングが止まるはず(報告1): {:?}",
        s.app.md_gif_poll_timeout()
    );
    assert!(
        !s.app.advance_md_gifs_if_due(),
        "ツリー表示中はコマ送りも起きないはず(報告1)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Report 1, tab-switch companion: switching to a *different* file within the same tab already goes
/// through `enter_preview`'s `if !same_file { self.md_image_cache.clear() }`, which is the one place
/// that genuinely empties the cache. Pins that this path keeps working once `back_to_tree`/mode
/// gating is added elsewhere — a change scoped to `md_gif_poll_timeout`/`advance_md_gifs_if_due`
/// alone must not weaken this existing behavior.
#[test]
fn e2e_ui_inline_gif_stops_polling_after_opening_a_different_file() {
    let dir = sandbox("inline_gif_stops_on_switch");
    {
        use image::codecs::gif::GifEncoder;
        use image::{Delay, Frame, Rgba, RgbaImage};
        let out = std::fs::File::create(dir.join("anim.gif")).unwrap();
        let mut enc = GifEncoder::new(out);
        let frames = [Rgba([220, 20, 20, 255]), Rgba([20, 20, 220, 255])]
            .into_iter()
            .map(|color| {
                Frame::from_parts(
                    RgbaImage::from_pixel(120, 80, color),
                    0,
                    0,
                    Delay::from_numer_denom_ms(100, 1),
                )
            });
        enc.encode_frames(frames).unwrap();
    }
    std::fs::write(dir.join("d.md"), "![anim](anim.gif)\n").unwrap();
    std::fs::write(dir.join("other.txt"), "plain text\n").unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();
    s.drain_md_images();
    s.drain_md_encodes();
    assert!(s.app.md_gif_poll_timeout().is_some());

    s.key('q');
    s.select("other.txt");
    s.enter();
    assert!(
        s.app.md_gif_poll_timeout().is_none(),
        "別ファイルを開けば GIF ポーリングは止まるはず(既存挙動の非退行)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Report 2: every math expression in a document spawns its own OS thread the instant it's first
/// scanned (`ensure_math_render`, `src/app/md_media.rs`), with no cap on how many run at once. A
/// document with many distinct `$...$` expressions therefore calls `std::thread::spawn` that many
/// times synchronously inside the very first `ensure_md_cache` pass (`src/app/md_render.rs`), which
/// runs on the UI thread before the first frame can draw. Confirmed by a *state* count — how many
/// entries land in the cache in this one pass, i.e. how many renders were actually kicked off — not a
/// wall-clock timing (`md_image_cache` is private to `app`; the count is read through the
/// `#[cfg(test)]` accessor added alongside this test in `src/app/md_media.rs`).
#[test]
fn e2e_ui_math_render_requests_are_bounded_not_all_at_once() {
    let dir = sandbox("math_render_bounded");
    let exprs: String = (0..80)
        .map(|i| format!("$a{i}$"))
        .collect::<Vec<_>>()
        .join("\n\n");
    std::fs::write(dir.join("d.md"), exprs).unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter(); // the very first ensure_md_cache pass — exactly where an unbounded implementation
               // calls std::thread::spawn 80 times in a row before this line returns
    let inserted = s.app.md_image_cache_len();
    assert!(
        inserted < 80,
        "80個の式が一度に全部スレッド起動されてはいけない(報告2): 挿入数={inserted}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Report 2, convergence companion: capping concurrency must not silently strand the expressions
/// beyond the cap in `Loading` forever. Drains only the decode channel (placement existence depends
/// on decode+layout, not on the separate protocol-encode step, which — like a real inline image — is
/// only requested for currently *visible* placements and would never arrive for most of these 40
/// off-screen ones, hanging the test) in rounds, mirroring the run loop's per-tick drain, until every
/// expression has a placement. Bounds the number of rounds so a regression that stops re-kicking
/// skipped entries (e.g. capping without ever retrying) fails loudly instead of hanging.
#[test]
fn e2e_ui_math_render_cap_eventually_renders_every_expression() {
    let dir = sandbox("math_render_cap_converges");
    let n = 40;
    let exprs: String = (0..n)
        .map(|i| format!("$a{i}$"))
        .collect::<Vec<_>>()
        .join("\n\n");
    std::fs::write(dir.join("d.md"), exprs).unwrap();
    let root = canon(&dir);

    let mut s = Sim::new(&root).with_media();
    s.select("d.md");
    s.enter();

    // Drain decode results in rounds until every expression is placed, or give up after a generous
    // number of rounds (well beyond ceil(n / cap) for any sane cap).
    let mut rounds = 0;
    while s.app.md_images().len() < n && rounds < n * 4 {
        s.drain_md_images();
        rounds += 1;
    }
    assert_eq!(
        s.app.md_images().len(),
        n,
        "上限を設けても、いずれ全ての式がレンダリングされ切るはず(報告2の修正が新しいスタックを生まない)"
    );
    std::fs::remove_dir_all(&dir).ok();
}

// =============================================================================
// Config-axis x real-keystroke sweep (final pre-release audit): fills the specific gaps found by
// cross-referencing `UI_CONFIG_COVERAGE` (H3, above) against every sub-field of the `theme` entry
// (only `code_bg` had a real e2e contrast; `code_theme`/`code_label_align`/`code_label_bg`/`bg` did
// not) plus a handful of concrete config x keystroke combinations `docs/archive/AUDIT-TESTS-2026-08.md`'s
// own H1/H3 gaps didn't happen to cover: `mermaid_rows` at a value LARGER than the default (only
// smaller was tested), `md_details = "closed"`, `icons` on the alert-header glyph, `y c` code-block
// copy from inside a plain (non-alert) blockquote and an open `<details>`, checkbox toggling in a
// loose list and inside an *open* `<details>` (only *collapsed* was tested), manual scroll keys
// (`j`/`g`/`G`) under both `wrap` values, Markdown-specific state surviving a tab switch, and one
// stress test with every Markdown toggle off at once.
// =============================================================================

/// `theme.code_theme`: the bundled two-face syntax theme used inside a Markdown fenced code block.
/// Only `code_bg` (not `code_theme`) had a real config x keystroke contrast before this — the
/// `UI_CONFIG_COVERAGE` table's `theme` entry names one test for the whole 5-field `ThemeConfig`.
/// Changing the theme name must repaint the keyword's own foreground color (confirmed against real
/// two-face palettes: TwoDark = Zed One Dark's purple keyword vs Dracula's pink).
#[test]
fn e2e_ui_theme_code_theme_changes_markdown_code_block_highlight_colors() {
    let dir = sandbox("ui_code_theme_md");
    std::fs::write(
        dir.join("d.md"),
        "```rust\nfn add(x: i32) -> i32 { x + 1 }\n```\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s_default = Sim::new(&root); // theme.code_theme = "TwoDark" (default)
    s_default.select("d.md");
    s_default.enter();
    let fg_default = s_default
        .style_of("fn")
        .and_then(|st| st.fg)
        .expect("既定テーマでも `fn` キーワードは色付けされるはず");

    let mut cfg = Config::default();
    cfg.ui.theme.code_theme = "Dracula".into();
    let mut s_dracula = Sim::with_config(&root, cfg);
    s_dracula.select("d.md");
    s_dracula.enter();
    let fg_dracula = s_dracula
        .style_of("fn")
        .and_then(|st| st.fg)
        .expect("Dracula テーマでも `fn` キーワードは色付けされるはず");

    assert_ne!(
        fg_default, fg_dracula,
        "code_theme を変えると Markdown コードブロック内のキーワード色が変わるはず: \
         TwoDark={fg_default:?} Dracula={fg_dracula:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `theme.code_label_align`: the language badge (`" rust "`) at the top of a fenced code block is
/// right-aligned by default and left-aligned (right after the gutter) when set to `"left"`.
#[test]
fn e2e_ui_theme_code_label_align_left_vs_right_moves_badge_in_markdown() {
    let dir = sandbox("ui_code_label_align_md");
    std::fs::write(dir.join("d.md"), "```rust\nfn f() {}\n```\n").unwrap();
    let root = canon(&dir);

    let mut s_right = Sim::new(&root); // theme.code_label_align = "right" (default)
    s_right.select("d.md");
    s_right.enter();
    let (_, col_right) = s_right
        .find_text(" rust ")
        .expect("既定=右寄せの言語バッジが見えるはず");

    let mut cfg = Config::default();
    cfg.ui.theme.code_label_align = "left".into();
    let mut s_left = Sim::with_config(&root, cfg);
    s_left.select("d.md");
    s_left.enter();
    let (_, col_left) = s_left
        .find_text(" rust ")
        .expect("left=左寄せの言語バッジが見えるはず");

    assert!(
        col_left < 10,
        "left はガター直後(左端寄り)に描かれるはず: col={col_left}"
    );
    assert!(
        col_right > col_left + 20,
        "right は left より十分右に描かれるはず: right={col_right} left={col_left}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `theme.code_label_bg`: `"auto"` (default) gives the language badge a brightened background; an
/// explicit color overrides it; `"none"` (combined with `code_bg="none"` — see the in-test comment)
/// removes it entirely (dim italic text on no background instead).
#[test]
fn e2e_ui_theme_code_label_bg_configurable_in_markdown() {
    let dir = sandbox("ui_code_label_bg_md");
    std::fs::write(dir.join("d.md"), "```rust\nfn f() {}\n```\n").unwrap();
    let root = canon(&dir);

    let mut s_auto = Sim::new(&root); // theme.code_label_bg = "auto" (default)
    s_auto.select("d.md");
    s_auto.enter();
    let bg_auto = s_auto
        .style_of(" rust ")
        .and_then(|st| st.bg)
        .expect("既定(auto)はバッジに背景色があるはず");

    // An explicit color overrides "auto"'s derived brightened shade.
    let mut cfg_red = Config::default();
    cfg_red.ui.theme.code_label_bg = "#ff0000".into();
    let mut s_red = Sim::with_config(&root, cfg_red);
    s_red.select("d.md");
    s_red.enter();
    let bg_red = s_red.style_of(" rust ").and_then(|st| st.bg);
    assert_eq!(
        bg_red,
        Some(ratatui::style::Color::Rgb(0xff, 0, 0)),
        "明示色を設定するとバッジの背景がそれになるはず: auto={bg_auto:?} red={bg_red:?}"
    );
    assert_ne!(Some(bg_auto), bg_red, "auto と明示色は異なる色のはず");

    // `code_label_bg="none"` alone is NOT enough to observe "no background at all": `code_header`'s
    // own final step (`markdown.rs`) applies `line.style(Style::new().bg(bg))` for the WHOLE header
    // line whenever `code_bg` (the surrounding code block's own background, a SEPARATE setting) is
    // still `Some` — a badge span with no `.bg()` of its own then still shows the code block's base
    // color through that line-level style. Isolating "truly no background" requires BOTH set to
    // "none" together.
    let mut cfg_none = Config::default();
    cfg_none.ui.theme.code_label_bg = "none".into();
    cfg_none.ui.theme.code_bg = "none".into();
    let mut s_none = Sim::with_config(&root, cfg_none);
    s_none.select("d.md");
    s_none.enter();
    // Same `Color::Reset`-sentinel caveat as `e2e_ui_theme_bg_paints_the_whole_frame_background`:
    // ratatui's own `Cell::default()` bg is `Some(Color::Reset)`, not a literal `None` — so "no
    // explicit background was painted" is the absence of an `Rgb` variant, not `Option::is_none()`.
    let bg_none = s_none.style_of(" rust ").and_then(|st| st.bg);
    assert!(
        !matches!(bg_none, Some(ratatui::style::Color::Rgb(..))),
        "code_label_bg=none かつ code_bg=none はバッジに明示的な Rgb 背景が無いはず: {bg_none:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `theme.bg`: paints the WHOLE frame's background (`ui::render`'s own base coat, applied before any
/// widget draws — see that call site's own doc comment), so it reaches a blank cell in the tree view
/// too, not just Markdown — but it is exercised here specifically because this whole section audits
/// every `ThemeConfig` sub-field, and this is the only one with zero prior real e2e contrast.
#[test]
fn e2e_ui_theme_bg_paints_the_whole_frame_background() {
    let dir = sandbox("ui_theme_bg_cfg");
    std::fs::write(dir.join("d.md"), "hello\n").unwrap();
    let root = canon(&dir);

    // Default theme.bg = "none": a blank tree-view cell carries no EXPLICIT (Rgb) background paint
    // — `ratatui::buffer::Cell::default()`'s own bg is `Some(Color::Reset)` (a sentinel meaning "no
    // color set," not a literal `None`), so the real signal that `theme.bg` did NOT run its base
    // coat (`ui::render`'s own `frame.buffer_mut().set_style(area, Style::new().bg(bg))`) is the
    // ABSENCE of an `Rgb` variant, not `Option::is_none()`.
    let s_default = Sim::new(&root);
    let bg_default = s_default.cell_style(3, 85).bg;
    assert!(
        !matches!(bg_default, Some(ratatui::style::Color::Rgb(..))),
        "既定(none)は明示的な Rgb 背景を塗らないはず: {bg_default:?}"
    );

    let mut cfg = Config::default();
    cfg.ui.theme.bg = "#123456".into();
    let s_painted = Sim::with_config(&root, cfg);
    let bg_painted = s_painted.cell_style(3, 85).bg;
    assert_eq!(
        bg_painted,
        Some(ratatui::style::Color::Rgb(0x12, 0x34, 0x56)),
        "theme.bg は画面全体の背景として塗られるはず: {bg_painted:?}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `mermaid_rows`: the pre-existing `e2e_ui_mermaid_rows_changes_reserved_diagram_height` only ever
/// compares a SMALLER value (4) against the default (24) — both well under the standard 90x26
/// terminal's own viewport clamp (measured directly: `mermaid_fit_rows` caps at
/// `preview_viewport - 2` = 20 rows there, so anything >= 22 clamps to the exact same 20 as the
/// default and a "larger value" contrast would be unobservable). This uses a taller terminal
/// (`with_config_sized`) so the cap never engages, and confirms a LARGER explicit value actually
/// grows the diagram past the default — the other half of "小さい値/大きい値" this config's own doc
/// comment promises.
#[test]
fn e2e_ui_mermaid_rows_large_value_grows_diagram_beyond_default() {
    let dir = sandbox("ui_mermaid_rows_large_cfg");
    std::fs::write(
        dir.join("d.md"),
        "```mermaid\nflowchart TD\nA-->B-->C-->D-->E-->F-->G-->H\n```\n",
    )
    .unwrap();
    let root = canon(&dir);

    let mut s_default = Sim::with_config_sized(&root, Config::default(), 90, 60).with_picker();
    s_default.select("d.md");
    s_default.enter();
    assert!(
        s_default.app.preview_viewport_for_test() > 26,
        "前提: 広い viewport で mermaid_rows=24(既定) がクランプされないはず: viewport={}",
        s_default.app.preview_viewport_for_test()
    );
    let rows_default = s_default.app.md_images()[0].rows;

    let mut cfg_large = Config::default();
    cfg_large.ui.mermaid_rows = 48;
    let mut s_large = Sim::with_config_sized(&root, cfg_large, 90, 60).with_picker();
    s_large.select("d.md");
    s_large.enter();
    let rows_large = s_large.app.md_images()[0].rows;

    assert!(
        rows_large > rows_default,
        "mermaid_rows=48 は既定24より大きい高さになるはず(十分な viewport下): \
         large={rows_large} default={rows_default}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `md_details = "closed"`: forces a `<details>` block collapsed even when its `open` attribute
/// says otherwise (the mirror image of the pre-existing `"open"` override test) — the "auto"/"open"
/// axis was covered, "closed" was not.
#[test]
fn e2e_ui_md_details_closed_config_forces_collapse_despite_open_attribute() {
    let dir = sandbox("ui_md_details_closed_cfg");
    std::fs::write(
        dir.join("d.md"),
        "<details open>\n<summary>Sum</summary>\n\nBODYMARKER\n\n</details>\n",
    )
    .unwrap();
    let root = canon(&dir);

    // "auto" (default) honors the `open` attribute → expanded.
    let mut s_auto = Sim::new(&root);
    s_auto.select("d.md");
    s_auto.enter();
    s_auto.see("BODYMARKER");

    // md_details="closed" forces it collapsed even though the attribute says open.
    let mut cfg = Config::default();
    cfg.ui.md_details = "closed".into();
    let mut s_closed = Sim::with_config(&root, cfg);
    s_closed.select("d.md");
    s_closed.enter();
    s_closed.dont_see("BODYMARKER");
    std::fs::remove_dir_all(&dir).ok();
}

/// `icons`: a GitHub alert's header carries a Nerd Font icon before the label when `icons=true`
/// (`alert_header_line`), shifting the label's own screen column right by the icon-glyph + space
/// width. `icons` was already covered elsewhere via the task-marker glyph, but never against the
/// alert header specifically.
#[test]
fn e2e_ui_icons_alert_header_glyph_shifts_label_column() {
    let dir = sandbox("ui_icons_alert_cfg");
    std::fs::write(dir.join("d.md"), "> [!NOTE]\n> body\n").unwrap();
    let root = canon(&dir);

    let mut cfg_off = Config::default();
    cfg_off.ui.icons = false;
    let mut s_off = Sim::with_config(&root, cfg_off);
    s_off.select("d.md");
    s_off.enter();
    let (_, col_off) = s_off
        .find_text("Note")
        .expect("icons=false でもラベルテキストは見えるはず");

    let mut s_on = Sim::new(&root); // default icons=true
    s_on.select("d.md");
    s_on.enter();
    let (_, col_on) = s_on
        .find_text("Note")
        .expect("icons=true でもラベルテキストは見えるはず");

    assert!(
        col_on > col_off,
        "icons=true は Nerd Font アイコン分だけラベルが右にずれるはず: on={col_on} off={col_off}"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// `y c` inside a PLAIN blockquote (no `[!TYPE]` header — a genuine `Quote`, not an alert). Every
/// existing `y c` coverage is at top level or inside a list item; a quote-nested code block is a
/// distinct code path (`render_code_block_dispatch` reached through `render_quote` → `render_block`,
/// documented as "an intentional superset of what `parser_code_blocks` reports" in
/// `model::BlockKind::CodeBlock`'s own doc comment) that had no keystroke-level coverage at all.
#[test]
fn e2e_md_code_block_inside_plain_blockquote_is_focusable_and_copyable() {
    let dir = sandbox("ui_md_quote_code_cfg");
    std::fs::write(
        dir.join("d.md"),
        "> Intro line.\n>\n> ```rust\n> fn quoted() {}\n> ```\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("d.md");
    s.enter();
    s.see("rust"); // the fence's own bar-prefixed language header
    s.tab(); // the only focusable item: the fenced code block
    assert!(
        s.app.md_focused_code(),
        "引用内のコードブロックにフォーカスできるはず"
    );
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("fn quoted() {}"),
        "引用の `>` を含まない生ソースが取得できるはず"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "y c でコピー通知が出るはず");
    std::fs::remove_dir_all(&dir).ok();
}

/// `y c` inside an OPEN `<details>` block — Tab must land on the summary first, then the nested code
/// block, and the copied text must be the bar-free raw source (`render_bar_prefixed_body`'s own
/// isolation, documented at length on that function).
#[test]
fn e2e_md_code_block_inside_open_details_is_focusable_and_copyable() {
    let dir = sandbox("ui_md_details_code_cfg");
    std::fs::write(
        dir.join("d.md"),
        "<details open>\n<summary>Sum</summary>\n\n```python\ndef f():\n    pass\n```\n\n</details>\n",
    )
    .unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("d.md");
    s.enter();
    s.see("python"); // the fence's language header, inside the open details body

    // Document order: summary, then the code block.
    s.tab();
    assert!(
        s.app.md_focused_details().is_some(),
        "1番目=summary(details) のはず"
    );
    s.tab();
    assert!(
        s.app.md_focused_code(),
        "2番目=details 内のコードブロックのはず"
    );
    assert_eq!(
        s.app.focused_code_text().as_deref(),
        Some("def f():\n    pass"),
        "details のバー(▏)を含まない生ソースが取得できるはず"
    );
    s.key('y');
    s.key('c');
    assert!(s.app.flash.is_some(), "y c でコピー通知が出るはず");
    std::fs::remove_dir_all(&dir).ok();
}

/// A LOOSE list (a blank line between items) of task checkboxes — the shape that used to panic
/// tui-markdown (`loose_list_task_item_does_not_panic`, v0.8.0), and that the block-model renderer
/// drew with the list marker and the item's own text on two separate rows, leaving the checkbox as
/// literal `[ ]`: no icon, no focus, no toggle. `render_item` now keeps a task item's checkbox on
/// the marker's own row whether the list is tight or loose, so every checkbox behaves the same way
/// regardless of the blank lines around it.
#[test]
fn e2e_task_toggle_works_in_a_loose_list() {
    let dir = sandbox("ui_md_loose_list_should_work_cfg");
    let body = "- [ ] one\n\n- [ ] two\n\n- [ ] three\n";
    std::fs::write(dir.join("d.md"), body).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("d.md");
    s.enter();
    let read = || std::fs::read_to_string(dir.join("d.md")).unwrap();

    s.tab();
    s.key(' ');
    assert!(read().contains("- [x] one"), "1番目: {}", read());
    s.dont_see("couldn't toggle checkbox");

    s.tab();
    s.key(' ');
    assert!(read().contains("- [x] two"), "2番目: {}", read());
    assert!(
        read().contains("- [x] one"),
        "1番目は変化しないまま: {}",
        read()
    );

    s.tab();
    s.key(' ');
    assert!(read().contains("- [x] three"), "3番目: {}", read());
    s.dont_see("couldn't toggle checkbox");
    std::fs::remove_dir_all(&dir).ok();
}

/// Checkbox toggling on a checkbox that's actually visible inside an OPEN `<details>` block — the
/// complement of the pre-existing `e2e_task_toggle_works_with_a_collapsed_details_block` (which only
/// ever proves a *hidden* checkbox is correctly excluded from the scanner's count; this proves a
/// *visible* one inside an open block is correctly included and toggled).
#[test]
fn e2e_task_toggle_works_on_checkbox_inside_an_open_details_block() {
    let dir = sandbox("ui_md_details_open_task_cfg");
    std::fs::write(
        dir.join("d.md"),
        "<details open>\n<summary>Sum</summary>\n\n- [ ] inside open details\n\n</details>\n",
    )
    .unwrap();
    let mut s = Sim::with_config(&canon(&dir), Config::default());
    s.select("d.md");
    s.enter();
    let read = || std::fs::read_to_string(dir.join("d.md")).unwrap();

    // 1st focusable: the summary. 2nd: the visible checkbox inside the open body.
    s.tab();
    assert!(s.app.md_focused_details().is_some(), "1番目=summary のはず");
    s.tab();
    assert!(
        s.app.md_focused_task(),
        "2番目=開いた details 内の可視タスクのはず"
    );
    s.key(' ');
    assert!(
        read().contains("- [x] inside open details"),
        "開いた details 内のタスクがトグルされるはず: {}",
        read()
    );
    s.dont_see("couldn't toggle checkbox");
    std::fs::remove_dir_all(&dir).ok();
}

/// A checkbox inside a plain block quote (`> - [ ] x`, no `[!TYPE]` header). It used to be drawn as
/// literal text and skipped by `Tab` entirely, because `render_quote` rendered its children into the
/// surrounding `Writer` with the `>` prefix already installed, so the top-level decoration pass never
/// saw a line whose own text began with a bullet. `render_quote` now isolates, decorates, then
/// prefixes - the same order an alert's and a `<details>`'s body already used - so a quoted checkbox
/// behaves exactly like one anywhere else.
#[test]
fn e2e_task_toggle_works_inside_a_plain_blockquote() {
    let dir = sandbox("ui_md_quote_task_should_work_cfg");
    std::fs::write(dir.join("d.md"), "> - [ ] quoted task\n").unwrap();
    let mut s = Sim::with_config(&canon(&dir), Config::default());
    s.select("d.md");
    s.enter();
    let read = || std::fs::read_to_string(dir.join("d.md")).unwrap();
    s.tab();
    assert!(
        s.app.md_focused_task(),
        "引用内のタスクにフォーカスできるはず"
    );
    s.key(' ');
    assert!(
        read().contains("> - [x] quoted task"),
        "引用内のタスクがトグルされるはず: {}",
        read()
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Regression: an **ordered**-list task item (`"1. [ ] x"`) used to be pushed into
/// `RenderOut::tasks` unconditionally, the same as an unordered one — even though
/// `decorate_extras`'s own `replace_task_checkbox` never decorates one (`task_prefix_state`'s own
/// "first byte must be `-`/`*`/`+`" contract; a `push_item_marker`'s own ordered-item marker line
/// always starts `"N. "`, a digit, never a bullet — see either's own doc comment). A document
/// mixing an ordered task list *ahead of* an unordered one therefore recorded a "ghost" entry for
/// every invisible ordered checkbox, shifting every later, real, on-screen checkbox's own ordinal
/// down by one in `RenderOut::tasks` — `build_md_items`'s "Nth sentinel found on screen ==
/// `task_marks[N]`" pairing (see that field's own doc comment) then handed the **visible**
/// checkbox's own Tab-focus/`Space`-toggle the **invisible** ordered item's own byte offset
/// instead: pressing `Space` on the one checkbox actually on screen silently edited a *different*
/// line of the file. Found live on exactly this shape (2026-08), reproduced here byte-for-byte:
/// `Space` on the sole focusable checkbox must flip line 4 (`"- [ ] visible bullet"`), never line
/// 1 or 2 (the two ordered, undrawn ones) — this is the "toggle writes to the wrong line while the
/// checkbox count still agrees" class principle #3 exists to rule out, one level more subtle than a
/// pure count mismatch (which the existing safe-refusal path already catches): here the counts
/// looked fine (`task_marks.len() == 1` in production before this fix too, `render_item` never
/// having produced more than the one real GFM task on this exact document — the mis-pairing shows
/// up only once a *second*, undrawn task exists earlier in the file to steal the first slot), so a
/// naive "does the write happen at all" check would not have caught it — the position itself has to
/// be checked. The general, corpus-wide form of this check (every case in the golden-snapshot
/// corpus, not just this one document) lives in `app/tests.rs`'s own
/// `model_render_records_exactly_the_sentinels_it_draws_across_the_corpus`.
#[test]
fn e2e_ordered_list_ghost_task_does_not_steal_the_visible_checkboxs_toggle() {
    let dir = sandbox("md_ordered_ghost_task");
    let body = "1. [ ] ordered invisible\n2. [ ] ordered invisible two\n\n- [ ] visible bullet\n";
    std::fs::write(dir.join("mix.md"), body).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("mix.md");
    s.enter();

    // Exactly one checkbox is ever drawn/focusable — GFM never decorates an ordered item's own
    // checkbox, so both "1. [ ]"/"2. [ ]" lines stay literal, undrawn text (see this test's own
    // doc comment) — proven directly against `md_items`, not inferred from Tab behavior alone.
    assert_eq!(
        s.app.md_task_item_count_for_test(),
        1,
        "画面上のチェックボックスは可視の1個だけのはず"
    );

    s.tab(); // the only Tab stop for a checkbox: the visible bullet's own
    assert!(
        s.app.md_focused_task(),
        "Tab は唯一の可視チェックボックス(visible bullet)にフォーカスするはず"
    );
    s.key(' ');

    let src = std::fs::read_to_string(dir.join("mix.md")).unwrap();
    assert!(
        src.contains("- [x] visible bullet"),
        "Space はフォーカスした可視チェックボックス(4行目)をトグルするはず: {src}"
    );
    assert!(
        src.contains("1. [ ] ordered invisible\n"),
        "1行目(不可視の順序付きタスク)は変化しないはず: {src}"
    );
    assert!(
        src.contains("2. [ ] ordered invisible two\n"),
        "2行目(不可視の順序付きタスク)は変化しないはず: {src}"
    );
    s.dont_see("couldn't toggle checkbox");
    std::fs::remove_dir_all(&dir).ok();
}

/// The `Details` analog of the ordered-list-task regression above: two independent, sibling
/// top-level `<details>` blocks (both closed by default), toggled one at a time through the real
/// Tab/Space path, confirming the **first** one's own toggle only ever opens the **first** one's own
/// body — never the second's — and vice versa. Unlike a task/code-block/mermaid record,
/// `Details`'s own Tab-focus ordinal has no separate accumulator to have desynced from (it is
/// computed purely self-referentially — see `app/tests.rs`'s own
/// `model_render_records_exactly_the_sentinels_it_draws_across_the_corpus` doc comment) — the risk
/// here is the *other* sequence, `render_details_from_model`'s own `next_details_open` counter,
/// landing on the wrong slot of `details_states` for one of these two blocks; this proves it does
/// not, end to end, via the exact same `Tab`/`Space` keys a reader would press.
#[test]
fn e2e_two_sibling_details_blocks_toggle_independently() {
    let dir = sandbox("md_two_sibling_details");
    let body = "<details>\n<summary>One</summary>\n\nBODY-ONE\n\n</details>\n\n\
                <details>\n<summary>Two</summary>\n\nBODY-TWO\n\n</details>\n";
    std::fs::write(dir.join("d.md"), body).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("d.md");
    s.enter();
    s.dont_see("BODY-ONE");
    s.dont_see("BODY-TWO");

    s.tab(); // 1st Tab stop: "One"'s own summary (ordinal 0)
    assert!(
        s.app.md_focused_details().is_some(),
        "1つ目は details のはず"
    );
    s.key(' '); // open ordinal 0
    s.see("BODY-ONE");
    s.dont_see("BODY-TWO");

    s.tab(); // 2nd Tab stop: "Two"'s own summary (ordinal 1) — count of summaries is unchanged
             // by One's own body becoming visible, so this is still the very next item.
    assert!(
        s.app.md_focused_details().is_some(),
        "2つ目も details のはず"
    );
    s.key(' '); // open ordinal 1
    s.see("BODY-ONE"); // unaffected by opening the second block
    s.see("BODY-TWO");
    std::fs::remove_dir_all(&dir).ok();
}

/// Manual scroll keys (`j`/`k`/`g`/`G` — `Motion::Down/Up/Top/Bottom` on `Surface::PreviewText`,
/// which moves `preview_scroll` directly for a non-windowed decorated Markdown document) under both
/// `wrap` values. Existing coverage only ever drove scrolling indirectly via Tab-follow
/// (`e2e_md_wrapped_focus_follows_offscreen_item`); this drives it directly, the way a reader
/// actually pages through a long document without touching any link/task/code-block focus target.
#[test]
fn e2e_md_manual_scroll_moves_preview_scroll_wrap_on_and_off() {
    let mut src = String::from("# Doc\n\n");
    for i in 0..80 {
        src.push_str(&format!("paragraph line {i}\n\n"));
    }
    for (label, wrap) in [("wrap_on", true), ("wrap_off", false)] {
        let dir = sandbox(&format!("ui_md_manual_scroll_{label}"));
        std::fs::write(dir.join("d.md"), &src).unwrap();
        let mut cfg = Config::default();
        cfg.ui.wrap = wrap;
        let mut s = Sim::with_config(&canon(&dir), cfg);
        s.select("d.md");
        s.enter();
        assert_eq!(s.app.tab.preview_scroll, 0, "[{label}] 初期スクロール0");

        for _ in 0..10 {
            s.key('j');
        }
        let after_j = s.app.tab.preview_scroll;
        assert!(
            after_j > 0,
            "[{label}] j 連打でスクロールが進むはず: scroll={after_j}"
        );

        s.key('G');
        let bottom = s.app.tab.preview_scroll;
        assert!(
            bottom > after_j,
            "[{label}] G で j 連打後よりさらに末尾までスクロールするはず: \
             after_j={after_j} bottom={bottom}"
        );

        s.key('g');
        assert_eq!(s.app.tab.preview_scroll, 0, "[{label}] g で先頭へ戻るはず");
        std::fs::remove_dir_all(&dir).ok();
    }
}

/// Markdown-specific preview state (decorated-view scroll position, raw-source toggle) must survive
/// opening a new tab and switching back — `t`/`[` are declared `global` (keymap.rs), so they must
/// work while a Markdown preview (not the tree) is showing, and per-tab state must round-trip
/// through `TabState`/`PerTab`'s own snapshot/load. Distinct from
/// `e2e_tab_switch_reloads_tree_from_disk` (which only exercises the tree) and from session-restore
/// (which is a fresh-process concept, not a same-process tab switch).
///
/// The scroll-position and raw-toggle checks are two SEPARATE round trips (not one continuous
/// sequence): confirmed directly (a scratch probe) that pressing `R` itself resets
/// `tab.preview_scroll` to 0 as part of switching into raw/windowed mode — raw mode tracks its own
/// view position through a different mechanism (`FileWindow`), not `preview_scroll` — so comparing a
/// decorated-mode scroll value against what `preview_scroll` reads AFTER a raw-mode toggle would
/// conflate "the mode switch reset it" with "the tab switch lost it." Testing them independently
/// isolates what a tab switch actually does to each one.
#[test]
fn e2e_md_state_preserved_across_tab_switch() {
    let dir = sandbox("ui_md_tab_switch_state");
    let mut src = String::from("# Doc\n\n");
    for i in 0..60 {
        src.push_str(&format!("paragraph line {i}\n\n"));
    }
    std::fs::write(dir.join("d.md"), &src).unwrap();
    let mut s = Sim::new(&canon(&dir));
    s.select("d.md");
    s.enter();

    // 1) Decorated-view scroll position survives a tab switch.
    for _ in 0..10 {
        s.key('j');
    }
    let scroll_before = s.app.tab.preview_scroll;
    assert!(scroll_before > 0, "前提: スクロールが進んでいる");
    assert!(!s.app.is_raw_source(), "前提: まだ装飾表示のまま");

    s.key('t'); // open a fresh (blank) tab
    assert_eq!(s.app.tab_count(), 2, "t でタブが増える");
    s.key('['); // switch back to the original tab
    assert_eq!(s.app.active_tab_index(), 0, "[ で元のタブへ戻る");
    assert_eq!(
        s.app.tab.preview_scroll, scroll_before,
        "装飾 Markdown のスクロール位置がタブ切替後も保持されるはず: \
         before={scroll_before} after={}",
        s.app.tab.preview_scroll
    );

    // 2) The raw/decorated MODE flag itself survives a further tab switch (checked separately from
    // scroll — see this test's own doc comment for why the two can't share one round trip).
    s.key('R');
    assert!(s.app.is_raw_source(), "前提: R で raw に切替わる");
    s.key(']'); // to the tab opened above
    assert_eq!(s.app.active_tab_index(), 1, "] で新規タブへ");
    s.key('['); // back to the original (raw) tab
    assert_eq!(s.app.active_tab_index(), 0, "[ で元のタブへ戻る");
    assert!(
        s.app.is_raw_source(),
        "raw 表示モードがタブ切替後も保持されるはず"
    );
    std::fs::remove_dir_all(&dir).ok();
}

/// Every Markdown decoration toggle off at once, on one document that exercises all of them —
/// front-matter deliberately excluded (`md_frontmatter=false` does NOT restore the literal `---`
/// text: `ParseOptions::ENABLE_YAML_STYLE_METADATA_BLOCKS` is unconditionally on in tui-markdown
/// itself, see `e2e_markdown_front_matter_off_leaves_body_intact`'s own comment, so it would need a
/// style-level assertion, not a text one, and is already covered on its own). A robustness sweep:
/// disabling every toggle together must never crash and must leave every marker exactly as literal
/// text as disabling each one individually already proves — no toggle should start firing only when
/// combined with the others (e.g. an interaction between `md_alerts=false` and `md_inline_html=false`
/// changing how the alert's `[!NOTE]` marker or its body is masked).
#[test]
fn e2e_md_all_toggles_off_renders_without_crashing() {
    let dir = sandbox("ui_md_all_off_cfg");
    let body = concat!(
        "> [!NOTE]\n> alert body\n\n",
        "text[^1] with :rocket: and https://x.example and H<sub>2</sub>O\n\n",
        "[^1]: a footnote\n\n",
        "- [ ] task one\n\n",
        "```mermaid\nflowchart TD\nA-->B\n```\n\n",
        "$E=mc^2$\n",
    );
    std::fs::write(dir.join("d.md"), body).unwrap();
    let mut cfg = Config::default();
    cfg.ui.md_alerts = false;
    cfg.ui.md_autolink = false;
    cfg.ui.md_emoji = false;
    cfg.ui.md_footnotes = false;
    cfg.ui.md_inline_html = false;
    cfg.ui.mermaid = "text".into();
    cfg.ui.math = "text".into();
    cfg.ui.icons = false;
    cfg.ui.wrap = false;
    let mut s = Sim::with_config(&canon(&dir), cfg);
    s.select("d.md");
    s.enter();

    s.see("[!NOTE]"); // alert marker left literal (no callout box)
    assert!(
        s.app.md_link_targets().is_empty(),
        "autolink off: リンク無し"
    );
    s.see(":rocket:"); // emoji shortcode left literal
    s.see("https://x.example"); // bare URL not linked
    s.see("[^1]"); // footnote marker left literal
    s.see("H2O"); // <sub> tags stripped, "2" stays literal (not superscript)
    s.see("[ ] task one"); // icons=false: ASCII checkbox
    s.see("$E=mc^2$"); // math left as raw LaTeX text
    s.dont_see("can not preview"); // no crash-fallback banner anywhere
    std::fs::remove_dir_all(&dir).ok();
}