debtmap 0.16.6

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
/// Call graph validation and health checks
///
/// This module provides validation tools to detect structural issues,
/// orphaned nodes, and suspicious patterns in the call graph.
use crate::priority::call_graph::{CallGraph, FunctionId};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

/// Configuration for call graph validation
#[derive(Debug, Clone, Default)]
pub struct CallGraphValidationConfig {
    /// Functions expected to be orphaned (won't be flagged as issues)
    pub orphan_whitelist: HashSet<String>,
    /// Additional entry points beyond standard detection
    pub additional_entry_points: HashSet<String>,
}

impl CallGraphValidationConfig {
    /// Create a new empty configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a function to the orphan whitelist
    pub fn add_orphan_whitelist(&mut self, function_name: String) -> &mut Self {
        self.orphan_whitelist.insert(function_name);
        self
    }

    /// Add an additional entry point
    pub fn add_entry_point(&mut self, function_name: String) -> &mut Self {
        self.additional_entry_points.insert(function_name);
        self
    }

    /// Check if a function is whitelisted as an expected orphan
    pub fn is_whitelisted_orphan(&self, function: &FunctionId) -> bool {
        self.orphan_whitelist.contains(&function.name)
    }

    /// Check if a function is configured as an additional entry point
    pub fn is_additional_entry_point(&self, function: &FunctionId) -> bool {
        self.additional_entry_points.contains(&function.name)
    }
}

/// Structural issue in the call graph
#[derive(Debug, Clone)]
pub enum StructuralIssue {
    /// Edge references non-existent node
    DanglingEdge {
        caller: FunctionId,
        callee: FunctionId,
    },
    /// Function is unreachable (no callers, not an entry point)
    UnreachableFunction {
        function: FunctionId,
        reason: UnreachableReason,
    },
    /// Function is completely isolated (no callers, no callees, not entry point)
    IsolatedFunction { function: FunctionId },
    /// Duplicate nodes
    DuplicateNode { function: FunctionId, count: usize },
}

/// Reason why a function is unreachable
#[derive(Debug, Clone)]
pub enum UnreachableReason {
    /// Not called by any function
    NoCallers,
}

/// Warning about suspicious patterns
#[derive(Debug, Clone)]
pub enum ValidationWarning {
    /// Function has unexpectedly many callers
    TooManyCallers { function: FunctionId, count: usize },
    /// Function has unexpectedly many callees
    TooManyCallees { function: FunctionId, count: usize },
    /// All functions in file have 0 callers (suspicious)
    FileWithNoCalls {
        file: PathBuf,
        function_count: usize,
    },
    /// Public function has 0 callers
    UnusedPublicFunction { function: FunctionId },
}

/// Informational observations (not errors or warnings)
#[derive(Debug, Clone)]
pub enum ValidationInfo {
    /// Leaf function (has callers, no callees) - this is normal
    LeafFunction {
        function: FunctionId,
        caller_count: usize,
    },
    /// Recursive function (calls itself)
    SelfReferentialFunction { function: FunctionId },
}

/// Validation report
#[derive(Debug, Clone)]
pub struct ValidationReport {
    /// Structural issues (dangling edges, etc.)
    pub structural_issues: Vec<StructuralIssue>,
    /// Heuristic warnings (suspicious patterns)
    pub warnings: Vec<ValidationWarning>,
    /// Informational observations (not issues)
    pub info: Vec<ValidationInfo>,
    /// Overall health score (0-100)
    pub health_score: u32,
    /// Detailed statistics
    pub statistics: ValidationStatistics,
}

/// Detailed validation statistics
#[derive(Debug, Clone, Default)]
pub struct ValidationStatistics {
    pub total_functions: usize,
    pub entry_points: usize,
    pub leaf_functions: usize,
    pub unreachable_functions: usize,
    pub isolated_functions: usize,
    pub recursive_functions: usize,
}

impl ValidationReport {
    /// Create a new empty validation report
    fn new() -> Self {
        Self {
            structural_issues: Vec::new(),
            warnings: Vec::new(),
            info: Vec::new(),
            health_score: 100,
            statistics: ValidationStatistics::default(),
        }
    }

    /// Calculate health score based on issues and warnings
    fn calculate_health_score(&mut self) {
        let mut score: u32 = 100;

        // Count issue types separately for refined weighting
        let mut unreachable_count = 0;
        let mut isolated_count = 0;
        let mut dangling_edge_count = 0;
        let mut duplicate_count = 0;

        for issue in &self.structural_issues {
            match issue {
                StructuralIssue::UnreachableFunction { .. } => unreachable_count += 1,
                StructuralIssue::IsolatedFunction { .. } => isolated_count += 1,
                StructuralIssue::DanglingEdge { .. } => dangling_edge_count += 1,
                StructuralIssue::DuplicateNode { .. } => duplicate_count += 1,
            }
        }

        // Dangling edges are critical (graph corruption) - 10 points each
        score = score.saturating_sub(dangling_edge_count * 10);

        // Duplicates are serious (data integrity) - 5 points each
        score = score.saturating_sub(duplicate_count * 5);

        // Unreachable functions are moderate (dead code) - 1 point each
        score = score.saturating_sub(unreachable_count);

        // Isolated functions are low concern (might be work-in-progress) - 0.5 points each
        score = score.saturating_sub((isolated_count as f32 * 0.5) as u32);

        // Warnings are minor - 2 points each
        score = score.saturating_sub(self.warnings.len() as u32 * 2);

        // Info items don't affect health score (they're informational)

        self.health_score = score;
    }

    /// Check if the report has any issues
    pub fn has_issues(&self) -> bool {
        !self.structural_issues.is_empty() || !self.warnings.is_empty()
    }
}

/// Call graph validator
pub struct CallGraphValidator;

impl CallGraphValidator {
    /// Validate call graph structure
    pub fn validate(call_graph: &CallGraph) -> ValidationReport {
        Self::validate_with_config(call_graph, &CallGraphValidationConfig::default())
    }

    /// Validate call graph structure with custom configuration
    pub fn validate_with_config(
        call_graph: &CallGraph,
        config: &CallGraphValidationConfig,
    ) -> ValidationReport {
        let mut report = ValidationReport::new();

        // Check for structural issues
        Self::check_dangling_edges(call_graph, &mut report);
        Self::check_orphaned_nodes(call_graph, &mut report, config);
        Self::check_duplicate_nodes(call_graph, &mut report);

        // Check for suspicious patterns
        Self::check_heuristics(call_graph, &mut report);

        // Calculate overall health score
        report.calculate_health_score();

        report
    }

    /// Check for dangling edges (references to non-existent functions)
    fn check_dangling_edges(call_graph: &CallGraph, report: &mut ValidationReport) {
        let all_function_ids: HashSet<_> = call_graph.get_all_functions().cloned().collect();

        for function in call_graph.get_all_functions() {
            // Check callees
            let callees = call_graph.get_callees(function);
            for callee in callees {
                if !all_function_ids.contains(&callee) {
                    report
                        .structural_issues
                        .push(StructuralIssue::DanglingEdge {
                            caller: function.clone(),
                            callee: callee.clone(),
                        });
                }
            }
        }
    }

    /// Check if a function is an entry point (expected to have no callers)
    fn is_entry_point(function: &FunctionId, config: &CallGraphValidationConfig) -> bool {
        // Check configured additional entry points
        if config.is_additional_entry_point(function) {
            return true;
        }

        // Main function
        if function.name == "main" {
            return true;
        }

        // Test functions
        if function.name.starts_with("test_")
            || function.name.contains("::test_")
            || function.name.starts_with("#[test]")
        {
            return true;
        }

        // Benchmark functions
        if function.name.starts_with("bench_")
            || function.name.contains("::bench_")
            || function.name.starts_with("#[bench]")
        {
            return true;
        }

        // Check file path for examples and benchmarks
        if let Some(path_str) = function.file.to_str() {
            if path_str.contains("/examples/")
                || path_str.contains("/benches/")
                || path_str.starts_with("examples/")
                || path_str.starts_with("benches/")
            {
                return true;
            }
        }

        // Check for lib.rs or main.rs (library APIs)
        if let Some(file_name) = function.file.file_name().and_then(|s| s.to_str()) {
            if file_name == "lib.rs" || file_name == "main.rs" {
                // Functions in lib.rs with short names (< 30 chars) likely public exports
                if function.name.len() < 30 && !function.name.contains("::") {
                    return true;
                }
            }
        }

        // Trait implementations - common patterns (contains ::)
        if function.name.contains("::") {
            let trait_methods = [
                "default",
                "new",
                "clone",
                "clone_box",
                "clone_from",
                "from",
                "into",
                "fmt",
                "display",
                "debug",
                "drop",
                "deref",
                "deref_mut",
                "hash",
                "eq",
                "builder",
                "create",
                "with_",
                "try_from",
                "try_into",
            ];

            let name_lower = function.name.to_lowercase();
            if trait_methods
                .iter()
                .any(|&method| name_lower.contains(method))
            {
                return true;
            }
        }

        // Constructor patterns (without ::)
        if function.name == "new"
            || function.name == "builder"
            || function.name == "create"
            || function.name.starts_with("with_")
        {
            return true;
        }

        false
    }

    /// Check if function is self-referential (recursive)
    fn is_self_referential(function: &FunctionId, call_graph: &CallGraph) -> bool {
        let callees = call_graph.get_callees(function);
        callees.iter().any(|callee| callee == function)
    }

    /// Check for orphaned nodes (functions with no connections)
    fn check_orphaned_nodes(
        call_graph: &CallGraph,
        report: &mut ValidationReport,
        config: &CallGraphValidationConfig,
    ) {
        for function in call_graph.get_all_functions() {
            let has_callers = !call_graph.get_callers(function).is_empty();
            let has_callees = !call_graph.get_callees(function).is_empty();
            let is_entry_point = Self::is_entry_point(function, config);
            let is_self_referential = Self::is_self_referential(function, call_graph);
            let is_whitelisted = config.is_whitelisted_orphan(function);

            // Update statistics
            report.statistics.total_functions += 1;
            if is_entry_point {
                report.statistics.entry_points += 1;
            }
            if is_self_referential {
                report.statistics.recursive_functions += 1;
                report.info.push(ValidationInfo::SelfReferentialFunction {
                    function: function.clone(),
                });
            }

            // LEAF FUNCTION: Has callers but no callees (NORMAL - not an issue)
            if has_callers && !has_callees {
                report.statistics.leaf_functions += 1;
                report.info.push(ValidationInfo::LeafFunction {
                    function: function.clone(),
                    caller_count: call_graph.get_callers(function).len(),
                });
                continue; // NOT an issue
            }

            // SELF-REFERENTIAL: Calls itself (recursive)
            if is_self_referential {
                // Not isolated, even if no other callers/callees
                continue; // NOT an issue
            }

            // ISOLATED: No callers, no callees (true orphan)
            if !has_callers && !has_callees && !is_entry_point {
                report.statistics.isolated_functions += 1;
                // Skip if whitelisted
                if !is_whitelisted {
                    report
                        .structural_issues
                        .push(StructuralIssue::IsolatedFunction {
                            function: function.clone(),
                        });
                }
                continue;
            }

            // UNREACHABLE: No callers but has callees (dead code with dependencies)
            if !has_callers && has_callees && !is_entry_point {
                report.statistics.unreachable_functions += 1;
                // Skip if whitelisted
                if !is_whitelisted {
                    report
                        .structural_issues
                        .push(StructuralIssue::UnreachableFunction {
                            function: function.clone(),
                            reason: UnreachableReason::NoCallers,
                        });
                }
            }
        }
    }

    /// Check for duplicate nodes (same function registered multiple times)
    fn check_duplicate_nodes(call_graph: &CallGraph, report: &mut ValidationReport) {
        let mut function_counts: HashMap<String, Vec<FunctionId>> = HashMap::new();

        for function in call_graph.get_all_functions() {
            let key = format!("{}:{}", function.file.display(), function.name);
            function_counts
                .entry(key)
                .or_default()
                .push(function.clone());
        }

        for (_, functions) in function_counts {
            if functions.len() > 1 {
                report
                    .structural_issues
                    .push(StructuralIssue::DuplicateNode {
                        function: functions[0].clone(),
                        count: functions.len(),
                    });
            }
        }
    }

    /// Check for common suspicious patterns
    fn check_heuristics(call_graph: &CallGraph, report: &mut ValidationReport) {
        Self::check_high_fan_in(call_graph, report);
        Self::check_high_fan_out(call_graph, report);
        Self::check_files_with_no_calls(call_graph, report);
        Self::check_unused_public_functions(call_graph, report);
    }

    /// Check for functions with unusually high fan-in (many callers)
    fn check_high_fan_in(call_graph: &CallGraph, report: &mut ValidationReport) {
        const HIGH_CALLER_THRESHOLD: usize = 50;

        for function in call_graph.get_all_functions() {
            let callers = call_graph.get_callers(function);
            if callers.len() > HIGH_CALLER_THRESHOLD {
                report.warnings.push(ValidationWarning::TooManyCallers {
                    function: function.clone(),
                    count: callers.len(),
                });
            }
        }
    }

    /// Check for functions with unusually high fan-out (many callees)
    fn check_high_fan_out(call_graph: &CallGraph, report: &mut ValidationReport) {
        const HIGH_CALLEE_THRESHOLD: usize = 50;

        for function in call_graph.get_all_functions() {
            let callees = call_graph.get_callees(function);
            if callees.len() > HIGH_CALLEE_THRESHOLD {
                report.warnings.push(ValidationWarning::TooManyCallees {
                    function: function.clone(),
                    count: callees.len(),
                });
            }
        }
    }

    /// Check for files where all functions have no callers
    fn check_files_with_no_calls(call_graph: &CallGraph, report: &mut ValidationReport) {
        let mut file_functions: HashMap<PathBuf, Vec<FunctionId>> = HashMap::new();

        for function in call_graph.get_all_functions() {
            file_functions
                .entry(function.file.clone())
                .or_default()
                .push(function.clone());
        }

        for (file, functions) in file_functions {
            if functions.is_empty() {
                continue;
            }

            let all_have_no_callers = functions
                .iter()
                .all(|func| call_graph.get_callers(func).is_empty());

            // Skip files with entry points (main, tests)
            let has_entry_point = functions.iter().any(|f| {
                f.name == "main" || f.name.starts_with("test_") || f.name.contains("::test_")
            });

            if all_have_no_callers && !has_entry_point && functions.len() >= 3 {
                report.warnings.push(ValidationWarning::FileWithNoCalls {
                    file: file.clone(),
                    function_count: functions.len(),
                });
            }
        }
    }

    /// Check for public functions with no callers
    fn check_unused_public_functions(call_graph: &CallGraph, report: &mut ValidationReport) {
        for function in call_graph.get_all_functions() {
            // Simple heuristic: functions not in impl blocks and starting with lowercase
            // are likely standalone functions that could be public
            let is_standalone = !function.name.contains("::");
            let starts_lowercase = function
                .name
                .chars()
                .next()
                .map(|c| c.is_lowercase())
                .unwrap_or(false);

            if is_standalone && starts_lowercase {
                let has_no_callers = call_graph.get_callers(function).is_empty();

                // Skip test and main functions
                let is_entry_point = function.name == "main"
                    || function.name.starts_with("test_")
                    || function.name.contains("::test_");

                if has_no_callers && !is_entry_point {
                    report
                        .warnings
                        .push(ValidationWarning::UnusedPublicFunction {
                            function: function.clone(),
                        });
                }
            }
        }
    }

    /// Validate against expected patterns
    pub fn validate_expectations(
        call_graph: &CallGraph,
        expectations: &[Expectation],
    ) -> ValidationReport {
        Self::validate_expectations_with_config(
            call_graph,
            expectations,
            &CallGraphValidationConfig::default(),
        )
    }

    /// Validate against expected patterns with custom configuration
    pub fn validate_expectations_with_config(
        call_graph: &CallGraph,
        expectations: &[Expectation],
        config: &CallGraphValidationConfig,
    ) -> ValidationReport {
        let report = Self::validate_with_config(call_graph, config);

        for expectation in expectations {
            if !expectation.check(call_graph) {
                // Add expectation failure as a structural issue
                // For now, we'll skip this as it requires more complex reporting
            }
        }

        report
    }
}

/// Expected pattern in the call graph
#[derive(Debug, Clone)]
pub struct Expectation {
    pub description: String,
    pub check: fn(&CallGraph) -> bool,
}

impl Expectation {
    /// Create a new expectation
    pub fn new(description: String, check: fn(&CallGraph) -> bool) -> Self {
        Self { description, check }
    }

    /// Check if the expectation is met
    pub fn check(&self, call_graph: &CallGraph) -> bool {
        (self.check)(call_graph)
    }
}

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

    #[test]
    fn test_validation_report_creation() {
        let report = ValidationReport::new();
        assert_eq!(report.health_score, 100);
        assert!(!report.has_issues());
    }

    #[test]
    fn test_health_score_calculation() {
        let mut report = ValidationReport::new();

        // Add structural issues
        report
            .structural_issues
            .push(StructuralIssue::IsolatedFunction {
                function: FunctionId::new(PathBuf::from("test.rs"), "func".to_string(), 10),
            });

        // Add warnings
        report.warnings.push(ValidationWarning::TooManyCallers {
            function: FunctionId::new(PathBuf::from("test.rs"), "popular".to_string(), 20),
            count: 100,
        });

        report.calculate_health_score();

        // 100 - 0 (isolated: 0.5 rounds to 0) - 2 (warning) = 98
        assert_eq!(report.health_score, 98);
        assert!(report.has_issues());
    }

    #[test]
    fn test_validate_empty_graph() {
        let graph = CallGraph::new();
        let report = CallGraphValidator::validate(&graph);

        assert!(!report.has_issues());
        assert_eq!(report.health_score, 100);
    }

    #[test]
    fn test_expectation() {
        let expectation = Expectation::new("Has at least one function".to_string(), |graph| {
            graph.node_count() > 0
        });

        let empty_graph = CallGraph::new();
        assert!(!expectation.check(&empty_graph));
    }

    #[test]
    fn test_leaf_function_not_orphaned() {
        let mut call_graph = CallGraph::new();
        let leaf = FunctionId::new(PathBuf::from("test.rs"), "utility_fn".to_string(), 10);
        let caller = FunctionId::new(PathBuf::from("test.rs"), "main_fn".to_string(), 5);

        call_graph.add_function(leaf.clone(), false, false, 1, 10);
        call_graph.add_function(caller.clone(), false, false, 1, 5);
        call_graph.add_call_parts(
            caller,
            leaf.clone(),
            crate::priority::call_graph::CallType::Direct,
        );

        let report = CallGraphValidator::validate(&call_graph);

        // Leaf function should NOT be in structural_issues
        assert!(
            !report
                .structural_issues
                .iter()
                .any(|issue| matches!(issue, StructuralIssue::IsolatedFunction { .. })),
            "Leaf function should not be flagged as isolated"
        );

        // Should be in info as leaf
        assert!(
            report
                .info
                .iter()
                .any(|info| matches!(info, ValidationInfo::LeafFunction { .. })),
            "Leaf function should be in info"
        );
    }

    #[test]
    fn test_self_referential_not_isolated() {
        let mut call_graph = CallGraph::new();
        let recursive = FunctionId::new(PathBuf::from("test.rs"), "factorial".to_string(), 10);

        call_graph.add_function(recursive.clone(), false, false, 1, 10);
        call_graph.add_call_parts(
            recursive.clone(),
            recursive.clone(),
            crate::priority::call_graph::CallType::Direct,
        ); // Self-call

        let report = CallGraphValidator::validate(&call_graph);

        // Should NOT be marked as isolated
        assert!(
            !report
                .structural_issues
                .iter()
                .any(|issue| matches!(issue, StructuralIssue::IsolatedFunction { .. })),
            "Recursive function should not be flagged as isolated"
        );

        // Should be in info as self-referential
        assert!(
            report
                .info
                .iter()
                .any(|info| matches!(info, ValidationInfo::SelfReferentialFunction { .. })),
            "Recursive function should be in info"
        );
    }

    #[test]
    fn test_entry_point_detection() {
        let config = CallGraphValidationConfig::default();
        let test_cases = vec![
            ("src/main.rs", "main"),
            ("src/lib.rs", "test_my_function"),
            ("examples/demo.rs", "demo_main"),
            ("benches/my_bench.rs", "bench_performance"),
            ("src/traits.rs", "Default::default"),
            ("src/types.rs", "MyType::new"),
        ];

        for (file, name) in test_cases {
            let func = FunctionId::new(PathBuf::from(file), name.to_string(), 1);
            assert!(
                CallGraphValidator::is_entry_point(&func, &config),
                "Expected {} in {} to be entry point",
                name,
                file
            );
        }
    }

    #[test]
    fn test_isolated_function_detected() {
        let mut call_graph = CallGraph::new();
        let isolated = FunctionId::new(PathBuf::from("test.rs"), "unused_fn".to_string(), 10);

        call_graph.add_function(isolated.clone(), false, false, 1, 10);
        // No calls added

        let report = CallGraphValidator::validate(&call_graph);

        // Should be marked as isolated
        assert!(
            report.structural_issues.iter().any(
                |issue| matches!(issue, StructuralIssue::IsolatedFunction { function } if function == &isolated)
            ),
            "Isolated function should be detected"
        );
    }

    #[test]
    fn test_health_score_improved() {
        let mut call_graph = CallGraph::new();

        // Add main function once
        let main = FunctionId::new(PathBuf::from("test.rs"), "main".to_string(), 1);
        call_graph.add_function(main.clone(), true, false, 1, 5);

        // Add many leaf functions (should NOT hurt score significantly)
        for i in 0..1000 {
            let leaf = FunctionId::new(PathBuf::from("test.rs"), format!("leaf_{}", i), i * 10);
            call_graph.add_function(leaf.clone(), false, false, 1, 10);
            call_graph.add_call_parts(
                main.clone(),
                leaf,
                crate::priority::call_graph::CallType::Direct,
            );
        }

        let report = CallGraphValidator::validate(&call_graph);

        // Health score should be high (no real issues)
        assert!(
            report.health_score >= 80,
            "Health score should be 80+ for leaf functions, got {}",
            report.health_score
        );
    }

    #[test]
    fn test_unreachable_function_detected() {
        let mut call_graph = CallGraph::new();
        let unreachable = FunctionId::new(PathBuf::from("test.rs"), "dead_code".to_string(), 10);
        let callee = FunctionId::new(PathBuf::from("test.rs"), "helper".to_string(), 20);

        call_graph.add_function(unreachable.clone(), false, false, 1, 10);
        call_graph.add_function(callee.clone(), false, false, 1, 5);
        call_graph.add_call_parts(
            unreachable.clone(),
            callee,
            crate::priority::call_graph::CallType::Direct,
        ); // Dead code calls helper

        let report = CallGraphValidator::validate(&call_graph);

        // Should be marked as unreachable
        assert!(
            report.structural_issues.iter().any(
                |issue| matches!(issue, StructuralIssue::UnreachableFunction { function, .. } if function == &unreachable)
            ),
            "Unreachable function should be detected"
        );
    }

    #[test]
    fn test_statistics_collected() {
        let mut call_graph = CallGraph::new();

        // Add entry point
        let main = FunctionId::new(PathBuf::from("main.rs"), "main".to_string(), 1);
        call_graph.add_function(main.clone(), true, false, 1, 5);

        // Add leaf function
        let leaf = FunctionId::new(PathBuf::from("test.rs"), "utility".to_string(), 10);
        call_graph.add_function(leaf.clone(), false, false, 1, 10);
        call_graph.add_call_parts(
            main.clone(),
            leaf,
            crate::priority::call_graph::CallType::Direct,
        );

        // Add recursive function
        let recursive = FunctionId::new(PathBuf::from("test.rs"), "factorial".to_string(), 20);
        call_graph.add_function(recursive.clone(), false, false, 1, 15);
        call_graph.add_call_parts(
            recursive.clone(),
            recursive.clone(),
            crate::priority::call_graph::CallType::Direct,
        );

        let report = CallGraphValidator::validate(&call_graph);

        // Verify statistics
        assert_eq!(report.statistics.total_functions, 3);
        assert_eq!(report.statistics.entry_points, 1);
        assert_eq!(report.statistics.leaf_functions, 1);
        assert_eq!(report.statistics.recursive_functions, 1);
        assert_eq!(report.statistics.isolated_functions, 0);
        assert_eq!(report.statistics.unreachable_functions, 0);
    }

    #[test]
    fn test_orphan_whitelist() {
        let mut call_graph = CallGraph::new();
        let isolated = FunctionId::new(PathBuf::from("test.rs"), "utility_fn".to_string(), 10);

        call_graph.add_function(isolated.clone(), false, false, 1, 10);
        // No calls added - function is isolated

        // Validate without config - should be flagged as isolated
        let report = CallGraphValidator::validate(&call_graph);
        assert!(
            report.structural_issues.iter().any(
                |issue| matches!(issue, StructuralIssue::IsolatedFunction { function } if function == &isolated)
            ),
            "Isolated function should be detected without whitelist"
        );

        // Validate with whitelist config - should NOT be flagged
        let mut config = CallGraphValidationConfig::new();
        config.add_orphan_whitelist("utility_fn".to_string());
        let report_with_config = CallGraphValidator::validate_with_config(&call_graph, &config);

        assert!(
            !report_with_config.structural_issues.iter().any(
                |issue| matches!(issue, StructuralIssue::IsolatedFunction { function } if function == &isolated)
            ),
            "Whitelisted function should not be flagged as isolated"
        );
    }

    #[test]
    fn test_additional_entry_points() {
        let mut call_graph = CallGraph::new();
        let custom_entry = FunctionId::new(PathBuf::from("test.rs"), "custom_main".to_string(), 10);
        let helper = FunctionId::new(PathBuf::from("test.rs"), "helper".to_string(), 20);

        call_graph.add_function(custom_entry.clone(), false, false, 1, 10);
        call_graph.add_function(helper.clone(), false, false, 1, 5);
        call_graph.add_call_parts(
            custom_entry.clone(),
            helper,
            crate::priority::call_graph::CallType::Direct,
        );

        // Without config - custom_main has no callers, should be unreachable
        let report = CallGraphValidator::validate(&call_graph);
        assert!(
            report.structural_issues.iter().any(
                |issue| matches!(issue, StructuralIssue::UnreachableFunction { function, .. } if function == &custom_entry)
            ),
            "Custom entry point should be unreachable without config"
        );

        // With additional entry point config - should NOT be flagged
        let mut config = CallGraphValidationConfig::new();
        config.add_entry_point("custom_main".to_string());
        let report_with_config = CallGraphValidator::validate_with_config(&call_graph, &config);

        assert!(
            !report_with_config.structural_issues.iter().any(
                |issue| matches!(issue, StructuralIssue::UnreachableFunction { function, .. } if function == &custom_entry)
            ),
            "Configured entry point should not be flagged as unreachable"
        );
        assert_eq!(report_with_config.statistics.entry_points, 1);
    }

    #[test]
    fn test_config_builder_pattern() {
        let mut config = CallGraphValidationConfig::new();
        config
            .add_orphan_whitelist("temp_fn".to_string())
            .add_orphan_whitelist("debug_fn".to_string())
            .add_entry_point("custom_entry".to_string());

        assert_eq!(config.orphan_whitelist.len(), 2);
        assert_eq!(config.additional_entry_points.len(), 1);
        assert!(config.orphan_whitelist.contains("temp_fn"));
        assert!(config.additional_entry_points.contains("custom_entry"));
    }

    #[test]
    #[ignore] // Integration test - run explicitly
    fn test_real_project_health_score() {
        use crate::builders::call_graph;
        use crate::core::Language;
        use crate::io::walker;
        use std::env;

        // Get the project root (debtmap's own codebase)
        let project_root = env::current_dir().expect("Failed to get current directory");

        // Find Rust files in the project
        let config = crate::config::get_config();
        let files =
            walker::find_project_files_with_config(&project_root, vec![Language::Rust], config)
                .expect("Failed to find project files");

        // Analyze files to get function metrics
        let file_metrics: Vec<_> = files
            .iter()
            .filter_map(|path| crate::analysis_utils::analyze_single_file(path))
            .collect();

        // Extract all functions from file metrics
        let all_functions: Vec<_> = file_metrics
            .iter()
            .flat_map(|fm| fm.complexity.functions.clone())
            .collect();

        // Build call graph
        let mut call_graph = call_graph::build_initial_call_graph(&all_functions);

        // Build full call graph with Rust-specific features
        call_graph::process_rust_files_for_call_graph(
            &project_root,
            &mut call_graph,
            false,
            false,
            |_| {}, // No-op progress callback for test
        )
        .expect("Failed to process Rust files");

        // Validate the call graph
        let validation_report = CallGraphValidator::validate(&call_graph);

        // Assert health score is >= 70 (spec requirement)
        assert!(
            validation_report.health_score >= 70,
            "Health score {} is below threshold 70. Structural issues: {}, Warnings: {}",
            validation_report.health_score,
            validation_report.structural_issues.len(),
            validation_report.warnings.len()
        );

        // Assert isolated functions are < 500 (spec requirement)
        assert!(
            validation_report.statistics.isolated_functions < 500,
            "Isolated functions {} exceeds threshold 500",
            validation_report.statistics.isolated_functions
        );

        // Print summary for manual inspection
        eprintln!("\n=== Debtmap Self-Analysis Health Report ===");
        eprintln!("Health Score: {}/100", validation_report.health_score);
        eprintln!(
            "Total Functions: {}",
            validation_report.statistics.total_functions
        );
        eprintln!(
            "Entry Points: {}",
            validation_report.statistics.entry_points
        );
        eprintln!(
            "Isolated Functions: {}",
            validation_report.statistics.isolated_functions
        );
        eprintln!(
            "Structural Issues: {}",
            validation_report.structural_issues.len()
        );
        eprintln!("Warnings: {}", validation_report.warnings.len());
    }
}