croft-software 0.2.232

VSCode-style TUI written in Rust
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
//! The explorer: the file tree, its marks, drag and drop, file operations and
//! explorer views.

use super::*;

#[test]
fn explorer_boots_with_gradient_focus_under_the_default_theme() {
    // Regression: the explorer auto-focuses at startup, but the brand-theme
    // gradient focus used to arm only on the first focus change, because the
    // tree kept its own copy that `sync_focus_flags` filled in. The tree now
    // draws with the frame's context, built from the app's theme and focus
    // as they are when it draws, so the first frame has both.
    let tmp = tempfile::tempdir().unwrap();
    let app = App::new(tmp.path().to_path_buf()).unwrap();
    assert_eq!(app.theme, crate::theme::Theme::LOCH);
    assert!(app.theme.gradient(), "the default is a brand theme");
    assert!(
        side_view_has_keys(&app, SidebarView::Explorer),
        "explorer should boot focused"
    );
}

/// #147 + #539: the PRIMARY instance's poll walks EVERY root's expanded rows,
/// so a multi-root workspace's secondary folders reach the noise classifier
/// matching neither of the primary's own roots. Measured whole, a secondary
/// folder at `<tmp>/build/second` had its `build` ancestor make every change
/// under it read as noise: the poll refreshed the tree but reported
/// `finder_relevant = false`, so the Cmd+P index stayed stale for that folder
/// for as long as it was open.
#[test]
fn poll_classifies_a_secondary_root_against_the_root_that_owns_it() {
    let tmp = tempfile::tempdir().unwrap();
    let primary = tmp.path().join("primary");
    let secondary = tmp.path().join("build/second");
    std::fs::create_dir_all(&primary).unwrap();
    std::fs::create_dir_all(&secondary).unwrap();
    let mut app = App::new(primary.clone()).unwrap();
    // The watcher is off: this is the POLL's question, and leaving events on
    // would let them answer it instead.
    app.fs_watch.disable();
    app.tree.add_root(secondary.clone());

    // Baseline every expanded dir, so the next poll sees ONLY what moves
    // after it. Without this the primary's own row is "changed" too and
    // would set `finder_relevant` on its own, hiding the secondary entirely.
    app.fs_watch.force_poll_due();
    let _ = app.fs_watch.poll(&mut app.tree, &[]);

    std::fs::write(secondary.join("a.rs"), b"fn main() {}").unwrap();
    app.fs_watch.force_poll_due();
    let poll = app.fs_watch.poll(&mut app.tree, &[]);

    assert!(
        poll.dirs_changed,
        "precondition: the write moved the secondary root's mtime"
    );
    assert!(
        poll.finder_relevant,
        "a secondary workspace folder is workspace content, whatever its own \
         path is spelled under"
    );
}

/// #1486: the poll stamps every folder a compact row folds in, so a file
/// added to `main` splits `src/main/java/com/acme` with no watcher, and
/// `disable_compact_folders` lists every level again at once.
#[test]
fn the_poll_splits_a_compact_row_and_the_setting_unfolds_it() {
    let tmp = tempfile::tempdir().unwrap();
    let src = tmp.path().join("src");
    std::fs::create_dir_all(src.join("main/java/com/acme")).unwrap();
    std::fs::write(src.join("main/java/com/acme/App.java"), "").unwrap();
    std::fs::write(tmp.path().join("README.md"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.fs_watch.disable();
    let row = app.tree.row_of(&src).unwrap();
    app.tree.select(row);
    app.tree.expand_selected();
    assert_eq!(app.tree.nodes[row].label(), "src/main/java/com/acme");
    app.focus_pane(Pane::Tree);
    assert_eq!(
        app.a11y_snapshot().item.as_deref(),
        Some("src/main/java/com/acme"),
        "a screen reader hears the row as shown"
    );
    app.fs_watch.force_poll_due();
    let _ = app.fs_watch.poll(&mut app.tree, &[]);

    std::fs::write(src.join("main/Other.java"), "").unwrap();
    app.fs_watch.force_poll_due();
    assert!(app.fs_watch.poll(&mut app.tree, &[]).dirs_changed);
    let label = |app: &App, path: &Path| app.tree.nodes[app.tree.row_of(path).unwrap()].label();
    assert_eq!(label(&app, &src), "src/main");
    assert_eq!(label(&app, &src.join("main/java")), "java/com/acme");

    app.apply_merged_settings(&crate::prefs::Prefs {
        disable_compact_folders: true,
        ..Default::default()
    });
    assert_eq!(label(&app, &src), "src");
    assert_eq!(label(&app, &src.join("main/java/com")), "com");
}

fn dir_node(path: &Path) -> crate::widgets::file_tree::Node {
    crate::widgets::file_tree::Node {
        path: path.to_path_buf(),
        depth: 1,
        is_dir: true,
        expanded: false,
        loaded: false,
        folded: 0,
    }
}

#[test]
fn tree_context_menu_on_file_offers_new_file_new_folder_then_cut_copy_rename_delete() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("hello.txt");
    std::fs::write(&f, "hi").unwrap();
    let n = file_node(&f);
    let target = f.parent().unwrap().to_path_buf();
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&f),
        &target,
        None,
        None,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert_eq!(
        labels,
        [
            "New File",
            "New Folder",
            "Cut",
            "Copy",
            "Rename",
            "Select for Compare",
            "Delete",
        ],
    );
    assert!(matches!(&items[0].1, MenuAction::Create(CreateKind::File)));
    assert!(matches!(
        &items[1].1,
        MenuAction::Create(CreateKind::Folder)
    ));
    assert!(matches!(&items[2].1, MenuAction::Cut(ps) if ps == &vec![f.clone()]));
    assert!(matches!(&items[3].1, MenuAction::Copy(ps) if ps == &vec![f.clone()]));
    assert!(matches!(&items[4].1, MenuAction::Rename(p) if p == &f));
    assert!(matches!(&items[5].1, MenuAction::SelectForCompare(p) if p == &f));
    assert!(matches!(&items[6].1, MenuAction::Delete { paths } if paths == &vec![f.clone()]));
}

#[test]
fn tree_context_menu_adds_reveal_in_finder_when_local_macos() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("hello.txt");
    std::fs::write(&f, "hi").unwrap();
    let n = file_node(&f);
    let target = f.parent().unwrap().to_path_buf();
    let mut items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&f),
        &target,
        None,
        None,
    );
    maybe_add_reveal_in_finder(&mut items, Some(f.as_path()), true);
    // Lands directly beneath the New File / New Folder block, mirroring VS
    // Code's top "reveal" placement.
    assert!(matches!(
        &items[2].1,
        MenuAction::RevealInFinder(p) if p == &f
    ));
    assert_eq!(items[2].0, "Reveal in Finder");
}

#[test]
fn tree_context_menu_omits_reveal_in_finder_when_remote() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("hello.txt");
    std::fs::write(&f, "hi").unwrap();
    let n = file_node(&f);
    let target = f.parent().unwrap().to_path_buf();
    let mut items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&f),
        &target,
        None,
        None,
    );
    // A remote SSH session disables Finder: the entry must not appear at all,
    // not even as a disabled no-op.
    maybe_add_reveal_in_finder(&mut items, Some(f.as_path()), false);
    assert!(
        !items
            .iter()
            .any(|(_, a)| matches!(a, MenuAction::RevealInFinder(_)))
    );
}

#[test]
fn tree_context_menu_omits_reveal_in_finder_when_no_entry_clicked() {
    let tmp = tempfile::tempdir().unwrap();
    let mut items = build_tree_context_menu_items(None, tmp.path(), &[], tmp.path(), None, None);
    // Right-clicking empty tree space has no path to reveal even on local macOS.
    maybe_add_reveal_in_finder(&mut items, None, true);
    assert!(
        !items
            .iter()
            .any(|(_, a)| matches!(a, MenuAction::RevealInFinder(_)))
    );
}

#[test]
fn reveal_in_finder_shortcut_hint_is_opt_cmd_r() {
    let action = MenuAction::RevealInFinder(PathBuf::from("/tmp/x"));
    assert_eq!(shortcut_for(&action), Some("⌥⌘R"));
}

#[test]
fn explorer_shortcut_predicates_recognise_expected_keys() {
    assert!(is_tree_new_file_key(key(
        KeyCode::Char('f'),
        KeyModifiers::SUPER
    )));
    assert!(is_tree_new_file_key(key(
        KeyCode::Char('F'),
        KeyModifiers::CONTROL
    )));
    assert!(!is_tree_new_file_key(key(
        KeyCode::Char('f'),
        KeyModifiers::NONE
    )));
    // Shift+Super disqualifies New File so Cmd+Shift+F can fall through to
    // the global Search jump (New Folder now lives on Cmd+Shift+N).
    assert!(!is_tree_new_file_key(key(
        KeyCode::Char('F'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT
    )));

    assert!(is_tree_new_folder_key(key(
        KeyCode::Char('N'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT
    )));
    assert!(is_tree_new_folder_key(key(
        KeyCode::Char('N'),
        KeyModifiers::CONTROL | KeyModifiers::SHIFT
    )));
    assert!(!is_tree_new_folder_key(key(
        KeyCode::Char('n'),
        KeyModifiers::SUPER
    )));
    assert!(
        !is_tree_new_folder_key(key(
            KeyCode::Char('F'),
            KeyModifiers::SUPER | KeyModifiers::SHIFT
        )),
        "Cmd+Shift+F is no longer the New Folder chord — it must fall through to the global Search jump"
    );

    assert!(is_tree_rename_key(key(
        KeyCode::Char('r'),
        KeyModifiers::SUPER
    )));
    assert!(is_tree_rename_key(key(KeyCode::F(2), KeyModifiers::NONE)));
    assert!(!is_tree_rename_key(key(
        KeyCode::Char('r'),
        KeyModifiers::NONE
    )));

    assert!(is_tree_make_root_key(key(
        KeyCode::Char('/'),
        KeyModifiers::SUPER
    )));
    assert!(is_tree_make_root_key(key(
        KeyCode::Char('/'),
        KeyModifiers::CONTROL
    )));
    assert!(!is_tree_make_root_key(key(
        KeyCode::Char('/'),
        KeyModifiers::NONE
    )));

    let cmd_shift = KeyModifiers::SUPER | KeyModifiers::SHIFT;
    let ctrl_shift = KeyModifiers::CONTROL | KeyModifiers::SHIFT;
    assert!(is_tree_make_parent_root_key(key(
        KeyCode::Char('/'),
        cmd_shift
    )));
    assert!(is_tree_make_parent_root_key(key(
        KeyCode::Char('/'),
        ctrl_shift
    )));
    assert!(is_tree_make_parent_root_key(key(
        KeyCode::Char('?'),
        KeyModifiers::SUPER
    )));
    assert!(is_tree_make_parent_root_key(key(
        KeyCode::Char('?'),
        KeyModifiers::CONTROL
    )));
    assert!(is_tree_make_parent_root_key(key(
        KeyCode::Char('?'),
        cmd_shift
    )));
    assert!(!is_tree_make_parent_root_key(key(
        KeyCode::Char('/'),
        KeyModifiers::SUPER
    )));
    assert!(!is_tree_make_parent_root_key(key(
        KeyCode::Char('/'),
        KeyModifiers::NONE
    )));
    assert!(!is_tree_make_parent_root_key(key(
        KeyCode::Char('?'),
        KeyModifiers::NONE
    )));
    assert!(!is_tree_make_root_key(key(KeyCode::Char('/'), cmd_shift)));
}

#[test]
fn tree_typeahead_first_keystroke_jumps_to_first_matching_visible_node() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join("apple")).unwrap();
    std::fs::write(tmp.path().join("banana.txt"), "").unwrap();
    std::fs::write(tmp.path().join("cherry.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.apply_tree_typeahead('b', std::time::Instant::now());
    let picked = app.tree.nodes[app.tree.selected]
        .path
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    assert_eq!(picked, "banana.txt", "typing 'b' must select banana.txt");
}

#[test]
fn tree_typeahead_accumulates_prefix_within_window() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join("test")).unwrap();
    std::fs::create_dir(tmp.path().join("toolbox")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let t0 = std::time::Instant::now();
    app.apply_tree_typeahead('t', t0);
    app.apply_tree_typeahead('e', t0 + std::time::Duration::from_millis(100));
    let name = app.tree.nodes[app.tree.selected]
        .path
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    assert_eq!(
        name, "test",
        "'te' typed within the window must land on 'test', not 'toolbox'"
    );
}

#[test]
fn tree_typeahead_resets_after_window_expires_and_cycles() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join("test")).unwrap();
    std::fs::create_dir(tmp.path().join("toolbox")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let t0 = std::time::Instant::now();
    app.apply_tree_typeahead('t', t0);
    let first = app.tree.selected;
    let first_name = app.tree.nodes[first]
        .path
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    app.apply_tree_typeahead('t', t0 + std::time::Duration::from_millis(1000));
    let second = app.tree.selected;
    let second_name = app.tree.nodes[second]
        .path
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    assert_ne!(
        first, second,
        "the second 't' after the window expires must cycle to the next 't' match"
    );
    assert!(
        first_name.starts_with('t') && second_name.starts_with('t'),
        "both cycle stops must start with 't'"
    );
}

#[test]
fn tree_typeahead_is_case_insensitive() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("README.md"), "").unwrap();
    std::fs::write(tmp.path().join("zzz.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.apply_tree_typeahead('r', std::time::Instant::now());
    let picked = app.tree.nodes[app.tree.selected]
        .path
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    assert_eq!(picked, "README.md");
}

#[test]
fn tree_typeahead_via_handle_tree_key_jumps_selection() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join("apple")).unwrap();
    std::fs::write(tmp.path().join("banana.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.focus_pane(Pane::Tree);
    app.sidebar_view = SidebarView::Explorer;
    app.handle_tree_key(key(KeyCode::Char('b'), KeyModifiers::NONE));
    let picked = app.tree.nodes[app.tree.selected]
        .path
        .file_name()
        .unwrap()
        .to_string_lossy()
        .into_owned();
    assert_eq!(
        picked, "banana.txt",
        "a plain letter routed through handle_tree_key must drive typeahead"
    );
}

#[test]
fn tree_typeahead_does_not_fire_when_modifier_held() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("banana.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let before = app.tree.selected;
    app.focus_pane(Pane::Tree);
    app.sidebar_view = SidebarView::Explorer;
    app.handle_tree_key(key(KeyCode::Char('b'), KeyModifiers::CONTROL));
    assert_eq!(
        app.tree.selected, before,
        "Ctrl+letter must not be consumed as typeahead"
    );
    assert!(
        app.tree_typeahead.is_none(),
        "Ctrl-modified keys must not seed the typeahead buffer"
    );
}

#[test]
fn cmd_shift_n_in_explorer_pane_creates_a_new_folder() {
    // With Explorer focused, Cmd+Shift+N opens the New Folder prompt
    // (the user-requested binding, moved off Cmd+Shift+F so that chord
    // can always reach the global Search jump).
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.focus_pane(Pane::Tree);
    app.sidebar_view = SidebarView::Explorer;
    app.handle_key(key(
        KeyCode::Char('N'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT,
    ))
    .unwrap();
    assert!(
        matches!(
            app.prompt.as_ref(),
            Some(Prompt {
                kind: PromptKind::Create(CreateKind::Folder),
                ..
            })
        ),
        "Explorer-focused Cmd+Shift+N must open a New Folder prompt"
    );
    assert_eq!(
        app.sidebar_view,
        SidebarView::Explorer,
        "must NOT jump to Search"
    );
}

#[test]
fn cmd_shift_f_in_explorer_pane_jumps_to_search_not_new_folder() {
    // Cmd+Shift+F is no longer the Explorer's New Folder chord — it
    // now always jumps to the Search sidebar, even when Explorer is
    // focused. New Folder lives on Cmd+Shift+N.
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.focus_pane(Pane::Tree);
    app.sidebar_view = SidebarView::Explorer;
    app.handle_key(key(
        KeyCode::Char('F'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT,
    ))
    .unwrap();
    assert!(
        app.prompt.is_none(),
        "Cmd+Shift+F must NOT open the New Folder prompt anymore"
    );
    assert_eq!(app.sidebar_view, SidebarView::Search);
}

#[test]
fn cmd_shift_f_outside_explorer_still_jumps_to_search() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.focus_pane(Pane::Editor);
    app.sidebar_view = SidebarView::Explorer;
    app.handle_key(key(
        KeyCode::Char('F'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT,
    ))
    .unwrap();
    assert_eq!(app.sidebar_view, SidebarView::Search);
}

#[test]
fn tree_context_menu_on_subfolder_offers_make_root_between_rename_and_delete() {
    // "Make root" should appear on folder right-click so the user can
    // re-root the workspace into that folder (e.g., a nested git repo).
    // It must NOT appear on file right-clicks - files can't be a
    // workspace root.
    let tmp = tempfile::tempdir().unwrap();
    let d = tmp.path().join("project_a");
    std::fs::create_dir(&d).unwrap();
    let n = dir_node(&d);
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&d),
        &d,
        None,
        None,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert!(
        labels.contains(&"Make root"),
        "folder right-click must include Make root, got {labels:?}"
    );
    let mr = items
        .iter()
        .find_map(|(_, a)| match a {
            MenuAction::MakeRoot(p) => Some(p),
            _ => None,
        })
        .expect("Make root action must carry the folder path");
    assert_eq!(mr, &d);
}

#[test]
fn tree_context_menu_on_file_does_not_offer_make_root() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("hello.txt");
    std::fs::write(&f, "hi").unwrap();
    let n = file_node(&f);
    let target = f.parent().unwrap().to_path_buf();
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&f),
        &target,
        None,
        None,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert!(
        !labels.contains(&"Make root"),
        "files cannot be roots; Make root must be hidden for files: {labels:?}"
    );
}

#[test]
fn cmd_shift_slash_on_a_folder_changes_workspace_root_to_that_folder_s_parent() {
    let tmp = tempfile::tempdir().unwrap();
    let mid = tmp.path().join("mid");
    let leaf = mid.join("leaf");
    std::fs::create_dir_all(&leaf).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let mid_idx = app.tree.nodes.iter().position(|n| n.path == mid).unwrap();
    app.tree.selected = mid_idx;
    app.tree.expand_selected();
    let leaf_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path == leaf)
        .expect("leaf must be visible after expanding mid/");
    app.tree.selected = leaf_idx;
    app.focus = Pane::Tree;
    app.sidebar_view = SidebarView::Explorer;

    let handled = app.handle_explorer_shortcut(key(KeyCode::Char('?'), KeyModifiers::SUPER));

    assert!(handled);
    assert_eq!(app.workspace_root(), mid);
    assert_eq!(app.tree.root, mid);
}

#[test]
fn cmd_shift_slash_on_the_tree_root_walks_up_one_filesystem_level() {
    let tmp = tempfile::tempdir().unwrap();
    let inner = tmp.path().join("inner");
    std::fs::create_dir(&inner).unwrap();
    let mut app = App::new(inner.clone()).unwrap();
    app.tree.selected = 0;
    app.focus = Pane::Tree;
    app.sidebar_view = SidebarView::Explorer;

    let handled = app.handle_explorer_shortcut(key(
        KeyCode::Char('/'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT,
    ));

    assert!(handled);
    assert_eq!(app.workspace_root(), tmp.path());
    assert_eq!(app.tree.root, tmp.path());
}

#[test]
fn dispatch_make_root_changes_tree_root_and_workspace_root() {
    let tmp = tempfile::tempdir().unwrap();
    let project = tmp.path().join("project_b");
    std::fs::create_dir(&project).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let original_root = app.tree.root.clone();
    app.dispatch_menu_action(MenuAction::MakeRoot(project.clone()), project.clone());
    assert_ne!(app.tree.root, original_root);
    assert_eq!(app.tree.root, project);
    assert_eq!(app.workspace_root(), project);
    // The tree's root node must reflect the new path.
    assert_eq!(app.tree.nodes[0].path, project);
}

#[test]
fn tree_context_menu_on_subfolder_offers_new_file_new_folder_then_cut_copy_rename_delete() {
    let tmp = tempfile::tempdir().unwrap();
    let d = tmp.path().join("sub");
    std::fs::create_dir(&d).unwrap();
    let n = dir_node(&d);
    let target = d.clone();
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&d),
        &target,
        None,
        None,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert_eq!(
        labels,
        [
            "New File",
            "New Folder",
            "Cut",
            "Copy",
            "Rename",
            "Make root",
            "Delete"
        ],
    );
}

/// #1174: Cut then Paste with the selection still in the cut file's folder
/// leaves the file alone and keeps the cut, so pasting on the folder it was
/// meant for still moves it there.
#[test]
fn cut_then_paste_in_the_same_folder_leaves_the_file_alone() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("notes.txt"), "notes\n").unwrap();
    std::fs::create_dir(tmp.path().join("sub")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.set_sidebar_view(SidebarView::Explorer);
    app.focus_pane(Pane::Tree);
    let select = |app: &mut App, name: &str| {
        app.tree.selected = app
            .tree
            .nodes
            .iter()
            .position(|n| n.path.file_name().is_some_and(|f| f == name))
            .unwrap_or_else(|| panic!("{name} in the tree"));
    };
    select(&mut app, "notes.txt");
    let notes = app.tree.nodes[app.tree.selected].path.clone();
    app.handle_key(key(KeyCode::Char('x'), KeyModifiers::CONTROL))
        .unwrap();
    app.handle_key(key(KeyCode::Char('v'), KeyModifiers::CONTROL))
        .unwrap();
    assert!(notes.exists(), "the file keeps its name");
    assert!(!notes.with_file_name("notes copy.txt").exists());
    assert_eq!(app.status, "Already in this folder: nothing to move");
    assert!(app.tree_clipboard.is_some(), "the cut is kept");

    select(&mut app, "sub");
    let sub = app.tree.nodes[app.tree.selected].path.clone();
    app.handle_key(key(KeyCode::Char('v'), KeyModifiers::CONTROL))
        .unwrap();
    assert!(sub.join("notes.txt").exists(), "{}", app.status);
    assert!(!notes.exists());
    assert!(app.tree_clipboard.is_none());
}

/// Negative (#1174): a cut pasted into another folder still moves the
/// file there, and a copy pasted into its own folder still makes
/// "<name> copy", as before.
#[test]
fn cut_to_another_folder_moves_and_copy_in_place_still_duplicates() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("notes.txt"), "notes\n").unwrap();
    std::fs::write(tmp.path().join("todo.txt"), "todo\n").unwrap();
    std::fs::create_dir(tmp.path().join("sub")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.set_sidebar_view(SidebarView::Explorer);
    app.focus_pane(Pane::Tree);
    let select = |app: &mut App, name: &str| {
        app.tree.selected = app
            .tree
            .nodes
            .iter()
            .position(|n| n.path.file_name().is_some_and(|f| f == name))
            .unwrap_or_else(|| panic!("{name} in the tree"));
    };
    select(&mut app, "notes.txt");
    app.handle_key(key(KeyCode::Char('x'), KeyModifiers::CONTROL))
        .unwrap();
    select(&mut app, "sub");
    app.handle_key(key(KeyCode::Char('v'), KeyModifiers::CONTROL))
        .unwrap();
    assert!(tmp.path().join("sub/notes.txt").exists(), "{}", app.status);
    assert!(!tmp.path().join("notes.txt").exists());
    assert!(app.tree_clipboard.is_none(), "a done move clears the cut");
    select(&mut app, "todo.txt");
    app.handle_key(key(KeyCode::Char('c'), KeyModifiers::CONTROL))
        .unwrap();
    app.handle_key(key(KeyCode::Char('v'), KeyModifiers::CONTROL))
        .unwrap();
    assert!(tmp.path().join("todo copy.txt").exists(), "{}", app.status);
    assert!(tmp.path().join("todo.txt").exists());
}

#[test]
fn tree_context_menu_with_clipboard_offers_paste_on_directory() {
    let tmp = tempfile::tempdir().unwrap();
    let d = tmp.path().join("sub");
    std::fs::create_dir(&d).unwrap();
    let n = dir_node(&d);
    let clip = ExplorerClipboard {
        mode: ExplorerClipMode::Cut,
        paths: vec![tmp.path().join("a.txt")],
    };
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&d),
        &d,
        Some(&clip),
        None,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert_eq!(
        labels,
        [
            "New File",
            "New Folder",
            "Cut",
            "Copy",
            "Paste",
            "Rename",
            "Make root",
            "Delete"
        ],
    );
}

#[test]
fn tree_context_menu_on_a_deeply_nested_folder_creates_inside_that_folder() {
    // Regression for the user-reported "right-click on nested
    // folders only shows Cut/Copy/Rename/Delete; I cannot create a
    // new file or folder inside the folder I clicked". The menu
    // must surface New File / New Folder, and the resolved
    // target_dir must be the right-clicked folder itself so the
    // create lands inside it (not at the workspace root).
    let tmp = tempfile::tempdir().unwrap();
    let nested = tmp.path().join("a").join("b").join("c");
    std::fs::create_dir_all(&nested).unwrap();
    let n = dir_node(&nested);
    let resolved_target = crate::widgets::file_tree::create_target_dir_for(Some(&n), tmp.path());
    assert_eq!(
        resolved_target, nested,
        "right-click on a nested folder must target the folder itself, not the workspace root",
    );
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&nested),
        &resolved_target,
        None,
        None,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert!(
        labels.contains(&"New File"),
        "nested-folder right-click must offer New File; labels = {labels:?}",
    );
    assert!(
        labels.contains(&"New Folder"),
        "nested-folder right-click must offer New Folder; labels = {labels:?}",
    );
}

/// #1294: `Ctrl`+`Z` in the Explorer opens the zoxide jump, as Cmd+Z does,
/// so a terminal that never sends Super reaches it.
#[test]
fn ctrl_z_in_the_explorer_opens_the_zoxide_jump_like_cmd_z() {
    for mods in [KeyModifiers::CONTROL, KeyModifiers::SUPER] {
        let tmp = tempfile::tempdir().unwrap();
        let mut app = App::new(tmp.path().to_path_buf()).unwrap();
        app.focus_pane(Pane::Tree);
        app.handle_key(key(KeyCode::Char('z'), mods)).unwrap();
        assert!(app.zoxide_jump.is_some(), "{mods:?}+Z opens it");
    }
}

#[test]
fn tree_context_menu_offers_compare_with_selected_when_anchor_is_present() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a.txt");
    std::fs::write(&a, "v1").unwrap();
    let b = tmp.path().join("b.txt");
    std::fs::write(&b, "v2").unwrap();
    let n = file_node(&b);
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&b),
        tmp.path(),
        None,
        Some(a.as_path()),
    );
    let kinds: Vec<&MenuAction> = items.iter().map(|(_, a)| a).collect();
    assert!(
        kinds
            .iter()
            .any(|a| matches!(a, MenuAction::CompareWithSelected { .. })),
        "Compare with Selected must be offered when an anchor is set"
    );
    assert!(
        kinds
            .iter()
            .any(|a| matches!(a, MenuAction::SelectForCompare(_))),
        "Select for Compare must always be offered for single files"
    );
}

#[test]
fn tree_context_menu_hides_compare_when_anchor_is_the_same_file() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a.txt");
    std::fs::write(&a, "v1").unwrap();
    let n = file_node(&a);
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&a),
        tmp.path(),
        None,
        Some(a.as_path()),
    );
    let kinds: Vec<&MenuAction> = items.iter().map(|(_, a)| a).collect();
    assert!(
        !kinds
            .iter()
            .any(|a| matches!(a, MenuAction::CompareWithSelected { .. })),
        "Compare with Selected must not appear against itself"
    );
}

#[test]
fn tree_context_menu_with_two_files_offers_compare_selected() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a.txt");
    std::fs::write(&a, "v1").unwrap();
    let b = tmp.path().join("b.txt");
    std::fs::write(&b, "v2").unwrap();
    let n = file_node(&a);
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        &[a.clone(), b.clone()],
        tmp.path(),
        None,
        None,
    );
    let compare = items
        .iter()
        .find(|(s, _)| s == "Compare Selected")
        .expect("Compare Selected must be offered for a two-file selection");
    assert!(matches!(
        compare.1,
        MenuAction::CompareWithSelected { ref anchor, ref other }
            if anchor == &a && other == &b
    ));
}

#[test]
fn tree_context_menu_hides_compare_selected_when_a_folder_is_selected() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a.txt");
    std::fs::write(&a, "v1").unwrap();
    let d = tmp.path().join("dir");
    std::fs::create_dir(&d).unwrap();
    let n = file_node(&a);
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        &[a.clone(), d.clone()],
        tmp.path(),
        None,
        None,
    );
    assert!(
        !items.iter().any(|(s, _)| s == "Compare Selected"),
        "Compare Selected must not appear when the selection includes a folder"
    );
}

#[test]
fn tree_context_menu_with_multi_selection_promotes_delete_count() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("hello.txt");
    std::fs::write(&f, "hi").unwrap();
    let f2 = tmp.path().join("bye.txt");
    std::fs::write(&f2, "bye").unwrap();
    let n = file_node(&f);
    let target = f.parent().unwrap().to_path_buf();
    let items = build_tree_context_menu_items(
        Some(&n),
        tmp.path(),
        &[f.clone(), f2.clone()],
        &target,
        None,
        None,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert_eq!(
        labels,
        [
            "New File",
            "New Folder",
            "Cut",
            "Copy",
            "Compare Selected",
            "Delete 2 items"
        ],
    );
}

#[test]
fn tree_context_menu_on_empty_space_offers_new_file_and_new_folder_only() {
    let tmp = tempfile::tempdir().unwrap();
    let items = build_tree_context_menu_items(None, tmp.path(), &[], tmp.path(), None, None);
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert_eq!(labels, ["New File", "New Folder"]);
    assert!(matches!(items[0].1, MenuAction::Create(CreateKind::File)));
    assert!(matches!(items[1].1, MenuAction::Create(CreateKind::Folder)));
}

#[test]
fn tree_context_menu_on_empty_space_includes_paste_when_clipboard_set() {
    let tmp = tempfile::tempdir().unwrap();
    let clip = ExplorerClipboard {
        mode: ExplorerClipMode::Copy,
        paths: vec![tmp.path().join("x.txt")],
    };
    let items = build_tree_context_menu_items(None, tmp.path(), &[], tmp.path(), Some(&clip), None);
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert_eq!(labels, ["New File", "New Folder", "Paste"]);
}

#[test]
fn tree_context_menu_on_workspace_root_offers_new_file_and_new_folder_only() {
    // Right-clicking the workspace root row must NOT offer Rename/Delete
    // (the root cannot be renamed or moved to trash from inside croft).
    let tmp = tempfile::tempdir().unwrap();
    let n = dir_node(tmp.path());
    let items = build_tree_context_menu_items(Some(&n), tmp.path(), &[], tmp.path(), None, None);
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    assert_eq!(labels, ["New File", "New Folder"]);
}

/// Issue #217: the Explorer's file names were white-on-white under Croft
/// Light (a bare `Color::White` that bypassed the theme adapter). Render
/// the tree under the light theme and inspect the actual name cell: the
/// text must be dark, and under a dark theme it must stay white.
#[test]
fn explorer_names_are_legible_on_the_light_theme() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
    for (theme, want_dark) in [
        (crate::theme::Theme::DARK_BLUE, false),
        (crate::theme::Theme::from_id("light"), true),
    ] {
        let mut app = App::new(tmp.path().to_path_buf()).unwrap();
        app.apply_theme(theme);
        let backend = ratatui::backend::TestBackend::new(120, 40);
        let mut term = ratatui::Terminal::new(backend).unwrap();
        term.draw(|frame| app.render(frame)).unwrap();
        // Find the cell where "alpha.rs" is painted in the tree column.
        let buf = term.backend().buffer();
        let mut found = None;
        'rows: for y in 0..40u16 {
            for x in 0..40u16 {
                if buf[(x, y)].symbol() == "a"
                    && buf[(x + 1, y)].symbol() == "l"
                    && buf[(x + 2, y)].symbol() == "p"
                    && buf[(x + 3, y)].symbol() == "h"
                {
                    found = Some((x, y));
                    break 'rows;
                }
            }
        }
        let (x, y) = found.expect("alpha.rs must be visible in the Explorer");
        let fg = buf[(x, y)].fg;
        if want_dark {
            let ratatui::style::Color::Rgb(r, g, b) = fg else {
                panic!("light tree names must resolve to Rgb, got {fg:?}")
            };
            let luma = 0.299 * f32::from(r) + 0.587 * f32::from(g) + 0.114 * f32::from(b);
            assert!(luma < 128.0, "tree name must be dark on white, got {fg:?}");
        } else {
            assert_eq!(
                fg,
                ratatui::style::Color::White,
                "dark themes keep the historical white tree names"
            );
        }
    }
}

/// #465: the Explorer menu offers the viewer for a single file it handles,
/// after Rename and before the compare entries, and for nothing else.
#[test]
fn the_explorer_menu_offers_a_viewer_for_a_single_matching_file_only() {
    let tmp = tempfile::tempdir().unwrap();
    let csv = tmp.path().join("data.csv");
    let md = tmp.path().join("notes.md");
    std::fs::write(&csv, "a,b\n").unwrap();
    std::fs::write(&md, "# hi\n").unwrap();
    let viewer_for = |file: &Path| {
        (file.extension().and_then(|e| e.to_str()) == Some("csv"))
            .then(|| (String::from("Open in csvlens"), String::from("csvlens")))
    };
    let target = tmp.path().to_path_buf();

    let n = file_node(&csv);
    let items = build_tree_context_menu_items_with(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&csv),
        &target,
        None,
        None,
        viewer_for,
    );
    let labels: Vec<&str> = items.iter().map(|(s, _)| s.as_str()).collect();
    let at = labels
        .iter()
        .position(|l| *l == "Open in csvlens")
        .expect("a csv gets the viewer entry");
    assert_eq!(labels[at - 1], "Rename", "it follows Rename");
    assert!(
        matches!(&items[at].1, MenuAction::OpenInViewer(id, p) if id == "csvlens" && p == &csv)
    );

    let n = file_node(&md);
    let items = build_tree_context_menu_items_with(
        Some(&n),
        tmp.path(),
        std::slice::from_ref(&md),
        &target,
        None,
        None,
        viewer_for,
    );
    assert!(
        !items.iter().any(|(l, _)| l == "Open in csvlens"),
        "a kind no viewer handles gets no entry"
    );

    // A multi-selection is not one file, so no viewer entry either.
    let n = file_node(&csv);
    let both = vec![csv.clone(), md.clone()];
    let items = build_tree_context_menu_items_with(
        Some(&n),
        tmp.path(),
        &both,
        &target,
        None,
        None,
        viewer_for,
    );
    assert!(
        !items.iter().any(|(l, _)| l == "Open in csvlens"),
        "a multi-selection gets no viewer entry"
    );
}

#[test]
fn cmd_shift_e_jumps_to_explorer_from_any_pane() {
    let mut app = editor_app_with_lines(&["x"]);
    app.set_sidebar_view(SidebarView::Search);
    app.show_tree = false;
    app.handle_key(key(
        KeyCode::Char('e'),
        KeyModifiers::SUPER | KeyModifiers::SHIFT,
    ))
    .unwrap();
    assert_eq!(app.sidebar_view, SidebarView::Explorer);
    assert!(
        app.show_tree,
        "Cmd+Shift+E must un-collapse the sidebar if it was hidden"
    );
}

#[test]
fn cmd_f_in_explorer_still_opens_the_new_file_prompt() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.focus_pane(Pane::Tree);
    app.handle_key(key(KeyCode::Char('f'), KeyModifiers::SUPER))
        .unwrap();
    assert!(
        app.prompt.is_some(),
        "Cmd+F in the Explorer pane must keep creating a new file — the existing behaviour the user asked us to preserve"
    );
    assert!(
        app.editor_find.is_none(),
        "Cmd+F in the Explorer must NOT open the editor Find overlay; the chord is overloaded by pane"
    );
}

#[test]
fn picking_a_nested_file_via_cmd_p_expands_every_parent_dir_in_the_explorer_and_selects_the_row() {
    let tmp = tempfile::tempdir().unwrap();
    let nested_dir = tmp.path().join("packages").join("inner").join("citations");
    std::fs::create_dir_all(&nested_dir).unwrap();
    let target = nested_dir.join("storage.py");
    std::fs::write(&target, "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_inner.height = 20;
    app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
        .unwrap();
    app.handle_key(key(KeyCode::Char('s'), KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Char('t'), KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Char('o'), KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
        .unwrap();
    let selected = app
        .tree
        .selected_path()
        .map(|p| p.to_path_buf())
        .expect("a row must be selected after Enter on the finder");
    assert_eq!(
        selected, target,
        "the explorer cursor must land on the picked file, not stay on whichever row was selected before — got {selected:?}"
    );
    // A compact row (#1486) shows every folder it folds in.
    let visible_paths: Vec<String> = app
        .tree
        .nodes
        .iter()
        .flat_map(|n| n.shown_paths())
        .map(|p| p.display().to_string())
        .collect();
    for expected_parent in [
        tmp.path().join("packages"),
        tmp.path().join("packages").join("inner"),
        tmp.path().join("packages").join("inner").join("citations"),
    ] {
        let s = expected_parent.display().to_string();
        assert!(
            visible_paths.contains(&s),
            "parent dir {s} must appear in the flattened node list — i.e. every ancestor of the picked file must be expanded so the row is reachable: got {visible_paths:?}"
        );
    }
}

#[test]
fn cmd_p_index_refreshes_after_a_new_file_lands_on_disk() {
    // Regression: the Cmd+P index was built once at App::new and
    // never refreshed, so files created later (by another process,
    // by a git checkout that swapped the working tree, or by an
    // in-app create) stayed invisible to Cmd+P until croft was
    // restarted. drain_fs_events now triggers kick_file_finder_index_rebuild
    // so creates and deletes propagate into the haystack the
    // finder ranks against.
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join(".git")).unwrap();
    std::fs::write(tmp.path().join("alpha.rs"), b"").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    // Wait for the initial background index to land.
    let mut waited = 0u32;
    loop {
        app.poll_file_finder_index();
        if app.file_finder_index.is_some() {
            break;
        }
        if waited >= 5000 {
            panic!("initial index must land within 5s");
        }
        std::thread::sleep(std::time::Duration::from_millis(20));
        waited += 20;
    }
    // Drain any startup FS-watcher noise so the next loop reacts
    // only to the file we are about to create.
    for _ in 0..20 {
        let _ = app.drain_fs_events();
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    let new_dir = tmp
        .path()
        .join("packages/ant-ts-lib-noggin/src/entities/files");
    std::fs::create_dir_all(&new_dir).unwrap();
    std::fs::write(new_dir.join("resolver.ts"), b"export {};\n").unwrap();
    let started = std::time::Instant::now();
    let mut saw = false;
    for _ in 0..400 {
        let _ = app.drain_fs_events();
        app.poll_file_finder_index();
        if let Some(idx) = app.file_finder_index.as_ref()
            && idx
                .iter()
                .any(|e| e.rel == "packages/ant-ts-lib-noggin/src/entities/files/resolver.ts")
        {
            saw = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    assert!(
        saw,
        "Cmd+P index must include resolver.ts within a few seconds of it being written to disk; without this, branch-new files stay invisible to Cmd+P until croft restarts"
    );
    assert!(
        started.elapsed() <= std::time::Duration::from_secs(5),
        "index refresh must finish within 5s of the FS event on a 2-file workspace"
    );
}

/// #2618: a Go to File index rebuild asked for while another was running
/// still runs when the running one crashes, instead of waiting for the next
/// file change.
#[test]
fn a_rebuild_queued_behind_a_crashed_index_build_still_runs() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join(".git")).unwrap();
    std::fs::write(tmp.path().join("alpha.rs"), b"").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    // A build whose thread crashes.
    app.file_finder_index_job = Some(
        crate::jobs::Job::spawn(
            "file-index",
            crate::jobs::Start::Now,
            || -> Vec<crate::file_index::FileEntry> {
                panic!("the index build crashed (on purpose, in a test)")
            },
        )
        .unwrap(),
    );
    // A file lands while it runs, and asks for a rebuild.
    std::fs::write(tmp.path().join("beta.rs"), b"").unwrap();
    app.kick_file_finder_index_rebuild();
    crate::test_budget::await_spawned(
        std::time::Duration::from_secs(5),
        "the rebuild queued behind the crashed build to land",
        || {
            app.poll_file_finder_index();
            app.file_finder_index
                .as_ref()
                .is_some_and(|index| index.iter().any(|e| e.rel == "beta.rs"))
        },
    );
}

#[test]
fn click_on_tree_row_aligned_with_terminal_splitter_y_selects_the_row_not_the_splitter() {
    // Regression for the user's "I can't click on the last two files,
    // but arrow+Enter opens them" report. Root cause: the editor /
    // terminal splitter handler at handle_mouse checks `m.row == y
    // || m.row == y.saturating_sub(1)` WITHOUT gating by column, so
    // a click on a tree row in the LEFT column whose y happens to
    // line up with the editor/terminal splitter (which only lives
    // in the RIGHT column) was hijacked into a splitter-drag and
    // never reached the Explorer's `node_at_y` branch.
    let tmp = tempfile::tempdir().unwrap();
    for name in [
        "alpha.txt",
        "beta.txt",
        "gamma.txt",
        "delta.txt",
        "epsilon.txt",
        "zeta.txt",
    ] {
        std::fs::write(tmp.path().join(name), b"").unwrap();
    }
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    // Layout: activity-bar(4) + tree-pane on the left, editor +
    // terminal stacked on the right. Pick a splitter row that
    // intersects one of the rendered tree rows.
    app.tree.last_area = Rect {
        x: 4,
        y: 0,
        width: 32,
        height: 12,
    };
    app.tree.pretend_drawn(Rect {
        x: 5,
        y: 1,
        width: 30,
        height: 10,
    });
    app.sidebar_splitter_x = Some(36);
    // Splitter sits inside the tree's rendered row band.
    app.terminal_splitter_y = Some(6);
    // The clicked tree row's y MUST equal terminal_splitter_y to
    // trigger the bug; column stays well inside tree.last_inner.x..
    let click_row = 6u16;
    let click_col = app.tree.last_inner.x + 2;
    let target_idx = (click_row - app.tree.last_inner.y) as usize;
    assert!(
        target_idx < app.tree.nodes.len(),
        "test setup must put a real tree row at the splitter y"
    );
    let click = crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: click_col,
        row: click_row,
        modifiers: KeyModifiers::NONE,
    };
    app.handle_mouse(click);
    assert!(
        !matches!(app.gesture, Some(Gesture::Splitter(_))),
        "click in the LEFT (tree) column must never arm a splitter drag, even when the row y lines up with the editor/terminal splitter that lives in the RIGHT column"
    );
    assert_eq!(
        app.tree.selected, target_idx,
        "the tree row under the click must become selected; the user reported the bottom rows of the tree being unclickable even though they were plainly visible"
    );
}

#[test]
fn double_clicking_directory_toggles_only_once() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join("sub")).unwrap();
    std::fs::write(tmp.path().join("sub").join("child.txt"), "hi").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_area = Rect {
        x: 0,
        y: 0,
        width: 40,
        height: 8,
    };
    app.tree.pretend_drawn(Rect {
        x: 1,
        y: 1,
        width: 38,
        height: 6,
    });
    let sub_idx = app
        .tree
        .nodes
        .iter()
        .position(|node| node.path.ends_with("sub"))
        .unwrap();
    let row = app.tree.last_inner.y + sub_idx as u16;
    let click = crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 6,
        row,
        modifiers: KeyModifiers::NONE,
    };

    app.handle_mouse(click);
    let sub_idx = app
        .tree
        .nodes
        .iter()
        .position(|node| node.path.ends_with("sub"))
        .unwrap();
    assert!(app.tree.nodes[sub_idx].expanded);
    let expanded_len = app.tree.nodes.len();

    app.handle_mouse(click);
    let sub_idx = app
        .tree
        .nodes
        .iter()
        .position(|node| node.path.ends_with("sub"))
        .unwrap();
    assert!(app.tree.nodes[sub_idx].expanded);
    assert_eq!(app.tree.nodes.len(), expanded_len);
}

#[test]
fn shift_arrow_extends_tree_selection() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), "").unwrap();
    std::fs::write(tmp.path().join("b.txt"), "").unwrap();
    std::fs::write(tmp.path().join("c.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.select_replace(1);
    app.handle_tree_key(key(KeyCode::Down, KeyModifiers::SHIFT));
    app.handle_tree_key(key(KeyCode::Down, KeyModifiers::SHIFT));
    assert_eq!(app.tree.action_paths().len(), 3);
}

#[test]
fn cmd_a_in_tree_marks_all_visible() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), "").unwrap();
    std::fs::write(tmp.path().join("b.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.handle_tree_key(key(KeyCode::Char('a'), KeyModifiers::SUPER));
    assert_eq!(app.tree.marked.len(), app.tree.nodes.len());
}

#[test]
fn esc_in_tree_clears_marks() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.toggle_mark(1);
    assert!(!app.tree.marked.is_empty());
    app.handle_tree_key(key(KeyCode::Esc, KeyModifiers::NONE));
    assert!(app.tree.marked.is_empty());
}

#[test]
fn cmd_c_then_cmd_v_copies_explorer_paths_into_target() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("src.txt"), "hello").unwrap();
    std::fs::create_dir(tmp.path().join("dest")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let src_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("src.txt"))
        .unwrap();
    app.tree.select_replace(src_idx);
    app.handle_tree_key(key(KeyCode::Char('c'), KeyModifiers::SUPER));
    let dest_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("dest"))
        .unwrap();
    app.tree.select_replace(dest_idx);
    app.handle_tree_key(key(KeyCode::Char('v'), KeyModifiers::SUPER));
    let dest_file = tmp.path().join("dest/src.txt");
    assert!(dest_file.exists(), "copy must place file in target dir");
    assert!(
        tmp.path().join("src.txt").exists(),
        "copy must keep the source"
    );
}

#[test]
fn cmd_x_then_cmd_v_moves_explorer_paths_into_target() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("src.txt"), "hello").unwrap();
    std::fs::create_dir(tmp.path().join("dest")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let src_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("src.txt"))
        .unwrap();
    app.tree.select_replace(src_idx);
    app.handle_tree_key(key(KeyCode::Char('x'), KeyModifiers::SUPER));
    let dest_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("dest"))
        .unwrap();
    app.tree.select_replace(dest_idx);
    app.handle_tree_key(key(KeyCode::Char('v'), KeyModifiers::SUPER));
    assert!(tmp.path().join("dest/src.txt").exists());
    assert!(
        !tmp.path().join("src.txt").exists(),
        "cut must remove source"
    );
    assert!(
        app.tree_clipboard.is_none(),
        "cut clipboard must be consumed on paste"
    );
}

#[test]
fn delete_key_trashes_every_marked_path() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), "").unwrap();
    std::fs::write(tmp.path().join("b.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let a_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("a.txt"))
        .unwrap();
    let b_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("b.txt"))
        .unwrap();
    app.tree.select_replace(a_idx);
    app.tree.toggle_mark(b_idx);
    // Delete now asks first: it opens a confirmation popup, nothing is trashed yet.
    app.handle_tree_key(key(KeyCode::Delete, KeyModifiers::NONE));
    assert!(
        app.input_prompt.is_some(),
        "Delete must open a confirm popup"
    );
    assert!(
        tmp.path().join("a.txt").exists(),
        "not trashed until confirmed"
    );
    // Enter confirms; now both selected entries move to Trash.
    app.submit_input_prompt();
    assert!(!tmp.path().join("a.txt").exists());
    assert!(!tmp.path().join("b.txt").exists());
}

#[test]
fn drag_drop_moves_marked_paths_to_target_dir() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("src.txt"), "x").unwrap();
    std::fs::create_dir(tmp.path().join("dest")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_area = Rect {
        x: 0,
        y: 0,
        width: 40,
        height: 8,
    };
    app.tree.pretend_drawn(Rect {
        x: 1,
        y: 1,
        width: 38,
        height: 6,
    });
    let src_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("src.txt"))
        .unwrap();
    let dest_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("dest"))
        .unwrap();
    let src_row = app.tree.last_inner.y + src_idx as u16;
    let dest_row = app.tree.last_inner.y + dest_idx as u16;
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 6,
        row: src_row,
        modifiers: KeyModifiers::NONE,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Drag(crossterm::event::MouseButton::Left),
        column: 6,
        row: dest_row,
        modifiers: KeyModifiers::NONE,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Up(crossterm::event::MouseButton::Left),
        column: 6,
        row: dest_row,
        modifiers: KeyModifiers::NONE,
    });
    assert!(tmp.path().join("dest/src.txt").exists());
    assert!(!tmp.path().join("src.txt").exists());
}

#[test]
fn alt_drag_copies_instead_of_moves() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("src.txt"), "x").unwrap();
    std::fs::create_dir(tmp.path().join("dest")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_area = Rect {
        x: 0,
        y: 0,
        width: 40,
        height: 8,
    };
    app.tree.pretend_drawn(Rect {
        x: 1,
        y: 1,
        width: 38,
        height: 6,
    });
    let src_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("src.txt"))
        .unwrap();
    let dest_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("dest"))
        .unwrap();
    let src_row = app.tree.last_inner.y + src_idx as u16;
    let dest_row = app.tree.last_inner.y + dest_idx as u16;
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 6,
        row: src_row,
        modifiers: KeyModifiers::NONE,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Drag(crossterm::event::MouseButton::Left),
        column: 6,
        row: dest_row,
        modifiers: KeyModifiers::ALT,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Up(crossterm::event::MouseButton::Left),
        column: 6,
        row: dest_row,
        modifiers: KeyModifiers::ALT,
    });
    assert!(tmp.path().join("dest/src.txt").exists());
    assert!(
        tmp.path().join("src.txt").exists(),
        "Alt-drag must keep the source"
    );
}

#[test]
fn dragging_an_explorer_item_out_onto_the_editor_cancels_the_drop_and_does_not_select_text() {
    use crossterm::event::{MouseButton, MouseEventKind};
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("src.txt");
    std::fs::write(&f, "alpha\nbeta\ngamma\n").unwrap();
    std::fs::create_dir(tmp.path().join("dest")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&f).unwrap();
    draw(&mut app, 120, 40);

    let src_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("src.txt"))
        .unwrap();
    let dest_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("dest"))
        .unwrap();
    let col = app.tree.last_inner.x + 2;
    let src_row = app.tree.last_inner.y + src_idx as u16;
    let dest_row = app.tree.last_inner.y + dest_idx as u16;
    let ed_col = app.editor.last_inner.x + app.editor.last_gutter_width + 2;
    let ed_row = app.editor.last_inner.y + 1;
    assert!(
        rect_contains(app.editor.last_inner, ed_col, ed_row)
            && !rect_contains(app.tree.last_area, ed_col, ed_row),
        "precondition: editor drop point must sit inside the editor and outside the tree"
    );

    // Start dragging `src.txt` over `dest/`.
    app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, src_row));
    app.handle_mouse(mouse(
        MouseEventKind::Drag(MouseButton::Left),
        col,
        dest_row,
    ));
    assert_eq!(
        app.tree.drag_target,
        Some(dest_idx),
        "precondition: hovering dest/ highlights it as drop target"
    );

    // Drag out of Explorer into the editor and release there.
    app.handle_mouse(mouse(
        MouseEventKind::Drag(MouseButton::Left),
        ed_col,
        ed_row,
    ));
    assert_eq!(
        app.tree.drag_target, None,
        "leaving Explorer onto the editor must clear the drop target"
    );
    assert!(
        app.editor.selection.is_none_or(|s| !s.has_area()),
        "dragging an Explorer item over the editor must not start an editor selection"
    );

    app.handle_mouse(mouse(MouseEventKind::Up(MouseButton::Left), ed_col, ed_row));
    assert!(
        tmp.path().join("src.txt").exists() && !tmp.path().join("dest/src.txt").exists(),
        "dropping over the editor must cancel instead of moving into the last hovered folder"
    );
    assert_eq!(app.status, "Drop cancelled");
    assert!(app.gesture.is_none());
}

#[test]
fn a_lost_release_on_an_explorer_drag_is_abandoned_by_a_new_press_without_moving_files() {
    use crossterm::event::{MouseButton, MouseEventKind};
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("src.txt");
    std::fs::write(&f, "alpha\nbeta\n").unwrap();
    std::fs::create_dir(tmp.path().join("dest")).unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.editor.open_pinned(&f).unwrap();
    draw(&mut app, 120, 40);

    let src_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("src.txt"))
        .unwrap();
    let dest_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("dest"))
        .unwrap();
    let col = app.tree.last_inner.x + 2;
    let src_row = app.tree.last_inner.y + src_idx as u16;
    let dest_row = app.tree.last_inner.y + dest_idx as u16;

    app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, src_row));
    app.handle_mouse(mouse(
        MouseEventKind::Drag(MouseButton::Left),
        col,
        dest_row,
    ));
    assert_eq!(app.tree.drag_target, Some(dest_idx));

    // Lost release: next press lands on the sidebar splitter instead.
    let seam = app.sidebar_splitter_x.expect("sidebar splitter drawn");
    app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), seam, 5));
    assert_eq!(
        app.tree.drag_target, None,
        "a new press abandons the Explorer drag and clears its target highlight"
    );
    assert_eq!(app.gesture, Some(Gesture::Splitter(SplitterDrag::Sidebar)));

    app.handle_mouse(mouse(MouseEventKind::Up(MouseButton::Left), seam, 5));
    assert!(
        tmp.path().join("src.txt").exists() && !tmp.path().join("dest/src.txt").exists(),
        "releasing the replacement gesture must not drop the abandoned Explorer drag"
    );
    assert!(app.gesture.is_none());
}

#[test]
fn shift_click_extends_marks_from_anchor() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), "").unwrap();
    std::fs::write(tmp.path().join("b.txt"), "").unwrap();
    std::fs::write(tmp.path().join("c.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_area = Rect {
        x: 0,
        y: 0,
        width: 40,
        height: 8,
    };
    app.tree.pretend_drawn(Rect {
        x: 1,
        y: 1,
        width: 38,
        height: 6,
    });
    let first_idx = 1usize;
    let last_idx = app.tree.nodes.len() - 1;
    let first_row = app.tree.last_inner.y + first_idx as u16;
    let last_row = app.tree.last_inner.y + last_idx as u16;
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 6,
        row: first_row,
        modifiers: KeyModifiers::NONE,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Up(crossterm::event::MouseButton::Left),
        column: 6,
        row: first_row,
        modifiers: KeyModifiers::NONE,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 6,
        row: last_row,
        modifiers: KeyModifiers::SHIFT,
    });
    let span = last_idx - first_idx + 1;
    assert_eq!(app.tree.action_paths().len(), span);
}

#[test]
fn alt_click_toggles_individual_mark_on_release() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), "").unwrap();
    std::fs::write(tmp.path().join("b.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_area = Rect {
        x: 0,
        y: 0,
        width: 40,
        height: 8,
    };
    app.tree.pretend_drawn(Rect {
        x: 1,
        y: 1,
        width: 38,
        height: 6,
    });
    let a_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("a.txt"))
        .unwrap();
    let b_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("b.txt"))
        .unwrap();
    let a_row = app.tree.last_inner.y + a_idx as u16;
    let b_row = app.tree.last_inner.y + b_idx as u16;
    for row in [a_row, b_row] {
        app.handle_mouse(crossterm::event::MouseEvent {
            kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
            column: 6,
            row,
            modifiers: KeyModifiers::ALT,
        });
        app.handle_mouse(crossterm::event::MouseEvent {
            kind: crossterm::event::MouseEventKind::Up(crossterm::event::MouseButton::Left),
            column: 6,
            row,
            modifiers: KeyModifiers::ALT,
        });
    }
    let marked: BTreeSet<PathBuf> = app.tree.marked.clone();
    assert!(marked.contains(&tmp.path().join("a.txt")));
    assert!(marked.contains(&tmp.path().join("b.txt")));
}

#[test]
fn ctrl_click_also_toggles_individual_mark() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("a.txt"), "").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_area = Rect {
        x: 0,
        y: 0,
        width: 40,
        height: 8,
    };
    app.tree.pretend_drawn(Rect {
        x: 1,
        y: 1,
        width: 38,
        height: 6,
    });
    let a_idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.ends_with("a.txt"))
        .unwrap();
    let row = app.tree.last_inner.y + a_idx as u16;
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 6,
        row,
        modifiers: KeyModifiers::CONTROL,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Up(crossterm::event::MouseButton::Left),
        column: 6,
        row,
        modifiers: KeyModifiers::CONTROL,
    });
    assert!(app.tree.marked.contains(&tmp.path().join("a.txt")));
}

#[test]
fn alt_click_does_not_open_the_file() {
    // Toggle is meant to mark — never to open. The deferred-toggle flow
    // must not reach `editor.open_preview` when Alt/Ctrl is held.
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("a.txt");
    std::fs::write(&f, "hi").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.tree.last_area = Rect {
        x: 0,
        y: 0,
        width: 40,
        height: 8,
    };
    app.tree.pretend_drawn(Rect {
        x: 1,
        y: 1,
        width: 38,
        height: 6,
    });
    let idx = app.tree.nodes.iter().position(|n| n.path == f).unwrap();
    let row = app.tree.last_inner.y + idx as u16;
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
        column: 6,
        row,
        modifiers: KeyModifiers::ALT,
    });
    app.handle_mouse(crossterm::event::MouseEvent {
        kind: crossterm::event::MouseEventKind::Up(crossterm::event::MouseButton::Left),
        column: 6,
        row,
        modifiers: KeyModifiers::ALT,
    });
    assert!(
        app.editor.is_blank_initial(),
        "alt-click must not open the file"
    );
}

#[test]
fn explorer_drop_on_terminal_focus_does_not_hijack_text_paste() {
    // Regression guard: when sidebar is Explorer and focus is on the
    // embedded terminal, pasting still goes to the terminal so the
    // user can type a path into a shell command.
    let workspace = tempfile::tempdir().unwrap();
    let outside = tempfile::tempdir().unwrap();
    let src = outside.path().join("typed.txt");
    std::fs::write(&src, "x").unwrap();
    let mut app = App::new(workspace.path().to_path_buf()).unwrap();
    app.set_sidebar_view(SidebarView::Explorer);
    app.show_terminal = true;
    app.focus_pane(Pane::Terminal);
    app.handle_paste(&format!("{}\n", src.display()));
    assert!(
        src.exists(),
        "explorer-with-terminal-focus drop must NOT move the source",
    );
    assert!(!workspace.path().join("typed.txt").exists());
}

/// On a terminal that never sends Super, Ctrl+Z with the Explorer focused
/// opens the jump through the real key path (#1294).
#[test]
fn ctrl_z_in_the_explorer_opens_the_zoxide_jump() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.focus = Pane::Tree;
    app.sidebar_view = SidebarView::Explorer;
    app.handle_key(key(KeyCode::Char('z'), KeyModifiers::CONTROL))
        .unwrap();
    assert!(app.zoxide_jump.is_some(), "status: {}", app.status);
}

#[test]
fn cmd_z_in_explorer_opens_the_zoxide_jump_popup() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    assert!(
        app.is_explorer_focused(),
        "precondition: a fresh app is focused on the Explorer pane"
    );
    assert!(app.zoxide_jump.is_none(), "popup starts closed");
    let handled = app.handle_explorer_shortcut(key(KeyCode::Char('z'), KeyModifiers::SUPER));
    assert!(
        handled,
        "Cmd+Z must be consumed by the Explorer shortcut layer"
    );
    assert!(
        app.zoxide_jump.is_some(),
        "Cmd+Z in the Explorer must open the zoxide jump popup"
    );
    // Esc closes it again.
    app.handle_zoxide_jump_key(key(KeyCode::Esc, KeyModifiers::NONE));
    assert!(app.zoxide_jump.is_none(), "Esc must close the jump popup");
}

/// #926: on a host without zoxide the jump popup asks before installing
/// anything. `n` closes it with nothing run; `y` starts the install, once.
#[test]
fn the_jump_popup_installs_zoxide_only_when_the_user_says_y() {
    use crate::zoxide::{InstallState, install_requests};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let open_without_zoxide = |app: &mut App| {
        app.handle_explorer_shortcut(key(KeyCode::Char('z'), KeyModifiers::SUPER));
        // Whatever this machine has: the popup as a host without zoxide sees it.
        let jump = app.zoxide_jump.as_mut().expect("Cmd+Z opens the popup");
        jump.set_results(None);
        jump.install_state = InstallState::Idle;
    };
    let before = install_requests();

    open_without_zoxide(&mut app);
    app.handle_zoxide_jump_key(key(KeyCode::Char('n'), KeyModifiers::NONE));
    assert!(app.zoxide_jump.is_none(), "n declines and closes the popup");
    assert_eq!(install_requests(), before, "n installs nothing");

    open_without_zoxide(&mut app);
    app.handle_zoxide_jump_key(key(KeyCode::Char('y'), KeyModifiers::NONE));
    assert_eq!(install_requests(), before + 1, "y starts the install");
    let jump = app.zoxide_jump.as_ref().expect("the popup stays open");
    assert_eq!(jump.install_state, InstallState::Running);
    assert!(
        jump.query.is_empty(),
        "the answer is not typed into the query"
    );
    assert!(
        app.status.to_lowercase().contains("installing"),
        "status: {}",
        app.status
    );
    app.handle_zoxide_jump_key(key(KeyCode::Char('y'), KeyModifiers::NONE));
    assert_eq!(
        install_requests(),
        before + 1,
        "a second y while it runs starts nothing"
    );
}

/// #926: with zoxide installed but no hook in the rc, launching croft no
/// longer wires one, so the empty popup offers the hook alone: `y` adds it,
/// installs nothing, and a typed query is a query, not an answer.
#[test]
fn the_jump_popup_offers_the_hook_alone_when_zoxide_is_installed() {
    use crate::zoxide::{hook_requests, install_requests};
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let open_with_unhooked_zoxide = |app: &mut App| {
        app.handle_explorer_shortcut(key(KeyCode::Char('z'), KeyModifiers::SUPER));
        let jump = app.zoxide_jump.as_mut().expect("Cmd+Z opens the popup");
        jump.set_results(Some(Vec::new()));
        jump.hook_offer = Some(String::from("~/.bashrc"));
    };
    let (hooks, installs) = (hook_requests(), install_requests());

    open_with_unhooked_zoxide(&mut app);
    assert!(app.zoxide_jump.as_ref().unwrap().hook_offer_shown());
    app.handle_zoxide_jump_key(key(KeyCode::Char('n'), KeyModifiers::NONE));
    assert!(app.zoxide_jump.is_none(), "n declines and closes the popup");
    assert_eq!(hook_requests(), hooks, "n adds nothing");

    open_with_unhooked_zoxide(&mut app);
    app.handle_zoxide_jump_key(key(KeyCode::Char('y'), KeyModifiers::NONE));
    assert_eq!(hook_requests(), hooks + 1, "y adds the hook");
    assert_eq!(install_requests(), installs, "zoxide is there: no install");
    let jump = app.zoxide_jump.as_ref().expect("the popup stays open");
    assert!(jump.hook_offer.is_none(), "offered once");
    assert!(
        jump.query.is_empty(),
        "the answer is not typed into the query"
    );
    app.close_zoxide_jump();

    open_with_unhooked_zoxide(&mut app);
    app.zoxide_jump.as_mut().unwrap().push_char('p');
    app.handle_zoxide_jump_key(key(KeyCode::Char('y'), KeyModifiers::NONE));
    assert_eq!(hook_requests(), hooks + 1, "a query's `y` is a letter");
    assert_eq!(app.zoxide_jump.as_ref().unwrap().query, "py");
}

/// #1345: the Explorer hides a `files.exclude` folder, and only that:
/// `search.exclude` leaves the fixtures listed, as in VS Code.
#[test]
fn the_explorer_hides_files_exclude_folders_only() {
    let (app, t) = app_with_vscode_excludes();
    let listed: Vec<_> = app.tree.nodes.iter().map(|n| n.path.clone()).collect();
    assert!(!listed.contains(&t.path().join("generated")), "{listed:?}");
    assert!(listed.contains(&t.path().join("tests")), "{listed:?}");
    assert!(listed.contains(&t.path().join("src")), "{listed:?}");
}

/// #360: the built-in secret rules sit in the trigger set by default,
/// leave it when the setting is switched off, and come back on; a reveal
/// window lasts ten seconds and asks for a redraw when it closes.
#[test]
fn secret_redaction_toggles_the_builtin_rules_and_reveal_expires() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    assert!(app.secret_redaction, "on by default");
    assert!(
        app.triggers.has_redactions(),
        "the built-in rules are loaded"
    );
    assert_eq!(
        crate::triggers::mask_text("AKIAIOSFODNN7EXAMPLE", &app.triggers, false),
        "\u{2022}".repeat(20)
    );
    app.toggle_secret_redaction();
    assert!(!app.secret_redaction);
    assert!(
        !app.triggers.has_redactions(),
        "off means no built-in rules"
    );
    app.toggle_secret_redaction();
    assert!(app.triggers.has_redactions());

    assert!(!app.redactions_revealed());
    app.reveal_redacted_secrets();
    assert!(app.redactions_revealed());
    assert!(
        !app.tick_redaction_reveal(),
        "still inside the window: no change"
    );
    app.redaction_reveal_until = Some(std::time::Instant::now());
    assert!(
        app.tick_redaction_reveal(),
        "the window closing is a redraw"
    );
    assert!(!app.redactions_revealed());
    assert!(app.status.contains("masked again"));

    // The chip reads what was PAINTED: a hidden panel paints nothing, so a
    // stale count from the last visible frame must not survive it.
    if let Some(t) = app.terminals.first_mut() {
        t.redacted_on_screen = 3;
    }
    assert_eq!(app.redacted_on_screen(), 3);
    app.show_terminal = false;
    let backend = ratatui::backend::TestBackend::new(120, 40);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    term.draw(|f| app.render(f)).unwrap();
    assert_eq!(app.redacted_on_screen(), 0, "a hidden pane counts nothing");
    app.show_terminal = true;

    // The clipboard keeps the real value unless a rule says otherwise; the
    // scrollback dump always masks.
    assert_eq!(
        app.terminal_text_for_copy(String::from("k=AKIAIOSFODNN7EXAMPLE")),
        "k=AKIAIOSFODNN7EXAMPLE"
    );
    app.triggers = std::sync::Arc::new(crate::triggers::TriggerSet::from_json(
        r#"[{ "regex": "AKIA[0-9A-Z]{16}", "action": "redact", "copy": "masked" }]"#,
    ));
    assert_eq!(
        app.terminal_text_for_copy(String::from("k=AKIAIOSFODNN7EXAMPLE")),
        format!("k={}", "\u{2022}".repeat(20))
    );
}

#[test]
fn explorer_views_menu_omits_dependencies_in_a_folder_with_no_manifest() {
    // A plain folder carries no package manifest, so the DEPENDENCIES view is
    // not offered at all — mirroring how rust-analyzer's "Rust Dependencies"
    // view simply does not exist outside a Rust project. The other four views
    // are always available, each with a checkmark reflecting its visibility.
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("main.rs"), "fn main() {}\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.open_explorer_views_menu();
    let menu = app.context_menu.as_ref().expect("menu must open");
    assert_eq!(
        menu.items.len(),
        ExplorerView::ALL.len() - 1,
        "the Dependencies row is dropped when no manifest is present"
    );
    assert!(
        !menu
            .items
            .iter()
            .any(|e| menu_label(e).contains("Dependencies")),
        "no dependency toggle in a manifest-less folder: {:?}",
        menu.items
    );
    for entry in &menu.items {
        let label = menu_label(entry);
        let checked = label.starts_with('\u{2713}');
        // Every listed row names an always-available view and its check glyph
        // matches that view's current visibility.
        let view = ExplorerView::ALL
            .iter()
            .find(|v| label.contains(v.label()))
            .unwrap_or_else(|| panic!("unrecognized menu row {label:?}"));
        assert_eq!(checked, app.explorer_views.is_visible(*view));
    }
}

#[test]
fn explorer_views_menu_labels_dependencies_for_the_detected_ecosystem() {
    // A Cargo workspace makes the DEPENDENCIES view appear, labeled for the
    // detected ecosystem ("Rust Dependencies"), so the menu is language-aware
    // rather than hardcoded.
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("Cargo.toml"),
        "[package]\nname = \"x\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.open_explorer_views_menu();
    let menu = app.context_menu.as_ref().expect("menu must open");
    assert_eq!(menu.items.len(), ExplorerView::ALL.len());
    assert!(
        menu.items
            .iter()
            .any(|e| menu_label(e).contains("Rust Dependencies")),
        "a Cargo root labels the view 'Rust Dependencies': {:?}",
        menu.items
    );
}

/// An app on a Node folder whose `package.json` lists `left-pad`, with the
/// DEPENDENCIES section shown and open.
fn left_pad_app() -> (tempfile::TempDir, App) {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(
        tmp.path().join("package.json"),
        r#"{ "dependencies": { "left-pad": "1.3.0" } }"#,
    )
    .unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    if !app.explorer_views.is_visible(ExplorerView::Dependencies) {
        app.explorer_views.toggle(ExplorerView::Dependencies);
    }
    app.dependencies.collapsed = false;
    (tmp, app)
}

/// The DEPENDENCIES section as it draws.
fn dependencies_text(app: &mut App) -> String {
    let area = ratatui::layout::Rect::new(0, 0, 40, 6);
    let mut buf = ratatui::buffer::Buffer::empty(area);
    let cx = app.view_cx(app.focus == Pane::Tree && app.sidebar_view == SidebarView::Explorer);
    app.dependencies.draw(&cx, area, &mut buf);
    (0..area.height)
        .map(|y| {
            (0..area.width)
                .map(|x| buf[(x, y)].symbol())
                .collect::<String>()
                + "\n"
        })
        .collect()
}

/// #2572: a dependency lookup whose thread crashed settles the
/// section on saying so, instead of "Loading" until the folder changes.
#[test]
fn a_dependency_lookup_whose_thread_crashed_says_it_could_not_list() {
    let (_tmp, mut app) = left_pad_app();
    app.sync_explorer_panels();
    app.deps_fetch = Some(
        crate::jobs::Job::spawn(
            "dependencies",
            crate::jobs::Start::Now,
            || -> Vec<crate::dependencies::Dependency> {
                panic!("the lookup crashed (on purpose, in a test)")
            },
        )
        .unwrap(),
    );
    crate::test_budget::await_spawned(
        std::time::Duration::from_secs(1),
        "the crashed lookup to settle",
        || {
            app.sync_explorer_panels();
            app.deps_fetch.is_none()
        },
    );
    let text = dependencies_text(&mut app);
    assert!(text.contains("Could not list dependencies"), "got:\n{text}");
    assert!(!text.contains("Loading"), "got:\n{text}");
}

/// #2573: a lookup still out for the folder just left is dropped, so
/// its list can never land in the new folder's section, even when it
/// answers last.
#[test]
fn a_lookup_for_the_folder_just_left_never_lands_in_the_new_one() {
    let (_tmp, mut app) = left_pad_app();
    // The folder just left still has its lookup out, and it has answered.
    app.deps_fetch = Some(crate::jobs::Job::finished(vec![
        crate::dependencies::Dependency {
            name: "serde".into(),
            version: "1.0.0".into(),
            ecosystem: crate::dependencies::Ecosystem::Rust,
        },
    ]));
    // The re-root that left it.
    app.deps_fetched = None;
    crate::test_budget::await_spawned(
        std::time::Duration::from_secs(1),
        "this folder's list to land",
        || {
            app.sync_explorer_panels();
            dependencies_text(&mut app).contains("left-pad")
        },
    );
    let text = dependencies_text(&mut app);
    assert!(!text.contains("serde"), "got:\n{text}");
}

/// An app on a repo whose committed `seed.txt` is open, with the TIMELINE
/// section shown and open.
fn seed_timeline_app() -> (tempfile::TempDir, App, PathBuf) {
    let tmp = make_committed_repo();
    let file = tmp.path().join("seed.txt");
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.open_file_at_launch(&file);
    if !app.explorer_views.is_visible(ExplorerView::Timeline) {
        app.explorer_views.toggle(ExplorerView::Timeline);
    }
    app.timeline.collapsed = false;
    (tmp, app, file)
}

/// The TIMELINE section as it draws.
fn timeline_text(app: &mut App) -> String {
    let area = ratatui::layout::Rect::new(0, 0, 40, 8);
    let mut buf = ratatui::buffer::Buffer::empty(area);
    let cx = app.view_cx(app.focus == Pane::Tree && app.sidebar_view == SidebarView::Explorer);
    app.timeline.draw(&cx, area, &mut buf);
    (0..area.height)
        .map(|y| {
            (0..area.width)
                .map(|x| buf[(x, y)].symbol())
                .collect::<String>()
                + "\n"
        })
        .collect()
}

/// #2574: a TIMELINE lookup whose thread crashed settles the section on
/// saying so, instead of "Loading" until another file is opened.
#[test]
fn a_timeline_lookup_whose_thread_crashed_says_it_could_not_load() {
    let (_tmp, mut app, _file) = seed_timeline_app();
    app.sync_explorer_panels();
    app.timeline_fetch = Some(
        crate::jobs::Job::spawn(
            "timeline",
            crate::jobs::Start::Now,
            || -> (PathBuf, Vec<crate::git::FileHistoryEntry>) {
                panic!("the lookup crashed (on purpose, in a test)")
            },
        )
        .unwrap(),
    );
    crate::test_budget::await_spawned(
        std::time::Duration::from_secs(1),
        "the crashed lookup to settle",
        || {
            app.sync_explorer_panels();
            app.timeline_fetch.is_none()
        },
    );
    let text = timeline_text(&mut app);
    assert!(text.contains("Could not load history"), "got:\n{text}");
    assert!(!text.contains("Loading"), "got:\n{text}");
}

/// #2575: starting a lookup drops the one still out, so an older list for
/// the same file, read before the newest version existed, can never
/// replace the newer one, even when it answers last.
#[test]
fn an_older_timeline_lookup_never_replaces_a_newer_one() {
    let (_tmp, mut app, file) = seed_timeline_app();
    // An older lookup for this file is still out, and has answered.
    app.timeline_fetch = Some(crate::jobs::Job::finished((
        file,
        vec![crate::git::FileHistoryEntry {
            short_hash: "0ld0ld0".into(),
            summary: "an older list".into(),
            author: "someone".into(),
            age_secs: 60,
        }],
    )));
    // A save's new version asks for the history again.
    app.timeline_fetched = None;
    crate::test_budget::await_spawned(
        std::time::Duration::from_secs(5),
        "the newer list to land",
        || {
            app.sync_explorer_panels();
            timeline_text(&mut app).contains("init")
        },
    );
    let text = timeline_text(&mut app);
    assert!(!text.contains("an older list"), "got:\n{text}");
}

#[test]
fn toggling_an_explorer_view_flips_and_persists_its_visibility() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let before = app.explorer_views.is_visible(ExplorerView::Outline);
    app.toggle_explorer_view(ExplorerView::Outline);
    assert_eq!(
        app.explorer_views.is_visible(ExplorerView::Outline),
        !before,
        "toggling Outline must flip its visibility"
    );
}

/// #1608: clicking the `src` crumb lists the folder, folders first, and
/// Enter on `f3.rs` opens it.
#[test]
fn clicking_a_folder_crumb_lists_it_and_enter_opens_a_file() {
    let (mut app, tmp) = app_on_src_f2("// f2.rs\n");
    click_crumb(&mut app, 0);
    let menu = app.context_menu.as_ref().expect("the src picker");
    assert_eq!(
        menu_labels(&menu.items),
        vec!["sub/", "f1.rs", "f2.rs", "f3.rs"]
    );
    assert_eq!(menu.selected, 0);
    for _ in 0..3 {
        app.handle_key(key(KeyCode::Down, KeyModifiers::NONE))
            .unwrap();
    }
    app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
        .unwrap();
    assert!(app.context_menu.is_none());
    assert_eq!(
        app.editor.path.as_deref(),
        Some(tmp.path().join("src/f3.rs").as_path())
    );
}

#[test]
fn opening_a_workspace_file_installs_its_exact_folder_set() {
    // #163: an explicitly opened .code-workspace defines the arrangement
    // exactly — the automatic per-primary store is not merged in.
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("alpha");
    let b = tmp.path().join("beta");
    let c = tmp.path().join("gamma");
    for d in [&a, &b, &c] {
        std::fs::create_dir(d).unwrap();
    }
    let file = tmp.path().join("proj.code-workspace");
    crate::workspace::write_workspace_file(&file, &[b.clone(), c.clone()]).unwrap();

    let mut app = App::new(a.clone()).unwrap();
    // Poison the auto-store for primary B with a DIFFERENT folder: the
    // file must win, so alpha never joins.
    let b_canon = b.canonicalize().unwrap();
    crate::workspace::save_folders_for_root(
        &app.workspace_folders_path.clone(),
        &b_canon.display().to_string(),
        vec![a.clone()],
    )
    .unwrap();

    app.open_workspace_file(&file);

    let roots: Vec<PathBuf> = app.roots.iter().map(Path::to_path_buf).collect();
    assert_eq!(
        app.workspace_root(),
        b_canon,
        "first folder becomes primary"
    );
    assert!(
        roots.contains(&c.canonicalize().unwrap()),
        "the file's second folder joins: {roots:?}"
    );
    assert!(
        !roots.contains(&a.canonicalize().unwrap()),
        "the auto-store's folder does NOT merge in: {roots:?}"
    );

    // Save Workspace As round-trips the live set through the prompt path.
    let dest = tmp.path().join("saved.code-workspace");
    use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
    app.open_input_prompt(
        InputPrompt::new(
            InputPurpose::SaveWorkspaceAs,
            String::from("Save Workspace As"),
            "",
        )
        .with_value(dest.display().to_string()),
    );
    app.submit_input_prompt();
    let saved = crate::workspace::parse_workspace_file(&dest).unwrap();
    assert_eq!(saved, roots, "the saved file names the live folder set");

    // A malformed file refuses with the error in the status.
    let bad = tmp.path().join("bad.code-workspace");
    std::fs::write(&bad, "{ nope").unwrap();
    let before: Vec<PathBuf> = app.roots.iter().map(Path::to_path_buf).collect();
    app.open_workspace_file(&bad);
    assert!(app.status.starts_with("Workspace file:"), "{}", app.status);
    let after: Vec<PathBuf> = app.roots.iter().map(Path::to_path_buf).collect();
    assert_eq!(before, after, "a bad file changes nothing");
}

/// #1602: `~/` in a typed path is the home directory, and a relative path
/// is under the workspace.
#[test]
fn a_typed_path_expands_the_home_and_sits_under_the_workspace() {
    let home = std::path::Path::new("/home/h");
    let root = std::path::Path::new("/work/ws");
    assert_eq!(
        resolve_typed_path("~/team.code-workspace", Some(home), root),
        Some(home.join("team.code-workspace"))
    );
    assert_eq!(
        resolve_typed_path(" sub/x.code-workspace ", Some(home), root),
        Some(root.join("sub/x.code-workspace"))
    );
    assert_eq!(
        resolve_typed_path("/abs/x.code-workspace", Some(home), root),
        Some(std::path::Path::new("/abs/x.code-workspace").to_path_buf())
    );
    assert_eq!(
        resolve_typed_path("~user/x", Some(home), root),
        Some(root.join("~user/x")),
        "only `~/` is the home directory"
    );
    assert_eq!(
        resolve_typed_path("~/team.code-workspace", None, root),
        None,
        "with no home, `~/` is refused, not put under the workspace"
    );
}

/// Removes a path made relative to the process directory, if a run made it:
/// a path that was already there when the guard was made is never touched.
struct RemoveOnDrop(Option<PathBuf>);

impl RemoveOnDrop {
    fn new(path: PathBuf) -> Self {
        Self((!path.exists()).then_some(path))
    }
}

impl Drop for RemoveOnDrop {
    fn drop(&mut self) {
        if let Some(path) = &self.0 {
            let _ = std::fs::remove_dir_all(path);
        }
    }
}

/// #1602: Save Workspace As with a relative path writes under the workspace
/// root, not the directory croft was started from, and the status line
/// names the file as written.
#[test]
fn save_workspace_as_writes_a_relative_path_under_the_workspace() {
    let tmp = tempfile::tempdir().unwrap();
    let rel = format!("croft-1602-{}", std::process::id());
    let _cleanup = RemoveOnDrop::new(std::env::current_dir().unwrap().join(&rel));
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.run_command(crate::widgets::command_palette::Command::SaveWorkspaceAs);
    app.input_prompt.as_mut().unwrap().value = format!("{rel}/team.code-workspace");
    app.submit_input_prompt();
    let dest = tmp.path().join(&rel).join("team.code-workspace");
    assert!(dest.is_file(), "{}", app.status);
    assert!(
        !std::env::current_dir().unwrap().join(&rel).exists(),
        "nothing under the launch directory"
    );
    assert!(
        app.status.contains(&dest.display().to_string()),
        "{}",
        app.status
    );
    assert!(app.status.contains("created folder"), "{}", app.status);
}

/// #1602: a bare name gets the `.code-workspace` extension.
#[test]
fn save_workspace_as_adds_the_extension_to_a_bare_name() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.run_command(crate::widgets::command_palette::Command::SaveWorkspaceAs);
    app.input_prompt.as_mut().unwrap().value = tmp.path().join("team").display().to_string();
    app.submit_input_prompt();
    assert!(
        tmp.path().join("team.code-workspace").is_file(),
        "{}",
        app.status
    );
    assert!(!tmp.path().join("team").exists());
}

/// Negative (#1602): the suggested absolute path is written where it says,
/// with no "created folder" note for a folder that exists.
#[test]
fn save_workspace_as_keeps_the_suggested_path() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.run_command(crate::widgets::command_palette::Command::SaveWorkspaceAs);
    let suggested = PathBuf::from(&app.input_prompt.as_ref().unwrap().value);
    app.submit_input_prompt();
    assert!(suggested.is_file(), "{}", app.status);
    assert!(!app.status.contains("created folder"), "{}", app.status);
}

/// #1012: Save Workspace As suggests the team's committed file. Listing the
/// same folders, it is left alone; with another folder, saving asks first,
/// and a yes changes only `folders`, keeping names, configs and comments.
#[test]
fn save_workspace_as_asks_before_changing_a_team_file_and_keeps_the_rest_of_it() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().canonicalize().unwrap();
    for d in ["wsx", "wsx_lib", "extra"] {
        std::fs::create_dir(root.join(d)).unwrap();
    }
    let file = root.join("wsx/wsx.code-workspace");
    let team = "{\n  \"folders\": [ { \"path\": \".\" }, { \"path\": \"../wsx_lib\", \"name\": \"shared lib\" } ],\n  // F5 runs the app\n  \"launch\": { \"configurations\": [ { \"name\": \"Run app\" } ] }\n}\n";
    std::fs::write(&file, team).unwrap();
    let mut app = App::new(root.join("wsx")).unwrap();
    app.open_workspace_file(&file);
    let save = |app: &mut App| {
        app.run_command(crate::widgets::command_palette::Command::SaveWorkspaceAs);
        assert_eq!(
            app.input_prompt.as_ref().unwrap().value,
            file.display().to_string()
        );
        app.submit_input_prompt();
    };
    save(&mut app);
    assert!(app.input_prompt.is_none(), "nothing to ask: {}", app.status);
    assert!(app.status.contains("already up to date"), "{}", app.status);
    assert_eq!(std::fs::read_to_string(&file).unwrap(), team);

    app.add_workspace_folder(root.join("extra"));
    save(&mut app);
    assert!(app.input_prompt.as_ref().is_some_and(|p| p.confirm));
    app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
        .unwrap();
    assert!(app.input_prompt.is_none());
    assert_eq!(
        std::fs::read_to_string(&file).unwrap(),
        team,
        "Esc keeps it"
    );

    save(&mut app);
    app.submit_input_prompt();
    let out = std::fs::read_to_string(&file).unwrap();
    assert!(
        app.status.starts_with("Saved workspace (3 folders)"),
        "{}",
        app.status
    );
    assert_eq!(
        out,
        team.replace(
            "\"shared lib\" }",
            "\"shared lib\" }, {\"path\":\"../extra\"}"
        )
    );
}

#[test]
fn workspace_folder_set_persists_across_sessions_and_reroots() {
    // #153: the folder set lived only in memory — reopening the same
    // workspace came back single-folder.
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a");
    let b = tmp.path().join("b");
    let c = tmp.path().join("c");
    std::fs::create_dir(&a).unwrap();
    std::fs::create_dir(&b).unwrap();
    std::fs::create_dir(&c).unwrap();
    let store = tmp.path().join("folders.json");

    let mut app = App::new(a.clone()).unwrap();
    app.workspace_folders_path = store.clone();
    app.add_workspace_folder(b.clone());
    app.add_workspace_folder(c.clone());

    // A fresh session on the same primary restores the arrangement.
    let mut app2 = App::new(a.clone()).unwrap();
    app2.workspace_folders_path = store.clone();
    app2.restore_workspace_folders();
    let b_canon = b.canonicalize().unwrap();
    let c_canon = c.canonicalize().unwrap();
    let roots: Vec<PathBuf> = app2.roots.iter().map(Path::to_path_buf).collect();
    assert!(
        roots.contains(&b_canon) && roots.contains(&c_canon),
        "both folders come back: {roots:?}"
    );

    // A vanished folder is dropped from the record, not restored.
    std::fs::remove_dir_all(&c).unwrap();
    let mut app3 = App::new(a.clone()).unwrap();
    app3.workspace_folders_path = store.clone();
    app3.restore_workspace_folders();
    let roots: Vec<PathBuf> = app3.roots.iter().map(Path::to_path_buf).collect();
    assert!(roots.contains(&b_canon) && !roots.contains(&c_canon));
    let map = crate::workspace::load_folders(&store);
    assert_eq!(
        map.get(&a.display().to_string()),
        Some(&vec![b_canon.clone()]),
        "the record rewrites to what survived"
    );

    // Removing the last secondary folder prunes the key.
    app3.remove_workspace_folder(b_canon.clone());
    assert!(
        !crate::workspace::load_folders(&store).contains_key(&a.display().to_string()),
        "a single-folder workspace never lingers in the store"
    );

    // Re-rooting into a workspace with a saved set restores it.
    crate::workspace::save_folders_for_root(
        &store,
        &b_canon.display().to_string(),
        vec![a.clone()],
    )
    .unwrap();
    app3.change_workspace_root(b_canon.clone());
    let roots: Vec<PathBuf> = app3.roots.iter().map(Path::to_path_buf).collect();
    assert!(
        roots.contains(&a.canonicalize().unwrap()),
        "the new primary's saved folders come back with the re-root: {roots:?}"
    );
}

#[test]
fn focusing_a_secondary_roots_file_rebinds_the_test_runner_and_tasks() {
    // #151: the runner gate and the worker resolved against the primary
    // only — from a secondary folder's file, Run All refused with "no
    // test runner" even when that folder is a cargo project.
    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("plain");
    let proj = tmp.path().join("proj");
    std::fs::create_dir(&plain).unwrap();
    std::fs::create_dir_all(proj.join("src")).unwrap();
    std::fs::write(
        proj.join("Cargo.toml"),
        "[package]\nname = \"proj\"\nversion = \"0.0.1\"\nedition = \"2021\"\n",
    )
    .unwrap();
    std::fs::write(
        proj.join("src/main.rs"),
        "fn main() {}\n#[test]\nfn proj_truth() {}\n",
    )
    .unwrap();
    std::fs::write(proj.join("Makefile"), "build:\n\techo hi\n").unwrap();

    let mut app = App::new(plain.clone()).unwrap();
    app.add_workspace_folder(proj.clone());
    let proj_canon = proj.canonicalize().unwrap();

    // Primary focused: no runner, the gate refuses.
    app.status.clear();
    app.run_all_tests();
    assert_eq!(app.status, crate::testing::NO_RUNNER_STATUS);

    // Focus the cargo project's file: the follow tick rebinds the
    // Testing stack and the gate passes.
    app.editor
        .open_pinned(&proj_canon.join("src/main.rs"))
        .unwrap();
    settle_test_root(&mut app);
    assert_eq!(
        app.active_test_root, proj_canon,
        "the Testing stack follows"
    );
    app.status.clear();
    app.run_all_tests();
    assert_ne!(
        app.status,
        crate::testing::NO_RUNNER_STATUS,
        "the secondary folder's cargo runner is found"
    );

    // The open-Testing-view path discovers against the active folder:
    // this is the exact live sequence, and the discovery gate had its own
    // primary-root check the entry-point gates didn't cover.
    app.open_testing_view();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120);
    loop {
        let _ = app.test_worker.drain(&mut app.testing);
        if app
            .testing
            .cases_for_test()
            .iter()
            .any(|(name, _)| name.contains("proj_truth"))
        {
            break;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "discovery never returned the secondary project's tests; cases: {:?}",
            app.testing.cases_for_test()
        );
        std::thread::sleep(std::time::Duration::from_millis(50));
    }

    // Drain the discovery's Finished before running: breaking out on the
    // first case leaves the panel busy, and run_all_tests refuses while a
    // discovery is in flight.
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
    while app.testing.is_busy() {
        let _ = app.test_worker.drain(&mut app.testing);
        assert!(
            std::time::Instant::now() < deadline,
            "discovery never reported Finished"
        );
        std::thread::sleep(std::time::Duration::from_millis(20));
    }

    // And a full RUN executes in that folder: the case must go green,
    // which only happens if cargo built and ran the secondary project.
    app.run_all_tests();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(120);
    loop {
        let _ = app.test_worker.drain(&mut app.testing);
        if app.testing.cases_for_test().iter().any(|(name, status)| {
            name.contains("proj_truth") && *status == crate::testing::model::TestStatus::Passed
        }) {
            break;
        }
        assert!(
            std::time::Instant::now() < deadline,
            "the secondary project's test never passed; cases: {:?}",
            app.testing.cases_for_test()
        );
        std::thread::sleep(std::time::Duration::from_millis(50));
    }

    // Tasks discover against the active folder too.
    let tasks = crate::tasks::discover_tasks(&app.active_workspace_root());
    assert!(
        tasks.iter().any(|t| t.command.contains("make")),
        "the secondary folder's Makefile tasks are offered: {:?}",
        tasks.iter().map(|t| t.command.clone()).collect::<Vec<_>>()
    );
}

#[test]
fn focusing_a_secondary_roots_file_flips_source_control_to_its_repo() {
    // #149: git state was singular (one worker at the primary), so a
    // secondary root's repository was invisible — no branch, no panel
    // entries, no scm_root for its operations.
    let tmp = tempfile::tempdir().unwrap();
    let plain = tmp.path().join("plain");
    let repo = tmp.path().join("repo");
    std::fs::create_dir(&plain).unwrap();
    std::fs::create_dir(&repo).unwrap();
    for args in [
        vec!["init", "-q", "-b", "trunk"],
        vec!["config", "user.email", "a@b"],
        vec!["config", "user.name", "a"],
    ] {
        git_ok(&repo, &args);
    }
    std::fs::write(repo.join("seed.txt"), "one\n").unwrap();
    for args in [vec!["add", "."], vec!["commit", "-qm", "init"]] {
        git_ok(&repo, &args);
    }
    std::fs::write(repo.join("seed.txt"), "one\ntwo\n").unwrap();

    // Primary is NOT a repo; the repo joins as a secondary folder.
    let mut app = App::new(plain.clone()).unwrap();
    app.add_workspace_folder(repo.clone());
    let repo_canon = repo.canonicalize().unwrap();

    // Focus the secondary root's file: the SCM surfaces must follow.
    app.editor
        .open_pinned(&repo_canon.join("seed.txt"))
        .unwrap();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(15);
    let mut flipped = false;
    while std::time::Instant::now() < deadline {
        let _ = app.drain_git_responses();
        if app.source_control.status.branch.as_deref() == Some("trunk")
            && app
                .source_control
                .entries
                .iter()
                .any(|e| e.path == "seed.txt")
        {
            flipped = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(20));
    }
    assert!(
        flipped,
        "the panel must show the secondary root's repo (branch + changes); saw {:?} / {:?}",
        app.source_control.status.branch,
        app.source_control
            .entries
            .iter()
            .map(|e| e.path.clone())
            .collect::<Vec<_>>()
    );
    assert_eq!(
        app.scm_root().canonicalize().unwrap(),
        repo_canon,
        "panel operations address the active repo's toplevel"
    );

    // The mutation path must hit the SAME repository the panel shows
    // (#150 review: staging addressed the active repo while commit still
    // ran against the primary — stage in B, commit in A).
    app.stage_all_source_control();
    app.source_control.message = String::from("from the panel");
    app.commit_staged_source_control();
    wait_for_git_net(&mut app);
    let log = git_ok(&repo, &["log", "--oneline"]);
    assert!(
        log.contains("from the panel"),
        "the commit lands in the ACTIVE repo, the one the panel shows"
    );
}

#[test]
fn add_and_remove_workspace_folder_fan_out_across_tree_index_and_search() {
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("alpha");
    let b = tmp.path().join("beta");
    std::fs::create_dir(&a).unwrap();
    std::fs::create_dir(&b).unwrap();
    std::fs::write(a.join("main.rs"), "fn main() {}\n").unwrap();
    std::fs::write(b.join("util.rs"), "pub fn u() {}\n").unwrap();
    let mut app = App::new(a.clone()).unwrap();

    app.add_workspace_folder(b.clone());

    let b_canon = b.canonicalize().unwrap();
    assert!(app.roots.is_multi(), "the model grew");
    assert!(
        app.tree.root_paths().any(|r| r == b_canon),
        "the Explorer gained the section"
    );
    assert_eq!(app.search.roots.len(), 2, "the search panel spans both");
    assert_eq!(
        app.fs_watch_extra.len(),
        1,
        "the added root gets its own watcher share"
    );
    // The Cmd+P index rebuild runs on a walker thread: poll it in, then
    // both roots' files must appear under root-name prefixes.
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
    loop {
        app.try_install_pending_init();
        if let Some(idx) = app.file_finder_index.as_ref() {
            let rels: Vec<&str> = idx.iter().map(|e| e.rel.as_str()).collect();
            if rels.contains(&"alpha/main.rs") && rels.contains(&"beta/util.rs") {
                break;
            }
        }
        assert!(
            std::time::Instant::now() < deadline,
            "the multi-root Cmd+P index never landed; entries: {:?}",
            app.file_finder_index
                .as_ref()
                .map(|i| i.iter().map(|e| e.rel.clone()).collect::<Vec<_>>())
        );
        std::thread::sleep(std::time::Duration::from_millis(20));
    }

    // Adding it again is a no-op with an explanation.
    app.add_workspace_folder(b.clone());
    assert_eq!(app.status, "Folder is already in the workspace");

    // The primary is not removable; the secondary is, and everything
    // collapses back to single-root.
    let a_canon = a.canonicalize().unwrap();
    app.remove_workspace_folder(a_canon);
    assert!(app.roots.is_multi(), "removing the primary is refused");
    app.remove_workspace_folder(b_canon.clone());
    assert!(!app.roots.is_multi());
    assert!(
        !app.tree.root_paths().any(|r| r == b_canon),
        "the section left the Explorer"
    );
    assert_eq!(app.search.roots.len(), 1);
    assert!(app.fs_watch_extra.is_empty());
}

#[test]
fn delete_filters_workspace_roots_out_of_a_mixed_selection() {
    // #146 review: the context menu forwards a mixed multi-selection
    // wholesale into MenuAction::Delete, and Select All marks the root
    // rows too — so the batch path must refuse root rows at the
    // delete_paths choke point, not rely on the per-node guard.
    let tmp = tempfile::tempdir().unwrap();
    let a = tmp.path().join("a");
    let b = tmp.path().join("b");
    std::fs::create_dir(&a).unwrap();
    std::fs::create_dir(&b).unwrap();
    std::fs::write(a.join("f.txt"), "x\n").unwrap();
    let mut app = App::new(a.clone()).unwrap();
    app.tree.add_root(b.clone());

    // Mixed batch: a real file plus BOTH root rows (what Select All +
    // right-click Delete assembles).
    app.delete_paths(vec![a.join("f.txt"), a.clone(), b.clone()]);
    let prompt = app.input_prompt.as_ref().expect("the confirm opens");
    match &prompt.purpose {
        crate::widgets::input_prompt::InputPurpose::TreeDelete { paths } => {
            assert_eq!(
                paths,
                &vec![a.join("f.txt")],
                "both workspace roots are filtered; only the file survives to the confirm"
            );
        }
        other => panic!("wrong prompt purpose: {other:?}"),
    }
    app.input_prompt = None;

    // A batch of ONLY roots opens no prompt at all.
    app.delete_paths(vec![a.clone(), b.clone()]);
    assert!(
        app.input_prompt.is_none(),
        "a roots-only batch is refused outright"
    );
    assert_eq!(app.status, "Workspace roots cannot be deleted");
}

#[test]
fn answering_no_to_the_trash_confirm_keeps_the_file() {
    // #855: the confirm was a text field prefilled with `trash`. Clearing it
    // and typing `no` read as an answer, yet Enter trashed the file anyway.
    // It is a yes/no question now: typing and pasting go nowhere, and N
    // keeps the file.
    let tmp = tempfile::tempdir().unwrap();
    let file = tmp.path().join("scratch.py");
    std::fs::write(&file, "x\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.focus_pane(Pane::Tree);

    app.delete_paths(vec![file.clone()]);
    for _ in 0..5 {
        app.handle_key(key(KeyCode::Backspace, KeyModifiers::NONE))
            .unwrap();
    }
    app.handle_key(key(KeyCode::Char('x'), KeyModifiers::NONE))
        .unwrap();
    app.handle_paste("no");
    app.handle_key(key(KeyCode::Char('y'), KeyModifiers::CONTROL))
        .unwrap();
    let prompt = app
        .input_prompt
        .as_ref()
        .expect("keys that do not answer leave the question up");
    assert_eq!(prompt.value, "", "nothing typed or pasted lands in it");

    app.handle_key(key(KeyCode::Char('n'), KeyModifiers::NONE))
        .unwrap();
    assert!(app.input_prompt.is_none(), "N answers no");
    assert!(file.exists(), "the file stays where it was");
}

#[test]
fn a_markdown_file_opened_from_the_explorer_scrolls_its_preview() {
    let tmp = tempfile::tempdir().unwrap();
    let body: String = (0..200).map(|i| format!("line {i}\n")).collect();
    std::fs::write(tmp.path().join("notes.md"), body).unwrap();

    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    open_from_explorer(&mut app, "notes.md");
    assert!(matches!(app.focus, Pane::Editor));
    app.toggle_markdown_preview();
    assert!(app.editor.markdown_preview.is_some());

    app.handle_key(key(KeyCode::Down, KeyModifiers::NONE))
        .unwrap();
    let scrolled = app
        .editor
        .markdown_preview
        .as_ref()
        .map(|p| p.scroll)
        .unwrap();
    assert!(scrolled > 0, "Down must scroll the rendered Markdown");
}

#[test]
fn a_text_file_opened_from_the_explorer_takes_the_arrow_keys() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("main.rs"), "one\ntwo\nthree\n").unwrap();

    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    open_from_explorer(&mut app, "main.rs");
    let before = app.tree.selected;
    app.handle_key(key(KeyCode::Down, KeyModifiers::NONE))
        .unwrap();
    assert_eq!(app.editor.cursor_row, 1, "Down moves the caret in the file");
    assert_eq!(
        app.tree.selected, before,
        "the Explorer selection must not move once the file is open"
    );
}

#[test]
fn right_arrow_in_the_explorer_previews_a_file_without_stealing_focus() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("peek.txt"), "hello\n").unwrap();

    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    let idx = app
        .tree
        .nodes
        .iter()
        .position(|n| n.path.file_name().is_some_and(|f| f == "peek.txt"))
        .expect("peek.txt in the tree");
    app.tree.selected = idx;
    app.focus_pane(Pane::Tree);
    app.handle_key(key(KeyCode::Right, KeyModifiers::NONE))
        .unwrap();

    assert_eq!(app.editor.lines[0], "hello");
    assert!(
        matches!(app.focus, Pane::Tree),
        "Right is an Explorer navigation gesture: it previews but keeps the keyboard in the tree"
    );
}

/// The invariant behind every test above, applied to one file of each kind
/// croft previews: opening a file must leave the keyboard where that file's
/// navigation keys are handled. Add a new preview type, add a row here.
#[test]
fn opening_any_file_type_from_the_explorer_lands_the_keyboard_in_the_editor() {
    require_pdf_backend();
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("plain.txt"), "alpha\nbeta\n").unwrap();
    std::fs::write(tmp.path().join("code.rs"), "fn main() {}\n").unwrap();
    std::fs::write(tmp.path().join("notes.md"), "# Title\n\nbody\n").unwrap();
    std::fs::write(tmp.path().join("data.csv"), csv_body(5)).unwrap();
    std::fs::write(tmp.path().join("data.tsv"), "a\tb\n1\t2\n").unwrap();
    std::fs::write(tmp.path().join("conf.json"), "{\"a\": 1}\n").unwrap();
    write_test_png(&tmp.path().join("shot.png"));
    write_three_page_pdf(&tmp.path().join("doc.pdf"));

    for name in [
        "plain.txt",
        "code.rs",
        "notes.md",
        "data.csv",
        "data.tsv",
        "conf.json",
        "shot.png",
        "doc.pdf",
    ] {
        let mut app = App::new(tmp.path().to_path_buf()).unwrap();
        open_from_explorer(&mut app, name);
        assert!(
            matches!(app.focus, Pane::Editor),
            "{name}: opening it must hand the keyboard to the editor, or its navigation keys are dead"
        );
    }
}

#[test]
fn clicking_any_file_type_in_the_explorer_lands_the_keyboard_in_the_editor() {
    require_pdf_backend();
    let tmp = tempfile::tempdir().unwrap();
    std::fs::write(tmp.path().join("plain.txt"), "alpha\nbeta\n").unwrap();
    std::fs::write(tmp.path().join("data.csv"), csv_body(5)).unwrap();
    std::fs::write(tmp.path().join("notes.md"), "# Title\n").unwrap();
    write_test_png(&tmp.path().join("shot.png"));
    write_three_page_pdf(&tmp.path().join("doc.pdf"));

    for name in ["plain.txt", "data.csv", "notes.md", "shot.png", "doc.pdf"] {
        let mut app = App::new(tmp.path().to_path_buf()).unwrap();
        click_open_in_explorer(&mut app, name);
        assert!(
            matches!(app.focus, Pane::Editor),
            "{name}: one click must hand the keyboard to the editor"
        );
    }
}

/// Selecting a folder row must not move focus: the click is a tree gesture and
/// the user is still browsing.
#[test]
fn clicking_a_folder_in_the_explorer_keeps_the_keyboard_in_the_tree() {
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir(tmp.path().join("src")).unwrap();
    std::fs::write(tmp.path().join("src/lib.rs"), "fn main() {}\n").unwrap();

    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    click_open_in_explorer(&mut app, "src");
    assert!(
        matches!(app.focus, Pane::Tree),
        "expanding a folder is a tree gesture: the keyboard stays in the Explorer"
    );
}

/// Reveal-in-explorer targets the sidebar, so it must work even when
/// auto-hide has collapsed it — otherwise the command silently does nothing
/// visible.
#[test]
fn reveal_in_explorer_shows_a_collapsed_sidebar() {
    let tmp = tempfile::tempdir().unwrap();
    let f = tmp.path().join("a.rs");
    std::fs::write(&f, "fn main() {}\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.sidebar_auto_hide = true;
    app.show_tree = true;
    app.focus_pane(Pane::Editor);
    // #302: the collapse arrives on the dwell tick rather than the focus move.
    // What this test protects — that a sidebar-targeting command reveals an
    // ALREADY-COLLAPSED sidebar — is unchanged; it just has to reach that
    // collapsed state the way the app now does.
    app.tick_sidebar_auto_hide_at(
        std::time::Instant::now() + crate::app::sidebar_dwell::AUTO_HIDE_DWELL,
    );
    assert!(!app.show_tree, "collapsed first");

    app.reveal_in_explorer(f);
    assert!(
        app.show_tree,
        "a sidebar-targeting command reveals the sidebar"
    );
}

/// #345: the Explorer's unreviewed dots track the ledger, on every path
/// that changes it.
///
/// The predicate and the painting are tested in `file_tree`; this is the
/// wiring between them, which is where a decoration feature actually breaks.
/// A dot that lags the ledger is worse than no dot at all, because "no dot"
/// is precisely what a reviewed file looks like — so a stale one tells the
/// user they have reviewed something they have not.
///
/// **The drain is driven for real, not stood in for.** An earlier version of
/// this test called `attribute_writes_to_working_agents` and then
/// `sync_agent_lane_decorations` by hand, which tests the helper and not the
/// wiring: deleting the drain's own sync left every assertion passing. Since
/// the drain is the path that fires on every agent write, that was the one
/// call site the feature cannot afford to have untested.
#[test]
fn the_explorer_dots_follow_the_agent_ledger() {
    let tmp = tempfile::tempdir().unwrap();
    let touched = tmp.path().join("touched.rs");
    std::fs::write(&touched, "fn a() {}\n").unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    assert!(
        app.tree.agent_touched.is_empty(),
        "staging: nothing decorated before an agent writes"
    );

    app.terminals[0].set_agent(Some(crate::agents::AgentLane {
        name: String::from("claude"),
        status: crate::agents::AgentStatus::Working,
    }));

    // Let the watcher establish itself first, as the other fs-watch tests
    // do: a write racing the initial registration is simply not delivered,
    // which would make this test flaky rather than wrong.
    for _ in 0..20 {
        let _ = app.drain_fs_events();
        std::thread::sleep(std::time::Duration::from_millis(10));
    }

    // Through the real drain: write the file, let the watcher deliver it,
    // and call the function the event loop calls. No explicit sync here —
    // that is the property under test.
    std::fs::write(&touched, "fn a() { todo!() }\n").unwrap();
    crate::test_budget::await_spawned(
        crate::test_budget::tests::SHELL_PAINT_BASE,
        "the watcher event to reach the tree",
        || {
            app.drain_fs_events();
            app.tree.agent_touched.contains(&touched)
        },
    );
    assert!(
        app.tree.agent_touched.contains(&touched),
        "the drain must light the row it attributed, without a nudge \
         (ledger has {} unreviewed)",
        app.agent_ledger.unreviewed_files()
    );
    assert!(app.tree.is_agent_touched(&touched));
    // The chip counts the set the tree draws. Asserted HERE, where both
    // sides are 1: after the lane is cleared below they are both 0, and an
    // equality that only ever compares 0 to 0 cannot fail.
    assert_eq!(
        app.agent_ledger.unreviewed_files(),
        app.tree.agent_touched.len(),
        "the badge and the dots must be the same number"
    );

    // Reviewing goes through the public entry point, which syncs itself.
    assert!(app.mark_agent_file_reviewed("claude", &touched));
    assert!(
        !app.tree.agent_touched.contains(&touched),
        "marking reviewed must clear the dot without a further nudge"
    );

    // Mark-the-whole-lane reviewed is the other syncing entry point.
    let mut changed = std::collections::BTreeSet::new();
    changed.insert(touched.clone());
    std::fs::write(&touched, "fn a() { 1 }\n").unwrap();
    assert!(app.attribute_writes_to_working_agents(&changed));
    app.sync_agent_lane_decorations();
    assert!(
        app.tree.agent_touched.contains(&touched),
        "staging: lit again"
    );
    app.mark_agent_lane_reviewed("claude");
    assert!(
        app.tree.agent_touched.is_empty(),
        "marking the lane reviewed must clear every dot"
    );
}

/// #345: removing a workspace folder takes its review queue with it.
///
/// The rows leave the tree on their own, so no stale dot renders — but the
/// status chip counts the same set, and a count that outlives the folder it
/// describes sends the user hunting for rows that are not there.
#[test]
fn removing_a_folder_drops_its_rows_from_the_review_queue() {
    let primary = tempfile::tempdir().unwrap();
    let secondary = tempfile::tempdir().unwrap();
    let in_second = secondary.path().join("s.rs");
    std::fs::write(&in_second, "fn s() {}\n").unwrap();
    let mut app = App::new(primary.path().to_path_buf()).unwrap();
    app.roots.add(secondary.path().to_path_buf());

    app.terminals[0].set_agent(Some(crate::agents::AgentLane {
        name: String::from("claude"),
        status: crate::agents::AgentStatus::Working,
    }));
    let mut changed = std::collections::BTreeSet::new();
    changed.insert(in_second.clone());
    assert!(app.attribute_writes_to_working_agents(&changed));
    app.sync_agent_lane_decorations();
    assert_eq!(
        app.tree.agent_touched.len(),
        1,
        "staging: the secondary root's file is queued"
    );

    app.remove_workspace_folder(secondary.path().to_path_buf());
    assert!(
        app.tree.agent_touched.is_empty(),
        "the removed folder's rows outlived it: {:?}",
        app.tree.agent_touched
    );
    assert_eq!(app.agent_ledger.unreviewed_files(), 0);
}

#[test]
fn cmd_k_e_still_reveals_in_the_explorer() {
    // The design proposal had picked Cmd+K E for restore-all. It is croft's
    // reveal-in-Explorer, and an `e` arm added below the existing one would
    // never have been reached - a chord that silently does nothing. This
    // pins the collision so the mistake cannot be made a second time.
    let (_tmp, mut app, _term) = app_with_terminal_panes(2);
    app.toggle_terminal_collapse(1);
    assert!(app.handle_cmd_k_chord(key(KeyCode::Char('e'), KeyModifiers::NONE)));
    // BOTH halves, in this test. "Did not collapse" alone passes against a
    // build where `e` reaches nothing at all, while the test's NAME goes on
    // claiming it revealed. With no file open the reveal arm says so, and
    // that is the presence half over the same state.
    assert_eq!(
        app.status, "Reveal in Explorer View: no active file",
        "Cmd+K E must still reach reveal-in-Explorer"
    );
    assert!(
        app.terminals[1].collapsed,
        "and must not have been quietly taken over by collapse"
    );
}

/// While a session is shown the tree carries the list as its last section;
/// a change swaps that section rather than stacking a second one.
#[test]
fn the_tree_breakpoint_section_follows_changes_without_duplicating() {
    use crate::widgets::run_debug::{DebugRow, DebugRowKind};
    let (_tmp, mut app, a, _b) = app_with_breakpoints();
    app.run_debug.debug_active = true;
    app.run_debug.debug_rows = vec![DebugRow {
        indent: 0,
        kind: DebugRowKind::Header {
            title: String::from("CALL STACK"),
        },
    }];
    app.sync_breakpoint_list();
    let count = |app: &App| {
        (
            app.run_debug
                .debug_rows
                .iter()
                .filter(
                    |r| matches!(&r.kind, DebugRowKind::Header { title } if title == "BREAKPOINTS"),
                )
                .count(),
            app.run_debug
                .debug_rows
                .iter()
                .filter(|r| matches!(r.kind, DebugRowKind::Breakpoint { .. }))
                .count(),
        )
    };
    assert_eq!(count(&app), (1, 3));
    app.editor.breakpoints.get_mut(&a).unwrap().insert(2);
    app.sync_breakpoint_list();
    assert_eq!(count(&app), (1, 4), "one header, one more row");
    assert!(matches!(
        &app.run_debug.debug_rows[0].kind,
        DebugRowKind::Header { title } if title == "CALL STACK"
    ));
}

#[test]
fn renaming_a_file_in_the_explorer_keeps_its_local_history() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().canonicalize().unwrap();
    std::fs::write(root.join("notes.txt"), "v3\n").unwrap();
    let (mut app, _hist) = app_with_history(&root);
    crate::history::record_in(&app.history_root, &root.join("notes.txt"), b"v1\n", 1_000).unwrap();
    crate::history::record_in(&app.history_root, &root.join("notes.txt"), b"v2\n", 60_000).unwrap();
    assert!(app.run_file_move(FileMove::Rename {
        parent: root.clone(),
        old: root.join("notes.txt"),
        new_name: String::from("journal.txt"),
    }));
    let moved = crate::history::entries_in(&app.history_root, &root.join("journal.txt"));
    assert_eq!(
        moved.iter().map(|s| s.millis).collect::<Vec<_>>(),
        [60_000, 1_000]
    );
    assert!(crate::history::entries_in(&app.history_root, &root.join("notes.txt")).is_empty());
}

#[test]
fn cutting_a_folder_into_another_keeps_its_files_history() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().canonicalize().unwrap();
    std::fs::create_dir_all(root.join("docs")).unwrap();
    std::fs::create_dir_all(root.join("archive")).unwrap();
    std::fs::write(root.join("docs").join("a.md"), "a\n").unwrap();
    let (mut app, _hist) = app_with_history(&root);
    let old = root.join("docs").join("a.md");
    crate::history::record_in(&app.history_root, &old, b"a0\n", 1_000).unwrap();
    assert!(app.run_file_move(FileMove::Paste {
        dest_dir: root.join("archive"),
        paths: vec![root.join("docs")],
    }));
    let new = root.join("archive").join("docs").join("a.md");
    assert!(new.exists());
    assert_eq!(crate::history::entries_in(&app.history_root, &new).len(), 1);
    assert!(crate::history::entries_in(&app.history_root, &old).is_empty());
}

#[test]
fn moving_files_and_folders_carries_every_open_tab_along() {
    let tmp = tempfile::tempdir().unwrap();
    let root = tmp.path().canonicalize().unwrap();
    std::fs::create_dir_all(root.join("dir")).unwrap();
    std::fs::create_dir_all(root.join("dest")).unwrap();
    std::fs::write(root.join("a.txt"), "a").unwrap();
    std::fs::write(root.join("dir/b.txt"), "b").unwrap();
    std::fs::write(root.join("c.txt"), "c").unwrap();
    let mut app = App::new(root.clone()).unwrap();
    app.editor.open_pinned(&root.join("a.txt")).unwrap();
    app.editor.open_in_new_tab(&root.join("dir/b.txt")).unwrap();
    app.editor.open_in_new_tab(&root.join("c.txt")).unwrap();
    // c.txt is active; a.txt and dir/b.txt sit in background tabs.
    assert!(app.apply_paste_or_drop(
        &root.join("dest"),
        &[root.join("a.txt"), root.join("dir")],
        ExplorerClipMode::Cut
    ));
    let paths: Vec<_> = app
        .editor
        .editors
        .iter()
        .filter_map(|e| e.path.clone())
        .collect();
    assert!(paths.contains(&root.join("dest/a.txt")), "{paths:?}");
    assert!(paths.contains(&root.join("dest/dir/b.txt")), "{paths:?}");
}

#[test]
fn the_explorer_dims_files_missing_from_the_scrubbed_commit() {
    // #371: at a commit, a file its tree lacks dims; the working tree and
    // closing the scrubber clear it.
    let repo = scrub_repo();
    std::fs::write(repo.path().join("new.txt"), "fresh\n").unwrap();
    let mut app = App::new(repo.path().to_path_buf()).unwrap();
    let new = repo.path().join("new.txt");
    let old = repo.path().join("a.txt");
    app.scrub_history();
    assert!(app.tree.scrub_tree.is_none(), "opening parks at the tree");
    app.handle_scrubber_key(KeyCode::Left);
    assert!(app.tree.is_absent_at_scrub(&new), "new.txt is not in HEAD");
    assert!(!app.tree.is_absent_at_scrub(&old), "a.txt is");
    app.handle_scrubber_key(KeyCode::Home);
    assert!(
        !app.tree.is_absent_at_scrub(&new),
        "Home clears the dimming"
    );
    app.handle_scrubber_key(KeyCode::Left);
    assert!(app.tree.is_absent_at_scrub(&new));
    app.handle_scrubber_key(KeyCode::Right);
    assert!(
        !app.tree.is_absent_at_scrub(&new),
        "stepping to the working tree clears it"
    );
    app.handle_scrubber_key(KeyCode::Left);
    app.handle_scrubber_key(KeyCode::Esc);
    assert!(app.tree.scrub_tree.is_none(), "closing clears it");
}

#[test]
fn saving_a_file_whose_folder_was_removed_puts_the_folder_back() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = app_with_plan_whose_folder_was_removed(tmp.path());
    // The first press may stop to say the file changed on disk; the second
    // is the consent to write it anyway.
    app.save();
    if app.editor.dirty {
        app.save();
    }
    assert!(!app.editor.dirty, "still unsaved: {}", app.status);
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("notes/plan.md")).unwrap(),
        "# Plan\n\n- step one\n- step two\n"
    );
}

#[test]
fn a_save_whose_folder_is_now_a_file_still_fails_and_leaves_that_file_alone() {
    // Negative: putting a missing folder back must not paper over a path
    // that can't be a folder. A file now sits where the folder was.
    let tmp = tempfile::tempdir().unwrap();
    let mut app = app_with_plan_whose_folder_was_removed(tmp.path());
    std::fs::write(tmp.path().join("notes"), "a file, not a folder\n").unwrap();
    app.save();
    app.save();
    assert!(app.editor.dirty, "the edits are still only in the buffer");
    assert!(app.status.starts_with("Save failed"), "{}", app.status);
    assert_eq!(
        std::fs::read_to_string(tmp.path().join("notes")).unwrap(),
        "a file, not a folder\n"
    );
}

/// #852: File: New File… / New Folder… open the Explorer's create prompt
/// from any pane: beside the active file from the editor, in the Explorer
/// selection while the Explorer has focus (as its own chords do).
#[test]
fn new_file_and_folder_from_the_palette_open_the_create_prompt() {
    use crate::widgets::command_palette::Command;
    let tmp = tempfile::tempdir().unwrap();
    std::fs::create_dir_all(tmp.path().join("sub")).unwrap();
    let mut app = app_with_open_file(tmp.path(), "sub/a.txt", "x");
    let file_dir = app
        .editor
        .path
        .as_deref()
        .and_then(Path::parent)
        .map(Path::to_path_buf)
        .unwrap();
    assert!(file_dir.ends_with("sub"));
    app.run_command(Command::NewFile);
    match app.prompt.as_ref() {
        Some(Prompt {
            kind: PromptKind::Create(CreateKind::File),
            target_dir,
            ..
        }) => assert_eq!(target_dir, &file_dir),
        _ => panic!("File: New File… must open the New File prompt"),
    }
    app.prompt = None;
    app.focus_pane(Pane::Tree);
    app.sidebar_view = SidebarView::Explorer;
    app.run_command(Command::NewFolder);
    let explorer_dir = app.explorer_create_target_dir();
    match app.prompt.as_ref() {
        Some(Prompt {
            kind: PromptKind::Create(CreateKind::Folder),
            target_dir,
            ..
        }) => assert_eq!(target_dir, &explorer_dir),
        _ => panic!("File: New Folder… must open the New Folder prompt"),
    }
}

#[test]
fn a_click_on_the_explorer_outline_still_jumps_with_the_secondary_bar_up() {
    let tmp = tempfile::tempdir().unwrap();
    let (mut app, _) = secondary_outline_app(&tmp, 30);
    let backend = ratatui::backend::TestBackend::new(160, 45);
    let mut term = ratatui::Terminal::new(backend).unwrap();
    term.draw(|f| app.render(f)).unwrap();
    let (left, _) = outline_hosts(&app);
    let (x, y) = find_painted(&term, "sym_004", left).expect("sym_004 in the Explorer");
    left_click(&mut app, x + 2, y);
    assert_eq!(
        app.editor.cursor_row, 8,
        "the caret moves to sym_004's line"
    );
}

fn new_file_prompt(app: &mut App, tmp: &std::path::Path) {
    app.open_create_prompt(CreateKind::File, tmp.to_path_buf());
}

fn type_into_prompt(app: &mut App, text: &str) {
    for c in text.chars() {
        app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
            .unwrap();
    }
}

#[test]
fn the_new_file_prompt_edits_at_a_caret() {
    // New File, New Folder and Rename used to append and pop at the end only:
    // a typo in the middle of a name meant retyping the tail.
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    new_file_prompt(&mut app, tmp.path());
    type_into_prompt(&mut app, "mian.rs");
    // Six Lefts put the caret after the "m"; drop the "i", step over the "a"
    // and put the "i" back where it belongs.
    for _ in 0..6 {
        app.handle_key(key(KeyCode::Left, KeyModifiers::NONE))
            .unwrap();
    }
    app.handle_key(key(KeyCode::Delete, KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Right, KeyModifiers::NONE))
        .unwrap();
    type_into_prompt(&mut app, "i");
    let p = app.prompt.as_ref().expect("prompt stays open");
    assert_eq!(p.buffer, "main.rs");
    app.handle_key(key(KeyCode::Home, KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Delete, KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::End, KeyModifiers::NONE))
        .unwrap();
    app.handle_key(key(KeyCode::Backspace, KeyModifiers::NONE))
        .unwrap();
    assert_eq!(app.prompt.as_ref().unwrap().buffer, "ain.r");
}

#[test]
fn the_new_file_prompt_walks_and_deletes_by_word_and_takes_a_paste_at_the_caret() {
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    new_file_prompt(&mut app, tmp.path());
    type_into_prompt(&mut app, "docs/old name.md");
    app.handle_key(key(KeyCode::Backspace, KeyModifiers::CONTROL))
        .unwrap();
    assert_eq!(app.prompt.as_ref().unwrap().buffer, "docs/old name.");
    app.handle_key(key(KeyCode::Left, KeyModifiers::CONTROL))
        .unwrap();
    app.handle_paste("X\nY");
    let p = app.prompt.as_ref().unwrap();
    assert_eq!(
        p.buffer, "docs/old XYname.",
        "paste drops the newline and lands at the caret"
    );
}

#[test]
fn a_paste_with_quick_open_over_the_new_file_prompt_goes_to_quick_open() {
    // Quick Open opens over a prompt without closing it and takes the keys
    // typed into it; a paste must follow the keys, not change the name the
    // prompt is holding underneath.
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    new_file_prompt(&mut app, tmp.path());
    type_into_prompt(&mut app, "main.rs");
    app.handle_key(key(KeyCode::Char('p'), KeyModifiers::CONTROL))
        .unwrap();
    assert!(app.file_finder.is_some(), "Ctrl+P opens Quick Open");
    assert!(app.prompt.is_some(), "the prompt stays open underneath");
    app.handle_paste("lib.rs");
    assert_eq!(app.file_finder.as_ref().unwrap().query, "lib.rs");
    assert_eq!(app.prompt.as_ref().unwrap().buffer, "main.rs");
}

#[test]
fn a_paste_with_a_modal_over_the_new_file_prompt_leaves_the_name_alone() {
    // A picker or dialog with no text field of its own, opened over a
    // prompt, takes its keys; a paste then must not edit the hidden name.
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    new_file_prompt(&mut app, tmp.path());
    type_into_prompt(&mut app, "main.rs");
    app.open_shortcuts_modal();
    app.handle_paste("lib.rs");
    assert_eq!(app.prompt.as_ref().unwrap().buffer, "main.rs");
    assert!(
        !app.editor.lines.join("\n").contains("lib.rs"),
        "nor does the paste fall through to the editor behind"
    );
}

#[test]
fn ctrl_v_as_a_key_pastes_into_the_new_file_prompt_at_the_caret() {
    // A host with bracketed paste off sends Ctrl/Cmd+V as a key; the prompt
    // reads the clipboard itself, as Quick Open does, instead of dropping it.
    let tmp = tempfile::tempdir().unwrap();
    let mut app = App::new(tmp.path().to_path_buf()).unwrap();
    app.clipboard_reader = || Some(String::from("lib\n"));
    new_file_prompt(&mut app, tmp.path());
    type_into_prompt(&mut app, "src/.rs");
    for _ in 0..3 {
        app.handle_key(key(KeyCode::Left, KeyModifiers::NONE))
            .unwrap();
    }
    app.handle_key(key(KeyCode::Char('v'), KeyModifiers::CONTROL))
        .unwrap();
    assert_eq!(app.prompt.as_ref().unwrap().buffer, "src/lib.rs");
}