pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
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
#![cfg_attr(coverage_nightly, coverage(off))]

#[cfg(test)]
mod tests_part2 {
    use crate::cli::handlers::comprehensive_analysis_handler::output::{
        format_complexity_section, format_dead_code_section, format_satd_section,
    };
    use crate::cli::handlers::comprehensive_analysis_handler::types::ComprehensiveAnalysisConfig;
    use crate::cli::ComprehensiveOutputFormat;
    use crate::services::facades::complexity_facade::{
        ComplexityAnalysisResult, ComplexityViolation,
    };
    use crate::services::facades::dead_code_facade::{
        DeadCodeAnalysisResult, DeadCodeItem, DeadCodeType,
    };
    use crate::services::facades::satd_facade::{SatdAnalysisResult, SatdSeverity, SatdViolation};
    use std::path::PathBuf;

    fn make_default_config() -> ComprehensiveAnalysisConfig {
        ComprehensiveAnalysisConfig {
            project_path: PathBuf::from("/test/project"),
            file: None,
            files: Vec::new(),
            format: ComprehensiveOutputFormat::Json,
            include_duplicates: false,
            include_dead_code: true,
            include_defects: false,
            include_complexity: true,
            include_tdg: false,
            confidence_threshold: 0.7,
            min_lines: 50,
            include: None,
            exclude: None,
            output: None,
            perf: false,
            executive_summary: true,
            top_files: 10,
        }
    }

    #[test]
    fn test_format_complexity_section() {
        let complexity = ComplexityAnalysisResult {
            total_files: 25,
            violations: vec![ComplexityViolation {
                file_path: "src/main.rs".to_string(),
                function_name: "complex_fn".to_string(),
                line_number: 10,
                complexity: 30,
                complexity_type: "cyclomatic".to_string(),
            }],
            average_complexity: 12.5,
            max_complexity: 30,
            summary: "Test summary".to_string(),
        };
        let mut output = String::new();
        format_complexity_section(&mut output, &complexity, 5).unwrap();
        assert!(output.contains("## Complexity Analysis"));
        assert!(output.contains("**Files Analyzed**: 25"));
        assert!(output.contains("**Average Complexity**: 12.5"));
        assert!(output.contains("**Max Complexity**: 30"));
        assert!(output.contains("**Violations**: 1"));
        assert!(output.contains("### Top Complexity Violations"));
        assert!(output.contains("complex_fn"));
    }

    #[test]
    fn test_format_complexity_section_no_violations() {
        let complexity = ComplexityAnalysisResult {
            total_files: 10,
            violations: vec![],
            average_complexity: 5.0,
            max_complexity: 10,
            summary: "Clean".to_string(),
        };
        let mut output = String::new();
        format_complexity_section(&mut output, &complexity, 5).unwrap();
        assert!(output.contains("**Violations**: 0"));
        assert!(!output.contains("### Top Complexity Violations"));
    }

    #[test]
    fn test_format_dead_code_section() {
        let dead_code = DeadCodeAnalysisResult {
            total_files: 15,
            dead_items: vec![DeadCodeItem {
                file_path: "src/old.rs".to_string(),
                item_name: "old_function".to_string(),
                item_type: DeadCodeType::Function,
                line_number: 50,
                reason: "Never referenced".to_string(),
            }],
            dead_percentage: 3.5,
            summary: "Found dead code".to_string(),
        };
        let mut output = String::new();
        format_dead_code_section(&mut output, &dead_code, 5).unwrap();
        assert!(output.contains("## Dead Code Analysis"));
        assert!(output.contains("**Files Analyzed**: 15"));
        assert!(output.contains("**Dead Items**: 1"));
        assert!(output.contains("**Dead Code %**: 3.5%"));
        assert!(output.contains("old_function"));
    }

    #[test]
    fn test_format_dead_code_section_empty() {
        let dead_code = DeadCodeAnalysisResult {
            total_files: 10,
            dead_items: vec![],
            dead_percentage: 0.0,
            summary: "No dead code".to_string(),
        };
        let mut output = String::new();
        format_dead_code_section(&mut output, &dead_code, 5).unwrap();
        assert!(output.contains("**Dead Items**: 0"));
        assert!(!output.contains("### Dead Code Items"));
    }

    #[test]
    fn test_format_satd_section() {
        let satd = SatdAnalysisResult {
            total_files: 8,
            violations: vec![SatdViolation {
                file_path: "src/hack.rs".to_string(),
                line_number: 15,
                violation_type: "HACK".to_string(),
                message: "Temporary workaround".to_string(),
                severity: SatdSeverity::High,
            }],
            summary: "Found SATD".to_string(),
        };
        let mut output = String::new();
        format_satd_section(&mut output, &satd, 5).unwrap();
        assert!(output.contains("## Technical Debt (SATD) Analysis"));
        assert!(output.contains("**Files Analyzed**: 8"));
        assert!(output.contains("**Violations**: 1"));
        assert!(output.contains("HACK"));
        assert!(output.contains("High"));
    }

    #[test]
    fn test_format_satd_section_empty() {
        let satd = SatdAnalysisResult {
            total_files: 5,
            violations: vec![],
            summary: "No SATD".to_string(),
        };
        let mut output = String::new();
        format_satd_section(&mut output, &satd, 5).unwrap();
        assert!(output.contains("**Violations**: 0"));
        assert!(!output.contains("### SATD Violations"));
    }

    #[test]
    fn test_format_complexity_section_limits_to_five() {
        let violations: Vec<ComplexityViolation> = (0..10)
            .map(|i| ComplexityViolation {
                file_path: format!("src/file{i}.rs"),
                function_name: format!("function{i}"),
                line_number: i,
                complexity: 25 + i as u32,
                complexity_type: "cyclomatic".to_string(),
            })
            .collect();
        let complexity = ComplexityAnalysisResult {
            total_files: 10,
            violations,
            average_complexity: 30.0,
            max_complexity: 34,
            summary: "Many violations".to_string(),
        };
        let mut output = String::new();
        format_complexity_section(&mut output, &complexity, 5).unwrap();
        assert!(output.contains("5. "));
        assert!(!output.contains("6. "));
    }

    #[test]
    fn test_format_dead_code_section_limits_to_five() {
        let dead_items: Vec<DeadCodeItem> = (0..10)
            .map(|i| DeadCodeItem {
                file_path: format!("src/file{i}.rs"),
                item_name: format!("item{i}"),
                item_type: DeadCodeType::Function,
                line_number: i,
                reason: "Unused".to_string(),
            })
            .collect();
        let dead_code = DeadCodeAnalysisResult {
            total_files: 10,
            dead_items,
            dead_percentage: 10.0,
            summary: "Many dead items".to_string(),
        };
        let mut output = String::new();
        format_dead_code_section(&mut output, &dead_code, 5).unwrap();
        assert!(output.contains("5. "));
        assert!(!output.contains("6. "));
    }

    #[test]
    fn test_format_satd_section_limits_to_five() {
        let violations: Vec<SatdViolation> = (0..10)
            .map(|i| SatdViolation {
                file_path: format!("src/file{i}.rs"),
                line_number: i,
                violation_type: "TODO".to_string(),
                message: format!("Message {i}"),
                severity: SatdSeverity::Low,
            })
            .collect();
        let satd = SatdAnalysisResult {
            total_files: 10,
            violations,
            summary: "Many SATD items".to_string(),
        };
        let mut output = String::new();
        format_satd_section(&mut output, &satd, 5).unwrap();
        assert!(output.contains("5. "));
        assert!(!output.contains("6. "));
    }

    /// The three sections above were `.iter().take(5)` with the 5 written into
    /// the source, so `analyze comprehensive --top-files 1` and
    /// `--top-files 50` produced byte-identical reports (md5
    /// c1b7cf8d353b4a8f…) over a corpus with 87 complexity violations, 45 dead
    /// items and 63 SATD violations. The row count is the flag.
    #[test]
    fn every_section_row_count_follows_top_files() {
        let complexity = ComplexityAnalysisResult {
            total_files: 10,
            violations: (0..10)
                .map(|i| ComplexityViolation {
                    file_path: format!("src/file{i}.rs"),
                    function_name: format!("function{i}"),
                    line_number: i,
                    complexity: 25 + i as u32,
                    complexity_type: "cyclomatic".to_string(),
                })
                .collect(),
            average_complexity: 30.0,
            max_complexity: 34,
            summary: "Many violations".to_string(),
        };
        let dead_code = DeadCodeAnalysisResult {
            total_files: 10,
            dead_items: (0..10)
                .map(|i| DeadCodeItem {
                    file_path: format!("src/file{i}.rs"),
                    item_name: format!("item{i}"),
                    item_type: DeadCodeType::Function,
                    line_number: i,
                    reason: "Unused".to_string(),
                })
                .collect(),
            dead_percentage: 10.0,
            summary: "Many dead items".to_string(),
        };
        let satd = SatdAnalysisResult {
            total_files: 10,
            violations: (0..10)
                .map(|i| SatdViolation {
                    file_path: format!("src/file{i}.rs"),
                    line_number: i,
                    violation_type: "TODO".to_string(),
                    message: format!("Message {i}"),
                    severity: SatdSeverity::Low,
                })
                .collect(),
            summary: "Many SATD items".to_string(),
        };

        let numbered = |s: &str| {
            s.lines()
                .filter(|l| {
                    l.trim_start()
                        .split('.')
                        .next()
                        .is_some_and(|h| !h.is_empty() && h.chars().all(|c| c.is_ascii_digit()))
                })
                .count()
        };

        for (limit, expected) in [(1usize, 1usize), (3, 3), (50, 10), (0, 10)] {
            let mut c = String::new();
            format_complexity_section(&mut c, &complexity, limit).unwrap();
            assert_eq!(
                numbered(&c),
                expected,
                "complexity --top-files {limit}:\n{c}"
            );

            let mut d = String::new();
            format_dead_code_section(&mut d, &dead_code, limit).unwrap();
            assert_eq!(
                numbered(&d),
                expected,
                "dead code --top-files {limit}:\n{d}"
            );

            let mut s = String::new();
            format_satd_section(&mut s, &satd, limit).unwrap();
            assert_eq!(numbered(&s), expected, "satd --top-files {limit}:\n{s}");
        }
    }

    #[test]
    fn test_dead_code_type_variants_in_output() {
        let dead_items = vec![
            DeadCodeItem {
                file_path: "test.rs".to_string(),
                item_name: "unused_fn".to_string(),
                item_type: DeadCodeType::Function,
                line_number: 1,
                reason: "test".to_string(),
            },
            DeadCodeItem {
                file_path: "test.rs".to_string(),
                item_name: "UnusedClass".to_string(),
                item_type: DeadCodeType::Class,
                line_number: 10,
                reason: "test".to_string(),
            },
            DeadCodeItem {
                file_path: "test.rs".to_string(),
                item_name: "unused_var".to_string(),
                item_type: DeadCodeType::Variable,
                line_number: 20,
                reason: "test".to_string(),
            },
            DeadCodeItem {
                file_path: "test.rs".to_string(),
                item_name: "unused_import".to_string(),
                item_type: DeadCodeType::Import,
                line_number: 30,
                reason: "test".to_string(),
            },
            DeadCodeItem {
                file_path: "test.rs".to_string(),
                item_name: "unreachable".to_string(),
                item_type: DeadCodeType::UnreachableCode,
                line_number: 40,
                reason: "test".to_string(),
            },
        ];
        let dead_code = DeadCodeAnalysisResult {
            total_files: 1,
            dead_items,
            dead_percentage: 5.0,
            summary: "Test".to_string(),
        };
        let mut output = String::new();
        format_dead_code_section(&mut output, &dead_code, 5).unwrap();
        assert!(output.contains("Function"));
        assert!(output.contains("Class"));
        assert!(output.contains("Variable"));
        assert!(output.contains("Import"));
        assert!(output.contains("UnreachableCode"));
    }

    #[test]
    fn test_satd_severity_variants_in_output() {
        let violations = vec![
            SatdViolation {
                file_path: "test.rs".to_string(),
                line_number: 1,
                violation_type: "FIXME".to_string(),
                message: "Critical".to_string(),
                severity: SatdSeverity::Critical,
            },
            SatdViolation {
                file_path: "test.rs".to_string(),
                line_number: 2,
                violation_type: "TODO".to_string(),
                message: "High".to_string(),
                severity: SatdSeverity::High,
            },
            SatdViolation {
                file_path: "test.rs".to_string(),
                line_number: 3,
                violation_type: "NOTE".to_string(),
                message: "Medium".to_string(),
                severity: SatdSeverity::Medium,
            },
            SatdViolation {
                file_path: "test.rs".to_string(),
                line_number: 4,
                violation_type: "XXX".to_string(),
                message: "Low".to_string(),
                severity: SatdSeverity::Low,
            },
        ];
        let satd = SatdAnalysisResult {
            total_files: 1,
            violations,
            summary: "Test".to_string(),
        };
        let mut output = String::new();
        format_satd_section(&mut output, &satd, 5).unwrap();
        assert!(output.contains("Critical"));
        assert!(output.contains("High"));
        assert!(output.contains("Medium"));
        assert!(output.contains("Low"));
    }

    #[test]
    fn test_create_default_config_fields() {
        let config = make_default_config();
        assert_eq!(config.project_path.to_str().unwrap(), "/test/project");
        assert!(config.include_dead_code);
        assert!(config.include_complexity);
        assert!(!config.include_duplicates);
        assert!(!config.include_defects);
        assert!(!config.include_tdg);
        assert_eq!(config.top_files, 10);
    }
}