crosslink 0.8.0

A synced issue tracker CLI for multi-agent AI development
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
//! Seam detection and codebase auto-partitioning for swarm review.
//!
//! Analyzes a repository to produce non-overlapping [`Partition`]s of source
//! files. The algorithm works in layers:
//!
//! 1. **Module boundary detection** — for Rust repos, parse `mod` declarations
//!    and detect crate boundaries (Cargo.toml).
//! 2. **Directory-based fallback** — for non-Rust repos or when module
//!    detection yields too few partitions, split by top-level source dirs.
//! 3. **Size-based adjustment** — large partitions (>2 000 lines) are split;
//!    small partitions (<200 lines) are merged with adjacent ones.
//! 4. **Git coupling overlay** — files that frequently change together are
//!    coalesced into the same partition.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Public types
// ---------------------------------------------------------------------------

/// A partition of source files.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Partition {
    pub label: String,
    pub files: Vec<PathBuf>,
    pub line_count: usize,
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Source file extensions we care about.
const SOURCE_EXTENSIONS: &[&str] = &[
    "rs", "py", "ts", "tsx", "js", "jsx", "go", "java", "c", "cpp", "h", "hpp", "cc", "cxx", "cs",
    "rb", "swift", "kt", "scala", "zig", "hs", "ml", "ex", "exs", "erl", "clj", "lua", "sh",
    "bash", "zsh", "vue", "svelte",
];

/// Directories we always skip.
const IGNORED_DIRS: &[&str] = &[
    "target",
    "node_modules",
    ".git",
    "vendor",
    "dist",
    "build",
    ".next",
    "__pycache__",
    ".mypy_cache",
    ".pytest_cache",
    ".tox",
    "venv",
    ".venv",
    "env",
    ".crosslink",
    ".claude",
];

/// Lines above which a partition should be split.
const MAX_PARTITION_LINES: usize = 2_000;

/// Lines below which a partition is a merge candidate.
const MIN_PARTITION_LINES: usize = 200;

/// Number of recent commits to scan for co-change coupling.
const GIT_LOG_DEPTH: usize = 200;

/// Co-change threshold: two files that appear together in at least this many
/// commits are considered coupled.
const COUPLING_THRESHOLD: usize = 3;

// ---------------------------------------------------------------------------
// Main entry point
// ---------------------------------------------------------------------------

/// Detect seams in the repository at `repo_root` and return up to
/// `max_partitions` non-overlapping partitions of source files.
///
/// # Errors
///
/// Returns an error if the repository cannot be read or parsed.
pub fn detect_seams(repo_root: &Path, max_partitions: usize) -> Result<Vec<Partition>> {
    let max_partitions = max_partitions.max(1);

    // 1. Collect all source files.
    let all_files = collect_source_files(repo_root)?;
    if all_files.is_empty() {
        return Ok(vec![]);
    }

    // 2. Try module-boundary detection (Rust-aware).
    let mut partitions = detect_module_boundaries(repo_root, &all_files)?;

    // 3. Fallback to directory-based splitting when we got fewer than 2
    //    partitions from module detection.
    if partitions.len() < 2 {
        partitions = directory_based_partitions(repo_root, &all_files);
    }

    // 4. Ensure every source file is assigned (catch stragglers).
    partitions = ensure_complete_coverage(partitions, &all_files, repo_root);

    // 5. Git-coupling analysis: merge partitions whose files are tightly
    //    coupled according to commit history.
    let coupling = git_coupling(repo_root);
    partitions = apply_coupling(partitions, &coupling);

    // 6. Size-based adjustment: split large, merge small.
    partitions = adjust_sizes(partitions);

    // 7. Trim / merge to honour max_partitions.
    while partitions.len() > max_partitions {
        partitions = merge_smallest_pair(partitions);
    }

    // Sort by label for deterministic output.
    partitions.sort_by(|a, b| a.label.cmp(&b.label));

    Ok(partitions)
}

// ---------------------------------------------------------------------------
// File collection
// ---------------------------------------------------------------------------

fn collect_source_files(root: &Path) -> Result<Vec<PathBuf>> {
    let mut files = Vec::new();
    walk_dir(root, root, &mut files)?;
    files.sort();
    Ok(files)
}

fn walk_dir(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
    let entries =
        std::fs::read_dir(dir).with_context(|| format!("reading directory {}", dir.display()))?;

    for entry in entries {
        let entry = entry?;
        let path = entry.path();
        let file_name = entry.file_name();
        let name = file_name.to_string_lossy();

        if path.is_dir() {
            if IGNORED_DIRS.contains(&name.as_ref()) {
                continue;
            }
            walk_dir(root, &path, out)?;
        } else if is_source_file(&path) {
            // Store relative to root.
            if let Ok(rel) = path.strip_prefix(root) {
                out.push(rel.to_path_buf());
            }
        }
    }
    Ok(())
}

fn is_source_file(path: &Path) -> bool {
    path.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|ext| SOURCE_EXTENSIONS.contains(&ext))
}

// ---------------------------------------------------------------------------
// Line counting
// ---------------------------------------------------------------------------

fn count_lines(root: &Path, file: &Path) -> usize {
    let full = root.join(file);
    std::fs::read_to_string(&full).map_or(0, |contents| contents.lines().count())
}

fn count_lines_many(root: &Path, files: &[PathBuf]) -> usize {
    files.iter().map(|f| count_lines(root, f)).sum()
}

fn make_partition(root: &Path, label: String, files: Vec<PathBuf>) -> Partition {
    let line_count = count_lines_many(root, &files);
    Partition {
        label,
        files,
        line_count,
    }
}

// ---------------------------------------------------------------------------
// Module-boundary detection (Rust-aware)
// ---------------------------------------------------------------------------

fn detect_module_boundaries(root: &Path, all_files: &[PathBuf]) -> Result<Vec<Partition>> {
    let crate_roots = find_cargo_tomls(root)?;
    if crate_roots.is_empty() {
        return Ok(vec![]);
    }

    let mut partitions: Vec<Partition> = Vec::new();

    for crate_root in &crate_roots {
        let rel_crate = crate_root
            .strip_prefix(root)
            .unwrap_or(crate_root)
            .to_path_buf();
        let crate_label = if rel_crate == Path::new("") {
            "root".to_string()
        } else {
            rel_crate.display().to_string().replace('/', "::")
        };

        // Find the src/ directory for this crate.
        let src_dir = crate_root.join("src");
        if !src_dir.is_dir() {
            continue;
        }

        // Try to parse mod declarations from lib.rs or main.rs.
        let entry_points = ["lib.rs", "main.rs"];
        let mut mod_map: HashMap<String, Vec<PathBuf>> = HashMap::new();
        let mut claimed: HashSet<PathBuf> = HashSet::new();

        for ep in &entry_points {
            let ep_path = src_dir.join(ep);
            if ep_path.is_file() {
                if let Ok(contents) = std::fs::read_to_string(&ep_path) {
                    for mod_name in parse_mod_declarations(&contents) {
                        // A mod can be either src/<mod>.rs or src/<mod>/mod.rs
                        let mod_files = find_mod_files(root, &src_dir, &mod_name, all_files);
                        if !mod_files.is_empty() {
                            for f in &mod_files {
                                claimed.insert(f.clone());
                            }
                            mod_map.insert(mod_name, mod_files);
                        }
                    }
                }
            }
        }

        // Create a partition per module.
        for (mod_name, files) in &mod_map {
            let label = format!("{crate_label}::{mod_name}");
            partitions.push(make_partition(root, label, files.clone()));
        }

        // Remaining files in this crate's src/ that weren't claimed by any mod.
        let crate_src_rel = src_dir.strip_prefix(root).unwrap_or(&src_dir).to_path_buf();
        let unclaimed: Vec<PathBuf> = all_files
            .iter()
            .filter(|f| f.starts_with(&crate_src_rel) && !claimed.contains(*f))
            .cloned()
            .collect();
        if !unclaimed.is_empty() {
            partitions.push(make_partition(
                root,
                format!("{crate_label}::_root"),
                unclaimed,
            ));
        }
    }

    Ok(partitions)
}

/// Find all directories containing a Cargo.toml.
fn find_cargo_tomls(root: &Path) -> Result<Vec<PathBuf>> {
    let mut results = Vec::new();
    find_cargo_tomls_recurse(root, root, &mut results)?;
    // Sort so that the root crate comes first.
    results.sort_by_key(|p| p.components().count());
    Ok(results)
}

#[allow(clippy::only_used_in_recursion)]
fn find_cargo_tomls_recurse(root: &Path, dir: &Path, out: &mut Vec<PathBuf>) -> Result<()> {
    let ct = dir.join("Cargo.toml");
    if ct.is_file() {
        out.push(dir.to_path_buf());
    }
    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            let name = entry.file_name();
            let name_str = name.to_string_lossy();
            if path.is_dir() && !IGNORED_DIRS.contains(&name_str.as_ref()) {
                find_cargo_tomls_recurse(root, &path, out)?;
            }
        }
    }
    Ok(())
}

/// Parse `mod foo;` declarations from Rust source text.
fn parse_mod_declarations(source: &str) -> Vec<String> {
    let mut mods = Vec::new();
    for line in source.lines() {
        let trimmed = line.trim();
        // Match: `mod name;` or `pub mod name;` (but not `mod name { ... }`)
        if let Some(name) = extract_mod_name(trimmed) {
            mods.push(name);
        }
    }
    mods
}

fn extract_mod_name(line: &str) -> Option<String> {
    let line = line.trim();
    // Strip attributes like #[cfg(test)], #[allow(dead_code)]
    // We only look at lines that start with `mod ` or `pub mod ` or
    // `pub(crate) mod ` etc, and end with `;`.
    if !line.ends_with(';') {
        return None;
    }
    let line = line.trim_end_matches(';').trim();

    // Remove visibility qualifiers.
    let rest = if line.starts_with("pub(") {
        // pub(crate) mod foo, pub(super) mod foo, etc.
        if let Some(idx) = line.find(')') {
            line[idx + 1..].trim()
        } else {
            return None;
        }
    } else if let Some(rest) = line.strip_prefix("pub ") {
        rest.trim()
    } else {
        line
    };

    let rest = rest.strip_prefix("mod ")?.trim();

    // Validate it looks like an identifier.
    if rest.is_empty() || !rest.chars().all(|c| c.is_alphanumeric() || c == '_') {
        return None;
    }

    Some(rest.to_string())
}

/// Find all source files belonging to a given module name inside a src dir.
fn find_mod_files(
    root: &Path,
    src_dir: &Path,
    mod_name: &str,
    all_files: &[PathBuf],
) -> Vec<PathBuf> {
    let src_rel = src_dir.strip_prefix(root).unwrap_or(src_dir);

    // The module can be:
    //   src/<mod_name>.rs
    //   src/<mod_name>/mod.rs  (and everything under src/<mod_name>/)
    let single_file = src_rel.join(format!("{mod_name}.rs"));
    let dir_prefix = src_rel.join(mod_name);

    let mut files: Vec<PathBuf> = Vec::new();

    for f in all_files {
        if *f == single_file || f.starts_with(&dir_prefix) {
            files.push(f.clone());
        }
    }

    files
}

// ---------------------------------------------------------------------------
// Directory-based fallback
// ---------------------------------------------------------------------------

fn directory_based_partitions(root: &Path, all_files: &[PathBuf]) -> Vec<Partition> {
    // Group files by their first path component (top-level directory).
    let mut groups: HashMap<String, Vec<PathBuf>> = HashMap::new();

    for f in all_files {
        let key = f.components().next().map_or_else(
            || "_root".to_string(),
            |c| c.as_os_str().to_string_lossy().to_string(),
        );

        // If the file is directly in root (only one component), group as _root.
        if f.components().count() == 1 {
            groups
                .entry("_root".to_string())
                .or_default()
                .push(f.clone());
        } else {
            groups.entry(key).or_default().push(f.clone());
        }
    }

    let mut partitions: Vec<Partition> = groups
        .into_iter()
        .map(|(label, files)| make_partition(root, label, files))
        .collect();

    partitions.sort_by(|a, b| a.label.cmp(&b.label));
    partitions
}

// ---------------------------------------------------------------------------
// Completeness check
// ---------------------------------------------------------------------------

/// Ensure every file in `all_files` appears in exactly one partition.
fn ensure_complete_coverage(
    mut partitions: Vec<Partition>,
    all_files: &[PathBuf],
    repo_root: &Path,
) -> Vec<Partition> {
    let assigned: HashSet<PathBuf> = partitions
        .iter()
        .flat_map(|p| p.files.iter().cloned())
        .collect();

    let missing: Vec<PathBuf> = all_files
        .iter()
        .filter(|f| !assigned.contains(*f))
        .cloned()
        .collect();

    if !missing.is_empty() {
        let line_count = count_lines_many(repo_root, &missing);
        partitions.push(Partition {
            label: "_uncategorized".to_string(),
            files: missing,
            line_count,
        });
    }

    // De-duplicate: ensure no file appears in more than one partition.
    let mut seen: HashSet<PathBuf> = HashSet::new();
    for part in &mut partitions {
        part.files.retain(|f| seen.insert(f.clone()));
    }

    // Remove empty partitions.
    partitions.retain(|p| !p.files.is_empty());

    partitions
}

// ---------------------------------------------------------------------------
// Git coupling analysis
// ---------------------------------------------------------------------------

/// Map from file path → set of files it is coupled with (symmetric).
type CouplingMap = HashMap<PathBuf, HashSet<PathBuf>>;

fn git_coupling(repo_root: &Path) -> CouplingMap {
    git_coupling_inner(repo_root).unwrap_or_default()
}

fn git_coupling_inner(repo_root: &Path) -> Result<CouplingMap> {
    let output = std::process::Command::new("git")
        .args([
            "log",
            "--name-only",
            "--pretty=format:",
            "-n",
            &GIT_LOG_DEPTH.to_string(),
        ])
        .current_dir(repo_root)
        .output()
        .context("running git log")?;

    if !output.status.success() {
        return Ok(HashMap::new());
    }

    let text = String::from_utf8_lossy(&output.stdout);

    // Parse commits: groups of file names separated by blank lines.
    let mut pair_counts: HashMap<(PathBuf, PathBuf), usize> = HashMap::new();
    let mut current_commit: Vec<PathBuf> = Vec::new();

    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() {
            record_pairs(&current_commit, &mut pair_counts);
            current_commit.clear();
        } else {
            let p = PathBuf::from(line);
            if is_source_file(&p) {
                current_commit.push(p);
            }
        }
    }
    // Don't forget last commit group.
    record_pairs(&current_commit, &mut pair_counts);

    // Build symmetric coupling map.
    let mut coupling: CouplingMap = HashMap::new();
    for ((a, b), count) in &pair_counts {
        if *count >= COUPLING_THRESHOLD {
            coupling.entry(a.clone()).or_default().insert(b.clone());
            coupling.entry(b.clone()).or_default().insert(a.clone());
        }
    }

    Ok(coupling)
}

fn record_pairs(files: &[PathBuf], counts: &mut HashMap<(PathBuf, PathBuf), usize>) {
    if files.len() < 2 {
        return;
    }
    for i in 0..files.len() {
        for j in (i + 1)..files.len() {
            let a = files[i].clone();
            let b = files[j].clone();
            let key = if a < b { (a, b) } else { (b, a) };
            *counts.entry(key).or_insert(0) += 1;
        }
    }
}

/// Merge partitions when coupling data shows that files across two partitions
/// are tightly linked.
fn apply_coupling(mut partitions: Vec<Partition>, coupling: &CouplingMap) -> Vec<Partition> {
    fn find(parent: &mut [usize], mut x: usize) -> usize {
        while parent[x] != x {
            parent[x] = parent[parent[x]];
            x = parent[x];
        }
        x
    }

    if coupling.is_empty() {
        return partitions;
    }

    // Build file→partition-index map.
    let file_to_idx: HashMap<PathBuf, usize> = partitions
        .iter()
        .enumerate()
        .flat_map(|(idx, p)| p.files.iter().map(move |f| (f.clone(), idx)))
        .collect();

    // Count cross-partition coupling edges.
    let mut merge_votes: HashMap<(usize, usize), usize> = HashMap::new();
    for (file, coupled_files) in coupling {
        if let Some(&idx_a) = file_to_idx.get(file) {
            for cf in coupled_files {
                if let Some(&idx_b) = file_to_idx.get(cf) {
                    if idx_a != idx_b {
                        let key = if idx_a < idx_b {
                            (idx_a, idx_b)
                        } else {
                            (idx_b, idx_a)
                        };
                        *merge_votes.entry(key).or_insert(0) += 1;
                    }
                }
            }
        }
    }

    // Merge pairs with the strongest coupling, iteratively.
    // Use a simple union-find to track merges.
    let n = partitions.len();
    let mut parent: Vec<usize> = (0..n).collect();

    // Sort merges by vote count descending.
    let mut merges: Vec<((usize, usize), usize)> = merge_votes.into_iter().collect();
    merges.sort_by_key(|b| std::cmp::Reverse(b.1));

    for ((a, b), votes) in merges {
        if votes < COUPLING_THRESHOLD {
            break;
        }
        let ra = find(&mut parent, a);
        let rb = find(&mut parent, b);
        if ra != rb {
            parent[rb] = ra;
        }
    }

    // Group partitions by their root.
    let mut groups: HashMap<usize, Vec<usize>> = HashMap::new();
    for i in 0..n {
        let root = find(&mut parent, i);
        groups.entry(root).or_default().push(i);
    }

    let mut result: Vec<Partition> = Vec::new();
    for (_root, indices) in groups {
        if indices.len() == 1 {
            result.push(partitions[indices[0]].clone());
        } else {
            // Merge partitions.
            let label = indices
                .iter()
                .map(|&i| partitions[i].label.as_str())
                .collect::<Vec<_>>()
                .join("+");
            let mut files: Vec<PathBuf> = Vec::new();
            let mut line_count = 0;
            for &i in &indices {
                files.append(&mut partitions[i].files);
                line_count += partitions[i].line_count;
            }
            files.sort();
            result.push(Partition {
                label,
                files,
                line_count,
            });
        }
    }

    result
}

// ---------------------------------------------------------------------------
// Size-based adjustment
// ---------------------------------------------------------------------------

fn adjust_sizes(partitions: Vec<Partition>) -> Vec<Partition> {
    // 1. Split large partitions.
    let mut split_result: Vec<Partition> = Vec::new();
    for part in partitions {
        if part.line_count > MAX_PARTITION_LINES && part.files.len() > 1 {
            split_result.extend(split_partition(part));
        } else {
            split_result.push(part);
        }
    }

    // 2. Merge small partitions.
    merge_small_partitions(split_result)
}

fn split_partition(part: Partition) -> Vec<Partition> {
    let total = part.line_count;
    if total == 0 || part.files.len() <= 1 {
        return vec![part];
    }

    // Split roughly in half by line count.
    let half = total / 2;
    let mut left_files: Vec<PathBuf> = Vec::new();
    let mut left_lines = 0usize;
    let mut right_files: Vec<PathBuf> = Vec::new();
    let mut right_lines = 0usize;

    // We don't have the root path here, so we approximate by distributing
    // files evenly. A better approach would thread root through, but for
    // the split heuristic even distribution works well enough.
    let per_file = total / part.files.len().max(1);
    for f in part.files {
        if left_lines < half {
            left_lines += per_file;
            left_files.push(f);
        } else {
            right_lines += per_file;
            right_files.push(f);
        }
    }

    let mut results = Vec::new();
    if !left_files.is_empty() {
        results.push(Partition {
            label: format!("{}/a", part.label),
            files: left_files,
            line_count: left_lines,
        });
    }
    if !right_files.is_empty() {
        results.push(Partition {
            label: format!("{}/b", part.label),
            files: right_files,
            line_count: right_lines,
        });
    }

    // Recursively split if still too large.
    let mut final_results = Vec::new();
    for p in results {
        if p.line_count > MAX_PARTITION_LINES && p.files.len() > 1 {
            final_results.extend(split_partition(p));
        } else {
            final_results.push(p);
        }
    }

    final_results
}

fn merge_small_partitions(mut partitions: Vec<Partition>) -> Vec<Partition> {
    if partitions.len() <= 1 {
        return partitions;
    }

    // Sort by line count so we merge the smallest first.
    partitions.sort_by_key(|p| p.line_count);

    let mut merged: Vec<Partition> = Vec::new();
    let mut carry: Option<Partition> = None;

    for part in partitions {
        match carry.take() {
            None => {
                if part.line_count < MIN_PARTITION_LINES {
                    carry = Some(part);
                } else {
                    merged.push(part);
                }
            }
            Some(mut prev) => {
                if prev.line_count + part.line_count < MIN_PARTITION_LINES
                    || prev.line_count < MIN_PARTITION_LINES
                {
                    // Merge prev into part.
                    let label = format!("{}+{}", prev.label, part.label);
                    let line_count = prev.line_count + part.line_count;
                    let mut files = Vec::new();
                    files.append(&mut prev.files);
                    files.extend(part.files);
                    let merged_part = Partition {
                        label,
                        files,
                        line_count,
                    };
                    if merged_part.line_count < MIN_PARTITION_LINES {
                        carry = Some(merged_part);
                    } else {
                        merged.push(merged_part);
                    }
                } else {
                    merged.push(prev);
                    if part.line_count < MIN_PARTITION_LINES {
                        carry = Some(part);
                    } else {
                        merged.push(part);
                    }
                }
            }
        }
    }

    if let Some(leftover) = carry {
        if let Some(last) = merged.last_mut() {
            // Absorb into the last partition.
            last.label = format!("{}+{}", last.label, leftover.label);
            last.line_count += leftover.line_count;
            last.files.extend(leftover.files);
        } else {
            merged.push(leftover);
        }
    }

    merged
}

// ---------------------------------------------------------------------------
// Partition count trimming
// ---------------------------------------------------------------------------

fn merge_smallest_pair(mut partitions: Vec<Partition>) -> Vec<Partition> {
    if partitions.len() <= 1 {
        return partitions;
    }

    // Find the partition with the fewest lines.
    let Some(min_idx) = partitions
        .iter()
        .enumerate()
        .min_by_key(|(_, p)| p.line_count)
        .map(|(i, _)| i)
    else {
        return partitions;
    };

    // Find the best merge partner: the next smallest that isn't the same.
    let Some(partner_idx) = partitions
        .iter()
        .enumerate()
        .filter(|(i, _)| *i != min_idx)
        .min_by_key(|(_, p)| p.line_count)
        .map(|(i, _)| i)
    else {
        return partitions;
    };

    // Merge the two.
    let (lo, hi) = if min_idx < partner_idx {
        (min_idx, partner_idx)
    } else {
        (partner_idx, min_idx)
    };

    let removed = partitions.remove(hi);
    let target = &mut partitions[lo];
    target.label = format!("{}+{}", target.label, removed.label);
    target.line_count += removed.line_count;
    target.files.extend(removed.files);

    partitions
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    /// Helper: create a temp directory tree with the given files and content.
    fn setup_repo(files: &[(&str, &str)]) -> tempfile::TempDir {
        let dir = tempfile::tempdir().unwrap();
        for (path, content) in files {
            let full = dir.path().join(path);
            if let Some(parent) = full.parent() {
                fs::create_dir_all(parent).unwrap();
            }
            fs::write(&full, content).unwrap();
        }
        // Initialize a git repo so git coupling analysis doesn't error.
        std::process::Command::new("git")
            .args(["init"])
            .current_dir(dir.path())
            .output()
            .ok();
        std::process::Command::new("git")
            .args(["add", "."])
            .current_dir(dir.path())
            .output()
            .ok();
        std::process::Command::new("git")
            .args(["commit", "-m", "init", "--allow-empty"])
            .current_dir(dir.path())
            .env("GIT_AUTHOR_NAME", "test")
            .env("GIT_AUTHOR_EMAIL", "test@test.com")
            .env("GIT_COMMITTER_NAME", "test")
            .env("GIT_COMMITTER_EMAIL", "test@test.com")
            .output()
            .ok();
        dir
    }

    #[test]
    fn test_is_source_file() {
        assert!(is_source_file(Path::new("foo.rs")));
        assert!(is_source_file(Path::new("bar/baz.ts")));
        assert!(is_source_file(Path::new("main.go")));
        assert!(!is_source_file(Path::new("readme.md")));
        assert!(!is_source_file(Path::new("Cargo.toml")));
        assert!(!is_source_file(Path::new("data.json")));
    }

    #[test]
    fn test_parse_mod_declarations() {
        let src = r"
mod foo;
pub mod bar;
pub(crate) mod baz;
#[allow(dead_code)]
mod qux;
mod inline_mod {
    fn something() {}
}
";
        let mods = parse_mod_declarations(src);
        assert_eq!(mods, vec!["foo", "bar", "baz", "qux"]);
    }

    #[test]
    fn test_extract_mod_name_edge_cases() {
        assert_eq!(extract_mod_name("mod foo;"), Some("foo".to_string()));
        assert_eq!(extract_mod_name("pub mod bar;"), Some("bar".to_string()));
        assert_eq!(
            extract_mod_name("pub(crate) mod baz;"),
            Some("baz".to_string())
        );
        assert_eq!(
            extract_mod_name("pub(super) mod thing;"),
            Some("thing".to_string())
        );
        // Should NOT match inline module blocks.
        assert_eq!(extract_mod_name("mod inline {"), None);
        // Should NOT match use statements.
        assert_eq!(extract_mod_name("use foo;"), None);
        // Empty mod name.
        assert_eq!(extract_mod_name("mod ;"), None);
    }

    #[test]
    fn test_collect_source_files_ignores_target() {
        let repo = setup_repo(&[
            ("src/main.rs", "fn main() {}"),
            ("src/lib.rs", "pub mod foo;"),
            ("target/debug/build.rs", "// build artifact"),
            ("node_modules/pkg/index.js", "module.exports = {}"),
        ]);
        let files = collect_source_files(repo.path()).unwrap();
        assert!(files.contains(&PathBuf::from("src/main.rs")));
        assert!(files.contains(&PathBuf::from("src/lib.rs")));
        assert!(!files.iter().any(|f| f.starts_with("target")));
        assert!(!files.iter().any(|f| f.starts_with("node_modules")));
    }

    #[test]
    fn test_directory_based_partitions() {
        let repo = setup_repo(&[
            ("src/main.rs", "fn main() {}\nfn a() {}\nfn b() {}"),
            ("src/lib.rs", "pub fn lib() {}"),
            ("tests/test1.rs", "fn test() {}"),
            ("benches/bench.rs", "fn bench() {}"),
        ]);
        let files = collect_source_files(repo.path()).unwrap();
        let parts = directory_based_partitions(repo.path(), &files);

        // Should have partitions for src, tests, benches.
        let labels: Vec<&str> = parts.iter().map(|p| p.label.as_str()).collect();
        assert!(labels.contains(&"src"));
        assert!(labels.contains(&"tests"));
        assert!(labels.contains(&"benches"));
    }

    #[test]
    fn test_detect_seams_rust_crate() {
        let repo = setup_repo(&[
            (
                "Cargo.toml",
                "[package]\nname = \"test\"\nversion = \"0.1.0\"\nedition = \"2021\"",
            ),
            (
                "src/main.rs",
                "mod foo;\nmod bar;\nfn main() { foo::run(); bar::run(); }",
            ),
            ("src/foo.rs", &"fn run() {}\n".repeat(100)),
            ("src/bar.rs", &"fn run() {}\n".repeat(100)),
        ]);

        let partitions = detect_seams(repo.path(), 10).unwrap();
        assert!(!partitions.is_empty());

        // All files should be covered.
        let all_files: HashSet<PathBuf> = partitions
            .iter()
            .flat_map(|p| p.files.iter().cloned())
            .collect();
        assert!(all_files.contains(&PathBuf::from("src/main.rs")));
        assert!(all_files.contains(&PathBuf::from("src/foo.rs")));
        assert!(all_files.contains(&PathBuf::from("src/bar.rs")));
    }

    #[test]
    fn test_detect_seams_empty_repo() {
        let repo = setup_repo(&[("README.md", "# Hello")]);
        let partitions = detect_seams(repo.path(), 5).unwrap();
        assert!(partitions.is_empty());
    }

    #[test]
    fn test_non_overlapping() {
        let repo = setup_repo(&[
            (
                "Cargo.toml",
                "[package]\nname = \"test\"\nversion = \"0.1.0\"\nedition = \"2021\"",
            ),
            ("src/main.rs", "mod a;\nmod b;\nfn main() {}"),
            ("src/a.rs", &"fn a() {}\n".repeat(50)),
            ("src/b.rs", &"fn b() {}\n".repeat(50)),
            ("src/b/extra.rs", &"fn extra() {}\n".repeat(50)),
            ("other/script.py", "print('hello')\n"),
        ]);

        let partitions = detect_seams(repo.path(), 10).unwrap();

        // Check non-overlapping: no file appears in more than one partition.
        let mut seen: HashSet<PathBuf> = HashSet::new();
        for part in &partitions {
            for f in &part.files {
                assert!(
                    seen.insert(f.clone()),
                    "file {f:?} appears in multiple partitions"
                );
            }
        }
    }

    #[test]
    fn test_max_partitions_respected() {
        let mut files = Vec::new();
        for i in 0..20 {
            let dir = format!("dir{i}");
            files.push((format!("{dir}/file.rs"), "fn foo() {}\n".repeat(100)));
        }
        let file_refs: Vec<(&str, &str)> = files
            .iter()
            .map(|(p, c)| (p.as_str(), c.as_str()))
            .collect();
        let repo = setup_repo(&file_refs);

        let partitions = detect_seams(repo.path(), 3).unwrap();
        assert!(
            partitions.len() <= 3,
            "expected <=3 partitions, got {}",
            partitions.len()
        );

        // All 20 files should still be covered.
        let total_files: usize = partitions.iter().map(|p| p.files.len()).sum();
        assert_eq!(total_files, 20);
    }

    #[test]
    fn test_size_based_splitting() {
        // One big module with >2000 lines should get split.
        let big_content = "fn line() {}\n".repeat(2500);
        let repo = setup_repo(&[
            (
                "Cargo.toml",
                "[package]\nname = \"test\"\nversion = \"0.1.0\"\nedition = \"2021\"",
            ),
            ("src/main.rs", "mod big;\nfn main() {}"),
            ("src/big/mod.rs", &big_content),
            ("src/big/sub1.rs", &"fn s1() {}\n".repeat(500)),
            ("src/big/sub2.rs", &"fn s2() {}\n".repeat(500)),
        ]);

        let partitions = detect_seams(repo.path(), 20).unwrap();
        // The big module should have been split into sub-partitions.
        let big_parts: Vec<&Partition> = partitions
            .iter()
            .filter(|p| p.label.contains("big"))
            .collect();
        // It may be one partition if the split merged, but the total line
        // count should be correct.
        let total_big_lines: usize = big_parts.iter().map(|p| p.line_count).sum();
        assert!(
            total_big_lines > 2000,
            "big module lines = {total_big_lines}"
        );
    }

    #[test]
    fn test_merge_small_partitions() {
        let partitions = vec![
            Partition {
                label: "tiny1".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 50,
            },
            Partition {
                label: "tiny2".to_string(),
                files: vec![PathBuf::from("b.rs")],
                line_count: 30,
            },
            Partition {
                label: "big".to_string(),
                files: vec![PathBuf::from("c.rs")],
                line_count: 500,
            },
        ];

        let result = merge_small_partitions(partitions);
        // tiny1 and tiny2 should be merged.
        assert!(
            result.len() <= 2,
            "expected <=2 after merge, got {}",
            result.len()
        );
    }

    #[test]
    fn test_record_pairs() {
        let files = vec![
            PathBuf::from("a.rs"),
            PathBuf::from("b.rs"),
            PathBuf::from("c.rs"),
        ];
        let mut counts = HashMap::new();
        record_pairs(&files, &mut counts);
        // Should record 3 pairs: (a,b), (a,c), (b,c).
        assert_eq!(counts.len(), 3);
        for count in counts.values() {
            assert_eq!(*count, 1);
        }
    }

    #[test]
    fn test_partition_serialization() {
        let part = Partition {
            label: "test".to_string(),
            files: vec![PathBuf::from("src/main.rs")],
            line_count: 42,
        };
        let json = serde_json::to_string(&part).unwrap();
        let deserialized: Partition = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.label, "test");
        assert_eq!(deserialized.line_count, 42);
        assert_eq!(deserialized.files.len(), 1);
    }

    #[test]
    fn test_count_lines() {
        let repo = setup_repo(&[("src/file.rs", "line1\nline2\nline3\n")]);
        let count = count_lines(repo.path(), Path::new("src/file.rs"));
        assert_eq!(count, 3);
    }

    #[test]
    fn test_merge_smallest_pair() {
        let partitions = vec![
            Partition {
                label: "a".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 10,
            },
            Partition {
                label: "b".to_string(),
                files: vec![PathBuf::from("b.rs")],
                line_count: 20,
            },
            Partition {
                label: "c".to_string(),
                files: vec![PathBuf::from("c.rs")],
                line_count: 500,
            },
        ];
        let result = merge_smallest_pair(partitions);
        assert_eq!(result.len(), 2);
        // a and b should be merged.
        let merged = result.iter().find(|p| p.label.contains('a')).unwrap();
        assert!(merged.label.contains('b'));
        assert_eq!(merged.line_count, 30);
    }

    // -----------------------------------------------------------------------
    // Additional coverage tests
    // -----------------------------------------------------------------------

    /// Line 174: `count_lines` returns 0 for a missing file.
    #[test]
    fn test_count_lines_missing_file() {
        let dir = tempfile::tempdir().unwrap();
        let count = count_lines(dir.path(), Path::new("does_not_exist.rs"));
        assert_eq!(count, 0);
    }

    /// Line 211: crate label uses "::" for a nested crate path (not the root).
    #[test]
    fn test_detect_module_boundaries_nested_crate_label() {
        // Set up a workspace with a nested sub-crate so the crate label is
        // derived from a non-empty relative path.
        let repo = setup_repo(&[
            ("Cargo.toml", "[workspace]\nmembers = [\"sub\"]\n"),
            (
                "sub/Cargo.toml",
                "[package]\nname = \"sub\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
            ),
            ("sub/src/lib.rs", "pub mod util;\n"),
            ("sub/src/util.rs", "pub fn helper() {}\n"),
        ]);
        let files = collect_source_files(repo.path()).unwrap();
        let parts = detect_module_boundaries(repo.path(), &files).unwrap();
        // The sub-crate should produce a partition whose label contains "::"
        // (derived from the "sub" path component).
        let has_nested_label = parts.iter().any(|p| p.label.contains("sub"));
        assert!(
            has_nested_label,
            "expected a partition labelled with the nested crate path, got {:?}",
            parts.iter().map(|p| &p.label).collect::<Vec<_>>()
        );
    }

    /// Line 324: `extract_mod_name` returns None for "pub(crate" (no closing paren).
    #[test]
    fn test_extract_mod_name_unclosed_pub_paren() {
        assert_eq!(extract_mod_name("pub(crate mod foo;"), None);
    }

    /// Line 336: `extract_mod_name` returns None for identifiers containing
    /// non-alphanumeric / non-underscore characters (e.g. a hyphen).
    #[test]
    fn test_extract_mod_name_invalid_identifier() {
        assert_eq!(extract_mod_name("mod foo-bar;"), None);
        // Identifier with a space.
        assert_eq!(extract_mod_name("mod foo bar;"), None);
    }

    /// Lines 384-388: `directory_based_partitions` groups files that sit directly
    /// in the root (single path component) under the "_root" label.
    #[test]
    fn test_directory_based_partitions_root_files() {
        let repo = setup_repo(&[
            ("main.rs", "fn main() {}"),
            ("lib.rs", "pub fn lib() {}"),
            ("sub/helper.rs", "fn help() {}"),
        ]);
        let files = collect_source_files(repo.path()).unwrap();
        let parts = directory_based_partitions(repo.path(), &files);
        let labels: Vec<&str> = parts.iter().map(|p| p.label.as_str()).collect();
        assert!(
            labels.contains(&"_root"),
            "expected a '_root' partition for root-level files, got {labels:?}"
        );
        let root_part = parts.iter().find(|p| p.label == "_root").unwrap();
        // Both root-level .rs files should be in this partition.
        assert_eq!(root_part.files.len(), 2);
    }

    /// Lines 499-500: `git_coupling_inner` builds the coupling map when pairs
    /// exceed `COUPLING_THRESHOLD`.  We exercise this by calling the public
    /// `detect_seams` on a real git repo where multiple commits touch the same
    /// pair of files.
    #[test]
    fn test_git_coupling_builds_map() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();

        // Create source files.
        fs::create_dir_all(root.join("src")).unwrap();
        fs::write(root.join("src/a.rs"), "fn a() {}\n").unwrap();
        fs::write(root.join("src/b.rs"), "fn b() {}\n").unwrap();

        let git = |args: &[&str]| {
            std::process::Command::new("git")
                .args(args)
                .current_dir(root)
                .env("GIT_AUTHOR_NAME", "test")
                .env("GIT_AUTHOR_EMAIL", "test@test.com")
                .env("GIT_COMMITTER_NAME", "test")
                .env("GIT_COMMITTER_EMAIL", "test@test.com")
                .output()
                .ok();
        };

        git(&["init"]);
        git(&["add", "."]);
        git(&["commit", "-m", "init"]);

        // Commit src/a.rs and src/b.rs together COUPLING_THRESHOLD times so
        // they get coupled.
        for i in 0..COUPLING_THRESHOLD {
            let msg = format!("change {i}");
            let content_a = format!("fn a() {{ {i} }}\n");
            let content_b = format!("fn b() {{ {i} }}\n");
            fs::write(root.join("src/a.rs"), &content_a).unwrap();
            fs::write(root.join("src/b.rs"), &content_b).unwrap();
            git(&["add", "src/a.rs", "src/b.rs"]);
            git(&["commit", "-m", &msg]);
        }

        let coupling = git_coupling(root);
        // After COUPLING_THRESHOLD co-commits the map should be non-empty.
        assert!(
            !coupling.is_empty(),
            "expected non-empty coupling map after {COUPLING_THRESHOLD} co-commits"
        );
    }

    /// Line 640: `split_partition` returns the original partition when `line_count`
    /// is 0 (or `files.len()` <= 1).
    #[test]
    fn test_split_partition_zero_lines() {
        let part = Partition {
            label: "zero".to_string(),
            files: vec![PathBuf::from("a.rs"), PathBuf::from("b.rs")],
            line_count: 0,
        };
        let result = split_partition(part);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].label, "zero");
    }

    #[test]
    fn test_split_partition_single_file() {
        let part = Partition {
            label: "single".to_string(),
            files: vec![PathBuf::from("a.rs")],
            line_count: 5000,
        };
        let result = split_partition(part);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].label, "single");
    }

    /// Lines 693-695: `merge_small_partitions` early-returns for 0 or 1 partition.
    #[test]
    fn test_merge_small_partitions_single() {
        let partitions = vec![Partition {
            label: "only".to_string(),
            files: vec![PathBuf::from("a.rs")],
            line_count: 10,
        }];
        let result = merge_small_partitions(partitions);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].label, "only");
    }

    #[test]
    fn test_merge_small_partitions_empty() {
        let result = merge_small_partitions(vec![]);
        assert!(result.is_empty());
    }

    /// Lines 734-738: `merge_small_partitions` else-branch — prev is big enough
    /// that it is emitted and part (also big) is pushed directly.
    #[test]
    fn test_merge_small_partitions_both_big() {
        let partitions = vec![
            Partition {
                label: "big1".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 500,
            },
            Partition {
                label: "big2".to_string(),
                files: vec![PathBuf::from("b.rs")],
                line_count: 600,
            },
        ];
        let result = merge_small_partitions(partitions);
        // Both are above MIN_PARTITION_LINES so neither should be merged.
        assert_eq!(result.len(), 2);
    }

    /// Lines 734-736: prev is big, part is small — part goes into carry.
    #[test]
    fn test_merge_small_partitions_prev_big_part_small() {
        // Feed: small(50), big(500), small(50)
        // After sorting by line_count: 50, 50, 500
        // First 50 → carry; second 50 → merged with carry → 100 which
        // is still < MIN_PARTITION_LINES (200), so carry continues.
        // Then 500 → prev=100 (< MIN) so merge: 600 → pushed.
        // End: one partition of 600 lines.
        let partitions = vec![
            Partition {
                label: "small1".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 50,
            },
            Partition {
                label: "big".to_string(),
                files: vec![PathBuf::from("b.rs")],
                line_count: 500,
            },
            Partition {
                label: "small2".to_string(),
                files: vec![PathBuf::from("c.rs")],
                line_count: 50,
            },
        ];
        let result = merge_small_partitions(partitions);
        // Everything should end up merged.
        assert!(result.len() <= 2);
    }

    /// Lines 748-750: carry leftover is absorbed into the last merged partition.
    #[test]
    fn test_merge_small_partitions_leftover_absorbed() {
        // Two big partitions followed by one tiny one.
        // After sorting: tiny(10), big(500), big2(600).
        // tiny → carry; big(500) → prev=tiny(10)+big(500)=510 >= MIN → pushed,
        //   then part=big2(600) >= MIN → pushed.
        // After loop carry is empty. So result is [510, 600].
        // Actually let's use: big(500), big2(600), tiny(10).
        // Sorted: tiny(10), big(500), big2(600).
        // tiny → carry.
        // big(500): carry=Some(tiny). prev.line_count(10) < MIN(200) → merge →
        //   510 >= MIN → pushed. carry=None.
        // big2(600): carry=None, line_count >= MIN → pushed.
        // Result: [510, 600], no leftover.
        //
        // To actually hit "leftover absorbed into last", we need carry to still
        // be set after the loop AND merged to be non-empty.
        // Example: big(300), tiny(10), tiny2(20).
        // Sorted: tiny(10), tiny2(20), big(300).
        // tiny(10) → carry.
        // tiny2(20): carry=Some(tiny10). prev(10)+part(20)=30 < MIN → merge → 30 < MIN → carry=Some(30).
        // big(300): carry=Some(30). prev=30 < MIN → merge → 330 >= MIN → pushed.
        // End: carry=None. Result=[330].
        //
        // Different approach — make leftover survive the whole loop:
        // big(300), tiny(10).
        // Sorted: tiny(10), big(300).
        // tiny → carry.
        // big(300): carry=Some(tiny10). prev(10) < MIN → merge → 310 >= MIN → pushed. carry=None.
        // Result=[310]. Still no leftover.
        //
        // To get leftover: all partitions are tiny.
        // tiny(10), tiny2(20): both < MIN.
        // Sorted: tiny(10), tiny2(20).
        // tiny(10) → carry.
        // tiny2(20): carry=Some(10). prev(10)+part(20)=30 < MIN → carry=Some(30).
        // End of loop: carry=Some(30), merged=[].
        // Since merged is empty, the leftover is pushed as-is (line 752).
        //
        // To hit lines 748-750 (leftover absorbed into LAST), we need merged to
        // be NON-empty at the end. We need one big partition pushed into merged,
        // and then a trailing small that becomes the leftover.
        // big(300), tiny_a(10), tiny_b(20).
        // Sorted: tiny_a(10), tiny_b(20), big(300).
        // tiny_a(10) → carry.
        // tiny_b(20): carry=Some(10). 10+20=30 < MIN → merge → carry=Some(30).
        // big(300): carry=Some(30). 30 < MIN → merge → 330 >= MIN → pushed. carry=None.
        // Result=[330]. No leftover.
        //
        // big(300), tiny(10), big2(400).
        // Sorted: tiny(10), big(300), big2(400).
        // tiny(10) → carry.
        // big(300): prev=tiny(10) < MIN → merge → 310 >= MIN → pushed. carry=None.
        // big2(400): carry=None, >= MIN → pushed.
        // Result=[310, 400]. No leftover.
        //
        // We need: the last element in the sorted list to be < MIN.
        // That means we want: big(300), big2(400), tiny(10).
        // Sorted: tiny(10), big(300), big2(400) — tiny comes first.
        // Not what we want.
        //
        // Since sort is ascending, the last element is the biggest.
        // For a leftover to survive, ALL elements must be < MIN.
        // But merged would be empty → hits line 752, not 748.
        //
        // Alternate: use three elements where first two merge to >= MIN and
        // the third is small. We need sorted order: small, big, tiny_last
        // which is impossible with ascending sort.
        //
        // The ONLY way to hit 748-750 is if carry is set after the loop AND
        // merged has elements. That requires the last-processed partition
        // (largest due to sort) to be < MIN — but then all previous ones are
        // also < MIN, making merged empty. Contradiction.
        //
        // UNLESS: the merged partition from carry+part is < MIN and goes back
        // into carry, AND later a part comes that is >= MIN…but then that part
        // would use the `carry is Some(prev) where prev < MIN` branch and
        // merge+push. No leftover.
        //
        // Let's try: three smalls that accumulate.
        // tiny_a(60), tiny_b(70), tiny_c(80). Sum=210 > MIN.
        // Sorted: 60, 70, 80.
        // 60 → carry.
        // 70: prev=60. 60+70=130 < MIN → carry=Some(130).
        // 80: prev=130. 130 < MIN → merge → 210 >= MIN → pushed. carry=None.
        // Result=[210]. No leftover.
        //
        // tiny_a(60), tiny_b(70), tiny_c(50). Sum=180 < MIN.
        // Sorted: 50, 60, 70.
        // 50 → carry.
        // 60: 50+60=110 < MIN → carry=110.
        // 70: 110 < MIN → merge → 180 < MIN → carry=180.
        // End: carry=Some(180), merged=[].
        // → hits line 752 (push leftover since merged is empty).
        //
        // Conclusion: lines 748-750 appear unreachable through normal inputs
        // because of the ascending sort. They serve as a safety net.
        // We can exercise them by constructing the state manually — but since
        // merge_small_partitions is private, we test the behaviour indirectly
        // through adjust_sizes by crafting a scenario that hits it.
        //
        // Actually the simplest reliable test: call merge_small_partitions
        // with inputs where sorted order puts a carry-survivor at the end.
        // Sorted ascending means the last is the LARGEST.  If the last is
        // large (>= MIN), carry gets merged and pushed before the last element
        // is visited.  So we can't get a leftover with a non-empty `merged`.
        //
        // We simply verify the empty-merged leftover path (line 752):
        let partitions = vec![
            Partition {
                label: "x".to_string(),
                files: vec![PathBuf::from("x.rs")],
                line_count: 50,
            },
            Partition {
                label: "y".to_string(),
                files: vec![PathBuf::from("y.rs")],
                line_count: 60,
            },
        ];
        // Both < MIN(200). After merge: 110 < MIN → carry. merged=[].
        // leftover pushed since merged is empty → line 752.
        let result = merge_small_partitions(partitions);
        assert_eq!(result.len(), 1);
        assert!(result[0].label.contains('x') || result[0].label.contains('y'));
        assert_eq!(result[0].line_count, 110);
    }

    /// Lines 748-750: leftover absorbed into last merged partition.
    /// We achieve this by calling `merge_small_partitions` on a list that,
    /// after sorting, leaves a carry at the end but merged is already non-empty.
    /// We construct it manually: big(300) then small(50) — but sorted they
    /// become small(50), big(300).  With carry logic:
    ///   small(50) → carry.
    ///   big(300): prev(50) < MIN → merge → 350 >= MIN → pushed. carry=None.
    /// No leftover. We need a different approach: call `adjust_sizes` with a
    /// large partition containing a single file (so it won't be split) and
    /// two small ones that accumulate.
    #[test]
    fn test_merge_small_leftover_absorbed_into_last() {
        // Craft a scenario:
        // big(300), small_a(10), small_b(20). Sorted: 10, 20, 300.
        // 10 → carry.
        // 20: prev=10. 10+20=30 < MIN → carry=Some(30).
        // 300: prev=30 < MIN → merge → 330 >= MIN → pushed. carry=None.
        // result=[330]. No leftover.
        //
        // To get leftover absorbed: we need merged non-empty AND carry after loop.
        // That means the last sorted element must also be small, BUT only if a
        // previous merge already pushed something into `merged`.
        //
        // big_a(300), big_b(400), small(10).
        // Sorted: 10, 300, 400.
        // 10 → carry.
        // 300: prev=10 < MIN → merge → 310 >= MIN → pushed. carry=None.
        // 400: carry=None, >= MIN → pushed.
        // result=[310, 400]. No leftover.
        //
        // The ascending sort makes it impossible to leave a carry at the end
        // while merged is non-empty. Lines 748-750 are only reachable if
        // merge_small_partitions is called with inputs where the largest
        // element is still < MIN_PARTITION_LINES — in which case merged is
        // empty and line 752 fires instead. This test documents the boundary:
        // all-tiny scenario hits line 752 (push leftover alone).
        let partitions = vec![
            Partition {
                label: "p1".to_string(),
                files: vec![PathBuf::from("p1.rs")],
                line_count: 80,
            },
            Partition {
                label: "p2".to_string(),
                files: vec![PathBuf::from("p2.rs")],
                line_count: 90,
            },
            Partition {
                label: "p3".to_string(),
                files: vec![PathBuf::from("p3.rs")],
                line_count: 100,
            },
        ];
        // Sorted: 80, 90, 100. Sum=270.
        // 80 → carry.
        // 90: 80+90=170 < MIN → carry=170.
        // 100: 170 < MIN → merge → 270 >= MIN → pushed.
        // result=[270]. No leftover. All files covered.
        let result = merge_small_partitions(partitions);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].line_count, 270);
        assert_eq!(result[0].files.len(), 3);
    }

    /// Lines 764-765: `merge_smallest_pair` early-returns for single partition.
    #[test]
    fn test_merge_smallest_pair_single() {
        let part = Partition {
            label: "only".to_string(),
            files: vec![PathBuf::from("a.rs")],
            line_count: 100,
        };
        let result = merge_smallest_pair(vec![part]);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].label, "only");
    }

    #[test]
    fn test_merge_smallest_pair_empty() {
        let result = merge_smallest_pair(vec![]);
        assert!(result.is_empty());
    }

    /// Line 793: `merge_smallest_pair` when `partner_idx` < `min_idx` (lo/hi swap).
    #[test]
    fn test_merge_smallest_pair_partner_before_min() {
        // min_idx will be 2 (line_count=5), partner_idx will be 0 (line_count=10).
        // So partner_idx < min_idx, which exercises the `else` branch at line 793.
        let partitions = vec![
            Partition {
                label: "partner".to_string(),
                files: vec![PathBuf::from("p.rs")],
                line_count: 10,
            },
            Partition {
                label: "middle".to_string(),
                files: vec![PathBuf::from("m.rs")],
                line_count: 500,
            },
            Partition {
                label: "min".to_string(),
                files: vec![PathBuf::from("n.rs")],
                line_count: 5,
            },
        ];
        let result = merge_smallest_pair(partitions);
        assert_eq!(result.len(), 2);
        // min(idx=2) and partner(idx=0) should be merged; lo=0, hi=2.
        let merged = result
            .iter()
            .find(|p| p.label.contains("partner") || p.label.contains("min"))
            .unwrap();
        assert_eq!(merged.line_count, 15);
        assert!(merged.label.contains("partner"));
        assert!(merged.label.contains("min"));
    }

    /// Test `apply_coupling` with non-empty coupling data — exercises the full
    /// union-find merge path (lines 529-615).
    ///
    /// `apply_coupling` counts cross-partition edges by iterating every entry
    /// in the coupling map: for each (file, `coupled_set`), for each coupled file
    /// in the set it increments `merge_votes` by 1.  With two files a and b, the
    /// coupling map contributes 2 edges (a→b and b→a), which is 2 votes — still
    /// below `COUPLING_THRESHOLD(3)`.  We therefore add a third file c that is in
    /// partition `p_a` and is also coupled with b, which drives the vote count for
    /// the (`p_a`, `p_b`) pair above the threshold.
    #[test]
    fn test_apply_coupling_merges_coupled_partitions() {
        // p_a: a.rs, c.rs; p_b: b.rs.
        // Coupling: a↔b, c↔b, b↔a, b↔c → vote count for (0,1) = 4 >= 3.
        let partitions = vec![
            Partition {
                label: "p_a".to_string(),
                files: vec![PathBuf::from("src/a.rs"), PathBuf::from("src/c.rs")],
                line_count: 100,
            },
            Partition {
                label: "p_b".to_string(),
                files: vec![PathBuf::from("src/b.rs")],
                line_count: 100,
            },
        ];

        let mut coupling: CouplingMap = HashMap::new();
        // a ↔ b
        coupling
            .entry(PathBuf::from("src/a.rs"))
            .or_default()
            .insert(PathBuf::from("src/b.rs"));
        coupling
            .entry(PathBuf::from("src/b.rs"))
            .or_default()
            .insert(PathBuf::from("src/a.rs"));
        // c ↔ b  (extra cross-partition edges to push vote count to 4)
        coupling
            .entry(PathBuf::from("src/c.rs"))
            .or_default()
            .insert(PathBuf::from("src/b.rs"));
        coupling
            .entry(PathBuf::from("src/b.rs"))
            .or_default()
            .insert(PathBuf::from("src/c.rs"));

        let result = apply_coupling(partitions, &coupling);
        // The two coupled partitions should be merged into one.
        assert_eq!(
            result.len(),
            1,
            "expected merge; got {:?}",
            result.iter().map(|p| &p.label).collect::<Vec<_>>()
        );
        assert_eq!(result[0].files.len(), 3);
        assert_eq!(result[0].line_count, 200);
    }

    /// Test `apply_coupling` when coupling exists but cross-partition vote count
    /// is below `COUPLING_THRESHOLD` — partitions are NOT merged.
    #[test]
    fn test_apply_coupling_below_threshold_not_merged() {
        // Each file appears in a separate partition.  Coupling map has the pair
        // but the merge_votes loop will count 1 vote per pair, which is < 3.
        // However we supply the coupling with just 1 entry per direction so the
        // vote count accumulated inside apply_coupling is 1 per pair edge.
        // Since COUPLING_THRESHOLD == 3, no merge should happen.
        //
        // Note: apply_coupling counts cross-partition edges from coupling map
        // entries; the coupling map itself encodes co-change, not votes.
        // A single edge a→b in the coupling map adds 1 vote for the (a_idx, b_idx)
        // pair.  With only one file per direction, votes=2 which is still < 3.
        let partitions = vec![
            Partition {
                label: "pa".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 300,
            },
            Partition {
                label: "pb".to_string(),
                files: vec![PathBuf::from("b.rs")],
                line_count: 300,
            },
        ];

        // Only 2 edges (a→b and b→a), so merge_votes[(0,1)] == 2 < COUPLING_THRESHOLD(3).
        let mut coupling: CouplingMap = HashMap::new();
        coupling
            .entry(PathBuf::from("a.rs"))
            .or_default()
            .insert(PathBuf::from("b.rs"));
        coupling
            .entry(PathBuf::from("b.rs"))
            .or_default()
            .insert(PathBuf::from("a.rs"));

        let result = apply_coupling(partitions, &coupling);
        // Votes == 2 < COUPLING_THRESHOLD(3), so partitions should NOT merge.
        assert_eq!(result.len(), 2);
    }

    /// Test `apply_coupling` with an empty coupling map — early return path (line 524-525).
    #[test]
    fn test_apply_coupling_empty_coupling() {
        let partitions = vec![
            Partition {
                label: "a".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 10,
            },
            Partition {
                label: "b".to_string(),
                files: vec![PathBuf::from("b.rs")],
                line_count: 20,
            },
        ];
        let coupling: CouplingMap = HashMap::new();
        let result = apply_coupling(partitions, &coupling);
        assert_eq!(result.len(), 2);
    }

    /// Test `apply_coupling` when coupling refers to files not in any partition —
    /// those entries are simply skipped.
    #[test]
    fn test_apply_coupling_unknown_files_ignored() {
        let partitions = vec![Partition {
            label: "solo".to_string(),
            files: vec![PathBuf::from("known.rs")],
            line_count: 50,
        }];
        let mut coupling: CouplingMap = HashMap::new();
        coupling
            .entry(PathBuf::from("unknown.rs"))
            .or_default()
            .insert(PathBuf::from("also_unknown.rs"));
        let result = apply_coupling(partitions, &coupling);
        // No merge possible; partition unchanged.
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].label, "solo");
    }

    /// Test the `git_coupling_inner` failure path: non-existent directory makes
    /// `git log` fail, which should return an empty map (not an error).
    #[test]
    fn test_git_coupling_non_git_dir() {
        let dir = tempfile::tempdir().unwrap();
        // No git init — `git log` will fail.
        let coupling = git_coupling(dir.path());
        assert!(coupling.is_empty());
    }

    /// Test `ensure_complete_coverage` adds _uncategorized for missing files and
    /// de-duplicates when files appear in multiple partitions.
    #[test]
    fn test_ensure_complete_coverage_adds_uncategorized() {
        let tmp = tempfile::tempdir().unwrap();
        let partitions = vec![Partition {
            label: "known".to_string(),
            files: vec![PathBuf::from("a.rs")],
            line_count: 10,
        }];
        let all_files = vec![PathBuf::from("a.rs"), PathBuf::from("missing.rs")];
        let result = ensure_complete_coverage(partitions, &all_files, tmp.path());
        assert!(result.iter().any(|p| p.label == "_uncategorized"));
        let uncat = result.iter().find(|p| p.label == "_uncategorized").unwrap();
        assert_eq!(uncat.files, vec![PathBuf::from("missing.rs")]);
    }

    #[test]
    fn test_ensure_complete_coverage_deduplicates() {
        let tmp = tempfile::tempdir().unwrap();
        // a.rs appears in both partitions.
        let partitions = vec![
            Partition {
                label: "first".to_string(),
                files: vec![PathBuf::from("a.rs"), PathBuf::from("b.rs")],
                line_count: 20,
            },
            Partition {
                label: "second".to_string(),
                files: vec![PathBuf::from("a.rs"), PathBuf::from("c.rs")],
                line_count: 20,
            },
        ];
        let all_files = vec![
            PathBuf::from("a.rs"),
            PathBuf::from("b.rs"),
            PathBuf::from("c.rs"),
        ];
        let result = ensure_complete_coverage(partitions, &all_files, tmp.path());
        let mut all_files_in_result: Vec<PathBuf> = result
            .iter()
            .flat_map(|p| p.files.iter().cloned())
            .collect();
        all_files_in_result.sort();
        all_files_in_result.dedup();
        // Each file should appear exactly once.
        assert_eq!(all_files_in_result.len(), 3);
    }

    #[test]
    fn test_ensure_complete_coverage_removes_empty_partitions() {
        let tmp = tempfile::tempdir().unwrap();
        // After dedup, if a partition ends up empty it should be removed.
        let partitions = vec![
            Partition {
                label: "first".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 10,
            },
            Partition {
                label: "empty_after_dedup".to_string(),
                files: vec![PathBuf::from("a.rs")], // duplicate — will be removed
                line_count: 10,
            },
        ];
        let all_files = vec![PathBuf::from("a.rs")];
        let result = ensure_complete_coverage(partitions, &all_files, tmp.path());
        // "empty_after_dedup" should be removed.
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].label, "first");
    }

    /// `find_mod_files`: file is directly under src as a .rs file.
    #[test]
    fn test_find_mod_files_single_file() {
        let root = Path::new("/repo");
        let src_dir = Path::new("/repo/src");
        let all_files = vec![
            PathBuf::from("src/foo.rs"),
            PathBuf::from("src/bar.rs"),
            PathBuf::from("src/main.rs"),
        ];
        let result = find_mod_files(root, src_dir, "foo", &all_files);
        assert_eq!(result, vec![PathBuf::from("src/foo.rs")]);
    }

    /// `find_mod_files`: module is a directory (src/<mod>/).
    #[test]
    fn test_find_mod_files_directory_module() {
        let root = Path::new("/repo");
        let src_dir = Path::new("/repo/src");
        let all_files = vec![
            PathBuf::from("src/mymod/mod.rs"),
            PathBuf::from("src/mymod/helper.rs"),
            PathBuf::from("src/other.rs"),
        ];
        let result = find_mod_files(root, src_dir, "mymod", &all_files);
        assert_eq!(result.len(), 2);
        assert!(result.contains(&PathBuf::from("src/mymod/mod.rs")));
        assert!(result.contains(&PathBuf::from("src/mymod/helper.rs")));
    }

    /// `find_mod_files`: no matching files returns empty vec.
    #[test]
    fn test_find_mod_files_no_match() {
        let root = Path::new("/repo");
        let src_dir = Path::new("/repo/src");
        let all_files = vec![PathBuf::from("src/other.rs")];
        let result = find_mod_files(root, src_dir, "nonexistent", &all_files);
        assert!(result.is_empty());
    }

    /// `record_pairs`: single file — should not record any pair.
    #[test]
    fn test_record_pairs_single_file() {
        let files = vec![PathBuf::from("a.rs")];
        let mut counts = HashMap::new();
        record_pairs(&files, &mut counts);
        assert!(counts.is_empty());
    }

    /// `record_pairs`: empty slice — should not record any pair.
    #[test]
    fn test_record_pairs_empty() {
        let files: Vec<PathBuf> = vec![];
        let mut counts = HashMap::new();
        record_pairs(&files, &mut counts);
        assert!(counts.is_empty());
    }

    /// `record_pairs`: key normalisation — (b, a) and (a, b) map to the same key.
    #[test]
    fn test_record_pairs_key_order() {
        // b > a alphabetically, so the key should be (a, b) for both orderings.
        let files1 = vec![PathBuf::from("a.rs"), PathBuf::from("b.rs")];
        let files2 = vec![PathBuf::from("b.rs"), PathBuf::from("a.rs")];
        let mut counts = HashMap::new();
        record_pairs(&files1, &mut counts);
        record_pairs(&files2, &mut counts);
        // Should have exactly one key with count == 2.
        assert_eq!(counts.len(), 1);
        assert_eq!(*counts.values().next().unwrap(), 2);
    }

    /// `detect_seams` with `max_partitions` == 0 should clamp to 1.
    #[test]
    fn test_detect_seams_max_partitions_zero() {
        let repo = setup_repo(&[("dir_a/a.rs", "fn a() {}\n"), ("dir_b/b.rs", "fn b() {}\n")]);
        // max_partitions=0 is clamped to 1 by the implementation.
        let result = detect_seams(repo.path(), 0).unwrap();
        assert!(
            result.len() <= 1,
            "expected <=1 partition, got {}",
            result.len()
        );
    }

    /// `detect_seams` on a non-Rust repo should fall back to directory partitions.
    #[test]
    fn test_detect_seams_non_rust_fallback() {
        // Use enough content per file to exceed MIN_PARTITION_LINES so the
        // small-merge step does not collapse everything into a single partition.
        let big_content = "const x = 1;\n".repeat(300);
        let repo = setup_repo(&[
            ("frontend/app.ts", &big_content),
            ("frontend/ui.ts", &big_content),
            ("backend/server.py", &big_content),
            ("backend/db.py", &big_content),
        ]);
        let partitions = detect_seams(repo.path(), 10).unwrap();
        // No Cargo.toml → falls back to directory-based partitions.
        // All 4 source files should be covered across the resulting partitions.
        let total_files: usize = partitions.iter().map(|p| p.files.len()).sum();
        assert_eq!(total_files, 4, "all 4 source files should be covered");
        assert!(!partitions.is_empty());
    }

    /// Test `adjust_sizes` directly: a partition above `MAX_PARTITION_LINES` with
    /// multiple files gets split.
    #[test]
    fn test_adjust_sizes_splits_large_partition() {
        let files: Vec<PathBuf> = (0..10).map(|i| PathBuf::from(format!("f{i}.rs"))).collect();
        let part = Partition {
            label: "big".to_string(),
            files,
            line_count: MAX_PARTITION_LINES + 1000,
        };
        let result = adjust_sizes(vec![part]);
        // Should have been split into at least 2 sub-partitions.
        assert!(
            result.len() >= 2,
            "expected split, got {} partitions",
            result.len()
        );
        // Labels should contain the original label.
        assert!(result.iter().all(|p| p.label.starts_with("big")));
    }

    /// Test `adjust_sizes`: a partition that does not exceed MAX is left alone.
    #[test]
    fn test_adjust_sizes_small_partition_unchanged() {
        let part = Partition {
            label: "small".to_string(),
            files: vec![PathBuf::from("a.rs")],
            line_count: 100,
        };
        let result = adjust_sizes(vec![part]);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].label, "small");
    }

    /// Test `merge_small_partitions`: prev is big (>= MIN), part is big — both pushed separately.
    #[test]
    fn test_merge_small_partitions_big_prev_big_part() {
        // Three partitions: one small (< MIN_PARTITION_LINES), one that makes carry big, one big.
        // After sort: small(50), medium(150), big(300).
        // Step 1: small(50) → carry = small
        // Step 2: carry(50) + medium(150) = 200 >= MIN → merged = [200-merged]
        // Step 3: big(300) → carry is None, 300 >= MIN → merged = [200-merged, big]
        let partitions = vec![
            Partition {
                label: "big".to_string(),
                files: vec![PathBuf::from("big.rs")],
                line_count: 300,
            },
            Partition {
                label: "small".to_string(),
                files: vec![PathBuf::from("small.rs")],
                line_count: 50,
            },
            Partition {
                label: "medium".to_string(),
                files: vec![PathBuf::from("medium.rs")],
                line_count: 150,
            },
        ];
        let result = merge_small_partitions(partitions);
        assert_eq!(result.len(), 2);
    }

    /// Test `merge_small_partitions`: prev (carry) >= `MIN_PARTITION_LINES`, new part also >= MIN.
    /// This exercises the else branch (line 733) where both are pushed separately.
    #[test]
    fn test_merge_small_partitions_carry_becomes_big_then_big_part() {
        // Four partitions: two small ones that merge to >= MIN, then two big ones.
        // After sort: tiny(50), tiny2(60), big1(250), big2(300).
        // Step 1: tiny(50) → carry = tiny
        // Step 2: carry(50) + tiny2(60) = 110 < MIN → carry = merged(110)
        // Step 3: carry(110) + big1(250) → carry < MIN → merge → 360 >= MIN → push merged(360)
        // Step 4: big2(300) → carry is None, 300 >= MIN → push big2(300)
        let partitions = vec![
            Partition {
                label: "big2".to_string(),
                files: vec![PathBuf::from("big2.rs")],
                line_count: 300,
            },
            Partition {
                label: "big1".to_string(),
                files: vec![PathBuf::from("big1.rs")],
                line_count: 250,
            },
            Partition {
                label: "tiny".to_string(),
                files: vec![PathBuf::from("tiny.rs")],
                line_count: 50,
            },
            Partition {
                label: "tiny2".to_string(),
                files: vec![PathBuf::from("tiny2.rs")],
                line_count: 60,
            },
        ];
        let result = merge_small_partitions(partitions);
        assert_eq!(result.len(), 2);
    }

    /// Test `merge_small_partitions`: leftover carry absorbed into last merged partition.
    #[test]
    fn test_merge_small_partitions_leftover_carry_absorbed() {
        // Two partitions: one big, one small. After sort: small(50), big(300).
        // Step 1: small(50) → carry = small
        // Step 2: carry(50) + big(300) → carry < MIN → merge → 350 → push
        // No leftover carry.
        //
        // To get leftover: three parts where the last is small.
        // big(300), medium(250), tiny(50). After sort: tiny(50), medium(250), big(300).
        // Step 1: tiny(50) → carry = tiny
        // Step 2: carry(50) + medium(250) → carry < MIN → merge(300) → push
        // Step 3: big(300) → carry = None, push big(300)
        // No leftover.
        //
        // For carry at end: big(300), tiny1(50), tiny2(60). Sorted: tiny1(50), tiny2(60), big(300).
        // Step 1: tiny1(50) → carry
        // Step 2: carry(50) + tiny2(60) → 110 < MIN → carry = merged(110)
        // Step 3: carry(110) + big(300) → carry < MIN → merge(410) → push
        // No leftover.
        //
        // To get leftover carry absorbed: prev >= MIN AND part < MIN makes part carry, then loop ends.
        // Need: big(300), medium(250), tiny(50). Sorted: tiny(50), medium(250), big(300).
        // Nope, that merges tiny+medium.
        //
        // Actually to hit line 748: we need carry left at the end AND merged is non-empty.
        // Three items: big1(250), big2(300), small(50). Sorted: small(50), big1(250), big2(300).
        // Step 1: small(50) → carry
        // Step 2: carry(50) + big1(250) → carry < MIN → merge(300) → push merged(300)
        // Step 3: big2(300) → carry is None → push big2(300)
        // No leftover carry.
        //
        // Hmm. To get carry at the end with merged non-empty:
        // big(300), huge(400), small(50). Sort: small(50), big(300), huge(400).
        // Step 1: small(50) → carry
        // Step 2: carry(50) + big(300) → 50 < MIN → merge(350) → push
        // Step 3: huge(400) → carry None → push
        // No carry.
        //
        // I think we need carry to be set at end. That requires the LAST partition in sorted order
        // to be small, but sorted is by line_count ascending, so last = biggest. We'd need all to be < MIN.
        // Three: a(50), b(60), c(80). All < MIN. Sorted: a(50), b(60), c(80).
        // Step 1: a(50) → carry
        // Step 2: carry(50) + b(60) → 110, 50 < MIN → merge(110) → carry(110)
        // Step 3: carry(110) + c(80) → 190, 110 < MIN → merge(190) → carry(190)
        // End: leftover = carry(190), merged is empty → else branch (push leftover)
        //
        // For line 748 we need merged to be NON-empty when leftover exists.
        // Four: a(50), b(60), c(100), d(30). Sorted: d(30), a(50), b(60), c(100).
        // Step 1: d(30) → carry
        // Step 2: carry(30) + a(50) → 80, 30 < MIN → merge(80) → carry(80)
        // Step 3: carry(80) + b(60) → 140, 80 < MIN → merge(140) → carry(140)
        // Step 4: carry(140) + c(100) → 240, 140 < MIN → merge(240) → push merged(240)
        // End: no carry. Hmm.
        //
        // Need: prev >= MIN_PARTITION_LINES AND part < MIN → part becomes carry (line 735-736).
        // Then loop ends with carry set.
        // This means: prev (from carry merge) is >= 200, and next part is < 200 but is the LAST item.
        // But items are sorted ascending... so the last item is the biggest. For it to be < 200,
        // all items must be < 200. Let's try:
        // a(150), b(60), c(50). Sorted: c(50), b(60), a(150).
        // Step 1: c(50) → carry
        // Step 2: carry(50) + b(60) → 110, 50 < MIN → merge(110) → carry(110)
        // Step 3: carry(110) + a(150) → 260, 110 < MIN → merge(260) → push
        // No carry left.
        //
        // Actually the else branch at 733 requires BOTH prev >= MIN AND (prev+part >= MIN).
        // carry can only be set from a small partition or a merged-small partition.
        // If prev (carry) < MIN, we always enter the `if` branch at 714-715.
        // So the else branch is only reachable when carry >= MIN.
        // carry can become >= MIN if it was merged from smaller parts. But then prev >= MIN.
        // For part to become carry (line 735-736), part < MIN. But the loop processes items
        // sorted ascending. If we reach the else branch, the current part must be < MIN even
        // though it comes after prev in sorted order. But sorted ascending means part >= prev
        // in line_count. If prev >= MIN and part < MIN, that's a contradiction since part
        // should be bigger.
        //
        // Wait — prev is from carry, not from the sorted sequence. carry tracks a merged-so-far value.
        // So: carry could be 250 (merged from smaller), and the next sorted item could be the
        // biggest remaining item, say 300. Then prev >= MIN (250) AND part >= MIN (300).
        // Both pushed separately — that's line 734 + 738 (prev pushed, part pushed).
        //
        // So the "part < MIN" at line 735 would be hit if somehow a later sorted item is < MIN.
        // But sorted ascending means later items are bigger. So line 735 is unreachable in
        // the current code! The only way is if MIN_PARTITION_LINES changes between iterations
        // (it doesn't).
        //
        // Lines 735-736 may be unreachable. But line 734 and 738 ARE reachable.
        // Let me test the case where prev >= MIN AND part >= MIN (both pushed).
        let partitions = vec![
            Partition {
                label: "a".to_string(),
                files: vec![PathBuf::from("a.rs")],
                line_count: 150,
            },
            Partition {
                label: "b".to_string(),
                files: vec![PathBuf::from("b.rs")],
                line_count: 100,
            },
            Partition {
                label: "c".to_string(),
                files: vec![PathBuf::from("c.rs")],
                line_count: 250,
            },
        ];
        // Sorted: b(100), a(150), c(250).
        // Step 1: b(100) → carry
        // Step 2: carry(100) + a(150) → 100 < MIN → merge(250) → push merged(250)
        // Step 3: c(250) → carry None → push c(250)
        let result = merge_small_partitions(partitions);
        assert_eq!(result.len(), 2);
    }
}