pmat 3.16.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
    #[test]
    fn test_get_ngrams() {
        let ngrams = get_ngrams("hello", 2);
        assert!(ngrams.contains("he"));
        assert!(ngrams.contains("el"));
        assert!(ngrams.contains("ll"));
        assert!(ngrams.contains("lo"));
        assert_eq!(ngrams.len(), 4);

        // Test with string shorter than n
        let short_ngrams = get_ngrams("hi", 3);
        assert_eq!(short_ngrams.len(), 1);
        assert!(short_ngrams.contains("hi"));
    }

    #[test]
    fn test_soundex_code() {
        assert_eq!(soundex_code('B'), '1');
        assert_eq!(soundex_code('C'), '2');
        assert_eq!(soundex_code('D'), '3');
        assert_eq!(soundex_code('L'), '4');
        assert_eq!(soundex_code('M'), '5');
        assert_eq!(soundex_code('R'), '6');
        assert_eq!(soundex_code('A'), '0');
        assert_eq!(soundex_code('E'), '0');
    }

    #[test]
    fn test_format_quality_gate_output_json() {
        let results = QualityGateResults {
            passed: false,
            total_violations: 10,
            complexity_violations: 3,
            dead_code_violations: 2,
            satd_violations: 1,
            entropy_violations: 1,
            security_violations: 2,
            duplicate_violations: 1,
            coverage_violations: 0,
            section_violations: 0,
            provability_violations: 0,
            provability_score: Some(85.5),
            violations: Vec::new(),
        };

        let violations = vec![
            QualityViolation {
                check_type: "complexity".to_string(),
                severity: "error".to_string(),
                message: "Function exceeds complexity threshold".to_string(),
                file: "src/main.rs".to_string(),
                line: Some(42),
            },
            QualityViolation {
                check_type: "dead_code".to_string(),
                severity: "warning".to_string(),
                message: "Unused function detected".to_string(),
                file: "src/utils.rs".to_string(),
                line: Some(100),
            },
        ];

        let output =
            format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Json);
        assert!(output.is_ok());

        let json = output.unwrap();
        assert!(json.contains("\"passed\": false"));
        assert!(json.contains("\"total_violations\": 10"));
        assert!(json.contains("\"complexity_violations\": 3"));
        assert!(json.contains("src/main.rs"));
    }

    #[test]
    fn test_format_quality_gate_output_human() {
        let results = QualityGateResults {
            passed: true,
            total_violations: 0,
            complexity_violations: 0,
            dead_code_violations: 0,
            satd_violations: 0,
            entropy_violations: 0,
            security_violations: 0,
            duplicate_violations: 0,
            coverage_violations: 0,
            section_violations: 0,
            provability_violations: 0,
            provability_score: Some(95.0),
            violations: Vec::new(),
        };

        let violations = vec![];

        let output =
            format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Human);
        assert!(output.is_ok());

        let text = output.unwrap();
        assert!(text.contains("✅ PASSED"));
        assert!(text.contains("Total violations: 0"));
        assert!(text.contains("Provability score: 95.00"));
    }

    #[test]
    fn test_format_quality_gate_output_junit() {
        let results = QualityGateResults {
            passed: false,
            total_violations: 2,
            complexity_violations: 1,
            dead_code_violations: 1,
            satd_violations: 0,
            entropy_violations: 0,
            security_violations: 0,
            duplicate_violations: 0,
            coverage_violations: 0,
            section_violations: 0,
            provability_violations: 0,
            provability_score: None,
            violations: Vec::new(),
        };

        let violations = vec![QualityViolation {
            check_type: "complexity".to_string(),
            severity: "error".to_string(),
            message: "Cyclomatic complexity 25 exceeds limit 20".to_string(),
            file: "src/complex.rs".to_string(),
            line: Some(50),
        }];

        let output =
            format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Junit);
        assert!(output.is_ok());

        let xml = output.unwrap();
        assert!(xml.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
        assert!(xml.contains("<testsuites name=\"Quality Gate\">"));
        assert!(xml.contains("<testcase name=\"Cyclomatic complexity 25 exceeds limit 20\""));
        assert!(xml.contains(
            "<failure message=\"Cyclomatic complexity 25 exceeds limit 20\" type=\"error\"/>"
        ));
    }

    #[test]
    fn test_format_quality_gate_output_summary() {
        let results = QualityGateResults {
            passed: true,
            total_violations: 0,
            complexity_violations: 0,
            dead_code_violations: 0,
            satd_violations: 0,
            entropy_violations: 0,
            security_violations: 0,
            duplicate_violations: 0,
            coverage_violations: 0,
            section_violations: 0,
            provability_violations: 0,
            provability_score: None,
            violations: Vec::new(),
        };

        let violations = vec![];

        let output =
            format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Summary);
        assert!(output.is_ok());

        let text = output.unwrap();
        assert!(text.contains("Quality Gate: PASSED"));
        assert!(text.contains("Total violations: 0"));
        assert!(!text.contains("##")); // Summary should be minimal
    }

    #[test]
    fn test_format_quality_gate_output_detailed() {
        let results = QualityGateResults {
            passed: false,
            total_violations: 5,
            complexity_violations: 1,
            dead_code_violations: 1,
            satd_violations: 1,
            entropy_violations: 0,
            security_violations: 1,
            duplicate_violations: 1,
            coverage_violations: 0,
            section_violations: 0,
            provability_violations: 0,
            provability_score: Some(78.5),
            violations: Vec::new(),
        };

        let violations = vec![QualityViolation {
            check_type: "security".to_string(),
            severity: "error".to_string(),
            message: "Potential SQL injection vulnerability".to_string(),
            file: "src/db.rs".to_string(),
            line: Some(123),
        }];

        let output =
            format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Detailed);
        assert!(output.is_ok());

        let text = output.unwrap();
        assert!(text.contains("❌ FAILED"));
        assert!(text.contains("## Violations by Type"));
        assert!(text.contains("- Complexity: 1"));
        assert!(text.contains("- Security: 1"));
        assert!(text.contains("Potential SQL injection vulnerability"));
        assert!(text.contains("src/db.rs:123"));
    }

    #[test]
    fn test_format_quality_gate_output_all_violation_types() {
        let results = QualityGateResults {
            passed: false,
            total_violations: 9,
            complexity_violations: 1,
            dead_code_violations: 1,
            satd_violations: 1,
            entropy_violations: 1,
            security_violations: 1,
            duplicate_violations: 1,
            coverage_violations: 1,
            section_violations: 1,
            provability_violations: 1,
            provability_score: Some(65.0),
            violations: Vec::new(),
        };

        let violations = vec![];

        let output =
            format_quality_gate_output(&results, &violations, QualityGateOutputFormat::Human);
        assert!(output.is_ok());

        let text = output.unwrap();
        assert!(text.contains("## Complexity violations: 1"));
        assert!(text.contains("## Dead code violations: 1"));
        assert!(text.contains("## Technical debt violations: 1"));
        assert!(text.contains("## Entropy violations: 1"));
        assert!(text.contains("## Security violations: 1"));
        assert!(text.contains("## Duplicate code violations: 1"));
    }

    // TDD Tests for extracted helper functions (Toyota Way)
    // Testing the functions we extracted to reduce complexity

    #[test]
    fn test_create_complexity_test_file() {
        use std::io::Read;
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Test successful file creation
        let result = create_complexity_test_file(project_path);
        assert!(result.is_ok());

        // Verify file was created
        let src_dir = project_path.join("src");
        let test_file = src_dir.join("complex.rs");
        assert!(test_file.exists());

        // Verify file contents contain expected functions
        let mut contents = String::new();
        std::fs::File::open(&test_file)
            .unwrap()
            .read_to_string(&mut contents)
            .unwrap();
        eprintln!("File contents:\n{}", contents);
        assert!(contents.contains("fn simple_function()"));
        assert!(contents.contains("fn moderate_function("));
        assert!(contents.contains("for i in 0..10"));
    }

    #[tokio::test]
    async fn test_validate_complexity_threshold_pass() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Create test file first
        create_complexity_test_file(project_path).unwrap();

        // This should not panic since threshold is high enough
        validate_complexity_threshold_pass(project_path, 25).await;

        // Test passes if no assertion fails
    }

    #[tokio::test]
    async fn test_validate_complexity_threshold_fail() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let project_path = temp_dir.path();

        // Create test file first
        create_complexity_test_file(project_path).unwrap();

        // This should not panic - it should find violations with low threshold
        validate_complexity_threshold_fail(project_path, 1).await;

        // Test passes if no assertion fails
    }

    #[test]
    fn test_apply_churn_file_filtering() {
        use crate::models::churn::{ChurnSummary, CodeChurnAnalysis, FileChurnMetrics};
        use chrono::Utc;
        use std::collections::HashMap;

        // Create test analysis with multiple files
        let mut analysis = CodeChurnAnalysis {
            generated_at: Utc::now(),
            period_days: 30,
            repository_root: std::path::PathBuf::from("."),
            files: vec![
                FileChurnMetrics {
                    path: std::path::PathBuf::from("file1.rs"),
                    relative_path: "file1.rs".to_string(),
                    commit_count: 10,
                    unique_authors: vec!["dev1".to_string()],
                    additions: 100,
                    deletions: 50,
                    churn_score: 0.8,
                    last_modified: Utc::now(),
                    first_seen: Utc::now(),
                },
                FileChurnMetrics {
                    path: std::path::PathBuf::from("file2.rs"),
                    relative_path: "file2.rs".to_string(),
                    commit_count: 15,
                    unique_authors: vec!["dev2".to_string()],
                    additions: 200,
                    deletions: 100,
                    churn_score: 0.9,
                    last_modified: Utc::now(),
                    first_seen: Utc::now(),
                },
                FileChurnMetrics {
                    path: std::path::PathBuf::from("file3.rs"),
                    relative_path: "file3.rs".to_string(),
                    commit_count: 5,
                    unique_authors: vec!["dev3".to_string()],
                    additions: 50,
                    deletions: 25,
                    churn_score: 0.3,
                    last_modified: Utc::now(),
                    first_seen: Utc::now(),
                },
            ],
            summary: ChurnSummary {
                total_commits: 30,
                total_files_changed: 3,
                author_contributions: HashMap::new(),
                hotspot_files: vec![],
                stable_files: vec![],
                mean_churn_score: 0.0,
                variance_churn_score: 0.0,
                stddev_churn_score: 0.0,
            },
        };

        // Test with no filtering (top_files = 0)
        let original_count = analysis.files.len();
        apply_churn_file_filtering(&mut analysis, 0);
        assert_eq!(analysis.files.len(), original_count);

        // Test with filtering (top_files = 2)
        apply_churn_file_filtering(&mut analysis, 2);
        assert_eq!(analysis.files.len(), 2);
        // Should be sorted by commit count desc, so file2 (15) and file1 (10)
        assert_eq!(analysis.files[0].commit_count, 15);
        assert_eq!(analysis.files[1].commit_count, 10);
    }

    #[test]
    fn test_format_churn_content() {
        use crate::models::churn::{ChurnOutputFormat, ChurnSummary, CodeChurnAnalysis};
        use chrono::Utc;
        use std::collections::HashMap;

        let analysis = CodeChurnAnalysis {
            generated_at: Utc::now(),
            period_days: 30,
            repository_root: std::path::PathBuf::from("."),
            files: vec![],
            summary: ChurnSummary {
                total_commits: 0,
                total_files_changed: 0,
                author_contributions: HashMap::new(),
                hotspot_files: vec![],
                stable_files: vec![],
                mean_churn_score: 0.0,
                variance_churn_score: 0.0,
                stddev_churn_score: 0.0,
            },
        };

        // Test JSON format
        let json_result = format_churn_content(&analysis, ChurnOutputFormat::Json);
        assert!(json_result.is_ok());
        let json_content = json_result.unwrap();
        assert!(json_content.contains("generated_at"));

        // Test Summary format
        let summary_result = format_churn_content(&analysis, ChurnOutputFormat::Summary);
        assert!(summary_result.is_ok());

        // Test Markdown format
        let markdown_result = format_churn_content(&analysis, ChurnOutputFormat::Markdown);
        assert!(markdown_result.is_ok());

        // Test CSV format
        let csv_result = format_churn_content(&analysis, ChurnOutputFormat::Csv);
        assert!(csv_result.is_ok());
    }

    #[test]
    #[ignore] // Five Whys: Process-global CWD modification causes race conditions under parallel execution
              // Root cause: std::env::set_current_dir() is process-wide, not thread-local
              // Fix attempted: RAII CwdGuard failed because current_dir() fails if CWD deleted
              // Decision: Mark as #[ignore] - unsuitable for parallel test execution
              // Run manually: cargo test test_run_comprehensive_analyses_basic -- --ignored --test-threads=1
    fn test_run_comprehensive_analyses_basic() {
        // This is a simple test to verify the function signature and basic structure
        // In a real scenario, we'd need to mock the analysis functions

        use std::path::PathBuf;

        // Create basic test data
        let mut report = ComprehensiveReport::default();
        let project_path = PathBuf::from(".");

        // Test with all options disabled (minimal execution path)
        let rt = tokio::runtime::Runtime::new().unwrap();
        let config = ComprehensiveAnalysisConfig::new(
            false, // include_complexity
            false, // include_tdg
            false, // include_dead_code
            false, // include_defects
            false, // include_duplicates
            &None, // include
            &None, // exclude
            0.5,   // confidence_threshold
            10,    // min_lines
        );
        let result = rt.block_on(async {
            run_comprehensive_analyses(&mut report, &project_path, &config).await
        });

        // Should succeed with minimal configuration
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_write_comprehensive_output() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let output_file = temp_dir.path().join("test_output.json");

        let report = ComprehensiveReport::default();

        // Test writing to file
        let result = write_comprehensive_output(
            &report,
            ComprehensiveOutputFormat::Json,
            false, // executive_summary
            Some(output_file.clone()),
        )
        .await;

        assert!(result.is_ok());
        assert!(output_file.exists());

        // Test writing to stdout (no file path)
        let stdout_result =
            write_comprehensive_output(&report, ComprehensiveOutputFormat::Json, false, None).await;

        assert!(stdout_result.is_ok());
    }