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

    #[test]
    fn test_format_sarif_with_violations() {
        let mut result = create_full_test_result();
        result.quality_gate.violations = vec![
            QualityViolation {
                rule: "max_defect_density".to_string(),
                threshold: 0.05,
                actual: 0.10,
                severity: "blocking".to_string(),
            },
            QualityViolation {
                rule: "max_single_file_violations".to_string(),
                threshold: 50.0,
                actual: 60.0,
                severity: "warning".to_string(),
            },
        ];

        let output = format_sarif(&result).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&output).unwrap();

        // #701: this used to assert `results.len() == 2`, i.e. that SARIF
        // carried only the two quality-gate breaches. That is the behaviour
        // format_sarif was fixed *away* from: building the document out of
        // gate breaches alone made a passing gate emit `"results": []` while
        // `-f detailed` listed the located clippy findings for the same run.
        // The located findings come first, the gate breaches ride along after
        // them. The fragment was never compiled, so it kept asserting the old
        // shape unchallenged.
        let results = parsed["runs"][0]["results"].as_array().unwrap();
        assert_eq!(results.len(), result.all_violations.len() + 2);

        // Located findings first, each with a region.
        assert_eq!(results[0]["ruleId"], "unused_variable");
        assert!(results[0]["locations"][0]["physicalLocation"]["region"].is_object());

        // Then the gate breaches: blocking maps to error, everything else to warning.
        let gate = &results[result.all_violations.len()..];
        assert_eq!(gate[0]["level"], "error");
        assert_eq!(gate[0]["ruleId"], "max_defect_density");
        assert_eq!(gate[1]["level"], "warning");
        assert_eq!(gate[1]["ruleId"], "max_single_file_violations");
    }

    // LintHotspotResult Tests

    #[test]
    fn test_lint_hotspot_result_serialization() {
        let result = create_full_test_result();
        let json = serde_json::to_string(&result).unwrap();
        let deserialized: LintHotspotResult = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.hotspot.file, result.hotspot.file);
        assert_eq!(
            deserialized.total_project_violations,
            result.total_project_violations
        );
        assert_eq!(
            deserialized.quality_gate.passed,
            result.quality_gate.passed
        );
    }

    #[test]
    fn test_lint_hotspot_result_with_all_fields() {
        let mut result = create_full_test_result();
        result.enforcement = Some(EnforcementMetadata {
            enforcement_score: 8.5,
            requires_enforcement: true,
            estimated_fix_time: 1800,
            automation_confidence: 0.85,
            enforcement_priority: 3,
        });
        result.refactor_chain = Some(RefactorChain {
            id: "test".to_string(),
            estimated_reduction: 10,
            automation_confidence: 0.9,
            steps: vec![],
        });

        let json = serde_json::to_string(&result).unwrap();
        let deserialized: LintHotspotResult = serde_json::from_str(&json).unwrap();

        assert!(deserialized.enforcement.is_some());
        assert!(deserialized.refactor_chain.is_some());
    }

    // recalculate_hotspot_metrics Tests

    #[test]
    fn test_recalculate_hotspot_metrics() {
        let mut result = create_full_test_result();
        result.hotspot.detailed_violations = vec![
            create_test_violation("src/main.rs", 10, "lint_a", "warning"),
            create_test_violation("src/main.rs", 20, "lint_b", "warning"),
        ];
        result.hotspot.sloc = 100;

        recalculate_hotspot_metrics(&mut result);

        assert_eq!(result.hotspot.total_violations, 2);
        assert!((result.hotspot.defect_density - 0.02).abs() < f64::EPSILON);
    }

    #[test]
    fn test_recalculate_hotspot_metrics_zero_sloc() {
        let mut result = create_full_test_result();
        result.hotspot.detailed_violations = vec![
            create_test_violation("src/main.rs", 10, "lint_a", "warning"),
        ];
        result.hotspot.sloc = 0;

        recalculate_hotspot_metrics(&mut result);

        assert_eq!(result.hotspot.total_violations, 1);
        // defect_density should not change when sloc is 0
    }

    // should_exit_with_error Tests (comprehensive)

    #[test]
    fn test_should_exit_with_error_comprehensive() {
        let mut result = create_full_test_result();
        let params = LintHotspotParams {
            project_path: PathBuf::from("/test"),
            file: None,
            format: LintHotspotOutputFormat::Summary,
            max_density: 5.0,
            min_confidence: 0.7,
            enforce: false,
            dry_run: false,
            enforcement_metadata: false,
            output: None,
            perf: false,
            clippy_flags: String::new(),
            top_files: 10,
            include: vec![],
            exclude: vec![],
        };

        // Quality gate passed, no enforce
        result.quality_gate.passed = true;
        result.total_project_violations = 10;
        assert!(!should_exit_with_error(&result, &params));

        // Quality gate failed
        result.quality_gate.passed = false;
        assert!(should_exit_with_error(&result, &params));
    }

    // log_analysis_start Tests

    #[test]
    fn test_log_analysis_start_non_json() {
        // This test verifies the function doesn't panic for non-JSON formats
        log_analysis_start(&LintHotspotOutputFormat::Summary);
        log_analysis_start(&LintHotspotOutputFormat::Detailed);
        log_analysis_start(&LintHotspotOutputFormat::Sarif);
    }

    #[test]
    fn test_log_analysis_start_json() {
        // This test verifies the function doesn't panic for JSON format
        log_analysis_start(&LintHotspotOutputFormat::Json);
    }

    // log_single_file_mode Tests

    #[test]
    fn test_log_single_file_mode_non_json() {
        let file_path = PathBuf::from("src/main.rs");
        log_single_file_mode(&file_path, &LintHotspotOutputFormat::Summary);
        log_single_file_mode(&file_path, &LintHotspotOutputFormat::Detailed);
    }

    #[test]
    fn test_log_single_file_mode_json() {
        let file_path = PathBuf::from("src/main.rs");
        log_single_file_mode(&file_path, &LintHotspotOutputFormat::Json);
    }

    // generate_enforcement_metadata_if_needed Tests

    #[test]
    fn test_generate_enforcement_metadata_when_requested() {
        let hotspot = create_test_hotspot("src/main.rs", 10, 100, 5, 5);
        let params = LintHotspotParams {
            project_path: PathBuf::from("/test"),
            file: None,
            format: LintHotspotOutputFormat::Summary,
            max_density: 5.0,
            min_confidence: 0.7,
            enforce: false,
            dry_run: false,
            enforcement_metadata: true, // Explicitly requested
            output: None,
            perf: false,
            clippy_flags: String::new(),
            top_files: 10,
            include: vec![],
            exclude: vec![],
        };

        let metadata = generate_enforcement_metadata_if_needed(&hotspot, &params);
        assert!(metadata.is_some());
    }

    #[test]
    fn test_generate_enforcement_metadata_when_enforce() {
        let hotspot = create_test_hotspot("src/main.rs", 10, 100, 5, 5);
        let params = LintHotspotParams {
            project_path: PathBuf::from("/test"),
            file: None,
            format: LintHotspotOutputFormat::Summary,
            max_density: 5.0,
            min_confidence: 0.7,
            enforce: true, // Enforce flag set
            dry_run: false,
            enforcement_metadata: false,
            output: None,
            perf: false,
            clippy_flags: String::new(),
            top_files: 10,
            include: vec![],
            exclude: vec![],
        };

        let metadata = generate_enforcement_metadata_if_needed(&hotspot, &params);
        assert!(metadata.is_some());
    }

    #[test]
    fn test_generate_enforcement_metadata_not_requested() {
        let hotspot = create_test_hotspot("src/main.rs", 10, 100, 5, 5);
        let params = LintHotspotParams {
            project_path: PathBuf::from("/test"),
            file: None,
            format: LintHotspotOutputFormat::Summary,
            max_density: 5.0,
            min_confidence: 0.7,
            enforce: false,
            dry_run: false,
            enforcement_metadata: false,
            output: None,
            perf: false,
            clippy_flags: String::new(),
            top_files: 10,
            include: vec![],
            exclude: vec![],
        };

        let metadata = generate_enforcement_metadata_if_needed(&hotspot, &params);
        assert!(metadata.is_none());
    }

    // generate_refactor_chain_if_needed Tests

    #[test]
    fn test_generate_refactor_chain_when_enforce() {
        let hotspot = create_test_hotspot("src/main.rs", 10, 100, 5, 5);
        let params = LintHotspotParams {
            project_path: PathBuf::from("/test"),
            file: None,
            format: LintHotspotOutputFormat::Summary,
            max_density: 5.0,
            min_confidence: 0.7,
            enforce: true,
            dry_run: false,
            enforcement_metadata: false,
            output: None,
            perf: false,
            clippy_flags: String::new(),
            top_files: 10,
            include: vec![],
            exclude: vec![],
        };

        let chain = generate_refactor_chain_if_needed(&hotspot, &params, &None);
        assert!(chain.is_some());
    }

    #[test]
    fn test_generate_refactor_chain_when_enforcement_required() {
        let hotspot = create_test_hotspot("src/main.rs", 10, 100, 5, 5);
        let params = LintHotspotParams {
            project_path: PathBuf::from("/test"),
            file: None,
            format: LintHotspotOutputFormat::Summary,
            max_density: 5.0,
            min_confidence: 0.7,
            enforce: false,
            dry_run: false,
            enforcement_metadata: false,
            output: None,
            perf: false,
            clippy_flags: String::new(),
            top_files: 10,
            include: vec![],
            exclude: vec![],
        };

        let enforcement = Some(EnforcementMetadata {
            enforcement_score: 8.0,
            requires_enforcement: true,
            estimated_fix_time: 1800,
            automation_confidence: 0.85,
            enforcement_priority: 3,
        });

        let chain = generate_refactor_chain_if_needed(&hotspot, &params, &enforcement);
        assert!(chain.is_some());
    }

    #[test]
    fn test_generate_refactor_chain_not_needed() {
        let hotspot = create_test_hotspot("src/main.rs", 10, 100, 5, 5);
        let params = LintHotspotParams {
            project_path: PathBuf::from("/test"),
            file: None,
            format: LintHotspotOutputFormat::Summary,
            max_density: 5.0,
            min_confidence: 0.7,
            enforce: false,
            dry_run: false,
            enforcement_metadata: false,
            output: None,
            perf: false,
            clippy_flags: String::new(),
            top_files: 10,
            include: vec![],
            exclude: vec![],
        };

        let chain = generate_refactor_chain_if_needed(&hotspot, &params, &None);
        assert!(chain.is_none());
    }

    // Property-based Tests

    mod property_tests {
        use super::*;
        use proptest::prelude::*;

        proptest! {
            #[test]
            fn test_defect_density_never_negative(violations in 0usize..1000, sloc in 1usize..10000) {
                let density = calculate_defect_density(violations, sloc);
                prop_assert!(density >= 0.0);
            }

            #[test]
            fn test_total_violations_equals_sum(
                errors in 0usize..100,
                warnings in 0usize..100,
                suggestions in 0usize..100
            ) {
                let metrics = FileMetrics {
                    violations: HashMap::new(),
                    severity_counts: SeverityDistribution {
                        error: errors,
                        warning: warnings,
                        suggestion: suggestions,
                        note: 0,
                    },
                    sloc: 100,
                    detailed_violations: vec![],
                };

                let total = calculate_total_violations(&metrics);
                prop_assert_eq!(total, errors + warnings + suggestions);
            }

            #[test]
            // #701: this used to assert `sloc >= 0` on a `usize`, which is
            // true for every possible value and so measured nothing (clippy
            // flags it as a useless comparison). It now bounds sloc by the
            // only thing it can be: the number of lines it was counted from.
            fn test_count_sloc_bounded_by_line_count(content in ".*") {
                let sloc = count_sloc(&content);
                prop_assert!(sloc <= content.lines().count());
            }

            #[test]
            fn test_enforcement_score_bounded(violations in 1usize..100, sloc in 1usize..1000) {
                let mut hotspot = create_test_hotspot("test.rs", violations, sloc, violations / 2, violations - violations / 2);
                hotspot.defect_density = violations as f64 / sloc as f64;

                let metadata = calculate_enforcement_metadata(&hotspot, 0.7);
                prop_assert!(metadata.enforcement_score >= 0.0);
                prop_assert!(metadata.enforcement_score <= 10.0);
            }

            #[test]
            fn test_quality_gate_consistency(density in 0.0f64..10.0, threshold in 0.1f64..10.0) {
                let violations = (density * 100.0) as usize;
                let hotspot = create_test_hotspot("test.rs", violations, 100, violations / 2, violations - violations / 2);

                let status = check_quality_gates(&hotspot, threshold);

                // If density exceeds threshold, gate should fail
                if hotspot.defect_density > threshold {
                    prop_assert!(!status.passed || status.violations.iter().any(|v| v.rule == "max_defect_density"));
                }
            }
        }
    }