arbor-graph 3.1.0

Graph schema and relationship tracking for Arbor
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
//! Graph builder for constructing the code graph from parsed nodes.
//!
//! Two-pass construction:
//!   1. Add all nodes — populates symbol table and import map
//!   2. Resolve edges — uses import context to create accurate edges

use crate::edge::{Edge, EdgeKind};
use crate::graph::{ArborGraph, NodeId};
use crate::symbol_table::SymbolTable;
use arbor_core::{
    clean_type_name, field_type_ref, return_type_ref, type_relation_ref, typed_receiver, CodeNode,
    NodeKind, TypeRelationKind,
};
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::path::{Path, PathBuf};
use tracing::debug;

/// Beyond this many candidate definitions, a bare name carries no information
/// and linking to all of them would swamp the graph with noise.
const MAX_AMBIGUOUS_FANOUT: usize = 4;

/// Tighter fan-out cap for calls whose receiver type is unknown.
const MAX_UNKNOWN_RECEIVER_FANOUT: usize = 2;

/// Confidence multiplier for a call on a receiver we could not type
/// (`userService.findOne()`). The method name is real evidence, but which
/// `findOne` it reaches is a guess.
const UNKNOWN_RECEIVER_PENALTY: f32 = 0.55;

/// Confidence for a call whose receiver's type was read from the code
/// (`draft: &mut Draft`). Inferred, so a little below a written path.
const TYPED_RECEIVER_CONFIDENCE: f32 = 0.9;

/// How a call on a typed receiver (`.run_started@AppState.draft`) resolved.
enum TypedReceiver {
    /// The method on the receiver's type, or on each implementor of a trait.
    Found(Vec<NodeId>),
    /// The type isn't defined in this project (`reqwest::Client`): the
    /// method is external too, and must not bind to a same-named project one.
    External,
    /// The type is known but its method isn't (a trait default elsewhere, a
    /// `Deref` target, a struct from a macro). Fall back to the method name.
    Unknown,
}

/// Builds an ArborGraph from parsed code nodes.
pub struct GraphBuilder {
    graph: ArborGraph,
    symbol_table: SymbolTable,
    name_to_id: HashMap<String, String>,

    /// Per-file map of locally-bound name → source module specifier.
    /// Built from Import nodes that carry their imported names in `references`.
    ///
    /// Example:
    ///   `import { validate } from '@babel/types'`
    ///   → import_map["file.ts"]["validate"] = "@babel/types"
    ///
    /// Used during edge resolution to verify that a direct call like `validate()`
    /// is indeed an intentional import, not a same-name coincidence.
    import_map: HashMap<String, HashMap<String, String>>,

    /// Namespace import aliases: file → alias → source module.
    ///
    /// Example:
    ///   `import * as types from '@babel/types'`
    ///   → namespace_imports["file.ts"]["types"] = "@babel/types"
    ///
    /// Used to resolve calls like `types.validate()` — though since we now DROP
    /// those calls at parse time, this is reserved for future use when we add
    /// a richer call-site representation.
    namespace_imports: HashMap<String, HashMap<String, String>>,
}

impl Default for GraphBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl GraphBuilder {
    pub fn new() -> Self {
        Self {
            graph: ArborGraph::new(),
            symbol_table: SymbolTable::new(),
            name_to_id: HashMap::new(),
            import_map: HashMap::new(),
            namespace_imports: HashMap::new(),
        }
    }

    /// Adds nodes from a parsed file to the graph.
    ///
    /// This pass does two things:
    ///   - Adds real code entities (functions, classes, etc.) to the graph and symbol table
    ///   - Processes Import nodes to build the per-file import map
    ///
    /// Call this for all files before calling `build()`.
    pub fn add_nodes(&mut self, nodes: Vec<CodeNode>) {
        for node in nodes {
            // Import nodes carry the import map — process them but don't add to graph
            // (they are not code entities we want in centrality analysis)
            if node.kind == NodeKind::Import {
                let file = node.file.clone();
                let module = node.name.clone();

                for imported_name in &node.references {
                    if let Some(alias) = imported_name.strip_prefix("*as:") {
                        // `import * as alias from 'module'`
                        self.namespace_imports
                            .entry(file.clone())
                            .or_default()
                            .insert(alias.to_string(), module.clone());
                    } else {
                        // `import { name } from 'module'` or `import DefaultName from 'module'`
                        self.import_map
                            .entry(file.clone())
                            .or_default()
                            .insert(imported_name.clone(), module.clone());
                    }
                }
                // Import nodes are intentionally NOT added to the graph.
                // They caused misleading centrality scores (e.g. "psycopg.types.range [Import]
                // 330 callers") because every call to a symbol named after the import path
                // was attributed to the import node itself.
                continue;
            }

            let id_str = node.id.clone();
            let name = node.name.clone();
            let qualified = node.qualified_name.clone();
            let file = PathBuf::from(&node.file);

            let node_idx = self.graph.add_node(node);

            if !qualified.is_empty() {
                self.symbol_table
                    .insert(qualified.clone(), node_idx, file.clone());
            }

            self.name_to_id.insert(name.clone(), id_str.clone());
            self.name_to_id.insert(qualified, id_str);
        }
    }

    /// Resolves references into actual graph edges.
    ///
    /// Each reference `R` from file `F` resolves through
    /// [`SymbolTable::resolve_ref`], which reports *how* it matched. That
    /// quality becomes the edge's confidence, so a downstream consumer can
    /// distinguish a proven call from an educated guess instead of treating
    /// every edge as fact.
    ///
    /// Ambiguous references (several equally-plausible definitions) produce a
    /// low-confidence edge to each candidate, capped at
    /// [`MAX_AMBIGUOUS_FANOUT`]. Dropping them outright would silently hide
    /// real call paths; emitting them unlabelled would overstate certainty.
    ///
    /// References with no definition in the repository are dropped — they are
    /// external or stdlib symbols.
    pub fn resolve_edges(&mut self) {
        let mut edges_to_add: Vec<(NodeId, NodeId, f32)> = Vec::new();

        let node_indices: Vec<NodeId> = self.graph.node_indexes().collect();

        for from_idx in node_indices {
            let (references, from_file) = {
                let node = self.graph.get(from_idx).unwrap();
                (node.references.clone(), PathBuf::from(&node.file))
            };

            let from_file_str = from_file.to_string_lossy().to_string();

            for reference in references {
                // Inheritance is not a call. `extends:` / `implements:` and
                // `self.method` / `super.method` are resolved in
                // [`resolve_inheritance`](Self::resolve_inheritance), where an
                // override can hide a base method. Falling through to a bare
                // name here would attach the edge to the wrong definition.
                if type_relation_ref(&reference).is_some()
                    || receiver_method(&reference).is_some()
                    || field_type_ref(&reference).is_some()
                    || return_type_ref(&reference).is_some()
                {
                    continue;
                }

                // `.run_started@AppState.draft`: the receiver's type is known,
                // so the call reaches that type's method and nothing else.
                if let Some((method, receiver)) = typed_receiver(&reference) {
                    match self.resolve_typed_receiver(method, receiver, &from_file) {
                        TypedReceiver::Found(targets) => {
                            for to_idx in targets {
                                if to_idx != from_idx {
                                    edges_to_add.push((
                                        from_idx,
                                        to_idx,
                                        TYPED_RECEIVER_CONFIDENCE,
                                    ));
                                }
                            }
                            continue;
                        }
                        TypedReceiver::External => continue,
                        TypedReceiver::Unknown => {}
                    }
                }

                // A leading `.` marks a call on a receiver whose type we could
                // not determine (`userService.findOne()`). Resolve it by method
                // name, but never let it claim the confidence of a real match.
                let (lookup, receiver_unknown) = match typed_receiver(&reference) {
                    Some((method, _)) => (method, true),
                    None => match reference.strip_prefix('.') {
                        Some(method) => (method, true),
                        None => (reference.as_str(), false),
                    },
                };

                if lookup.is_empty() {
                    continue;
                }

                // Hand the referencing file's imports to the resolver so a bare
                // name that matches several modules can be pinned to the one
                // the author actually imported. Without this the resolver
                // falls through to SameDir and attaches the edge to whichever
                // definition happens to sit in the caller's own directory.
                let file_imports = self.import_map.get(&from_file_str);
                // `jobs::enqueue` / `Provider::new` name where the definition
                // lives; matching only the last segment would drop the call or
                // bind it to a same-named function in the wrong module.
                let resolution = if !receiver_unknown && lookup.contains("::") {
                    self.symbol_table.resolve_path(lookup, &from_file)
                } else {
                    self.symbol_table
                        .resolve_ref_with_imports(lookup, &from_file, file_imports)
                };

                if !resolution.is_resolved() {
                    // The overwhelming majority of references are stdlib or
                    // third-party and have no definition here. Expected, not
                    // notable.
                    debug!(
                        "Unresolved reference '{}' in {} (likely external/stdlib)",
                        reference,
                        from_file.display()
                    );
                    continue;
                }

                let mut base_confidence = resolution.confidence();
                if receiver_unknown {
                    base_confidence *= UNKNOWN_RECEIVER_PENALTY;
                }
                let mut candidates = resolution.candidates();
                // In Rust `x.f()` is always a method call (modules use `::`),
                // so it can't reach a free function that shares the name:
                // sqlx's `.bind(...)` is not a test helper called `bind`.
                if receiver_unknown && from_file_str.ends_with(".rs") {
                    candidates.retain(|id| {
                        self.graph
                            .get(*id)
                            .is_some_and(|n| n.kind == NodeKind::Method)
                    });
                    if candidates.is_empty() {
                        continue;
                    }
                }

                // A receiver-unknown call to a name shared by many symbols
                // (`get`, `run`, `execute`) is pure noise; hold it to a tighter
                // fan-out than a properly-qualified reference.
                let fanout_limit = if receiver_unknown {
                    MAX_UNKNOWN_RECEIVER_FANOUT
                } else {
                    MAX_AMBIGUOUS_FANOUT
                };

                if candidates.len() > fanout_limit {
                    // A name like `new`, `get`, or `run` with dozens of
                    // definitions carries no information. Linking to all of
                    // them would swamp the graph.
                    // Routine on any real codebase — `.get`, `.encode`, `.match`
                    // are shared by many types. Diagnostic, not a user problem.
                    debug!(
                        "Reference '{}' in {} has {} candidate definitions — too ambiguous to link",
                        reference,
                        from_file.display(),
                        candidates.len()
                    );
                    continue;
                }

                for to_idx in candidates {
                    if from_idx == to_idx {
                        continue;
                    }

                    // Method names are never in the import map — importing
                    // `UserService` does not import `findOne` — so import
                    // validation only applies to bare and qualified references.
                    let confidence = if receiver_unknown {
                        base_confidence
                    } else {
                        self.apply_import_validation(
                            &reference,
                            &from_file,
                            &from_file_str,
                            to_idx,
                            base_confidence,
                        )
                    };

                    if confidence <= 0.0 {
                        continue;
                    }

                    edges_to_add.push((from_idx, to_idx, confidence));
                }
            }
        }

        for (from_id, to_id, confidence) in edges_to_add {
            self.graph.add_edge(
                from_id,
                to_id,
                Edge::new(EdgeKind::Calls).with_confidence(confidence),
            );
        }
    }

    /// Downgrades (rather than drops) an edge whose name is not imported.
    ///
    /// If the referencing file declares imports and this bare name is not among
    /// them, a cross-module match is probably a same-name coincidence. The old
    /// behaviour dropped such edges unless the target was in the same directory
    /// — which meant that in a flat `src/` layout, the common case for JS and
    /// Python projects, the check never fired at all. Confidence-weighting
    /// applies the same suspicion uniformly instead of exempting whole layouts.
    ///
    /// Returns `0.0` to drop the edge entirely.
    fn apply_import_validation(
        &self,
        reference: &str,
        from_file: &Path,
        from_file_str: &str,
        to_idx: NodeId,
        base_confidence: f32,
    ) -> f32 {
        let Some(file_imports) = self.import_map.get(from_file_str) else {
            return base_confidence;
        };
        if file_imports.is_empty() || reference.contains('.') || reference.contains("::") {
            return base_confidence;
        }
        if file_imports.contains_key(reference) {
            // Explicitly imported — the strongest possible corroboration.
            return base_confidence.max(0.95);
        }

        let Some(to_node) = self.graph.get(to_idx) else {
            return 0.0;
        };
        let to_file = PathBuf::from(&to_node.file);

        if to_file == from_file {
            // Defined in this very file; imports are irrelevant.
            return base_confidence;
        }

        if to_file.parent() == from_file.parent() {
            // Same directory but never imported. Plausible in languages with
            // implicit module scope, suspicious everywhere else.
            return base_confidence * 0.6;
        }

        // Different module and not imported: almost certainly a name collision.
        debug!(
            "Downgrading unimported cross-module reference '{}' in {} → {}",
            reference,
            from_file.display(),
            to_file.display()
        );
        base_confidence * 0.3
    }

    /// Emits `extends` / `implements` edges and inherited-method reachability.
    ///
    /// A subclass reaches each base it names. A method the subclass does not
    /// define is reachable from the subclass; an override blocks that path.
    /// `self`/`this` calls bind to the nearest definition, including the
    /// enclosing type. `super`/`base` calls start at the parents, so an
    /// override that calls `super` still reaches the base method.
    ///
    /// These type edges are not `Calls`. PageRank stays on the call graph.
    fn resolve_inheritance(&mut self) {
        let indices: Vec<NodeId> = self.graph.node_indexes().collect();
        let types = inherited_types(&self.graph, &indices);
        let (methods_of, method_owner) = index_methods(&self.graph, &indices, &types);
        let mut pending = Vec::new();
        let parents = self.link_type_parents(&types, &mut pending);
        link_inherited_methods(&types, &parents, &methods_of, &mut pending);
        link_receiver_calls(
            &self.graph,
            &indices,
            &method_owner,
            &parents,
            &methods_of,
            &mut pending,
        );
        self.commit_inheritance_edges(pending);
    }

    /// Resolves each written base to a type node and records the parent list.
    fn link_type_parents(
        &self,
        types: &[InheritedType],
        pending: &mut Vec<PendingEdge>,
    ) -> HashMap<NodeId, Vec<NodeId>> {
        let mut parents: HashMap<NodeId, Vec<NodeId>> = HashMap::new();
        for ty in types {
            let file = PathBuf::from(&ty.file);
            let mut resolved = Vec::new();
            let relations = ty
                .extends
                .iter()
                .map(|name| (TypeRelationKind::Extends, name))
                .chain(
                    ty.implements
                        .iter()
                        .map(|name| (TypeRelationKind::Implements, name)),
                );
            for (recorded, name) in relations {
                let Some(target) = self.resolve_type_name(name, &file) else {
                    continue;
                };
                if target == ty.id || resolved.contains(&target) {
                    continue;
                }
                resolved.push(target);
                pending.push(type_edge(self, ty, recorded, target));
            }
            if !resolved.is_empty() {
                parents.insert(ty.id, resolved);
            }
        }
        parents
    }

    fn commit_inheritance_edges(&mut self, pending: Vec<PendingEdge>) {
        let mut seen: HashSet<(NodeId, NodeId, u8)> = HashSet::new();
        for (from, to, kind, confidence) in pending {
            if from == to || !seen.insert((from, to, edge_tag(kind))) {
                continue;
            }
            self.graph
                .add_edge(from, to, Edge::new(kind).with_confidence(confidence));
        }
    }

    /// Resolves a method call whose receiver type path the extractor read from
    /// the code: `Draft`, `AppState.draft` for a field of an `AppState`, or
    /// `prepare()` / `mac::Draft::capture().inner()` for what calls return.
    fn resolve_typed_receiver(&self, method: &str, receiver: &str, file: &Path) -> TypedReceiver {
        let mut segments = receiver.split('.');
        let Some(base) = segments.next() else {
            return TypedReceiver::Unknown;
        };
        let mut current = match base.strip_suffix("()") {
            Some(callee) => match self.returned_type(self.callable(callee, file)) {
                Ok(id) => id,
                Err(outcome) => return outcome,
            },
            None => match self.type_named(base, file) {
                Ok(id) => id,
                Err(outcome) => return outcome,
            },
        };
        for segment in segments {
            let Some(node) = self.graph.get(current) else {
                return TypedReceiver::Unknown;
            };
            let owner_file = PathBuf::from(&node.file);
            if let Some(called) = segment.strip_suffix("()") {
                // `draft.inner().run()`: whatever `Draft::inner` returns.
                let path = format!("{}::{called}", node.name);
                current = match self.returned_type(self.callable(&path, &owner_file)) {
                    Ok(id) => id,
                    Err(outcome) => return outcome,
                };
                continue;
            }
            let field_type = node
                .references
                .iter()
                .filter_map(|reference| field_type_ref(reference))
                .find(|(name, _)| *name == segment)
                .map(|(_, type_name)| type_name.to_string());
            let Some(field_type) = field_type else {
                return TypedReceiver::Unknown;
            };
            current = match self.type_named(&field_type, &owner_file) {
                Ok(id) => id,
                Err(outcome) => return outcome,
            };
        }

        let Some(owner) = self.graph.get(current) else {
            return TypedReceiver::Unknown;
        };
        let owner_file = PathBuf::from(&owner.file);
        let mut owners = vec![owner.name.clone()];
        if owner.kind == NodeKind::Interface {
            // `obs: &mut dyn RunObserver`: any implementor's method may run.
            owners.extend(self.implementors(&owner.name, &owner_file));
        } else {
            // A method from a trait the type implements (a default body).
            owners.extend(
                owner.references.iter().filter_map(|reference| {
                    match type_relation_ref(reference) {
                        Some((TypeRelationKind::Implements, name)) => Some(name.to_string()),
                        _ => None,
                    }
                }),
            );
        }
        let mut targets = Vec::new();
        for owner_name in owners {
            let resolution = self
                .symbol_table
                .resolve_path(&format!("{owner_name}::{method}"), &owner_file);
            for id in resolution.candidates() {
                let is_method = self
                    .graph
                    .get(id)
                    .is_some_and(|n| matches!(n.kind, NodeKind::Method | NodeKind::Function));
                if is_method && !targets.contains(&id) {
                    targets.push(id);
                }
            }
        }
        if targets.is_empty() || targets.len() > MAX_AMBIGUOUS_FANOUT {
            TypedReceiver::Unknown
        } else {
            TypedReceiver::Found(targets)
        }
    }

    /// One type node for `name`, or why there isn't one.
    fn type_named(&self, name: &str, file: &Path) -> Result<NodeId, TypedReceiver> {
        match self.resolve_type_name(name, file) {
            Some(id) => Ok(id),
            None if self.defines_type(name, file) => Err(TypedReceiver::Unknown),
            None => Err(TypedReceiver::External),
        }
    }

    /// The functions or methods a call path names: `prepare`, `drafts::load`,
    /// `Draft::capture`.
    fn callable(&self, path: &str, file: &Path) -> Vec<NodeId> {
        let resolution = if path.contains("::") {
            self.symbol_table.resolve_path(path, file)
        } else {
            let imports = self.import_map.get(&file.to_string_lossy().to_string());
            self.symbol_table
                .resolve_ref_with_imports(path, file, imports)
        };
        resolution
            .candidates()
            .into_iter()
            .filter(|id| {
                self.graph
                    .get(*id)
                    .is_some_and(|n| matches!(n.kind, NodeKind::Function | NodeKind::Method))
            })
            .collect()
    }

    /// The type node the given functions return, when they agree on one.
    fn returned_type(&self, functions: Vec<NodeId>) -> Result<NodeId, TypedReceiver> {
        let mut returned: Option<(String, PathBuf)> = None;
        for id in functions {
            let Some(function) = self.graph.get(id) else {
                continue;
            };
            let Some(type_name) = function.references.iter().find_map(|r| return_type_ref(r))
            else {
                // Unit, a primitive, a tuple or an unbounded generic.
                return Err(TypedReceiver::Unknown);
            };
            match &returned {
                Some((seen, _)) if seen != type_name => return Err(TypedReceiver::Unknown),
                Some(_) => {}
                None => returned = Some((type_name.to_string(), PathBuf::from(&function.file))),
            }
        }
        // No project function by that name: an external call, whose result
        // may still be a project type (`serde_json::from_str::<Draft>`).
        let Some((type_name, file)) = returned else {
            return Err(TypedReceiver::Unknown);
        };
        self.type_named(&type_name, &file)
    }

    /// Whether any type in the file's language is called `name`, even if the
    /// name is too ambiguous to pick one.
    fn defines_type(&self, name: &str, file: &Path) -> bool {
        self.symbol_table
            .resolve_ref_with_imports(name, file, None)
            .candidates()
            .into_iter()
            .any(|id| {
                self.graph
                    .get(id)
                    .is_some_and(|node| is_inheritable_type(node.kind) && node.name == name)
            })
    }

    /// Names of the types in `file`'s language that implement trait `name`.
    fn implementors(&self, name: &str, file: &Path) -> Vec<String> {
        let mut found = Vec::new();
        for idx in self.graph.node_indexes() {
            let Some(node) = self.graph.get(idx) else {
                continue;
            };
            let implements = node.references.iter().any(|reference| {
                matches!(type_relation_ref(reference),
                    Some((TypeRelationKind::Implements, target)) if target == name)
            });
            if implements
                && crate::symbol_table::same_language(file, Path::new(&node.file))
                && !found.contains(&node.name)
            {
                found.push(node.name.clone());
            }
        }
        found
    }

    /// Resolves a base or interface name to one type node.
    ///
    /// A path like `pkg.Base` is tried whole, then as its last segment, so a
    /// class recorded under the simple name `Base` still matches. Several
    /// equally plausible types are dropped rather than guessed.
    fn resolve_type_name(&self, name: &str, file: &Path) -> Option<NodeId> {
        let file_str = file.to_string_lossy().to_string();
        let imports = self.import_map.get(&file_str);
        let mut candidates_names = vec![name.to_string()];
        if let Some(last) = name.rsplit(['.', ':']).find(|segment| !segment.is_empty()) {
            if last != name {
                candidates_names.push(last.to_string());
            }
        }

        for candidate in &candidates_names {
            let resolution = self
                .symbol_table
                .resolve_ref_with_imports(candidate, file, imports);
            let type_ids: Vec<NodeId> = resolution
                .candidates()
                .into_iter()
                .filter(|id| {
                    self.graph
                        .get(*id)
                        .is_some_and(|node| is_inheritable_type(node.kind))
                })
                .collect();
            if type_ids.len() == 1 {
                return Some(type_ids[0]);
            }
            let same_file: Vec<NodeId> = type_ids
                .into_iter()
                .filter(|id| {
                    self.graph
                        .get(*id)
                        .is_some_and(|node| Path::new(&node.file) == file)
                })
                .collect();
            if same_file.len() == 1 {
                return Some(same_file[0]);
            }
        }
        None
    }

    /// Finishes building and returns the graph.
    pub fn build(mut self) -> ArborGraph {
        self.resolve_edges();
        self.resolve_inheritance();
        self.graph
    }

    /// Builds without resolving edges (for incremental updates).
    pub fn build_without_resolve(self) -> ArborGraph {
        self.graph
    }
}

struct InheritedType {
    id: NodeId,
    file: String,
    /// Length of the raw qualified name. Tie-breaking uses this, not the cleaned form.
    qualified_len: usize,
    clean_name: String,
    clean_qualified: String,
    extends: Vec<String>,
    implements: Vec<String>,
}

type PendingEdge = (NodeId, NodeId, EdgeKind, f32);

fn inherited_types(graph: &ArborGraph, indices: &[NodeId]) -> Vec<InheritedType> {
    let mut types = Vec::new();
    for id in indices {
        let Some(node) = graph.get(*id) else {
            continue;
        };
        if !is_inheritable_type(node.kind) {
            continue;
        }
        let mut extends = Vec::new();
        let mut implements = Vec::new();
        for reference in &node.references {
            if let Some((kind, name)) = type_relation_ref(reference) {
                match kind {
                    TypeRelationKind::Extends => extends.push(name.to_string()),
                    TypeRelationKind::Implements => implements.push(name.to_string()),
                }
            }
        }
        types.push(InheritedType {
            id: *id,
            file: node.file.clone(),
            qualified_len: node.qualified_name.len(),
            clean_name: clean_type_name(&node.name),
            clean_qualified: clean_type_name(&node.qualified_name),
            extends,
            implements,
        });
    }
    types
}

fn index_methods(
    graph: &ArborGraph,
    indices: &[NodeId],
    types: &[InheritedType],
) -> (
    HashMap<NodeId, BTreeMap<String, NodeId>>,
    HashMap<NodeId, NodeId>,
) {
    let mut types_by_file: HashMap<&str, Vec<&InheritedType>> = HashMap::new();
    let mut rust_types_by_name: HashMap<&str, Vec<&InheritedType>> = HashMap::new();
    for ty in types {
        types_by_file.entry(ty.file.as_str()).or_default().push(ty);
        if ty.file.ends_with(".rs") {
            rust_types_by_name
                .entry(ty.clean_name.as_str())
                .or_default()
                .push(ty);
        }
    }

    let mut methods_of: HashMap<NodeId, BTreeMap<String, NodeId>> = HashMap::new();
    let mut method_owner: HashMap<NodeId, NodeId> = HashMap::new();
    for id in indices {
        let Some(node) = graph.get(*id) else {
            continue;
        };
        if node.kind != NodeKind::Method {
            continue;
        }
        let Some((parent, method_name)) = split_owner(&node.qualified_name) else {
            continue;
        };
        let parent = clean_type_name(parent);
        if parent.is_empty() || method_name.is_empty() {
            continue;
        }
        let file_types = types_by_file
            .get(node.file.as_str())
            .map(Vec::as_slice)
            .unwrap_or_default();
        let owners: Vec<NodeId> = file_types
            .iter()
            .filter(|ty| ty.clean_name == parent || ty.clean_qualified == parent)
            .map(|ty| ty.id)
            .collect();
        let owner = unique_owner(&owners, |id| {
            file_types
                .iter()
                .find(|ty| ty.id == id)
                .map(|ty| ty.qualified_len)
                .unwrap_or(0)
        })
        .or_else(|| {
            // A Rust `impl Provider` block may sit in any file of the crate,
            // far from `struct Provider`. Without this its methods have no
            // owner, and every `self.method()` call inside them is lost.
            // Look where Rust puts it: the impl file's own module directory
            // first, then its parents. Tests often declare their own mock
            // `struct Provider`, so a repo-wide unique name is too strict.
            if !node.file.ends_with(".rs") || !owners.is_empty() {
                return None;
            }
            let candidates = rust_types_by_name.get(parent.as_str())?;
            let mut dir = Path::new(&node.file).parent();
            while let Some(current) = dir {
                let here: Vec<NodeId> = candidates
                    .iter()
                    .filter(|ty| Path::new(&ty.file).parent() == Some(current))
                    .map(|ty| ty.id)
                    .collect();
                match here.as_slice() {
                    [] => dir = current.parent(),
                    [only] => return Some(*only),
                    _ => return None,
                }
            }
            None
        });
        let Some(owner) = owner else {
            continue;
        };
        methods_of
            .entry(owner)
            .or_default()
            .entry(method_name.to_string())
            .or_insert(*id);
        method_owner.insert(*id, owner);
    }
    (methods_of, method_owner)
}

fn type_edge(
    builder: &GraphBuilder,
    ty: &InheritedType,
    recorded: TypeRelationKind,
    target: NodeId,
) -> PendingEdge {
    let target_kind = builder.graph.get(target).map(|node| node.kind);
    let edge_kind = match (recorded, target_kind) {
        (TypeRelationKind::Implements, _) | (_, Some(NodeKind::Interface)) => EdgeKind::Implements,
        _ => EdgeKind::Extends,
    };
    let same_file = builder
        .graph
        .get(target)
        .is_some_and(|node| node.file == ty.file);
    let confidence = if same_file { 0.95 } else { 0.85 };
    (ty.id, target, edge_kind, confidence)
}

fn link_inherited_methods(
    types: &[InheritedType],
    parents: &HashMap<NodeId, Vec<NodeId>>,
    methods_of: &HashMap<NodeId, BTreeMap<String, NodeId>>,
    pending: &mut Vec<PendingEdge>,
) {
    for ty in types {
        let own = methods_of.get(&ty.id);
        for (name, method_id) in inherited_methods(ty.id, parents, methods_of) {
            if own.and_then(|methods| methods.get(&name)).is_some() {
                continue;
            }
            pending.push((ty.id, method_id, EdgeKind::References, 0.9));
        }
    }
}

fn link_receiver_calls(
    graph: &ArborGraph,
    indices: &[NodeId],
    method_owner: &HashMap<NodeId, NodeId>,
    parents: &HashMap<NodeId, Vec<NodeId>>,
    methods_of: &HashMap<NodeId, BTreeMap<String, NodeId>>,
    pending: &mut Vec<PendingEdge>,
) {
    for id in indices {
        let Some(node) = graph.get(*id) else {
            continue;
        };
        if node.kind != NodeKind::Method {
            continue;
        }
        let Some(&owner) = method_owner.get(id) else {
            continue;
        };
        for reference in &node.references {
            let Some((is_super, method_name)) = receiver_method(reference) else {
                continue;
            };
            let Some(target) = nearest_method(owner, method_name, is_super, parents, methods_of)
            else {
                continue;
            };
            if target != *id {
                pending.push((*id, target, EdgeKind::Calls, 0.95));
            }
        }
    }
}

fn is_inheritable_type(kind: NodeKind) -> bool {
    matches!(
        kind,
        NodeKind::Class | NodeKind::Struct | NodeKind::Interface | NodeKind::Enum
    )
}

/// `Class.method` or `Class::method` → `("Class", "method")`.
///
/// When both separators appear, the later one wins, so `mod.Class::method`
/// is `("mod.Class", "method")`.
fn split_owner(qualified: &str) -> Option<(&str, &str)> {
    let dot = qualified.rfind('.');
    let colon = qualified.rfind("::");
    match (dot, colon) {
        (Some(dot_at), Some(colon_at)) if colon_at > dot_at => {
            Some((&qualified[..colon_at], &qualified[colon_at + 2..]))
        }
        (Some(dot_at), _) => Some((&qualified[..dot_at], &qualified[dot_at + 1..])),
        (None, Some(colon_at)) => Some((&qualified[..colon_at], &qualified[colon_at + 2..])),
        (None, None) => None,
    }
}

fn unique_owner(owners: &[NodeId], qualified_len: impl Fn(NodeId) -> usize) -> Option<NodeId> {
    match owners {
        [] => None,
        [only] => Some(*only),
        _ => {
            let mut best_len = 0usize;
            let mut best: Option<NodeId> = None;
            let mut tied = false;
            for id in owners {
                let len = qualified_len(*id);
                if len > best_len {
                    best_len = len;
                    best = Some(*id);
                    tied = false;
                } else if len == best_len {
                    tied = true;
                }
            }
            if tied {
                None
            } else {
                best
            }
        }
    }
}

/// Breadth-first, nearest base wins. This approximates C3 linearization and
/// agrees with it for single inheritance and simple diamonds.
fn inherited_methods(
    start: NodeId,
    parents: &HashMap<NodeId, Vec<NodeId>>,
    methods_of: &HashMap<NodeId, BTreeMap<String, NodeId>>,
) -> BTreeMap<String, NodeId> {
    let mut found = BTreeMap::new();
    let mut seen = HashSet::new();
    let mut queue = VecDeque::new();
    if let Some(bases) = parents.get(&start) {
        queue.extend(bases.iter().copied());
    }
    while let Some(current) = queue.pop_front() {
        if !seen.insert(current) {
            continue;
        }
        if let Some(methods) = methods_of.get(&current) {
            for (name, id) in methods {
                found.entry(name.clone()).or_insert(*id);
            }
        }
        if let Some(bases) = parents.get(&current) {
            for base in bases {
                if !seen.contains(base) {
                    queue.push_back(*base);
                }
            }
        }
    }
    found
}

/// Same nearest-base walk as [`inherited_methods`].
fn nearest_method(
    start: NodeId,
    name: &str,
    skip_self: bool,
    parents: &HashMap<NodeId, Vec<NodeId>>,
    methods_of: &HashMap<NodeId, BTreeMap<String, NodeId>>,
) -> Option<NodeId> {
    let own = if skip_self {
        None
    } else {
        methods_of.get(&start).and_then(|methods| methods.get(name))
    };
    if let Some(id) = own {
        return Some(*id);
    }
    let mut seen = HashSet::new();
    let mut queue = VecDeque::new();
    if let Some(bases) = parents.get(&start) {
        queue.extend(bases.iter().copied());
    }
    while let Some(current) = queue.pop_front() {
        if !seen.insert(current) {
            continue;
        }
        if let Some(id) = methods_of
            .get(&current)
            .and_then(|methods| methods.get(name))
        {
            return Some(*id);
        }
        if let Some(bases) = parents.get(&current) {
            for base in bases {
                if !seen.contains(base) {
                    queue.push_back(*base);
                }
            }
        }
    }
    None
}

/// `self.compute` / `this.compute` search from the enclosing type.
/// `super().compute` / `super.compute` / `base.compute` start at the parents.
fn receiver_method(reference: &str) -> Option<(bool, &str)> {
    const SUPER_PREFIXES: &[&str] = &["super().", "super.", "base."];
    const SAME_PREFIXES: &[&str] = &["self.", "this->", "this.", "Self::"];
    let reference = reference.trim();
    for prefix in SUPER_PREFIXES {
        if let Some(method) = reference.strip_prefix(prefix) {
            if is_plain_method(method) {
                return Some((true, method));
            }
        }
    }
    for prefix in SAME_PREFIXES {
        if let Some(method) = reference.strip_prefix(prefix) {
            if is_plain_method(method) {
                return Some((false, method));
            }
        }
    }
    None
}

fn is_plain_method(name: &str) -> bool {
    !name.is_empty()
        && name
            .chars()
            .next()
            .is_some_and(|c| c.is_alphabetic() || c == '_')
        && name.chars().all(|c| c.is_alphanumeric() || c == '_')
}

fn edge_tag(kind: EdgeKind) -> u8 {
    match kind {
        EdgeKind::Calls => 1,
        EdgeKind::Extends => 2,
        EdgeKind::Implements => 3,
        EdgeKind::References => 4,
        EdgeKind::Imports => 5,
        EdgeKind::UsesType => 6,
        EdgeKind::Contains => 7,
        EdgeKind::FlowsTo => 8,
        EdgeKind::DataDependency => 9,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use arbor_core::NodeKind;
    use petgraph::visit::{EdgeRef, IntoEdgeReferences};

    #[test]
    fn test_builder_adds_nodes() {
        let mut builder = GraphBuilder::new();
        let node1 = CodeNode::new("foo", "foo", NodeKind::Function, "test.rs");
        let node2 = CodeNode::new("bar", "bar", NodeKind::Function, "test.rs");
        builder.add_nodes(vec![node1, node2]);
        let graph = builder.build();
        assert_eq!(graph.node_count(), 2);
    }

    #[test]
    fn test_builder_resolves_edges() {
        let mut builder = GraphBuilder::new();
        let caller = CodeNode::new("caller", "caller", NodeKind::Function, "test.rs")
            .with_references(vec!["callee".to_string()]);
        let callee = CodeNode::new("callee", "callee", NodeKind::Function, "test.rs");
        builder.add_nodes(vec![caller, callee]);
        let graph = builder.build();
        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);
    }

    #[test]
    fn test_cross_file_resolution() {
        let mut builder = GraphBuilder::new();
        let caller = CodeNode::new("main", "main", NodeKind::Function, "main.rs")
            .with_references(vec!["pkg.Utils.helper".to_string()]);
        let mut callee = CodeNode::new("helper", "helper", NodeKind::Method, "utils.rs");
        callee.qualified_name = "pkg.Utils.helper".to_string();
        builder.add_nodes(vec![caller]);
        builder.add_nodes(vec![callee]);
        let graph = builder.build();
        assert_eq!(graph.node_count(), 2);
        assert_eq!(
            graph.edge_count(),
            1,
            "Should resolve cross-file edge via FQN"
        );
    }

    #[test]
    fn test_unresolved_references_no_false_edges() {
        let mut builder = GraphBuilder::new();
        let node = CodeNode::new("caller", "caller", NodeKind::Function, "a.rs")
            .with_references(vec!["nonexistent_function".to_string()]);
        builder.add_nodes(vec![node]);
        let graph = builder.build();
        assert_eq!(graph.node_count(), 1);
        assert_eq!(
            graph.edge_count(),
            0,
            "Unresolved references must not create edges"
        );
    }

    #[test]
    fn test_import_nodes_not_added_to_graph() {
        let mut builder = GraphBuilder::new();
        let import_node = CodeNode::new("./utils", "./utils", NodeKind::Import, "main.ts")
            .with_references(vec!["validate".to_string()]);
        let func = CodeNode::new("main", "main", NodeKind::Function, "main.ts");
        builder.add_nodes(vec![import_node, func]);
        let graph = builder.build();
        // Only the function should be in the graph, not the import node
        assert_eq!(graph.node_count(), 1);
    }

    #[test]
    fn test_import_map_built_correctly() {
        let mut builder = GraphBuilder::new();
        let import_node =
            CodeNode::new("@babel/types", "@babel/types", NodeKind::Import, "file.ts")
                .with_references(vec!["validate".to_string(), "clone".to_string()]);
        builder.add_nodes(vec![import_node]);
        assert_eq!(
            builder
                .import_map
                .get("file.ts")
                .and_then(|m| m.get("validate")),
            Some(&"@babel/types".to_string())
        );
        assert_eq!(
            builder
                .import_map
                .get("file.ts")
                .and_then(|m| m.get("clone")),
            Some(&"@babel/types".to_string())
        );
    }

    #[test]
    fn test_namespace_import_map() {
        let mut builder = GraphBuilder::new();
        let import_node =
            CodeNode::new("@babel/types", "@babel/types", NodeKind::Import, "file.ts")
                .with_references(vec!["*as:types".to_string()]);
        builder.add_nodes(vec![import_node]);
        assert_eq!(
            builder
                .namespace_imports
                .get("file.ts")
                .and_then(|m| m.get("types")),
            Some(&"@babel/types".to_string())
        );
    }

    #[test]
    fn test_build_empty_graph() {
        let builder = GraphBuilder::new();
        let graph = builder.build();
        assert_eq!(graph.node_count(), 0);
        assert_eq!(graph.edge_count(), 0);
    }

    /// Confidence of the single edge in the graph.
    fn only_edge_confidence(graph: &ArborGraph) -> f32 {
        let edges = graph.export_edges();
        assert_eq!(edges.len(), 1, "expected exactly one edge");
        graph
            .graph
            .edge_weights()
            .next()
            .expect("edge weight")
            .confidence
    }

    #[test]
    fn exact_fqn_match_is_full_confidence() {
        let mut b = GraphBuilder::new();
        let caller = CodeNode::new("main", "main", NodeKind::Function, "main.rs")
            .with_references(vec!["pkg.Utils.helper".to_string()]);
        let mut callee = CodeNode::new("helper", "helper", NodeKind::Method, "utils.rs");
        callee.qualified_name = "pkg.Utils.helper".to_string();
        b.add_nodes(vec![caller, callee]);
        let graph = b.build();

        assert_eq!(only_edge_confidence(&graph), 1.0);
    }

    #[test]
    fn unknown_receiver_call_creates_a_weak_edge() {
        // `.findOne` — the shape the TS parser used to discard entirely.
        let mut b = GraphBuilder::new();
        let caller = CodeNode::new("handler", "handler", NodeKind::Function, "api.ts")
            .with_references(vec![".findOne".to_string()]);
        let callee = CodeNode::new("findOne", "UserRepo.findOne", NodeKind::Method, "repo.ts");
        b.add_nodes(vec![caller, callee]);
        let graph = b.build();

        let confidence = only_edge_confidence(&graph);
        assert!(
            confidence > 0.0 && confidence < Edge::CONFIDENT_THRESHOLD,
            "edge should exist but not be treated as proven, got {confidence}"
        );
    }

    /// `run_started` on three types, plus a trait two of them implement.
    fn receiver_fixture(caller_refs: &[&str]) -> ArborGraph {
        let mut b = GraphBuilder::new();
        let mut nodes = vec![
            CodeNode::new("on_key", "on_key", NodeKind::Function, "src/lib.rs")
                .with_references(caller_refs.iter().map(|r| r.to_string()).collect()),
            CodeNode::new("AppState", "AppState", NodeKind::Struct, "src/state.rs")
                .with_references(vec![arbor_core::field_ref("draft", "Draft")]),
            CodeNode::new("Draft", "Draft", NodeKind::Struct, "src/draft.rs"),
            CodeNode::new(
                "Telemetry",
                "Telemetry",
                NodeKind::Struct,
                "src/telemetry.rs",
            ),
            CodeNode::new("Logger", "Logger", NodeKind::Struct, "src/observer.rs"),
            CodeNode::new(
                "RunObserver",
                "RunObserver",
                NodeKind::Interface,
                "src/observer.rs",
            ),
        ];
        nodes[4].add_type_relation(TypeRelationKind::Implements, "RunObserver");
        nodes[3].add_type_relation(TypeRelationKind::Implements, "RunObserver");
        for (owner, file) in [
            ("Draft", "src/draft.rs"),
            ("Telemetry", "src/telemetry.rs"),
            ("Logger", "src/observer.rs"),
        ] {
            nodes.push(CodeNode::new(
                "run_started",
                format!("{owner}.run_started"),
                NodeKind::Method,
                file,
            ));
        }
        b.add_nodes(nodes);
        b.build()
    }

    fn callees(graph: &ArborGraph) -> Vec<String> {
        use petgraph::visit::EdgeRef;
        let mut names: Vec<String> = graph
            .graph
            .edge_references()
            .filter(|edge| edge.weight().kind == EdgeKind::Calls)
            .filter_map(|edge| graph.get(edge.target()).map(|n| n.qualified_name.clone()))
            .collect();
        names.sort();
        names
    }

    #[test]
    fn typed_receiver_reaches_only_its_own_type() {
        // Name-based resolution sees three `run_started` and links none.
        assert!(callees(&receiver_fixture(&[".run_started"])).is_empty());
        let graph = receiver_fixture(&[".run_started@Draft"]);
        assert_eq!(callees(&graph), ["Draft.run_started"]);
        let call = graph
            .graph
            .edge_weights()
            .find(|edge| edge.kind == EdgeKind::Calls)
            .expect("the call edge");
        assert!((call.confidence - TYPED_RECEIVER_CONFIDENCE).abs() < 1e-6);
    }

    #[test]
    fn typed_receiver_follows_fields_into_other_files() {
        let graph = receiver_fixture(&[".run_started@AppState.draft"]);
        assert_eq!(callees(&graph), ["Draft.run_started"]);
    }

    #[test]
    fn trait_typed_receiver_reaches_each_implementor() {
        let graph = receiver_fixture(&[".run_started@RunObserver"]);
        assert_eq!(
            callees(&graph),
            ["Logger.run_started", "Telemetry.run_started"]
        );
    }

    #[test]
    fn external_receiver_never_binds_to_a_project_method() {
        // `client.run_started()` on `reqwest::Client` is not Draft's method.
        assert!(callees(&receiver_fixture(&[".run_started@Client"])).is_empty());
    }

    #[test]
    fn project_type_without_the_method_falls_back_to_the_name() {
        // A field this struct doesn't declare: the type is no longer known,
        // so the call is resolved by name (here: too ambiguous to link).
        assert!(callees(&receiver_fixture(&[".run_started@AppState.missing"])).is_empty());
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            CodeNode::new("on_key", "on_key", NodeKind::Function, "src/lib.rs")
                .with_references(vec![".flush@Draft".to_string()]),
            CodeNode::new("Draft", "Draft", NodeKind::Struct, "src/draft.rs"),
            CodeNode::new("flush", "Buffer.flush", NodeKind::Method, "src/buffer.rs"),
        ]);
        let graph = b.build();
        // Draft has no `flush` (a Deref target might): a weak name-based edge.
        assert_eq!(callees(&graph), ["Buffer.flush"]);
        assert!(only_edge_confidence(&graph) < TYPED_RECEIVER_CONFIDENCE);
    }

    #[test]
    fn typed_receiver_follows_return_types() {
        let mut b = GraphBuilder::new();
        let mut prepare = CodeNode::new("prepare", "prepare", NodeKind::Function, "src/lib.rs")
            .with_references(vec![arbor_core::returns_ref("Prepared")]);
        prepare.line_end = 0;
        b.add_nodes(vec![
            CodeNode::new("on_key", "on_key", NodeKind::Function, "src/lib.rs").with_references(
                vec![
                    ".run_started@prepare()".to_string(),
                    ".flush@mac::Prepared::capture().buffer()".to_string(),
                ],
            ),
            prepare,
            CodeNode::new("Prepared", "Prepared", NodeKind::Struct, "src/mac.rs"),
            CodeNode::new("Buffer", "Buffer", NodeKind::Struct, "src/buffer.rs"),
            CodeNode::new(
                "capture",
                "Prepared.capture",
                NodeKind::Method,
                "src/mac.rs",
            )
            .with_references(vec![arbor_core::returns_ref("Prepared")]),
            CodeNode::new("buffer", "Prepared.buffer", NodeKind::Method, "src/mac.rs")
                .with_references(vec![arbor_core::returns_ref("Buffer")]),
            CodeNode::new(
                "run_started",
                "Prepared.run_started",
                NodeKind::Method,
                "src/mac.rs",
            ),
            CodeNode::new(
                "run_started",
                "Other.run_started",
                NodeKind::Method,
                "src/other.rs",
            ),
            CodeNode::new(
                "run_started",
                "Third.run_started",
                NodeKind::Method,
                "src/third.rs",
            ),
            CodeNode::new("flush", "Buffer.flush", NodeKind::Method, "src/buffer.rs"),
            CodeNode::new("flush", "Pipe.flush", NodeKind::Method, "src/pipe.rs"),
            CodeNode::new("flush", "Sink.flush", NodeKind::Method, "src/sink.rs"),
        ]);
        let graph = b.build();
        assert_eq!(callees(&graph), ["Buffer.flush", "Prepared.run_started"]);
    }

    #[test]
    fn field_types_are_not_calls() {
        assert!(
            callees(&receiver_fixture(&[])).is_empty(),
            "a field: reference is not a call"
        );
    }

    #[test]
    fn unknown_receiver_with_many_candidates_is_not_linked() {
        // `.get` defined on three different types is noise, not evidence.
        let mut b = GraphBuilder::new();
        let caller = CodeNode::new("handler", "handler", NodeKind::Function, "api.ts")
            .with_references(vec![".get".to_string()]);
        b.add_nodes(vec![caller]);
        for (i, owner) in ["Cache", "Store", "Config"].iter().enumerate() {
            b.add_nodes(vec![CodeNode::new(
                "get",
                format!("{owner}.get"),
                NodeKind::Method,
                format!("m{i}.ts"),
            )]);
        }
        let graph = b.build();

        assert_eq!(
            graph.edge_count(),
            0,
            "over the fan-out cap, so no edge should be guessed"
        );
    }

    #[test]
    fn colliding_symbols_both_remain_reachable() {
        // The old table overwrote one of these, orphaning it with zero callers.
        let mut b = GraphBuilder::new();
        let caller = CodeNode::new("main", "main", NodeKind::Function, "src/main.py")
            .with_references(vec!["process".to_string()]);
        b.add_nodes(vec![caller]);
        b.add_nodes(vec![CodeNode::new(
            "process",
            "process",
            NodeKind::Function,
            "src/utils.py",
        )]);
        b.add_nodes(vec![CodeNode::new(
            "process",
            "process",
            NodeKind::Function,
            "src/helpers.py",
        )]);
        let graph = b.build();

        // Both candidates are linked, each flagged as uncertain.
        assert_eq!(graph.edge_count(), 2);
        for weight in graph.graph.edge_weights() {
            assert!(
                !weight.is_confident(),
                "ambiguous resolution must not claim certainty"
            );
        }
    }

    #[test]
    fn unimported_cross_module_reference_is_downgraded_not_dropped() {
        let mut b = GraphBuilder::new();
        let import = CodeNode::new("./other", "./other", NodeKind::Import, "src/a/main.ts")
            .with_references(vec!["somethingElse".to_string()]);
        let caller = CodeNode::new("main", "main", NodeKind::Function, "src/a/main.ts")
            .with_references(vec!["validate".to_string()]);
        let callee = CodeNode::new("validate", "validate", NodeKind::Function, "src/z/far.ts");
        b.add_nodes(vec![import, caller, callee]);
        let graph = b.build();

        let confidence = only_edge_confidence(&graph);
        assert!(
            confidence > 0.0 && confidence < 0.5,
            "not imported and in another module — weak, got {confidence}"
        );
    }

    #[test]
    fn explicitly_imported_reference_is_strong() {
        let mut b = GraphBuilder::new();
        let import = CodeNode::new("./far", "./far", NodeKind::Import, "src/a/main.ts")
            .with_references(vec!["validate".to_string()]);
        let caller = CodeNode::new("main", "main", NodeKind::Function, "src/a/main.ts")
            .with_references(vec!["validate".to_string()]);
        let callee = CodeNode::new("validate", "validate", NodeKind::Function, "src/z/far.ts");
        b.add_nodes(vec![import, caller, callee]);
        let graph = b.build();

        assert!(
            only_edge_confidence(&graph) >= Edge::CONFIDENT_THRESHOLD,
            "an explicit import is strong corroboration"
        );
    }

    #[test]
    fn same_dir_unimported_reference_is_no_longer_a_free_pass() {
        // Flat `src/` layouts made the old filter a no-op, so same-name
        // collisions sailed through at full strength.
        let mut b = GraphBuilder::new();
        let import = CodeNode::new("./x", "./x", NodeKind::Import, "src/main.ts")
            .with_references(vec!["other".to_string()]);
        let caller = CodeNode::new("main", "main", NodeKind::Function, "src/main.ts")
            .with_references(vec!["validate".to_string()]);
        let callee = CodeNode::new("validate", "validate", NodeKind::Function, "src/sibling.ts");
        b.add_nodes(vec![import, caller, callee]);
        let graph = b.build();

        let confidence = only_edge_confidence(&graph);
        assert!(
            confidence < Edge::CONFIDENT_THRESHOLD,
            "same-dir but unimported should carry doubt, got {confidence}"
        );
    }

    #[test]
    fn build_is_deterministic_across_insertion_orders() {
        let make = |reverse: bool| {
            let mut b = GraphBuilder::new();
            let mut defs = vec![
                CodeNode::new("run", "A.run", NodeKind::Method, "src/a/a.ts"),
                CodeNode::new("run", "B.run", NodeKind::Method, "src/b/b.ts"),
            ];
            if reverse {
                defs.reverse();
            }
            b.add_nodes(vec![CodeNode::new(
                "main",
                "main",
                NodeKind::Function,
                "src/c/main.ts",
            )
            .with_references(vec!["run".to_string()])]);
            b.add_nodes(defs);
            let graph = b.build();
            let mut sig: Vec<String> = graph
                .export_edges()
                .into_iter()
                .map(|e| format!("{}->{}", e.source, e.target))
                .collect();
            sig.sort();
            sig
        };

        assert_eq!(make(false), make(true));
    }

    #[test]
    fn test_qualified_static_call_resolves_to_correct_class() {
        // Regression: `MathUtils.add()` in Calc must link to MathUtils.add, NOT to a
        // same-named `add` on a sibling class. Relies on the parser keeping the class
        // qualifier so the builder gets an exact FQN match.
        let mut b = GraphBuilder::new();
        let caller = CodeNode::new("compute", "Calc.compute", NodeKind::Method, "src/Calc.java")
            .with_references(vec!["MathUtils.add".to_string()]);
        // Same-dir sibling with a colliding bare name — the old bug linked here.
        let sibling = CodeNode::new("add", "Sibling.add", NodeKind::Method, "src/Sibling.java");
        let target = CodeNode::new(
            "add",
            "MathUtils.add",
            NodeKind::Method,
            "src/util/MathUtils.java",
        );
        b.add_nodes(vec![caller, sibling, target]);
        let graph = b.build();

        let compute_idx = graph
            .node_indexes()
            .find(|&i| graph.get(i).unwrap().name == "compute")
            .unwrap();
        let callees = graph.get_callees(compute_idx);
        assert_eq!(callees.len(), 1, "exactly one resolved callee");
        assert_eq!(
            callees[0].qualified_name, "MathUtils.add",
            "static call must resolve to the qualified class, not a same-named sibling"
        );
    }

    fn kinds_between(graph: &ArborGraph, from_q: &str, to_q: &str) -> Vec<EdgeKind> {
        let mut kinds = Vec::new();
        for edge in graph.graph.edge_references() {
            let source = graph.get(edge.source()).unwrap();
            let target = graph.get(edge.target()).unwrap();
            if source.qualified_name == from_q && target.qualified_name == to_q {
                kinds.push(edge.weight().kind);
            }
        }
        kinds
    }

    fn inheritance_fixture() -> ArborGraph {
        let file = "hard/inheritance.py";
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            CodeNode::new("Base", "Base", NodeKind::Class, file),
            CodeNode::new("compute", "Base.compute", NodeKind::Method, file),
            CodeNode::new("shared", "Base.shared", NodeKind::Method, file),
            CodeNode::new("Middle", "Middle", NodeKind::Class, file).with_extends(["Base"]),
            CodeNode::new("compute", "Middle.compute", NodeKind::Method, file)
                .with_references(vec!["super().compute".to_string()]),
            CodeNode::new("Derived", "Derived", NodeKind::Class, file).with_extends(["Middle"]),
            CodeNode::new("shared", "Derived.shared", NodeKind::Method, file),
            CodeNode::new("Leaf", "Leaf", NodeKind::Class, file).with_extends(["Derived"]),
            CodeNode::new("run", "Leaf.run", NodeKind::Method, file).with_references(vec![
                "self.compute".to_string(),
                "self.shared".to_string(),
                "len".to_string(),
            ]),
            // Same simple name, no superclass. Must not become an extender.
            CodeNode::new("UserService", "UserService", NodeKind::Class, file),
        ]);
        b.build()
    }

    #[test]
    fn subclass_extends_its_base() {
        let graph = inheritance_fixture();
        assert_eq!(
            kinds_between(&graph, "Middle", "Base"),
            vec![EdgeKind::Extends]
        );
        assert_eq!(
            kinds_between(&graph, "Derived", "Middle"),
            vec![EdgeKind::Extends]
        );
        assert_eq!(
            kinds_between(&graph, "Leaf", "Derived"),
            vec![EdgeKind::Extends]
        );
        assert!(
            kinds_between(&graph, "UserService", "Base").is_empty(),
            "a class with no base is not an extender"
        );

        let base = graph
            .node_indexes()
            .find(|&id| graph.get(id).unwrap().qualified_name == "Base")
            .unwrap();
        let reachers: Vec<_> = graph
            .direct_dependents(base)
            .into_iter()
            .map(|node| node.qualified_name.as_str())
            .collect();
        assert!(
            reachers.contains(&"Middle"),
            "Base is reached by Middle, got {reachers:?}"
        );
        assert!(graph.get_callers(base).is_empty(), "extends is not a call");
    }

    #[test]
    fn inherited_method_reaches_the_defining_class_until_overridden() {
        let graph = inheritance_fixture();

        // Leaf.run's self.compute binds to Middle.compute, not Base.compute.
        assert_eq!(
            kinds_between(&graph, "Leaf.run", "Middle.compute"),
            vec![EdgeKind::Calls]
        );
        assert!(kinds_between(&graph, "Leaf.run", "Base.compute").is_empty());

        // super() in the override still reaches the base method.
        assert_eq!(
            kinds_between(&graph, "Middle.compute", "Base.compute"),
            vec![EdgeKind::Calls]
        );

        // Leaf inherits compute from Middle (the nearest definition).
        assert_eq!(
            kinds_between(&graph, "Leaf", "Middle.compute"),
            vec![EdgeKind::References]
        );
        assert!(kinds_between(&graph, "Leaf", "Base.compute").is_empty());

        // Derived overrides shared, so neither Derived nor Leaf reaches Base.shared.
        assert!(kinds_between(&graph, "Derived", "Base.shared").is_empty());
        assert!(kinds_between(&graph, "Leaf", "Base.shared").is_empty());
        assert_eq!(
            kinds_between(&graph, "Leaf.run", "Derived.shared"),
            vec![EdgeKind::Calls]
        );
        assert!(kinds_between(&graph, "Leaf.run", "Base.shared").is_empty());

        // Middle does not override shared, so it reaches Base.shared.
        assert_eq!(
            kinds_between(&graph, "Middle", "Base.shared"),
            vec![EdgeKind::References]
        );

        let base_compute = graph
            .node_indexes()
            .find(|&id| graph.get(id).unwrap().qualified_name == "Base.compute")
            .unwrap();
        let impact = graph.analyze_impact(base_compute, 5);
        let upstream: Vec<_> = impact
            .upstream
            .iter()
            .map(|node| node.node_info.qualified_name.as_str())
            .collect();
        assert!(
            upstream.contains(&"Leaf"),
            "changing Base.compute must reach Leaf, got {upstream:?}"
        );

        let base_shared = graph
            .node_indexes()
            .find(|&id| graph.get(id).unwrap().qualified_name == "Base.shared")
            .unwrap();
        let shared_impact = graph.analyze_impact(base_shared, 5);
        let direct: Vec<_> = shared_impact
            .direct_only()
            .into_iter()
            .map(|node| node.node_info.qualified_name.clone())
            .collect();
        assert!(
            direct.iter().any(|name| name == "Middle"),
            "Middle inherits shared, got {direct:?}"
        );
        assert!(
            direct
                .iter()
                .all(|name| name != "Derived" && name != "Leaf" && name != "Leaf.run"),
            "Derived's override must not make Base.shared a direct dependency, got {direct:?}"
        );
    }

    #[test]
    fn diamond_inherits_the_nearest_override() {
        let file = "diamond.py";
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            CodeNode::new("A", "A", NodeKind::Class, file),
            CodeNode::new("f", "A.f", NodeKind::Method, file),
            CodeNode::new("B", "B", NodeKind::Class, file).with_extends(["A"]),
            CodeNode::new("C", "C", NodeKind::Class, file).with_extends(["A"]),
            CodeNode::new("f", "C.f", NodeKind::Method, file),
            CodeNode::new("D", "D", NodeKind::Class, file).with_extends(["B", "C"]),
        ]);
        let graph = b.build();
        assert_eq!(
            kinds_between(&graph, "D", "C.f"),
            vec![EdgeKind::References],
            "D(B, C) sees C.f before A.f"
        );
        assert!(kinds_between(&graph, "D", "A.f").is_empty());
    }

    #[test]
    fn super_stops_at_the_intermediate_override() {
        let file = "super_chain.py";
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            CodeNode::new("A", "A", NodeKind::Class, file),
            CodeNode::new("foo", "A.foo", NodeKind::Method, file),
            CodeNode::new("B", "B", NodeKind::Class, file).with_extends(["A"]),
            CodeNode::new("foo", "B.foo", NodeKind::Method, file),
            CodeNode::new("C", "C", NodeKind::Class, file).with_extends(["B"]),
            CodeNode::new("foo", "C.foo", NodeKind::Method, file)
                .with_references(vec!["super().foo".to_string()]),
        ]);
        let graph = b.build();
        assert_eq!(
            kinds_between(&graph, "C.foo", "B.foo"),
            vec![EdgeKind::Calls],
            "super() in C must reach B's override"
        );
        assert!(
            kinds_between(&graph, "C.foo", "A.foo").is_empty(),
            "super() must not skip B and land on A"
        );
    }
}

#[cfg(test)]
mod rust_path_tests {
    use super::*;
    use arbor_core::NodeKind;

    fn function(name: &str, file: &str, calls: &[&str]) -> CodeNode {
        CodeNode::new(name, name, NodeKind::Function, file)
            .with_references(calls.iter().map(|c| c.to_string()).collect())
    }

    fn method(owner: &str, name: &str, file: &str, calls: &[&str]) -> CodeNode {
        CodeNode::new(name, format!("{owner}.{name}"), NodeKind::Method, file)
            .with_references(calls.iter().map(|c| c.to_string()).collect())
    }

    fn callers_of(graph: &ArborGraph, name: &str, file: &str) -> Vec<String> {
        let id = graph
            .node_indexes()
            .find(|id| {
                graph
                    .get(*id)
                    .is_some_and(|n| n.name == name && n.file == file)
            })
            .unwrap_or_else(|| panic!("no {name} in {file}"));
        let mut names: Vec<String> = graph
            .get_callers(id)
            .into_iter()
            .map(|n| n.name.clone())
            .collect();
        names.sort();
        names
    }

    #[test]
    fn module_paths_resolve_to_the_named_module_and_never_across_languages() {
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            function("enqueue", "src/app/jobs.rs", &[]),
            function("enqueue", "src/billing/jobs_old.rs", &[]),
            function("enqueue", "web/src/components/Toast.tsx", &[]),
            function("dispatch", "src/app/dispatch.rs", &["jobs::enqueue"]),
            function("start", "web/src/lib/bridge.ts", &["enqueue"]),
        ]);
        let graph = b.build();
        assert_eq!(
            callers_of(&graph, "enqueue", "src/app/jobs.rs"),
            vec!["dispatch"]
        );
        assert!(callers_of(&graph, "enqueue", "src/billing/jobs_old.rs").is_empty());
        // The TypeScript call binds to the TypeScript definition only.
        assert_eq!(
            callers_of(&graph, "enqueue", "web/src/components/Toast.tsx"),
            vec!["start"]
        );
    }

    #[test]
    fn mod_rs_crate_prefixes_and_re_exports_resolve() {
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            function("create_project", "src/app/mod.rs", &[]),
            function("record", "src/app/audit/writer.rs", &[]),
            function(
                "handler",
                "src/routes/mod.rs",
                &["app::create_project", "app::audit::record"],
            ),
            function(
                "persistence_test",
                "tests/integration.rs",
                &["my_app::app::create_project", "app::record"],
            ),
        ]);
        let graph = b.build();
        assert_eq!(
            callers_of(&graph, "create_project", "src/app/mod.rs"),
            vec!["handler", "persistence_test"]
        );
        // `app::record` is a `pub use audit::writer::record` re-export.
        assert_eq!(
            callers_of(&graph, "record", "src/app/audit/writer.rs"),
            vec!["handler", "persistence_test"]
        );
    }

    #[test]
    fn external_crate_paths_do_not_bind_to_local_names() {
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            function("query", "src/db/queries.rs", &[]),
            function("load", "src/routes/mod.rs", &["sqlx::query"]),
        ]);
        let graph = b.build();
        assert!(callers_of(&graph, "query", "src/db/queries.rs").is_empty());
    }

    #[test]
    fn type_paths_resolve_to_the_types_method() {
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            CodeNode::new(
                "Provider",
                "Provider",
                NodeKind::Struct,
                "src/billing/mod.rs",
            ),
            method("Provider", "new", "src/billing/mod.rs", &[]),
            method("Fixture", "new", "tests/fixture.rs", &[]),
            function("boot", "src/main.rs", &["Provider::new"]),
        ]);
        let graph = b.build();
        assert_eq!(
            callers_of(&graph, "new", "src/billing/mod.rs"),
            vec!["boot"]
        );
        assert!(callers_of(&graph, "new", "tests/fixture.rs").is_empty());
    }

    #[test]
    fn a_mock_struct_with_the_same_name_does_not_hide_the_real_owner() {
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            CodeNode::new(
                "Provider",
                "Provider",
                NodeKind::Struct,
                "src/payments/mod.rs",
            ),
            CodeNode::new("Provider", "Provider", NodeKind::Struct, "tests/mocks.rs"),
            method("Provider", "owner_of", "src/payments/checkout.rs", &[]),
            method(
                "Provider",
                "order_history",
                "src/payments/cancellation.rs",
                &["self.owner_of", ".bind"],
            ),
            function("bind", "src/payments/tests.rs", &[]),
        ]);
        let graph = b.build();
        assert_eq!(
            callers_of(&graph, "owner_of", "src/payments/checkout.rs"),
            vec!["order_history"]
        );
        // `.bind(...)` is a method call; it can't reach a free function.
        assert!(callers_of(&graph, "bind", "src/payments/tests.rs").is_empty());
    }

    #[test]
    fn self_calls_reach_methods_whose_impl_is_in_another_file() {
        let mut b = GraphBuilder::new();
        b.add_nodes(vec![
            CodeNode::new(
                "Provider",
                "Provider",
                NodeKind::Struct,
                "src/payments/mod.rs",
            ),
            method("Provider", "owner_of", "src/payments/checkout.rs", &[]),
            method(
                "Provider",
                "order_history",
                "src/payments/cancellation.rs",
                &["self.owner_of"],
            ),
        ]);
        let graph = b.build();
        assert_eq!(
            callers_of(&graph, "owner_of", "src/payments/checkout.rs"),
            vec!["order_history"]
        );
    }
}