git-loom 0.25.0

A Git CLI tool that weaves together multiple feature branches into integration branches
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
use std::collections::HashSet;
use std::path::PathBuf;
use std::sync::mpsc::channel;

use crossterm::event::{Event, KeyEvent, MouseEvent};
use ratatui::style::Style;

use super::*;
use crate::core::repo::{BranchInfo, CommitInfo, ContextCommit, FileChange, UpstreamInfo};
use crate::core::ui::PromptKind;
use crate::tui::widgets::common::diff_line_style;

fn oid(hex_char: char) -> git2::Oid {
    git2::Oid::from_str(&hex_char.to_string().repeat(40)).unwrap()
}

fn file(path: &str, index: char, worktree: char) -> FileChange {
    FileChange {
        path: path.to_string(),
        index,
        worktree,
    }
}

fn commit(c: char, parent: char, message: &str) -> CommitInfo {
    CommitInfo {
        oid: oid(c),
        short_id: c.to_string().repeat(7),
        message: message.to_string(),
        change_id: None,
        parent_oid: Some(oid(parent)),
        files: vec![file("src/parser.rs", 'M', ' ')],
    }
}

fn context_commit(short_hash: &str) -> ContextCommit {
    ContextCommit {
        short_hash: short_hash.to_string(),
        message: "older".to_string(),
        date: "2025-12-31".to_string(),
    }
}

fn upstream() -> UpstreamInfo {
    UpstreamInfo {
        label: "origin/main".to_string(),
        tip_oid: oid('9'),
        merge_base_oid: oid('9'),
        base_short_id: "9999999".to_string(),
        base_message: "base".to_string(),
        base_date: "2026-01-01".to_string(),
        commits_ahead: 0,
    }
}

/// `feature-a` owning one commit `a` on base `9`, two local changes.
fn make_info() -> RepoInfo {
    RepoInfo {
        branch_name: "integration".to_string(),
        upstream: upstream(),
        commits: vec![commit('a', '9', "Add parser")],
        branches: vec![BranchInfo {
            name: "feature-a".to_string(),
            tip_oid: oid('a'),
            remote: None,
        }],
        working_changes: vec![file("a.rs", 'M', ' '), file("b.rs", ' ', 'M')],
        context_commits: Vec::new(),
    }
}

fn snapshot_of(info: RepoInfo) -> Snapshot {
    let ids = IdAllocator::new(info.collect_entities());
    Snapshot {
        workdir: PathBuf::from("."),
        git_dir: PathBuf::from("."),
        cwd_prefix: String::new(),
        info,
        ids,
    }
}

fn make_snapshot() -> Snapshot {
    snapshot_of(make_info())
}

fn make_theme() -> TuiTheme {
    TuiTheme::from_graph_theme(&graph::Theme::dark())
}

fn make_app(snapshot: Snapshot, theme: &TuiTheme) -> App<'_> {
    let mut expanded = HashSet::new();
    expanded.insert(LOCAL_CHANGES_KEY.to_string());
    App::new(snapshot, theme, graph::Theme::dark(), expanded, 1)
}

fn cursor_key(app: &App) -> String {
    app.rows[app.tree.cursor()].key.clone()
}

fn move_cursor_to(app: &mut App, key: &str) {
    let pos = app.rows.iter().position(|r| r.key == key).unwrap();
    app.tree.set_cursor(pos);
}

fn press(app: &mut App, code: KeyCode) -> KeyResult<Outcome> {
    app.handle_key(PaneId::Left, code, KeyModifiers::NONE)
}

/// Draw the whole shell and read the screen back as text.
fn rendered_lines(shell: &mut Shell<App>) -> Vec<String> {
    rendered_lines_at(shell, 100)
}

fn rendered_lines_at(shell: &mut Shell<App>, width: u16) -> Vec<String> {
    let backend = ratatui::backend::TestBackend::new(width, 30);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let buffer = terminal.backend().buffer();
    (0..buffer.area.height)
        .map(|y| {
            (0..buffer.area.width)
                .map(|x| buffer[(x, y)].symbol())
                .collect::<String>()
        })
        .collect()
}

/// The tree line naming `needle`, gutter included.
fn tree_line<'a>(lines: &'a [String], needle: &str) -> &'a str {
    lines
        .iter()
        .find(|l| l.contains(needle))
        .unwrap_or_else(|| panic!("no row for {needle}: {lines:#?}"))
}

/// A worker that blocks until `release` is dropped, then ends as cancelled
/// (so finishing it touches no repository).
fn blocked_worker(app: &mut App) -> std::sync::mpsc::Sender<()> {
    let (release, wait) = channel::<()>();
    let handle = std::thread::spawn(move || {
        let _ = wait.recv();
        Err(Cancelled.into())
    });
    app.running = Some(Running {
        handle,
        command: "loom reword a1".to_string(),
        spinner: None,
        ticks: 0,
    });
    release
}

/// Poll until the worker has been reaped.
fn poll_until_idle(app: &mut App) {
    for _ in 0..200 {
        app.poll_background();
        if app.running.is_none() {
            return;
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    panic!("worker never finished");
}

#[test]
fn cursor_starts_on_first_focusable_row() {
    let theme = make_theme();
    let app = make_app(make_snapshot(), &theme);
    assert_eq!(cursor_key(&app), LOCAL_CHANGES_KEY);
}

#[test]
fn cursor_skips_spacer_rows() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);

    // local changes → a.rs → b.rs → (spacer skipped) → branch name
    app.move_cursor(1);
    app.move_cursor(1);
    app.move_cursor(1);
    assert_eq!(cursor_key(&app), "br:feature-a");
}

#[test]
fn expand_collapse_commit_keeps_cursor() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let key = oid('a').to_string();
    move_cursor_to(&mut app, &key);

    app.expand_current();
    assert_eq!(cursor_key(&app), key);
    assert!(
        app.rows
            .iter()
            .any(|r| matches!(r.kind, RowKind::CommitFile { .. }))
    );

    app.collapse_current();
    assert_eq!(cursor_key(&app), key);
    assert!(
        !app.rows
            .iter()
            .any(|r| matches!(r.kind, RowKind::CommitFile { .. }))
    );
}

#[test]
fn collapse_on_child_row_collapses_parent() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:b.rs");

    app.collapse_current();
    assert_eq!(cursor_key(&app), LOCAL_CHANGES_KEY);
    assert!(
        !app.rows
            .iter()
            .any(|r| matches!(r.kind, RowKind::WorkingFile { .. }))
    );
}

#[test]
fn space_toggles_selection_and_advances() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");

    app.toggle_selection();
    assert!(app.selected.contains("wf:a.rs"));
    assert_eq!(cursor_key(&app), "wf:b.rs");

    app.toggle_selection();
    assert!(app.selected.contains("wf:b.rs"));
}

#[test]
fn selection_holds_one_class_of_row() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();

    move_cursor_to(&mut app, &oid('a').to_string());
    app.toggle_selection();
    assert_eq!(app.selected.len(), 1);
    assert!(app.selected.contains("wf:a.rs"));
    assert_eq!(cursor_key(&app), oid('a').to_string());
    assert_eq!(
        app.notice.as_deref(),
        Some("selection holds files; Esc clears it")
    );
}

/// `zz` subsumes the files under it, so the header is its own class.
#[test]
fn local_changes_header_does_not_mix_with_its_files() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, LOCAL_CHANGES_KEY);
    app.toggle_selection();

    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    assert_eq!(app.selected.len(), 1);
    assert!(app.selected.contains(LOCAL_CHANGES_KEY));
}

#[test]
fn deselecting_the_last_row_frees_the_class() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    assert!(app.selected.is_empty());

    move_cursor_to(&mut app, &oid('a').to_string());
    app.toggle_selection();
    assert!(app.selected.contains(&oid('a').to_string()));
}

#[test]
fn deselecting_one_of_several_rows_keeps_the_class() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    app.toggle_selection();
    move_cursor_to(&mut app, "wf:b.rs");
    app.toggle_selection();
    assert_eq!(app.selected.len(), 1);

    move_cursor_to(&mut app, &oid('a').to_string());
    app.toggle_selection();
    assert!(!app.selected.contains(&oid('a').to_string()));
}

/// A selection the user can no longer see must not veto the next one.
#[test]
fn collapsing_a_parent_forgets_the_rows_it_hides() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, &oid('a').to_string());
    app.expand_current();
    let file_key = format!("{}:0", oid('a'));
    move_cursor_to(&mut app, &file_key);
    app.toggle_selection();

    move_cursor_to(&mut app, LOCAL_CHANGES_KEY);
    app.collapse_current();
    assert!(app.selected.contains(&file_key));

    move_cursor_to(&mut app, &oid('a').to_string());
    app.collapse_current();
    assert!(app.selected.is_empty());

    app.toggle_selection();
    assert!(app.selected.contains(&oid('a').to_string()));
    assert_eq!(app.notice, None);
}

/// `Esc` reads as cancel, so it never quits: `q` does.
#[test]
fn escape_clears_selection_and_never_quits() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();

    app.handle_escape();
    assert!(app.selected.is_empty());
    assert!(app.outcome.is_none());

    app.handle_escape();
    assert!(app.outcome.is_none());
}

#[test]
fn help_popup_opens_with_question_mark() {
    let theme = make_theme();
    let mut shell = Shell::new(make_app(make_snapshot(), &theme));
    shell.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('?'),
        KeyModifiers::NONE,
    )));
    assert!(matches!(shell.app.popup, Some(Popup::Help { .. })));
    let lines = rendered_lines(&mut shell);
    assert!(
        lines.iter().any(|l| l.contains("Help — ? or Esc to close")),
        "got: {:?}",
        lines
    );
    assert!(lines.iter().any(|l| l.contains("Navigation")));

    // Under the popup action keys are inert and `q` closes it, not the TUI.
    let exit = shell.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('d'),
        KeyModifiers::NONE,
    )));
    assert!(exit.is_none());
    assert!(matches!(shell.app.popup, Some(Popup::Help { .. })));
    let exit = shell.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('q'),
        KeyModifiers::NONE,
    )));
    assert!(exit.is_none());
    assert!(shell.app.popup.is_none());
}

fn pending_commit_key() -> String {
    PENDING_COMMIT_OID.to_string()
}

#[test]
fn commit_collects_the_selected_working_files_and_lands_on_integration() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    move_cursor_to(&mut app, "wf:b.rs");
    app.toggle_selection();

    press(&mut app, KeyCode::Char('c'));
    assert_eq!(cursor_key(&app), pending_commit_key(), "the cursor follows");
    assert_eq!(
        app.rows[app.tree.cursor()].target,
        None,
        "the placeholder names no object, so it is no command's argument"
    );
    // Default destination: the integration line, its own section above every
    // feature branch.
    let at = app.tree.cursor();
    let branch = app.rows.iter().position(|r| r.key == "br:feature-a");
    assert_eq!(branch, Some(at + 2), "one spacer between the two sections");

    let Some(Action::Commit { source, dest }) = app.confirm_commit_target() else {
        panic!("expected a commit action");
    };
    let CommitSource::Files(files) = source else {
        panic!("`c` names its files");
    };
    assert_eq!(files.len(), 2);
    assert_eq!(dest, CommitDest::Integration);
    assert!(matches!(app.mode, Mode::Normal));

    // A failed commit leaves no new row to land on, and the placeholder is
    // gone after the reload: go back to where `c` was pressed — the row
    // Space advanced onto after the last selected file.
    app.finish_action(Err(anyhow::anyhow!("boom")));
    assert_eq!(app.next_cursor.as_deref(), Some("br:feature-a"));
    assert_eq!(app.fallback_cursor, None);
}

/// Down moves the commit to `feature-a`'s tip, drawn inside the branch above
/// the commit it owns; there is nothing below it to move to.
#[test]
fn commit_destination_moves_with_the_arrows() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    press(&mut app, KeyCode::Char('c'));

    press(&mut app, KeyCode::Down);
    let at = app.tree.cursor();
    assert_eq!(cursor_key(&app), pending_commit_key());
    assert_eq!(app.rows[at - 1].key, "br:feature-a");
    assert_eq!(app.rows[at + 1].key, oid('a').to_string());

    press(&mut app, KeyCode::Down);
    assert_eq!(app.tree.cursor(), at, "the last destination is the end");

    press(&mut app, KeyCode::Up);
    let Some(Action::Commit { dest, .. }) = app.confirm_commit_target() else {
        panic!("expected a commit action");
    };
    assert_eq!(dest, CommitDest::Integration);
}

#[test]
fn commit_on_the_local_changes_header_takes_everything_as_zz() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, LOCAL_CHANGES_KEY);

    press(&mut app, KeyCode::Char('c'));
    let Some(Action::Commit { source, .. }) = app.confirm_commit_target() else {
        panic!("expected a commit action");
    };
    assert_eq!(
        source,
        CommitSource::Files(vec![app.snapshot.ids.get_unstaged().to_string()])
    );
}

/// A repo the pick can actually read, with `make_info`'s rows over it.
fn app_over_repo<'a>(repo: &crate::core::test_helpers::TestRepo, theme: &'a TuiTheme) -> App<'a> {
    let snapshot = Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..make_snapshot()
    };
    make_app(snapshot, theme)
}

/// How long a worker gets before a test calls it hung. Bounded by the clock,
/// not by a tick count: a 1ms sleep is ~15ms on Windows, so counting tries
/// turns a real failure into a minute of waiting.
fn deadline() -> std::time::Instant {
    std::time::Instant::now() + std::time::Duration::from_secs(10)
}

/// Poll as the shell does until the pick is ready, so the worker's result is
/// taken on the loop rather than joined inline.
fn poll_until_take_over(app: &mut App) {
    let deadline = deadline();
    while std::time::Instant::now() < deadline {
        if matches!(app.poll_background(), Tick::TakeOver) {
            return;
        }
        std::thread::sleep(std::time::Duration::from_millis(1));
    }
    panic!("the pick never became ready");
}

/// Reading the hunks is a git per changed file, so `C` hands it to a worker
/// and the loop keeps drawing; only once it is read does the shell get asked
/// for the terminal.
#[test]
fn commit_with_hunks_reads_the_working_tree_off_the_loop() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_over_repo(&repo, &theme);
    move_cursor_to(&mut app, "wf:a.rs");

    press(&mut app, KeyCode::Char('C'));
    assert!(matches!(app.mode, Mode::Normal), "no destination yet");
    let Some(Pick::Collecting { origin, .. }) = &app.pick else {
        panic!("no pick requested");
    };
    assert_eq!(origin, "wf:a.rs");
    assert_eq!(
        app.log.last().unwrap().command,
        format!("loom add -p {}", app.snapshot.ids.get_unstaged())
    );
    assert!(app.modal_active(), "no key may start anything meanwhile");
    assert!(
        app.mode_hint()
            .is_some_and(|h| h.contains("reading the working tree")),
        "the loop draws a spinner while the worker reads"
    );

    poll_until_take_over(&mut app);
    let Some(Pick::Ready { entries, .. }) = &app.pick else {
        panic!("the pick is not ready");
    };
    assert_eq!(
        app.log.len(),
        1,
        "the read reports into the entry the press opened, not a later one"
    );
    assert_eq!(
        entries.iter().map(|e| e.path.as_str()).collect::<Vec<_>>(),
        vec!["tracked.txt"]
    );
}

/// The read leaves the loop running, so a popup can be opened over it. The
/// selector draws on the bare terminal and would land under that popup, which
/// would still hold the keyboard over the placement afterwards.
#[test]
fn a_ready_pick_waits_for_a_popup_to_close() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_over_repo(&repo, &theme);
    move_cursor_to(&mut app, "wf:a.rs");

    press(&mut app, KeyCode::Char('C'));
    press(&mut app, KeyCode::Char('L'));
    assert!(app.popup.is_some(), "the log is open over the read");

    let mut ready = false;
    let deadline = deadline();
    while std::time::Instant::now() < deadline {
        let tick = app.poll_background();
        assert!(
            !matches!(tick, Tick::TakeOver),
            "the terminal must not be taken under a popup"
        );
        // Held, not dropped: the read finishes and then waits.
        if matches!(app.pick, Some(Pick::Ready { .. })) {
            // The frame still carries the spinner of a read that is over, so
            // the tick that ends it has to ask for one more draw.
            assert!(
                !matches!(tick, Tick::Idle),
                "becoming ready must redraw, or the stale spinner stays"
            );
            ready = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(1));
    }
    assert!(ready, "the read never finished");
    for _ in 0..20 {
        assert!(
            !matches!(app.poll_background(), Tick::TakeOver),
            "a ready pick keeps waiting while the popup is up"
        );
    }

    press(&mut app, KeyCode::Esc);
    assert!(app.popup.is_none(), "the log is dismissed");
    poll_until_take_over(&mut app);
}

/// `Esc` ends the pick, but the worker is waited for rather than abandoned:
/// it reads with `git`, and letting it outlive the pick would put it beside
/// whatever the freed keyboard starts next, over the same index.
#[test]
fn a_collecting_pick_is_cancelled_by_esc() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_over_repo(&repo, &theme);
    move_cursor_to(&mut app, "wf:a.rs");

    press(&mut app, KeyCode::Char('C'));
    press(&mut app, KeyCode::Esc);
    assert!(
        matches!(
            app.pick,
            Some(Pick::Collecting {
                cancelled: true,
                ..
            })
        ),
        "held until the worker lands, so no action can run beside it"
    );
    assert!(
        app.mode_hint().is_some_and(|h| h.contains("cancelling")),
        "the bar says the read is winding down"
    );

    let deadline = deadline();
    while std::time::Instant::now() < deadline && app.pick.is_some() {
        let tick = app.poll_background();
        assert!(
            !matches!(tick, Tick::TakeOver),
            "a cancelled pick never takes the terminal"
        );
        std::thread::sleep(std::time::Duration::from_millis(1));
    }
    assert!(app.pick.is_none(), "the pick ends with its worker");
    assert_eq!(app.notice.as_deref(), Some("commit cancelled"));
    assert!(matches!(app.mode, Mode::Normal), "no destination is placed");
    // The entry the press opened says what became of it.
    assert_eq!(app.log.len(), 1);
    assert_eq!(
        app.log[0].lines,
        vec![(Level::Warn, "Cancelled".to_string())]
    );
}

/// Spec 020 makes it normative that every way out of a pick leaves a line.
/// The selector needs a terminal, so the outcomes are driven through the seam
/// below it.
#[test]
fn every_pick_outcome_says_so_in_its_entry() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();

    let mut app = app_on(&repo, &theme);
    app.log.push(LogEntry {
        command: "loom add -p zz".to_string(),
        lines: Vec::new(),
    });
    app.finish_pick(Ok(false), LOCAL_CHANGES_KEY.to_string());
    assert_eq!(
        app.log[0].lines,
        vec![(Level::Warn, "Cancelled".to_string())],
        "a selector that kept nothing"
    );
    assert_eq!(app.notice.as_deref(), Some("commit cancelled"));

    let mut app = app_on(&repo, &theme);
    app.log.push(LogEntry {
        command: "loom add -p zz".to_string(),
        lines: Vec::new(),
    });
    app.finish_pick(
        Err(anyhow::anyhow!("could not stage")),
        LOCAL_CHANGES_KEY.to_string(),
    );
    assert_eq!(
        app.log[0].lines,
        vec![(Level::Error, "could not stage".to_string())],
        "a staging that failed"
    );
    assert!(matches!(app.popup, Some(Popup::Notice { .. })));
}

/// `q` is queued like `Esc`, not dropped: the pick still waits for its worker,
/// and the session ends once it lands.
#[test]
fn quitting_during_a_read_leaves_once_the_worker_lands() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_over_repo(&repo, &theme);
    move_cursor_to(&mut app, "wf:a.rs");

    press(&mut app, KeyCode::Char('C'));
    press(&mut app, KeyCode::Char('q'));
    assert!(
        matches!(
            app.pick,
            Some(Pick::Collecting {
                cancelled: true,
                ..
            })
        ),
        "the read is still waited for"
    );

    let deadline = deadline();
    let mut left = false;
    while std::time::Instant::now() < deadline {
        if matches!(app.poll_background(), Tick::Exit(Outcome::Quit)) {
            left = true;
            break;
        }
        std::thread::sleep(std::time::Duration::from_millis(1));
    }
    assert!(left, "the queued quit never happened");
    assert!(app.pick.is_none(), "the pick ended with its worker");
}

/// A worker that cannot read the tree reports through a popup and drops the
/// pick, rather than handing the terminal over to a selector with nothing in
/// it: the loop must not be left asking for `TakeOver` forever.
#[test]
fn a_pick_that_cannot_read_the_working_tree_reports_and_stops() {
    let dir = tempfile::tempdir().unwrap();
    let theme = make_theme();
    let snapshot = Snapshot {
        workdir: dir.path().to_path_buf(),
        git_dir: dir.path().to_path_buf(),
        ..make_snapshot()
    };
    let mut app = make_app(snapshot, &theme);
    move_cursor_to(&mut app, "wf:a.rs");

    press(&mut app, KeyCode::Char('C'));
    let deadline = deadline();
    while std::time::Instant::now() < deadline {
        if app.pick.is_none() {
            break;
        }
        let tick = app.poll_background();
        assert!(
            !matches!(tick, Tick::TakeOver),
            "a failed read must never take the terminal"
        );
        std::thread::sleep(std::time::Duration::from_millis(1));
    }
    assert!(app.pick.is_none(), "the pick never ended");
    assert!(matches!(
        app.popup,
        Some(Popup::Notice {
            then: AfterNotice::Nothing,
            ..
        })
    ));
    // Into the entry the press opened, as a failed action reports.
    assert_eq!(
        app.log.last().unwrap().lines.first().map(|(l, _)| *l),
        Some(Level::Error),
        "the failure is on record, not only on screen"
    );
}

/// The selector rewrites the whole index, so it opens over every local change
/// — the cursor row and the selection only decide where the cursor goes back.
#[test]
fn commit_with_hunks_picks_from_every_local_change() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_over_repo(&repo, &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    move_cursor_to(&mut app, &oid('a').to_string());

    press(&mut app, KeyCode::Char('C'));
    assert!(matches!(app.pick, Some(Pick::Collecting { .. })));
    assert_eq!(
        app.log.last().unwrap().command,
        format!("loom add -p {}", app.snapshot.ids.get_unstaged()),
        "the pick covers every local change, not the cursor row"
    );
    // Drained before the fixture goes: a worker still reading it would race
    // the tempdir's removal.
    poll_until_take_over(&mut app);
}

/// `C` is in the key list that is inert while a commit is being placed, so it
/// cannot discard the pending placement to start a pick.
#[test]
fn commit_with_hunks_is_inert_while_a_commit_is_being_placed() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    press(&mut app, KeyCode::Char('c'));
    assert!(matches!(app.mode, Mode::CommitTarget { .. }));

    press(&mut app, KeyCode::Char('C'));
    assert!(app.pick.is_none(), "no pick may start");
    assert!(
        matches!(app.mode, Mode::CommitTarget { .. }),
        "still placing"
    );
}

#[test]
fn commit_with_hunks_refuses_an_empty_working_tree() {
    let mut info = make_info();
    info.working_changes.clear();
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);

    press(&mut app, KeyCode::Char('C'));
    assert!(app.pick.is_none());
    assert_eq!(app.notice.as_deref(), Some("commit: no local changes"));
}

/// The pick is staged before the placement, so the placeholder counts the
/// files the index holds — `a.rs` here, not the two local changes.
#[test]
fn a_staged_commit_counts_the_files_the_index_holds() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);

    let source_rows = app.index_sources();
    app.enter_commit_target(
        CommitSource::Index,
        source_rows,
        LOCAL_CHANGES_KEY.to_string(),
    );

    let mut shell = Shell::new(app);
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let buffer = terminal.backend().buffer();
    let lines: Vec<String> = (0..buffer.area.height)
        .map(|y| {
            (0..buffer.area.width)
                .map(|x| buffer[(x, y)].symbol())
                .collect::<String>()
        })
        .collect();
    assert!(
        lines
            .iter()
            .any(|line| line.contains("[CREATE COMMIT] new commit (1 file)")),
        "{lines:#?}"
    );

    // The staging already happened, so the commit itself names no files.
    let app = &mut shell.app;
    let Some(action) = app.confirm_commit_target() else {
        panic!("expected a commit action");
    };
    assert_eq!(
        action,
        Action::Commit {
            source: CommitSource::Index,
            dest: CommitDest::Integration,
        }
    );
    assert_eq!(app.command_line(&action), "loom commit -i");
}

/// The half of `C` that writes the index. The selector needs a terminal and
/// cannot run here, so the entries it would return are built directly.
fn picked_entries(repo: &crate::core::test_helpers::TestRepo, keep: bool) -> Vec<FileEntry> {
    let mut entries =
        crate::core::staging::collect_file_entries(&repo.repo, &repo.workdir(), None).unwrap();
    for entry in &mut entries {
        for hunk in &mut entry.hunks {
            hunk.selected = keep;
        }
    }
    entries
}

fn repo_with_one_unstaged_hunk() -> crate::core::test_helpers::TestRepo {
    let repo = crate::core::test_helpers::TestRepo::new();
    repo.write_file(
        "tracked.txt",
        "one
",
    );
    repo.stage_files(&["tracked.txt"]);
    repo.commit_staged("Add file");
    repo.write_file(
        "tracked.txt",
        "one
two
",
    );
    repo
}

fn app_on<'a>(repo: &crate::core::test_helpers::TestRepo, theme: &'a TuiTheme) -> App<'a> {
    let mut info = make_info();
    info.working_changes = vec![file("tracked.txt", ' ', 'M')];
    let snapshot = Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..snapshot_of(info)
    };
    make_app(snapshot, theme)
}

#[test]
fn a_pick_stages_the_hunks_it_kept() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_on(&repo, &theme);

    let picked = picked_entries(&repo, true);
    let logged = app.log.len();
    assert!(app.apply_pick(picked).unwrap());

    let status = repo.status_porcelain();
    assert!(status.contains("M  tracked.txt"), "{status}");
    // The press opens the entry; staging reports into it rather than a second.
    assert_eq!(app.log.len(), logged, "no second `loom add -p` entry");
}

/// Keeping nothing is not a failure and not a write: the index is left exactly
/// as it was.
#[test]
fn a_pick_that_kept_nothing_leaves_the_index_alone() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_on(&repo, &theme);
    let before = repo.status_porcelain();

    let picked = picked_entries(&repo, false);
    assert!(!app.apply_pick(picked).unwrap());

    assert_eq!(repo.status_porcelain(), before);
}

/// The pane previews the index the selector staged, not the whole worktree.
#[test]
fn a_staged_commit_previews_the_index() {
    let repo = crate::core::test_helpers::TestRepo::new();
    repo.write_file("staged.txt", "original");
    repo.stage_files(&["staged.txt"]);
    repo.commit_staged("Add file");
    repo.write_file("staged.txt", "picked");
    repo.stage_files(&["staged.txt"]);
    repo.write_file("loose.txt", "left behind");

    let mut info = make_info();
    info.working_changes = vec![file("staged.txt", 'M', ' '), file("loose.txt", '?', '?')];
    let snapshot = Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..snapshot_of(info)
    };
    let theme = make_theme();
    let mut app = make_app(snapshot, &theme);

    let source_rows = app.index_sources();
    app.enter_commit_target(
        CommitSource::Index,
        source_rows,
        LOCAL_CHANGES_KEY.to_string(),
    );
    app.ensure_diff_cached();

    let text: String = app.diff_cache[&pending_commit_key()]
        .iter()
        .flat_map(|line| line.spans.iter().map(|s| s.content.to_string()))
        .collect();
    assert!(text.contains("+picked"), "got: {text}");
    assert!(
        !text.contains("loose.txt"),
        "the unstaged file is not in it: {text}"
    );
}

#[test]
fn commit_refuses_rows_that_are_not_local_changes() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, &oid('a').to_string());
    app.toggle_selection();

    press(&mut app, KeyCode::Char('c'));
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(
        app.notice.as_deref(),
        Some("commit: move to local changes or select files")
    );
}

/// Nothing to commit: `zz` on an empty working tree would only fail deeper in.
#[test]
fn commit_refuses_an_empty_working_tree() {
    let mut info = make_info();
    info.working_changes.clear();
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);
    move_cursor_to(&mut app, LOCAL_CHANGES_KEY);

    press(&mut app, KeyCode::Char('c'));
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.notice.as_deref(), Some("commit: no local changes"));
}

/// Two branches at one tip are two destinations, and committing to either
/// splits the group — the placeholder ends up under the name it was sent to,
/// never the other one.
#[test]
fn a_co_located_group_gives_one_destination_per_name() {
    let mut info = make_info();
    info.branches.push(BranchInfo {
        name: "feature-b".to_string(),
        tip_oid: oid('a'),
        remote: None,
    });
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);

    press(&mut app, KeyCode::Char('c'));
    let Mode::CommitTarget { dests, .. } = &app.mode else {
        panic!("expected commit mode");
    };
    let dests = dests.clone();
    assert_eq!(dests.len(), 3, "the integration line plus both names");

    for dest in &dests[1..] {
        press(&mut app, KeyCode::Down);
        let at = app.tree.cursor();
        let CommitDest::Branch(name) = dest else {
            panic!("only the first destination is the integration line");
        };
        assert_eq!(
            app.rows[at - 1].key,
            branch_key(name),
            "placeholder is not under {name}"
        );
        let other = branch_key(if name == "feature-a" {
            "feature-b"
        } else {
            "feature-a"
        });
        let other_at = app.rows.iter().position(|r| r.key == other);
        assert!(
            other_at > Some(at),
            "the other name did not stay behind with the commit"
        );
    }
}

/// `feature-b` (owning `a`) is stacked on `feature-a` (owning `b`):
/// committing to the lower branch keeps the upper one on top of the new
/// commit, as the relocation rebase will leave it.
#[test]
fn commit_to_a_branch_under_a_stack_keeps_the_stack() {
    let mut info = make_info();
    info.commits = vec![
        commit('a', 'b', "Add parser"),
        commit('b', '9', "Add lexer"),
    ];
    info.branches = vec![
        BranchInfo {
            name: "feature-a".to_string(),
            tip_oid: oid('b'),
            remote: None,
        },
        BranchInfo {
            name: "feature-b".to_string(),
            tip_oid: oid('a'),
            remote: None,
        },
    ];
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);

    press(&mut app, KeyCode::Char('c'));
    press(&mut app, KeyCode::Down);
    press(&mut app, KeyCode::Down);
    let Mode::CommitTarget { dests, index, .. } = &app.mode else {
        panic!("expected commit mode");
    };
    assert_eq!(dests[*index], CommitDest::Branch("feature-a".to_string()));

    let at = app.tree.cursor();
    assert_eq!(app.rows[at - 1].key, "br:feature-a");
    assert_eq!(app.rows[at + 1].key, oid('b').to_string());
    assert!(
        app.rows[..at].iter().any(|r| r.key == oid('a').to_string()),
        "feature-b still stacked above"
    );
}

/// A branch with no commits of its own is at the base: its commit forks from
/// there, parallel to the other branches, not stacked on them (Spec 006).
#[test]
fn commit_to_an_empty_branch_forks_from_the_base() {
    let mut info = make_info();
    info.branches.push(BranchInfo {
        name: "feature-b".to_string(),
        tip_oid: oid('9'),
        remote: None,
    });
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);

    press(&mut app, KeyCode::Char('c'));
    let Mode::CommitTarget { dests, .. } = &app.mode else {
        panic!("expected commit mode");
    };
    assert_eq!(
        dests.clone(),
        vec![
            CommitDest::Integration,
            CommitDest::Branch("feature-b".to_string()),
            CommitDest::Branch("feature-a".to_string()),
        ]
    );

    press(&mut app, KeyCode::Down);
    let at = app.tree.cursor();
    assert_eq!(app.rows[at - 1].key, "br:feature-b");
    // Its own section, kept above feature-a's while placing, parallel to it.
    assert!(matches!(app.rows[at + 1].kind, RowKind::Spacer("├╯")));
    assert!(
        app.rows[at..].iter().any(|r| r.key == oid('a').to_string()),
        "feature-a still owns its commit below"
    );
}

/// Every destination draws the placeholder in the section it names, in the
/// order `↑`/`↓` walk them: a stack and a branch owning nothing included.
#[test]
fn every_commit_destination_draws_the_placeholder_in_its_own_section() {
    let mut info = make_info();
    info.commits = vec![
        commit('a', 'b', "Add parser"),
        commit('b', '9', "Add lexer"),
    ];
    info.branches = vec![
        BranchInfo {
            name: "feature-a".to_string(),
            tip_oid: oid('b'),
            remote: None,
        },
        BranchInfo {
            name: "feature-b".to_string(),
            tip_oid: oid('a'),
            remote: None,
        },
        BranchInfo {
            name: "feature-c".to_string(),
            tip_oid: oid('9'),
            remote: None,
        },
    ];
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);

    press(&mut app, KeyCode::Char('c'));
    let Mode::CommitTarget { dests, .. } = &app.mode else {
        panic!("expected commit mode");
    };
    let dests = dests.clone();
    assert_eq!(dests.len(), 4, "the integration line plus three branches");
    assert_eq!(dests[0], CommitDest::Integration);

    for dest in &dests[1..] {
        press(&mut app, KeyCode::Down);
        let at = app.tree.cursor();
        assert_eq!(cursor_key(&app), PENDING_COMMIT_OID.to_string());
        let CommitDest::Branch(name) = dest else {
            panic!("only the first destination is the integration line");
        };
        assert_eq!(
            app.rows[at - 1].key,
            branch_key(name),
            "placeholder is not under {name}"
        );
    }
}

/// Every placement reuses one row key, so a new one must not show the diff
/// of the files the last one picked; moving the commit keeps it, since only
/// the destination changed.
#[test]
fn a_new_placement_drops_the_cached_preview_but_moving_keeps_it() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    press(&mut app, KeyCode::Char('c'));

    let key = pending_commit_key();
    let marker = vec![Line::from("stale")];
    app.diff_cache.insert(key.clone(), marker.clone());
    press(&mut app, KeyCode::Down);
    assert_eq!(
        app.diff_cache.get(&key),
        Some(&marker),
        "moving recomputed a diff that cannot have changed"
    );

    app.handle_escape();
    move_cursor_to(&mut app, "wf:b.rs");
    press(&mut app, KeyCode::Char('c'));
    assert_ne!(
        app.diff_cache.get(&key),
        Some(&marker),
        "the previous placement's diff survived"
    );
}

/// The cursor must stay on the placeholder: the wheel moves the destination
/// like `↑`/`↓`, and a click, which would land anywhere, is ignored.
#[test]
fn mouse_moves_the_destination_but_never_leaves_the_placeholder() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    press(&mut app, KeyCode::Char('c'));

    let mut shell = Shell::new(app);
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let tree_area = shell.areas()[0];
    let at = |kind| {
        Event::Mouse(MouseEvent {
            kind,
            column: tree_area.x + 1,
            row: tree_area.y + 1,
            modifiers: KeyModifiers::NONE,
        })
    };

    shell.handle_event(at(MouseEventKind::ScrollDown));
    let Mode::CommitTarget { dests, index, .. } = &shell.app.mode else {
        panic!("expected commit mode");
    };
    assert_eq!(dests[*index], CommitDest::Branch("feature-a".to_string()));
    assert_eq!(cursor_key(&shell.app), PENDING_COMMIT_OID.to_string());

    shell.handle_event(at(MouseEventKind::Down(MouseButton::Left)));
    assert_eq!(cursor_key(&shell.app), PENDING_COMMIT_OID.to_string());
    let Mode::CommitTarget { dests, index, .. } = &shell.app.mode else {
        panic!("expected commit mode");
    };
    assert_eq!(
        dests[*index],
        CommitDest::Branch("feature-a".to_string()),
        "a click changed the destination"
    );
}

#[test]
fn commit_is_cancelled_by_escape() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    let rows_before = app.rows.len();

    press(&mut app, KeyCode::Char('c'));
    assert!(app.rows.iter().any(|r| r.key == pending_commit_key()));

    app.handle_escape();
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.rows.len(), rows_before, "the placeholder is gone");
    assert_eq!(cursor_key(&app), "wf:a.rs");
    assert!(app.outcome.is_none());
}

#[test]
fn commit_mode_blocks_action_keys() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    press(&mut app, KeyCode::Char('c'));

    for code in [
        KeyCode::Char(' '),
        KeyCode::Char('c'),
        KeyCode::Char('f'),
        KeyCode::Char('F'),
        KeyCode::Char('b'),
        KeyCode::Char('d'),
        KeyCode::Char('r'),
        KeyCode::Char('R'),
        KeyCode::Char('+'),
        KeyCode::Char('='),
        KeyCode::Char('-'),
        KeyCode::F(5),
        // Folding walks the cursor off a row, so it is blocked too.
        KeyCode::Left,
        KeyCode::Right,
        KeyCode::Char('h'),
        KeyCode::Char('l'),
    ] {
        press(&mut app, code);
        assert!(
            matches!(app.mode, Mode::CommitTarget { .. }),
            "{code:?} left commit mode"
        );
        assert_eq!(cursor_key(&app), pending_commit_key(), "{code:?} moved on");
        assert_eq!(
            app.notice.as_deref(),
            Some("commit: Enter to confirm, Esc to cancel"),
            "{code:?} reported the wrong mode"
        );
    }
}

#[test]
fn fold_flow_uses_selection_as_sources_and_cursor_as_target() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();

    app.action_fold_start();
    assert!(matches!(app.mode, Mode::FoldTarget { .. }));

    move_cursor_to(&mut app, &oid('a').to_string());
    let Some(Action::Fold {
        sources,
        target,
        hunks: None,
    }) = app.confirm_fold_target()
    else {
        panic!("expected a fold action");
    };
    assert_eq!(sources, vec![app.snapshot.ids.get_file("a.rs").to_string()]);
    assert_eq!(target, oid('a').to_string());
    assert!(matches!(app.mode, Mode::Normal));
}

/// `feature-b` owning `b` and `feature-a` owning `a`, side by side on `9`.
fn two_branch_info() -> RepoInfo {
    RepoInfo {
        commits: vec![
            commit('b', '9', "Add lexer"),
            commit('a', '9', "Add parser"),
        ],
        branches: vec![
            BranchInfo {
                name: "feature-a".to_string(),
                tip_oid: oid('a'),
                remote: None,
            },
            BranchInfo {
                name: "feature-b".to_string(),
                tip_oid: oid('b'),
                remote: None,
            },
        ],
        ..make_info()
    }
}

/// The target keys a pending fold walks, with their effects, in tree order.
fn fold_targets_of(app: &App) -> Vec<(String, FoldEffect)> {
    let Mode::FoldTarget { targets, .. } = &app.mode else {
        panic!("not picking a fold target");
    };
    targets.iter().map(|t| (t.key.clone(), t.effect)).collect()
}

fn fold_start_on(app: &mut App, keys: &[&str]) {
    for key in keys {
        move_cursor_to(app, key);
        app.toggle_selection();
    }
    app.action_fold_start();
}

#[test]
fn folding_files_walks_the_commits_tagged_amend() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(two_branch_info()), &theme);
    fold_start_on(&mut app, &["wf:a.rs"]);

    assert_eq!(
        fold_targets_of(&app),
        vec![
            (oid('b').to_string(), FoldEffect::Amend),
            (oid('a').to_string(), FoldEffect::Amend),
        ]
    );
    assert_eq!(cursor_key(&app), oid('b').to_string());
    press(&mut app, KeyCode::Down);
    assert_eq!(cursor_key(&app), oid('a').to_string());

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(tree_line(&lines, "Add parser").contains("[AMEND] Add parser"));
    assert!(!tree_line(&lines, "Add lexer").contains("[AMEND]"));
    assert!(tree_line(&lines, "a.rs").contains("▸ "), "{lines:#?}");
}

#[test]
fn folding_a_commit_offers_uncommit_noop_and_amend() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(two_branch_info()), &theme);
    fold_start_on(&mut app, &[&oid('a').to_string()]);

    // No branch: moving commits is not a fold in the tree.
    assert_eq!(
        fold_targets_of(&app),
        vec![
            (LOCAL_CHANGES_KEY.to_string(), FoldEffect::Uncommit),
            // Not a git repo's commits, so the fixup check is left to the
            // command.
            (oid('b').to_string(), FoldEffect::Amend),
            (oid('a').to_string(), FoldEffect::Noop),
        ]
    );
    assert_eq!(cursor_key(&app), oid('a').to_string(), "starts on itself");
    assert!(app.confirm_fold_target().is_none());
    assert!(matches!(app.mode, Mode::FoldTarget { .. }));
    assert!(app.notice.as_deref().unwrap().contains("nothing to do"));

    press(&mut app, KeyCode::Up);
    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(tree_line(&lines, "Add lexer").contains("[AMEND] Add lexer"));
    assert!(!tree_line(&lines, "Add parser").contains("[NOOP]"));
    let Some(Action::Fold {
        sources,
        target,
        hunks: None,
    }) = shell.app.confirm_fold_target()
    else {
        panic!("expected a fold action");
    };
    assert_eq!(sources, vec![oid('a').to_string()]);
    assert_eq!(target, oid('b').to_string());
}

#[test]
fn folding_a_commit_into_the_working_tree_tags_it_uncommit() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(two_branch_info()), &theme);
    fold_start_on(&mut app, &[&oid('a').to_string()]);
    while cursor_key(&app) != LOCAL_CHANGES_KEY {
        press(&mut app, KeyCode::Up);
    }

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(
        tree_line(&lines, "local changes").contains("[UNCOMMIT]"),
        "{lines:#?}"
    );
}

#[test]
fn cancelling_a_fold_puts_the_cursor_back() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(two_branch_info()), &theme);
    fold_start_on(&mut app, &[&oid('a').to_string()]);
    let Mode::FoldTarget { origin, .. } = &app.mode else {
        panic!("not picking a fold target");
    };
    let origin = origin.clone();
    press(&mut app, KeyCode::Up);

    press(&mut app, KeyCode::Esc);
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(cursor_key(&app), origin);
}

#[test]
fn folding_a_commit_file_offers_uncommit_noop_and_moves() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(two_branch_info()), &theme);
    let a = oid('a').to_string();
    app.expanded.insert(a.clone());
    app.rebuild_rows(&a);
    fold_start_on(&mut app, &[&format!("{a}:0")]);

    assert_eq!(
        fold_targets_of(&app),
        vec![
            (LOCAL_CHANGES_KEY.to_string(), FoldEffect::Uncommit),
            (oid('b').to_string(), FoldEffect::MoveFile),
            (a.clone(), FoldEffect::Noop),
        ]
    );
    assert_eq!(cursor_key(&app), a, "starts on the file's own commit");
    press(&mut app, KeyCode::Up);

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(tree_line(&lines, "Add lexer").contains("[MOVE] Add lexer"));
    assert!(!tree_line(&lines, "Add parser").contains("[NOOP]"));
}

#[test]
fn fold_refuses_sources_the_command_would() {
    let theme = make_theme();
    let a = oid('a').to_string();
    let b = oid('b').to_string();
    for (keys, expected) in [
        (
            vec!["br:feature-a".to_string()],
            "fold: a branch cannot be folded",
        ),
        (
            vec![format!("{a}:0"), format!("{b}:0")],
            "fold: one commit file at a time",
        ),
        (vec![b.clone(), a.clone()], "fold: one commit at a time"),
    ] {
        let mut app = make_app(snapshot_of(two_branch_info()), &theme);
        app.expanded.insert(a.clone());
        app.expanded.insert(b.clone());
        app.rebuild_rows(&a);
        let keys: Vec<&str> = keys.iter().map(String::as_str).collect();
        fold_start_on(&mut app, &keys);
        assert!(matches!(app.mode, Mode::Normal), "{keys:?}");
        assert_eq!(app.notice.as_deref(), Some(expected));
    }
}

/// `loom fold <c> <target>` refuses a target the source does not descend
/// from, so a newer commit is no target at all.
#[test]
fn a_fixup_is_offered_only_into_commits_the_source_descends_from() {
    let repo = crate::core::test_helpers::TestRepo::new();
    let base = repo.head_oid();
    let c1 = repo.commit("one", "one.txt");
    let c2 = repo.commit("two", "two.txt");
    let c3 = repo.commit("three", "three.txt");
    let real = |oid: git2::Oid, parent: git2::Oid, message: &str| CommitInfo {
        oid,
        parent_oid: Some(parent),
        message: message.to_string(),
        ..commit('0', '0', "")
    };
    let mut info = make_info();
    info.upstream.merge_base_oid = base;
    info.commits = vec![
        real(c3, c2, "three"),
        real(c2, c1, "two"),
        real(c1, base, "one"),
    ];
    info.branches.clear();
    let snapshot = Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..snapshot_of(info)
    };
    let theme = make_theme();
    let mut app = make_app(snapshot, &theme);
    fold_start_on(&mut app, &[&c2.to_string()]);

    assert_eq!(
        fold_targets_of(&app),
        vec![
            (LOCAL_CHANGES_KEY.to_string(), FoldEffect::Uncommit),
            (c2.to_string(), FoldEffect::Noop),
            (c1.to_string(), FoldEffect::Amend),
        ]
    );
}

/// The places a pending move walks, in tree order.
fn move_slots_of(app: &App) -> Vec<MoveSlot> {
    let Mode::MoveTarget { slots, .. } = &app.mode else {
        panic!("not moving a commit");
    };
    slots.clone()
}

fn move_slot_of(app: &App) -> MoveSlot {
    let Mode::MoveTarget { slots, index, .. } = &app.mode else {
        panic!("not moving a commit");
    };
    slots[*index].clone()
}

fn move_start_on(app: &mut App, key: &str) {
    move_cursor_to(app, key);
    press(app, KeyCode::Char('m'));
}

fn row_at(app: &App, key: &str) -> usize {
    app.rows.iter().position(|r| r.key == key).unwrap()
}

#[test]
fn moving_a_commit_walks_the_places_it_can_go() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(two_branch_info()), &theme);
    let a = oid('a').to_string();
    move_start_on(&mut app, &a);

    // `feature-b`'s tip is `--above b`, spelled that way; `feature-a`, which
    // `a` empties, is where `a` already is.
    assert_eq!(
        move_slots_of(&app),
        vec![
            MoveSlot::Above(oid('b')),
            MoveSlot::Below(oid('b')),
            MoveSlot::Stay,
        ]
    );
    assert_eq!(cursor_key(&app), a, "starts where it is");
    assert!(app.confirm_move_target().is_none());
    assert!(app.notice.as_deref().unwrap().contains("already here"));

    press(&mut app, KeyCode::Up);
    assert_eq!(cursor_key(&app), a, "the cursor follows the commit");
    assert_eq!(row_at(&app, &a), row_at(&app, &oid('b').to_string()) + 1);
    // Emptied, `feature-a` keeps its place until the move runs.
    assert!(row_at(&app, "br:feature-b") < row_at(&app, "br:feature-a"));

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(tree_line(&lines, "Add parser").contains("[MOVE] Add parser"));
    assert!(!tree_line(&lines, "Add lexer").contains("[MOVE]"));

    let action = shell.app.confirm_move_target().expect("a move");
    assert_eq!(
        shell.app.command_line(&action),
        format!(
            "loom fold {} --below {}",
            shell.app.snapshot.ids.get_commit(oid('a')),
            shell.app.snapshot.ids.get_commit(oid('b'))
        )
    );
    assert_eq!(
        action,
        Action::Move {
            commit: oid('a'),
            slot: MoveSlot::Below(oid('b')),
        }
    );
    assert!(matches!(shell.app.mode, Mode::Normal));
}

#[test]
fn moving_a_commit_reaches_an_empty_branch_through_its_name() {
    let theme = make_theme();
    let mut info = two_branch_info();
    info.branches.push(BranchInfo {
        name: "feature-c".to_string(),
        tip_oid: oid('9'),
        remote: None,
    });
    let mut app = make_app(snapshot_of(info), &theme);
    move_start_on(&mut app, &oid('a').to_string());

    let empty = MoveSlot::Branch("feature-c".to_string());
    assert!(move_slots_of(&app).contains(&empty));
    while move_slot_of(&app) != empty {
        press(&mut app, KeyCode::Up);
    }
    assert_eq!(
        row_at(&app, &oid('a').to_string()),
        row_at(&app, "br:feature-c") + 1
    );
    assert_eq!(
        app.confirm_move_target(),
        Some(Action::Move {
            commit: oid('a'),
            slot: empty,
        })
    );
}

/// Loose `l` on `feature-b` ← `feature-a`'s `a`, with `feature-c` empty at
/// the base.
fn loose_empty_and_owning_info() -> RepoInfo {
    let mut info = two_branch_info();
    info.commits.insert(0, commit('c', 'b', "Loose fix"));
    info.branches.push(BranchInfo {
        name: "feature-c".to_string(),
        tip_oid: oid('9'),
        remote: None,
    });
    info
}

fn branch_order(app: &App) -> Vec<String> {
    app.rows
        .iter()
        .filter(|r| matches!(r.kind, RowKind::BranchName { .. }))
        .map(|r| r.key.clone())
        .collect()
}

#[test]
fn loose_commits_are_drawn_above_empty_branches_above_owning_ones() {
    let theme = make_theme();
    let app = make_app(snapshot_of(loose_empty_and_owning_info()), &theme);
    assert!(row_at(&app, &oid('c').to_string()) < row_at(&app, "br:feature-c"));
    assert_eq!(
        branch_order(&app),
        ["br:feature-c", "br:feature-b", "br:feature-a"]
    );
}

/// Filling `feature-c` or emptying `feature-a` regrouped them mid-walk, so
/// `↑`/`↓` seemed to jump.
#[test]
fn moving_a_commit_keeps_every_branch_in_place() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(loose_empty_and_owning_info()), &theme);
    let before = branch_order(&app);
    move_start_on(&mut app, &oid('a').to_string());
    let slots = move_slots_of(&app);
    assert!(slots.contains(&MoveSlot::Branch("feature-c".to_string())));
    for _ in 0..slots.len() {
        press(&mut app, KeyCode::Up);
        assert_eq!(branch_order(&app), before, "at {:?}", move_slot_of(&app));
    }
    while move_slot_of(&app) != MoveSlot::Branch("feature-c".to_string()) {
        press(&mut app, KeyCode::Down);
        assert_eq!(branch_order(&app), before, "at {:?}", move_slot_of(&app));
    }
    assert_eq!(
        row_at(&app, &oid('a').to_string()),
        row_at(&app, "br:feature-c") + 1
    );
}

/// `a` ← `b` on `feature-a`: `--above a` for `b` is where `b` already is, and
/// `fold --above` refuses it.
#[test]
fn moving_a_commit_skips_the_place_the_command_refuses() {
    let theme = make_theme();
    let mut info = make_info();
    info.commits = vec![
        commit('b', 'a', "Add lexer"),
        commit('a', '9', "Add parser"),
    ];
    info.branches[0].tip_oid = oid('b');
    let mut app = make_app(snapshot_of(info), &theme);
    move_start_on(&mut app, &oid('b').to_string());

    assert_eq!(
        move_slots_of(&app),
        vec![MoveSlot::Stay, MoveSlot::Below(oid('a'))]
    );
}

#[test]
fn moving_a_commit_above_a_branch_tip_carries_its_names_along() {
    let mut info = two_branch_info();
    info.branches.push(BranchInfo {
        name: "feature-b2".to_string(),
        tip_oid: oid('b'),
        remote: None,
    });
    place_moved_commit(&mut info, oid('a'), &MoveSlot::Above(oid('b')));

    let tip = |name: &str| {
        info.branches
            .iter()
            .find(|b| b.name == name)
            .unwrap()
            .tip_oid
    };
    assert_eq!(tip("feature-b"), oid('a'));
    assert_eq!(tip("feature-b2"), oid('a'));
    assert_eq!(tip("feature-a"), oid('9'));
    let order: Vec<git2::Oid> = info.commits.iter().map(|c| c.oid).collect();
    assert_eq!(order, vec![oid('a'), oid('b')]);
    assert_eq!(info.commits[0].parent_oid, Some(oid('b')));
}

#[test]
fn move_refuses_what_is_not_one_commit() {
    let theme = make_theme();
    let a = oid('a').to_string();
    let b = oid('b').to_string();
    for (keys, expected) in [
        (vec!["br:feature-a".to_string()], "move: move to a commit"),
        (vec!["wf:a.rs".to_string()], "move: move to a commit"),
        (vec![b.clone(), a.clone()], "move: one commit at a time"),
    ] {
        let mut app = make_app(snapshot_of(two_branch_info()), &theme);
        for key in &keys {
            move_cursor_to(&mut app, key);
            app.toggle_selection();
        }
        press(&mut app, KeyCode::Char('m'));
        assert!(matches!(app.mode, Mode::Normal), "{keys:?}");
        assert_eq!(app.notice.as_deref(), Some(expected));
    }

    let mut app = make_app(make_snapshot(), &theme);
    move_start_on(&mut app, &a);
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.notice.as_deref(), Some("move: nowhere else to put it"));
}

#[test]
fn move_mode_blocks_action_keys_and_escape_cancels_it() {
    let theme = make_theme();
    let mut app = make_app(snapshot_of(two_branch_info()), &theme);
    let a = oid('a').to_string();
    move_start_on(&mut app, &a);
    for code in [
        KeyCode::Char(' '),
        KeyCode::Char('m'),
        KeyCode::Char('f'),
        KeyCode::Char('d'),
        KeyCode::Left,
    ] {
        press(&mut app, code);
        assert!(
            matches!(app.mode, Mode::MoveTarget { .. }),
            "{code:?} left move mode"
        );
        assert!(app.notice.is_some(), "{code:?} showed no notice");
    }

    press(&mut app, KeyCode::Up);
    press(&mut app, KeyCode::Esc);
    assert!(matches!(app.mode, Mode::Normal));
    assert!(app.outcome.is_none());
    assert_eq!(cursor_key(&app), a);
    assert_eq!(row_at(&app, &a), row_at(&app, "br:feature-a") + 1);
}

#[test]
fn fold_mode_blocks_action_keys() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.action_fold_start();
    assert!(matches!(app.mode, Mode::FoldTarget { .. }));

    for code in [
        KeyCode::Char(' '),
        KeyCode::Char('c'),
        KeyCode::Char('f'),
        KeyCode::Char('b'),
        KeyCode::Char('d'),
        KeyCode::Char('r'),
        KeyCode::Char('R'),
        KeyCode::Char('+'),
        KeyCode::Char('='),
        KeyCode::Char('-'),
        KeyCode::F(5),
    ] {
        let result = app.handle_key(PaneId::Left, code, KeyModifiers::NONE);
        assert!(
            matches!(result, KeyResult::Handled),
            "{code:?} must not exit fold mode"
        );
        assert!(
            matches!(app.mode, Mode::FoldTarget { .. }),
            "{code:?} left fold mode"
        );
        assert!(app.selected.is_empty(), "{code:?} changed the selection");
        assert!(app.notice.is_some(), "{code:?} showed no notice");
    }
}

#[test]
fn escape_cancels_fold_mode() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.action_fold_start();
    assert!(matches!(app.mode, Mode::FoldTarget { .. }));

    app.handle_escape();
    assert!(matches!(app.mode, Mode::Normal));
    assert!(app.outcome.is_none());
}

#[test]
fn drop_and_reword_require_an_actionable_row() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);

    // Upstream row: not actionable.
    let up_key = app
        .rows
        .iter()
        .find(|r| matches!(r.kind, RowKind::Upstream { .. }))
        .unwrap()
        .key
        .clone();
    move_cursor_to(&mut app, &up_key);
    assert!(app.action_drop().is_none());
    assert!(app.action_reword().is_none());

    // Commit row: actionable.
    move_cursor_to(&mut app, &oid('a').to_string());
    let Some(Action::Reword { target, name }) = app.action_reword() else {
        panic!("expected a reword action");
    };
    assert_eq!(target, oid('a').to_string());
    assert_eq!(name, None);
    assert_eq!(
        app.action_drop(),
        Some(Action::Drop {
            targets: vec![oid('a').to_string()]
        })
    );
}

#[test]
fn drop_takes_the_local_changes_header_as_zz() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, LOCAL_CHANGES_KEY);
    let zz = app.snapshot.ids.get_unstaged().to_string();
    assert_eq!(app.action_drop(), Some(Action::Drop { targets: vec![zz] }));
}

#[test]
fn drop_takes_every_selected_working_file_but_only_files_together() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    press(&mut app, KeyCode::Char(' '));
    press(&mut app, KeyCode::Char(' '));
    // The cursor moved on past the selection; the selection still wins.
    let ids = &app.snapshot.ids;
    let expected = vec![
        ids.get_file("a.rs").to_string(),
        ids.get_file("b.rs").to_string(),
    ];
    assert_eq!(app.action_drop(), Some(Action::Drop { targets: expected }));

    app.selected.insert(oid('a').to_string());
    assert!(app.action_drop().is_none());
    assert_eq!(
        app.notice.as_deref(),
        Some("drop: only files can be dropped together")
    );
}

#[test]
fn reword_on_a_branch_renames_it_in_the_tree() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "br:feature-a");

    assert!(matches!(
        press(&mut app, KeyCode::Char('r')),
        KeyResult::Handled
    ));
    let Mode::RenameBranch { branch, field } = &app.mode else {
        panic!("expected rename mode");
    };
    assert_eq!(branch, "feature-a");
    assert_eq!(
        field.value(),
        "feature-a",
        "the field starts at the old name"
    );
    assert!(app.modal_active(), "the field must own every key");

    // Action and quit keys type into the field instead of firing.
    for code in [KeyCode::Backspace, KeyCode::Char('q')] {
        assert!(matches!(press(&mut app, code), KeyResult::Handled));
    }
    // A chord is not text: Ctrl-U must not rename the branch to `feature-qu`.
    app.handle_key(PaneId::Left, KeyCode::Char('u'), KeyModifiers::CONTROL);
    let Mode::RenameBranch { field, .. } = &app.mode else {
        panic!("expected rename mode");
    };
    assert_eq!(field.value(), "feature-q");
    assert!(
        app.handle_rename_key(KeyCode::Backspace, KeyModifiers::NONE)
            .is_none()
    );
    assert!(
        app.handle_rename_key(KeyCode::Char('b'), KeyModifiers::NONE)
            .is_none()
    );
    let Some(Action::Reword { target, name }) =
        app.handle_rename_key(KeyCode::Enter, KeyModifiers::NONE)
    else {
        panic!("expected a reword action");
    };
    assert_eq!(target, "feature-a");
    assert_eq!(name.as_deref(), Some("feature-b"));
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.next_cursor.as_deref(), Some("br:feature-b"));

    // A failed rename leaves the cursor on the row that kept its name.
    app.finish_action(Err(anyhow::anyhow!("boom")));
    assert_eq!(app.next_cursor, None);
}

#[test]
fn rename_is_cancelled_by_escape_and_by_an_unchanged_name() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "br:feature-a");

    app.action_reword();
    assert!(
        app.handle_rename_key(KeyCode::Esc, KeyModifiers::NONE)
            .is_none()
    );
    assert!(matches!(app.mode, Mode::Normal));

    app.action_reword();
    assert!(
        app.handle_rename_key(KeyCode::Enter, KeyModifiers::NONE)
            .is_none()
    );
    assert!(matches!(app.mode, Mode::Normal));

    // An empty name keeps the field open rather than renaming to nothing.
    app.action_reword();
    for _ in 0.."feature-a".len() {
        app.handle_rename_key(KeyCode::Backspace, KeyModifiers::NONE);
    }
    assert!(
        app.handle_rename_key(KeyCode::Enter, KeyModifiers::NONE)
            .is_none()
    );
    assert!(matches!(app.mode, Mode::RenameBranch { .. }));
}

/// Key of the fake branch drawn while a new branch is being named.
fn pending_key() -> String {
    branch_key(NEW_BRANCH_NAME)
}

#[test]
fn new_branch_on_a_tip_commit_is_drawn_co_located_with_its_branch() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, &oid('a').to_string());

    press(&mut app, KeyCode::Char('b'));
    let Mode::NewBranch {
        target,
        tip,
        origin,
        ..
    } = &app.mode
    else {
        panic!("expected new-branch mode");
    };
    assert_eq!(target.as_deref(), Some(oid('a').to_string().as_str()));
    assert_eq!(*tip, oid('a'));
    assert_eq!(origin, &oid('a').to_string());
    assert_eq!(
        cursor_key(&app),
        pending_key(),
        "the field takes the cursor"
    );
    assert!(app.modal_active(), "the field must own every key");

    // Same tip as feature-a: one co-located group. The placeholder is drawn
    // on top because the name that will order the group is not typed yet.
    let at = app.tree.cursor();
    assert_eq!(app.rows[at + 1].key, "br:feature-a");
    assert_eq!(app.rows[at + 2].key, oid('a').to_string());
    let RowKind::BranchName {
        connector, range, ..
    } = &app.rows[at + 1].kind
    else {
        panic!("expected a branch row");
    };
    assert_eq!(*connector, "│├─", "feature-a hangs under the placeholder");
    assert!(range.is_some(), "drawn as owning `a`");
    assert_eq!(
        diff_text(&app.snapshot, &app.rows[at]),
        "branch not created yet",
        "but nothing to diff until it exists"
    );

    // Action and quit keys type into the field instead of firing.
    for c in "qb".chars() {
        press(&mut app, KeyCode::Char(c));
    }
    let Some(Action::NewBranch { name, target }) =
        app.handle_name_key(KeyCode::Enter, KeyModifiers::NONE)
    else {
        panic!("expected a branch action");
    };
    assert_eq!(name, "qb");
    assert_eq!(target, Some(oid('a').to_string()));
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.next_cursor.as_deref(), Some("br:qb"));
    // The row keeps the typed name while the command runs.
    let RowKind::BranchName { name, .. } = &app.rows[at].kind else {
        panic!("expected a branch row");
    };
    assert_eq!(name, "qb");

    // A failed create has no `qb` row to land on and the fake row is gone
    // too: go back to where `b` was pressed.
    app.finish_action(Err(anyhow::anyhow!("boom")));
    assert_eq!(
        app.next_cursor.as_deref(),
        Some(oid('a').to_string().as_str())
    );
    assert_eq!(app.fallback_cursor, None);
}

/// feature-a owns `a` and `b`; branching at `b` splits it into a stack.
#[test]
fn new_branch_inside_a_branch_splits_it_into_a_stack() {
    let mut info = make_info();
    info.commits = vec![
        commit('a', 'b', "Add parser"),
        commit('b', '9', "Add lexer"),
    ];
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);
    move_cursor_to(&mut app, &oid('b').to_string());

    press(&mut app, KeyCode::Char('b'));
    let at = app.tree.cursor();
    assert_eq!(cursor_key(&app), pending_key());
    assert_eq!(app.rows[at + 1].key, oid('b').to_string());
    assert!(
        matches!(app.rows[at - 1].kind, RowKind::Spacer("││")),
        "stacked under feature-a"
    );
    assert_eq!(app.rows[at - 2].key, oid('a').to_string());
}

#[test]
fn new_branch_on_a_branch_row_targets_that_branch() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "br:feature-a");

    press(&mut app, KeyCode::Char('b'));
    let Mode::NewBranch { target, tip, .. } = &app.mode else {
        panic!("expected new-branch mode");
    };
    assert_eq!(target.as_deref(), Some("feature-a"));
    assert_eq!(*tip, oid('a'), "a branch target means its tip");
    let at = app.tree.cursor();
    assert_eq!(cursor_key(&app), pending_key());
    assert_eq!(app.rows[at + 1].key, "br:feature-a");

    for c in "fb".chars() {
        press(&mut app, KeyCode::Char(c));
    }
    assert!(
        app.handle_name_key(KeyCode::Enter, KeyModifiers::NONE)
            .is_some()
    );
    // Success follows the new branch; the fallback is not used.
    app.finish_action(Ok(()));
    assert_eq!(app.next_cursor.as_deref(), Some("br:fb"));
    assert_eq!(app.fallback_cursor, None);
}

/// `c` is loose on the integration line; `-t` on it weaves it onto the new
/// branch (Spec 005), so the preview must already draw it there.
#[test]
fn new_branch_on_a_loose_commit_pulls_it_off_the_integration_line() {
    let mut info = make_info();
    info.commits = vec![commit('c', 'a', "Tweak"), commit('a', '9', "Add parser")];
    let theme = make_theme();
    let mut app = make_app(snapshot_of(info), &theme);
    move_cursor_to(&mut app, &oid('c').to_string());
    assert!(
        matches!(
            app.rows[app.tree.cursor()].kind,
            RowKind::Commit {
                dot_color: None,
                ..
            }
        ),
        "loose until `b`"
    );

    press(&mut app, KeyCode::Char('b'));
    let Mode::NewBranch { target, tip, .. } = &app.mode else {
        panic!("expected new-branch mode");
    };
    assert_eq!(target.as_deref(), Some(oid('c').to_string().as_str()));
    assert_eq!(*tip, oid('c'));

    let at = app.tree.cursor();
    assert_eq!(cursor_key(&app), pending_key());
    let RowKind::BranchName { range, .. } = &app.rows[at].kind else {
        panic!("expected a branch row");
    };
    assert_eq!(
        range.clone(),
        Some((oid('a').to_string(), oid('c').to_string())),
        "owning `c` alone: feature-a's tip stops the walk"
    );
    assert_eq!(app.rows[at + 1].key, oid('c').to_string());
    assert!(
        matches!(
            app.rows[at + 1].kind,
            RowKind::Commit {
                dot_color: Some(_),
                ..
            }
        ),
        "drawn on the branch, not the integration line"
    );
    let next = app.rows[at + 2..]
        .iter()
        .find(|r| r.focusable)
        .expect("a row after the commit");
    assert_eq!(next.key, "br:feature-a", "stacked on feature-a");
}

#[test]
fn new_branch_off_a_commit_or_branch_lands_at_the_base() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");

    press(&mut app, KeyCode::Char('b'));
    let Mode::NewBranch { target, tip, .. } = &app.mode else {
        panic!("expected new-branch mode");
    };
    assert_eq!(*target, None, "no -t: the branch is created at the base");
    assert_eq!(*tip, oid('9'));
    let at = app.tree.cursor();
    assert_eq!(cursor_key(&app), pending_key());
    let RowKind::BranchName { range, .. } = &app.rows[at].kind else {
        panic!("expected a branch row");
    };
    assert!(range.is_none(), "an empty branch owns nothing");
    let next = app.rows[at + 1..]
        .iter()
        .find(|r| r.focusable)
        .expect("a row after the new branch");
    assert_eq!(
        next.key, "br:feature-a",
        "an empty branch is drawn above the branches that own commits"
    );
    let before = app.rows[..at]
        .iter()
        .rev()
        .find(|r| r.focusable)
        .expect("a row before the new branch");
    assert_eq!(before.key, "wf:b.rs", "right under the local changes");
}

#[test]
fn new_branch_is_cancelled_by_escape_and_by_an_empty_name() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, &oid('a').to_string());
    let rows_before = app.rows.len();

    press(&mut app, KeyCode::Char('b'));
    assert!(
        app.handle_name_key(KeyCode::Esc, KeyModifiers::NONE)
            .is_none()
    );
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.rows.len(), rows_before, "the fake branch is gone");
    assert_eq!(cursor_key(&app), oid('a').to_string());

    // Enter with nothing typed stops the creation, like Esc.
    press(&mut app, KeyCode::Char('b'));
    assert!(
        app.handle_name_key(KeyCode::Enter, KeyModifiers::NONE)
            .is_none()
    );
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.rows.len(), rows_before);

    // A chord is not text: Ctrl-C cancels instead of typing a `c`.
    press(&mut app, KeyCode::Char('b'));
    app.handle_key(PaneId::Left, KeyCode::Char('c'), KeyModifiers::CONTROL);
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.rows.len(), rows_before);
}

#[test]
fn command_line_uses_the_short_ids_the_tree_shows() {
    let theme = make_theme();
    let app = make_app(make_snapshot(), &theme);
    let ids = &app.snapshot.ids;
    let commit = ids.get_commit(oid('a')).to_string();
    let file = ids.get_file("a.rs").to_string();

    assert_eq!(
        app.command_line(&Action::Fold {
            sources: vec![file.clone(), ids.get_unstaged().to_string()],
            target: oid('a').to_string(),
            hunks: None,
        }),
        format!("loom fold {} {} {}", file, ids.get_unstaged(), commit)
    );
    assert_eq!(
        app.command_line(&Action::Fold {
            sources: vec![oid('a').to_string()],
            target: ids.get_unstaged().to_string(),
            hunks: Some(HunkArgs::new(
                vec!["src/a b.rs:1".to_string()],
                Some("0123abcd".to_string())
            )),
        }),
        format!(
            "loom fold -p {} {} --hunks 'src/a b.rs:1' --hunks-from 0123abcd",
            commit,
            ids.get_unstaged()
        )
    );
    assert_eq!(
        app.command_line(&Action::FoldHunks {
            sources: vec![file.clone()],
            picked: Vec::new(),
            stamp: Vec::new(),
            target: oid('a').to_string(),
        }),
        format!("loom fold -p {} {}", file, commit)
    );
    assert_eq!(
        app.command_line(&Action::Commit {
            source: CommitSource::Files(vec![ids.get_unstaged().to_string()]),
            dest: CommitDest::Integration,
        }),
        format!("loom commit -i {}", ids.get_unstaged())
    );
    assert_eq!(
        app.command_line(&Action::Commit {
            source: CommitSource::Files(vec![file.clone()]),
            dest: CommitDest::Branch("feature-a".to_string()),
        }),
        format!("loom commit -b {} {}", ids.get_branch("feature-a"), file)
    );
    assert_eq!(
        app.command_line(&Action::NewBranch {
            name: "feature-b".to_string(),
            target: Some("feature-a".to_string()),
        }),
        format!(
            "loom branch new feature-b -t {}",
            ids.get_branch("feature-a")
        )
    );
    assert_eq!(
        app.command_line(&Action::NewBranch {
            name: "feature-b".to_string(),
            target: None,
        }),
        "loom branch new feature-b"
    );
    assert_eq!(
        app.command_line(&Action::Drop {
            targets: vec![oid('a').to_string()],
        }),
        format!("loom drop {}", commit)
    );
    assert_eq!(
        app.command_line(&Action::Split {
            commit: oid('a').to_string(),
            files: vec!["src/a b.rs".to_string()],
            hunks: None,
        }),
        format!("loom split {} 'src/a b.rs'", commit)
    );
    assert_eq!(
        app.command_line(&Action::Split {
            commit: oid('a').to_string(),
            files: Vec::new(),
            hunks: Some(HunkArgs::new(
                vec!["src/a b.rs:1".to_string()],
                Some("0123abcd".to_string())
            )),
        }),
        format!(
            "loom split -p {} --hunks 'src/a b.rs:1' --hunks-from 0123abcd",
            commit
        )
    );
    // A target no row shows (rewritten meanwhile) is passed through.
    assert_eq!(
        app.command_line(&Action::Reword {
            target: "deadbeef".to_string(),
            name: None,
        }),
        "loom reword deadbeef"
    );
    assert_eq!(
        app.command_line(&Action::Reword {
            target: "feature-a".to_string(),
            name: Some("feature-b".to_string()),
        }),
        format!("loom reword {} -m feature-b", ids.get_branch("feature-a"))
    );
}

#[test]
fn prompt_request_opens_a_popup_that_owns_the_keys_and_replies() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let (reply, answer) = channel();
    app.handle_request(Request::Prompt {
        kind: PromptKind::Input { placeholder: None },
        prompt: "Branch name".to_string(),
        error: None,
        reply,
    });
    assert!(app.modal_active());
    let Some(Popup::Prompt { prompt, .. }) = &app.popup else {
        panic!("expected a prompt popup");
    };
    assert_eq!(prompt.title(), "Branch name");

    // Action keys type into the field instead of firing.
    for c in ['f', 'd', 'q'] {
        assert!(matches!(
            press(&mut app, KeyCode::Char(c)),
            KeyResult::Handled
        ));
    }
    assert!(app.popup.is_some());
    press(&mut app, KeyCode::Enter);
    assert!(app.popup.is_none());
    assert_eq!(
        answer.recv().unwrap(),
        Some(Answer::Text("fdq".to_string()))
    );

    let (reply, answer) = channel();
    app.handle_request(Request::Prompt {
        kind: PromptKind::Confirm,
        prompt: "Drop?".to_string(),
        error: None,
        reply,
    });
    press(&mut app, KeyCode::Esc);
    assert_eq!(answer.recv().unwrap(), None);
    assert!(app.popup.is_none());
}

#[test]
fn messages_land_in_the_current_log_entry() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    app.log.push(LogEntry {
        command: "loom reword a1".to_string(),
        lines: Vec::new(),
    });
    app.handle_request(Request::Message {
        level: Level::Success,
        text: "Reworded `a1`".to_string(),
    });
    assert_eq!(
        app.log[0].lines,
        vec![(Level::Success, "Reworded `a1`".to_string())]
    );

    // Success: the last ✓ line becomes the status-bar notice.
    assert!(app.finish_action(Ok(())));
    assert_eq!(app.notice.as_deref(), Some("✓ Reworded `a1`"));
    assert!(app.popup.is_none());

    // Several ✓ lines (`drop a b`): the last one, with a count of the rest.
    for text in ["Restored `a`", "Deleted `b`"] {
        app.handle_request(Request::Message {
            level: Level::Success,
            text: text.to_string(),
        });
    }
    assert!(app.finish_action(Ok(())));
    assert_eq!(
        app.notice.as_deref(),
        Some("✓ Deleted `b` (+2 more, L: log)")
    );

    // Failure: an error popup, and the line is logged.
    assert!(!app.finish_action(Err(anyhow::anyhow!("Nothing to commit"))));
    assert!(matches!(
        app.popup,
        Some(Popup::Notice {
            then: AfterNotice::Reload,
            ..
        })
    ));
    assert_eq!(
        app.log[0].lines.last(),
        Some(&(Level::Error, "Nothing to commit".to_string()))
    );
    press(&mut app, KeyCode::Char('x'));
    assert!(app.popup.is_some(), "only Enter/Esc dismiss a notice");

    // A cancelled prompt is not an error.
    app.popup = None;
    assert!(!app.finish_action(Err(Cancelled.into())));
    assert_eq!(app.notice.as_deref(), Some("cancelled"));
    assert!(app.popup.is_none());
}

/// A failure while the log is open must not yank the log out from under the
/// reader; the reload happens behind it instead.
#[test]
fn a_failure_keeps_an_open_log() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    app.log.push(LogEntry {
        command: "loom drop a1".to_string(),
        lines: Vec::new(),
    });
    app.open_log();

    assert!(app.finish_action(Err(anyhow::anyhow!("Nothing to commit"))));
    assert!(matches!(app.popup, Some(Popup::Log { .. })));
    assert_eq!(
        app.log[0].lines.last(),
        Some(&(Level::Error, "Nothing to commit".to_string()))
    );
}

#[test]
fn running_action_blocks_tree_keys_until_it_finishes() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let release = blocked_worker(&mut app);
    assert!(app.modal_active());

    press(&mut app, KeyCode::Char('c'));
    assert!(app.popup.is_none());
    assert_eq!(app.notice.as_deref(), Some("an action is running…"));
    assert!(matches!(app.poll_background(), Tick::Redraw));
    assert!(app.mode_hint().unwrap().contains("loom reword a1"));

    // The log stays reachable while waiting.
    press(&mut app, KeyCode::Char('L'));
    assert!(matches!(app.popup, Some(Popup::Log { .. })));
    press(&mut app, KeyCode::Esc);
    assert!(app.popup.is_none());

    drop(release);
    poll_until_idle(&mut app);
    assert!(!app.modal_active());
    assert_eq!(app.notice.as_deref(), Some("cancelled"));
    assert!(matches!(app.poll_background(), Tick::Idle));
}

#[test]
fn suspend_request_reaches_the_shell_and_resume_ends_the_wait() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let release = blocked_worker(&mut app);
    let (ack, acked) = channel();
    app.request_tx.send(Request::Suspend(ack)).unwrap();
    let Tick::Suspend(ack) = app.poll_background() else {
        panic!("expected a suspend tick");
    };
    ack.send(()).unwrap();
    assert!(acked.recv().is_ok());

    app.request_tx.send(Request::Resume).unwrap();
    app.wait_for_resume();

    // A worker gone without a Resume ends the wait too.
    drop(release);
    app.wait_for_resume();
    poll_until_idle(&mut app);
}

#[test]
fn log_popup_toggles_and_scrolls() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    press(&mut app, KeyCode::Char('L'));
    assert!(matches!(app.popup, Some(Popup::Log { .. })));
    press(&mut app, KeyCode::Down);
    assert!(matches!(app.popup, Some(Popup::Log { .. })));
    press(&mut app, KeyCode::Char('L'));
    assert!(app.popup.is_none());
}

#[test]
fn apply_snapshot_keeps_cursor_and_expansion_but_drops_selection() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let key = oid('a').to_string();
    move_cursor_to(&mut app, &key);
    app.expand_current();
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    move_cursor_to(&mut app, &key);

    app.apply_snapshot(make_snapshot());
    assert_eq!(cursor_key(&app), key);
    assert!(
        app.rows
            .iter()
            .any(|r| matches!(r.kind, RowKind::CommitFile { .. }))
    );
    assert!(app.selected.is_empty());
    assert!(
        app.diff_cache.len() <= 1,
        "cache holds at most the cursor row"
    );
}

#[test]
fn popups_render_over_the_panes() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let (reply, _answer) = channel();
    app.handle_request(Request::Prompt {
        kind: PromptKind::Select {
            items: vec!["feature-a".to_string()],
            allow_other: true,
        },
        prompt: "Target branch".to_string(),
        error: None,
        reply,
    });
    let mut shell = Shell::new(app);
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let buffer = terminal.backend().buffer();
    let text: String = (0..buffer.area.height)
        .map(|y| {
            (0..buffer.area.width)
                .map(|x| buffer[(x, y)].symbol())
                .collect::<String>()
        })
        .collect::<Vec<_>>()
        .join("\n");
    assert!(text.contains(" Target branch "));
    assert!(text.contains("feature-a"));
}

#[test]
fn rename_draws_the_edited_name_on_the_branch_row() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "br:feature-a");
    press(&mut app, KeyCode::Char('r'));
    for code in [KeyCode::Backspace, KeyCode::Char('z')] {
        press(&mut app, code);
    }

    let mut shell = Shell::new(app);
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let buffer = terminal.backend().buffer();
    let text: String = (0..buffer.area.height)
        .map(|y| {
            (0..buffer.area.width)
                .map(|x| buffer[(x, y)].symbol())
                .collect::<String>()
        })
        .collect::<Vec<_>>()
        .join("\n");
    // The trailing space of the field carries the cursor cell.
    assert!(
        text.contains("[feature-z ]"),
        "field drawn in place: {text}"
    );
    assert!(text.contains(" Rename branch "));
    assert!(text.contains("Esc to cancel"));

    let (x, y) = (0..buffer.area.height)
        .flat_map(|y| (0..buffer.area.width).map(move |x| (x, y)))
        .find(|&(x, y)| {
            buffer[(x, y)]
                .style()
                .add_modifier
                .contains(Modifier::REVERSED)
        })
        .expect("no cursor cell");
    assert_eq!(
        buffer[(x - 1, y)].symbol(),
        "z",
        "cursor sits after the name"
    );
}

#[test]
fn new_branch_draws_the_field_on_the_fake_branch_row() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, &oid('a').to_string());
    press(&mut app, KeyCode::Char('b'));
    for c in "fix".chars() {
        press(&mut app, KeyCode::Char(c));
    }

    let mut shell = Shell::new(app);
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let buffer = terminal.backend().buffer();
    let lines: Vec<String> = (0..buffer.area.height)
        .map(|y| {
            (0..buffer.area.width)
                .map(|x| buffer[(x, y)].symbol())
                .collect::<String>()
        })
        .collect();
    let at = lines
        .iter()
        // The trailing space of the field carries the cursor cell.
        .position(|line| line.contains("│╭─ [CREATE BRANCH] [fix ]"))
        .expect("no fake branch row");
    assert!(lines[at + 1].contains("│├─ fa [feature-a]"), "{lines:#?}");
    assert!(lines[at + 2].contains("Add parser"), "{lines:#?}");
    assert!(lines.iter().any(|line| line.contains(" New branch ")));
    assert!(lines.iter().any(|line| line.contains("Enter to create")));
}

#[test]
fn commit_draws_a_placeholder_row_at_its_destination() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, LOCAL_CHANGES_KEY);
    press(&mut app, KeyCode::Char('c'));
    press(&mut app, KeyCode::Down);

    let mut shell = Shell::new(app);
    let backend = ratatui::backend::TestBackend::new(100, 30);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let buffer = terminal.backend().buffer();
    let lines: Vec<String> = (0..buffer.area.height)
        .map(|y| {
            (0..buffer.area.width)
                .map(|x| buffer[(x, y)].symbol())
                .collect::<String>()
        })
        .collect();
    let at = lines
        .iter()
        .position(|line| line.contains("[CREATE COMMIT] new commit (2 files)"))
        .expect("no placeholder commit row");
    assert!(lines[at - 1].contains("[feature-a]"), "{lines:#?}");
    assert!(lines[at + 1].contains("Add parser"), "{lines:#?}");
    assert!(
        lines
            .iter()
            .any(|l| l.contains(" Commit zz → [feature-a] "))
    );
    assert!(lines.iter().any(|l| l.contains("Enter to commit")));
}

#[test]
fn commit_marks_the_header_and_the_files_it_covers() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, LOCAL_CHANGES_KEY);
    press(&mut app, KeyCode::Char('c'));

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(
        tree_line(&lines, "[local changes]").contains("▸ "),
        "{lines:#?}"
    );
    assert!(tree_line(&lines, "a.rs").contains("▸ "), "{lines:#?}");
    assert!(tree_line(&lines, "b.rs").contains("▸ "), "{lines:#?}");
    assert!(
        !tree_line(&lines, "[feature-a]").contains("▸ "),
        "{lines:#?}"
    );
}

#[test]
fn commit_marks_the_cursor_row_when_nothing_is_selected() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    press(&mut app, KeyCode::Char('c'));

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(tree_line(&lines, "a.rs").contains("▸ "), "{lines:#?}");
    // The sibling is neither named nor covered: `c` took one file.
    assert!(!tree_line(&lines, "b.rs").contains("▸ "), "{lines:#?}");
    assert!(
        lines
            .iter()
            .any(|l| l.contains(" Commit aa1 → [integration] ")),
        "{lines:#?}"
    );
}

#[test]
fn cancelling_a_commit_takes_the_source_marks_with_it() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    press(&mut app, KeyCode::Char('c'));
    app.handle_escape();

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(!tree_line(&lines, "a.rs").contains("▸ "), "{lines:#?}");
    assert!(lines.iter().any(|l| l.contains(" Status ")), "{lines:#?}");
}

#[test]
fn fold_marks_its_sources_while_the_target_is_picked() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    move_cursor_to(&mut app, "wf:a.rs");
    app.toggle_selection();
    app.action_fold_start();
    move_cursor_to(&mut app, &oid('a').to_string());

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    // The source outranks its own `✓`: the cursor is on the target now.
    assert!(tree_line(&lines, "a.rs").contains("▸ "), "{lines:#?}");
    assert!(!tree_line(&lines, "a.rs").contains("✓ "), "{lines:#?}");
    assert!(
        lines.iter().any(|l| l.contains(" Fold aa1 into... ")),
        "{lines:#?}"
    );
}

#[test]
fn a_title_too_narrow_for_the_sources_counts_them_instead() {
    let theme = make_theme();
    let mut info = make_info();
    info.working_changes = (0..6)
        .map(|i| file(&format!("f{i}.rs"), ' ', 'M'))
        .collect();
    let mut app = make_app(snapshot_of(info), &theme);
    for i in 0..6 {
        move_cursor_to(&mut app, &format!("wf:f{i}.rs"));
        app.toggle_selection();
    }
    press(&mut app, KeyCode::Char('c'));

    let mut shell = Shell::new(app);
    // Wide enough for the count, too narrow for the six short IDs.
    let lines = rendered_lines_at(&mut shell, 90);
    assert!(
        lines
            .iter()
            .any(|l| l.contains(" Commit 6 item(s) → [integration] ")),
        "{lines:#?}"
    );
}

/// The staged files are hidden behind a closed header, so the header itself
/// has to carry the mark: a `C` placement with nothing marked is the case the
/// marks exist for.
#[test]
fn a_staged_commit_marks_the_closed_header() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    app.expanded.remove(LOCAL_CHANGES_KEY);
    app.rebuild_rows(LOCAL_CHANGES_KEY);
    let source_rows = app.index_sources();
    app.enter_commit_target(
        CommitSource::Index,
        source_rows,
        LOCAL_CHANGES_KEY.to_string(),
    );

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(
        tree_line(&lines, "[local changes]").contains("▸ "),
        "{lines:#?}"
    );
}

/// `C` commits the index, which no row names — only the staged files it
/// carries can be pointed at.
#[test]
fn a_staged_commit_marks_the_files_the_index_holds() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let source_rows = app.index_sources();
    app.enter_commit_target(
        CommitSource::Index,
        source_rows,
        LOCAL_CHANGES_KEY.to_string(),
    );

    let mut shell = Shell::new(app);
    let lines = rendered_lines(&mut shell);
    assert!(tree_line(&lines, "a.rs").contains("▸ "), "{lines:#?}");
    assert!(!tree_line(&lines, "b.rs").contains("▸ "), "{lines:#?}");
    assert!(
        tree_line(&lines, "[local changes]").contains("▸ "),
        "{lines:#?}"
    );
    assert!(
        lines.iter().any(|l| l.contains(" Commit the index → ")),
        "{lines:#?}"
    );
}

#[test]
fn render_smoke_test_on_every_focusable_row() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    // Expand everything so file rows render too.
    app.expanded.insert(oid('a').to_string());
    app.rebuild_rows(LOCAL_CHANGES_KEY);

    let mut shell = Shell::new(app);
    let backend = ratatui::backend::TestBackend::new(120, 40);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    for i in 0..shell.app.rows.len() {
        if shell.app.rows[i].focusable {
            shell.app.tree.set_cursor(i);
            terminal.draw(|f| shell.render(f)).unwrap();
        }
    }
}

#[test]
fn mouse_click_on_tree_bottom_border_is_ignored() {
    let theme = make_theme();
    let mut shell = Shell::new(make_app(make_snapshot(), &theme));

    // Small terminal so rows exist below the visible tree area.
    let backend = ratatui::backend::TestBackend::new(80, 8);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let tree_area = shell.areas()[0];
    assert!(shell.app.rows.len() > (tree_area.height as usize).saturating_sub(2));

    // Before the guard, a click on the bottom border mapped to the row just
    // past the last visible one and moved the cursor there.
    shell.handle_event(Event::Mouse(MouseEvent {
        kind: MouseEventKind::Down(MouseButton::Left),
        column: tree_area.x + 1,
        row: tree_area.y + tree_area.height - 1,
        modifiers: KeyModifiers::NONE,
    }));
    assert_eq!(shell.app.tree.cursor(), 0);
}

/// The composed status bar must match the string pinned in specs/020-tui.md.
#[test]
fn status_bar_matches_spec() {
    let theme = make_theme();
    let mut shell = Shell::new(make_app(make_snapshot(), &theme));

    let backend = ratatui::backend::TestBackend::new(180, 12);
    let mut terminal = ratatui::Terminal::new(backend).unwrap();
    terminal.draw(|f| shell.render(f)).unwrap();
    let buffer = terminal.backend().buffer();
    let last_row: String = (0..buffer.area.width)
        .map(|x| buffer[(x, buffer.area.height - 1)].symbol())
        .collect();
    assert!(
        last_row.starts_with(" Commit: c/C | Fold: f/F | Branch: b | Drop: d | Help: ? | Quit: q"),
        "got: {:?}",
        last_row
    );
}

#[test]
fn ctrl_c_quits_via_shell() {
    let theme = make_theme();
    let mut shell = Shell::new(make_app(make_snapshot(), &theme));
    let exit = shell.handle_event(Event::Key(KeyEvent::new(
        KeyCode::Char('c'),
        KeyModifiers::CONTROL,
    )));
    assert!(matches!(exit, Some(Outcome::Quit)));
}

// -- diff_text against a real repo -------------------------------------------

fn repo_snapshot(repo: &crate::core::test_helpers::TestRepo) -> Snapshot {
    Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..make_snapshot()
    }
}

fn row(kind: RowKind, key: &str) -> Row {
    Row {
        kind,
        sid: String::new(),
        target: None,
        key: key.to_string(),
        focusable: true,
        expandable: false,
        expanded: false,
    }
}

#[test]
fn diff_text_per_row_kind() {
    let repo = crate::core::test_helpers::TestRepo::new();
    let base = repo.head_oid();
    repo.write_file("file.txt", "original content\n");
    repo.stage_files(&["file.txt"]);
    repo.commit_staged("Add file");
    let commit_oid = repo.head_oid();
    repo.write_file("file.txt", "changed\n");
    let snapshot = repo_snapshot(&repo);

    // Local changes: diff HEAD; count 0 short-circuits.
    let text = diff_text(&snapshot, &row(RowKind::LocalChanges { count: 1 }, "z"));
    assert!(text.contains("-original content"));
    assert!(text.contains("+changed"));
    let text = diff_text(&snapshot, &row(RowKind::LocalChanges { count: 0 }, "z"));
    assert_eq!(text, "no changes");

    // Working file: diff of that path.
    let kind = RowKind::WorkingFile {
        path: "file.txt".to_string(),
        index: ' ',
        worktree: 'M',
    };
    assert!(diff_text(&snapshot, &row(kind, "wf")).contains("+changed"));

    // Untracked working file routes through untracked_file_text.
    repo.write_file("new.txt", "fresh\n");
    let kind = RowKind::WorkingFile {
        path: "new.txt".to_string(),
        index: '?',
        worktree: '?',
    };
    let text = diff_text(&snapshot, &row(kind, "wf"));
    assert!(text.starts_with("untracked file: new.txt"));
    assert!(text.contains("+fresh"));

    // Branch with a range vs. one without commits of its own.
    let kind = RowKind::BranchName {
        name: "b".to_string(),
        remote: None,
        connector: "",
        range: Some((base.to_string(), commit_oid.to_string())),
    };
    assert!(diff_text(&snapshot, &row(kind, "br")).contains("original content"));
    let kind = RowKind::BranchName {
        name: "b".to_string(),
        remote: None,
        connector: "",
        range: None,
    };
    assert_eq!(
        diff_text(&snapshot, &row(kind, "br")),
        "branch has no commits of its own"
    );

    // Commit: git show with stat and patch.
    let kind = RowKind::Commit {
        oid: commit_oid,
        message: String::new(),
        dot_color: None,
        file_count: 1,
    };
    let text = diff_text(&snapshot, &row(kind, "c"));
    assert!(text.contains("Add file"));
    assert!(text.contains("+original content"));

    // Commit file: the patch without the commit header.
    let kind = RowKind::CommitFile {
        oid: commit_oid,
        path: "file.txt".to_string(),
        index: 'M',
        worktree: ' ',
        on_branch: false,
    };
    let text = diff_text(&snapshot, &row(kind, "cf"));
    assert!(text.contains("+original content"));
    assert!(!text.contains("Add file"));

    // Upstream: informational text, no git call.
    let kind = RowKind::Upstream {
        label: "origin/main".to_string(),
        base_short_id: "9999999".to_string(),
        base_message: "base".to_string(),
        commits_ahead: 2,
    };
    let text = diff_text(&snapshot, &row(kind, "up"));
    assert!(text.contains("upstream: origin/main"));
    assert!(text.contains("2 new commit(s)"));

    // Spacer: empty.
    assert_eq!(diff_text(&snapshot, &row(RowKind::Spacer("│"), "")), "");
}

/// What the pane really shows while a commit is being placed: the changes,
/// not the "not created yet" text `diff_text` falls back to once the mode is
/// over and the row is briefly still on screen.
#[test]
fn the_placeholder_pane_shows_the_preview_while_placing() {
    let repo = crate::core::test_helpers::TestRepo::new();
    repo.write_file("file.txt", "original content\n");
    repo.stage_files(&["file.txt"]);
    repo.commit_staged("Add file");
    repo.write_file("file.txt", "changed\n");

    let mut info = make_info();
    info.working_changes = vec![file("file.txt", ' ', 'M')];
    let snapshot = Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..snapshot_of(info)
    };
    let theme = make_theme();
    let mut app = make_app(snapshot, &theme);
    move_cursor_to(&mut app, "wf:file.txt");

    press(&mut app, KeyCode::Char('c'));
    let text: String = app.diff_cache[&pending_commit_key()]
        .iter()
        .flat_map(|line| line.spans.iter().map(|s| s.content.to_string()))
        .collect();
    assert!(text.contains("+changed"), "got: {text}");
    assert!(!text.contains("not created yet"), "got: {text}");
}

/// A failed tracked diff ends mid-line, so the untracked header that follows
/// must not be glued to it.
#[test]
fn pending_commit_diff_keeps_a_failed_diff_off_the_untracked_header() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("new.txt"), "fresh\n").unwrap();

    let mut info = make_info();
    info.working_changes = vec![file("tracked.rs", ' ', 'M'), file("new.txt", '?', '?')];
    let snapshot = Snapshot {
        workdir: dir.path().to_path_buf(),
        git_dir: dir.path().to_path_buf(),
        ..snapshot_of(info)
    };

    // Not a repository, so the tracked diff fails and returns its message,
    // which is what ends mid-line: without the guard the header is glued on.
    let only_tracked = pending_commit_diff(
        &snapshot,
        &[snapshot.ids.get_file("tracked.rs").to_string()],
    );
    assert!(only_tracked.starts_with("error:"), "got: {only_tracked}");
    assert!(!only_tracked.ends_with('\n'), "got: {only_tracked}");

    let text = pending_commit_diff(&snapshot, &[snapshot.ids.get_unstaged().to_string()]);
    assert!(text.contains("error:"), "got: {text}");
    assert!(text.contains("\nuntracked file: new.txt"), "got: {text}");
}

/// The placeholder commit shows what it will contain, not the row it sits on.
#[test]
fn pending_commit_diff_shows_the_files_it_will_hold() {
    let repo = crate::core::test_helpers::TestRepo::new();
    repo.write_file("file.txt", "original content\n");
    repo.stage_files(&["file.txt"]);
    repo.commit_staged("Add file");
    repo.write_file("file.txt", "changed\n");
    repo.write_file("new.txt", "fresh\n");

    repo.write_file("other.txt", "second\n");
    repo.stage_files(&["other.txt"]);
    repo.commit_staged("Add other");
    repo.write_file("other.txt", "second changed\n");

    let mut info = make_info();
    info.working_changes = vec![
        file("file.txt", ' ', 'M'),
        file("other.txt", ' ', 'M'),
        file("new.txt", '?', '?'),
    ];
    let snapshot = Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..snapshot_of(info)
    };
    let ids = &snapshot.ids;

    let one = pending_commit_diff(&snapshot, &[ids.get_file("file.txt").to_string()]);
    assert!(one.contains("+changed"));
    assert!(!one.contains("fresh"), "only the file it holds");
    assert!(!one.contains("second changed"), "only the file it holds");

    // Several tracked files go out as one `git diff`, and both must be in it.
    let two = pending_commit_diff(
        &snapshot,
        &[
            ids.get_file("file.txt").to_string(),
            ids.get_file("other.txt").to_string(),
        ],
    );
    assert!(two.contains("+changed"), "got: {two}");
    assert!(two.contains("+second changed"), "got: {two}");

    // `zz` commits through `git add -A`: the untracked file goes in, so the
    // preview has to show it next to the tracked change.
    let all = pending_commit_diff(&snapshot, &[ids.get_unstaged().to_string()]);
    assert!(all.contains("+changed"));
    assert!(all.contains("+fresh"), "untracked file missing from zz");

    let untracked = pending_commit_diff(&snapshot, &[ids.get_file("new.txt").to_string()]);
    assert!(untracked.contains("+fresh"), "untracked shown as added");

    // Nothing tracked to diff: the whole-tree diff is empty and only the
    // untracked file is left, which must not read as an empty pane.
    repo.write_file("file.txt", "original content\n");
    repo.write_file("other.txt", "second\n");
    let all = pending_commit_diff(&snapshot, &[ids.get_unstaged().to_string()]);
    assert_eq!(all.trim(), "untracked file: new.txt\n+fresh");

    // Nothing at all: never a blank pane.
    let mut info = make_info();
    info.working_changes.clear();
    let empty = Snapshot {
        workdir: repo.workdir(),
        git_dir: repo.repo.path().to_path_buf(),
        ..snapshot_of(info)
    };
    let text = pending_commit_diff(&empty, &[empty.ids.get_unstaged().to_string()]);
    assert_eq!(text, "no changes");
}

#[test]
fn untracked_file_text_renders_added_lines() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("new.txt"), "one\ntwo\n").unwrap();
    let text = untracked_file_text(dir.path(), "new.txt");
    assert!(text.starts_with("untracked file: new.txt\n"));
    assert!(text.contains("+one\n"));
    assert!(text.contains("+two\n"));
    assert!(!text.contains("..."));
}

#[test]
fn untracked_file_text_detects_binary() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("blob"), b"\x00\x01\x02").unwrap();
    assert_eq!(untracked_file_text(dir.path(), "blob"), "(binary file)");
}

#[test]
fn untracked_file_text_reports_unreadable_file() {
    let dir = tempfile::tempdir().unwrap();
    assert_eq!(untracked_file_text(dir.path(), "nope"), "(unreadable file)");
}

#[test]
fn untracked_file_text_truncates_oversized_files() {
    let dir = tempfile::tempdir().unwrap();
    let line = format!("{}\n", "x".repeat(1023)); // 1 KB per line
    std::fs::write(dir.path().join("big.txt"), line.repeat(300)).unwrap();
    let text = untracked_file_text(dir.path(), "big.txt");
    assert!(text.ends_with("...\n"));
    // Only the 256 KB prefix is rendered, not the whole 300 KB.
    assert!(text.len() <= 257 * 1024);
}

#[test]
fn untracked_file_text_caps_line_count() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("many.txt"), "a\n".repeat(3000)).unwrap();
    let text = untracked_file_text(dir.path(), "many.txt");
    assert!(text.ends_with("...\n"));
    assert_eq!(text.lines().filter(|l| l.starts_with('+')).count(), 2000);
}

#[test]
fn diff_line_styles_map_prefixes() {
    let theme = make_theme();
    assert_eq!(diff_line_style("+added", &theme), theme.added);
    assert_eq!(diff_line_style("-removed", &theme), theme.removed);
    assert_eq!(diff_line_style("@@ -1 +1 @@", &theme), theme.hunk_header);
    assert_eq!(diff_line_style(" context", &theme), theme.context);
    assert_ne!(diff_line_style("+++ b/file", &theme), theme.added);
    assert_ne!(diff_line_style("--- a/file", &theme), theme.removed);
}

#[test]
fn git_show_header_renders_in_normal_colors() {
    let theme = make_theme();
    let text = "commit aaaa\nAuthor: Someone\nDate: today\n\n    Add parser\n\n\
                 src/parser.rs | 2 +-\n\ndiff --git a/src/parser.rs b/src/parser.rs\n\
                 @@ -1 +1 @@\n-old\n+new\n    indented context\n";
    let lines = colorize_diff(text, &theme);
    let style_of = |needle: &str| {
        lines
            .iter()
            .find(|l| l.spans[0].content.contains(needle))
            .unwrap()
            .spans[0]
            .style
    };

    // Header: commit line dimmed, message and metadata in default colors.
    assert_eq!(style_of("commit aaaa"), theme.dim);
    assert_eq!(style_of("Add parser"), Style::default());
    assert_eq!(style_of("Author:"), Style::default());
    // Patch region still colorized, including indented context lines.
    assert_eq!(style_of("+new"), theme.added);
    assert_eq!(style_of("-old"), theme.removed);
    assert_eq!(style_of("indented context"), theme.context);
}

#[test]
fn plain_diff_text_has_no_header_region() {
    let theme = make_theme();
    let lines = colorize_diff("diff --git a/f b/f\n+new\n", &theme);
    assert_eq!(lines[1].spans[0].style, theme.added);
}

/// Shrinking the context (`-`) removes the row the cursor sits on; it must
/// land on the neighbour, not back at the top of the tree.
#[test]
fn cursor_lands_next_to_a_row_that_disappeared() {
    let theme = make_theme();
    let mut info = make_info();
    info.context_commits = vec![context_commit("1111111"), context_commit("2222222")];
    let mut app = make_app(snapshot_of(info), &theme);
    move_cursor_to(&mut app, "ctx:2222222");

    let mut shallower = make_info();
    shallower.context_commits = vec![context_commit("1111111")];
    app.apply_snapshot(snapshot_of(shallower));

    assert_eq!(cursor_key(&app), "ctx:1111111");
}

#[test]
fn shrinking_the_context_below_one_does_nothing() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let before = cursor_key(&app);

    app.change_context(-1);

    assert_eq!(app.context, 1);
    assert!(app.popup.is_none(), "no reload, so no failure popup");
    assert_eq!(cursor_key(&app), before);
}

/// Asking for more context than history holds must leave the depth on what
/// the tree shows, or `-` would take as many presses to change anything.
#[test]
fn growing_the_context_clamps_to_the_history_that_exists() {
    let test_repo = crate::core::test_helpers::TestRepo::new_with_remote();
    let theme = make_theme();

    test_repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);

        app.change_context(1);

        assert!(app.snapshot.info.context_commits.is_empty());
        assert_eq!(app.context, 1);
    });
}

fn context_messages(app: &App) -> Vec<String> {
    app.rows
        .iter()
        .filter_map(|row| match &row.kind {
            RowKind::Context { message, .. } => Some(message.clone()),
            _ => None,
        })
        .collect()
}

#[test]
fn context_keys_add_and_remove_one_commit_before_the_base() {
    let test_repo = crate::core::test_helpers::TestRepo::new_with_remote();
    let tip = test_repo.add_remote_commits(&["Older", "Newer"]);
    test_repo.fetch_remote();
    test_repo.reset_hard(tip);
    let theme = make_theme();

    test_repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        let press = |app: &mut App, key: char| {
            app.handle_key(PaneId::Left, KeyCode::Char(key), KeyModifiers::NONE);
        };
        assert!(context_messages(&app).is_empty());

        press(&mut app, '+');
        assert_eq!(app.context, 2);
        assert_eq!(context_messages(&app), ["Older"]);

        press(&mut app, '=');
        assert_eq!(app.context, 3);
        assert_eq!(context_messages(&app), ["Older", "Initial"]);

        press(&mut app, '-');
        assert_eq!(app.context, 2);
        assert_eq!(context_messages(&app), ["Older"]);
    });
}

/// A conflicted add is `!` on the index side and `?` on the worktree side
/// (`repo::gather`). The tree and the agent JSON both call it conflicted, and
/// the TUI row must agree rather than falling through to the plain branch.
#[test]
fn tui_row_marks_a_one_sided_conflict_as_conflicted() {
    let theme = make_theme();
    let line_text = |index: char, worktree: char| {
        let kind = RowKind::WorkingFile {
            path: "both_added.rs".to_string(),
            index,
            worktree,
        };
        row_line(
            &row(kind, "wf"),
            &theme,
            "",
            RowMark::None,
            None,
            false,
            None,
        )
        .spans
        .iter()
        .map(|s| s.content.as_ref())
        .collect::<String>()
    };

    assert!(
        line_text('!', '?').contains("!!"),
        "{}",
        line_text('!', '?')
    );
    // The plain cases keep their markers.
    assert!(line_text('!', '!').contains("!!"));
    assert!(line_text('?', '?').contains("⁕"));
    assert!(line_text('M', ' ').contains("M"));
}

/// Twenty lines, so a change to the first and one to the last are two hunks.
fn twenty_lines(first: &str, last: &str) -> String {
    let mut lines = vec![first.to_string()];
    lines.extend((2..20).map(|i| format!("line {i}")));
    lines.push(last.to_string());
    lines.join("\n") + "\n"
}

/// Run an `F` read to the selector, then answer as the selector would,
/// keeping the first hunk only.
fn pick_first_hunk(app: &mut App) {
    pick_first_hunk_after(app, || {});
}

/// `pick_first_hunk`, running `meanwhile` while the selector would be open.
fn pick_first_hunk_after(app: &mut App, meanwhile: impl FnOnce()) {
    poll_until_take_over(app);
    meanwhile();
    let Some(Pick::Ready {
        mut entries,
        stamp,
        origin,
        purpose:
            PickFor::Fold {
                sources,
                targets,
                commit,
            },
    }) = app.pick.take()
    else {
        panic!("no fold pick ready");
    };
    for (i, hunk) in entries[0].hunks.iter_mut().enumerate() {
        hunk.selected = i == 0;
    }
    app.finish_fold_pick(Ok(Some(entries)), stamp, sources, targets, commit, origin);
}

/// Walk a pending move to `slot`, confirm it, and run what it confirms.
fn run_move_to(app: &mut App, slot: MoveSlot) {
    while move_slot_of(app) != slot {
        let before = move_slot_of(app);
        press(app, KeyCode::Down);
        assert_ne!(move_slot_of(app), before, "{slot:?} is not offered");
    }
    let action = app.confirm_move_target().expect("a move");
    let command = app.command_line(&action);
    execute_action(
        action,
        &command,
        &app.snapshot.git_dir,
        &graph::Theme::dark(),
    )
    .unwrap();
}

#[test]
fn moving_a_commit_runs_fold_below() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    let c1 = repo.commit("one", "one.txt");
    repo.commit("two", "two.txt");
    let c3 = repo.commit("three", "three.txt");
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_start_on(&mut app, &c3.to_string());
        run_move_to(&mut app, MoveSlot::Below(c1));
    });

    let subjects: Vec<String> = (0..3).map(|i| repo.get_subject(i)).collect();
    assert_eq!(subjects, ["two", "one", "three"]);
}

#[test]
fn moving_a_commit_into_an_empty_branch_runs_fold_onto_it() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    let base = repo.head_oid();
    repo.commit("one", "one.txt");
    let c2 = repo.commit("two", "two.txt");
    repo.create_branch_at_commit("feature-x", base);
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_start_on(&mut app, &c2.to_string());
        while move_slot_of(&app) != MoveSlot::Branch("feature-x".to_string()) {
            let before = move_slot_of(&app);
            press(&mut app, KeyCode::Down);
            assert_ne!(move_slot_of(&app), before, "the branch is not offered");
        }
        let action = app.confirm_move_target().expect("a move");
        let command = app.command_line(&action);
        execute_action(
            action,
            &command,
            &app.snapshot.git_dir,
            &graph::Theme::dark(),
        )
        .unwrap();
    });

    let tip = repo.get_branch_target("feature-x");
    assert_eq!(repo.find_commit(tip).summary().unwrap(), Some("two"));
    assert_eq!(repo.find_commit(tip).parent_id(0).unwrap(), base);
}

#[test]
fn fold_with_hunks_amends_the_picked_working_tree_hunks() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    repo.write_file("f.txt", &twenty_lines("first", "last"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Add f");
    let target = repo.head_oid();
    repo.write_file("f.txt", &twenty_lines("FIRST", "LAST"));
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, "wf:f.txt");
        press(&mut app, KeyCode::Char('F'));
        pick_first_hunk(&mut app);

        assert_eq!(
            fold_targets_of(&app),
            vec![(target.to_string(), FoldEffect::Amend)]
        );
        assert_eq!(app.row_mark(&app.rows[1]), RowMark::Source);
        assert_eq!(repo.status_porcelain(), " M f.txt\n", "nothing staged yet");
        let Some(action @ Action::FoldHunks { .. }) = app.confirm_fold_target() else {
            panic!("expected a hunk fold");
        };
        let command = app.command_line(&action);
        execute_action(
            action,
            &command,
            &app.snapshot.git_dir,
            &graph::Theme::dark(),
        )
        .unwrap();
    });

    assert_eq!(repo.get_message(0), "Add f");
    let folded = repo.diff_commit("HEAD");
    assert!(
        folded.contains("+FIRST") && !folded.contains("+LAST"),
        "{folded}"
    );
    let left = repo.diff_head_name_only();
    assert_eq!(left.trim(), "f.txt");
    assert_eq!(repo.read_file("f.txt"), twenty_lines("FIRST", "LAST"));
}

#[test]
fn fold_with_hunks_uncommits_the_picked_commit_hunks() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    repo.write_file("f.txt", &twenty_lines("first", "last"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Add f");
    let older = repo.head_oid();
    repo.write_file("f.txt", &twenty_lines("FIRST", "LAST"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Shout");
    let source = repo.head_oid();
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, &source.to_string());
        press(&mut app, KeyCode::Char('F'));
        pick_first_hunk(&mut app);

        assert_eq!(
            fold_targets_of(&app),
            vec![
                (LOCAL_CHANGES_KEY.to_string(), FoldEffect::Uncommit),
                (source.to_string(), FoldEffect::Noop),
                (older.to_string(), FoldEffect::MoveFile),
            ]
        );
        while cursor_key(&app) != LOCAL_CHANGES_KEY {
            press(&mut app, KeyCode::Up);
        }
        let Some(action @ Action::Fold { hunks: Some(_), .. }) = app.confirm_fold_target() else {
            panic!("expected a hunk fold");
        };
        let command = app.command_line(&action);
        assert!(
            command.contains("--hunks 'f.txt:1' --hunks-from"),
            "{command}"
        );
        execute_action(
            action,
            &command,
            &app.snapshot.git_dir,
            &graph::Theme::dark(),
        )
        .unwrap();
    });

    assert_eq!(repo.get_message(0), "Shout");
    let kept = repo.diff_commit("HEAD");
    assert!(kept.contains("+LAST") && !kept.contains("+FIRST"), "{kept}");
    assert_eq!(repo.read_file("f.txt"), twenty_lines("FIRST", "LAST"));
    assert_eq!(repo.status_porcelain(), " M f.txt\n");
}

#[test]
fn fold_with_hunks_moves_the_picked_commit_hunks_into_an_older_commit() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    repo.write_file("f.txt", &twenty_lines("first", "last"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Add f");
    let older = repo.head_oid();
    repo.write_file("f.txt", &twenty_lines("FIRST", "LAST"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Shout");
    let source = repo.head_oid();
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, &source.to_string());
        press(&mut app, KeyCode::Char('F'));
        pick_first_hunk(&mut app);
        while cursor_key(&app) != older.to_string() {
            press(&mut app, KeyCode::Down);
        }
        let Some(action @ Action::Fold { hunks: Some(_), .. }) = app.confirm_fold_target() else {
            panic!("expected a hunk fold");
        };
        let command = app.command_line(&action);
        execute_action(
            action,
            &command,
            &app.snapshot.git_dir,
            &graph::Theme::dark(),
        )
        .unwrap();
    });

    assert_eq!(repo.commit_messages().len(), 2);
    assert_eq!(repo.get_message(0), "Shout");
    let kept = repo.diff_commit("HEAD");
    assert!(kept.contains("+LAST") && !kept.contains("+FIRST"), "{kept}");
    let moved = repo.diff_commit("HEAD~1");
    assert!(
        moved.contains("+FIRST") && moved.contains("+last"),
        "{moved}"
    );
    assert_eq!(repo.status_porcelain(), "");
}

#[test]
fn fold_with_hunks_on_a_file_picks_from_every_local_change() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    repo.write_file("a.txt", "a\n");
    repo.write_file("b.txt", "b\n");
    repo.stage_files(&["a.txt", "b.txt"]);
    repo.commit_staged("Add a and b");
    repo.write_file("a.txt", "A\n");
    repo.write_file("b.txt", "B\n");
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, "wf:a.txt");
        press(&mut app, KeyCode::Char('F'));
        poll_until_take_over(&mut app);
        let Some(Pick::Ready {
            entries,
            purpose: PickFor::Fold { sources, .. },
            ..
        }) = &app.pick
        else {
            panic!("no fold pick ready");
        };
        let paths: Vec<&str> = entries.iter().map(|f| f.path.as_str()).collect();
        assert_eq!(paths, ["a.txt", "b.txt"]);
        assert_eq!(sources, &[app.snapshot.ids.get_unstaged().to_string()]);
    });
}

#[test]
fn fold_with_hunks_of_a_binary_only_commit_opens_the_selector() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    std::fs::write(repo.workdir().join("f.bin"), b"\0one").unwrap();
    repo.stage_files(&["f.bin"]);
    repo.commit_staged("Add f");
    let source = repo.head_oid();
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, &source.to_string());
        press(&mut app, KeyCode::Char('F'));
        poll_until_take_over(&mut app);
        let Some(Pick::Ready { entries, .. }) = &app.pick else {
            panic!("no fold pick ready");
        };
        let paths: Vec<&str> = entries.iter().map(|f| f.path.as_str()).collect();
        assert_eq!(paths, ["f.bin"]);
    });
}

#[test]
fn fold_with_hunks_marks_a_picked_binary_too() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    repo.write_file("f.txt", "text\n");
    std::fs::write(repo.workdir().join("f.bin"), b"\0one").unwrap();
    repo.stage_files(&["f.txt", "f.bin"]);
    repo.commit_staged("Add both");
    let source = repo.head_oid();
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, &source.to_string());
        press(&mut app, KeyCode::Char('F'));
        poll_until_take_over(&mut app);
        let Some(Pick::Ready {
            mut entries,
            stamp,
            origin,
            purpose:
                PickFor::Fold {
                    sources,
                    targets,
                    commit,
                },
        }) = app.pick.take()
        else {
            panic!("no fold pick ready");
        };
        for hunk in entries.iter_mut().flat_map(|f| &mut f.hunks) {
            hunk.selected = true;
        }
        app.finish_fold_pick(Ok(Some(entries)), stamp, sources, targets, commit, origin);

        let files = &app
            .snapshot
            .info
            .commits
            .iter()
            .find(|c| c.oid == source)
            .unwrap()
            .files;
        let text = files.iter().position(|f| f.path == "f.txt").unwrap();
        let binary = files.iter().position(|f| f.path == "f.bin").unwrap();
        let Mode::FoldTarget { source_rows, .. } = &app.mode else {
            panic!("not picking a fold target");
        };
        assert_eq!(
            source_rows.covered,
            HashSet::from([
                commit_file_key(source, text),
                commit_file_key(source, binary)
            ])
        );
    });
}

#[test]
fn fold_with_hunks_refuses_before_the_pick_when_nothing_takes_it() {
    let theme = make_theme();
    let info = RepoInfo {
        commits: Vec::new(),
        branches: Vec::new(),
        ..make_info()
    };
    let mut app = make_app(snapshot_of(info), &theme);
    move_cursor_to(&mut app, "wf:b.rs");
    press(&mut app, KeyCode::Char('F'));
    assert!(app.pick.is_none());
    assert_eq!(app.notice.as_deref(), Some("fold: nothing to fold into"));
}

/// `F` stages working-tree hunks only at confirm, so what changed while the
/// target was picked must refuse the fold, not slip into the commit — a
/// binary file included, which the listing shows only as a label.
#[test]
fn fold_with_hunks_refuses_a_pick_the_working_tree_moved_away_from() {
    let text = |last: &str| twenty_lines("FIRST", last).into_bytes();
    for (name, committed, picked, edited, in_selector) in [
        (
            "f.txt",
            Some(twenty_lines("first", "last").into_bytes()),
            text("LAST"),
            Some(text("LATER")),
            false,
        ),
        (
            "f.bin",
            Some(b"\0one".to_vec()),
            b"\0two".to_vec(),
            Some(b"\0three".to_vec()),
            false,
        ),
        (
            "f.bin",
            Some(b"\0one".to_vec()),
            b"\0two".to_vec(),
            Some(b"\0three".to_vec()),
            true,
        ),
        (
            "f.bin",
            Some(b"\0one".to_vec()),
            b"\0two".to_vec(),
            None,
            false,
        ),
        (
            "new.txt",
            None,
            b"one\n".to_vec(),
            Some(b"two\n".to_vec()),
            false,
        ),
        (
            "new.bin",
            None,
            b"\0one".to_vec(),
            Some(b"\0two".to_vec()),
            false,
        ),
    ] {
        let repo = crate::core::test_helpers::TestRepo::new_with_remote();
        let path = repo.workdir().join(name);
        repo.write_file("base.txt", "base\n");
        repo.stage_files(&["base.txt"]);
        if let Some(committed) = &committed {
            std::fs::write(&path, committed).unwrap();
            repo.stage_files(&[name]);
        }
        repo.commit_staged("Add f");
        let head = repo.head_oid();
        std::fs::write(&path, &picked).unwrap();
        let theme = make_theme();

        let result = repo.in_dir(|| {
            let mut app = make_app(load_snapshot(1).unwrap(), &theme);
            move_cursor_to(&mut app, &format!("wf:{name}"));
            press(&mut app, KeyCode::Char('F'));
            let edit = || {
                if let Some(edited) = &edited {
                    std::fs::write(&path, edited).unwrap();
                }
            };
            if in_selector {
                pick_first_hunk_after(&mut app, edit);
            } else {
                pick_first_hunk(&mut app);
                edit();
            }
            let action = app.confirm_fold_target().unwrap();
            let command = app.command_line(&action);
            execute_action(
                action,
                &command,
                &app.snapshot.git_dir,
                &graph::Theme::dark(),
            )
        });

        if edited.is_some() {
            let err = result.unwrap_err().to_string();
            assert!(
                err.contains("moved since the hunks were picked"),
                "{name}: {err}"
            );
            assert_eq!(repo.head_oid(), head, "{name}");
            let status = if committed.is_some() { " M" } else { "??" };
            assert_eq!(
                repo.status_porcelain(),
                format!("{status} {name}\n"),
                "{name}"
            );
        } else {
            result.unwrap();
            assert_eq!(repo.status_porcelain(), "", "{name}");
        }
    }
}

#[test]
fn a_fold_pick_that_kept_nothing_ends_with_a_notice() {
    let repo = repo_with_one_unstaged_hunk();
    let theme = make_theme();
    let mut app = app_on(&repo, &theme);
    let entries = picked_entries(&repo, false);

    app.finish_fold_pick(
        Ok(Some(entries)),
        Vec::new(),
        vec!["tr".to_string()],
        Vec::new(),
        None,
        "wf:tracked.txt".to_string(),
    );
    assert!(matches!(app.mode, Mode::Normal));
    assert_eq!(app.notice.as_deref(), Some("fold cancelled"));
}

#[test]
fn fold_with_hunks_refuses_what_it_cannot_pick_from() {
    let theme = make_theme();
    let mut info = two_branch_info();
    info.working_changes.clear();
    let a = oid('a').to_string();
    let b = oid('b').to_string();
    for (keys, expected) in [
        (
            vec!["br:feature-a"],
            "fold: move to a file, local changes, or a commit",
        ),
        (vec![b.as_str(), a.as_str()], "fold: one commit at a time"),
        (vec![LOCAL_CHANGES_KEY], "fold: no local changes"),
    ] {
        let mut app = make_app(snapshot_of(info.clone()), &theme);
        for key in &keys {
            move_cursor_to(&mut app, key);
            app.toggle_selection();
        }
        if keys == [LOCAL_CHANGES_KEY] {
            app.clear_selection();
            move_cursor_to(&mut app, LOCAL_CHANGES_KEY);
        }
        press(&mut app, KeyCode::Char('F'));
        assert!(app.pick.is_none(), "{keys:?}");
        assert_eq!(app.notice.as_deref(), Some(expected), "{keys:?}");
    }
}

/// `a`: one commit touching `src/lexer.rs` and `src/parser.rs`.
fn two_file_commit_info() -> RepoInfo {
    let mut info = make_info();
    info.commits[0].files = vec![
        file("src/lexer.rs", 'M', ' '),
        file("src/parser.rs", 'M', ' '),
    ];
    info
}

fn run_action(app: &App, action: Action) -> Result<()> {
    let command = app.command_line(&action);
    execute_action(
        action,
        &command,
        &app.snapshot.git_dir,
        &graph::Theme::dark(),
    )
}

#[test]
fn split_on_a_commit_file_takes_that_file_only() {
    let theme = make_theme();
    let snapshot = Snapshot {
        cwd_prefix: "src".to_string(),
        ..snapshot_of(two_file_commit_info())
    };
    let mut app = make_app(snapshot, &theme);
    move_cursor_to(&mut app, &oid('a').to_string());
    app.expand_current();
    move_cursor_to(&mut app, &commit_file_key(oid('a'), 1));

    assert_eq!(
        app.action_split(),
        Some(Action::Split {
            commit: oid('a').to_string(),
            files: vec!["parser.rs".to_string()],
            hunks: None,
        })
    );
}

#[test]
fn split_on_a_commit_row_picks_its_hunks() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    let source = repo.commit("one", "one.txt");
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, &source.to_string());
        press(&mut app, KeyCode::Char('s'));
        poll_until_take_over(&mut app);
        let Some(Pick::Ready {
            entries,
            purpose: PickFor::Split { commit },
            ..
        }) = &app.pick
        else {
            panic!("no split pick ready");
        };
        assert_eq!(*commit, source);
        let paths: Vec<&str> = entries.iter().map(|f| f.path.as_str()).collect();
        assert_eq!(paths, ["one.txt"]);
    });
}

#[test]
fn split_refuses_what_it_cannot_split() {
    let theme = make_theme();
    let a = oid('a').to_string();
    let b = oid('b').to_string();
    let lexer = commit_file_key(oid('a'), 0);
    let parser = commit_file_key(oid('a'), 1);
    let mut two_commits = two_branch_info();
    two_commits.commits[1].files = two_file_commit_info().commits[0].files.clone();
    for (info, keys, expected) in [
        (
            make_info(),
            vec![lexer.as_str()],
            "split: one file only, split its hunks with S",
        ),
        (
            two_file_commit_info(),
            vec![lexer.as_str(), parser.as_str()],
            "split: leave at least one file for the second commit",
        ),
        (
            two_file_commit_info(),
            vec!["br:feature-a"],
            "split: move to a commit or commit file",
        ),
        (
            two_file_commit_info(),
            vec!["wf:a.rs"],
            "split: move to a commit or commit file",
        ),
        (
            two_commits,
            vec![b.as_str(), a.as_str()],
            "split: one commit at a time",
        ),
    ] {
        let mut app = make_app(snapshot_of(info), &theme);
        move_cursor_to(&mut app, &a);
        app.expand_current();
        for key in &keys {
            move_cursor_to(&mut app, key);
            app.toggle_selection();
        }
        // One row goes through the cursor, not the selection.
        if keys.len() == 1 {
            app.clear_selection();
            move_cursor_to(&mut app, keys[0]);
        }
        assert_eq!(app.action_split(), None, "{keys:?}");
        assert_eq!(app.notice.as_deref(), Some(expected), "{keys:?}");
    }
}

#[test]
fn split_takes_the_selected_commit_files_out_first() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    for name in ["a.txt", "b.txt", "c.txt"] {
        repo.write_file(name, "x\n");
    }
    repo.stage_files(&["a.txt", "b.txt", "c.txt"]);
    repo.commit_staged("Add three");
    let source = repo.head_oid();
    repo.set_fake_editor("Add a and b");
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, &source.to_string());
        app.expand_current();
        for i in [0, 1] {
            move_cursor_to(&mut app, &commit_file_key(source, i));
            app.toggle_selection();
        }
        let action = app.action_split().expect("a split");
        run_action(&app, action).unwrap();
    });

    assert_eq!(repo.get_subject(0), "Add three");
    assert_eq!(repo.get_subject(1), "Add a and b");
    let first = repo.diff_commit("HEAD~1");
    assert!(
        first.contains("a.txt") && first.contains("b.txt") && !first.contains("c.txt"),
        "{first}"
    );
    assert_eq!(repo.status_porcelain(), "");
}

#[test]
fn split_with_hunks_puts_the_picked_hunks_first() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    repo.write_file("f.txt", &twenty_lines("first", "last"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Add f");
    repo.write_file("f.txt", &twenty_lines("FIRST", "LAST"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Shout");
    let source = repo.head_oid();
    repo.set_fake_editor("Shout first");
    let theme = make_theme();

    repo.in_dir(|| {
        let mut app = make_app(load_snapshot(1).unwrap(), &theme);
        move_cursor_to(&mut app, &source.to_string());
        press(&mut app, KeyCode::Char('S'));
        poll_until_take_over(&mut app);
        let Some(Pick::Ready {
            mut entries,
            purpose: PickFor::Split { commit },
            ..
        }) = app.pick.take()
        else {
            panic!("no split pick ready");
        };
        for (i, hunk) in entries[0].hunks.iter_mut().enumerate() {
            hunk.selected = i == 0;
        }
        let action = app
            .finish_split_pick(Ok(Some(entries)), commit)
            .expect("a split");
        let command = app.command_line(&action);
        assert!(
            command.contains("split -p") && command.contains("--hunks 'f.txt:1' --hunks-from"),
            "{command}"
        );
        run_action(&app, action).unwrap();
    });

    assert_eq!(repo.get_subject(0), "Shout");
    assert_eq!(repo.get_subject(1), "Shout first");
    let first = repo.diff_commit("HEAD~1");
    assert!(
        first.contains("+FIRST") && !first.contains("+LAST"),
        "{first}"
    );
    let second = repo.diff_commit("HEAD");
    assert!(
        second.contains("+LAST") && !second.contains("+FIRST"),
        "{second}"
    );
    assert_eq!(repo.status_porcelain(), "");
}

#[test]
fn a_split_pick_must_leave_something_on_each_side() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    repo.write_file("f.txt", &twenty_lines("first", "last"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Add f");
    repo.write_file("f.txt", &twenty_lines("FIRST", "LAST"));
    repo.stage_files(&["f.txt"]);
    repo.commit_staged("Shout");
    let source = repo.head_oid();
    let theme = make_theme();
    let mut app = app_over_repo(&repo, &theme);
    for (keep, expected) in [
        (false, "split cancelled"),
        (true, "split: leave at least one hunk for the second commit"),
    ] {
        let mut entries =
            staging::collect_commit_hunks(&repo.workdir(), &source.to_string(), &[]).unwrap();
        for hunk in entries.iter_mut().flat_map(|f| &mut f.hunks) {
            hunk.selected = keep;
        }
        assert!(!app.refuse_split_pick(&entries));
        assert_eq!(
            app.finish_split_pick(Ok(Some(entries)), source),
            None,
            "{keep}"
        );
        assert_eq!(app.notice.as_deref(), Some(expected), "{keep}");
    }
}

#[test]
fn a_split_pick_with_fewer_than_two_hunks_never_opens() {
    let repo = crate::core::test_helpers::TestRepo::new_with_remote();
    let one = repo.commit("one", "one.txt");
    let theme = make_theme();
    let mut app = app_over_repo(&repo, &theme);

    let entries = staging::collect_commit_hunks(&repo.workdir(), &one.to_string(), &[]).unwrap();
    assert!(app.refuse_split_pick(&entries));
    assert_eq!(
        app.notice.as_deref(),
        Some("split: one hunk only, nothing to split")
    );
    assert!(app.refuse_split_pick(&[]));
    assert_eq!(app.notice.as_deref(), Some("split: no hunks to pick"));
}

#[test]
fn absorb_takes_the_picked_files_else_every_change() {
    let theme = make_theme();
    let mut app = make_app(make_snapshot(), &theme);
    let a_rs = app.snapshot.ids.get_file("a.rs").to_string();

    move_cursor_to(&mut app, "wf:a.rs");
    assert_eq!(
        app.action_absorb(),
        Some(Action::Absorb {
            files: vec![a_rs.clone()]
        })
    );
    assert_eq!(
        app.command_line(&Action::Absorb {
            files: vec![a_rs.clone()]
        }),
        format!("loom absorb {a_rs}")
    );

    for key in [LOCAL_CHANGES_KEY.to_string(), oid('a').to_string()] {
        move_cursor_to(&mut app, &key);
        assert_eq!(
            app.action_absorb(),
            Some(Action::Absorb { files: vec![] }),
            "{key}"
        );
    }

    app.selected.insert(oid('a').to_string());
    assert!(app.action_absorb().is_none());
    assert_eq!(
        app.notice.as_deref(),
        Some("absorb: select files or local changes")
    );
}

#[test]
fn absorb_refuses_an_empty_working_tree() {
    let theme = make_theme();
    let mut snapshot = make_snapshot();
    snapshot.info.working_changes.clear();
    let mut app = make_app(snapshot, &theme);

    assert!(app.action_absorb().is_none());
    assert_eq!(app.notice.as_deref(), Some("absorb: no local changes"));
}