debtmap 0.16.4

Code complexity and technical debt 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
use super::function_name_matching::{
    extract_closure_parent, function_names_match, generate_function_name_variants, MatchConfidence,
};
use super::lcov::{normalize_demangled_name, strip_trailing_generics, FunctionCoverage, LcovData};
use super::path_normalization::normalize_path_components;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};

/// Normalize a path by removing leading ./
///
/// # Deprecated
///
/// This function is deprecated in favor of `normalize_path_components()` which
/// provides better cross-platform support. It is kept for backward compatibility
/// and will be removed in a future version.
#[deprecated(
    since = "0.1.0",
    note = "Use normalize_path_components() for better cross-platform support"
)]
pub fn normalize_path(path: &Path) -> PathBuf {
    let path_str = path.to_string_lossy();
    let cleaned = path_str.strip_prefix("./").unwrap_or(&path_str);
    PathBuf::from(cleaned)
}

/// Generate name variants for coverage matching
///
/// For trait implementation methods, LCOV may store just the method name
/// (e.g., `visit_expr`) while debtmap stores the full qualified name
/// (e.g., `RecursiveMatchDetector::visit_expr`).
///
/// This function generates variant names to try during coverage lookup:
/// 1. Method name only (last segment after `::`), if the name contains `::`
///
/// # Examples
///
/// ```
/// # use debtmap::risk::coverage_index::generate_name_variants;
/// let variants: Vec<String> = generate_name_variants("RecursiveMatchDetector::visit_expr").collect();
/// assert_eq!(variants, vec!["visit_expr"]);
///
/// let variants: Vec<String> = generate_name_variants("simple_function").collect();
/// assert_eq!(variants.len(), 0); // No variants for simple functions
/// ```
///
/// # Performance
///
/// O(1) time complexity - only splits on `::` delimiter
pub fn generate_name_variants(function_name: &str) -> impl Iterator<Item = String> + '_ {
    // Extract method name from "Type::method" or "path::to::Type::method"
    function_name
        .rsplit("::")
        .next()
        .filter(|method_name| {
            // Only generate variant if:
            // 1. The original name contains :: (is qualified)
            // 2. The method name is different from the full name
            function_name.contains("::") && *method_name != function_name
        })
        .map(|s| s.to_string())
        .into_iter()
}

/// Aggregated coverage from multiple monomorphized versions of a generic function
///
/// Uses intersection strategy: a line is covered if ANY monomorphization covers it
/// (i.e., uncovered only if ALL versions leave it uncovered).
#[derive(Debug, Clone)]
pub struct AggregateCoverage {
    /// Aggregate coverage percentage (averaged across all versions)
    pub coverage_pct: f64,
    /// Intersection of uncovered lines across all versions
    pub uncovered_lines: Vec<usize>,
    /// Number of monomorphized versions found
    pub version_count: usize,
}

impl AggregateCoverage {
    /// Create an aggregate from a single function (no monomorphization)
    fn single(func: &FunctionCoverage) -> Self {
        Self {
            coverage_pct: func.coverage_percentage,
            uncovered_lines: func.uncovered_lines.clone(),
            version_count: 1,
        }
    }
}

/// Pure function: Merge coverage data from multiple monomorphizations.
///
/// Uses intersection strategy: a line is uncovered only if ALL versions leave it uncovered.
/// This conservative approach ensures we don't claim coverage that doesn't exist in all paths.
///
/// # Strategy
///
/// - A line is **covered** if ANY monomorphization covers it
/// - A line is **uncovered** only if ALL monomorphizations leave it uncovered
/// - Coverage percentage is averaged across all versions
///
/// # Examples
///
/// For a generic function with two monomorphizations:
/// - `execute::<WorkflowExecutor>` - 70% coverage, uncovered: [10, 20, 30]
/// - `execute::<MockExecutor>` - 80% coverage, uncovered: [20, 40]
///
/// Result: 75% coverage (average), uncovered: \[20\] (intersection - only line uncovered in BOTH)
///
/// # Performance
///
/// O(m*n) time complexity where m is number of monomorphizations
/// and n is average number of uncovered lines.
fn merge_coverage(coverages: Vec<&FunctionCoverage>) -> AggregateCoverage {
    if coverages.is_empty() {
        return AggregateCoverage {
            coverage_pct: 0.0,
            uncovered_lines: vec![],
            version_count: 0,
        };
    }

    if coverages.len() == 1 {
        return AggregateCoverage {
            coverage_pct: coverages[0].coverage_percentage,
            uncovered_lines: coverages[0].uncovered_lines.clone(),
            version_count: 1,
        };
    }

    // Intersection strategy: line is uncovered only if ALL versions leave it uncovered
    let mut uncovered_in_all: HashSet<usize> =
        coverages[0].uncovered_lines.iter().copied().collect();

    for coverage in &coverages[1..] {
        let uncovered_set: HashSet<usize> = coverage.uncovered_lines.iter().copied().collect();
        uncovered_in_all = uncovered_in_all
            .intersection(&uncovered_set)
            .copied()
            .collect();
    }

    // Average coverage percentage across all versions (Spec 214 fix)
    let mut sorted_coverages = coverages;
    sorted_coverages.sort_by(|a, b| a.name.cmp(&b.name));

    let version_count = sorted_coverages.len();
    let avg_coverage: f64 = sorted_coverages
        .iter()
        .map(|c| c.coverage_percentage)
        .sum::<f64>()
        / version_count as f64;

    let mut uncovered_lines: Vec<usize> = uncovered_in_all.into_iter().collect();
    uncovered_lines.sort_unstable();

    AggregateCoverage {
        coverage_pct: avg_coverage,
        uncovered_lines,
        version_count,
    }
}

/// Pre-indexed coverage data for O(1) function lookups
///
/// # Data Structure
///
/// Uses nested HashMap for efficient lookups:
/// - Outer map: file path → functions in that file
/// - Inner map: function name → coverage data
///
/// # Performance Characteristics
///
/// - **Build Time**: O(n) where n = coverage records
/// - **Exact Match Lookup**: O(1) - file hash + function hash
/// - **Path Strategy Lookup**: O(m) where m = number of files
/// - **Memory**: ~200 bytes per function + ~100 bytes per file
///
/// # Lookup Strategies
///
/// 1. **Exact match**: O(1) hash lookups
/// 2. **Suffix matching**: O(files) iteration + O(1) lookup
/// 3. **Normalized equality**: O(files) iteration + O(1) lookup
///
/// The nested structure ensures we only iterate over files (typically ~375)
/// not functions (typically ~1,500), providing 4x-50x speedup for path matching.
///
/// # Usage
///
/// Build the index once from parsed LCOV data, then share it across threads
/// using `Arc` for concurrent access. The index provides O(1) lookups by
/// file path and function name, making it efficient for parallel analysis
/// of large codebases.
#[derive(Debug, Clone)]
pub struct CoverageIndex {
    /// Nested structure: file → (function_name → coverage)
    /// Enables O(1) file lookup followed by O(1) function lookup
    by_file: HashMap<PathBuf, HashMap<String, FunctionCoverage>>,

    /// Coverage records indexed by file path + line number for range queries
    /// BTreeMap allows efficient range queries for finding functions by line
    by_line: HashMap<PathBuf, BTreeMap<usize, FunctionCoverage>>,

    /// Pre-computed set of all file paths for faster iteration in fallback strategies
    file_paths: Vec<PathBuf>,

    /// Index from base function name to all monomorphized versions
    /// Maps (file, base_name) -> \[monomorphized_names\] for O(1) generic function lookup
    base_function_index: HashMap<(PathBuf, String), Vec<String>>,

    /// Index from method name to actual function names for trait method matching
    /// Maps (file, method_name) -> \[actual_function_names\] for O(1) variant matching
    /// Example: (recursive_detector.rs, "visit_expr") -> ["_RNvXs0_...visit_expr"]
    method_name_index: HashMap<(PathBuf, String), Vec<String>>,

    /// Statistics for debugging and monitoring
    stats: CoverageIndexStats,
}

/// Statistics about coverage index for observability
#[derive(Debug, Clone)]
pub struct CoverageIndexStats {
    pub total_files: usize,
    pub total_records: usize,
    pub index_build_time: Duration,
    pub estimated_memory_bytes: usize,
}

impl CoverageIndex {
    /// Create an empty coverage index
    pub fn empty() -> Self {
        CoverageIndex {
            by_file: HashMap::new(),
            by_line: HashMap::new(),
            file_paths: Vec::new(),
            base_function_index: HashMap::new(),
            method_name_index: HashMap::new(),
            stats: CoverageIndexStats {
                total_files: 0,
                total_records: 0,
                index_build_time: Duration::from_secs(0),
                estimated_memory_bytes: 0,
            },
        }
    }

    /// Build coverage index from LCOV data (O(n) operation)
    ///
    /// This creates four indexes:
    /// 1. Nested HashMap for O(1) file + function lookups
    /// 2. BTreeMap for line-based range queries
    /// 3. Base function index for generic/monomorphized function aggregation
    /// 4. Method name index for trait method variant matching
    pub fn from_coverage(coverage: &LcovData) -> Self {
        let start = Instant::now();

        let mut by_file: HashMap<PathBuf, HashMap<String, FunctionCoverage>> = HashMap::new();
        let mut by_line: HashMap<PathBuf, BTreeMap<usize, FunctionCoverage>> = HashMap::new();
        let mut base_function_index: HashMap<(PathBuf, String), Vec<String>> = HashMap::new();
        let mut method_name_index: HashMap<(PathBuf, String), Vec<String>> = HashMap::new();
        let mut total_records = 0;

        for (file_path, functions) in &coverage.functions {
            // Build inner HashMap for this file's functions
            let mut file_functions = HashMap::new();
            let mut line_map = BTreeMap::new();

            for func in functions {
                total_records += 1;

                // Insert into nested structure
                file_functions.insert(func.name.clone(), func.clone());

                // Index by start_line for range queries
                line_map.insert(func.start_line, func.clone());

                // Extract base name and update generic function index
                let base_name_cow = strip_trailing_generics(&func.name);
                let base_name = base_name_cow.as_ref();
                if base_name != func.name {
                    // This is a monomorphized function - add to index
                    base_function_index
                        .entry((file_path.clone(), base_name.to_string()))
                        .or_default()
                        .push(func.name.clone());
                }

                // Extract method name for trait method matching
                // Use the normalized method_name field if available
                let method_name = &func.normalized.method_name;
                if !method_name.is_empty() && method_name != &func.name {
                    // Add to method name index for O(1) variant lookups
                    method_name_index
                        .entry((file_path.clone(), method_name.clone()))
                        .or_default()
                        .push(func.name.clone());
                }
            }

            if !file_functions.is_empty() {
                by_file.insert(file_path.clone(), file_functions);
            }

            if !line_map.is_empty() {
                by_line.insert(file_path.clone(), line_map);
            }
        }

        // Pre-compute and sort file paths for faster, deterministic iteration
        let mut file_paths: Vec<PathBuf> = by_file.keys().cloned().collect();
        file_paths.sort();

        let index_build_time = start.elapsed();
        let total_files = by_file.len();

        // Estimate memory usage: ~200 bytes per record + ~100 bytes per file
        let estimated_memory_bytes = total_records * 200 + file_paths.len() * 100;

        CoverageIndex {
            by_file,
            by_line,
            file_paths,
            base_function_index,
            method_name_index,
            stats: CoverageIndexStats {
                total_files,
                total_records,
                index_build_time,
                estimated_memory_bytes,
            },
        }
    }

    /// Get function coverage by exact name (O(1) lookup)
    ///
    /// This is the primary lookup method and should be used when the exact
    /// function name is known. Also tries path normalization strategies.
    pub fn get_function_coverage(&self, file: &Path, function_name: &str) -> Option<f64> {
        log::debug!(
            "Looking up coverage for function '{}' in file '{}'",
            function_name,
            file.display()
        );

        // Normalize the query function name (remove angle brackets, etc.)
        let normalized_query = normalize_demangled_name(function_name);
        let query_name = &normalized_query.full_path;

        // O(1) exact match: file lookup + function lookup
        if let Some(file_functions) = self.by_file.get(file) {
            // Try exact match with normalized query first
            if let Some(f) = file_functions.get(query_name) {
                log::debug!(
                    "✓ Found via exact match (normalized): {}% coverage",
                    f.coverage_percentage
                );
                return Some(f.coverage_percentage / 100.0);
            }
            // Try original query (in case it was already normalized)
            if query_name != function_name {
                if let Some(f) = file_functions.get(function_name) {
                    log::debug!(
                        "✓ Found via exact match (original): {}% coverage",
                        f.coverage_percentage
                    );
                    return Some(f.coverage_percentage / 100.0);
                }
            }
        }

        log::debug!("Exact match failed, trying path strategies...");

        // O(files) fallback strategies - much faster than O(functions)
        // Try with normalized query first, then original
        if let Some(result) = self.find_by_path_strategies(file, query_name) {
            return Some(result.coverage_percentage / 100.0);
        }
        if query_name != function_name {
            self.find_by_path_strategies(file, function_name)
                .map(|f| f.coverage_percentage / 100.0)
        } else {
            None
        }
    }

    /// Try multiple path matching strategies to handle relative/absolute path mismatches
    ///
    /// This method iterates over FILES (not functions), providing O(files) complexity
    /// instead of O(functions). For 375 files with ~4 functions each, this is
    /// 375 iterations vs 1,500, a 4x speedup.
    ///
    /// Matching strategies in order:
    /// 1. Component-based suffix matching (robust cross-platform)
    /// 2. Legacy path suffix matching (backward compatibility)
    /// 3. Component-based exact match
    fn find_by_path_strategies(
        &self,
        query_path: &Path,
        function_name: &str,
    ) -> Option<&FunctionCoverage> {
        let query_components = normalize_path_components(query_path);

        log::debug!("Strategy 1: Component-based suffix matching");
        // Strategy 1: Component-based suffix matching - cross-platform robust
        for file_path in &self.file_paths {
            let file_components = normalize_path_components(file_path);

            // Check if query components are a suffix of file components
            if !query_components.is_empty() && query_components.len() <= file_components.len() {
                let file_suffix =
                    &file_components[file_components.len() - query_components.len()..];
                if query_components == file_suffix {
                    log::debug!("  Found path match: '{}'", file_path.display());
                    // O(1) lookup once we find the file
                    if let Some(file_functions) = self.by_file.get(file_path) {
                        // Use enhanced matching to find best match
                        if let Some((func, confidence)) =
                            self.find_best_matching_function(file_functions, function_name)
                        {
                            log::debug!(
                                "  ✓ Matched function via {:?} confidence: query '{}' matches stored '{}': {}%",
                                confidence,
                                function_name,
                                func.name,
                                func.coverage_percentage
                            );
                            return Some(func);
                        }
                        log::debug!("  ✗ No function match in this file");
                    }
                }
            }
        }

        log::debug!("Strategy 2: Component-based reverse suffix matching");
        // Strategy 2: Reverse suffix matching - file is suffix of query
        for file_path in &self.file_paths {
            let file_components = normalize_path_components(file_path);

            // Check if file components are a suffix of query components
            if !file_components.is_empty() && file_components.len() <= query_components.len() {
                let query_suffix =
                    &query_components[query_components.len() - file_components.len()..];
                if file_components == query_suffix {
                    log::debug!("  Found path match: '{}'", file_path.display());
                    if let Some(file_functions) = self.by_file.get(file_path) {
                        // Use enhanced matching to find best match
                        if let Some((func, confidence)) =
                            self.find_best_matching_function(file_functions, function_name)
                        {
                            log::debug!(
                                "  ✓ Matched function via {:?} confidence: query '{}' matches stored '{}': {}%",
                                confidence,
                                function_name,
                                func.name,
                                func.coverage_percentage
                            );
                            return Some(func);
                        }
                        log::debug!("  ✗ No function match in this file");
                    }
                }
            }
        }

        log::debug!("Strategy 3: Component-based exact equality");
        // Strategy 3: Exact component match
        for file_path in &self.file_paths {
            let file_components = normalize_path_components(file_path);
            if query_components == file_components {
                log::debug!("  Found path match: '{}'", file_path.display());
                if let Some(file_functions) = self.by_file.get(file_path) {
                    // Use enhanced matching to find best match
                    if let Some((func, confidence)) =
                        self.find_best_matching_function(file_functions, function_name)
                    {
                        log::debug!(
                            "  ✓ Matched function via {:?} confidence: query '{}' matches stored '{}': {}%",
                            confidence,
                            function_name,
                            func.name,
                            func.coverage_percentage
                        );
                        return Some(func);
                    }
                    log::debug!("  ✗ No function match in this file");
                }
            }
        }

        log::debug!("✗ All path strategies failed");
        None
    }

    /// Get function coverage using line number with tolerance (O(log n) lookup)
    ///
    /// Falls back to line-based lookup when exact name match fails.
    /// Uses BTreeMap range query for efficient lookups.
    /// Also tries aggregated coverage for generic/monomorphized functions.
    ///
    /// # Name Variant Matching (for trait methods)
    ///
    /// Tries multiple name variants before falling back to line-based lookup:
    /// 1. Full qualified name (e.g., `RecursiveMatchDetector::visit_expr`)
    /// 2. Method name only (e.g., `visit_expr`)
    ///
    /// This handles cases where LCOV stores demangled symbols with just the method
    /// name, while debtmap stores the full qualified name including the impl type.
    pub fn get_function_coverage_with_line(
        &self,
        file: &Path,
        function_name: &str,
        line: usize,
    ) -> Option<f64> {
        log::debug!(
            "Attempting coverage lookup for '{}' at {}:{}",
            function_name,
            file.display(),
            line
        );

        // Try aggregated coverage first (handles generics)
        if let Some(agg) = self.get_aggregated_coverage(file, function_name) {
            log::debug!(
                "✓ Coverage found via aggregated match: {:.1}%",
                agg.coverage_pct
            );
            return Some(agg.coverage_pct / 100.0);
        }

        // Try enhanced name variants (for trait methods, closures, generics)
        let variants = generate_function_name_variants(function_name);
        for variant in &variants {
            log::debug!("Trying name variant: '{}'", variant);
            // First try O(1) aggregated lookup
            if let Some(agg) = self.get_aggregated_coverage(file, variant) {
                log::debug!(
                    "✓ Coverage found via name variant '{}': {:.1}%",
                    variant,
                    agg.coverage_pct
                );
                return Some(agg.coverage_pct / 100.0);
            }
            // If that fails, try path strategies (handles file path mismatches)
            if let Some(func) = self.find_by_path_strategies(file, variant) {
                log::debug!(
                    "✓ Coverage found via name variant '{}' with path strategies: {:.1}%",
                    variant,
                    func.coverage_percentage
                );
                return Some(func.coverage_percentage / 100.0);
            }
        }

        // Try closure parent extraction (for async functions)
        if let Some(parent) = extract_closure_parent(function_name) {
            log::debug!("Trying closure parent: '{}'", parent);
            if let Some(agg) = self.get_aggregated_coverage(file, &parent) {
                log::debug!(
                    "✓ Coverage found via closure parent '{}': {:.1}%",
                    parent,
                    agg.coverage_pct
                );
                return Some(agg.coverage_pct / 100.0);
            }
            if let Some(func) = self.find_by_path_strategies(file, &parent) {
                log::debug!(
                    "✓ Coverage found via closure parent '{}' with path strategies: {:.1}%",
                    parent,
                    func.coverage_percentage
                );
                return Some(func.coverage_percentage / 100.0);
            }
        }

        // Try line-based lookup (O(log n)) - faster than path matching strategies
        log::debug!("Trying line-based lookup with tolerance ±2");
        match self.find_function_by_line(file, line, 2) {
            Some(f) => {
                log::debug!(
                    "✓ Coverage found via line-based fallback: matched '{}' at line {}, coverage {:.1}%",
                    f.name,
                    f.start_line,
                    f.coverage_percentage
                );
                return Some(f.coverage_percentage / 100.0);
            }
            None => {
                // Log diagnostic info about why line-based lookup failed
                if !self.by_line.contains_key(file) {
                    log::trace!(
                        "File '{}' not found in line-based index (has {} files indexed)",
                        file.display(),
                        self.by_line.len()
                    );
                } else {
                    let line_map = &self.by_line[file];
                    log::debug!(
                        "Line-based lookup failed: file has {} indexed functions, searched for line {} with ±2 tolerance",
                        line_map.len(),
                        line
                    );

                    // Show nearby lines to help diagnose tolerance issues
                    let min_line = line.saturating_sub(5);
                    let max_line = line.saturating_add(5);
                    let nearby_lines: Vec<usize> = line_map
                        .range(min_line..=max_line)
                        .map(|(l, _)| *l)
                        .collect();
                    if !nearby_lines.is_empty() {
                        log::debug!("Nearby indexed lines (±5): {:?}", nearby_lines);
                    }
                }
            }
        }

        // Only fall back to path matching strategies if line lookup fails
        log::debug!("Trying path matching strategies");
        match self.find_by_path_strategies(file, function_name) {
            Some(f) => {
                log::debug!(
                    "✓ Coverage found via path matching: {:.1}%",
                    f.coverage_percentage
                );
                Some(f.coverage_percentage / 100.0)
            }
            None => {
                log::trace!(
                    "✗ No coverage found for '{}' at {}:{} after all strategies",
                    function_name,
                    file.display(),
                    line
                );
                None
            }
        }
    }

    /// Get uncovered lines for a function
    pub fn get_function_uncovered_lines(
        &self,
        file: &Path,
        function_name: &str,
        line: usize,
    ) -> Option<Vec<usize>> {
        // O(1) file lookup + O(1) function lookup
        if let Some(file_functions) = self.by_file.get(file) {
            if let Some(func) = file_functions.get(function_name) {
                return Some(func.uncovered_lines.clone());
            }
        }

        // Try path matching strategies
        if let Some(func) = self.find_by_path_strategies(file, function_name) {
            return Some(func.uncovered_lines.clone());
        }

        // Fallback to line-based lookup
        self.find_function_by_line(file, line, 2)
            .map(|f| f.uncovered_lines.clone())
    }

    /// Find function by line number with tolerance (private helper)
    ///
    /// This is a fallback mechanism for when name-based matching fails.
    /// It's particularly useful for:
    /// - Trait implementation methods (name format varies)
    /// - Generic functions (multiple monomorphizations)
    /// - Functions where LCOV and AST disagree on naming
    ///
    /// # Arguments
    /// * `file` - Path to source file
    /// * `target_line` - Line number to search for
    /// * `tolerance` - Number of lines above/below to check (typically 2)
    ///
    /// # Returns
    /// The closest function within tolerance, or None if no match found.
    ///
    /// # Algorithm
    /// Uses BTreeMap range query for O(log n) performance. When multiple
    /// functions are within tolerance, returns the closest by absolute distance.
    fn find_function_by_line(
        &self,
        file: &Path,
        target_line: usize,
        tolerance: usize,
    ) -> Option<&FunctionCoverage> {
        let line_map = self.by_line.get(file)?;

        // Define search range with tolerance
        let min_line = target_line.saturating_sub(tolerance);
        let max_line = target_line.saturating_add(tolerance);

        log::trace!(
            "Searching line-based index: target={}, range={}..={}, index_size={}",
            target_line,
            min_line,
            max_line,
            line_map.len()
        );

        // Use BTreeMap range query to find functions in range (inclusive on both ends)
        let result = line_map
            .range(min_line..=max_line)
            .min_by_key(|(line, _)| line.abs_diff(target_line))
            .map(|(_, func)| func);

        if let Some(func) = result {
            log::trace!(
                "Found function '{}' at line {} (distance: {})",
                func.name,
                func.start_line,
                func.start_line.abs_diff(target_line)
            );
        } else {
            log::trace!("No function found within tolerance range");
        }

        result
    }

    /// Get index statistics
    pub fn stats(&self) -> &CoverageIndexStats {
        &self.stats
    }

    /// Find all monomorphizations of a function and aggregate coverage.
    ///
    /// Uses pre-built index for O(1) lookup of monomorphized versions.
    ///
    /// # Arguments
    ///
    /// * `file` - The file path containing the function
    /// * `function_name` - The base function name (without generic parameters)
    ///
    /// # Returns
    ///
    /// `Some(AggregateCoverage)` if any monomorphizations are found, `None` otherwise
    fn get_aggregated_coverage(
        &self,
        file: &Path,
        function_name: &str,
    ) -> Option<AggregateCoverage> {
        // Try exact match first (O(1))
        if let Some(file_functions) = self.by_file.get(file) {
            if let Some(exact) = file_functions.get(function_name) {
                return Some(AggregateCoverage::single(exact));
            }
        }

        // Try method name index for trait methods (O(1))
        // This handles cases where LCOV stores "visit_expr" but we're looking up "Type::visit_expr"
        if let Some(matching_functions) = self
            .method_name_index
            .get(&(file.to_path_buf(), function_name.to_string()))
        {
            let coverages: Vec<&FunctionCoverage> = matching_functions
                .iter()
                .filter_map(|name| self.by_file.get(file).and_then(|funcs| funcs.get(name)))
                .collect();

            if !coverages.is_empty() {
                log::debug!(
                    "✓ Found {} matching functions via method_name_index for '{}'",
                    coverages.len(),
                    function_name
                );
                return Some(merge_coverage(coverages));
            }
        }

        // Try monomorphized versions using index (O(1))
        if let Some(versions) = self
            .base_function_index
            .get(&(file.to_path_buf(), function_name.to_string()))
        {
            let coverages: Vec<&FunctionCoverage> = versions
                .iter()
                .filter_map(|name| self.by_file.get(file).and_then(|funcs| funcs.get(name)))
                .collect();

            if !coverages.is_empty() {
                return Some(merge_coverage(coverages));
            }
        }

        None
    }

    /// Find best matching function from a set of candidates using enhanced matching
    ///
    /// Uses the new function_name_matching module to find the best match with confidence scoring.
    /// Prefers higher confidence matches (exact > variant > fuzzy).
    ///
    /// # Arguments
    /// * `file_functions` - HashMap of function names to coverage data for a file
    /// * `query_name` - The function name we're looking for
    ///
    /// # Returns
    /// The best matching function and its confidence level, or None if no match found
    fn find_best_matching_function<'a>(
        &self,
        file_functions: &'a HashMap<String, FunctionCoverage>,
        query_name: &str,
    ) -> Option<(&'a FunctionCoverage, MatchConfidence)> {
        let mut best_match = None;
        let mut best_confidence = MatchConfidence::None;

        // Sort functions by name to ensure deterministic matching when multiple functions
        // have the same confidence level (Spec 214 fix)
        let mut sorted_functions: Vec<_> = file_functions.iter().collect();
        sorted_functions.sort_by(|a, b| a.0.cmp(b.0));

        for (lcov_name, func) in sorted_functions {
            let (matches, confidence) = function_names_match(query_name, lcov_name);

            if matches && confidence > best_confidence {
                best_match = Some(func);
                best_confidence = confidence;

                // Early exit on exact match
                if confidence == MatchConfidence::High {
                    break;
                }
            }
        }

        best_match.map(|func| (func, best_confidence))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::risk::lcov::NormalizedFunctionName;

    fn create_test_function_coverage(
        name: &str,
        start_line: usize,
        execution_count: u64,
        coverage_percentage: f64,
        uncovered_lines: Vec<usize>,
    ) -> FunctionCoverage {
        FunctionCoverage {
            name: name.to_string(),
            start_line,
            execution_count,
            coverage_percentage,
            uncovered_lines,
            normalized: NormalizedFunctionName {
                full_path: name.to_string(),
                method_name: name.to_string(),
                original: name.to_string(),
            },
        }
    }

    fn create_test_coverage() -> LcovData {
        let mut coverage = LcovData::default();

        let test_functions = vec![
            create_test_function_coverage("func_a", 10, 5, 100.0, vec![]),
            create_test_function_coverage("func_b", 20, 3, 75.0, vec![22, 24]),
            create_test_function_coverage("func_c", 30, 0, 0.0, vec![30, 31, 32, 33]),
        ];

        coverage
            .functions
            .insert(PathBuf::from("test.rs"), test_functions);
        coverage
    }

    #[test]
    fn test_index_build() {
        let coverage = create_test_coverage();
        let index = CoverageIndex::from_coverage(&coverage);

        assert_eq!(index.stats.total_files, 1);
        assert_eq!(index.stats.total_records, 3);
        assert!(index.stats.index_build_time < Duration::from_millis(10));
    }

    #[test]
    fn test_exact_function_lookup() {
        let coverage = create_test_coverage();
        let index = CoverageIndex::from_coverage(&coverage);

        // Test exact match
        assert_eq!(
            index.get_function_coverage(Path::new("test.rs"), "func_a"),
            Some(1.0) // 100% as fraction
        );
        assert_eq!(
            index.get_function_coverage(Path::new("test.rs"), "func_b"),
            Some(0.75) // 75% as fraction
        );
        assert_eq!(
            index.get_function_coverage(Path::new("test.rs"), "func_c"),
            Some(0.0)
        );
    }

    #[test]
    fn test_function_not_found() {
        let coverage = create_test_coverage();
        let index = CoverageIndex::from_coverage(&coverage);

        assert_eq!(
            index.get_function_coverage(Path::new("test.rs"), "nonexistent"),
            None
        );
        assert_eq!(
            index.get_function_coverage(Path::new("other.rs"), "func_a"),
            None
        );
    }

    #[test]
    fn test_line_based_lookup() {
        let coverage = create_test_coverage();
        let index = CoverageIndex::from_coverage(&coverage);

        // Test exact line match
        assert_eq!(
            index.get_function_coverage_with_line(Path::new("test.rs"), "unknown", 10),
            Some(1.0)
        );

        // Test within tolerance
        assert_eq!(
            index.get_function_coverage_with_line(Path::new("test.rs"), "unknown", 11),
            Some(1.0) // Should find func_a at line 10
        );

        // Test line 21 should match func_b at line 20
        assert_eq!(
            index.get_function_coverage_with_line(Path::new("test.rs"), "unknown", 21),
            Some(0.75)
        );
    }

    #[test]
    fn test_uncovered_lines() {
        let coverage = create_test_coverage();
        let index = CoverageIndex::from_coverage(&coverage);

        assert_eq!(
            index.get_function_uncovered_lines(Path::new("test.rs"), "func_a", 10),
            Some(vec![])
        );
        assert_eq!(
            index.get_function_uncovered_lines(Path::new("test.rs"), "func_b", 20),
            Some(vec![22, 24])
        );
        assert_eq!(
            index.get_function_uncovered_lines(Path::new("test.rs"), "func_c", 30),
            Some(vec![30, 31, 32, 33])
        );
    }

    #[test]
    fn test_empty_coverage() {
        let coverage = LcovData::default();
        let index = CoverageIndex::from_coverage(&coverage);

        assert_eq!(index.stats.total_files, 0);
        assert_eq!(index.stats.total_records, 0);
        assert_eq!(
            index.get_function_coverage(Path::new("test.rs"), "func"),
            None
        );
    }

    #[test]
    fn test_multiple_files() {
        let mut coverage = LcovData::default();

        coverage.functions.insert(
            PathBuf::from("file1.rs"),
            vec![create_test_function_coverage("func1", 5, 10, 100.0, vec![])],
        );

        coverage.functions.insert(
            PathBuf::from("file2.rs"),
            vec![create_test_function_coverage(
                "func2",
                15,
                0,
                0.0,
                vec![15, 16],
            )],
        );

        let index = CoverageIndex::from_coverage(&coverage);

        assert_eq!(index.stats.total_files, 2);
        assert_eq!(index.stats.total_records, 2);

        assert_eq!(
            index.get_function_coverage(Path::new("file1.rs"), "func1"),
            Some(1.0)
        );
        assert_eq!(
            index.get_function_coverage(Path::new("file2.rs"), "func2"),
            Some(0.0)
        );
    }

    #[test]
    fn test_merge_coverage_intersection() {
        let cov1 = create_test_function_coverage("func", 10, 5, 70.0, vec![10, 20, 30]);
        let cov2 = create_test_function_coverage("func", 10, 3, 80.0, vec![20, 40]);

        let agg = merge_coverage(vec![&cov1, &cov2]);
        assert_eq!(agg.version_count, 2);
        assert_eq!(agg.coverage_pct, 75.0); // Average: (70 + 80) / 2
                                            // Intersection: only line 20 is uncovered in BOTH versions
        assert_eq!(agg.uncovered_lines.len(), 1);
        assert!(agg.uncovered_lines.contains(&20));
        assert!(!agg.uncovered_lines.contains(&10)); // Covered in cov2
        assert!(!agg.uncovered_lines.contains(&40)); // Covered in cov1
    }

    #[test]
    fn test_merge_coverage_all_covered_in_some() {
        // If ANY version covers a line, it's considered covered (intersection)
        let cov1 = create_test_function_coverage("func", 10, 5, 50.0, vec![10, 20]);
        let cov2 = create_test_function_coverage("func", 10, 3, 50.0, vec![30, 40]);

        let agg = merge_coverage(vec![&cov1, &cov2]);
        // No lines uncovered in BOTH versions
        assert_eq!(agg.uncovered_lines.len(), 0);
    }

    #[test]
    fn test_merge_coverage_single() {
        let cov = create_test_function_coverage("func", 10, 5, 75.0, vec![10, 20]);

        let agg = merge_coverage(vec![&cov]);
        assert_eq!(agg.version_count, 1);
        assert_eq!(agg.coverage_pct, 75.0);
        assert_eq!(agg.uncovered_lines, vec![10, 20]);
    }

    #[test]
    fn test_merge_coverage_empty() {
        let agg = merge_coverage(vec![]);
        assert_eq!(agg.version_count, 0);
        assert_eq!(agg.coverage_pct, 0.0);
        assert_eq!(agg.uncovered_lines.len(), 0);
    }

    #[test]
    fn test_monomorphized_function_indexing() {
        use crate::risk::lcov::NormalizedFunctionName;

        let mut coverage = LcovData::default();

        // Create monomorphized versions of the same function
        coverage.functions.insert(
            PathBuf::from("test.rs"),
            vec![
                FunctionCoverage {
                    name: "Type::method::<WorkflowExecutor>".to_string(),
                    start_line: 10,
                    execution_count: 5,
                    coverage_percentage: 70.0,
                    uncovered_lines: vec![10, 20, 30],
                    normalized: NormalizedFunctionName {
                        full_path: "Type::method".to_string(),
                        method_name: "method".to_string(),
                        original: "Type::method::<WorkflowExecutor>".to_string(),
                    },
                },
                FunctionCoverage {
                    name: "Type::method::<MockExecutor>".to_string(),
                    start_line: 10,
                    execution_count: 3,
                    coverage_percentage: 80.0,
                    uncovered_lines: vec![20, 40],
                    normalized: NormalizedFunctionName {
                        full_path: "Type::method".to_string(),
                        method_name: "method".to_string(),
                        original: "Type::method::<MockExecutor>".to_string(),
                    },
                },
            ],
        );

        let index = CoverageIndex::from_coverage(&coverage);

        // Query for the base function name (without generics)
        let agg = index.get_aggregated_coverage(Path::new("test.rs"), "Type::method");
        assert!(agg.is_some());

        let agg = agg.unwrap();
        assert_eq!(agg.version_count, 2);
        assert_eq!(agg.coverage_pct, 75.0); // (70 + 80) / 2
                                            // Only line 20 is uncovered in both versions
        assert_eq!(agg.uncovered_lines, vec![20]);
    }

    #[test]
    fn test_generate_name_variants_trait_method() {
        let variants: Vec<String> =
            generate_name_variants("RecursiveMatchDetector::visit_expr").collect();
        assert_eq!(variants, vec!["visit_expr"]);
    }

    #[test]
    fn test_generate_name_variants_nested_path() {
        let variants: Vec<String> = generate_name_variants("crate::module::Type::method").collect();
        assert_eq!(variants, vec!["method"]);
    }

    #[test]
    fn test_generate_name_variants_simple_function() {
        let variants: Vec<String> = generate_name_variants("simple_function").collect();
        assert_eq!(variants.len(), 0); // No variants for functions without ::
    }

    #[test]
    fn test_generate_name_variants_single_segment() {
        let variants: Vec<String> = generate_name_variants("main").collect();
        assert_eq!(variants.len(), 0); // No variants for single segment names
    }

    #[test]
    fn test_trait_method_coverage_match_by_method_name() {
        use crate::risk::lcov::NormalizedFunctionName;

        let mut coverage = LcovData::default();

        // Simulate LCOV storing just the method name (common for trait implementations)
        coverage.functions.insert(
            PathBuf::from("src/complexity/recursive_detector.rs"),
            vec![FunctionCoverage {
                name: "visit_expr".to_string(),
                start_line: 177,
                execution_count: 3507,
                coverage_percentage: 90.2,
                uncovered_lines: vec![200, 205],
                normalized: NormalizedFunctionName {
                    full_path: "visit_expr".to_string(),
                    method_name: "visit_expr".to_string(),
                    original: "visit_expr".to_string(),
                },
            }],
        );

        let index = CoverageIndex::from_coverage(&coverage);

        // Query with full qualified name (what debtmap stores)
        let coverage = index.get_function_coverage_with_line(
            Path::new("src/complexity/recursive_detector.rs"),
            "RecursiveMatchDetector::visit_expr",
            177,
        );

        // Should find coverage via method name variant matching
        assert!(coverage.is_some());
        assert_eq!(coverage.unwrap(), 0.902); // 90.2% as fraction
    }

    #[test]
    fn test_trait_method_coverage_no_regression_exact_match() {
        use crate::risk::lcov::NormalizedFunctionName;

        let mut coverage = LcovData::default();

        // LCOV stores full qualified name (ideal case)
        coverage.functions.insert(
            PathBuf::from("src/test.rs"),
            vec![FunctionCoverage {
                name: "MyType::my_method".to_string(),
                start_line: 10,
                execution_count: 100,
                coverage_percentage: 95.0,
                uncovered_lines: vec![15],
                normalized: NormalizedFunctionName {
                    full_path: "MyType::my_method".to_string(),
                    method_name: "my_method".to_string(),
                    original: "MyType::my_method".to_string(),
                },
            }],
        );

        let index = CoverageIndex::from_coverage(&coverage);

        // Query with full qualified name - should still work via exact match
        let coverage = index.get_function_coverage_with_line(
            Path::new("src/test.rs"),
            "MyType::my_method",
            10,
        );

        assert!(coverage.is_some());
        assert_eq!(coverage.unwrap(), 0.95);
    }

    #[test]
    fn test_trait_method_coverage_method_name_conflict() {
        use crate::risk::lcov::NormalizedFunctionName;

        let mut coverage = LcovData::default();

        // Two different types with same method name, both stored in LCOV
        coverage.functions.insert(
            PathBuf::from("src/test.rs"),
            vec![
                FunctionCoverage {
                    name: "TypeA::process".to_string(),
                    start_line: 10,
                    execution_count: 50,
                    coverage_percentage: 80.0,
                    uncovered_lines: vec![12],
                    normalized: NormalizedFunctionName {
                        full_path: "TypeA::process".to_string(),
                        method_name: "process".to_string(),
                        original: "TypeA::process".to_string(),
                    },
                },
                FunctionCoverage {
                    name: "TypeB::process".to_string(),
                    start_line: 30,
                    execution_count: 75,
                    coverage_percentage: 90.0,
                    uncovered_lines: vec![35],
                    normalized: NormalizedFunctionName {
                        full_path: "TypeB::process".to_string(),
                        method_name: "process".to_string(),
                        original: "TypeB::process".to_string(),
                    },
                },
            ],
        );

        let index = CoverageIndex::from_coverage(&coverage);

        // Query TypeA::process - should get correct coverage
        let coverage_a =
            index.get_function_coverage_with_line(Path::new("src/test.rs"), "TypeA::process", 10);
        assert_eq!(coverage_a.unwrap(), 0.80);

        // Query TypeB::process - should get correct coverage
        let coverage_b =
            index.get_function_coverage_with_line(Path::new("src/test.rs"), "TypeB::process", 30);
        assert_eq!(coverage_b.unwrap(), 0.90);
    }

    // Tests for Spec 182: Line-Based Coverage Fallback Reliability

    #[test]
    fn test_line_based_fallback_exact_match() {
        let mut coverage = LcovData::default();
        coverage.functions.insert(
            PathBuf::from("test.rs"),
            vec![create_test_function_coverage("foo", 100, 10, 85.0, vec![])],
        );
        let index = CoverageIndex::from_coverage(&coverage);

        // Query with wrong name but exact line - should find via line-based fallback
        let result = index.get_function_coverage_with_line(
            Path::new("test.rs"),
            "WRONG_NAME", // Name won't match
            100,          // Exact line
        );

        assert!(
            result.is_some(),
            "Line-based fallback should find function at exact line"
        );
        assert_eq!(result.unwrap(), 0.85);
    }

    #[test]
    fn test_line_based_fallback_within_tolerance() {
        let mut coverage = LcovData::default();
        coverage.functions.insert(
            PathBuf::from("test.rs"),
            vec![create_test_function_coverage("foo", 100, 10, 85.0, vec![])],
        );
        let index = CoverageIndex::from_coverage(&coverage);

        // Try lines 98-102 (all within ±2 tolerance)
        for line in 98..=102 {
            let result =
                index.get_function_coverage_with_line(Path::new("test.rs"), "WRONG_NAME", line);

            assert!(
                result.is_some(),
                "Line {} should match function at 100 with ±2 tolerance",
                line
            );
            assert_eq!(result.unwrap(), 0.85);
        }
    }

    #[test]
    fn test_line_based_fallback_outside_tolerance() {
        let mut coverage = LcovData::default();
        coverage.functions.insert(
            PathBuf::from("test.rs"),
            vec![create_test_function_coverage("foo", 100, 10, 85.0, vec![])],
        );
        let index = CoverageIndex::from_coverage(&coverage);

        // Line 97 is just outside ±2 tolerance (100 - 2 = 98)
        let result = index.get_function_coverage_with_line(Path::new("test.rs"), "WRONG_NAME", 97);

        assert!(
            result.is_none(),
            "Line 97 should be outside ±2 tolerance of line 100"
        );

        // Line 103 is just outside ±2 tolerance (100 + 2 = 102)
        let result = index.get_function_coverage_with_line(Path::new("test.rs"), "WRONG_NAME", 103);

        assert!(
            result.is_none(),
            "Line 103 should be outside ±2 tolerance of line 100"
        );
    }

    #[test]
    fn test_line_based_fallback_chooses_closest() {
        let mut coverage = LcovData::default();
        coverage.functions.insert(
            PathBuf::from("test.rs"),
            vec![
                create_test_function_coverage("func_at_100", 100, 10, 80.0, vec![]),
                create_test_function_coverage("func_at_105", 105, 10, 90.0, vec![]),
            ],
        );
        let index = CoverageIndex::from_coverage(&coverage);

        // Line 102 is closer to 100 (distance 2) than 105 (distance 3)
        let result = index.get_function_coverage_with_line(Path::new("test.rs"), "WRONG_NAME", 102);

        assert!(result.is_some());
        assert_eq!(
            result.unwrap(),
            0.80,
            "Should match function at line 100 (closer)"
        );

        // Line 103 is closer to 105 (distance 2) than 100 (distance 3)
        let result = index.get_function_coverage_with_line(Path::new("test.rs"), "WRONG_NAME", 104);

        assert!(result.is_some());
        assert_eq!(
            result.unwrap(),
            0.90,
            "Should match function at line 105 (closer)"
        );
    }

    #[test]
    fn test_line_based_fallback_boundary_conditions() {
        let mut coverage = LcovData::default();
        coverage.functions.insert(
            PathBuf::from("test.rs"),
            vec![
                create_test_function_coverage("func_at_0", 0, 10, 70.0, vec![]),
                create_test_function_coverage(
                    "func_at_usize_max",
                    usize::MAX - 5,
                    10,
                    75.0,
                    vec![],
                ),
            ],
        );
        let index = CoverageIndex::from_coverage(&coverage);

        // Test line 0 with tolerance (should handle underflow correctly)
        let result = index.get_function_coverage_with_line(Path::new("test.rs"), "WRONG_NAME", 0);
        assert!(result.is_some());
        assert_eq!(result.unwrap(), 0.70);

        // Test near usize::MAX (should handle overflow correctly)
        let result = index.get_function_coverage_with_line(
            Path::new("test.rs"),
            "WRONG_NAME",
            usize::MAX - 4,
        );
        assert!(result.is_some());
        assert_eq!(result.unwrap(), 0.75);
    }

    #[test]
    fn test_line_index_populated_for_all_functions() {
        let mut coverage = LcovData::default();
        let test_functions = vec![
            create_test_function_coverage("func_a", 10, 5, 100.0, vec![]),
            create_test_function_coverage("func_b", 20, 3, 75.0, vec![22, 24]),
            create_test_function_coverage("func_c", 30, 0, 0.0, vec![30, 31, 32, 33]),
        ];
        coverage
            .functions
            .insert(PathBuf::from("test.rs"), test_functions);

        let index = CoverageIndex::from_coverage(&coverage);

        // Verify all functions are in the line index
        let file = Path::new("test.rs");
        assert!(
            index.by_line.contains_key(file),
            "File should be in line index"
        );

        let line_map = &index.by_line[file];
        assert_eq!(line_map.len(), 3, "All 3 functions should be in line index");
        assert!(
            line_map.contains_key(&10),
            "Function at line 10 should be indexed"
        );
        assert!(
            line_map.contains_key(&20),
            "Function at line 20 should be indexed"
        );
        assert!(
            line_map.contains_key(&30),
            "Function at line 30 should be indexed"
        );
    }

    #[test]
    fn test_line_based_fallback_with_trait_method() {
        use crate::risk::lcov::NormalizedFunctionName;

        let mut coverage = LcovData::default();

        // Simulate LCOV storing just the method name (common for trait implementations)
        coverage.functions.insert(
            PathBuf::from("src/test.rs"),
            vec![FunctionCoverage {
                name: "visit_expr".to_string(),
                start_line: 177,
                execution_count: 3507,
                coverage_percentage: 90.2,
                uncovered_lines: vec![200, 205],
                normalized: NormalizedFunctionName {
                    full_path: "visit_expr".to_string(),
                    method_name: "visit_expr".to_string(),
                    original: "visit_expr".to_string(),
                },
            }],
        );

        let index = CoverageIndex::from_coverage(&coverage);

        // Query with full qualified name that doesn't match, but correct line
        // This simulates the RecursiveMatchDetector::visit_expr case
        let coverage = index.get_function_coverage_with_line(
            Path::new("src/test.rs"),
            "SomeType::visit_expr", // Won't match stored "visit_expr" by aggregated lookup
            177,
        );

        // Should find coverage via line-based fallback
        assert!(
            coverage.is_some(),
            "Line-based fallback should find coverage when name doesn't match exactly"
        );
        assert_eq!(coverage.unwrap(), 0.902);
    }

    #[test]
    fn test_line_based_fallback_empty_file() {
        let coverage = LcovData::default();
        let index = CoverageIndex::from_coverage(&coverage);

        // Query for non-existent file
        let result =
            index.get_function_coverage_with_line(Path::new("nonexistent.rs"), "func", 100);

        assert!(result.is_none(), "Should return None for non-existent file");
    }

    #[test]
    fn test_tolerance_calculation_inclusive_range() {
        let mut coverage = LcovData::default();
        coverage.functions.insert(
            PathBuf::from("test.rs"),
            vec![
                create_test_function_coverage("func_98", 98, 10, 70.0, vec![]),
                create_test_function_coverage("func_100", 100, 10, 80.0, vec![]),
                create_test_function_coverage("func_102", 102, 10, 90.0, vec![]),
            ],
        );
        let index = CoverageIndex::from_coverage(&coverage);

        // Verify range is inclusive on both ends for target line 100 with tolerance 2
        // Should match lines 98, 100, 102 (range 98..=102)

        // Test exact boundary: line 98 (min_line = 100 - 2 = 98)
        let result = index.find_function_by_line(Path::new("test.rs"), 100, 2);
        assert!(result.is_some());
        assert_eq!(
            result.unwrap().name,
            "func_100",
            "Should find closest function at 100"
        );

        // Test that all functions in range are considered
        let result = index.find_function_by_line(Path::new("test.rs"), 99, 2);
        assert!(result.is_some());
        // 99 is equidistant from 98 and 100, should pick first (min_by_key behavior)
    }
}