vera2-core 0.12.16-aristoddle.1

Core engine for Vera: parsing, indexing, retrieval, storage, and embedding
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
use super::*;
use crate::parsing::languages::tree_sitter_grammar;

fn parse_and_extract(source: &str, lang: Language) -> Vec<RawSymbol> {
    let grammar = tree_sitter_grammar(lang).unwrap();
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(&grammar).unwrap();
    let tree = parser.parse(source, None).unwrap();
    extract_symbols(&tree, source.as_bytes(), lang)
}

#[test]
fn rust_extracts_functions() {
    let source = r#"
fn hello() {
    println!("hello");
}

fn world(x: i32) -> i32 {
    x + 1
}
"#;
    let symbols = parse_and_extract(source, Language::Rust);
    assert_eq!(symbols.len(), 2);
    assert_eq!(symbols[0].name.as_deref(), Some("hello"));
    assert_eq!(symbols[0].symbol_type, SymbolType::Function);
    assert_eq!(symbols[1].name.as_deref(), Some("world"));
}

#[test]
fn rust_extracts_structs_and_enums() {
    let source = r#"
struct Point {
    x: f64,
    y: f64,
}

enum Color {
    Red,
    Green,
    Blue,
}
"#;
    let symbols = parse_and_extract(source, Language::Rust);
    assert_eq!(symbols.len(), 2);
    assert_eq!(symbols[0].symbol_type, SymbolType::Struct);
    assert_eq!(symbols[0].name.as_deref(), Some("Point"));
    assert_eq!(symbols[1].symbol_type, SymbolType::Enum);
    assert_eq!(symbols[1].name.as_deref(), Some("Color"));
}

#[test]
fn rust_extracts_impl_methods() {
    let source = r#"
impl Point {
    fn new(x: f64, y: f64) -> Self {
        Self { x, y }
    }

    fn distance(&self) -> f64 {
        (self.x * self.x + self.y * self.y).sqrt()
        }
}
"#;
    let symbols = parse_and_extract(source, Language::Rust);
    assert_eq!(symbols.len(), 3);
    assert_eq!(symbols[0].symbol_type, SymbolType::Block);
    assert_eq!(symbols[0].name.as_deref(), Some("impl Point"));
    assert_eq!(symbols[1].symbol_type, SymbolType::Method);
    assert_eq!(symbols[1].name.as_deref(), Some("new"));
    assert_eq!(symbols[2].symbol_type, SymbolType::Method);
    assert_eq!(symbols[2].name.as_deref(), Some("distance"));
}

#[test]
fn rust_extracts_trait_impl_names() {
    let source = r#"
impl Sink for StdoutSink {
    fn write(&self) {}
}
"#;
    let symbols = parse_and_extract(source, Language::Rust);
    assert_eq!(symbols[0].symbol_type, SymbolType::Block);
    assert_eq!(symbols[0].name.as_deref(), Some("impl Sink for StdoutSink"));
}

#[test]
fn rust_extracts_traits() {
    let source = r#"
trait Drawable {
    fn draw(&self);
    fn area(&self) -> f64;
}
"#;
    let symbols = parse_and_extract(source, Language::Rust);
    assert_eq!(symbols.len(), 1);
    assert_eq!(symbols[0].symbol_type, SymbolType::Trait);
    assert_eq!(symbols[0].name.as_deref(), Some("Drawable"));
}

/// A `#[cfg(test)] mod tests { ... }` block holding the shapes that used to be
/// swallowed by the module symbol: a free function, a struct, and an impl.
const INLINE_MOD_SOURCE: &str = r#"
fn helper() -> i32 {
    1
}

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

    fn fixture() -> i32 {
        helper()
    }

    struct Case {
        value: i32,
    }

    impl Case {
        fn run(&self) -> i32 {
            fixture()
        }
    }
}
"#;

#[test]
fn rust_inline_mod_does_not_swallow_inner_symbols() {
    let symbols = parse_and_extract(INLINE_MOD_SOURCE, Language::Rust);
    let named: Vec<(&str, SymbolType)> = symbols
        .iter()
        .filter_map(|s| s.name.as_deref().map(|n| (n, s.symbol_type)))
        .collect();

    // Presence first: an extractor that returned nothing must not pass this.
    assert!(
        named.contains(&("helper", SymbolType::Function)),
        "expected the top-level function, got {named:?}"
    );
    assert!(
        named.contains(&("fixture", SymbolType::Function)),
        "expected the function inside the inline mod, got {named:?}"
    );
    assert!(
        named.contains(&("Case", SymbolType::Struct)),
        "expected the struct inside the inline mod, got {named:?}"
    );
    assert!(
        named.contains(&("run", SymbolType::Method)),
        "expected the impl method inside the inline mod, got {named:?}"
    );
    // The module itself stays indexable, as `impl` blocks do.
    assert!(
        named.contains(&("tests", SymbolType::Module)),
        "expected the module symbol itself, got {named:?}"
    );

    // Only then: the module no longer spans the inner symbols on its own.
    let module = symbols
        .iter()
        .find(|s| s.symbol_type == SymbolType::Module)
        .expect("module symbol");
    let fixture = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("fixture"))
        .expect("fixture symbol");
    assert!(
        fixture.start_byte > module.start_byte && fixture.end_byte < module.end_byte,
        "fixture should be nested strictly inside the module span"
    );
}

#[test]
fn rust_inline_mod_attributes_calls_to_the_calling_function() {
    let grammar = tree_sitter_grammar(Language::Rust).unwrap();
    let mut parser = tree_sitter::Parser::new();
    parser.set_language(&grammar).unwrap();
    let tree = parser.parse(INLINE_MOD_SOURCE, None).unwrap();
    let refs = crate::parsing::references::extract_references(
        &tree,
        INLINE_MOD_SOURCE.as_bytes(),
        Language::Rust,
    );

    let callers = |callee: &str| -> Vec<Option<String>> {
        refs.iter()
            .filter(|r| r.callee == callee)
            .map(|r| r.caller.clone())
            .collect()
    };

    // Presence first: the calls have to be seen at all.
    assert_eq!(
        callers("helper"),
        vec![Some("fixture".to_string())],
        "call to helper should be attributed to the calling function, not the module"
    );
    assert_eq!(
        callers("fixture"),
        vec![Some("run".to_string())],
        "call to fixture should be attributed to the calling method, not the module"
    );
}

#[test]
fn rust_trait_extracts_default_methods() {
    let source = r#"
trait Greeter {
    fn name(&self) -> String;

    fn greet(&self) -> String {
        format!("hi {}", self.name())
    }
}
"#;
    let symbols = parse_and_extract(source, Language::Rust);
    let named: Vec<(&str, SymbolType)> = symbols
        .iter()
        .filter_map(|s| s.name.as_deref().map(|n| (n, s.symbol_type)))
        .collect();
    assert!(
        named.contains(&("Greeter", SymbolType::Trait)),
        "expected the trait symbol, got {named:?}"
    );
    assert!(
        named.contains(&("greet", SymbolType::Method)),
        "expected the default method body, got {named:?}"
    );
}

#[test]
fn python_extracts_functions_and_class_methods() {
    let source = r#"
def hello():
    print("hello")

class MyClass:
    def __init__(self):
        self.x = 0

    def method(self):
        return self.x
"#;
    let symbols = parse_and_extract(source, Language::Python);
    // Should extract: hello (function), MyClass (class), __init__ (method), method (method)
    assert_eq!(symbols.len(), 4);
    assert_eq!(symbols[0].symbol_type, SymbolType::Function);
    assert_eq!(symbols[0].name.as_deref(), Some("hello"));
    assert_eq!(symbols[1].symbol_type, SymbolType::Class);
    assert_eq!(symbols[1].name.as_deref(), Some("MyClass"));
    assert_eq!(symbols[2].symbol_type, SymbolType::Method);
    assert_eq!(symbols[2].name.as_deref(), Some("__init__"));
    assert_eq!(symbols[3].symbol_type, SymbolType::Method);
    assert_eq!(symbols[3].name.as_deref(), Some("method"));
}

#[test]
fn python_class_without_methods_kept_as_class() {
    let source = r#"
class Config:
    DEBUG = True
    VERSION = "1.0"
"#;
    let symbols = parse_and_extract(source, Language::Python);
    assert_eq!(symbols.len(), 1);
    assert_eq!(symbols[0].symbol_type, SymbolType::Class);
    assert_eq!(symbols[0].name.as_deref(), Some("Config"));
}

#[test]
fn python_decorated_methods_extracted_separately() {
    let source = r#"
class MyClass:
    @staticmethod
    def static_method():
        pass

    @property
    def value(self):
        return self._value
"#;
    let symbols = parse_and_extract(source, Language::Python);
    assert_eq!(symbols.len(), 3);
    assert_eq!(symbols[0].symbol_type, SymbolType::Class);
    assert_eq!(symbols[0].name.as_deref(), Some("MyClass"));
    assert_eq!(symbols[1].symbol_type, SymbolType::Method);
    assert_eq!(symbols[1].name.as_deref(), Some("static_method"));
    assert_eq!(symbols[2].symbol_type, SymbolType::Method);
    assert_eq!(symbols[2].name.as_deref(), Some("value"));
}

#[test]
fn typescript_extracts_functions_and_interfaces() {
    let source = r#"
function greet(name: string): string {
    return `Hello, ${name}!`;
}

interface User {
    name: string;
    age: number;
}

class UserService {
    private users: User[];

    getUser(id: number): User {
        return this.users[id];
    }
}
"#;
    let symbols = parse_and_extract(source, Language::TypeScript);
    assert!(
        symbols.len() >= 4,
        "expected >= 4 symbols, got {}",
        symbols.len()
    );

    let func = symbols.iter().find(|s| s.name.as_deref() == Some("greet"));
    assert!(func.is_some(), "should find function 'greet'");
    assert_eq!(func.unwrap().symbol_type, SymbolType::Function);

    let iface = symbols.iter().find(|s| s.name.as_deref() == Some("User"));
    assert!(iface.is_some(), "should find interface 'User'");
    assert_eq!(iface.unwrap().symbol_type, SymbolType::Interface);

    let class = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("UserService"));
    assert!(class.is_some(), "should find class 'UserService'");
    assert_eq!(class.unwrap().symbol_type, SymbolType::Class);

    let method = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("getUser"));
    assert!(method.is_some(), "should find method 'getUser'");
    assert_eq!(method.unwrap().symbol_type, SymbolType::Method);
}

#[test]
fn go_extracts_functions_and_structs() {
    let source = r#"
package main

func Hello() string {
    return "hello"
}

type Point struct {
    X float64
    Y float64
}

func (p *Point) Distance() float64 {
    return p.X * p.X + p.Y * p.Y
}
"#;
    let symbols = parse_and_extract(source, Language::Go);
    assert!(
        symbols.len() >= 3,
        "expected >= 3 symbols, got {}",
        symbols.len()
    );

    let func = symbols.iter().find(|s| s.name.as_deref() == Some("Hello"));
    assert!(func.is_some(), "should find function 'Hello'");
    assert_eq!(func.unwrap().symbol_type, SymbolType::Function);

    let struc = symbols.iter().find(|s| s.name.as_deref() == Some("Point"));
    assert!(struc.is_some(), "should find struct 'Point'");
    assert_eq!(struc.unwrap().symbol_type, SymbolType::Struct);

    let method = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Distance"));
    assert!(method.is_some(), "should find method 'Distance'");
    assert_eq!(method.unwrap().symbol_type, SymbolType::Method);
}

#[test]
fn java_extracts_class_and_methods() {
    let source = r#"
class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }
}
"#;
    let symbols = parse_and_extract(source, Language::Java);
    assert!(
        symbols.len() >= 3,
        "expected >= 3 symbols, got {}",
        symbols.len()
    );

    let class = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Calculator"));
    assert!(class.is_some(), "should find class 'Calculator'");
    assert_eq!(class.unwrap().symbol_type, SymbolType::Class);

    let add = symbols.iter().find(|s| s.name.as_deref() == Some("add"));
    assert!(add.is_some(), "should find method 'add'");
    assert_eq!(add.unwrap().symbol_type, SymbolType::Method);

    let multiply = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("multiply"));
    assert!(multiply.is_some(), "should find method 'multiply'");
    assert_eq!(multiply.unwrap().symbol_type, SymbolType::Method);
}

#[test]
fn java_enum_extracts_methods() {
    let source = r#"
enum Direction {
    UP,
    DOWN;

    int code() {
        return ordinal();
    }
}
"#;
    let symbols = parse_and_extract(source, Language::Java);
    assert!(
        symbols.len() >= 2,
        "expected >= 2 symbols, got {}",
        symbols.len()
    );

    let enm = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Direction"));
    assert!(enm.is_some(), "should find enum 'Direction'");
    assert_eq!(enm.unwrap().symbol_type, SymbolType::Enum);

    let method = symbols.iter().find(|s| s.name.as_deref() == Some("code"));
    assert!(method.is_some(), "should find enum method 'code'");
    assert_eq!(method.unwrap().symbol_type, SymbolType::Method);
}

#[test]
fn c_extracts_functions_and_structs() {
    let source = r#"
struct Point {
    double x;
    double y;
};

double distance(struct Point* p) {
    return p->x * p->x + p->y * p->y;
}
"#;
    let symbols = parse_and_extract(source, Language::C);
    assert!(
        !symbols.is_empty(),
        "expected >= 1 symbol, got {}",
        symbols.len()
    );

    let func = symbols
        .iter()
        .find(|s| s.symbol_type == SymbolType::Function);
    assert!(func.is_some(), "should find a function");
    assert_eq!(func.unwrap().name.as_deref(), Some("distance"));
}

#[test]
fn cpp_extracts_classes_and_functions() {
    let source = r#"
class Shape {
public:
    virtual double area() = 0;
};

namespace geometry {
    double pi() {
        return 3.14159;
    }
}
"#;
    let symbols = parse_and_extract(source, Language::Cpp);
    assert!(
        !symbols.is_empty(),
        "expected >= 1 symbol, got {}",
        symbols.len()
    );

    let class = symbols.iter().find(|s| s.symbol_type == SymbolType::Class);
    assert!(class.is_some(), "should find a class");

    let ns = symbols.iter().find(|s| s.symbol_type == SymbolType::Module);
    assert!(ns.is_some(), "should find a namespace");
}

#[test]
fn ruby_extracts_functions_and_classes() {
    let source = r#"
module MyModule
end

class MyClass
end

def my_method
end
"#;
    let symbols = parse_and_extract(source, Language::Ruby);
    assert!(symbols.len() >= 3);
    let m = symbols.iter().find(|s| s.symbol_type == SymbolType::Module);
    assert!(m.is_some());
    assert_eq!(m.unwrap().name.as_deref(), Some("MyModule"));

    let c = symbols.iter().find(|s| s.symbol_type == SymbolType::Class);
    assert!(c.is_some());
    assert_eq!(c.unwrap().name.as_deref(), Some("MyClass"));

    let f = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("my_method"));
    assert!(f.is_some());
    assert_eq!(f.unwrap().symbol_type, SymbolType::Function);
}

#[test]
fn bash_extracts_functions() {
    let source = r#"
function foo() {
    echo "foo"
}
bar() {
    echo "bar"
}
"#;
    let symbols = parse_and_extract(source, Language::Bash);
    assert_eq!(symbols.len(), 2);
    assert_eq!(symbols[0].symbol_type, SymbolType::Function);
    assert_eq!(symbols[0].name.as_deref(), Some("foo"));
    assert_eq!(symbols[1].symbol_type, SymbolType::Function);
    assert_eq!(symbols[1].name.as_deref(), Some("bar"));
}

#[test]
fn bash_extracts_case_items_with_first_pattern_names() {
    let source = r#"function nvm() {
    case "$1" in
        use)
            echo use
            ;;
        "--help")
            echo help
            ;;
        a|b)
            echo alternate
            ;;
    esac
}"#;
    let symbols = parse_and_extract(source, Language::Bash);

    let named: Vec<_> = symbols
        .iter()
        .filter_map(|symbol| symbol.name.as_deref())
        .collect();
    assert_eq!(
        named,
        vec!["nvm", "nvm use", "nvm --help", "nvm a"],
        "case items should use the first pattern alternative"
    );
    for symbol in symbols.iter().skip(1) {
        assert_eq!(symbol.symbol_type, SymbolType::Function);
        assert!(symbol.start_row < symbol.end_row);
        assert!(symbol.start_byte < symbol.end_byte);
        assert!(source[symbol.start_byte..symbol.end_byte].contains(")"));
    }
}

#[test]
fn bash_extracts_nested_and_multiple_case_statements() {
    let source = r#"dispatch() {
    case "$1" in
        outer)
            case "$2" in
                inner)
                    echo inner
                    ;;
            esac
            ;;
    esac
    case "$3" in
        second)
            echo second
            ;;
    esac
}"#;
    let symbols = parse_and_extract(source, Language::Bash);

    let named: Vec<_> = symbols
        .iter()
        .filter_map(|symbol| symbol.name.as_deref())
        .collect();
    assert_eq!(
        named,
        vec![
            "dispatch",
            "dispatch outer",
            "dispatch inner",
            "dispatch second"
        ]
    );

    let outer = symbols
        .iter()
        .find(|symbol| symbol.name.as_deref() == Some("dispatch outer"))
        .unwrap();
    let inner = symbols
        .iter()
        .find(|symbol| symbol.name.as_deref() == Some("dispatch inner"))
        .unwrap();
    assert!(inner.start_byte > outer.start_byte);
    assert!(inner.end_byte < outer.end_byte);
}

#[test]
fn kotlin_extracts_types_and_functions() {
    let source = r#"
fun foo() {}
class Bar {}
interface Baz {}
object Qux {}
enum class Quux {}
"#;
    let symbols = parse_and_extract(source, Language::Kotlin);
    assert_eq!(symbols.len(), 5);

    let fun = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("foo"))
        .unwrap();
    assert_eq!(fun.symbol_type, SymbolType::Function);

    let cls = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Bar"))
        .unwrap();
    assert_eq!(cls.symbol_type, SymbolType::Class);

    let iface = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Baz"))
        .unwrap();
    assert_eq!(iface.symbol_type, SymbolType::Interface);

    let obj = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Qux"))
        .unwrap();
    assert_eq!(obj.symbol_type, SymbolType::Class);

    let enm = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Quux"))
        .unwrap();
    assert_eq!(enm.symbol_type, SymbolType::Enum);
}

#[test]
fn swift_extracts_types_and_functions() {
    let source = r#"
func foo() {}
class Bar {}
struct Baz {}
enum Qux {}
protocol Quux {}
"#;
    let symbols = parse_and_extract(source, Language::Swift);
    assert_eq!(symbols.len(), 5);

    let fun = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("foo"))
        .unwrap();
    assert_eq!(fun.symbol_type, SymbolType::Function);

    let cls = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Bar"))
        .unwrap();
    assert_eq!(cls.symbol_type, SymbolType::Class);

    let strc = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Baz"))
        .unwrap();
    assert_eq!(strc.symbol_type, SymbolType::Struct);

    let enm = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Qux"))
        .unwrap();
    assert_eq!(enm.symbol_type, SymbolType::Enum);

    let proto = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Quux"))
        .unwrap();
    assert_eq!(proto.symbol_type, SymbolType::Interface);
}

#[test]
fn zig_extracts_functions_and_structs() {
    let source = r#"
fn foo() void {}
const Bar = struct {};
"#;
    let symbols = parse_and_extract(source, Language::Zig);
    assert_eq!(symbols.len(), 2);

    let fun = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("foo"))
        .unwrap();
    assert_eq!(fun.symbol_type, SymbolType::Function);

    let strc = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("Bar"))
        .unwrap();
    assert_eq!(strc.symbol_type, SymbolType::Struct);
}

#[test]
fn lua_extracts_functions() {
    let source = r#"
function foo() end
local function bar() end
"#;
    let symbols = parse_and_extract(source, Language::Lua);
    assert_eq!(symbols.len(), 2);

    let foo = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("foo"))
        .unwrap();
    assert_eq!(foo.symbol_type, SymbolType::Function);

    let bar = symbols
        .iter()
        .find(|s| s.name.as_deref() == Some("bar"))
        .unwrap();
    assert_eq!(bar.symbol_type, SymbolType::Function);
}

#[test]
fn lua_extracts_function_assignments() {
    let source = "MiniAi.setup = function(config)\n  return 1\nend";
    let symbols = parse_and_extract(source, Language::Lua);

    assert_eq!(symbols.len(), 1);
    assert_eq!(symbols[0].name.as_deref(), Some("setup"));
    assert_eq!(symbols[0].symbol_type, SymbolType::Function);
    assert_eq!(symbols[0].start_byte, 0);
    assert_eq!(symbols[0].end_byte, source.len());
    assert_eq!(symbols[0].start_row, 0);
    assert_eq!(symbols[0].end_row, 2);
}

#[test]
fn lua_ignores_non_function_and_nested_assignments() {
    let source = r#"
M.method = function() end
local helper = function() end
M.value = 5
M.items[1] = function() end
M.table = { callback = function() end }
"#;
    let symbols = parse_and_extract(source, Language::Lua);

    assert_eq!(symbols.len(), 2);
    assert!(
        symbols
            .iter()
            .any(|symbol| symbol.name.as_deref() == Some("method"))
    );
    assert!(
        symbols
            .iter()
            .any(|symbol| symbol.name.as_deref() == Some("helper"))
    );
}

#[test]
fn scala_extracts_types_and_functions() {
    let source = r#"
def main(args: Array[String]): Unit = {}
class User {}
trait Logger {}
"#;
    let symbols = parse_and_extract(source, Language::Scala);
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function && s.name.as_deref() == Some("main"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Class && s.name.as_deref() == Some("User"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Trait && s.name.as_deref() == Some("Logger"))
    );
}

#[test]
fn csharp_extracts_types_and_methods() {
    let source = r#"
namespace MyApp {
    class Calculator {
        public int Add(int a, int b) { return a + b; }
    }
    interface IWorker {}
    struct Point {}
    enum Status { Ok, Error }
}
"#;
    let symbols = parse_and_extract(source, Language::CSharp);
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Module));
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Class));
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Method));
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Interface)
    );
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Struct));
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Enum));
}

#[test]
fn php_extracts_classes_and_functions() {
    let source = r#"
<?php
function foo() {}
class Bar {
    public function baz() {}
}
interface Qux {}
"#;
    let symbols = parse_and_extract(source, Language::Php);
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function)
    );
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Class));
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Method));
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Interface)
    );
}

#[test]
fn haskell_extracts_types_and_functions() {
    let source = r#"
data Point = Point Float Float
type Age = Int
add :: Int -> Int -> Int
add x y = x + y
"#;
    let symbols = parse_and_extract(source, Language::Haskell);
    assert!(symbols.iter().any(|s| s.symbol_type == SymbolType::Struct));
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::TypeAlias)
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function)
    );
}

#[test]
fn elixir_extracts_modules_and_functions() {
    let source = r#"
defmodule Math do
  def add(a, b) do
    a + b
  end
  defp sub(a, b), do: a - b
  defmacro mul(a, b) do
  end
end
"#;
    let symbols = parse_and_extract(source, Language::Elixir);
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Module && s.name.as_deref() == Some("Math"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function && s.name.as_deref() == Some("add"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function && s.name.as_deref() == Some("sub"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function && s.name.as_deref() == Some("mul"))
    );
}

#[test]
fn hcl_extracts_blocks() {
    let source = r#"
resource "aws_instance" "web" {
  ami = "ami-123"
}
module "vpc" {}
data "aws_ami" "ubuntu" {}
variable "image_id" {}
output "instance_ip" {}
"#;
    let symbols = parse_and_extract(source, Language::Hcl);
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Struct
                && s.name.as_deref() == Some("aws_instance"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Module && s.name.as_deref() == Some("vpc"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Struct && s.name.as_deref() == Some("aws_ami"))
    );
    assert!(
        symbols.iter().any(
            |s| s.symbol_type == SymbolType::TypeAlias && s.name.as_deref() == Some("image_id")
        )
    );
    assert!(symbols.iter().any(
        |s| s.symbol_type == SymbolType::TypeAlias && s.name.as_deref() == Some("instance_ip")
    ));
}

#[test]
fn sql_extracts_statements() {
    let source = r#"
CREATE TABLE users (id INT);
CREATE FUNCTION get_user() RETURNS INT AS $$ SELECT 1 $$ LANGUAGE SQL;
CREATE VIEW active_users AS SELECT * FROM users;
CREATE PROCEDURE my_proc() LANGUAGE SQL AS $$ $$;
"#;
    let symbols = parse_and_extract(source, Language::Sql);
    assert!(!symbols.is_empty());
}

#[test]
fn protobuf_extracts_types() {
    let source = r#"
syntax = "proto3";
message User { int32 id = 1; }
enum Status { ACTIVE = 0; }
service AuthService { rpc Login(User) returns (User); }
"#;
    let symbols = parse_and_extract(source, Language::Protobuf);
    assert!(!symbols.is_empty());
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Struct && s.name.as_deref() == Some("User"))
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Enum && s.name.as_deref() == Some("Status"))
    );
    assert!(symbols.iter().any(
            |s| s.symbol_type == SymbolType::Class && s.name.as_deref() == Some("AuthService")
        ));
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Method && s.name.as_deref() == Some("Login"))
    );
}

// ── Tier 1B symbol extraction tests ─────────────────────────

#[test]
fn html_extracts_elements() {
    let source = r#"
<html>
<head><title>Test</title></head>
<body>
  <div class="container">
    <p>Hello</p>
  </div>
  <script>console.log("hi")</script>
  <style>body { color: red; }</style>
</body>
</html>
"#;
    let symbols = parse_and_extract(source, Language::Html);
    assert!(!symbols.is_empty(), "HTML should extract some symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "HTML should extract block symbols"
    );
}

#[test]
fn css_extracts_rules() {
    let source = r#"
body {
    color: red;
    font-size: 16px;
}

.container {
    max-width: 1200px;
}

@media (max-width: 768px) {
    body { font-size: 14px; }
}
"#;
    let symbols = parse_and_extract(source, Language::Css);
    assert!(!symbols.is_empty(), "CSS should extract some symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "CSS should extract rule_set blocks"
    );
}

#[test]
fn scss_extracts_mixins_and_rules() {
    let source = r#"
$primary: #333;

@mixin flex-center {
    display: flex;
    align-items: center;
}

.container {
    @include flex-center;
    color: $primary;
}
"#;
    let symbols = parse_and_extract(source, Language::Scss);
    assert!(!symbols.is_empty(), "SCSS should extract some symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "SCSS should extract mixin as function"
    );
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "SCSS should extract rule_set blocks"
    );
}

#[test]
fn vue_extracts_sections() {
    let source = r#"
<template>
  <div>{{ message }}</div>
</template>

<script>
export default {
  data() { return { message: "Hello" } }
}
</script>

<style>
.container { color: red; }
</style>
"#;
    let symbols = parse_and_extract(source, Language::Vue);
    assert!(!symbols.is_empty(), "Vue should extract some symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "Vue should extract template/script/style blocks"
    );
}

#[test]
fn graphql_extracts_types_and_queries() {
    let source = r#"
type User {
    id: ID!
    name: String!
    email: String
}

enum Role {
    ADMIN
    USER
    GUEST
}

interface Node {
    id: ID!
}

input CreateUserInput {
    name: String!
    email: String!
}

query GetUser($id: ID!) {
    user(id: $id) {
        name
        email
    }
}
"#;
    let symbols = parse_and_extract(source, Language::GraphQl);
    assert!(!symbols.is_empty(), "GraphQL should extract symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Struct),
        "GraphQL should extract type as struct"
    );
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Enum),
        "GraphQL should extract enum"
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Interface),
        "GraphQL should extract interface"
    );
}

#[test]
fn cmake_extracts_functions_and_commands() {
    let source = r#"
cmake_minimum_required(VERSION 3.10)
project(MyProject)

function(my_helper ARG)
    message(STATUS "${ARG}")
endfunction()

macro(my_macro)
    set(MY_VAR "value")
endmacro()

add_executable(main src/main.cpp)
"#;
    let symbols = parse_and_extract(source, Language::CMake);
    assert!(!symbols.is_empty(), "CMake should extract symbols");
}

#[test]
fn dockerfile_extracts_instructions() {
    let source = r#"FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive
ARG BUILD_VERSION=1.0

RUN apt-get update && apt-get install -y curl

COPY . /app
WORKDIR /app

CMD ["./app"]
"#;
    let symbols = parse_and_extract(source, Language::Dockerfile);
    assert!(!symbols.is_empty(), "Dockerfile should extract symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "Dockerfile should extract FROM as block"
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Constant),
        "Dockerfile should extract ENV/ARG as constant"
    );
}

#[test]
fn xml_extracts_elements() {
    let source = r#"<?xml version="1.0" encoding="UTF-8"?>
<project>
    <name>MyProject</name>
    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>lib</artifactId>
        </dependency>
    </dependencies>
</project>
"#;
    let symbols = parse_and_extract(source, Language::Xml);
    assert!(!symbols.is_empty(), "XML should extract some symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "XML should extract elements as blocks"
    );
}

// ── Tier 2A symbol extraction tests ─────────────────────────

#[test]
fn objectivec_extracts_classes_and_methods() {
    let source = r#"
@interface Calculator : NSObject
- (int)add:(int)a to:(int)b;
@end

@implementation Calculator
- (int)add:(int)a to:(int)b {
    return a + b;
}
@end
"#;
    let symbols = parse_and_extract(source, Language::ObjectiveC);
    assert!(!symbols.is_empty(), "ObjC should extract symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Class),
        "ObjC should extract class interface/implementation"
    );
    // Methods are extracted inside class body
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Method),
        "ObjC should extract methods"
    );
}

#[test]
fn perl_extracts_functions() {
    let source = r#"
package MyModule;

sub hello {
    print "hello\n";
}

sub world {
    my ($name) = @_;
    print "hello $name\n";
}

1;
"#;
    let symbols = parse_and_extract(source, Language::Perl);
    assert!(!symbols.is_empty(), "Perl should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "Perl should extract functions"
    );
}

#[test]
fn julia_extracts_functions_and_structs() {
    let source = r#"
function hello()
    println("hello")
end

struct Point
    x::Float64
    y::Float64
end

module MyModule
end
"#;
    let symbols = parse_and_extract(source, Language::Julia);
    assert!(!symbols.is_empty(), "Julia should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "Julia should extract functions"
    );
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Struct),
        "Julia should extract structs"
    );
}

#[test]
fn nix_extracts_bindings() {
    let source = r#"
{
  hello = "world";
  foo = x: x + 1;
}
"#;
    let symbols = parse_and_extract(source, Language::Nix);
    assert!(!symbols.is_empty(), "Nix should extract symbols");
}

#[test]
fn ocaml_extracts_functions_and_types() {
    let source = r#"
let hello () = print_endline "hello"

type point = { x: float; y: float }

module MyModule = struct end
"#;
    let symbols = parse_and_extract(source, Language::OCaml);
    assert!(!symbols.is_empty(), "OCaml should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "OCaml should extract functions"
    );
}

#[test]
fn groovy_extracts_classes_and_functions() {
    let source = r#"
class Calculator {
    int add(int a, int b) {
        return a + b
    }
}

def hello() {
    println "hello"
}
"#;
    let symbols = parse_and_extract(source, Language::Groovy);
    assert!(!symbols.is_empty(), "Groovy should extract symbols");
}

#[test]
fn clojure_extracts_functions() {
    let source = r#"
(ns myapp.core)

(defn hello []
  (println "hello"))

(defn add [a b]
  (+ a b))
"#;
    let symbols = parse_and_extract(source, Language::Clojure);
    assert!(
        symbols.len() >= 3,
        "Clojure should extract at least 3 symbols (ns + 2 defn), got {}",
        symbols.len()
    );
    assert!(
            symbols
                .iter()
                .any(|s| s.name.as_deref() == Some("myapp.core")
                    && s.symbol_type == SymbolType::Module),
            "Clojure should extract ns 'myapp.core'"
        );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("hello") && s.symbol_type == SymbolType::Function),
        "Clojure should extract function 'hello'"
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("add") && s.symbol_type == SymbolType::Function),
        "Clojure should extract function 'add'"
    );
}

#[test]
fn clojure_extracts_defmacro() {
    let source = "(defmacro my-when [test & body]\n  `(if ~test (do ~@body)))\n";
    let symbols = parse_and_extract(source, Language::Clojure);
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("my-when") && s.symbol_type == SymbolType::Function),
        "Clojure should extract defmacro 'my-when'"
    );
}

#[test]
fn clojure_extracts_def() {
    let source = "(def pi 3.14159)\n";
    let symbols = parse_and_extract(source, Language::Clojure);
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("pi") && s.symbol_type == SymbolType::Variable),
        "Clojure should extract def 'pi'"
    );
}

#[test]
fn commonlisp_extracts_functions() {
    let source = r#"
(defun hello ()
  (format t "hello~%"))

(defun add (a b)
  (+ a b))

(defclass point ()
  ((x :initarg :x)
   (y :initarg :y)))
"#;
    let symbols = parse_and_extract(source, Language::CommonLisp);
    assert!(
        symbols.len() >= 3,
        "CL should extract at least 3 symbols (2 defun + defclass), got {}",
        symbols.len()
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("hello") && s.symbol_type == SymbolType::Function),
        "CL should extract function 'hello'"
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("add") && s.symbol_type == SymbolType::Function),
        "CL should extract function 'add'"
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("point") && s.symbol_type == SymbolType::Class),
        "CL should extract class 'point'"
    );
}

#[test]
fn commonlisp_extracts_defvar() {
    let source = "(defvar *my-var* 42)\n";
    let symbols = parse_and_extract(source, Language::CommonLisp);
    assert!(
            symbols
                .iter()
                .any(|s| s.name.as_deref() == Some("*my-var*")
                    && s.symbol_type == SymbolType::Variable),
            "CL should extract defvar '*my-var*'"
        );
}

#[test]
fn erlang_extracts_functions() {
    let source = r#"
-module(hello).
-export([hello/0, add/2]).

hello() -> ok.

add(A, B) -> A + B.
"#;
    let symbols = parse_and_extract(source, Language::Erlang);
    assert!(!symbols.is_empty(), "Erlang should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "Erlang should extract functions"
    );
}

#[test]
fn fsharp_extracts_functions() {
    let source = r#"
let hello () = printfn "hello"

let add a b = a + b

type Point = { X: float; Y: float }
"#;
    let symbols = parse_and_extract(source, Language::FSharp);
    assert!(!symbols.is_empty(), "F# should extract symbols");
}

#[test]
fn fortran_extracts_functions_and_subroutines() {
    let source = r#"
program hello
  print *, 'hello'
end program hello

subroutine greet(name)
  character(*), intent(in) :: name
  print *, 'Hello ', name
end subroutine greet

function add(a, b) result(c)
  integer, intent(in) :: a, b
  integer :: c
  c = a + b
end function add
"#;
    let symbols = parse_and_extract(source, Language::Fortran);
    assert!(!symbols.is_empty(), "Fortran should extract symbols");
}

#[test]
fn powershell_extracts_functions_and_classes() {
    let source = r#"
function Get-Hello {
    Write-Host "hello"
}

function Add-Numbers {
    param($a, $b)
    return $a + $b
}

class Calculator {
    [int] Add([int]$a, [int]$b) {
        return $a + $b
    }
}
"#;
    let symbols = parse_and_extract(source, Language::PowerShell);
    assert!(!symbols.is_empty(), "PowerShell should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "PowerShell should extract functions"
    );
}

#[test]
fn r_extracts_functions() {
    let source = r#"
hello <- function() {
  print("hello")
}

add <- function(a, b) {
  a + b
}
"#;
    let symbols = parse_and_extract(source, Language::R);
    assert!(!symbols.is_empty(), "R should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "R should extract functions"
    );
    assert!(
        symbols.iter().any(|s| s.name.as_deref() == Some("hello")),
        "R should extract function name 'hello'"
    );
}

// ── Tier 2A batch 2 symbol extraction tests ─────────────────

#[test]
fn matlab_extracts_functions() {
    let source = r#"
function y = square(x)
  y = x^2;
end

function result = add(a, b)
  result = a + b;
end
"#;
    let symbols = parse_and_extract(source, Language::Matlab);
    assert!(!symbols.is_empty(), "MATLAB should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "MATLAB should extract functions"
    );
}

#[test]
fn dlang_extracts_functions_and_classes() {
    let source = r#"
void main() {
    writeln("hello");
}

class Foo {
    int bar() { return 42; }
}

struct Point {
    float x, y;
}
"#;
    let symbols = parse_and_extract(source, Language::DLang);
    assert!(!symbols.is_empty(), "D should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "D should extract functions"
    );
}

#[test]
fn fish_extracts_functions() {
    let source = r#"
function hello
  echo hello
end

function greet -a name
  echo "Hello, $name"
end
"#;
    let symbols = parse_and_extract(source, Language::Fish);
    assert!(!symbols.is_empty(), "Fish should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "Fish should extract functions"
    );
}

#[test]
fn zsh_extracts_functions() {
    let source = r#"
function hello() {
  echo hello
}

greet() {
  echo "Hello, $1"
}
"#;
    let symbols = parse_and_extract(source, Language::Zsh);
    assert!(!symbols.is_empty(), "Zsh should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "Zsh should extract functions"
    );
}

#[test]
fn luau_extracts_functions() {
    let source = r#"
local function hello()
  print("hello")
end

function greet(name: string)
  print("Hello, " .. name)
end
"#;
    let symbols = parse_and_extract(source, Language::Luau);
    assert!(!symbols.is_empty(), "Luau should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "Luau should extract functions"
    );
}

#[test]
fn scheme_extracts_definitions() {
    let source = r#"
(define (hello)
  (display "hello"))

(define (add a b)
  (+ a b))
"#;
    let symbols = parse_and_extract(source, Language::Scheme);
    assert!(
        symbols.len() >= 2,
        "Scheme should extract at least 2 symbols, got {}",
        symbols.len()
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("hello") && s.symbol_type == SymbolType::Function),
        "Scheme should extract function 'hello'"
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("add") && s.symbol_type == SymbolType::Function),
        "Scheme should extract function 'add'"
    );
}

#[test]
fn scheme_extracts_variable_define() {
    let source = "(define x 42)\n";
    let symbols = parse_and_extract(source, Language::Scheme);
    assert!(
        symbols.iter().any(|s| s.name.as_deref() == Some("x")),
        "Scheme should extract variable 'x'"
    );
}

#[test]
fn scheme_extracts_define_syntax() {
    let source = "(define-syntax my-macro\n  (syntax-rules () ()))\n";
    let symbols = parse_and_extract(source, Language::Scheme);
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("my-macro")),
        "Scheme should extract define-syntax 'my-macro'"
    );
}

#[test]
fn racket_extracts_definitions() {
    let source = r#"
#lang racket

(define (hello)
  (displayln "hello"))

(define (add a b)
  (+ a b))
"#;
    let symbols = parse_and_extract(source, Language::Racket);
    assert!(
        symbols.len() >= 2,
        "Racket should extract at least 2 symbols, got {}",
        symbols.len()
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("hello") && s.symbol_type == SymbolType::Function),
        "Racket should extract function 'hello'"
    );
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("add") && s.symbol_type == SymbolType::Function),
        "Racket should extract function 'add'"
    );
}

#[test]
fn racket_extracts_struct() {
    let source = "#lang racket\n(struct point (x y))\n";
    let symbols = parse_and_extract(source, Language::Racket);
    assert!(
        symbols
            .iter()
            .any(|s| s.name.as_deref() == Some("point") && s.symbol_type == SymbolType::Struct),
        "Racket should extract struct 'point'"
    );
}

#[test]
fn elm_extracts_functions_and_types() {
    let source = r#"
module Main exposing (main)

type alias Model =
    { count : Int
    }

type Msg
    = Increment
    | Decrement

main =
    text "hello"
"#;
    let symbols = parse_and_extract(source, Language::Elm);
    assert!(!symbols.is_empty(), "Elm should extract symbols");
}

#[test]
fn glsl_extracts_functions() {
    let source = r#"
struct Light {
    vec3 position;
    vec3 color;
};

void main() {
    gl_FragColor = vec4(1.0);
}
"#;
    let symbols = parse_and_extract(source, Language::Glsl);
    assert!(!symbols.is_empty(), "GLSL should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "GLSL should extract functions"
    );
}

#[test]
fn hlsl_extracts_functions() {
    let source = r#"
struct VS_OUTPUT {
    float4 pos : SV_Position;
    float2 uv : TEXCOORD0;
};

float4 main(float4 pos : SV_Position) : SV_Target {
    return float4(1, 0, 0, 1);
}
"#;
    let symbols = parse_and_extract(source, Language::Hlsl);
    assert!(!symbols.is_empty(), "HLSL should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "HLSL should extract functions"
    );
}

// ── Tier 2B symbol extraction tests ─────────────────

#[test]
fn svelte_extracts_blocks() {
    let source = r#"
<script>
  let count = 0;
  function increment() { count += 1; }
</script>

<button on:click={increment}>
  Count: {count}
</button>

<style>
  button { color: red; }
</style>
"#;
    let symbols = parse_and_extract(source, Language::Svelte);
    assert!(!symbols.is_empty(), "Svelte should extract symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "Svelte should extract blocks"
    );
}

#[test]
fn astro_extracts_frontmatter_and_elements() {
    let source = r#"---
const title = "Hello";
const items = [1, 2, 3];
---
<html>
<head><title>{title}</title></head>
<body>
  <h1>{title}</h1>
</body>
</html>
"#;
    let symbols = parse_and_extract(source, Language::Astro);
    assert!(!symbols.is_empty(), "Astro should extract symbols");
}

#[test]
fn makefile_extracts_rules_and_variables() {
    let source = r#"
CC = gcc
CFLAGS = -Wall

all: build

build:
	$(CC) $(CFLAGS) -o main main.c

clean:
	rm -f main
"#;
    let symbols = parse_and_extract(source, Language::Makefile);
    assert!(!symbols.is_empty(), "Makefile should extract symbols");
    assert!(
        symbols
            .iter()
            .any(|s| s.symbol_type == SymbolType::Function),
        "Makefile should extract rules as functions"
    );
}

#[test]
fn ini_extracts_sections() {
    let source = r#"
[database]
host = localhost
port = 5432

[server]
bind = 0.0.0.0
"#;
    let symbols = parse_and_extract(source, Language::Ini);
    assert!(!symbols.is_empty(), "INI should extract symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Block),
        "INI should extract sections as blocks"
    );
}

#[test]
fn nginx_extracts_blocks() {
    let source = r#"
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
    }
}
"#;
    let symbols = parse_and_extract(source, Language::Nginx);
    assert!(!symbols.is_empty(), "Nginx should extract symbols");
}

#[test]
fn prisma_extracts_models_and_enums() {
    let source = r#"
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  author   User   @relation(fields: [authorId], references: [id])
  authorId Int
}

enum Role {
  USER
  ADMIN
}
"#;
    let symbols = parse_and_extract(source, Language::Prisma);
    assert!(!symbols.is_empty(), "Prisma should extract symbols");
    assert!(
        symbols.iter().any(|s| s.symbol_type == SymbolType::Struct),
        "Prisma should extract models as structs"
    );
}

/// Three levels of inline `mod` around a single function. Each nesting level
/// costs the shared depth budget, so the innermost item is the one at risk.
const NESTED_INLINE_MOD_SOURCE: &str = r#"
mod outer {
    mod middle {
        mod inner {
            fn buried() -> i32 {
                1
            }
        }
    }
}
"#;

#[test]
fn rust_nested_inline_mods_reach_the_innermost_item() {
    let symbols = parse_and_extract(NESTED_INLINE_MOD_SOURCE, Language::Rust);
    let named: Vec<(&str, SymbolType)> = symbols
        .iter()
        .filter_map(|s| s.name.as_deref().map(|n| (n, s.symbol_type)))
        .collect();

    // Presence first: the enclosing modules must be there, or an extractor that
    // returned nothing would satisfy the assertion below by accident.
    assert!(
        named.contains(&("outer", SymbolType::Module)),
        "expected the outermost module, got {named:?}"
    );
    assert!(
        named.contains(&("middle", SymbolType::Module)),
        "expected the second-level module, got {named:?}"
    );
    assert!(
        named.contains(&("inner", SymbolType::Module)),
        "expected the third-level module, got {named:?}"
    );
    assert!(
        named.contains(&("buried", SymbolType::Function)),
        "expected the function inside three levels of inline mod, got {named:?}"
    );
}