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
use crate::core::{DebtItem, DebtType, FunctionMetrics, Priority};
use std::path::{Path, PathBuf};

/// Represents different types of code smells
#[derive(Debug, Clone, PartialEq)]
pub enum SmellType {
    LongParameterList,
    LargeClass,
    LongMethod,
    FeatureEnvy,
    DataClump,
    DeepNesting,
    DuplicateCode,
}

/// A detected code smell with its location and details
#[derive(Debug, Clone)]
pub struct CodeSmell {
    pub smell_type: SmellType,
    pub location: PathBuf,
    pub line: usize,
    pub message: String,
    pub severity: Priority,
}

impl CodeSmell {
    /// Convert a code smell to a debt item
    pub fn to_debt_item(&self) -> DebtItem {
        DebtItem {
            id: format!(
                "smell-{:?}-{}-{}",
                self.smell_type,
                self.location.display(),
                self.line
            ),
            debt_type: DebtType::CodeSmell {
                smell_type: Some(format!("{:?}", self.smell_type)),
            },
            priority: self.severity,
            file: self.location.clone(),
            line: self.line,
            column: None,
            message: self.message.clone(),
            context: None,
        }
    }
}

/// Detect long parameter lists in functions
pub fn detect_long_parameter_list(func: &FunctionMetrics, param_count: usize) -> Option<CodeSmell> {
    const THRESHOLD: usize = 5;

    if param_count > THRESHOLD {
        Some(CodeSmell {
            smell_type: SmellType::LongParameterList,
            location: func.file.clone(),
            line: func.line,
            message: format!(
                "Function '{}' has {} parameters (threshold: {})",
                func.name, param_count, THRESHOLD
            ),
            severity: if param_count > THRESHOLD * 2 {
                Priority::High
            } else {
                Priority::Medium
            },
        })
    } else {
        None
    }
}

/// Detect large classes/modules based on line count
pub fn detect_large_module(path: &Path, line_count: usize) -> Option<CodeSmell> {
    const THRESHOLD: usize = 300;

    if line_count > THRESHOLD {
        Some(CodeSmell {
            smell_type: SmellType::LargeClass,
            location: path.to_path_buf(),
            line: 1,
            message: format!("Module has {line_count} lines (threshold: {THRESHOLD})"),
            severity: if line_count > THRESHOLD * 2 {
                Priority::High
            } else {
                Priority::Medium
            },
        })
    } else {
        None
    }
}

/// Detect long methods/functions
pub fn detect_long_method(func: &FunctionMetrics) -> Option<CodeSmell> {
    const THRESHOLD: usize = 50;

    if func.length > THRESHOLD {
        Some(CodeSmell {
            smell_type: SmellType::LongMethod,
            location: func.file.clone(),
            line: func.line,
            message: format!(
                "Function '{}' has {} lines (threshold: {})",
                func.name, func.length, THRESHOLD
            ),
            severity: if func.length > THRESHOLD * 2 {
                Priority::High
            } else {
                Priority::Medium
            },
        })
    } else {
        None
    }
}

/// Detect deep nesting in functions
pub fn detect_deep_nesting(func: &FunctionMetrics) -> Option<CodeSmell> {
    const THRESHOLD: u32 = 4;

    if func.nesting > THRESHOLD {
        Some(CodeSmell {
            smell_type: SmellType::DeepNesting,
            location: func.file.clone(),
            line: func.line,
            message: format!(
                "Function '{}' has nesting depth of {} (threshold: {})",
                func.name, func.nesting, THRESHOLD
            ),
            severity: if func.nesting > THRESHOLD * 2 {
                Priority::High
            } else {
                Priority::Medium
            },
        })
    } else {
        None
    }
}

/// Analyze a function for all code smells
pub fn analyze_function_smells(func: &FunctionMetrics, param_count: usize) -> Vec<CodeSmell> {
    let mut smells = Vec::new();

    if let Some(smell) = detect_long_parameter_list(func, param_count) {
        smells.push(smell);
    }

    if let Some(smell) = detect_long_method(func) {
        smells.push(smell);
    }

    if let Some(smell) = detect_deep_nesting(func) {
        smells.push(smell);
    }

    smells
}

/// Analyze a file for module-level code smells
pub fn analyze_module_smells(path: &Path, line_count: usize) -> Vec<CodeSmell> {
    let mut smells = Vec::new();

    if let Some(smell) = detect_large_module(path, line_count) {
        smells.push(smell);
    }

    smells
}

/// Detect data clumps - groups of parameters that often appear together
pub fn detect_data_clumps(functions: &[FunctionMetrics]) -> Vec<CodeSmell> {
    let mut smells = Vec::new();

    // This is a simplified implementation
    // In a real implementation, we'd analyze actual parameter names and types
    for i in 0..functions.len() {
        for j in i + 1..functions.len() {
            // If two functions are in the same file and have similar high parameter counts,
            // they might have data clumps
            if functions[i].file == functions[j].file {
                // This is a placeholder - real implementation would compare actual parameters
                if functions[i].length > 30 && functions[j].length > 30 {
                    smells.push(CodeSmell {
                        smell_type: SmellType::DataClump,
                        location: functions[i].file.clone(),
                        line: functions[i].line,
                        message: format!(
                            "Functions '{}' and '{}' may share data clumps",
                            functions[i].name, functions[j].name
                        ),
                        severity: Priority::Low,
                    });
                    break; // Only report once per function
                }
            }
        }
    }

    smells
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::FunctionMetrics;
    use std::path::PathBuf;

    #[test]
    fn test_detect_data_clumps_empty_functions() {
        let functions = vec![];
        let smells = detect_data_clumps(&functions);
        assert_eq!(
            smells.len(),
            0,
            "No smells should be detected for empty input"
        );
    }

    #[test]
    fn test_detect_data_clumps_single_function() {
        let functions = vec![FunctionMetrics {
            name: "large_function".to_string(),
            file: PathBuf::from("src/lib.rs"),
            line: 10,
            cyclomatic: 5,
            cognitive: 10,
            nesting: 2,
            length: 35,
            is_test: false,
            visibility: None,
            is_trait_method: false,
            in_test_module: false,
            entropy_score: None,
            is_pure: None,
            purity_confidence: None,
            purity_reason: None,
            call_dependencies: None,
            detected_patterns: None,
            upstream_callers: None,
            downstream_callees: None,
            mapping_pattern_result: None,
            adjusted_complexity: None,
            composition_metrics: None,
            language_specific: None,
            purity_level: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
        }];
        let smells = detect_data_clumps(&functions);
        assert_eq!(
            smells.len(),
            0,
            "Single function cannot have data clumps with itself"
        );
    }

    #[test]
    fn test_detect_data_clumps_different_files() {
        let functions = vec![
            FunctionMetrics {
                name: "function_a".to_string(),
                file: PathBuf::from("src/module_a.rs"),
                line: 10,
                cyclomatic: 5,
                cognitive: 10,
                nesting: 2,
                length: 35,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
            FunctionMetrics {
                name: "function_b".to_string(),
                file: PathBuf::from("src/module_b.rs"),
                line: 20,
                cyclomatic: 5,
                cognitive: 10,
                nesting: 2,
                length: 35,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
        ];
        let smells = detect_data_clumps(&functions);
        assert_eq!(
            smells.len(),
            0,
            "Functions in different files should not be reported as data clumps"
        );
    }

    #[test]
    fn test_detect_data_clumps_same_file_large_functions() {
        let functions = vec![
            FunctionMetrics {
                name: "process_user_data".to_string(),
                file: PathBuf::from("src/user_handler.rs"),
                line: 10,
                cyclomatic: 8,
                cognitive: 15,
                nesting: 3,
                length: 40,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
            FunctionMetrics {
                name: "validate_user_data".to_string(),
                file: PathBuf::from("src/user_handler.rs"),
                line: 60,
                cyclomatic: 6,
                cognitive: 12,
                nesting: 2,
                length: 35,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
        ];
        let smells = detect_data_clumps(&functions);
        assert_eq!(
            smells.len(),
            1,
            "Should detect data clump for large functions in same file"
        );

        let smell = &smells[0];
        assert_eq!(smell.smell_type, SmellType::DataClump);
        assert_eq!(smell.location, PathBuf::from("src/user_handler.rs"));
        assert_eq!(smell.line, 10);
        assert!(smell.message.contains("process_user_data"));
        assert!(smell.message.contains("validate_user_data"));
        assert_eq!(smell.severity, Priority::Low);
    }

    #[test]
    fn test_detect_data_clumps_multiple_clumps() {
        let functions = vec![
            FunctionMetrics {
                name: "func_a".to_string(),
                file: PathBuf::from("src/module.rs"),
                line: 10,
                cyclomatic: 5,
                cognitive: 10,
                nesting: 2,
                length: 35,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
            FunctionMetrics {
                name: "func_b".to_string(),
                file: PathBuf::from("src/module.rs"),
                line: 50,
                cyclomatic: 5,
                cognitive: 10,
                nesting: 2,
                length: 32,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
            FunctionMetrics {
                name: "func_c".to_string(),
                file: PathBuf::from("src/module.rs"),
                line: 90,
                cyclomatic: 5,
                cognitive: 10,
                nesting: 2,
                length: 31,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
            FunctionMetrics {
                name: "small_func".to_string(),
                file: PathBuf::from("src/module.rs"),
                line: 130,
                cyclomatic: 2,
                cognitive: 3,
                nesting: 1,
                length: 10,
                is_test: false,
                visibility: None,
                is_trait_method: false,
                in_test_module: false,
                entropy_score: None,
                is_pure: None,
                purity_confidence: None,
                purity_reason: None,
                call_dependencies: None,
                detected_patterns: None,
                upstream_callers: None,
                downstream_callees: None,
                mapping_pattern_result: None,
                adjusted_complexity: None,
                composition_metrics: None,
                language_specific: None,
                purity_level: None,
                error_swallowing_count: None,
                error_swallowing_patterns: None,
                entropy_analysis: None,
            },
        ];
        let smells = detect_data_clumps(&functions);

        // Should detect clumps between func_a & func_b, func_a & func_c
        // But due to break after first match per function, we get 2 smells (one for func_a, one for func_b)
        assert_eq!(smells.len(), 2, "Should detect multiple data clumps");

        // First smell should be between func_a and func_b
        assert_eq!(smells[0].line, 10);
        assert!(smells[0].message.contains("func_a"));
        assert!(smells[0].message.contains("func_b"));

        // Second smell should be between func_b and func_c
        assert_eq!(smells[1].line, 50);
        assert!(smells[1].message.contains("func_b"));
        assert!(smells[1].message.contains("func_c"));
    }

    #[test]
    fn test_detect_long_parameter_list() {
        let func = FunctionMetrics {
            name: "test_func".to_string(),
            file: PathBuf::from("src/test.rs"),
            line: 10,
            cyclomatic: 5,
            cognitive: 10,
            nesting: 2,
            length: 20,
            is_test: false,
            visibility: None,
            is_trait_method: false,
            in_test_module: false,
            entropy_score: None,
            is_pure: None,
            purity_confidence: None,
            purity_reason: None,
            call_dependencies: None,
            detected_patterns: None,
            upstream_callers: None,
            downstream_callees: None,
            mapping_pattern_result: None,
            adjusted_complexity: None,
            composition_metrics: None,
            language_specific: None,
            purity_level: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
        };

        // Test with parameter count below threshold
        let smell = detect_long_parameter_list(&func, 3);
        assert!(smell.is_none(), "Should not detect smell for 3 parameters");

        // Test with parameter count at threshold
        let smell = detect_long_parameter_list(&func, 5);
        assert!(smell.is_none(), "Should not detect smell at threshold");

        // Test with parameter count above threshold
        let smell = detect_long_parameter_list(&func, 6);
        assert!(smell.is_some(), "Should detect smell for 6 parameters");
        let smell = smell.unwrap();
        assert_eq!(smell.smell_type, SmellType::LongParameterList);
        assert_eq!(smell.severity, Priority::Medium);
        assert!(smell.message.contains("6 parameters"));

        // Test with parameter count way above threshold (high severity)
        let smell = detect_long_parameter_list(&func, 12);
        assert!(smell.is_some(), "Should detect smell for 12 parameters");
        let smell = smell.unwrap();
        assert_eq!(smell.severity, Priority::High);
    }

    #[test]
    fn test_detect_large_module() {
        let path = PathBuf::from("src/large_module.rs");

        // Test with line count below threshold
        let smell = detect_large_module(&path, 250);
        assert!(smell.is_none(), "Should not detect smell for 250 lines");

        // Test with line count at threshold
        let smell = detect_large_module(&path, 300);
        assert!(smell.is_none(), "Should not detect smell at threshold");

        // Test with line count above threshold
        let smell = detect_large_module(&path, 350);
        assert!(smell.is_some(), "Should detect smell for 350 lines");
        let smell = smell.unwrap();
        assert_eq!(smell.smell_type, SmellType::LargeClass);
        assert_eq!(smell.severity, Priority::Medium);
        assert!(smell.message.contains("350 lines"));

        // Test with line count way above threshold (high severity)
        let smell = detect_large_module(&path, 700);
        assert!(smell.is_some(), "Should detect smell for 700 lines");
        let smell = smell.unwrap();
        assert_eq!(smell.severity, Priority::High);
    }

    #[test]
    fn test_detect_long_method() {
        let func = FunctionMetrics {
            name: "long_func".to_string(),
            file: PathBuf::from("src/test.rs"),
            line: 10,
            cyclomatic: 5,
            cognitive: 10,
            nesting: 2,
            length: 40,
            is_test: false,
            visibility: None,
            is_trait_method: false,
            in_test_module: false,
            entropy_score: None,
            is_pure: None,
            purity_confidence: None,
            purity_reason: None,
            call_dependencies: None,
            detected_patterns: None,
            upstream_callers: None,
            downstream_callees: None,
            mapping_pattern_result: None,
            adjusted_complexity: None,
            composition_metrics: None,
            language_specific: None,
            purity_level: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
        };

        // Test with length below threshold
        let smell = detect_long_method(&func);
        assert!(smell.is_none(), "Should not detect smell for 40 lines");

        // Test with length above threshold
        let mut long_func = func.clone();
        long_func.length = 60;
        let smell = detect_long_method(&long_func);
        assert!(smell.is_some(), "Should detect smell for 60 lines");
        let smell = smell.unwrap();
        assert_eq!(smell.smell_type, SmellType::LongMethod);
        assert_eq!(smell.severity, Priority::Medium);
        assert!(smell.message.contains("60 lines"));

        // Test with length way above threshold (high severity)
        long_func.length = 120;
        let smell = detect_long_method(&long_func);
        assert!(smell.is_some(), "Should detect smell for 120 lines");
        let smell = smell.unwrap();
        assert_eq!(smell.severity, Priority::High);
    }

    #[test]
    fn test_detect_deep_nesting() {
        let func = FunctionMetrics {
            name: "nested_func".to_string(),
            file: PathBuf::from("src/test.rs"),
            line: 10,
            cyclomatic: 5,
            cognitive: 10,
            nesting: 3,
            length: 30,
            is_test: false,
            visibility: None,
            is_trait_method: false,
            in_test_module: false,
            entropy_score: None,
            is_pure: None,
            purity_confidence: None,
            purity_reason: None,
            call_dependencies: None,
            detected_patterns: None,
            upstream_callers: None,
            downstream_callees: None,
            mapping_pattern_result: None,
            adjusted_complexity: None,
            composition_metrics: None,
            language_specific: None,
            purity_level: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
        };

        // Test with nesting below threshold
        let smell = detect_deep_nesting(&func);
        assert!(
            smell.is_none(),
            "Should not detect smell for nesting depth 3"
        );

        // Test with nesting at threshold
        let mut nested_func = func.clone();
        nested_func.nesting = 4;
        let smell = detect_deep_nesting(&nested_func);
        assert!(smell.is_none(), "Should not detect smell at threshold");

        // Test with nesting above threshold
        nested_func.nesting = 5;
        let smell = detect_deep_nesting(&nested_func);
        assert!(smell.is_some(), "Should detect smell for nesting depth 5");
        let smell = smell.unwrap();
        assert_eq!(smell.smell_type, SmellType::DeepNesting);
        assert_eq!(smell.severity, Priority::Medium);
        assert!(smell.message.contains("nesting depth of 5"));

        // Test with nesting way above threshold (high severity)
        nested_func.nesting = 10;
        let smell = detect_deep_nesting(&nested_func);
        assert!(smell.is_some(), "Should detect smell for nesting depth 10");
        let smell = smell.unwrap();
        assert_eq!(smell.severity, Priority::High);
    }

    #[test]
    fn test_analyze_function_smells() {
        let func = FunctionMetrics {
            name: "complex_func".to_string(),
            file: PathBuf::from("src/test.rs"),
            line: 10,
            cyclomatic: 5,
            cognitive: 10,
            nesting: 5,
            length: 60,
            is_test: false,
            visibility: None,
            is_trait_method: false,
            in_test_module: false,
            entropy_score: None,
            is_pure: None,
            purity_confidence: None,
            purity_reason: None,
            call_dependencies: None,
            detected_patterns: None,
            upstream_callers: None,
            downstream_callees: None,
            mapping_pattern_result: None,
            adjusted_complexity: None,
            composition_metrics: None,
            language_specific: None,
            purity_level: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
        };

        // Test function with multiple smells
        let smells = analyze_function_smells(&func, 7);
        assert_eq!(smells.len(), 3, "Should detect 3 smells");

        // Verify each smell type is detected
        let smell_types: Vec<SmellType> = smells.iter().map(|s| s.smell_type.clone()).collect();
        assert!(smell_types.contains(&SmellType::LongParameterList));
        assert!(smell_types.contains(&SmellType::LongMethod));
        assert!(smell_types.contains(&SmellType::DeepNesting));

        // Test function with no smells
        let clean_func = FunctionMetrics {
            name: "clean_func".to_string(),
            file: PathBuf::from("src/test.rs"),
            line: 10,
            cyclomatic: 3,
            cognitive: 5,
            nesting: 2,
            length: 20,
            is_test: false,
            visibility: None,
            is_trait_method: false,
            in_test_module: false,
            entropy_score: None,
            is_pure: None,
            purity_confidence: None,
            purity_reason: None,
            call_dependencies: None,
            detected_patterns: None,
            upstream_callers: None,
            downstream_callees: None,
            mapping_pattern_result: None,
            adjusted_complexity: None,
            composition_metrics: None,
            language_specific: None,
            purity_level: None,
            error_swallowing_count: None,
            error_swallowing_patterns: None,
            entropy_analysis: None,
        };

        let smells = analyze_function_smells(&clean_func, 3);
        assert_eq!(smells.len(), 0, "Clean function should have no smells");
    }

    #[test]
    fn test_analyze_module_smells() {
        let path = PathBuf::from("src/module.rs");

        // Test small module with no smells
        let smells = analyze_module_smells(&path, 200);
        assert_eq!(smells.len(), 0, "Small module should have no smells");

        // Test large module
        let smells = analyze_module_smells(&path, 400);
        assert_eq!(smells.len(), 1, "Large module should have 1 smell");
        assert_eq!(smells[0].smell_type, SmellType::LargeClass);

        // Test edge case with exactly threshold
        let smells = analyze_module_smells(&path, 300);
        assert_eq!(smells.len(), 0, "Module at threshold should have no smells");
    }

    #[test]
    fn test_code_smell_to_debt_item() {
        let smell = CodeSmell {
            smell_type: SmellType::LongMethod,
            location: PathBuf::from("src/test.rs"),
            line: 42,
            message: "Test message".to_string(),
            severity: Priority::High,
        };

        let debt_item = smell.to_debt_item();
        assert!(matches!(debt_item.debt_type, DebtType::CodeSmell { .. }));
        assert_eq!(debt_item.file, PathBuf::from("src/test.rs"));
        assert_eq!(debt_item.line, 42);
        assert_eq!(debt_item.message, "Test message");
        assert_eq!(debt_item.priority, Priority::High);
    }
}