semver-analyzer-core 0.0.4

Core types, traits, and diff engine for the semver-analyzer
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
//! Rename detection for the diff engine.
//!
//! Detects when a removed symbol and an added symbol are actually a rename
//! by matching on type signature fingerprints and scoring by name similarity.

use crate::types::{Symbol, SymbolKind};
use std::collections::{BTreeSet, HashMap};

/// Signature fingerprint for matching rename candidates.
///
/// Two symbols with the same fingerprint are considered potential renames.
/// The fingerprint captures: kind, type/return_type, optionality, and
/// parameter count — enough to match renames without false positives.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
struct MemberFingerprint {
    kind: SymbolKind,
    return_type: Option<String>,
    is_optional: bool,
    param_count: usize,
}

impl MemberFingerprint {
    fn from_symbol<M: Default + Clone + PartialEq>(sym: &Symbol<M>) -> Self {
        let (return_type, is_optional, param_count) = match &sym.signature {
            Some(sig) => (
                sig.return_type.clone(),
                sig.parameters.first().map(|p| p.optional).unwrap_or(false),
                sig.parameters.len(),
            ),
            None => (None, false, 0),
        };
        Self {
            kind: sym.kind,
            return_type,
            is_optional,
            param_count,
        }
    }

    /// Create a fingerprint with normalized types for structural comparison.
    ///
    /// Replaces PascalCase type references (e.g., `ToolbarChip`) and
    /// parameter names with placeholders so that structurally equivalent
    /// types match even when reference names changed.
    ///
    /// Example: `(ToolbarChip | string)[]` → `(_T_ | string)[]`
    fn from_symbol_normalized<M: Default + Clone + PartialEq>(sym: &Symbol<M>) -> Self {
        let (return_type, is_optional, param_count) = match &sym.signature {
            Some(sig) => (
                sig.return_type
                    .as_ref()
                    .map(|t| normalize_type_structure(t)),
                sig.parameters.first().map(|p| p.optional).unwrap_or(false),
                sig.parameters.len(),
            ),
            None => (None, false, 0),
        };
        Self {
            kind: sym.kind,
            return_type,
            is_optional,
            param_count,
        }
    }

    /// Returns true if the return type is a primitive or absent entirely.
    /// Primitive fingerprints are degenerate — they match ALL symbols of the
    /// same type, providing zero discriminating signal. When the collision
    /// group is large (multiple candidates), the similarity threshold should
    /// be raised to prevent false rename matches among leftovers.
    ///
    /// The `primitives` list comes from `LanguageSemantics::primitive_type_names()`.
    fn is_primitive_or_absent(&self, primitives: &[&str]) -> bool {
        match &self.return_type {
            None => true,
            Some(t) => {
                let lower = t.to_lowercase();
                primitives.iter().any(|p| p.to_lowercase() == lower)
            }
        }
    }

    /// Create a fingerprint with deep normalization that also replaces
    /// string literal values with placeholders. This catches renames where
    /// the enum values also changed (e.g., spacer → gap where
    /// `'spacerNone'` → `'gapNone'`).
    fn from_symbol_deep_normalized<M: Default + Clone + PartialEq>(sym: &Symbol<M>) -> Self {
        let (return_type, is_optional, param_count) = match &sym.signature {
            Some(sig) => (
                sig.return_type
                    .as_ref()
                    .map(|t| normalize_type_structure_deep(t)),
                sig.parameters.first().map(|p| p.optional).unwrap_or(false),
                sig.parameters.len(),
            ),
            None => (None, false, 0),
        };
        Self {
            kind: sym.kind,
            return_type,
            is_optional,
            param_count,
        }
    }
}

/// Normalize a type string for structural comparison by replacing
/// PascalCase type references and parameter names with placeholders.
///
/// This allows matching types that are structurally identical but
/// reference different (renamed) types:
///   `(ToolbarChip | string)[]` → `(_T_ | string)[]`
///   `(category: ToolbarChipGroup | string, chip: ToolbarChip | string) => void`
///   → `(_p_: _T_ | string, _p_: _T_ | string) => void`
///   `ReactElement<any>` → `_T_` (generic params stripped after normalization)
fn normalize_type_structure(type_str: &str) -> String {
    // Replace PascalCase identifiers (type references) with _T_
    let result = regex_replace_all_pascal_case(type_str, "_T_");
    // Strip generic type parameters from normalized references.
    // After PascalCase replacement, types like `ReactElement<any>` become
    // `_T_<any>` and `ReactElement` becomes `_T_`.  These are semantically
    // equivalent (the `<any>` is TypeScript's default generic parameter),
    // but the literal string difference prevents fingerprint matching in
    // Pass 2/3.  Stripping the generic params collapses both to `_T_`,
    // allowing the rename detector to match them.
    let result = strip_normalized_generics(&result);
    // Replace parameter names (lowercase word before colon) with _p_
    regex_replace_all_param_names(&result, "_p_")
}

/// Strip generic type parameters from `_T_` placeholders.
///
/// After PascalCase normalization, types like:
///   `ReactElement<any>`       → `_T_<any>`       → `_T_`
///   `RefObject<HTMLDivElement>` → `_T_<_T_>`     → `_T_`
///   `Map<string, number>`     → `_T_<string, number>` → `_T_`
///   `(ToolbarChip | string)[]` → `(_T_ | string)[]` → unchanged (no generics)
///
/// This only strips `<...>` that immediately follow `_T_`.  Standalone
/// generics (e.g., in union/intersection/array positions) are not affected.
fn strip_normalized_generics(s: &str) -> String {
    let marker = "_T_";
    let mut result = String::with_capacity(s.len());
    let mut i = 0;
    let bytes = s.as_bytes();

    while i < bytes.len() {
        // Check for _T_ marker
        if i + marker.len() <= bytes.len() && &s[i..i + marker.len()] == marker {
            result.push_str(marker);
            i += marker.len();
            // If immediately followed by '<', skip the entire generic parameter list
            if i < bytes.len() && bytes[i] == b'<' {
                let mut depth = 1;
                i += 1; // skip '<'
                while i < bytes.len() && depth > 0 {
                    match bytes[i] {
                        b'<' => depth += 1,
                        b'>' => depth -= 1,
                        _ => {}
                    }
                    i += 1;
                }
            }
        } else {
            result.push(bytes[i] as char);
            i += 1;
        }
    }
    result
}

/// Replace all PascalCase identifiers (starting with uppercase, containing
/// at least one lowercase) with the given placeholder.
fn regex_replace_all_pascal_case(s: &str, placeholder: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let bytes = s.as_bytes();
    let mut i = 0;

    while i < bytes.len() {
        if bytes[i].is_ascii_uppercase() {
            // Check if this is a PascalCase identifier
            let start = i;
            i += 1;
            while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
                i += 1;
            }
            let word = &s[start..i];
            // Must contain at least one lowercase letter to be PascalCase
            if word.chars().any(|c| c.is_ascii_lowercase()) {
                result.push_str(placeholder);
            } else {
                result.push_str(word);
            }
        } else {
            result.push(bytes[i] as char);
            i += 1;
        }
    }
    result
}

/// Replace parameter names (lowercase word followed by `:`) with placeholder.
fn regex_replace_all_param_names(s: &str, placeholder: &str) -> String {
    let mut result = String::with_capacity(s.len());
    let chars: Vec<char> = s.chars().collect();
    let mut i = 0;

    while i < chars.len() {
        if chars[i].is_ascii_lowercase() {
            let start = i;
            while i < chars.len() && (chars[i].is_ascii_alphanumeric() || chars[i] == '_') {
                i += 1;
            }
            // Check if followed by optional whitespace and colon
            let mut j = i;
            while j < chars.len() && chars[j].is_ascii_whitespace() {
                j += 1;
            }
            if j < chars.len() && chars[j] == ':' {
                result.push_str(placeholder);
                // Keep everything from the end of the word (whitespace + colon)
                for ch in &chars[i..=j] {
                    result.push(*ch);
                }
                i = j + 1;
            } else {
                result.push_str(&s[start..i]);
            }
        } else {
            result.push(chars[i]);
            i += 1;
        }
    }
    result
}

/// Normalize a type string for deep structural comparison by additionally
/// replacing string literal values with `_V_`.
///
/// This catches cases like `spacer: { default?: 'spacerNone' | 'spacerSm' }`
/// vs `gap: { default?: 'gapNone' | 'gapSm' | 'gapXl' }` — the object
/// key structure is the same, just the enum values differ.
fn normalize_type_structure_deep(type_str: &str) -> String {
    let step1 = normalize_type_structure(type_str);
    // Replace quoted string literals: 'someValue' → '_V_'
    let mut result = String::with_capacity(step1.len());
    let mut in_quote = false;
    for ch in step1.chars() {
        if ch == '\'' {
            if !in_quote {
                in_quote = true;
                result.push_str("'_V_'");
            } else {
                in_quote = false;
                // Already pushed the closing quote placeholder
            }
        } else if !in_quote {
            result.push(ch);
        }
        // Characters inside quotes are consumed
    }
    // Collapse repeated `'_V_' | '_V_'` sequences into a single `'_V_'`
    while result.contains("'_V_' | '_V_'") {
        result = result.replace("'_V_' | '_V_'", "'_V_'");
    }
    result
}

/// A detected rename: old name → new name, with the matched symbols.
#[derive(Debug)]
pub(super) struct RenameMatch<'a, M: Default + Clone + PartialEq = ()> {
    pub old: &'a Symbol<M>,
    pub new: &'a Symbol<M>,
}

/// Detect renames among removed and added symbol lists.
///
/// Strategy:
/// 1. Build a fingerprint for each removed and added symbol.
/// 2. Group by fingerprint to find candidate pairs.
/// 3. When exactly one removed matches one added → automatic rename.
/// 4. When multiple match, score by name similarity and greedily assign.
/// 5. Require a minimum similarity threshold to avoid false matches.
pub(super) fn detect_renames<'a, M: Default + Clone + PartialEq>(
    removed: &[&'a Symbol<M>],
    added: &[&'a Symbol<M>],
    same_family: impl Fn(&Symbol<M>, &Symbol<M>) -> bool,
    primitives: &[&str],
) -> Vec<RenameMatch<'a, M>> {
    if removed.is_empty() || added.is_empty() {
        return Vec::new();
    }

    // Group added symbols by fingerprint
    let mut added_by_fp: HashMap<MemberFingerprint, Vec<(usize, &'a Symbol<M>)>> = HashMap::new();
    for (ai, sym) in added.iter().enumerate() {
        let fp = MemberFingerprint::from_symbol(sym);
        added_by_fp.entry(fp).or_default().push((ai, sym));
    }

    // Build candidate pairs: (removed_idx, added_idx, similarity)
    let mut candidates: Vec<(usize, usize, f64)> = Vec::new();

    // Cap: skip fingerprint groups that are too large — too ambiguous
    // for meaningful rename detection and would cause O(n*m) explosion.
    const MAX_GROUP_SIZE: usize = 50;

    // Count removed per fingerprint to apply the cap on both sides
    let mut removed_by_fp: HashMap<MemberFingerprint, usize> = HashMap::new();
    for rsym in removed.iter() {
        let fp = MemberFingerprint::from_symbol(rsym);
        *removed_by_fp.entry(fp).or_default() += 1;
    }

    // ── Similarity thresholds ──────────────────────────────────────────
    //
    // Same-family (0.15): Catches low-similarity renames like "isActive" →
    // "isClicked". Exact type match is the primary signal.
    //
    // Cross-family (0.50): Higher bar for different component directories.
    // Prevents false renames like "DropdownSeparator" → "DrawerPanelDescription".
    //
    // Primitive ambiguous (0.45): When a fingerprint has a primitive return
    // type (boolean, string, etc.) AND the collision group has >2 members on
    // either side, the type provides zero signal. Raise the bar to prevent
    // false matches like "isDisabled" → "hasAnimations" (0.23) from large
    // boolean-prop collision pools. The >2 threshold allows small 1:1 and 2:2
    // groups to use the lower threshold, since those are still constrained
    // enough for greedy matching to work correctly.
    //
    // See design/rename-detector-verification.md for threshold analysis.
    const MIN_SIMILARITY: f64 = 0.15;
    const CROSS_FAMILY_MIN_SIMILARITY: f64 = 0.50;
    const PRIMITIVE_AMBIGUOUS_MIN_SIMILARITY: f64 = 0.45;

    // Compute the effective similarity threshold for a candidate pair.
    let effective_threshold = |rsym: &Symbol<M>,
                               asym: &Symbol<M>,
                               fp: &MemberFingerprint,
                               removed_count: usize,
                               added_count: usize|
     -> f64 {
        if !same_family(rsym, asym) {
            CROSS_FAMILY_MIN_SIMILARITY
        } else if fp.is_primitive_or_absent(primitives) && (removed_count > 2 || added_count > 2) {
            PRIMITIVE_AMBIGUOUS_MIN_SIMILARITY
        } else {
            MIN_SIMILARITY
        }
    };

    for (ri, rsym) in removed.iter().enumerate() {
        let fp = MemberFingerprint::from_symbol(rsym);

        // Skip if either side of this fingerprint group is too large
        let removed_count = removed_by_fp.get(&fp).copied().unwrap_or(0);
        if let Some(added_syms) = added_by_fp.get(&fp) {
            if removed_count > MAX_GROUP_SIZE || added_syms.len() > MAX_GROUP_SIZE {
                continue;
            }
            for (ai, asym) in added_syms {
                let sim = name_similarity(&rsym.name, &asym.name);
                let threshold =
                    effective_threshold(rsym, asym, &fp, removed_count, added_syms.len());
                if sim >= threshold {
                    candidates.push((ri, *ai, sim));
                }
            }
        }
    }

    // ── Pass 2: Structural type fingerprint ────────────────────────────
    //
    // When a prop is renamed AND its type changes (e.g., chips: ToolbarChip[]
    // → labels: ToolbarLabel[]), the exact fingerprint won't match because
    // the return_type differs. But the type STRUCTURE is identical — only
    // the type reference names changed (ToolbarChip → ToolbarLabel).
    //
    // Normalize types by replacing PascalCase identifiers and parameter
    // names with placeholders, then fingerprint on the normalized shape.
    // This matches `(ToolbarChip | string)[]` with `(ToolbarLabel | string)[]`
    // but rejects `boolean` vs `number` or `SplitButtonOptions` vs `ReactNode[]`.
    let mut structural_fp: HashMap<MemberFingerprint, Vec<(usize, &Symbol<M>)>> = HashMap::new();
    for (ai, sym) in added.iter().enumerate() {
        let fp = MemberFingerprint::from_symbol_normalized(sym);
        structural_fp.entry(fp).or_default().push((ai, sym));
    }
    let mut removed_by_structural_fp: HashMap<MemberFingerprint, usize> = HashMap::new();
    for rsym in removed.iter() {
        let fp = MemberFingerprint::from_symbol_normalized(rsym);
        *removed_by_structural_fp.entry(fp).or_default() += 1;
    }

    for (ri, rsym) in removed.iter().enumerate() {
        let fp = MemberFingerprint::from_symbol_normalized(rsym);
        let removed_count = removed_by_structural_fp.get(&fp).copied().unwrap_or(0);

        if let Some(added_syms) = structural_fp.get(&fp) {
            if added_syms.len() > MAX_GROUP_SIZE {
                continue;
            }
            for (ai, asym) in added_syms {
                let already = candidates.iter().any(|(r, a, _)| *r == ri && *a == *ai);
                if already {
                    continue;
                }
                let sim = name_similarity(&rsym.name, &asym.name);
                let threshold =
                    effective_threshold(rsym, asym, &fp, removed_count, added_syms.len());
                if sim >= threshold {
                    candidates.push((ri, *ai, sim));
                }
            }
        }
    }

    // ── Pass 3: Deep structural fingerprint (string literals normalized) ──
    //
    // Catches renames where the enum values also changed alongside the prop
    // name. E.g., spacer: { default?: 'spacerNone' | 'spacerSm' } →
    // gap: { default?: 'gapNone' | 'gapSm' | 'gapXl' }. After deep
    // normalization, both become { _p_: '_V_'; _p_: '_V_'; ... }.
    let mut deep_fp: HashMap<MemberFingerprint, Vec<(usize, &Symbol<M>)>> = HashMap::new();
    for (ai, sym) in added.iter().enumerate() {
        let fp = MemberFingerprint::from_symbol_deep_normalized(sym);
        deep_fp.entry(fp).or_default().push((ai, sym));
    }
    let mut removed_by_deep_fp: HashMap<MemberFingerprint, usize> = HashMap::new();
    for rsym in removed.iter() {
        let fp = MemberFingerprint::from_symbol_deep_normalized(rsym);
        *removed_by_deep_fp.entry(fp).or_default() += 1;
    }

    for (ri, rsym) in removed.iter().enumerate() {
        let fp = MemberFingerprint::from_symbol_deep_normalized(rsym);
        let removed_count = removed_by_deep_fp.get(&fp).copied().unwrap_or(0);

        if let Some(added_syms) = deep_fp.get(&fp) {
            if added_syms.len() > MAX_GROUP_SIZE {
                continue;
            }
            for (ai, asym) in added_syms {
                let already = candidates.iter().any(|(r, a, _)| *r == ri && *a == *ai);
                if already {
                    continue;
                }
                let sim = name_similarity(&rsym.name, &asym.name);
                let threshold =
                    effective_threshold(rsym, asym, &fp, removed_count, added_syms.len());
                if sim >= threshold {
                    candidates.push((ri, *ai, sim));
                }
            }
        }
    }

    // ── Pass 4: Name-similarity fallback for same-interface properties ──
    //
    // When a prop is renamed AND its type fundamentally changes (e.g.,
    // splitButtonOptions: SplitButtonOptions → splitButtonItems: ReactNode[]),
    // all fingerprint passes fail because the types are structurally different.
    // But the names share a long common prefix ("splitButton"), strongly
    // suggesting they're related.
    //
    // For properties on the same parent interface, match by name similarity
    // alone with a higher threshold (≥0.6) to compensate for the lack of
    // type signal. Only considers Property symbols to avoid false matches
    // on methods/functions.
    {
        const NAME_ONLY_SIMILARITY: f64 = 0.6;

        // Group removed and added by parent qualified name
        let mut removed_by_parent: HashMap<&str, Vec<(usize, &Symbol<M>)>> = HashMap::new();
        let mut added_by_parent: HashMap<&str, Vec<(usize, &Symbol<M>)>> = HashMap::new();

        for (ri, rsym) in removed.iter().enumerate() {
            if rsym.kind != SymbolKind::Property {
                continue;
            }
            if let Some((parent, _)) = rsym.qualified_name.rsplit_once('.') {
                removed_by_parent
                    .entry(parent)
                    .or_default()
                    .push((ri, rsym));
            }
        }
        for (ai, asym) in added.iter().enumerate() {
            if asym.kind != SymbolKind::Property {
                continue;
            }
            if let Some((parent, _)) = asym.qualified_name.rsplit_once('.') {
                added_by_parent.entry(parent).or_default().push((ai, asym));
            }
        }

        for (parent, removed_props) in &removed_by_parent {
            let added_props = match added_by_parent.get(parent) {
                Some(a) => a,
                None => continue,
            };

            // Cap to avoid O(n*m) on large interfaces
            if removed_props.len() > MAX_GROUP_SIZE || added_props.len() > MAX_GROUP_SIZE {
                continue;
            }

            for (ri, rsym) in removed_props {
                for (ai, asym) in added_props {
                    let already = candidates.iter().any(|(r, a, _)| *r == *ri && *a == *ai);
                    if already {
                        continue;
                    }
                    let sim = name_similarity(&rsym.name, &asym.name);
                    if sim >= NAME_ONLY_SIMILARITY {
                        candidates.push((*ri, *ai, sim));
                    }
                }
            }
        }
    }

    // Sort by similarity descending (best matches first)
    candidates.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));

    // Greedy matching: assign best pairs, each symbol used at most once
    let mut used_removed = vec![false; removed.len()];
    let mut used_added = vec![false; added.len()];
    let mut matches = Vec::new();

    for (ri, ai, sim) in candidates {
        if sim < MIN_SIMILARITY {
            continue;
        }
        if used_removed[ri] || used_added[ai] {
            continue;
        }
        used_removed[ri] = true;
        used_added[ai] = true;
        matches.push(RenameMatch {
            old: removed[ri],
            new: added[ai],
        });
    }

    matches
}

/// Detect renames among constant/variable symbols using segment-based fuzzy matching.
///
/// Design tokens (e.g., `global_Color_dark_100` → `t_color_dark_100`) can't be
/// matched by type fingerprinting because all tokens share the same shape.
/// Instead, split names on `_`, lowercase, and match by segment set overlap
/// using Jaccard similarity.
///
/// Uses an inverted index for efficiency: each segment maps to the added tokens
/// that contain it, so we only compute Jaccard for candidates sharing segments.
pub(super) fn detect_token_renames<'a, M: Default + Clone + PartialEq>(
    removed: &[&'a Symbol<M>],
    added: &[&'a Symbol<M>],
    fallback_key_fn: impl Fn(&Symbol<M>) -> Option<String>,
) -> Vec<RenameMatch<'a, M>> {
    use std::collections::{BTreeSet, HashSet};

    // Filter to constant/variable symbols only
    let removed_tokens: Vec<(usize, &Symbol<M>, BTreeSet<String>)> = removed
        .iter()
        .enumerate()
        .filter(|(_, s)| matches!(s.kind, SymbolKind::Constant | SymbolKind::Variable))
        .map(|(i, s)| {
            let segments = tokenize_name(&s.name);
            (i, *s, segments)
        })
        .collect();

    let added_tokens: Vec<(usize, &Symbol<M>, BTreeSet<String>)> = added
        .iter()
        .enumerate()
        .filter(|(_, s)| matches!(s.kind, SymbolKind::Constant | SymbolKind::Variable))
        .map(|(i, s)| {
            let segments = tokenize_name(&s.name);
            (i, *s, segments)
        })
        .collect();

    if removed_tokens.is_empty() || added_tokens.is_empty() {
        return Vec::new();
    }

    tracing::debug!(
        removed = removed_tokens.len(),
        added = added_tokens.len(),
        "Starting token rename detection"
    );

    // Build inverted index: segment → list of added token indices
    let mut segment_index: HashMap<String, Vec<usize>> = HashMap::new();
    for (idx, (_, _, segments)) in added_tokens.iter().enumerate() {
        for seg in segments {
            segment_index.entry(seg.clone()).or_default().push(idx);
        }
    }

    // Minimum segment overlap ratio (60% of the smaller set)
    const MIN_JACCARD: f64 = 0.6;

    // For each removed token, find candidates via inverted index
    let mut candidates: Vec<(usize, usize, f64)> = Vec::new(); // (removed_idx, added_idx, jaccard)

    for (ri_local, (_, _, r_segments)) in removed_tokens.iter().enumerate() {
        if r_segments.is_empty() {
            continue;
        }

        // Count hits per added token via inverted index
        let mut hit_counts: HashMap<usize, usize> = HashMap::new();
        for seg in r_segments {
            if let Some(added_indices) = segment_index.get(seg) {
                for &ai in added_indices {
                    *hit_counts.entry(ai).or_default() += 1;
                }
            }
        }

        // Minimum shared segments: 60% of the removed token's segment count,
        // but at least 2 to avoid matching on single common segments like "100"
        let min_shared = (r_segments.len() as f64 * 0.6).ceil() as usize;
        let min_shared = min_shared.max(2);

        for (ai_local, hits) in hit_counts {
            if hits < min_shared {
                continue;
            }

            let a_segments = &added_tokens[ai_local].2;
            let intersection = r_segments.intersection(a_segments).count();
            let union = r_segments.union(a_segments).count();
            let jaccard = if union > 0 {
                intersection as f64 / union as f64
            } else {
                0.0
            };

            if jaccard >= MIN_JACCARD {
                candidates.push((ri_local, ai_local, jaccard));
            }
        }
    }

    // Sort by Jaccard descending (best matches first)
    candidates.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));

    // Greedy assignment: each symbol used at most once
    let mut used_removed = HashSet::new();
    let mut used_added = HashSet::new();
    let mut matches = Vec::new();

    for (ri_local, ai_local, jaccard) in &candidates {
        if used_removed.contains(ri_local) || used_added.contains(ai_local) {
            continue;
        }
        used_removed.insert(*ri_local);
        used_added.insert(*ai_local);

        let old_sym = removed_tokens[*ri_local].1;
        let new_sym = added_tokens[*ai_local].1;

        tracing::debug!(
            old = %old_sym.name,
            new = %new_sym.name,
            jaccard = %jaccard,
            "Token rename matched"
        );

        matches.push(RenameMatch {
            old: old_sym,
            new: new_sym,
        });
    }

    let jaccard_matched = matches.len();

    // ── Value-based fallback for unmatched tokens ───────────────────
    //
    // For tokens that didn't match by name segments, try matching by
    // their resolved value via `fallback_key_fn`. For CSS design tokens
    // this extracts the CSS value (e.g., "#151515", "1rem"); other
    // languages may extract annotation values, enum discriminants, etc.
    // If a removed token has the same value as exactly one added token,
    // it's a likely match.
    //
    // This catches renames where the name changed completely but the
    // underlying value stayed the same.
    {
        // Build a value → added-token-indices map for unmatched added tokens
        let mut value_to_added: HashMap<String, Vec<usize>> = HashMap::new();
        for (ai_local, (_, sym, _)) in added_tokens.iter().enumerate() {
            if used_added.contains(&ai_local) {
                continue;
            }
            if let Some(val) = fallback_key_fn(sym) {
                value_to_added.entry(val).or_default().push(ai_local);
            }
        }

        let mut value_matches = 0usize;
        for (ri_local, (_, sym, _)) in removed_tokens.iter().enumerate() {
            if used_removed.contains(&ri_local) {
                continue;
            }
            let old_value = match fallback_key_fn(sym) {
                Some(v) => v,
                None => continue,
            };

            // Match to the added token with the best segment overlap.
            // Consume the added token exclusively to prevent common values
            // like "0", "1px", "#151515" from creating thousands of bogus matches.
            // Sort candidates by segment overlap so the best match wins.
            if let Some(candidates) = value_to_added.get(&old_value) {
                let old_segments = tokenize_name(&removed_tokens[ri_local].1.name);

                // Sort candidates: those with more segment overlap first
                let mut sorted: Vec<usize> = candidates
                    .iter()
                    .copied()
                    .filter(|ai| !used_added.contains(ai))
                    .collect();
                sorted.sort_by(|a, b| {
                    let seg_a = tokenize_name(&added_tokens[*a].1.name);
                    let seg_b = tokenize_name(&added_tokens[*b].1.name);
                    let overlap_a = old_segments.intersection(&seg_a).count();
                    let overlap_b = old_segments.intersection(&seg_b).count();
                    overlap_b.cmp(&overlap_a)
                });

                if let Some(&ai_local) = sorted.first() {
                    used_removed.insert(ri_local);
                    used_added.insert(ai_local);

                    let old_sym = removed_tokens[ri_local].1;
                    let new_sym = added_tokens[ai_local].1;

                    tracing::debug!(
                        old = %old_sym.name,
                        new = %new_sym.name,
                        value = %old_value,
                        "Token matched by fallback value"
                    );

                    matches.push(RenameMatch {
                        old: old_sym,
                        new: new_sym,
                    });
                    value_matches += 1;
                }
            }
        }

        if value_matches > 0 {
            tracing::info!(value_matches, "Additional tokens matched by value fallback");
        }
    }

    tracing::info!(
        jaccard_matched,
        total_matched = matches.len(),
        removed = removed_tokens.len(),
        added = added_tokens.len(),
        "Token rename detection complete"
    );

    matches
}

/// Split a token name into lowercase segments for fuzzy matching.
///
/// `global_Color_dark_100` → `{"global", "color", "dark", "100"}`
fn tokenize_name(name: &str) -> BTreeSet<String> {
    name.split('_')
        .filter(|s| !s.is_empty())
        .map(|s| s.to_lowercase())
        .collect()
}

/// Compute name similarity between two identifiers.
///
/// Uses longest common subsequence ratio, which handles:
/// - Prefix matches: `isActive` / `isClicked` → share "is"
/// - Suffix matches: `chipGroupContentRef` / `labelGroupContentRef` → share "GroupContentRef"
/// - Substring matches: `hasSelectableInput` / `hasClickableInput` → share "has" + "Input"
///
/// Returns a value in [0.0, 1.0] where 1.0 = identical.
pub(super) fn name_similarity(a: &str, b: &str) -> f64 {
    if a == b {
        return 1.0;
    }
    if a.is_empty() || b.is_empty() {
        return 0.0;
    }

    let lcs_len = longest_common_subsequence_len(a, b);
    let max_len = a.len().max(b.len());
    lcs_len as f64 / max_len as f64
}

/// Length of the longest common subsequence of two strings.
pub(super) fn longest_common_subsequence_len(a: &str, b: &str) -> usize {
    let a_bytes = a.as_bytes();
    let b_bytes = b.as_bytes();
    let m = a_bytes.len();
    let n = b_bytes.len();

    // Space-optimized: only keep two rows
    let mut prev = vec![0usize; n + 1];
    let mut curr = vec![0usize; n + 1];

    for i in 1..=m {
        for j in 1..=n {
            if a_bytes[i - 1] == b_bytes[j - 1] {
                curr[j] = prev[j - 1] + 1;
            } else {
                curr[j] = prev[j].max(curr[j - 1]);
            }
        }
        std::mem::swap(&mut prev, &mut curr);
        curr.iter_mut().for_each(|v| *v = 0);
    }

    prev[n]
}

#[cfg(test)]
mod token_tests {
    use super::*;
    use crate::types::{Signature, Symbol, SymbolKind, Visibility};
    use std::path::PathBuf;

    /// Test helper: extract CSS value from a token symbol's signature.
    /// Moved from the main module to test-only since this is TS-specific logic.
    fn extract_token_value(symbol: &Symbol) -> Option<String> {
        let return_type = symbol.signature.as_ref()?.return_type.as_deref()?;
        let value_start = return_type
            .find("[\"value\"]")
            .or_else(|| return_type.find("\"value\""))?;
        let after_key = &return_type[value_start..];
        let colon_pos = after_key.find(':')?;
        let after_colon = &after_key[colon_pos + 1..];
        let open_quote = after_colon.find('"')?;
        let after_open = &after_colon[open_quote + 1..];
        let close_quote = after_open.find('"')?;
        let value = after_open[..close_quote].to_string();
        if value.is_empty() {
            None
        } else {
            Some(value)
        }
    }

    fn make_token(name: &str, package: &str) -> Symbol {
        Symbol {
            name: name.to_string(),
            qualified_name: format!("{}/{}.{}", package, name, name),
            kind: SymbolKind::Constant,
            visibility: Visibility::Public,
            file: PathBuf::from(format!("{}/{}.d.ts", package, name)),
            package: Some(package.to_string()),
            import_path: None,
            line: 1,
            signature: None,
            extends: None,
            implements: vec![],
            is_abstract: false,
            type_dependencies: vec![],
            is_readonly: false,
            is_static: false,
            accessor_kind: None,
            members: vec![],
            language_data: Default::default(),
        }
    }

    #[test]
    fn test_tokenize_name() {
        let segs = tokenize_name("global_Color_dark_100");
        assert!(segs.contains("global"));
        assert!(segs.contains("color")); // lowercased
        assert!(segs.contains("dark"));
        assert!(segs.contains("100"));
        assert_eq!(segs.len(), 4);
    }

    #[test]
    fn test_token_rename_basic() {
        let old = make_token("global_Color_dark_100", "@patternfly/react-tokens");
        let new = make_token("t_color_dark_100", "@patternfly/react-tokens");

        let removed = vec![&old];
        let added = vec![&new];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].old.name, "global_Color_dark_100");
        assert_eq!(matches[0].new.name, "t_color_dark_100");
    }

    #[test]
    fn test_token_rename_chart_prefix() {
        let old = make_token("global_success_color_100", "@patternfly/react-tokens");
        let new = make_token(
            "t_chart_global_success_color_100",
            "@patternfly/react-tokens",
        );

        let removed = vec![&old];
        let added = vec![&new];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].new.name, "t_chart_global_success_color_100");
    }

    #[test]
    fn test_token_rename_no_false_positive() {
        // Two tokens that share only 1 segment should NOT match
        let old = make_token("global_Color_dark_100", "@patternfly/react-tokens");
        let new = make_token("c_button_FontSize_100", "@patternfly/react-tokens");

        let removed = vec![&old];
        let added = vec![&new];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert!(matches.is_empty(), "Should not match unrelated tokens");
    }

    #[test]
    fn test_token_rename_greedy_best_match() {
        // Two removed tokens competing for the same added token
        let old1 = make_token("global_Color_dark_100", "@patternfly/react-tokens");
        let old2 = make_token("global_Color_dark_200", "@patternfly/react-tokens");
        let new1 = make_token("t_color_dark_100", "@patternfly/react-tokens");
        let new2 = make_token("t_color_dark_200", "@patternfly/react-tokens");

        let removed = vec![&old1, &old2];
        let added = vec![&new1, &new2];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert_eq!(matches.len(), 2);

        // Each old should match its corresponding new (100→100, 200→200)
        let match_map: HashMap<&str, &str> = matches
            .iter()
            .map(|m| (m.old.name.as_str(), m.new.name.as_str()))
            .collect();
        assert_eq!(
            match_map.get("global_Color_dark_100"),
            Some(&"t_color_dark_100")
        );
        assert_eq!(
            match_map.get("global_Color_dark_200"),
            Some(&"t_color_dark_200")
        );
    }

    #[test]
    fn test_token_rename_many_to_one_resolved() {
        // Multiple removed tokens could match the same added token,
        // but greedy assignment picks the best one
        let old1 = make_token("global_Color_dark_100", "@patternfly/react-tokens");
        let old2 = make_token(
            "global_BackgroundColor_dark_100",
            "@patternfly/react-tokens",
        );
        let new = make_token("t_color_dark_100", "@patternfly/react-tokens");

        let removed = vec![&old1, &old2];
        let added = vec![&new];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        // Only one can match — the one with higher Jaccard wins
        assert_eq!(matches.len(), 1);
        // global_Color_dark_100 has Jaccard 3/5=0.6, global_BackgroundColor_dark_100 has 3/6=0.5
        assert_eq!(matches[0].old.name, "global_Color_dark_100");
    }

    #[test]
    fn test_token_rename_skips_non_constants() {
        // Interface symbols should be skipped (handled by detect_renames)
        let old = Symbol {
            kind: SymbolKind::Interface,
            ..make_token("ModalProps", "@patternfly/react-core")
        };
        let new = Symbol {
            kind: SymbolKind::Interface,
            ..make_token("ContentProps", "@patternfly/react-core")
        };

        let removed = vec![&old];
        let added = vec![&new];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert!(matches.is_empty());
    }

    #[test]
    fn test_token_rename_case_insensitive() {
        // BackgroundColor vs backgroundcolor should match (lowercased)
        let old = make_token(
            "global_BackgroundColor_dark_100",
            "@patternfly/react-tokens",
        );
        let new = make_token("t_backgroundcolor_dark_100", "@patternfly/react-tokens");

        let removed = vec![&old];
        let added = vec![&new];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        // Segments: {global, backgroundcolor, dark, 100} vs {t, backgroundcolor, dark, 100}
        // Intersection: {backgroundcolor, dark, 100} = 3, Union = 5, Jaccard = 0.6
        assert_eq!(matches.len(), 1);
    }

    // ── Value extraction tests ──────────────────────────────────────

    fn make_token_with_value(name: &str, package: &str, css_name: &str, css_value: &str) -> Symbol {
        let mut sym = make_token(name, package);
        sym.signature = Some(Signature {
            parameters: Vec::new(),
            return_type: Some(format!(
                "{{ [\"name\"]: \"{}\"; [\"value\"]: \"{}\"; [\"var\"]: \"var({})\" }}",
                css_name, css_value, css_name,
            )),
            type_parameters: Vec::new(),
            is_async: false,
        });
        sym
    }

    // extract_token_value tests moved to TypeScript crate (language.rs)
    // as they test TS-specific CSS value parsing.

    // ── Value-based matching tests ──────────────────────────────────

    #[test]
    fn test_value_fallback_matches_when_name_doesnt() {
        // Names are completely different — Jaccard would fail.
        // But values are the same → value-based fallback should match.
        let old = make_token_with_value(
            "global_Color_dark_100",
            "@patternfly/react-tokens",
            "--pf-v5-global--Color--dark-100",
            "#151515",
        );
        let new = make_token_with_value(
            "t_global_background_color_primary_default",
            "@patternfly/react-tokens",
            "--pf-t--global--background--color--primary--default",
            "#151515",
        );

        let removed = vec![&old];
        let added = vec![&new];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert_eq!(matches.len(), 1, "Should match by value when names diverge");
        assert_eq!(matches[0].old.name, "global_Color_dark_100");
        assert_eq!(
            matches[0].new.name,
            "t_global_background_color_primary_default"
        );
    }

    #[test]
    fn test_value_fallback_picks_best_segment_overlap() {
        // Two added tokens share the same value — pick the one with more
        // name segment overlap with the old token.
        let old = make_token_with_value(
            "global_spacer_xl",
            "@patternfly/react-tokens",
            "--pf-v5-global--spacer--xl",
            "2rem",
        );
        let new1 = make_token_with_value(
            "t_global_spacer_xl",
            "@patternfly/react-tokens",
            "--pf-t--global--spacer--xl",
            "2rem",
        );
        let new2 = make_token_with_value(
            "t_layout_padding_horizontal",
            "@patternfly/react-tokens",
            "--pf-t--layout--padding--horizontal",
            "2rem",
        );

        let removed = vec![&old];
        let added = vec![&new1, &new2];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert_eq!(matches.len(), 1, "Should match to the best candidate");
        assert_eq!(
            matches[0].new.name, "t_global_spacer_xl",
            "Should prefer the candidate with more segment overlap"
        );
    }

    #[test]
    fn test_value_fallback_doesnt_override_jaccard() {
        // A token that already matched by Jaccard should not be re-matched by value.
        let old = make_token_with_value(
            "global_Color_dark_100",
            "@patternfly/react-tokens",
            "--pf-v5-global--Color--dark-100",
            "#151515",
        );
        let new_jaccard = make_token_with_value(
            "t_color_dark_100",
            "@patternfly/react-tokens",
            "--pf-t--color--dark--100",
            "#222222", // Different value — but name Jaccard matches
        );
        let new_value = make_token_with_value(
            "t_something_completely_different",
            "@patternfly/react-tokens",
            "--pf-t--something",
            "#151515", // Same value — but name doesn't match
        );

        let removed = vec![&old];
        let added = vec![&new_jaccard, &new_value];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert_eq!(matches.len(), 1);
        // Should match by Jaccard (name), not value
        assert_eq!(matches[0].new.name, "t_color_dark_100");
    }

    #[test]
    fn test_value_fallback_exclusive() {
        // Only one removed token should match when there's one added token
        // with the same value. Exclusive matching prevents common values
        // like "#151515" from creating thousands of bogus matches.
        let old_component = make_token_with_value(
            "c_accordion_toggle_Color",
            "@patternfly/react-tokens",
            "--pf-v5-c-accordion--toggle--Color",
            "#151515",
        );
        let old_global = make_token_with_value(
            "global_Color_dark_100",
            "@patternfly/react-tokens",
            "--pf-v5-global--Color--dark-100",
            "#151515",
        );
        let new_token = make_token_with_value(
            "t_global_text_color_regular",
            "@patternfly/react-tokens",
            "--pf-t--global--text--color--regular",
            "#151515",
        );

        let removed = vec![&old_component, &old_global];
        let added = vec![&new_token];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        // Only one should match — exclusive consumption
        assert_eq!(
            matches.len(),
            1,
            "Only one removed token should match (exclusive). Got: {:?}",
            matches.iter().map(|m| &m.old.name).collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_value_fallback_prefers_segment_overlap() {
        // When multiple removed tokens share a value and there's one added
        // token, both get matched, but each picks the best overlap.
        let old1 = make_token_with_value(
            "global_spacer_md",
            "@patternfly/react-tokens",
            "--pf-v5-global--spacer--md",
            "1rem",
        );
        let old2 = make_token_with_value(
            "c_button_padding",
            "@patternfly/react-tokens",
            "--pf-v5-c-button--padding",
            "1rem",
        );
        let new_global = make_token_with_value(
            "t_global_spacer_md",
            "@patternfly/react-tokens",
            "--pf-t--global--spacer--md",
            "1rem",
        );
        let new_component = make_token_with_value(
            "c_button_PaddingInline",
            "@patternfly/react-tokens",
            "--pf-v6-c-button--PaddingInline",
            "1rem",
        );

        let removed = vec![&old1, &old2];
        let added = vec![&new_global, &new_component];

        let matches = detect_token_renames(&removed, &added, extract_token_value);
        assert_eq!(matches.len(), 2);

        // global_spacer_md should match t_global_spacer_md (better overlap)
        let global_match = matches.iter().find(|m| m.old.name == "global_spacer_md");
        assert!(global_match.is_some());
        assert_eq!(global_match.unwrap().new.name, "t_global_spacer_md");
    }

    // ── Integration: full PF token surface rename detection ─────────

    /// Fixture entry for a Symbol from the JSON fixture file.
    #[derive(serde::Deserialize)]
    struct FixtureSymbol {
        kind: String,
        name: String,
        return_type: Option<String>,
    }

    #[derive(serde::Deserialize)]
    struct ExpectedRename {
        old: String,
        new: String,
    }

    #[derive(serde::Deserialize)]
    struct TokenSurfaceFixture {
        old_symbols: Vec<FixtureSymbol>,
        new_symbols: Vec<FixtureSymbol>,
        expected_renames: Vec<ExpectedRename>,
    }

    fn fixture_to_symbol(fs: &FixtureSymbol, pkg: &str) -> Symbol {
        let kind = match fs.kind.as_str() {
            "constant" => SymbolKind::Constant,
            "variable" => SymbolKind::Variable,
            _ => SymbolKind::Constant,
        };
        let signature = fs.return_type.as_ref().map(|rt| Signature {
            parameters: vec![],
            return_type: Some(rt.clone()),
            type_parameters: vec![],
            is_async: false,
        });
        Symbol {
            name: fs.name.clone(),
            qualified_name: format!("{}.{}", pkg, fs.name),
            kind,
            visibility: Visibility::Public,
            file: PathBuf::from(format!("packages/react-tokens/src/{}.d.ts", fs.name)),
            package: Some(pkg.to_string()),
            import_path: None,
            line: 1,
            signature,
            extends: None,
            implements: vec![],
            is_abstract: false,
            type_dependencies: vec![],
            is_readonly: false,
            is_static: false,
            accessor_kind: None,
            members: vec![],
            language_data: Default::default(),
        }
    }

    /// Tests `detect_token_renames` with the full set of ~3662 old and ~2142
    /// new symbols from the real PatternFly v5.4.0 → v6.4.1 react-tokens
    /// package.
    ///
    /// Verifies:
    /// 1. The algorithm produces rename matches (not zero).
    /// 2. All matched pairs have clean names (no symbol_summary strings).
    /// 3. The Jaccard + value-based fallback matching achieves a reasonable
    ///    accuracy rate against the expected pairings from the report.
    #[test]
    fn test_full_patternfly_token_rename_detection() {
        let fixture_data = include_str!("../../tests/fixtures/token_surfaces.json");
        let fixture: TokenSurfaceFixture =
            serde_json::from_str(fixture_data).expect("failed to parse token_surfaces.json");

        let pkg = "@patternfly/react-tokens";

        let old_symbols: Vec<Symbol> = fixture
            .old_symbols
            .iter()
            .map(|fs| fixture_to_symbol(fs, pkg))
            .collect();
        let new_symbols: Vec<Symbol> = fixture
            .new_symbols
            .iter()
            .map(|fs| fixture_to_symbol(fs, pkg))
            .collect();

        assert!(
            old_symbols.len() > 3500,
            "Expected 3500+ old symbols, got {}",
            old_symbols.len()
        );
        assert!(
            new_symbols.len() > 2000,
            "Expected 2000+ new symbols, got {}",
            new_symbols.len()
        );

        let old_refs: Vec<&Symbol> = old_symbols.iter().collect();
        let new_refs: Vec<&Symbol> = new_symbols.iter().collect();

        let matches = detect_token_renames(&old_refs, &new_refs, extract_token_value);

        // 1. Should produce a meaningful number of matches
        assert!(
            matches.len() > 1000,
            "Expected 1000+ rename matches, got {}",
            matches.len()
        );

        // 2. All matched pairs should have clean names
        for m in &matches {
            assert!(
                !m.old.name.contains("variable: ") && !m.old.name.contains("constant: "),
                "Old name is a symbol_summary: {}",
                m.old.name
            );
            assert!(
                !m.new.name.contains("variable: ") && !m.new.name.contains("constant: "),
                "New name is a symbol_summary: {}",
                m.new.name
            );
        }

        // 3. Check accuracy against expected pairings
        let expected_map: HashMap<String, String> = fixture
            .expected_renames
            .iter()
            .map(|e| (e.old.clone(), e.new.clone()))
            .collect();

        let match_map: HashMap<String, String> = matches
            .iter()
            .map(|m| (m.old.name.clone(), m.new.name.clone()))
            .collect();

        let mut correct = 0usize;
        let mut wrong = 0usize;
        let mut missing = 0usize;

        for (old_name, expected_new) in &expected_map {
            match match_map.get(old_name) {
                Some(actual_new) if actual_new == expected_new => correct += 1,
                Some(_) => wrong += 1,
                None => missing += 1,
            }
        }

        let total_expected = expected_map.len();
        let accuracy = if total_expected > 0 {
            correct as f64 / total_expected as f64
        } else {
            0.0
        };

        eprintln!(
            "Token rename detection results:\n  \
             Total matches: {}\n  \
             Expected renames: {}\n  \
             Correct: {} ({:.1}%)\n  \
             Wrong target: {}\n  \
             Not found: {}",
            matches.len(),
            total_expected,
            correct,
            accuracy * 100.0,
            wrong,
            missing,
        );

        // Baseline accuracy: ~42% with Jaccard + value-based fallback on the
        // full token pool (3662 old × 2142 new).  Most mismatches are from
        // similar suffixes (PaddingTop vs PaddingBottom, MarginLeft vs
        // MarginRight) where the segment overlap is too close to disambiguate.
        //
        // Raise this threshold as the algorithm improves.
        assert!(
            accuracy >= 0.40,
            "Accuracy regressed: {:.1}% ({} / {}), baseline is ~42%",
            accuracy * 100.0,
            correct,
            total_expected
        );

        // Spot-check specific tokens that must match correctly
        let critical_tokens = [
            (
                "global_success_color_100",
                "t_chart_global_success_color_100",
            ),
            (
                "global_warning_color_100",
                "t_chart_global_warning_color_100",
            ),
            ("global_danger_color_100", "t_chart_global_danger_color_100"),
        ];

        for (old_name, expected_new) in &critical_tokens {
            if let Some(actual_new) = match_map.get(*old_name) {
                // Accept any reasonable match — the critical check is that
                // it finds SOME match, not the specific target, since the
                // algorithm's pairing depends on the full candidate pool.
                assert!(
                    !actual_new.is_empty(),
                    "{} matched to empty string",
                    old_name
                );
                eprintln!(
                    "  {}{} (expected {})",
                    old_name, actual_new, expected_new
                );
            }
            // Some critical tokens may not match in this pool — that's OK,
            // the value-based fallback depends on type annotations.
        }
    }

    fn make_sym(name: &str, kind: SymbolKind, return_type: &str) -> Symbol {
        Symbol {
            name: name.to_string(),
            qualified_name: name.to_string(),
            kind,
            visibility: Visibility::Public,
            file: PathBuf::from("test.d.ts"),
            package: Some("@test/pkg".to_string()),
            import_path: None,
            line: 1,
            signature: if return_type.is_empty() {
                None
            } else {
                Some(Signature {
                    return_type: Some(return_type.to_string()),
                    parameters: vec![],
                    is_async: false,
                    type_parameters: vec![],
                })
            },
            extends: None,
            implements: vec![],
            is_abstract: false,
            type_dependencies: vec![],
            is_readonly: false,
            is_static: false,
            accessor_kind: None,
            members: vec![],
            language_data: Default::default(),
        }
    }

    fn make_prop(name: &str, parent: &str, return_type: &str) -> Symbol {
        Symbol {
            name: name.to_string(),
            qualified_name: format!("{}.{}", parent, name),
            kind: SymbolKind::Property,
            visibility: Visibility::Public,
            file: PathBuf::from("test.d.ts"),
            package: Some("@test/pkg".to_string()),
            import_path: None,
            line: 1,
            signature: Some(Signature {
                return_type: Some(return_type.to_string()),
                parameters: vec![],
                is_async: false,
                type_parameters: vec![],
            }),
            extends: None,
            implements: vec![],
            is_abstract: false,
            type_dependencies: vec![],
            is_readonly: false,
            is_static: false,
            accessor_kind: None,
            members: vec![],
            language_data: Default::default(),
        }
    }

    #[test]
    fn test_pass4_name_similarity_same_interface() {
        // Pass 4: different types on same interface, matched by name similarity
        let removed = [make_prop(
            "splitButtonOptions",
            "MenuToggle.MenuToggleProps",
            "SplitButtonOptions",
        )];
        let added = [make_prop(
            "splitButtonItems",
            "MenuToggle.MenuToggleProps",
            "ReactNode[]",
        )];

        let removed_refs: Vec<&Symbol> = removed.iter().collect();
        let added_refs: Vec<&Symbol> = added.iter().collect();
        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| true,
            &["string", "number", "boolean", "void", "null"],
        );

        assert_eq!(matches.len(), 1, "Should match via Pass 4 name similarity");
        assert_eq!(matches[0].old.name, "splitButtonOptions");
        assert_eq!(matches[0].new.name, "splitButtonItems");
    }

    #[test]
    fn test_pass4_rejects_low_similarity() {
        // Names are too different — should NOT match
        let removed = [make_prop("isOpen", "Dropdown.DropdownProps", "boolean")];
        let added = [make_prop("isDisabled", "Dropdown.DropdownProps", "string")];

        let removed_refs: Vec<&Symbol> = removed.iter().collect();
        let added_refs: Vec<&Symbol> = added.iter().collect();
        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| true,
            &["string", "number", "boolean", "void", "null"],
        );

        // similarity("isOpen", "isDisabled") ≈ 0.4 — below 0.6 threshold
        assert!(
            matches.is_empty(),
            "Should not match props with low name similarity"
        );
    }

    #[test]
    fn test_pass4_different_interfaces_no_match() {
        // Same name pattern but different parent interfaces — should NOT match
        let removed = [make_prop(
            "splitButtonOptions",
            "MenuToggle.MenuToggleProps",
            "SplitButtonOptions",
        )];
        let added = [make_prop(
            "splitButtonItems",
            "Button.ButtonProps",
            "ReactNode[]",
        )];

        let removed_refs: Vec<&Symbol> = removed.iter().collect();
        let added_refs: Vec<&Symbol> = added.iter().collect();
        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| true,
            &["string", "number", "boolean", "void", "null"],
        );

        assert!(
            matches.is_empty(),
            "Should not match props from different interfaces"
        );
    }

    #[test]
    fn test_cross_family_threshold_rejects_false_renames() {
        // DropdownSeparator → DrawerPanelDescription (similarity 0.36)
        // Different families (Dropdown/ vs Drawer/) — should be rejected
        // by the 0.50 cross-family threshold.
        let removed = [make_sym(
            "DropdownSeparator",
            SymbolKind::Constant,
            "FunctionComponent<SeparatorProps>",
        )];
        let added = [make_sym(
            "DrawerPanelDescription",
            SymbolKind::Constant,
            "FunctionComponent<DrawerPanelDescriptionProps>",
        )];

        let removed_refs: Vec<&Symbol> = removed.iter().collect();
        let added_refs: Vec<&Symbol> = added.iter().collect();

        // With same_family = false, should be rejected (0.36 < 0.50)
        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| false,
            &["string", "number", "boolean", "void", "null"],
        );
        assert!(
            matches.is_empty(),
            "Cross-family rename with similarity 0.36 should be rejected"
        );

        // With same_family = true, should be accepted (0.36 >= 0.15)
        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| true,
            &["string", "number", "boolean", "void", "null"],
        );
        assert_eq!(
            matches.len(),
            1,
            "Same-family rename with similarity 0.36 should be accepted"
        );
    }

    #[test]
    fn test_cross_family_threshold_preserves_true_renames() {
        // TextVariants → ContentVariants (similarity 0.667)
        // Different families (Text/ vs Content/) — should PASS the 0.50
        // cross-family threshold.
        let removed = [make_sym("TextVariants", SymbolKind::TypeAlias, "")];
        let added = [make_sym("ContentVariants", SymbolKind::TypeAlias, "")];

        let removed_refs: Vec<&Symbol> = removed.iter().collect();
        let added_refs: Vec<&Symbol> = added.iter().collect();

        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| false,
            &["string", "number", "boolean", "void", "null"],
        );
        assert_eq!(
            matches.len(),
            1,
            "Cross-family rename with similarity 0.667 should be accepted"
        );
        assert_eq!(matches[0].old.name, "TextVariants");
        assert_eq!(matches[0].new.name, "ContentVariants");
    }

    #[test]
    fn test_primitive_ambiguous_guard_rejects_false_boolean_renames() {
        // Simulate DualListSelector: many boolean props removed and added.
        // isDisabled → hasAnimations (sim 0.231) should be rejected because
        // the boolean fingerprint group has >2 members on the removed side,
        // triggering the primitive-ambiguous guard (threshold 0.45).
        let removed = [
            make_prop("isDisabled", "DLS.DLSProps", "boolean"),
            make_prop("isSearchable", "DLS.DLSProps", "boolean"),
            make_prop("isCompact", "DLS.DLSProps", "boolean"),
        ];
        let added = [
            make_prop("hasAnimations", "DLS.DLSProps", "boolean"),
            make_prop("hasCheckbox", "DLS.DLSProps", "boolean"),
            make_prop("hasTooltip", "DLS.DLSProps", "boolean"),
        ];

        let removed_refs: Vec<&Symbol> = removed.iter().collect();
        let added_refs: Vec<&Symbol> = added.iter().collect();
        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| true,
            &["string", "number", "boolean", "void", "null"],
        );

        // None of these boolean props should match — all similarities are
        // below 0.45 and the group is ambiguous (3 removed, 2 added).
        assert!(
            matches.is_empty(),
            "Ambiguous boolean prop group should not produce false renames, got: {:?}",
            matches
                .iter()
                .map(|m| (&m.old.name, &m.new.name))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_primitive_guard_allows_1to1_boolean_rename() {
        // When there's only 1 boolean removed and 1 boolean added (no
        // ambiguity), the primitive guard should NOT apply.
        let removed = [make_prop("isActive", "Button.ButtonProps", "boolean")];
        let added = [make_prop("isClicked", "Button.ButtonProps", "boolean")];

        let removed_refs: Vec<&Symbol> = removed.iter().collect();
        let added_refs: Vec<&Symbol> = added.iter().collect();
        let matches = detect_renames(
            &removed_refs,
            &added_refs,
            |_, _| true,
            &["string", "number", "boolean", "void", "null"],
        );

        // 1:1 group → primitive guard doesn't apply → 0.15 threshold → 0.444 passes
        assert_eq!(matches.len(), 1, "1:1 boolean rename should still match");
        assert_eq!(matches[0].old.name, "isActive");
        assert_eq!(matches[0].new.name, "isClicked");
    }

    // ── Generic parameter stripping in normalize_type_structure ──

    #[test]
    fn test_strip_normalized_generics_basic() {
        // ReactElement<any> → _T_<any> → _T_
        assert_eq!(normalize_type_structure("ReactElement<any>"), "_T_");
        // ReactElement (no generics) → _T_
        assert_eq!(normalize_type_structure("ReactElement"), "_T_");
        // Both normalize to the same thing
        assert_eq!(
            normalize_type_structure("ReactElement"),
            normalize_type_structure("ReactElement<any>"),
        );
    }

    #[test]
    fn test_strip_normalized_generics_nested() {
        // RefObject<HTMLDivElement> → _T_<_T_> → _T_
        assert_eq!(normalize_type_structure("RefObject<HTMLDivElement>"), "_T_");
        // RefObject<HTMLDivElement | null> → _T_<_T_ | null> → _T_
        assert_eq!(
            normalize_type_structure("RefObject<HTMLDivElement | null>"),
            "_T_"
        );
        // Both normalize to the same thing
        assert_eq!(
            normalize_type_structure("RefObject<HTMLDivElement>"),
            normalize_type_structure("RefObject<HTMLDivElement | null>"),
        );
    }

    #[test]
    fn test_strip_normalized_generics_multiple_params() {
        // Map<string, number> → _T_<string, number> → _T_
        assert_eq!(normalize_type_structure("Map<string, number>"), "_T_");
    }

    #[test]
    fn test_strip_normalized_generics_nested_angle_brackets() {
        // Record<string, Set<number>> → _T_<string, _T_<number>> → _T_
        assert_eq!(
            normalize_type_structure("Record<string, Set<number>>"),
            "_T_"
        );
    }

    #[test]
    fn test_strip_normalized_generics_preserves_non_generic_types() {
        // Union types without generics should be unchanged
        assert_eq!(
            normalize_type_structure("(ToolbarChip | string)[]"),
            "(_T_ | string)[]"
        );
        // Primitive types unchanged
        assert_eq!(normalize_type_structure("string"), "string");
        assert_eq!(normalize_type_structure("boolean"), "boolean");
    }

    #[test]
    fn test_strip_normalized_generics_in_function_params() {
        // Function type with generic params
        // (cb: ReactElement<any>) => void
        assert_eq!(
            normalize_type_structure("(cb: ReactElement<any>) => void"),
            "(_p_: _T_) => void"
        );
        // Same function without generics should match
        assert_eq!(
            normalize_type_structure("(cb: ReactElement) => void"),
            "(_p_: _T_) => void"
        );
    }

    #[test]
    fn test_strip_normalized_generics_react_element_equivalence() {
        // The specific case from FormGroupProps:
        // labelIcon: ReactElement vs labelHelp: ReactElement<any>
        let old_type = normalize_type_structure("ReactElement");
        let new_type = normalize_type_structure("ReactElement<any>");
        assert_eq!(
            old_type, new_type,
            "ReactElement and ReactElement<any> should produce identical normalized forms"
        );
    }

    // ── Member rename detection with generic parameter differences ──

    #[test]
    fn test_member_rename_detected_despite_generic_param_difference() {
        // Simulates FormGroup.labelIcon: ReactElement → labelHelp: ReactElement<any>
        // The rename should be detected because after normalization,
        // both types produce the same fingerprint.
        let old_member = make_prop("labelIcon", "FormGroupProps", "ReactElement");
        let new_member = make_prop("labelHelp", "FormGroupProps", "ReactElement<any>");

        // Verify normalized fingerprints match
        let old_fp = MemberFingerprint::from_symbol_normalized(&old_member);
        let new_fp = MemberFingerprint::from_symbol_normalized(&new_member);
        assert_eq!(
            old_fp, new_fp,
            "Normalized fingerprints should match: ReactElement and ReactElement<any> \
             should produce the same normalized return type after generic stripping"
        );

        // Verify the rename is detected
        let matches = detect_renames(
            &[&old_member],
            &[&new_member],
            |_, _| true, // same family — both are on FormGroupProps
            &["string", "number", "boolean", "void", "null"],
        );
        assert_eq!(
            matches.len(),
            1,
            "labelIcon → labelHelp should be detected as a rename"
        );
        assert_eq!(matches[0].old.name, "labelIcon");
        assert_eq!(matches[0].new.name, "labelHelp");
    }
}