omnidiff 0.2.0

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

use crate::test;
use anyhow::Result;

use super::plain_text_diff::*;
use super::render_options::*;
use super::summary::*;
use super::*;

fn text_range_on(row: usize, start: usize, end: usize) -> TextRange {
    TextRange::new(row, start, row, end)
}

fn changed(row: usize, start: usize, end: usize) -> RangeMatch {
    RangeMatch {
        source: text_range_on(row, start, end),
        destination: text_range_on(row, start, end),
        operation: TextOperation::Update,
    }
}

/// The corpus was painted narrow under both presets, so `whole_pair_updates` is off in both.
#[test]
fn neither_preset_turns_on_whole_pair_updates() {
    const { assert!(!RenderOptions::MINIMAL.whole_pair_updates) };
    const { assert!(!RenderOptions::FULL.whole_pair_updates) };
}

/// `rust-next-font-imports-generator`'s `Minimal` and `Full` paintings disagree on this.
#[test]
fn minimal_and_full_disagree_on_paint_reindent_only_moves() {
    const { assert!(!RenderOptions::MINIMAL.paint_reindent_only_moves) };
    const { assert!(RenderOptions::FULL.paint_reindent_only_moves) };
}

/// A `NestedConditionCollapse`-tagged body paints `Move` only under `paint_reindent_only_moves`.
#[test]
fn paint_reindent_only_moves_gates_only_tagged_nodes() {
    let body = "\x20               step_one();\n\
                 \x20               step_two();\n\
                 \x20               step_three();\n\
                 \x20               step_four();\n\
                 \x20               step_five();\n\
                 \x20               step_six();\n";
    let before = Code::from_string(
        &format!(
            "fn f() {{\n\
             \x20   if let A(a) = x {{\n\
             \x20       if let B(b) = y {{\n\
             {body}\
             \x20       }}\n\
             \x20   }}\n\
             }}\n"
        ),
        &crate::code::Language::Rust,
    );
    let after = Code::from_string(
        &format!(
            "fn f() {{\n\
             \x20   if let A(a) = x\n\
             \x20       && let B(b) = y\n\
             \x20   {{\n\
             {body}\
             \x20   }}\n\
             }}\n"
        ),
        &crate::code::Language::Rust,
    );
    let ast = crate::diff::diff_code(&before, &after);
    let node_cache = crate::diff::NodeCache::build(&before, &after);

    let painted = TextDiff::from_with_options(
        &before,
        &after,
        ast.ast.as_ref().unwrap(),
        &node_cache,
        RenderOptions::FULL,
    );
    assert!(
        painted
            .all(0)
            .iter()
            .any(|r| r.operation == TextOperation::Move
                && r.source.start_row >= 2
                && r.source.start_row <= 8),
        "paint_reindent_only_moves: true must still paint the reindented body Move"
    );

    let unpainted = TextDiff::from_with_options(
        &before,
        &after,
        ast.ast.as_ref().unwrap(),
        &node_cache,
        RenderOptions::MINIMAL,
    );
    assert!(
        !unpainted
            .all(0)
            .iter()
            .any(|r| r.operation == TextOperation::Move
                && r.source.start_row >= 2
                && r.source.start_row <= 8),
        "paint_reindent_only_moves: false must leave the tagged reindented body unpainted"
    );
}

/// `MINIMAL` suppresses a displacement-only `Move`; `FULL` keeps it.
#[test]
fn minimal_and_full_disagree_on_paint_displaced_moves() {
    const { assert!(!RenderOptions::MINIMAL.paint_displaced_moves) };
    const { assert!(RenderOptions::FULL.paint_displaced_moves) };
}

/// `shellscript-ansible-ansible-a-small-add`'s shape: a single-row node keeps its text and its
/// place beside the one edit on its row, but a line inserted above changes its row index. The
/// `FULL` half pins that this stays a preset split, not a blanket suppression.
#[test]
fn paint_displaced_moves_gates_a_node_pushed_down_by_an_insertion_above_it() {
    let before = Code::from_string(
        "fn main() {\n         \x20   let total = compute(first, second);\n         }\n",
        &crate::code::Language::Rust,
    );
    let after = Code::from_string(
        "fn main() {\n         \x20   setup();\n         \x20   let total = compute(extra, first, second);\n         }\n",
        &crate::code::Language::Rust,
    );
    let ast = crate::diff::diff_code(&before, &after);
    let node_cache = crate::diff::NodeCache::build(&before, &after);

    // The call row's unchanged tail, pushed down by `setup();` and right by `extra, `. Asserted as
    // "is there an `Identical` range there" because under `FULL` it is swallowed into a larger
    // multi-row `Move`, not a range of its own.
    let identical_tail_on_the_call_row = |options| {
        TextDiff::from_with_options(
            &before,
            &after,
            ast.ast.as_ref().unwrap(),
            &node_cache,
            options,
        )
        .all(0)
        .iter()
        .any(|r| {
            r.operation == TextOperation::Identical
                && r.source.start_row == 1
                && r.source.start_column == 33
        })
    };

    assert!(
        !identical_tail_on_the_call_row(RenderOptions::FULL),
        "paint_displaced_moves: true must keep painting the displaced tail Move"
    );
    assert!(
        identical_tail_on_the_call_row(RenderOptions::MINIMAL),
        "paint_displaced_moves: false must leave the purely displaced tail unpainted"
    );
}

/// The multi-row half (`rust-adding-a-variable-and-test-with-comments`): a closure whose first row
/// carries the one edit, pushed down by a line inserted above, other rows unchanged. `FULL` still
/// paints it.
#[test]
fn paint_displaced_moves_gates_a_multi_row_node_edited_on_its_first_row() {
    let before = Code::from_string(
        "fn main() {\n    let x = values.filter(row_len).map(|value| {\n        println!(\"{value}\");\n    });\n}\n",
        &crate::code::Language::Rust,
    );
    let after = Code::from_string(
        "fn main() {\n    setup();\n    let x = values.filter(paint_row_len).map(|value| {\n        println!(\"{value}\");\n    });\n}\n",
        &crate::code::Language::Rust,
    );
    let ast = crate::diff::diff_code(&before, &after);
    let node_cache = crate::diff::NodeCache::build(&before, &after);

    let multi_row_move_on_the_closure_row = |options| {
        TextDiff::from_with_options(
            &before,
            &after,
            ast.ast.as_ref().unwrap(),
            &node_cache,
            options,
        )
        .all(0)
        .iter()
        .any(|r| {
            r.operation == TextOperation::Move
                && r.source.start_row == 1
                && r.source.end_row > r.source.start_row
        })
    };

    assert!(
        multi_row_move_on_the_closure_row(RenderOptions::FULL),
        "paint_displaced_moves: true must keep painting the displaced closure Move"
    );
    assert!(
        !multi_row_move_on_the_closure_row(RenderOptions::MINIMAL),
        "paint_displaced_moves: false must leave the displaced closure unpainted"
    );
}

/// Two walks naming one relocation over extents a column apart agree; neither claim is withdrawn
/// under `FULL`, while `MINIMAL` keeps only the more economical account.
#[test]
fn reconcile_moves_keeps_two_overlapping_accounts_of_one_relocation() {
    let range_match = |source: TextRange, destination: TextRange, operation| RangeMatch {
        source,
        destination,
        operation,
    };
    // The before walk calls rows 3..9 moved; the after walk names the same relocation from one
    // column further in, which no exact-extent lookup can match.
    let mut before = [range_match(
        TextRange::new(3, 4, 9, 0),
        TextRange::new(3, 3, 9, 0),
        TextOperation::Move,
    )];
    let mut after = [range_match(
        TextRange::new(3, 2, 9, 0),
        TextRange::new(3, 5, 9, 0),
        TextOperation::Move,
    )];

    let mut minimal = (before.clone(), after.clone());
    reconcile_moves(&mut minimal.0, &mut minimal.1, RenderOptions::MINIMAL);
    reconcile_moves(&mut before, &mut after, RenderOptions::FULL);

    assert_eq!(
        (before[0].operation.clone(), after[0].operation.clone()),
        (TextOperation::Move, TextOperation::Move),
        "an overlapping account of the same relocation is agreement, not a disagreement to \
         resolve by withdrawing one side"
    );
    assert_eq!(
        (
            minimal.0[0].operation.clone(),
            minimal.1[0].operation.clone()
        ),
        (TextOperation::Move, TextOperation::Identical),
        "MINIMAL keeps the more economical single account - mirroring the relocation onto the \
         second panel doubles the bytes it paints"
    );
}

fn one_row(row: usize) -> TextRange {
    TextRange::new(row, 0, row + 1, 0)
}

fn row_pair(source_row: usize, destination_row: usize, operation: TextOperation) -> RangeMatch {
    RangeMatch {
        source: one_row(source_row),
        destination: one_row(destination_row),
        operation,
    }
}

/// One line moved below five: the before walk blames the five, the after walk the one. The
/// smaller account wins on both sides, and the winner's counterpart is promoted to `Move`.
#[test]
fn reconcile_moves_believes_the_side_that_blames_fewer_rows() {
    let mut before = vec![row_pair(0, 5, TextOperation::Identical)];
    before.extend((1..6).map(|row| row_pair(row, row - 1, TextOperation::Move)));
    let mut after = (0..5)
        .map(|row| row_pair(row, row + 1, TextOperation::Identical))
        .collect::<Vec<_>>();
    after.push(row_pair(5, 0, TextOperation::Move));

    reconcile_moves(&mut before, &mut after, RenderOptions::FULL);

    let moved_rows = |ranges: &[RangeMatch]| {
        ranges
            .iter()
            .filter(|r| r.operation == TextOperation::Move)
            .map(|r| r.source.start_row)
            .collect::<Vec<_>>()
    };
    assert_eq!(moved_rows(&before), vec![0]);
    assert_eq!(moved_rows(&after), vec![5]);
}

/// Two accounts of equal size: the before side's wins, so the result is a function of the input.
#[test]
fn reconcile_moves_breaks_a_tie_in_favour_of_the_before_side() {
    let mut before = vec![
        row_pair(0, 1, TextOperation::Move),
        row_pair(1, 0, TextOperation::Identical),
    ];
    let mut after = vec![
        row_pair(0, 1, TextOperation::Move),
        row_pair(1, 0, TextOperation::Identical),
    ];

    reconcile_moves(&mut before, &mut after, RenderOptions::FULL);

    let operations = |ranges: &[RangeMatch]| {
        ranges
            .iter()
            .map(|r| r.operation.clone())
            .collect::<Vec<_>>()
    };
    assert_eq!(
        operations(&before),
        vec![TextOperation::Move, TextOperation::Identical]
    );
    assert_eq!(
        operations(&after),
        vec![TextOperation::Identical, TextOperation::Move]
    );
}

/// `paint_resized_moves` is the third axis the two presets disagree on.
#[test]
fn minimal_and_full_disagree_on_paint_resized_moves() {
    const { assert!(!RenderOptions::MINIMAL.paint_resized_moves) };
    const { assert!(RenderOptions::FULL.paint_resized_moves) };
}

/// The first two options are post-filters and never need a rebuild; every other one does.
#[test]
fn only_the_post_filters_skip_a_rebuild() {
    let base = RenderOptions::FULL;
    for index in 0..base.options().len() {
        let mut toggled = base;
        toggled.toggle(index);
        assert_eq!(
            toggled.needs_rebuild_from(&base),
            index >= 2,
            "option {index} ({})",
            base.options()[index].0
        );
    }
    assert!(!base.needs_rebuild_from(&base));
}

/// `whole_identifier_updates` is the fourth, and ground-truth invariant 16 is why.
#[test]
fn minimal_and_full_disagree_on_whole_identifier_updates() {
    const { assert!(!RenderOptions::MINIMAL.whole_identifier_updates) };
    const { assert!(RenderOptions::FULL.whole_identifier_updates) };
}

/// The lone brackets and separators the painted corpus's `Minimal` drops.
#[test]
fn minimal_drops_the_punctuation_the_painted_corpus_drops() {
    for text in ["(", ")", "):", ";", "{", "}", "[]", "  ", " ,\n", "( )"] {
        assert!(
            is_structural_only(text),
            "{text:?} should be structural-only"
        );
    }
}

/// Operators look like punctuation and are the entire content of the change they appear in.
/// Dropping them would report a different diff, not a tighter one.
#[test]
fn minimal_keeps_operators_and_anything_carrying_meaning() {
    for text in ["+", "=", "<=", "&&", "=>", "foo", "1", "_", "a,", "->"] {
        assert!(!is_structural_only(text), "{text:?} should survive Minimal");
    }
}

/// A zero-width range marks where the other side gained or lost text - the only mark that side
/// has. Treating "no characters" as "only structural characters" would erase it.
#[test]
fn an_empty_range_is_not_structural() {
    assert!(!is_structural_only(""));
}

#[test]
fn full_returns_every_range_unchanged() {
    let source = "foo(bar);\n";
    let ranges = vec![changed(0, 3, 4), changed(0, 4, 7)];

    assert_eq!(
        ranges_for_options(&ranges, source, RenderOptions::FULL),
        ranges,
        "Full is exactly TextDiff::all"
    );
}

#[test]
fn minimal_drops_a_standalone_bracket_but_keeps_its_neighbour() {
    let source = "foo(bar);\n";
    // `(` alone, then `bar`.
    let ranges = vec![changed(0, 3, 4), changed(0, 4, 7)];

    let minimal = ranges_for_options(&ranges, source, RenderOptions::MINIMAL);

    assert_eq!(minimal.len(), 1, "got {minimal:?}");
    assert_eq!(minimal[0].source, text_range_on(0, 4, 7));
}

/// A bracket *inside* a larger range is not a standalone bracket - only whole ranges are
/// dropped, never parts of one, so a surviving range is byte-for-byte what `Full` shows.
#[test]
fn minimal_keeps_a_range_that_merely_contains_punctuation() {
    let source = "foo(bar);\n";
    let ranges = vec![changed(0, 0, 9)];

    assert_eq!(
        ranges_for_options(&ranges, source, RenderOptions::MINIMAL),
        ranges
    );
}

/// `Identical` ranges are the unpainted background, so dropping them changes nothing on
/// screen - but `line_operations` reads them to colour rows, and the two modes stay
/// comparable only if their range lists differ by content ranges alone.
#[test]
fn minimal_keeps_identical_ranges_even_when_they_are_pure_punctuation() {
    let source = "foo(bar);\n";
    let identical = RangeMatch {
        source: text_range_on(0, 3, 4),
        destination: text_range_on(0, 3, 4),
        operation: TextOperation::Identical,
    };

    assert_eq!(
        ranges_for_options(
            std::slice::from_ref(&identical),
            source,
            RenderOptions::MINIMAL
        ),
        vec![identical]
    );
}

/// Painting by hand, nobody marks the indentation in front of a change or the blank running
/// off the end of the line - a highlight that includes them reads as though the blank space
/// were part of the edit.
#[test]
fn minimal_trims_leading_and_trailing_whitespace_off_a_range() {
    let source = "    foo   \n";
    let ranges = vec![changed(0, 0, 10)];

    let minimal = ranges_for_options(&ranges, source, RenderOptions::MINIMAL);

    assert_eq!(minimal.len(), 1);
    assert_eq!(
        minimal[0].source,
        text_range_on(0, 4, 7),
        "the range should cover exactly `foo`"
    );
}

/// Interior whitespace sits *between* two things the range is genuinely about; cutting there
/// would report one edit as two.
#[test]
fn minimal_keeps_whitespace_inside_a_range() {
    let source = "a   b\n";
    let ranges = vec![changed(0, 0, 5)];

    assert_eq!(
        ranges_for_options(&ranges, source, RenderOptions::MINIMAL)[0].source,
        text_range_on(0, 0, 5)
    );
}

/// Trailing whitespace is not an option: even `FULL` trims it.
#[test]
fn full_keeps_leading_but_still_trims_trailing_whitespace() {
    let source = "    foo   \n";
    let ranges = vec![changed(0, 0, 10)];

    let full = ranges_for_options(&ranges, source, RenderOptions::FULL);

    assert_eq!(full.len(), 1);
    assert_eq!(
        full[0].source,
        text_range_on(0, 0, 7),
        "leading spaces survive, trailing ones do not"
    );
}

/// A multi-line block inserted on its own: with `leading_whitespace` off each row is highlighted
/// without its indentation, including interior rows that `FULL` keeps whole.
#[test]
fn leading_whitespace_off_splits_a_multiline_insert_per_row_trimmed() {
    let source = "    def added_function():\n        print(\"added\")\n";
    let range = RangeMatch {
        source: TextRange::new(0, 4, 1, 22),
        destination: TextRange::zero(),
        operation: TextOperation::Insert,
    };
    let options = RenderOptions {
        paint_reindent_only_moves: true,
        ..RenderOptions::MINIMAL
    };

    let result = ranges_for_options(std::slice::from_ref(&range), source, options);

    assert_eq!(
        result,
        vec![
            RangeMatch {
                source: TextRange::new(0, 4, 0, 25),
                destination: TextRange::zero(),
                operation: TextOperation::Insert,
            },
            RangeMatch {
                source: TextRange::new(1, 8, 1, 22),
                destination: TextRange::zero(),
                operation: TextOperation::Insert,
            },
        ],
        "row 0 keeps its own real content ('def added_function():'), row 1's own indentation \
         is trimmed independently rather than kept whole"
    );
}

/// A blank row in the middle of a multi-line insert has no real content of its own to select,
/// so it contributes no piece at all - not an empty one.
#[test]
fn leading_whitespace_off_drops_a_blank_interior_row() {
    let source = "line one\n\nline three\n";
    let range = RangeMatch {
        source: TextRange::new(0, 0, 2, 10),
        destination: TextRange::zero(),
        operation: TextOperation::Insert,
    };
    let options = RenderOptions {
        paint_reindent_only_moves: true,
        ..RenderOptions::MINIMAL
    };

    let result = ranges_for_options(std::slice::from_ref(&range), source, options);

    assert_eq!(
        result
            .iter()
            .map(|r| r.source.start_row)
            .collect::<Vec<_>>(),
        vec![0, 2],
        "the blank row 1 must contribute nothing: {result:?}"
    );
}

/// The per-row split applies only to `Insert`/`Delete`: a multi-row `Update` stays one range.
#[test]
fn leading_whitespace_off_does_not_split_a_multiline_update() {
    let source = "one\ntwo\nthree\n";
    let range = RangeMatch {
        source: TextRange::new(0, 0, 2, 5),
        destination: TextRange::new(0, 0, 2, 5),
        operation: TextOperation::Update,
    };
    let options = RenderOptions {
        paint_reindent_only_moves: true,
        ..RenderOptions::MINIMAL
    };

    let result = ranges_for_options(std::slice::from_ref(&range), source, options);

    assert_eq!(
        result.len(),
        1,
        "an Update range must not be split: {result:?}"
    );
}

/// The structural-punctuation filter still applies per row after the split - a row that is
/// nothing but a standalone bracket is dropped under `MINIMAL`'s `structural_punctuation: false`
/// exactly as a single-row range in the same shape already is.
#[test]
fn leading_whitespace_off_still_drops_a_structural_only_row() {
    let source = "{\n    body();\n}\n";
    let range = RangeMatch {
        source: TextRange::new(0, 0, 2, 1),
        destination: TextRange::zero(),
        operation: TextOperation::Insert,
    };
    let options = RenderOptions {
        paint_reindent_only_moves: true,
        ..RenderOptions::MINIMAL
    };

    let result = ranges_for_options(std::slice::from_ref(&range), source, options);

    assert_eq!(
        result
            .iter()
            .map(|r| r.source.start_row)
            .collect::<Vec<_>>(),
        vec![1],
        "rows 0 ('{{') and 2 ('}}') are standalone punctuation and must be dropped: {result:?}"
    );
}

/// `MINIMAL`'s standalone-punctuation behavior is independent of leading-whitespace: a range
/// that survives (real content, not pure punctuation) still gets its leading whitespace kept
/// when only `structural_punctuation` is off.
#[test]
fn structural_punctuation_off_alone_still_keeps_leading_whitespace() {
    let source = "    foo   \n";
    let ranges = vec![changed(0, 0, 10)];

    let options = RenderOptions {
        leading_whitespace: true,
        structural_punctuation: false,
        whole_pair_updates: false,
        paint_reindent_only_moves: true,
        paint_displaced_moves: true,
        paint_resized_moves: true,
        whole_identifier_updates: true,
    };
    let result = ranges_for_options(&ranges, source, options);

    assert_eq!(result.len(), 1);
    assert_eq!(result[0].source, text_range_on(0, 0, 7));
}

/// Symmetric case: `leading_whitespace` off with `structural_punctuation` on trims leading
/// whitespace but does not drop a range merely because part of it is punctuation - only a
/// range that is *nothing but* punctuation is ever dropped, and that's gated by the other
/// option entirely.
#[test]
fn leading_whitespace_off_alone_still_keeps_a_range_containing_punctuation() {
    let source = "    foo();   \n";
    let ranges = vec![changed(0, 0, 10)]; // "    foo();" - leading spaces, then `foo();`

    let options = RenderOptions {
        leading_whitespace: false,
        structural_punctuation: true,
        whole_pair_updates: false,
        paint_reindent_only_moves: true,
        paint_displaced_moves: true,
        paint_resized_moves: true,
        whole_identifier_updates: true,
    };
    let result = ranges_for_options(&ranges, source, options);

    assert_eq!(result.len(), 1);
    assert_eq!(
        result[0].source,
        text_range_on(0, 4, 10),
        "trims the leading spaces, keeps `foo();` whole - it isn't pure punctuation"
    );
}

/// `python-refactoring`, minimized: merging bundles `max_val = max(` into one `Insert` and leaves
/// `)` alone. The lone `)` is restored so a reader never sees `(` without it.
#[test]
fn a_lone_closing_paren_is_restored_when_its_open_partner_survives() {
    let source = "max_val = max(numbers)";
    let ranges = vec![
        RangeMatch {
            source: text_range_on(0, 0, 14), // "max_val = max("
            destination: TextRange::zero(),
            operation: TextOperation::Insert,
        },
        RangeMatch {
            source: text_range_on(0, 14, 21), // "numbers" - matched elsewhere, Identical
            destination: text_range_on(0, 14, 21),
            operation: TextOperation::Identical,
        },
        RangeMatch {
            source: text_range_on(0, 21, 22), // ")" - alone, purely structural
            destination: TextRange::zero(),
            operation: TextOperation::Insert,
        },
    ];

    let result = ranges_for_options(&ranges, source, RenderOptions::MINIMAL);

    assert!(
        result.iter().any(|r| r.source == text_range_on(0, 21, 22)),
        "the lone ')' must be restored: {result:?}"
    );
}

/// A pair whose halves are both standalone stays dropped.
#[test]
fn a_pair_that_is_entirely_standalone_punctuation_stays_dropped() {
    let source = "(x)";
    let ranges = vec![
        RangeMatch {
            source: text_range_on(0, 0, 1), // "(" alone
            destination: TextRange::zero(),
            operation: TextOperation::Insert,
        },
        RangeMatch {
            source: text_range_on(0, 1, 2), // "x" - matched elsewhere
            destination: text_range_on(0, 1, 2),
            operation: TextOperation::Identical,
        },
        RangeMatch {
            source: text_range_on(0, 2, 3), // ")" alone
            destination: TextRange::zero(),
            operation: TextOperation::Insert,
        },
    ];

    let result = ranges_for_options(&ranges, source, RenderOptions::MINIMAL);

    assert!(
        !result.iter().any(|r| r.source == text_range_on(0, 0, 1)),
        "neither half of a wholly standalone pair should be restored: {result:?}"
    );
    assert!(!result.iter().any(|r| r.source == text_range_on(0, 2, 3)));
}

/// Restoration is scoped to `Insert`/`Delete`: `javascript-refactor-arrow-func` paints a lone `");"`
/// `Move` that its ground truth never shows.
#[test]
fn a_lone_move_bracket_is_not_restored() {
    let source = "max_val = max(numbers)";
    let ranges = vec![
        RangeMatch {
            source: text_range_on(0, 0, 14),
            destination: TextRange::zero(),
            operation: TextOperation::Insert,
        },
        RangeMatch {
            source: text_range_on(0, 14, 21),
            destination: text_range_on(0, 14, 21),
            operation: TextOperation::Identical,
        },
        RangeMatch {
            source: text_range_on(0, 21, 22), // ")" alone, but Move this time
            destination: text_range_on(0, 21, 22),
            operation: TextOperation::Move,
        },
    ];

    let result = ranges_for_options(&ranges, source, RenderOptions::MINIMAL);

    assert!(
        !result.iter().any(|r| r.source == text_range_on(0, 21, 22)),
        "a lone Move bracket must not be restored: {result:?}"
    );
}

/// Trimming narrows the source only. The destination is a position in the *other* file, whose
/// text this side cannot see, and cross-panel navigation jumps to it.
#[test]
fn trimming_leaves_the_destination_alone() {
    let source = "  foo  \n";
    let ranges = vec![RangeMatch {
        source: text_range_on(0, 0, 7),
        destination: text_range_on(3, 0, 7),
        operation: TextOperation::Update,
    }];

    let minimal = ranges_for_options(&ranges, source, RenderOptions::MINIMAL);

    assert_eq!(minimal[0].source, text_range_on(0, 2, 5));
    assert_eq!(minimal[0].destination, text_range_on(3, 0, 7));
}

/// A multi-row range must not be judged by its first row alone: whitespace there says nothing
/// about the rows below it.
#[test]
fn a_multi_row_range_with_content_below_a_blank_first_row_survives() {
    let source = "   \n  keep\n";
    let ranges = vec![RangeMatch {
        source: TextRange::new(0, 0, 1, 6),
        destination: TextRange::new(0, 0, 1, 6),
        operation: TextOperation::Update,
    }];

    let minimal = ranges_for_options(&ranges, source, RenderOptions::MINIMAL);

    assert_eq!(minimal.len(), 1, "got {minimal:?}");
    assert_eq!(
        minimal[0].source,
        TextRange::new(1, 2, 1, 6),
        "and it should trim down to `keep`"
    );
}

/// A range that is nothing but blank rows is still dropped.
#[test]
fn a_multi_row_range_of_only_whitespace_is_dropped() {
    let source = "   \n   \n";
    let ranges = vec![RangeMatch {
        source: TextRange::new(0, 0, 1, 3),
        destination: TextRange::new(0, 0, 1, 3),
        operation: TextOperation::Update,
    }];

    assert!(ranges_for_options(&ranges, source, RenderOptions::MINIMAL).is_empty());
}

/// A range that doesn't read back is left alone. This is a display filter; deciding that
/// something it could not interpret is uninteresting is not its call to make.
#[test]
fn minimal_keeps_a_range_it_cannot_read() {
    let source = "short\n";
    let ranges = vec![changed(99, 0, 4)];

    assert_eq!(
        ranges_for_options(&ranges, source, RenderOptions::MINIMAL),
        ranges
    );
}

/// Every non-Identical range's source row span, in order - the shape of assertion these tests
/// care about (what changed, and in what order), without pinning down destination anchors
/// line by line.
fn changed_row_spans(ranges: &[RangeMatch]) -> Vec<(TextOperation, usize, usize)> {
    ranges
        .iter()
        .filter(|r| r.operation != TextOperation::Identical)
        .map(|r| (r.operation.clone(), r.source.start_row, r.source.end_row))
        .collect()
}

#[test]
fn plain_text_line_diff_matches_identical_lines() {
    let (before, after) = plain_text_line_diff("a\nb\nc\n", "a\nb\nc\n");
    assert!(changed_row_spans(&before).is_empty());
    assert!(changed_row_spans(&after).is_empty());
    assert_eq!(before.len(), 3, "every line should get an Identical range");
    assert!(
        before
            .iter()
            .all(|r| r.operation == TextOperation::Identical)
    );
}

#[test]
fn plain_text_line_diff_finds_a_pure_insertion() {
    let (before, after) = plain_text_line_diff("a\nc\n", "a\nb\nc\n");
    assert_eq!(changed_row_spans(&before), vec![]);
    assert_eq!(
        changed_row_spans(&after),
        vec![(TextOperation::Insert, 1, 2)]
    );
}

#[test]
fn plain_text_line_diff_finds_a_pure_deletion() {
    let (before, after) = plain_text_line_diff("a\nb\nc\n", "a\nc\n");
    assert_eq!(
        changed_row_spans(&before),
        vec![(TextOperation::Delete, 1, 2)]
    );
    assert_eq!(changed_row_spans(&after), vec![]);
}

/// Two lines sharing no affix are not a rewrite of each other: delete plus insert, as `diff -u`.
#[test]
fn plain_text_line_diff_treats_a_dissimilar_changed_line_as_delete_plus_insert() {
    let (before, after) = plain_text_line_diff("a\nOLD\nc\n", "a\nNEW\nc\n");
    assert_eq!(
        changed_row_spans(&before),
        vec![(TextOperation::Delete, 1, 2)]
    );
    assert_eq!(
        changed_row_spans(&after),
        vec![(TextOperation::Insert, 1, 2)]
    );
}

/// Every range on one row, as `(operation, start_column, end_column)` in byte columns.
fn row_column_spans(ranges: &[RangeMatch], row: usize) -> Vec<(TextOperation, usize, usize)> {
    ranges
        .iter()
        .filter(|r| r.source.start_row == row && r.source.end_row == row)
        .map(|r| {
            (
                r.operation.clone(),
                r.source.start_column,
                r.source.end_column,
            )
        })
        .collect()
}

/// A wide line whose one field changed highlights that field, not the row (a regenerated CSV row).
#[test]
fn plain_text_line_diff_narrows_a_changed_line_to_the_changed_part() {
    let before = "h\nname,alpha,beta,gamma,delta,37.282,epsilon,zeta\n";
    let after = "h\nname,alpha,beta,gamma,delta,37.860,epsilon,zeta\n";
    let (before_ranges, after_ranges) = plain_text_line_diff(before, after);

    // "37." is common prefix, "epsilon,zeta" common suffix; only "282"/"860" differ.
    let prefix_end = "name,alpha,beta,gamma,delta,37.".len();
    let middle_end = prefix_end + "282".len();
    assert_eq!(
        row_column_spans(&before_ranges, 1),
        vec![
            (TextOperation::Identical, 0, prefix_end),
            (TextOperation::Update, prefix_end, middle_end),
            (
                TextOperation::Identical,
                middle_end,
                "name,alpha,beta,gamma,delta,37.282,epsilon,zeta".len()
            ),
        ]
    );
    assert_eq!(
        row_column_spans(&after_ranges, 1).len(),
        3,
        "both sides must produce the same number of sub-ranges - see intra_line_ranges"
    );
}

/// A pure block insert stays one range, not one per line, so `n`/`p` navigation steps over it once.
#[test]
fn plain_text_line_diff_keeps_a_block_insert_merged() {
    let before = "same\n";
    let after = "same\nwholly different one\nwholly different two\nwholly different three\n";
    let (_, after_ranges) = plain_text_line_diff(before, after);
    assert_eq!(
        changed_row_spans(&after_ranges),
        vec![(TextOperation::Insert, 1, 4)],
        "three unrelated inserted lines must stay one merged range"
    );
}

/// Rows inserted among changed rows knock the sides out of step; `plan_gap` resynchronises
/// instead of ending refinement for the rest of the run.
#[test]
fn plain_text_line_diff_resynchronises_after_an_inserted_line() {
    let before = "row-a,1\nrow-b,1\nrow-c,1\n";
    let after = "row-a,2\nBRAND NEW UNRELATED LINE\nrow-b,2\nrow-c,2\n";
    let (before_ranges, after_ranges) = plain_text_line_diff(before, after);

    for (before_row, after_row) in [(0usize, 0usize), (1, 2), (2, 3)] {
        assert_eq!(
            row_column_spans(&before_ranges, before_row),
            vec![
                (TextOperation::Identical, 0, "row-a,".len()),
                (TextOperation::Update, "row-a,".len(), "row-a,1".len()),
            ],
            "before row {before_row} should narrow to its trailing digit"
        );
        assert_eq!(
            row_column_spans(&after_ranges, after_row).len(),
            2,
            "after row {after_row} must stay symmetric with its partner"
        );
    }
    assert!(
        changed_row_spans(&after_ranges).contains(&(TextOperation::Insert, 1, 2)),
        "the genuinely new line must still read as an insert: {:?}",
        changed_row_spans(&after_ranges)
    );
}

/// Byte columns, not characters: a multi-byte character before the change shifts every later
/// offset, and getting it wrong slices mid-character downstream.
#[test]
fn plain_text_line_diff_intra_line_columns_are_byte_offsets() {
    let before = "x\nprefix — value 1 suffix\n";
    let after = "x\nprefix — value 2 suffix\n";
    let (before_ranges, _) = plain_text_line_diff(before, after);

    let prefix_end = "prefix — value ".len(); // 17 bytes, 15 chars - the em dash is 3 bytes
    assert_eq!(
        row_column_spans(&before_ranges, 1),
        vec![
            (TextOperation::Identical, 0, prefix_end),
            (TextOperation::Update, prefix_end, prefix_end + 1),
            (
                TextOperation::Identical,
                prefix_end + 1,
                "prefix — value 1 suffix".len()
            ),
        ]
    );
    assert_eq!(
        prefix_end, 17,
        "sanity: the em dash must count as 3 bytes, not 1 char"
    );
}

/// Matches separated by gaps on both sides: each side's gaps are walked in its own row space, not
/// zipped together.
#[test]
fn plain_text_line_diff_handles_non_contiguous_matches() {
    let before = "same0\nDEL_A\nsame1\nDEL_B\nDEL_C\nsame2\n";
    let after = "same0\nINS_A\nINS_B\nsame1\nsame2\n";
    let (before_ranges, after_ranges) = plain_text_line_diff(before, after);

    assert_eq!(
        changed_row_spans(&before_ranges),
        vec![(TextOperation::Delete, 1, 2), (TextOperation::Delete, 3, 5)],
        "before's two unmatched runs (rows 1 and 3-4) must stay separate, not merge across \
         the row-2 match"
    );
    assert_eq!(
        changed_row_spans(&after_ranges),
        vec![(TextOperation::Insert, 1, 3)],
        "after's contiguous unmatched run (rows 1-2) must merge into one range"
    );

    // same0 (before row 0) matches after row 0; same1 (before row 2) matches after row 3;
    // same2 (before row 5) matches after row 4.
    let matches: Vec<_> = before_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Identical)
        .map(|r| (r.source.start_row, r.destination.start_row))
        .collect();
    assert_eq!(matches, vec![(0, 0), (2, 3), (5, 4)]);
}

/// An unmatched run's anchor is just past the preceding match's destination, not its own row
/// number, which is in the other side's coordinate space.
#[test]
fn plain_text_line_diff_anchors_unmatched_runs_at_the_preceding_match_destination() {
    // before: same0, DEL, same1        (3 lines)
    // after:  same0, INS_A, INS_B, same1  (4 lines) - "same1" sits at a different row on
    // each side (before row 2, after row 3), so a correct anchor must use the *destination*
    // coordinate space, not reuse the source row number.
    let before = "same0\nDEL\nsame1\n";
    let after = "same0\nINS_A\nINS_B\nsame1\n";
    let (before_ranges, after_ranges) = plain_text_line_diff(before, after);

    let delete = before_ranges
        .iter()
        .find(|r| r.operation == TextOperation::Delete)
        .expect("before should have one Delete range");
    assert_eq!(
        delete.destination.start_row, 1,
        "the deleted before-row-1 line has no real counterpart, so its cross-highlight \
         anchor should sit right after same0's match (after row 0), i.e. after row 1"
    );

    let insert = after_ranges
        .iter()
        .find(|r| r.operation == TextOperation::Insert)
        .expect("after should have one Insert range");
    assert_eq!(
        insert.destination.start_row, 1,
        "the inserted after-rows have no real counterpart, so its cross-highlight anchor \
         should sit right after same0's match (before row 0), i.e. before row 1"
    );
}

#[test]
fn plain_text_line_diff_handles_empty_before_as_a_pure_insertion() {
    let (before, after) = plain_text_line_diff("", "a\nb\n");
    assert!(before.is_empty());
    assert_eq!(
        changed_row_spans(&after),
        vec![(TextOperation::Insert, 0, 2)]
    );
}

#[test]
fn plain_text_line_diff_handles_empty_after_as_a_pure_deletion() {
    let (before, after) = plain_text_line_diff("a\nb\n", "");
    assert_eq!(
        changed_row_spans(&before),
        vec![(TextOperation::Delete, 0, 2)]
    );
    assert!(after.is_empty());
}

#[test]
fn plain_text_line_diff_treats_two_empty_files_as_no_changes() {
    let (before, after) = plain_text_line_diff("", "");
    assert!(before.is_empty());
    assert!(after.is_empty());
}

/// Past the edit-distance cap, the whole file is one `Delete` and one `Insert`. Uses a small cap:
/// reaching the real `PLAIN_TEXT_MAX_EDIT` costs quadratic memory.
#[test]
fn plain_text_line_diff_replaces_the_whole_file_past_the_edit_cap() {
    const SMALL_CAP: usize = 20;
    let before: String = (0..SMALL_CAP + 10)
        .map(|i| format!("before-unique-line-{i}\n"))
        .collect();
    let after: String = (0..SMALL_CAP + 10)
        .map(|i| format!("after-unique-line-{i}\n"))
        .collect();
    let (before_ranges, after_ranges) =
        plain_text_line_diff_with_max_edit(&before, &after, SMALL_CAP);

    assert_eq!(before_ranges.len(), 1);
    assert_eq!(before_ranges[0].operation, TextOperation::Delete);
    assert_eq!(
        before_ranges[0].source.end_row,
        SMALL_CAP + 10,
        "the single Delete range should cover every line, not just part of the file"
    );

    assert_eq!(after_ranges.len(), 1);
    assert_eq!(after_ranges[0].operation, TextOperation::Insert);
    assert_eq!(after_ranges[0].source.end_row, SMALL_CAP + 10);
}

/// The real `PLAIN_TEXT_MAX_EDIT` covers a scattered edit to a 10,000-line file. Cheap: Myers
/// stops at the actual edit distance, not the cap.
#[test]
fn plain_text_line_diff_handles_a_ten_thousand_line_file_with_scattered_changes() {
    let before: String = (0..10_000).map(|i| format!("line-{i}\n")).collect();
    let after: String = (0..10_000)
        .map(|i| {
            if i % 137 == 0 {
                format!("changed-line-{i}\n")
            } else {
                format!("line-{i}\n")
            }
        })
        .collect();
    let (before_ranges, after_ranges) = plain_text_line_diff(&before, &after);

    assert!(
        before_ranges
            .iter()
            .any(|r| r.operation == TextOperation::Delete),
        "a real per-line diff should find the scattered deletes, not give up and replace the \
         whole file: got {} before ranges",
        before_ranges.len()
    );
    assert!(
        after_ranges
            .iter()
            .any(|r| r.operation == TextOperation::Insert),
        "a real per-line diff should find the scattered inserts, not give up and replace the \
         whole file: got {} after ranges",
        after_ranges.len()
    );
    assert!(
        before_ranges.len() > 10,
        "10,000/137 ≈ 73 scattered changes should produce many small ranges, not one giant \
         replaced-whole-file range: got {} before ranges",
        before_ranges.len()
    );
}

#[test]
fn whole_file_class_identical_when_no_lines_changed() {
    assert_eq!(
        whole_file_text_class("a\nb\nc\n", "a\nb\nc\n"),
        WholeFileClass::Identical
    );
}

#[test]
fn whole_file_class_insert_only_when_nothing_deleted() {
    assert_eq!(
        whole_file_text_class("a\nc\n", "a\nb\nc\n"),
        WholeFileClass::InsertOnly
    );
}

#[test]
fn whole_file_class_delete_only_when_nothing_inserted() {
    assert_eq!(
        whole_file_text_class("a\nb\nc\n", "a\nc\n"),
        WholeFileClass::DeleteOnly
    );
}

#[test]
fn whole_file_class_mixed_when_both_inserted_and_deleted() {
    assert_eq!(
        whole_file_text_class("a\nb\nc\n", "a\nx\nc\n"),
        WholeFileClass::Mixed,
        "a changed line is a delete+insert pair, not an Update - so it's Mixed, not licensed"
    );
}

#[test]
fn whole_file_class_mixed_when_myers_lcs_gives_up() {
    const SMALL_CAP: usize = 20;
    let before: String = (0..SMALL_CAP + 10)
        .map(|i| format!("before-unique-line-{i}\n"))
        .collect();
    let after: String = (0..SMALL_CAP + 10)
        .map(|i| format!("after-unique-line-{i}\n"))
        .collect();
    let class = line_diff_core(&before, &after, SMALL_CAP)
        .map(|core| core.whole_file_class())
        .unwrap_or(WholeFileClass::Mixed);
    assert_eq!(
        class,
        WholeFileClass::Mixed,
        "a give-up must never be reported as a licensable class - no license should be \
         granted from an edit distance too large to have actually been measured"
    );
}

/// Cross-checks `whole_file_text_class` against an independent classification (Python
/// `difflib.SequenceMatcher`, `autojunk=False`) of the corpus in
/// `src/test/data/whole_file_text_classification_census.csv`. The class licenses a delete-free or
/// insert-free resolver, so wiring bugs need an external ground truth to catch.
#[test]
#[ignore = "slow"]
fn whole_file_text_class_matches_independent_census() -> Result<()> {
    let census_path = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("src")
        .join("test")
        .join("data")
        .join("whole_file_text_classification_census.csv");
    let census_csv = std::fs::read_to_string(&census_path)?;

    let pairs = test::helper::handmade_test_code_pairs()?;
    let mut mismatches = Vec::new();
    let mut checked = 0;
    for line in census_csv.lines().skip(1) {
        let (fixture, expected_str) = line
            .split_once(',')
            .expect("census CSV row must be `fixture,classification`");
        let expected = match expected_str {
            "Identical" => WholeFileClass::Identical,
            "InsertOnly" => WholeFileClass::InsertOnly,
            "DeleteOnly" => WholeFileClass::DeleteOnly,
            "Mixed" => WholeFileClass::Mixed,
            other => panic!("unknown census classification `{other}` for `{fixture}`"),
        };
        let Some((before, after)) = pairs.get(fixture) else {
            continue;
        };
        checked += 1;
        let actual = whole_file_text_class(&before.contents, &after.contents);
        if actual != expected {
            mismatches.push(format!("{fixture}: census={expected:?} rust={actual:?}"));
        }
    }

    assert!(
        checked > 300,
        "expected to check the vast majority of the 338-fixture corpus, only checked {checked}"
    );
    assert!(
        mismatches.is_empty(),
        "{} whole-file classification mismatches vs the independent census:\n{}",
        mismatches.len(),
        mismatches.join("\n")
    );
    Ok(())
}

/// A row covered by both an `Update` (a changed token) and an `Identical` range (the rest of the
/// row) is `Update`, whichever comes last.
#[test]
fn line_operations_does_not_let_a_same_row_identical_range_hide_a_real_change() {
    let ranges = vec![
        RangeMatch {
            source: TextRange::new(0, 4, 0, 12),
            destination: TextRange::new(0, 4, 0, 12),
            operation: TextOperation::Update,
        },
        // After the `Update` on purpose.
        RangeMatch {
            source: TextRange::new(0, 12, 1, 0),
            destination: TextRange::new(0, 12, 1, 0),
            operation: TextOperation::Identical,
        },
    ];
    assert_eq!(line_operations(&ranges, 1), vec![TextOperation::Update]);
}

#[test]
fn line_operations_treats_a_zero_width_range_as_a_placeholder_not_a_real_row() {
    let ranges = vec![RangeMatch {
        source: TextRange::new(1, 0, 1, 0),
        destination: TextRange::new(1, 0, 2, 0),
        operation: TextOperation::Delete,
    }];
    let ops = line_operations(&ranges, 3);
    assert_eq!(ops, vec![TextOperation::Identical; 3]);
}

/// Row 1 updated on both sides and row 3 inserted: the update counts once, not once per side.
#[test]
fn change_counts_tallies_insertions_deletions_and_updates_without_double_counting() {
    let update_range = |row: usize| RangeMatch {
        source: TextRange::new(row, 0, row, 1),
        destination: TextRange::new(row, 0, row, 1),
        operation: TextOperation::Update,
    };
    let before_ranges = vec![update_range(1)];
    let after_ranges = vec![
        update_range(1),
        RangeMatch {
            source: TextRange::new(3, 0, 3, 1),
            destination: TextRange::new(3, 0, 3, 1),
            operation: TextOperation::Insert,
        },
    ];

    let counts = change_counts("a\nb\nc", "a\nX\nc\nd", &before_ranges, &after_ranges);
    assert_eq!(
        counts,
        ChangeCounts {
            insertions: 1,
            deletions: 0,
            updates: 1,
            moves: 0,
        }
    );
}

/// A moved line exists on both sides, so `Move` counts once, from the after side.
#[test]
fn change_counts_tallies_moves_once_from_the_after_side() {
    let move_range = |row: usize, dest_row: usize| RangeMatch {
        source: TextRange::new(row, 0, row, 1),
        destination: TextRange::new(dest_row, 0, dest_row, 1),
        operation: TextOperation::Move,
    };
    let before_ranges = vec![move_range(0, 2)];
    let after_ranges = vec![move_range(2, 0)];

    let counts = change_counts("a\nb\nc", "b\nc\na", &before_ranges, &after_ranges);
    assert_eq!(
        counts,
        ChangeCounts {
            insertions: 0,
            deletions: 0,
            updates: 0,
            moves: 1,
        }
    );
}

#[test]
fn no_change_all_ranges() -> Result<()> {
    let (before, after) = &*test::helper::handmade_test_code_pair("rust-no-change")?;
    let node_cache = NodeCache::build(before, after);
    let diff = crate::diff::Diff::from_code(before, after);

    let text_diff = TextDiff::from(before, after, &diff.ast.unwrap(), &node_cache);

    let before_ranges = text_diff.all(0);
    assert_eq!(before_ranges.len(), 1, "Wrong number of before ranges");

    let after_ranges = text_diff.all(1);
    assert_eq!(after_ranges.len(), 1, "Wrong number of after ranges");

    assert_eq!(
        before_ranges[0].operation,
        TextOperation::Identical,
        "The identical part has wrong operation"
    );
    assert_eq!(
        before_ranges[0].source.start_row, 0,
        "The identical part has wrong source start row"
    );
    assert_eq!(
        before_ranges[0].source.start_column, 0,
        "The identical part has wrong source start column"
    );
    assert_eq!(
        before_ranges[0].source.end_row, 49,
        "The identical part has wrong source end row"
    );
    assert_eq!(
        before_ranges[0].source.end_column, 0,
        "The identical part has wrong source end column"
    );
    assert_eq!(
        before_ranges[0].destination.start_row, 0,
        "The identical part has wrong destination start row"
    );
    assert_eq!(
        before_ranges[0].destination.start_column, 0,
        "The identical part has wrong destination start column"
    );
    assert_eq!(
        before_ranges[0].destination.end_row, 49,
        "The identical part has wrong destination end row"
    );
    assert_eq!(
        before_ranges[0].destination.end_column, 0,
        "The identical part has wrong destination end column"
    );

    assert_eq!(
        after_ranges[0].operation,
        TextOperation::Identical,
        "When looking from after to before: The identical part has wrong operation"
    );
    assert_eq!(
        after_ranges[0].source.start_row, 0,
        "When looking from after to before: The identical part has wrong source start row"
    );
    assert_eq!(
        after_ranges[0].source.start_column, 0,
        "When looking from after to before: The identical part has wrong source start column"
    );
    assert_eq!(
        after_ranges[0].source.end_row, 49,
        "When looking from after to before: The identical part has wrong source end row"
    );
    assert_eq!(
        after_ranges[0].source.end_column, 0,
        "When looking from after to before: The identical part has wrong source end column"
    );
    assert_eq!(
        after_ranges[0].destination.start_row, 0,
        "When looking from after to before: The identical part has wrong destination start row"
    );
    assert_eq!(
        after_ranges[0].destination.start_column, 0,
        "When looking from after to before: The identical part has wrong destination start column"
    );
    assert_eq!(
        after_ranges[0].destination.end_row, 49,
        "When looking from after to before: The identical part has wrong destination end row"
    );
    assert_eq!(
        after_ranges[0].destination.end_column, 0,
        "When looking from after to before: The identical part has wrong destination end column"
    );

    Ok(())
}

#[test]
fn hello_world_added_message_all_ranges() -> Result<()> {
    let (before, after) =
        &*test::helper::handmade_test_code_pair("rust-hello-world-added-message")?;
    let node_cache = NodeCache::build(before, after);
    let diff = crate::diff::Diff::from_code(before, after);

    let text_diff = TextDiff::from(before, after, &diff.ast.unwrap(), &node_cache);

    let before_ranges = text_diff.all(0);
    assert_eq!(before_ranges.len(), 3, "Wrong number of before ranges");

    assert_eq!(
        before_ranges[0].operation,
        TextOperation::Identical,
        "The initial identical part has wrong operation"
    );
    assert_eq!(
        before_ranges[0].source.start_row, 0,
        "The initial identical part has wrong source start row"
    );
    assert_eq!(
        before_ranges[0].source.start_column, 0,
        "The initial identical part has wrong source start column"
    );
    assert_eq!(
        before_ranges[0].source.end_row, 2,
        "The initial identical part has wrong source end row"
    );
    assert_eq!(
        before_ranges[0].source.end_column, 0,
        "The initial identical part has wrong source end column"
    );
    assert_eq!(
        before_ranges[0].destination.start_row, 0,
        "The initial identical part has wrong destination start row"
    );
    assert_eq!(
        before_ranges[0].destination.start_column, 0,
        "The initial identical part has wrong destination start column"
    );
    assert_eq!(
        before_ranges[0].destination.end_row, 2,
        "The initial identical part has wrong destination end row"
    );
    assert_eq!(
        before_ranges[0].destination.end_column, 0,
        "The initial identical part has wrong destination end column"
    );

    assert_eq!(
        before_ranges[1].operation,
        TextOperation::Delete,
        "The virtual delete, that marks the 'insert' on the after side, has wrong operation"
    );
    assert_eq!(
        before_ranges[1].source.start_row, 2,
        "The virtual delete, that marks the 'insert' on the after side, has wrong source start row"
    );
    assert_eq!(
        before_ranges[1].source.start_column, 0,
        "The virtual delete, that marks the 'insert' on the after side, has wrong source start column"
    );
    assert_eq!(
        before_ranges[1].source.end_row, 2,
        "The virtual delete, that marks the 'insert' on the after side, has wrong source end row"
    );
    assert_eq!(
        before_ranges[1].source.end_column, 0,
        "The virtual delete, that marks the 'insert' on the after side, has wrong source end column"
    );
    // Note that because we ignore whitespace, the [(2, 0), (2, 2)> range is simply missing from
    // the result.
    assert_eq!(
        before_ranges[1].destination.start_row, 2,
        "The virtual delete, that marks the 'insert' on the after side, has wrong destination start row"
    );
    assert_eq!(
        before_ranges[1].destination.start_column, 2,
        "The virtual delete, that marks the 'insert' on the after side, has wrong destination start column"
    );
    assert_eq!(
        before_ranges[1].destination.end_row, 3,
        "The virtual delete, that marks the 'insert' on the after side, has wrong destination end row"
    );
    assert_eq!(
        before_ranges[1].destination.end_column, 0,
        "The virtual delete, that marks the 'insert' on the after side, has wrong destination end column"
    );

    assert_eq!(
        before_ranges[2].operation,
        TextOperation::Identical,
        "The final identical part has wrong operation"
    );
    assert_eq!(
        before_ranges[2].source.start_row, 2,
        "The final identical part has wrong source start row"
    );
    assert_eq!(
        before_ranges[2].source.start_column, 0,
        "The final identical part has wrong source start column"
    );
    assert_eq!(
        before_ranges[2].source.end_row, 3,
        "The final identical part has wrong source end row"
    );
    assert_eq!(
        before_ranges[2].source.end_column, 0,
        "The final identical part has wrong source end column"
    );
    assert_eq!(
        before_ranges[2].destination.start_row, 3,
        "The final identical part has wrong destination start row"
    );
    assert_eq!(
        before_ranges[2].destination.start_column, 0,
        "The final identical part has wrong destination start column"
    );
    assert_eq!(
        before_ranges[2].destination.end_row, 4,
        "The final identical part has wrong destination end row"
    );
    assert_eq!(
        before_ranges[2].destination.end_column, 0,
        "The final identical part has wrong destination end column"
    );

    let after_ranges = text_diff.all(1);
    assert_eq!(
        after_ranges.len(),
        3,
        "When looking from after to before: Wrong number of after ranges"
    );

    assert_eq!(
        after_ranges[0].operation,
        TextOperation::Identical,
        "When looking from after to before: The initial identical part has wrong operation"
    );
    assert_eq!(
        after_ranges[0].source.start_row, 0,
        "When looking from after to before: The initial identical part has wrong source start row"
    );
    assert_eq!(
        after_ranges[0].source.start_column, 0,
        "When looking from after to before: The initial identical part has wrong source start column"
    );
    assert_eq!(
        after_ranges[0].source.end_row, 2,
        "When looking from after to before: The initial identical part has wrong source end row"
    );
    assert_eq!(
        after_ranges[0].source.end_column, 0,
        "When looking from after to before: The initial identical part has wrong source end column"
    );
    assert_eq!(
        after_ranges[0].destination.start_row, 0,
        "When looking from after to before: The initial identical part has wrong destination start row"
    );
    assert_eq!(
        after_ranges[0].destination.start_column, 0,
        "When looking from after to before: The initial identical part has wrong destination start column"
    );
    assert_eq!(
        after_ranges[0].destination.end_row, 2,
        "When looking from after to before: The initial identical part has wrong destination end row"
    );
    assert_eq!(
        after_ranges[0].destination.end_column, 0,
        "When looking from after to before: The initial identical part has wrong destination end column"
    );

    assert_eq!(
        after_ranges[1].operation,
        TextOperation::Insert,
        "When looking from after to before: The insert has the wrong operation"
    );
    assert_eq!(
        after_ranges[1].source.start_row, 2,
        "When looking from after to before: The insert has wrong source start row"
    );
    assert_eq!(
        after_ranges[1].source.start_column, 2,
        "When looking from after to before: The insert has wrong source start column"
    );
    assert_eq!(
        after_ranges[1].source.end_row, 3,
        "When looking from after to before: The insert has wrong source end row"
    );
    assert_eq!(
        after_ranges[1].source.end_column, 0,
        "When looking from after to before: The insert has wrong source end column"
    );
    assert_eq!(
        after_ranges[1].destination.start_row, 2,
        "When looking from after to before: The insert has wrong destination start row"
    );
    assert_eq!(
        after_ranges[1].destination.start_column, 0,
        "When looking from after to before: The insert has wrong destination start column"
    );
    assert_eq!(
        after_ranges[1].destination.end_row, 2,
        "When looking from after to before: The insert has wrong destination end row"
    );
    assert_eq!(
        after_ranges[1].destination.end_column, 0,
        "When looking from after to before: The insert has wrong destination end column"
    );

    assert_eq!(
        after_ranges[2].operation,
        TextOperation::Identical,
        "When looking from after to before: The final identical part has wrong operation"
    );
    assert_eq!(
        after_ranges[2].source.start_row, 3,
        "When looking from after to before: The final identical part has wrong source start row"
    );
    assert_eq!(
        after_ranges[2].source.start_column, 0,
        "When looking from after to before: The final identical part has wrong source start column"
    );
    assert_eq!(
        after_ranges[2].source.end_row, 4,
        "When looking from after to before: The final identical part has wrong source end row"
    );
    assert_eq!(
        after_ranges[2].source.end_column, 0,
        "When looking from after to before: The final identical part has wrong source end column"
    );
    assert_eq!(
        after_ranges[2].destination.start_row, 2,
        "When looking from after to before: The final identical part has wrong destination start row"
    );
    assert_eq!(
        after_ranges[2].destination.start_column, 0,
        "When looking from after to before: The final identical part has wrong destination start column"
    );
    assert_eq!(
        after_ranges[2].destination.end_row, 3,
        "When looking from after to before: The final identical part has wrong destination end row"
    );
    assert_eq!(
        after_ranges[2].destination.end_column, 0,
        "When looking from after to before: The final identical part has wrong destination end column"
    );

    Ok(())
}

#[test]
fn python_leetcode_1_added_if_block_all_ranges() -> Result<()> {
    let (before, after) = &*test::helper::handmade_test_code_pair("python-added-if-block")?;
    let node_cache = NodeCache::build(before, after);
    let diff = crate::diff::Diff::from_code(before, after);

    let text_diff = TextDiff::from(before, after, &diff.ast.unwrap(), &node_cache);

    let before_ranges = text_diff.all(0);
    assert_eq!(before_ranges.len(), 3);

    assert_eq!(before_ranges[0].operation, TextOperation::Identical);
    assert_eq!(before_ranges[0].source.start_row, 0);
    assert_eq!(before_ranges[0].source.start_column, 0);
    assert_eq!(before_ranges[0].source.end_row, 20);
    assert_eq!(before_ranges[0].source.end_column, 0);
    assert_eq!(before_ranges[0].destination.start_row, 0);
    assert_eq!(before_ranges[0].destination.start_column, 0);
    assert_eq!(before_ranges[0].destination.end_row, 20);
    assert_eq!(before_ranges[0].destination.end_column, 0);

    // This is a "empty range" that indicates something exists here in the other side.
    // Note that because we ignore whitespace, the leading 4-space indentation of the new
    // "if" line is simply missing from the result, and the destination starts at column 4.
    assert_eq!(before_ranges[1].operation, TextOperation::Delete);
    assert_eq!(before_ranges[1].source.start_row, 20);
    assert_eq!(before_ranges[1].source.start_column, 0);
    assert_eq!(before_ranges[1].source.end_row, 20);
    assert_eq!(before_ranges[1].source.end_column, 0);
    assert_eq!(before_ranges[1].destination.start_row, 20);
    assert_eq!(before_ranges[1].destination.start_column, 4);
    assert_eq!(before_ranges[1].destination.end_row, 21);
    assert_eq!(before_ranges[1].destination.end_column, 0);

    // Note the order between the empty range and the actual range that exists. The empty range
    // must always be before an actual existing range, even if their start point is equal.
    // This is the print statement that was re-indented (column 4 -> column 8) because it now
    // lives one level deeper inside the new "if" block. Its text is identical, but its
    // position moved, so it's a Move rather than an Identical range.
    assert_eq!(before_ranges[2].operation, TextOperation::Move);
    assert_eq!(before_ranges[2].source.start_row, 20);
    assert_eq!(before_ranges[2].source.start_column, 4);
    assert_eq!(before_ranges[2].source.end_row, 21);
    assert_eq!(before_ranges[2].source.end_column, 0);
    assert_eq!(before_ranges[2].destination.start_row, 21);
    assert_eq!(before_ranges[2].destination.start_column, 8);
    assert_eq!(before_ranges[2].destination.end_row, 22);
    assert_eq!(before_ranges[2].destination.end_column, 0);

    let after_ranges = text_diff.all(1);
    // Note the symmetric relationships between source and destination ranges in the
    // before_ranges and after_ranges vectors.
    assert_eq!(after_ranges.len(), before_ranges.len());

    assert_eq!(after_ranges[0].operation, TextOperation::Identical);
    assert_eq!(after_ranges[0].source.start_row, 0);
    assert_eq!(after_ranges[0].source.start_column, 0);
    assert_eq!(after_ranges[0].source.end_row, 20);
    assert_eq!(after_ranges[0].source.end_column, 0);
    assert_eq!(after_ranges[0].destination.start_row, 0);
    assert_eq!(after_ranges[0].destination.start_column, 0);
    assert_eq!(after_ranges[0].destination.end_row, 20);
    assert_eq!(after_ranges[0].destination.end_column, 0);

    // The added "if" conditional (leading 4-space indentation ignored, same as above).
    assert_eq!(after_ranges[1].operation, TextOperation::Insert);
    assert_eq!(after_ranges[1].source.start_row, 20);
    assert_eq!(after_ranges[1].source.start_column, 4);
    assert_eq!(after_ranges[1].source.end_row, 21);
    assert_eq!(after_ranges[1].source.end_column, 0);
    assert_eq!(after_ranges[1].destination.start_row, 20);
    assert_eq!(after_ranges[1].destination.start_column, 0);
    assert_eq!(after_ranges[1].destination.end_row, 20);
    assert_eq!(after_ranges[1].destination.end_column, 0);

    // The matched existing implementation, moved one level deeper.
    assert_eq!(after_ranges[2].operation, TextOperation::Move);
    assert_eq!(after_ranges[2].source.start_row, 21);
    assert_eq!(after_ranges[2].source.start_column, 8);
    assert_eq!(after_ranges[2].source.end_row, 22);
    assert_eq!(after_ranges[2].source.end_column, 0);
    assert_eq!(after_ranges[2].destination.start_row, 20);
    assert_eq!(after_ranges[2].destination.start_column, 4);
    assert_eq!(after_ranges[2].destination.end_row, 21);
    assert_eq!(after_ranges[2].destination.end_column, 0);

    Ok(())
}

fn range(operation: TextOperation) -> RangeMatch {
    RangeMatch {
        source: TextRange::new(0, 0, 1, 0),
        destination: TextRange::new(0, 0, 1, 0),
        operation,
    }
}

#[test]
fn whitespace_stripped_equal_ignores_all_whitespace_differences() {
    assert!(whitespace_stripped_equal(
        "fn main() {\n    foo();\n}\n",
        "fn main(){foo();}"
    ));
    assert!(!whitespace_stripped_equal("fn main() {}", "fn other() {}"));
}

#[test]
fn summarize_diff_is_no_changes_when_every_range_is_identical() {
    let ranges = vec![range(TextOperation::Identical)];
    assert_eq!(
        summarize_diff("same", "same", &ranges, &ranges),
        Some(DiffSummary::NoChanges)
    );
}

#[test]
fn summarize_diff_is_no_changes_for_two_empty_files() {
    assert_eq!(
        summarize_diff("", "", &[], &[]),
        Some(DiffSummary::NoChanges)
    );
}

#[test]
fn summarize_diff_is_new_file_when_only_inserts_are_present() {
    let before_ranges: Vec<RangeMatch> = vec![];
    let after_ranges = vec![range(TextOperation::Insert)];
    assert_eq!(
        summarize_diff("", "fn main() {}", &before_ranges, &after_ranges),
        Some(DiffSummary::NewFile)
    );
}

/// One line added to an otherwise unchanged file is not `NewFile`: its `Identical` ranges
/// disqualify it.
#[test]
fn summarize_diff_is_not_new_file_when_inserts_are_mixed_with_identical_content() {
    let ranges = vec![
        range(TextOperation::Identical),
        range(TextOperation::Insert),
    ];
    assert_eq!(
        summarize_diff(
            "fn main() {\n    foo();\n}",
            "fn main() {\n    foo();\n    bar();\n}",
            &ranges,
            &ranges
        ),
        None
    );
}

#[test]
fn summarize_diff_is_not_deleted_file_when_deletes_are_mixed_with_identical_content() {
    let ranges = vec![
        range(TextOperation::Identical),
        range(TextOperation::Delete),
    ];
    assert_eq!(
        summarize_diff(
            "fn main() {\n    foo();\n    bar();\n}",
            "fn main() {\n    foo();\n}",
            &ranges,
            &ranges
        ),
        None
    );
}

#[test]
fn summarize_diff_is_deleted_file_when_only_deletes_are_present() {
    let before_ranges = vec![range(TextOperation::Delete)];
    let after_ranges: Vec<RangeMatch> = vec![];
    assert_eq!(
        summarize_diff("fn main() {}", "", &before_ranges, &after_ranges),
        Some(DiffSummary::DeletedFile)
    );
}

#[test]
fn summarize_diff_is_whitespace_only_when_stripped_content_matches_despite_move_ranges() {
    // A pure reindent paints `Move`s; only the stripped content says nothing changed.
    let ranges = vec![range(TextOperation::Move)];
    assert_eq!(
        summarize_diff(
            "fn main() {\nfoo();\n}",
            "fn main() {\n    foo();\n}",
            &ranges,
            &ranges
        ),
        Some(DiffSummary::WhitespaceOnly)
    );
}

#[test]
fn summarize_diff_is_refactor_moved_only_when_only_moves_are_present_and_content_really_differs() {
    let ranges = vec![range(TextOperation::Move)];
    assert_eq!(
        summarize_diff(
            "fn a() {}\nfn b() {}",
            "fn b() {}\nfn a() {}",
            &ranges,
            &ranges
        ),
        Some(DiffSummary::RefactorMovedOnly)
    );
}

#[test]
fn summarize_diff_is_none_for_a_genuine_mixed_edit() {
    let ranges = vec![range(TextOperation::Update), range(TextOperation::Insert)];
    assert_eq!(summarize_diff("a", "b", &ranges, &ranges), None);
}

#[test]
fn summarize_diff_prefers_whitespace_only_over_refactor_when_both_could_apply() {
    // Only `Move`s and whitespace-stripped-equal content: "reformatted" is the more specific claim.
    let ranges = vec![range(TextOperation::Move)];
    assert_eq!(
        summarize_diff("same", " same ", &ranges, &ranges),
        Some(DiffSummary::WhitespaceOnly)
    );
}

/// A reformat matched as one subtree whose start did not move produces no operations at all, and
/// is still `WhitespaceOnly`, not `NoChanges`: the files are not byte-identical.
#[test]
fn summarize_diff_is_whitespace_only_even_with_zero_operations_when_content_is_not_byte_identical()
{
    let no_ranges: Vec<RangeMatch> = vec![];
    assert_eq!(
        summarize_diff(
            "fn main() {\nfoo();\n}",
            "fn main() {\n    foo();\n}",
            &no_ranges,
            &no_ranges,
        ),
        Some(DiffSummary::WhitespaceOnly)
    );
}

/// A real parse and diff: `is_comment_only_diff` reads node kinds.
fn diff_ast(before_src: &str, after_src: &str) -> (crate::code::Code, crate::code::Code, ASTDiff) {
    let before = crate::code::Code::from_string(before_src, &crate::code::Language::Rust);
    let after = crate::code::Code::from_string(after_src, &crate::code::Language::Rust);
    let diff = crate::diff::diff_code(&before, &after);
    let ast = diff
        .ast
        .expect("diff_code should always produce an AST for valid Rust");
    (before, after, ast)
}

/// A new `line_comment` is a `//` leaf plus its own words; the words are painted too, not just the
/// marker (`rust-cost-optimization`).
#[test]
fn ranges_paints_a_wholly_new_comments_own_words_not_just_its_marker() {
    let (before, after, ast) = diff_ast("fn main() {}\n", "// hi there\nfn main() {}\n");
    let node_cache = NodeCache::build(&before, &after);
    let after_ranges = ranges(
        &after,
        &before,
        &ast,
        &node_cache,
        false,
        RenderOptions::FULL,
    );

    let words_painted = after_ranges.iter().any(|r| {
        r.operation == TextOperation::Insert
            && r.source.start_row == 0
            && r.source.start_column >= 2 // past the `//` marker itself
            && (r.source.end_row > r.source.start_row || r.source.end_column > r.source.start_column)
    });
    assert!(
        words_painted,
        "a brand-new comment's own words (everything after `//`) must be painted Insert, not \
         silently dropped: {after_ranges:?}"
    );
}

/// A string literal its children fully cover (`"` + content + `"`) descends normally, so the
/// whitespace between it and its siblings still merges: a new `let s = "a" + b;` is one `Insert`
/// end to end, not split around the literal (`java-add-logging`).
#[test]
fn a_no_gap_string_literal_does_not_break_whitespace_merging_with_a_sibling() {
    let (before, after, ast) =
        diff_ast("fn main() {}\n", "fn main() {\n    let s = \"a\" + b;\n}\n");
    let node_cache = NodeCache::build(&before, &after);
    let after_ranges = ranges(
        &after,
        &before,
        &ast,
        &node_cache,
        false,
        RenderOptions::FULL,
    );

    let row1_inserts: Vec<_> = after_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Insert && r.source.start_row == 1)
        .collect();
    assert_eq!(
        row1_inserts.len(),
        1,
        "the whole new `let s = \"a\" + b;` statement should merge into one Insert range - \
         more than one means a gap (like the whitespace around `+`) split it apart: \
         {after_ranges:?}"
    );
    assert_eq!(
        row1_inserts[0].source.start_column, 4,
        "the merged Insert range should start at `let`, right after the line's indentation: \
         {after_ranges:?}"
    );
}
/// Swapping two sibling functions (same column, different rows) paints `Move`, not nothing
/// (`crossed_backwards`).
#[test]
fn sibling_reorder_produces_move_ranges_and_a_refactor_moved_summary() {
    let before_src = "fn main() {\n    let a = 1;\n    println!(\"{}\", a);\n}\n\nfn helper(x: i32) -> i32 {\n    x * 2\n}\n";
    let after_src = "fn helper(x: i32) -> i32 {\n    x * 2\n}\n\nfn main() {\n    let a = 1;\n    println!(\"{}\", a);\n}\n";
    let (before, after, ast) = diff_ast(before_src, after_src);
    let node_cache = NodeCache::build(&before, &after);
    let text_diff = TextDiff::from(&before, &after, &ast, &node_cache);
    let before_ranges = text_diff.all(0);
    let after_ranges = text_diff.all(1);

    for (side, ranges) in [("before", &before_ranges), ("after", &after_ranges)] {
        assert!(
            ranges.iter().any(|r| r.operation == TextOperation::Move),
            "{side} side should contain a Move range for a sibling reorder, got {ranges:?}"
        );
        assert!(
            ranges
                .iter()
                .all(|r| matches!(r.operation, TextOperation::Move | TextOperation::Identical)),
            "{side} side of a pure reorder should only have Move/Identical ranges, got {ranges:?}"
        );
    }
    assert_eq!(
        summarize_diff(before_src, after_src, &before_ranges, &after_ranges),
        Some(DiffSummary::RefactorMovedOnly)
    );
}

/// Content shifted down by an insertion above keeps its column and order, so stays `Identical`.
#[test]
fn unrelated_insertion_does_not_flag_shifted_content_as_moved() {
    let before_src = "fn main() {\n    foo();\n}\n\nfn helper() {\n    bar();\n}\n";
    let after_src = "fn added() {}\n\nfn main() {\n    foo();\n}\n\nfn helper() {\n    bar();\n}\n";
    let (before, after, ast) = diff_ast(before_src, after_src);
    let node_cache = NodeCache::build(&before, &after);
    let text_diff = TextDiff::from(&before, &after, &ast, &node_cache);
    for (side, ranges) in [("before", &text_diff.all(0)), ("after", &text_diff.all(1))] {
        assert!(
            !ranges.iter().any(|r| r.operation == TextOperation::Move),
            "{side} side should have no Move ranges when content merely shifted down, got {ranges:?}"
        );
    }
}

/// A changed comment maps `MatchButNotIdentical`, not `Update`, and still counts as a change, in
/// agreement with what `ranges` paints.
#[test]
fn is_comment_only_diff_is_true_when_only_a_comments_text_changed() {
    let (before, after, ast) = diff_ast(
        "// old comment\nfn main() {}",
        "// new comment\nfn main() {}",
    );
    let node_cache = NodeCache::build(&before, &after);
    assert!(is_comment_only_diff(&before, &after, &ast, &node_cache));
}

#[test]
fn is_comment_only_diff_is_true_when_a_comment_was_inserted() {
    let (before, after, ast) = diff_ast("fn main() {}", "// a comment\nfn main() {}");
    let node_cache = NodeCache::build(&before, &after);
    assert!(is_comment_only_diff(&before, &after, &ast, &node_cache));
}

#[test]
fn is_comment_only_diff_is_true_when_a_comment_was_deleted() {
    let (before, after, ast) = diff_ast("// a comment\nfn main() {}", "fn main() {}");
    let node_cache = NodeCache::build(&before, &after);
    assert!(is_comment_only_diff(&before, &after, &ast, &node_cache));
}

#[test]
fn is_comment_only_diff_is_false_for_a_real_code_change() {
    let (before, after, ast) = diff_ast("fn main() { old(); }", "fn main() { new(); }");
    let node_cache = NodeCache::build(&before, &after);
    assert!(!is_comment_only_diff(&before, &after, &ast, &node_cache));
}

#[test]
fn is_comment_only_diff_is_false_when_a_comment_and_real_code_both_changed() {
    let (before, after, ast) = diff_ast(
        "// old comment\nfn main() { old(); }",
        "// new comment\nfn main() { new(); }",
    );
    let node_cache = NodeCache::build(&before, &after);
    assert!(!is_comment_only_diff(&before, &after, &ast, &node_cache));
}

#[test]
fn is_comment_only_diff_is_false_when_nothing_changed_at_all() {
    // "Comment-only" is a claim about what changed; with nothing changed it is false.
    let (before, after, ast) = diff_ast("fn main() {}", "fn main() {}");
    let node_cache = NodeCache::build(&before, &after);
    assert!(!is_comment_only_diff(&before, &after, &ast, &node_cache));
}

/// A statement wrapped one level deeper stays `Identical` in the AST, but its enclosing block
/// changed, so the diff is not comment-only.
#[test]
fn is_comment_only_diff_is_false_when_a_statement_moves_one_level_deeper() {
    let (before, after, ast) = diff_ast(
        "fn main() {\n    foo();\n    bar();\n}",
        "fn main() {\n    foo();\n    if true {\n        bar();\n    }\n}",
    );
    let node_cache = NodeCache::build(&before, &after);
    assert!(!is_comment_only_diff(&before, &after, &ast, &node_cache));
}

#[test]
fn summarize_diff_with_comment_check_reports_comment_only_over_no_classification() {
    let ranges = vec![range(TextOperation::Update)];
    assert_eq!(
        summarize_diff_with_comment_check("// a", "// b", &ranges, &ranges, true),
        Some(DiffSummary::CommentOnly)
    );
}

#[test]
fn summarize_diff_with_comment_check_reports_comment_only_over_refactor_moved_only() {
    let ranges = vec![range(TextOperation::Move)];
    assert_eq!(
        summarize_diff_with_comment_check(
            "fn a() {}\nfn b() {}",
            "fn b() {}\nfn a() {}",
            &ranges,
            &ranges,
            true,
        ),
        Some(DiffSummary::CommentOnly)
    );
}

#[test]
fn summarize_diff_with_comment_check_does_not_override_new_file() {
    let before_ranges: Vec<RangeMatch> = vec![];
    let after_ranges = vec![range(TextOperation::Insert)];
    assert_eq!(
        summarize_diff_with_comment_check(
            "",
            "// just a comment",
            &before_ranges,
            &after_ranges,
            true,
        ),
        Some(DiffSummary::NewFile),
        "a wholly new file should stay NewFile even if it's all comments"
    );
}

#[test]
fn summarize_diff_with_comment_check_ignores_the_flag_when_false() {
    let ranges = vec![range(TextOperation::Move)];
    assert_eq!(
        summarize_diff_with_comment_check(
            "fn a() {}\nfn b() {}",
            "fn b() {}\nfn a() {}",
            &ranges,
            &ranges,
            false,
        ),
        Some(DiffSummary::RefactorMovedOnly)
    );
}

/// Under `MINIMAL`, one changed character inside a long identifier is one narrow `Update`.
#[test]
fn ranges_decomposes_a_small_change_inside_a_long_identifier() {
    let (before, after, ast) = diff_ast(
        "fn main() {\n    let long_identifier_name = 5;\n}",
        "fn main() {\n    let long_identifier_nome = 5;\n}",
    );
    let node_cache = NodeCache::build(&before, &after);
    let before_ranges = ranges(
        &before,
        &after,
        &ast,
        &node_cache,
        true,
        RenderOptions::MINIMAL,
    );

    let updates: Vec<_> = before_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
    assert_eq!(
        updates.len(),
        1,
        "expected exactly one Update sub-range, got {before_ranges:?}"
    );
    let update = updates[0];
    assert_eq!(update.source.start_row, 1);
    assert_eq!(update.source.end_row, 1);
    assert_eq!(
        update.source.end_column - update.source.start_column,
        1,
        "the Update range should cover only the single changed character, not the whole \
         20-character identifier"
    );

    let after_ranges = ranges(
        &after,
        &before,
        &ast,
        &node_cache,
        false,
        RenderOptions::MINIMAL,
    );
    let after_updates: Vec<_> = after_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
    assert_eq!(after_updates.len(), 1);
    assert_eq!(
        after_updates[0].source.end_column - after_updates[0].source.start_column,
        1,
        "the after->before direction must independently find the same narrow width"
    );
}

/// Under `FULL`, the rename from the test above is painted whole on both sides - ground-truth
/// invariant 16's `Full` half, via [`RenderOptions::whole_identifier_updates`].
#[test]
fn full_paints_a_renamed_identifier_whole() {
    let (before, after, ast) = diff_ast(
        "fn main() {\n    let long_identifier_name = 5;\n}",
        "fn main() {\n    let long_identifier_nome = 5;\n}",
    );
    let node_cache = NodeCache::build(&before, &after);
    for (source, destination, source_is_before) in
        [(&before, &after, true), (&after, &before, false)]
    {
        let updates: Vec<_> = ranges(
            source,
            destination,
            &ast,
            &node_cache,
            source_is_before,
            RenderOptions::FULL,
        )
        .into_iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
        assert_eq!(updates.len(), 1, "expected one Update, got {updates:?}");
        assert_eq!(
            updates[0].source.end_column - updates[0].source.start_column,
            "long_identifier_name".len(),
            "FULL should paint the whole renamed identifier"
        );
    }
}

/// `whole_identifier_updates` is scoped to identifiers: a changed comment stays narrow under
/// `FULL`.
#[test]
fn full_keeps_a_changed_comment_narrow() {
    let (before, after, ast) = diff_ast(
        "// Copyright 2025 Example\nfn main() {}\n",
        "// Copyright 2026 Example\nfn main() {}\n",
    );
    let node_cache = NodeCache::build(&before, &after);
    let updates: Vec<_> = ranges(
        &before,
        &after,
        &ast,
        &node_cache,
        true,
        RenderOptions::FULL,
    )
    .into_iter()
    .filter(|r| r.operation == TextOperation::Update)
    .collect();
    assert_eq!(updates.len(), 1, "expected one Update, got {updates:?}");
    assert!(
        updates[0].source.end_column - updates[0].source.start_column <= 2,
        "only the changed digit(s) of the year should be painted, got {:?}",
        updates[0]
    );
}

/// [`RenderOptions::whole_pair_updates`] reports the same edit as one `Update` over the whole
/// identifier.
#[test]
fn ranges_reports_the_whole_identifier_when_whole_pair_updates_is_set() {
    let (before, after, ast) = diff_ast(
        "fn main() {\n    let long_identifier_name = 5;\n}",
        "fn main() {\n    let long_identifier_nome = 5;\n}",
    );
    let node_cache = NodeCache::build(&before, &after);
    let before_ranges = ranges(
        &before,
        &after,
        &ast,
        &node_cache,
        true,
        RenderOptions {
            whole_pair_updates: true,
            ..RenderOptions::FULL
        },
    );

    let updates: Vec<_> = before_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
    assert_eq!(
        updates.len(),
        1,
        "expected exactly one Update sub-range, got {before_ranges:?}"
    );
    assert_eq!(
        updates[0].source.end_column - updates[0].source.start_column,
        "long_identifier_name".len(),
        "the Update range should cover the whole identifier, not just the changed character"
    );
}

/// With no common prefix or suffix, the whole span is the `Update`.
#[test]
fn ranges_falls_back_to_a_whole_span_update_when_there_is_no_common_affix() {
    let (before, after, ast) = diff_ast(
        "fn main() {\n    let foo = 5;\n}",
        "fn main() {\n    let bar = 5;\n}",
    );
    let node_cache = NodeCache::build(&before, &after);
    let before_ranges = ranges(
        &before,
        &after,
        &ast,
        &node_cache,
        true,
        RenderOptions::FULL,
    );

    let updates: Vec<_> = before_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
    assert_eq!(updates.len(), 1);
    assert_eq!(
        updates[0].source.end_column - updates[0].source.start_column,
        3,
        "\"foo\" and \"bar\" share no common prefix/suffix, so the whole 3-character \
         identifier should be reported as changed"
    );
}

/// A comment's words sit in one gap after its `//` child, so a small change in them splits like an
/// identifier's.
#[test]
fn ranges_decomposes_a_small_change_inside_a_comment() {
    let (before, after, ast) = diff_ast(
        "// hello world!\nfn main() {}",
        "// hello universe!\nfn main() {}",
    );
    let node_cache = NodeCache::build(&before, &after);
    let before_ranges = ranges(
        &before,
        &after,
        &ast,
        &node_cache,
        true,
        RenderOptions::FULL,
    );

    let updates: Vec<_> = before_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
    assert_eq!(
        updates.len(),
        1,
        "expected exactly one Update sub-range inside the comment, got {before_ranges:?}"
    );
    assert_eq!(
        updates[0].source.end_column - updates[0].source.start_column,
        5,
        "\"world\" (5 chars) should be the only part marked changed, not the whole comment"
    );

    let identical_in_comment: Vec<_> = before_ranges
        .iter()
        .filter(|r| {
            r.operation == TextOperation::Identical
                && r.source.start_row == 0
                && r.source.end_row == 0
        })
        .collect();
    assert!(
        !identical_in_comment.is_empty(),
        "the common \"// hello \" prefix should be reported as Identical, not swallowed into \
         the Update: {before_ranges:?}"
    );
}

/// An unrelated earlier insertion shifts the changed identifier to different columns on each side.
/// The decomposed ranges bypass merging, so both sides still carry the narrow `Update` and
/// `merge_ranges` stays aligned.
#[test]
fn ranges_decomposition_survives_an_unrelated_earlier_insertion() {
    let (before, after, ast) = diff_ast(
        "fn main() {\n    let short = 1;\n    let long_identifier_name = 5;\n}",
        "fn main() {\n    let inserted_line = 0;\n    let short = 1;\n    \
         let long_identifier_nome = 5;\n}",
    );
    let node_cache = NodeCache::build(&before, &after);

    // `MINIMAL`: the narrow split is what this guards, and `FULL` paints the rename whole.
    let text_diff =
        TextDiff::from_with_options(&before, &after, &ast, &node_cache, RenderOptions::MINIMAL);
    let before_ranges = text_diff.all(0);
    let after_ranges = text_diff.all(1);

    let before_updates: Vec<_> = before_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
    let after_updates: Vec<_> = after_ranges
        .iter()
        .filter(|r| r.operation == TextOperation::Update)
        .collect();
    assert_eq!(before_updates.len(), 1, "{before_ranges:?}");
    assert_eq!(after_updates.len(), 1, "{after_ranges:?}");
    assert_eq!(
        before_updates[0].source.end_column - before_updates[0].source.start_column,
        1
    );
    assert_eq!(
        after_updates[0].source.end_column - after_updates[0].source.start_column,
        1
    );
}
/// A phrase appended inside a string literal is an `Insert`: one side's middle is empty, so
/// nothing was replaced.
#[test]
fn a_phrase_added_inside_a_string_renders_as_an_insert_not_an_update() {
    let before = Code::from_string(
        "let s = \"Fetch user data\";\n",
        &crate::code::Language::Rust,
    );
    let after = Code::from_string(
        "let s = \"Fetch user data from API\";\n",
        &crate::code::Language::Rust,
    );
    let diff = crate::diff::diff_code(&before, &after);
    let ast = diff.ast.as_ref().expect("an AST diff");
    let node_cache = NodeCache::build(&before, &after);
    let text_diff = TextDiff::from(&before, &after, ast, &node_cache);

    let ops: Vec<TextOperation> = text_diff
        .all(1)
        .iter()
        .filter(|r| !r.source.is_empty())
        .map(|r| r.operation.clone())
        .collect();
    assert!(
        ops.contains(&TextOperation::Insert),
        "the added words should read as an insertion, got {ops:?}"
    );
    assert!(
        !ops.contains(&TextOperation::Update),
        "and nothing here was replaced, so nothing should be an update: {ops:?}"
    );
}

/// `IntBox` -> `Box` leaves an empty middle too, but an identifier is a name: painters call it a
/// rename, not the deletion of `Int`.
#[test]
fn a_renamed_identifier_stays_an_update_even_though_one_side_is_empty() {
    let before = Code::from_string("struct IntBox;\n", &crate::code::Language::Rust);
    let after = Code::from_string("struct Box;\n", &crate::code::Language::Rust);
    let diff = crate::diff::diff_code(&before, &after);
    let ast = diff.ast.as_ref().expect("an AST diff");
    let node_cache = NodeCache::build(&before, &after);
    let text_diff = TextDiff::from(&before, &after, ast, &node_cache);

    let ops: Vec<TextOperation> = text_diff
        .all(0)
        .iter()
        .filter(|r| !r.source.is_empty())
        .map(|r| r.operation.clone())
        .collect();
    assert!(
        !ops.contains(&TextOperation::Delete),
        "a rename is not a deletion of the dropped prefix: {ops:?}"
    );
}

/// A row carrying two edits, one on each side of an untouched node (`cpp-add-const-correctness`).
/// The node is in neither common affix; `node_uniquely_placed_on_its_row` reads it as untouched
/// because its text occurs once on each row, at its own columns. `FULL` still paints it.
#[test]
fn paint_displaced_moves_gates_a_node_between_two_edits_on_its_own_row() {
    let before = Code::from_string(
        "void printVector(std::vector<int> vec) {\n}\n",
        &crate::code::Language::CPP,
    );
    let after = Code::from_string(
        "void printVector(const std::vector<int>& vec) {\n}\n",
        &crate::code::Language::CPP,
    );
    let ast = crate::diff::diff_code(&before, &after);
    let node_cache = crate::diff::NodeCache::build(&before, &after);

    let type_is_identical = |options| {
        TextDiff::from_with_options(
            &before,
            &after,
            ast.ast.as_ref().unwrap(),
            &node_cache,
            options,
        )
        .all(0)
        .iter()
        .any(|r| {
            r.operation == TextOperation::Identical
                && r.source.start_row == 0
                && r.source.start_column == 17
        })
    };

    assert!(
        type_is_identical(RenderOptions::MINIMAL),
        "the type neither edit touched must not paint Move under MINIMAL"
    );
    assert!(
        !type_is_identical(RenderOptions::FULL),
        "FULL still paints a displaced node, the same split paint_displaced_moves already makes"
    );
}

/// A row rewritten around its one surviving fragment (`typescript-async-await`): `function
/// fetchData(` is unique on both rows, but there is no common suffix, so it still paints `Move`.
#[test]
fn a_row_rewritten_around_its_one_surviving_fragment_still_paints_move() {
    let before = Code::from_string(
        "function fetchData(callback: (data: string) => void): void {\n    return;\n}\n",
        &crate::code::Language::TypeScript,
    );
    let after = Code::from_string(
        "async function fetchData(): Promise<string> {\n    return;\n}\n",
        &crate::code::Language::TypeScript,
    );
    let ast = crate::diff::diff_code(&before, &after);
    let node_cache = crate::diff::NodeCache::build(&before, &after);

    assert!(
        !TextDiff::from_with_options(
            &before,
            &after,
            ast.ast.as_ref().unwrap(),
            &node_cache,
            RenderOptions::MINIMAL,
        )
        .all(0)
        .iter()
        .any(|r| {
            r.operation == TextOperation::Identical
                && r.source.start_row == 0
                && r.source.start_column == 0
        }),
        "a fragment of a rewritten row is relocated, not displaced"
    );
}

/// Text that repeats on its row declines: "it stayed" and "it is the other one" both fit.
#[test]
fn a_node_whose_text_repeats_on_its_row_is_not_read_as_displaced() {
    let source_row: Vec<char> = "let a = count + count + one;".chars().collect();
    let destination_row: Vec<char> = "let a = count + two + count + one;".chars().collect();
    // The first `count`, at its own columns on both sides.
    let s = text_range_on(0, 8, 13);
    let d = text_range_on(0, 8, 13);
    assert!(!node_uniquely_placed_on_its_row(
        &source_row,
        &destination_row,
        &s,
        &d
    ));
}

/// `é` and `è` share their first UTF-8 byte, but no character: the common affixes stay on char
/// boundaries of both strings, so slicing at them never panics.
#[test]
fn common_affixes_never_split_a_multibyte_character() {
    assert_eq!(common_prefix_byte_len("é", "è"), 0);
    assert_eq!(common_suffix_byte_len("aé", "bé"), "é".len());
    assert_eq!(common_prefix_byte_len("xé", "xé!"), "xé".len());
}

/// Hashed lines carry no identity that survives relocation, so a moved line is a delete plus an
/// insert, as in `diff -u`.
#[test]
fn plain_text_line_diff_never_reports_a_move() {
    let (before, after) = plain_text_line_diff("moved line\na\nb\nc\n", "a\nb\nc\nmoved line\n");
    assert_eq!(
        changed_row_spans(&before),
        vec![(TextOperation::Delete, 0, 1)]
    );
    assert_eq!(
        changed_row_spans(&after),
        vec![(TextOperation::Insert, 3, 4)]
    );
}

/// `myers_lcs` can leave two identical lines unmatched (reordered duplicates); pairing them as a
/// rewrite would claim a match it deliberately did not make.
#[test]
fn shared_affix_declines_two_identical_lines() {
    assert_eq!(shared_affix("same line", "same line"), None);
    assert_eq!(shared_affix("same line", "same lime"), Some((7, 1)));
}

/// A range through the end of a file with no final newline ends one row past the last line, and
/// the trailing trim steps back onto that line rather than dropping the range.
#[test]
fn trailing_trim_keeps_a_range_that_runs_off_a_file_with_no_final_newline() {
    let lines: Vec<&str> = "a\nfoo()".split('\n').collect();
    assert_eq!(
        trim_trailing_whitespace(&lines, &TextRange::new(1, 0, 2, 0)),
        Some(TextRange::new(1, 0, 1, 5))
    );
}

/// Under `FULL`, a wholly new line's insertion grows back over its indentation, even when the
/// matched range before it ends at column 0 of that line.
#[test]
fn full_grows_an_insert_over_the_indentation_of_its_own_line() {
    let source = "fn f() {\n    new();\n}\n";
    let ranges = [
        RangeMatch {
            source: TextRange::new(0, 0, 1, 0),
            destination: TextRange::new(0, 0, 1, 0),
            operation: TextOperation::Identical,
        },
        RangeMatch {
            source: TextRange::new(1, 4, 1, 10),
            destination: TextRange::zero(),
            operation: TextOperation::Insert,
        },
    ];
    let painted = ranges_for_options(&ranges, source, RenderOptions::FULL);
    assert_eq!(painted[1].source, TextRange::new(1, 0, 1, 10));
}

/// Indentation in front of an insertion on a row that also holds matched content belongs to that
/// content, so the insertion does not grow over it (`const ` before a moved declaration).
#[test]
fn full_does_not_grow_an_insert_over_indentation_a_matched_range_shares() {
    let source = "    const int x\n";
    let ranges = [
        RangeMatch {
            source: TextRange::new(0, 4, 0, 10),
            destination: TextRange::zero(),
            operation: TextOperation::Insert,
        },
        RangeMatch {
            source: TextRange::new(0, 10, 0, 15),
            destination: TextRange::new(0, 4, 0, 9),
            operation: TextOperation::Move,
        },
    ];
    let painted = ranges_for_options(&ranges, source, RenderOptions::FULL);
    assert_eq!(painted[0].source, TextRange::new(0, 4, 0, 9));
}

#[test]
fn bracket_pair_partners_skips_a_mismatched_bracket() {
    assert!(bracket_pair_partners("(]").is_empty());
    assert_eq!(bracket_pair_partners("[()]").get(&0), Some(&3));
}

#[test]
fn toggling_an_out_of_range_option_is_a_no_op() {
    let mut options = RenderOptions::FULL;
    options.toggle(options.options().len());
    assert_eq!(options, RenderOptions::FULL);
}

#[test]
fn every_member_of_an_identical_group_paints_as_a_move_even_in_place() {
    use crate::code::Language;
    use crate::diff::{ASTMapping, ASTMappingReason};

    let before = Code::from_string("fn main() {\n    foo();\n}\n", &Language::Rust);
    let after = Code::from_string("fn main() {\n    foo();\n    foo();\n}\n", &Language::Rust);
    let statements = |code: &Code| -> Vec<usize> {
        let root = code.ast.as_ref().unwrap().root_node();
        let body = root.child(0).unwrap().child_by_field_name("body").unwrap();
        let mut cursor = body.walk();
        body.named_children(&mut cursor)
            .map(|node| node.id())
            .collect()
    };
    let mut diff = ASTDiff::default();
    // One call became two: the first copy is still in place, the second is new text.
    diff.add_group(
        &statements(&before),
        &statements(&after),
        ASTMapping::identical(ASTMappingReason::IdenticalHash),
    );
    let node_cache = NodeCache::build(&before, &after);

    for options in [RenderOptions::MINIMAL, RenderOptions::FULL] {
        let text = TextDiff::from_with_options(&before, &after, &diff, &node_cache, options);
        let moved_rows = |side: usize| -> Vec<usize> {
            text.all(side)
                .iter()
                .filter(|range| range.operation == TextOperation::Move)
                .map(|range| range.source.start_row)
                .collect()
        };
        assert_eq!(moved_rows(0), vec![1], "{options:?}");
        assert_eq!(moved_rows(1), vec![1, 2], "{options:?}");
    }
}