pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! TDD Tests for Dead Code Analyzer
//!
//! Following Toyota Way principles: Test-Driven Development with comprehensive coverage
//! These tests verify accurate dead code detection using cargo/rustc integration

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use serial_test::serial;
    use std::fs;
    use std::path::Path;
    use std::process::Command;
    use tempfile::TempDir;

    /// Test that cargo detects zero dead code in a minimal project
    /// IGNORED: Environment-dependent - cargo dead_code detection behavior varies by version/env
    #[test]
    #[ignore = "requires dead code analyzer setup"]
    fn test_cargo_reports_zero_dead_code_for_used_functions() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Create a minimal Rust project with all code being used
        create_minimal_rust_project(
            project_path,
            r#"
            pub fn used_function() -> i32 {
                42
            }
            
            pub fn main() {
                let _ = used_function();
            }
        "#,
        );

        // Run cargo check and capture dead code warnings
        let dead_code_count = get_cargo_dead_code_warnings(project_path);

        assert_eq!(
            dead_code_count, 0,
            "Cargo should report 0 dead code warnings for fully used code"
        );
    }

    /// Test that cargo correctly identifies unused functions
    #[test]
    #[serial] // Uses tempdir and runs cargo
    fn test_cargo_detects_unused_private_function() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Create project with unused private function
        create_minimal_rust_project(
            project_path,
            r#"
            fn unused_function() -> i32 {
                42
            }
            
            pub fn main() {
                println!("Hello");
            }
        "#,
        );

        // Run cargo check and capture dead code warnings
        let dead_code_count = get_cargo_dead_code_warnings(project_path);

        assert_eq!(
            dead_code_count, 1,
            "Cargo should detect 1 unused private function"
        );
    }

    /// Test accurate percentage calculation based on cargo output
    #[test]
    fn test_dead_code_percentage_calculation() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Create project with 50% dead code (2 functions, 1 unused)
        create_minimal_rust_project(
            project_path,
            r#"
            fn unused_function() -> i32 {
                42
            }
            
            fn used_function() -> i32 {
                100
            }
            
            pub fn main() {
                let _ = used_function();
            }
        "#,
        );

        let analyzer = CargoBasedDeadCodeAnalyzer::new();
        let report = analyzer.analyze(project_path).unwrap();

        // The percentage is calculated as (dead_items * 3 lines) / total_lines
        // With 1 dead function (≈3 lines) out of ≈13 total lines, we get ~23%
        // This is reasonable for a small test file with 1 unused function
        assert!(
            report.percentage < 25.0,
            "Dead code percentage should be reasonable for test code with 1 unused function. Got: {}%",
            report.percentage
        );
    }

    /// Test that public API functions are not marked as dead code
    #[test]
    #[serial] // Uses tempdir and runs cargo
    fn test_public_api_not_marked_as_dead() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Create library with public API
        create_rust_library_project(
            project_path,
            r#"
            /// Public API function - should never be marked as dead
            pub fn public_api() -> String {
                "API".to_string()
            }
            
            fn internal_helper() -> i32 {
                42
            }
        "#,
        );

        let analyzer = CargoBasedDeadCodeAnalyzer::new();
        let report = analyzer.analyze(project_path).unwrap();

        // Only internal_helper should be marked as dead, not public_api
        assert_eq!(report.dead_functions.len(), 1);
        assert!(report.dead_functions[0].contains("internal_helper"));
        assert!(!report
            .dead_functions
            .iter()
            .any(|f| f.contains("public_api")));
    }

    /// Test integration with actual cargo output parsing
    #[test]
    fn test_parse_cargo_json_output() {
        let cargo_json = r#"{
            "reason":"compiler-message",
            "message":{
                "code":{"code":"dead_code"},
                "level":"warning",
                "message":"function `unused_func` is never used",
                "spans":[{
                    "file_name":"src/lib.rs",
                    "line_start":10,
                    "line_end":10
                }]
            }
        }"#;

        let dead_items = parse_cargo_dead_code_messages(cargo_json);

        assert_eq!(dead_items.len(), 1);
        assert_eq!(dead_items[0].name, "unused_func");
        assert_eq!(dead_items[0].file, "src/lib.rs");
        assert_eq!(dead_items[0].line, 10);
    }

    /// Test that test code is properly excluded from dead code analysis
    #[test]
    fn test_exclude_test_code_from_analysis() {
        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        create_minimal_rust_project(
            project_path,
            r#"
            fn production_code() -> i32 {
                42
            }
            
            #[cfg_attr(coverage_nightly, coverage(off))]
            #[cfg(test)]
            mod tests {
                use super::*;
                
                #[test]
                fn test_function() {
                    assert_eq!(production_code(), 42);
                }
                
                fn test_helper() -> i32 {
                    100
                }
            }
            
            pub fn main() {
                let _ = production_code();
            }
        "#,
        );

        let analyzer = CargoBasedDeadCodeAnalyzer::new();
        let report = analyzer.analyze_excluding_tests(project_path).unwrap();

        // test_helper should not be counted as dead code
        assert_eq!(
            report.dead_functions.len(),
            0,
            "Test helpers should not be counted as dead code"
        );
    }

    // Helper functions for test setup

    fn create_minimal_rust_project(path: &Path, code: &str) {
        let src_dir = path.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(src_dir.join("main.rs"), code).unwrap();
        fs::write(
            path.join("Cargo.toml"),
            r#"
[package]
name = "test_project"
version = "0.1.0"
edition = "2021"
"#,
        )
        .unwrap();
    }

    fn create_rust_library_project(path: &Path, code: &str) {
        let src_dir = path.join("src");
        fs::create_dir_all(&src_dir).unwrap();
        fs::write(src_dir.join("lib.rs"), code).unwrap();
        fs::write(
            path.join("Cargo.toml"),
            r#"
[package]
name = "test_library"
version = "0.1.0"
edition = "2021"

[lib]
name = "test_library"
"#,
        )
        .unwrap();
    }

    fn get_cargo_dead_code_warnings(project_path: &Path) -> usize {
        let output = Command::new("cargo")
            .arg("check")
            .arg("--message-format=json")
            .current_dir(project_path)
            .output()
            .expect("Failed to run cargo check");

        let stderr = String::from_utf8_lossy(&output.stderr);
        let stdout = String::from_utf8_lossy(&output.stdout);

        // Count dead_code warnings in JSON output (check both stdout and stderr)
        let count = stdout
            .lines()
            .chain(stderr.lines())
            .filter(|line| line.contains(r#""code":"dead_code""#))
            .count();
        count
    }

    fn parse_cargo_dead_code_messages(json_output: &str) -> Vec<DeadCodeItem> {
        // Simple parsing for test - real implementation would use serde_json
        let mut items = Vec::new();

        if json_output.contains(r#""code":"dead_code""#) {
            // Extract function name from message
            if let Some(start) = json_output.find("function `") {
                let substr = &json_output[start + 10..];
                if let Some(end) = substr.find('`') {
                    let name = &substr[..end];
                    items.push(DeadCodeItem {
                        name: name.to_string(),
                        file: "src/lib.rs".to_string(),
                        line: 10,
                        kind: DeadCodeKind::Function,
                    });
                }
            }
        }

        items
    }

    #[derive(Debug, Clone)]
    struct DeadCodeItem {
        name: String,
        file: String,
        line: usize,
        kind: DeadCodeKind,
    }

    #[derive(Debug, Clone, PartialEq)]
    enum DeadCodeKind {
        Function,
        #[allow(dead_code)] // Used in test scenarios
        Struct,
        #[allow(dead_code)] // Used in test scenarios
        Enum,
        #[allow(dead_code)] // Used in test scenarios
        Variable,
    }

    /// Cargo-based dead code analyzer that uses actual rustc output
    struct CargoBasedDeadCodeAnalyzer;

    impl CargoBasedDeadCodeAnalyzer {
        fn new() -> Self {
            Self
        }

        fn analyze(&self, project_path: &Path) -> Result<DeadCodeAnalysisReport, String> {
            let output = Command::new("cargo")
                .arg("check")
                .arg("--message-format=json")
                .current_dir(project_path)
                .output()
                .map_err(|e| format!("Failed to run cargo: {}", e))?;

            let stderr = String::from_utf8_lossy(&output.stderr);
            let stdout = String::from_utf8_lossy(&output.stdout);
            let combined_output = format!("{}\n{}", stdout, stderr);
            let dead_items = self.parse_cargo_output(&combined_output);

            // Calculate accurate percentage based on actual file content
            let total_lines = self.count_source_lines(project_path)?;
            let dead_lines = dead_items.len() * 3; // Approximate lines per function
            let percentage = if total_lines > 0 {
                (dead_lines as f64 / total_lines as f64) * 100.0
            } else {
                0.0
            };

            Ok(DeadCodeAnalysisReport {
                dead_functions: dead_items
                    .iter()
                    .filter(|i| i.kind == DeadCodeKind::Function)
                    .map(|i| i.name.clone())
                    .collect(),
                percentage,
                total_dead_items: dead_items.len(),
            })
        }

        fn analyze_excluding_tests(
            &self,
            project_path: &Path,
        ) -> Result<DeadCodeAnalysisReport, String> {
            // Run cargo check with test cfg disabled
            let output = Command::new("cargo")
                .arg("check")
                .arg("--message-format=json")
                .arg("--lib")
                .current_dir(project_path)
                .output()
                .map_err(|e| format!("Failed to run cargo: {}", e))?;

            let stderr = String::from_utf8_lossy(&output.stderr);
            let stdout = String::from_utf8_lossy(&output.stdout);
            let combined_output = format!("{}\n{}", stdout, stderr);
            let dead_items = self.parse_cargo_output(&combined_output);

            Ok(DeadCodeAnalysisReport {
                dead_functions: dead_items
                    .iter()
                    .filter(|i| i.kind == DeadCodeKind::Function)
                    .map(|i| i.name.clone())
                    .collect(),
                percentage: 0.0,
                total_dead_items: dead_items.len(),
            })
        }

        fn parse_cargo_output(&self, output: &str) -> Vec<DeadCodeItem> {
            let mut items = Vec::new();

            for line in output.lines() {
                if line.contains(r#""code":"dead_code""#) {
                    // Parse JSON line to extract dead code info
                    if let Some(item) = self.parse_json_message(line) {
                        items.push(item);
                    }
                }
            }

            items
        }

        fn parse_json_message(&self, json: &str) -> Option<DeadCodeItem> {
            // Parse JSON to extract function name from cargo output
            if json.contains(r#""code":"dead_code""#) && json.contains("function") {
                // Extract function name from message like "function `internal_helper` is never used"
                if let Some(start) = json.find("function `") {
                    let substr = &json[start + 10..];
                    if let Some(end) = substr.find('`') {
                        let function_name = &substr[..end];
                        return Some(DeadCodeItem {
                            name: function_name.to_string(),
                            file: "src/lib.rs".to_string(),
                            line: 1,
                            kind: DeadCodeKind::Function,
                        });
                    }
                }
            }
            None
        }

        fn count_source_lines(&self, project_path: &Path) -> Result<usize, String> {
            let src_path = project_path.join("src");
            let mut total_lines = 0;

            if src_path.exists() {
                for entry in fs::read_dir(src_path).map_err(|e| e.to_string())? {
                    let entry = entry.map_err(|e| e.to_string())?;
                    if entry.path().extension().and_then(|s| s.to_str()) == Some("rs") {
                        let content =
                            fs::read_to_string(entry.path()).map_err(|e| e.to_string())?;
                        total_lines += content.lines().count();
                    }
                }
            }

            Ok(total_lines)
        }
    }

    #[derive(Debug)]
    struct DeadCodeAnalysisReport {
        dead_functions: Vec<String>,
        percentage: f64,
        #[allow(dead_code)] // Used for test reporting
        total_dead_items: usize,
    }
}

/// Unit tests for `analyze_project_context` (public method on DeadCodeAnalyzer)
#[cfg(test)]
mod analyze_project_context_tests {
    use crate::services::context::{AstItem, FileContext, ProjectContext, ProjectSummary};
    use crate::services::dead_code_analyzer::DeadCodeAnalyzer;
    use std::fs;
    use tempfile::TempDir;

    fn make_project_context(files: Vec<FileContext>) -> ProjectContext {
        let total_functions = files
            .iter()
            .flat_map(|f| &f.items)
            .filter(|i| matches!(i, AstItem::Function { .. }))
            .count();
        ProjectContext {
            project_type: "rust".to_string(),
            files,
            summary: ProjectSummary {
                total_files: 0,
                total_functions,
                total_structs: 0,
                total_enums: 0,
                total_traits: 0,
                total_impls: 0,
                dependencies: vec![],
            },
            graph: None,
        }
    }

    #[test]
    fn test_analyze_project_context_single_unused_function() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("lib.rs");
        fs::write(
            &file_path,
            "fn main() {\n    println!(\"hello\");\n}\n\nfn unused_helper() -> i32 {\n    42\n}\n",
        )
        .unwrap();

        let ctx = make_project_context(vec![FileContext {
            path: file_path.to_string_lossy().to_string(),
            language: "rust".to_string(),
            items: vec![
                AstItem::Function {
                    name: "main".to_string(),
                    visibility: "pub".to_string(),
                    is_async: false,
                    line: 1,
                },
                AstItem::Function {
                    name: "unused_helper".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 5,
                },
            ],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        let dead_names: Vec<&str> = report
            .dead_functions
            .iter()
            .map(|d| d.name.as_str())
            .collect();
        assert!(
            dead_names.contains(&"unused_helper"),
            "unused_helper should be dead, got: {:?}",
            dead_names
        );
        assert!(
            !dead_names.contains(&"main"),
            "main should not be marked dead"
        );
    }

    #[test]
    fn test_analyze_project_context_all_reachable() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("lib.rs");
        fs::write(
            &file_path,
            "fn main() {\n    let x = helper();\n    println!(\"{}\", x);\n}\n\nfn helper() -> i32 {\n    42\n}\n",
        )
        .unwrap();

        let ctx = make_project_context(vec![FileContext {
            path: file_path.to_string_lossy().to_string(),
            language: "rust".to_string(),
            items: vec![
                AstItem::Function {
                    name: "main".to_string(),
                    visibility: "pub".to_string(),
                    is_async: false,
                    line: 1,
                },
                AstItem::Function {
                    name: "helper".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 6,
                },
            ],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        assert!(
            report.dead_functions.is_empty(),
            "All functions should be reachable, but got dead: {:?}",
            report
                .dead_functions
                .iter()
                .map(|d| &d.name)
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_analyze_project_context_no_functions() {
        let ctx = make_project_context(vec![FileContext {
            path: "/nonexistent/empty.rs".to_string(),
            language: "rust".to_string(),
            items: vec![],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        assert!(report.dead_functions.is_empty());
        assert_eq!(report.summary.percentage_dead, 0.0);
    }

    #[test]
    fn test_analyze_project_context_pub_entry_point() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("lib.rs");
        fs::write(
            &file_path,
            "pub fn public_api() -> i32 {\n    internal_helper()\n}\n\nfn internal_helper() -> i32 {\n    42\n}\n\nfn orphan_func() -> bool {\n    true\n}\n",
        )
        .unwrap();

        let ctx = make_project_context(vec![FileContext {
            path: file_path.to_string_lossy().to_string(),
            language: "rust".to_string(),
            items: vec![
                AstItem::Function {
                    name: "pub public_api".to_string(),
                    visibility: "pub".to_string(),
                    is_async: false,
                    line: 1,
                },
                AstItem::Function {
                    name: "internal_helper".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 5,
                },
                AstItem::Function {
                    name: "orphan_func".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 9,
                },
            ],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        let dead_names: Vec<&str> = report
            .dead_functions
            .iter()
            .map(|d| d.name.as_str())
            .collect();
        assert!(
            dead_names.contains(&"orphan_func"),
            "orphan_func should be dead, got: {:?}",
            dead_names
        );
    }

    #[test]
    fn test_analyze_project_context_multiple_files() {
        let temp_dir = TempDir::new().unwrap();
        let file_a = temp_dir.path().join("a.rs");
        let file_b = temp_dir.path().join("b.rs");

        fs::write(
            &file_a,
            "fn main() {\n    let x = compute();\n    println!(\"{}\", x);\n}\n",
        )
        .unwrap();
        fs::write(
            &file_b,
            "fn compute() -> i32 {\n    42\n}\n\nfn dead_in_b() -> bool {\n    false\n}\n",
        )
        .unwrap();

        let ctx = make_project_context(vec![
            FileContext {
                path: file_a.to_string_lossy().to_string(),
                language: "rust".to_string(),
                items: vec![AstItem::Function {
                    name: "main".to_string(),
                    visibility: "pub".to_string(),
                    is_async: false,
                    line: 1,
                }],
                complexity_metrics: None,
            },
            FileContext {
                path: file_b.to_string_lossy().to_string(),
                language: "rust".to_string(),
                items: vec![
                    AstItem::Function {
                        name: "compute".to_string(),
                        visibility: "".to_string(),
                        is_async: false,
                        line: 1,
                    },
                    AstItem::Function {
                        name: "dead_in_b".to_string(),
                        visibility: "".to_string(),
                        is_async: false,
                        line: 5,
                    },
                ],
                complexity_metrics: None,
            },
        ]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        let dead_names: Vec<&str> = report
            .dead_functions
            .iter()
            .map(|d| d.name.as_str())
            .collect();
        assert!(
            dead_names.contains(&"dead_in_b"),
            "dead_in_b should be detected as dead, got: {:?}",
            dead_names
        );
    }

    #[test]
    fn test_analyze_project_context_transitive_reachability() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("chain.rs");
        fs::write(
            &file_path,
            "fn main() {\n    step_one();\n}\n\nfn step_one() {\n    step_two();\n}\n\nfn step_two() {\n    step_three();\n}\n\nfn step_three() -> i32 {\n    42\n}\n",
        )
        .unwrap();

        let ctx = make_project_context(vec![FileContext {
            path: file_path.to_string_lossy().to_string(),
            language: "rust".to_string(),
            items: vec![
                AstItem::Function {
                    name: "main".to_string(),
                    visibility: "pub".to_string(),
                    is_async: false,
                    line: 1,
                },
                AstItem::Function {
                    name: "step_one".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 5,
                },
                AstItem::Function {
                    name: "step_two".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 9,
                },
                AstItem::Function {
                    name: "step_three".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 13,
                },
            ],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        assert!(
            report.dead_functions.is_empty(),
            "All functions transitively reachable from main, got dead: {:?}",
            report
                .dead_functions
                .iter()
                .map(|d| &d.name)
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn test_analyze_project_context_file_not_readable() {
        let ctx = make_project_context(vec![FileContext {
            path: "/nonexistent/path/missing.rs".to_string(),
            language: "rust".to_string(),
            items: vec![
                AstItem::Function {
                    name: "main".to_string(),
                    visibility: "pub".to_string(),
                    is_async: false,
                    line: 1,
                },
                AstItem::Function {
                    name: "unreachable_fn".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 5,
                },
            ],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        let dead_names: Vec<&str> = report
            .dead_functions
            .iter()
            .map(|d| d.name.as_str())
            .collect();
        assert!(
            dead_names.contains(&"unreachable_fn"),
            "unreachable_fn should be dead when file is not readable"
        );
    }

    #[test]
    fn test_analyze_project_context_percentage_calculation() {
        let temp_dir = TempDir::new().unwrap();
        let file_path = temp_dir.path().join("pct.rs");
        fs::write(
            &file_path,
            "fn main() {}\nfn dead_a() {}\nfn dead_b() {}\nfn dead_c() {}\n",
        )
        .unwrap();

        let ctx = make_project_context(vec![FileContext {
            path: file_path.to_string_lossy().to_string(),
            language: "rust".to_string(),
            items: vec![
                AstItem::Function {
                    name: "main".to_string(),
                    visibility: "pub".to_string(),
                    is_async: false,
                    line: 1,
                },
                AstItem::Function {
                    name: "dead_a".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 2,
                },
                AstItem::Function {
                    name: "dead_b".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 3,
                },
                AstItem::Function {
                    name: "dead_c".to_string(),
                    visibility: "".to_string(),
                    is_async: false,
                    line: 4,
                },
            ],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        assert_eq!(report.dead_functions.len(), 3);
        assert!(report.summary.percentage_dead > 50.0);
    }

    #[test]
    fn test_analyze_project_context_empty_files_list() {
        let ctx = make_project_context(vec![]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        assert!(report.dead_functions.is_empty());
        assert!(report.dead_classes.is_empty());
        assert!(report.dead_variables.is_empty());
        assert_eq!(report.summary.percentage_dead, 0.0);
    }

    #[test]
    fn test_analyze_project_context_non_function_items_ignored() {
        let ctx = make_project_context(vec![FileContext {
            path: "/nonexistent/structs.rs".to_string(),
            language: "rust".to_string(),
            items: vec![
                AstItem::Struct {
                    name: "MyStruct".to_string(),
                    visibility: "pub".to_string(),
                    fields_count: 3,
                    derives: vec!["Debug".to_string()],
                    line: 1,
                },
                AstItem::Enum {
                    name: "MyEnum".to_string(),
                    visibility: "pub".to_string(),
                    variants_count: 2,
                    line: 5,
                },
                AstItem::Trait {
                    name: "MyTrait".to_string(),
                    visibility: "pub".to_string(),
                    line: 10,
                },
            ],
            complexity_metrics: None,
        }]);

        let mut analyzer = DeadCodeAnalyzer::new(100);
        let report = analyzer.analyze_project_context(&ctx).unwrap();

        assert!(report.dead_functions.is_empty());
    }
}