pmat 3.15.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
// Tests for PDCA loop: construction, iteration results, regression checks,
// progress calculation, run execution, config, convergence, and metrics.

mod coverage_tests {
    use super::*;
    use std::time::Duration;
    use tempfile::TempDir;

    // ==================== PdcaLoop Construction Tests ====================

    #[test]
    fn test_pdca_loop_new_uses_default_config() {
        let pdca = PdcaLoop::new();
        assert_eq!(pdca.max_iterations(), 100);
    }

    #[test]
    fn test_pdca_loop_default_trait() {
        let pdca = PdcaLoop::default();
        assert_eq!(pdca.max_iterations(), 100);
    }

    #[test]
    fn test_pdca_loop_with_custom_config() {
        let config = OracleConfig {
            max_iterations: 50,
            min_progress_per_iteration: 0.01,
            stagnation_threshold: 3,
            andon_enabled: false,
            require_human_approval_above: None,
            auto_apply_threshold: 0.85,
            review_threshold: 0.6,
            batch_size: 5,
        };
        let targets = ConvergenceTargets {
            test_coverage: 0.90,
            mutation_score: 0.80,
            ..Default::default()
        };
        let pdca = PdcaLoop::with_config(config, targets);
        assert_eq!(pdca.max_iterations(), 50);
    }

    // ==================== PdcaIterationResult Tests ====================

    #[test]
    fn test_pdca_iteration_result_fields() {
        let result = PdcaIterationResult {
            iteration: 1,
            defects_found: 10,
            defects_fixed: 5,
            defects_skipped: 3,
            metrics_before: ProjectMetrics::default(),
            metrics_after: ProjectMetrics::default(),
            converged: false,
        };

        assert_eq!(result.iteration, 1);
        assert_eq!(result.defects_found, 10);
        assert_eq!(result.defects_fixed, 5);
        assert_eq!(result.defects_skipped, 3);
        assert!(!result.converged);
    }

    #[test]
    fn test_pdca_iteration_result_converged() {
        let result = PdcaIterationResult {
            iteration: 5,
            defects_found: 0,
            defects_fixed: 0,
            defects_skipped: 0,
            metrics_before: ProjectMetrics::default(),
            metrics_after: ProjectMetrics {
                test_coverage: 0.95,
                mutation_score: 0.85,
                ..Default::default()
            },
            converged: true,
        };

        assert!(result.converged);
        assert_eq!(result.iteration, 5);
    }

    // ==================== Regression Check Tests ====================

    #[test]
    fn test_check_regression_no_regression() {
        let pdca = PdcaLoop::new();
        let before = ProjectMetrics {
            test_coverage: 0.80,
            compiler_errors: 5,
            test_failures: 2,
            ..Default::default()
        };
        let after = ProjectMetrics {
            test_coverage: 0.82,
            compiler_errors: 3,
            test_failures: 1,
            ..Default::default()
        };

        let result = pdca.check_regression(&before, &after);
        assert!(result.is_ok());
    }

    #[test]
    fn test_check_regression_coverage_decreased() {
        let pdca = PdcaLoop::new();
        let before = ProjectMetrics {
            test_coverage: 0.80,
            ..Default::default()
        };
        let after = ProjectMetrics {
            test_coverage: 0.75, // Decreased by more than 1%
            ..Default::default()
        };

        let result = pdca.check_regression(&before, &after);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("ANDON: Coverage decreased"));
    }

    #[test]
    fn test_check_regression_compiler_errors_increased() {
        let pdca = PdcaLoop::new();
        let before = ProjectMetrics {
            compiler_errors: 5,
            ..Default::default()
        };
        let after = ProjectMetrics {
            compiler_errors: 8,
            ..Default::default()
        };

        let result = pdca.check_regression(&before, &after);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("ANDON: Compiler errors increased"));
    }

    #[test]
    fn test_check_regression_test_failures_increased() {
        let pdca = PdcaLoop::new();
        let before = ProjectMetrics {
            test_failures: 2,
            ..Default::default()
        };
        let after = ProjectMetrics {
            test_failures: 5,
            ..Default::default()
        };

        let result = pdca.check_regression(&before, &after);
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("ANDON: Test failures increased"));
    }

    #[test]
    fn test_check_regression_minor_coverage_decrease_ok() {
        let pdca = PdcaLoop::new();
        let before = ProjectMetrics {
            test_coverage: 0.80,
            ..Default::default()
        };
        let after = ProjectMetrics {
            test_coverage: 0.795, // Decreased by less than 1%
            ..Default::default()
        };

        let result = pdca.check_regression(&before, &after);
        assert!(result.is_ok());
    }

    // ==================== Progress Calculation Tests ====================

    #[test]
    fn test_calculate_progress_single_iteration() {
        let pdca = PdcaLoop::new();
        let results = vec![PdcaIterationResult {
            iteration: 1,
            defects_found: 10,
            defects_fixed: 5,
            defects_skipped: 3,
            metrics_before: ProjectMetrics::default(),
            metrics_after: ProjectMetrics::default(),
            converged: false,
        }];

        // Single iteration should return 1.0
        let progress = pdca.calculate_progress(&results);
        assert!((progress - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_calculate_progress_multiple_iterations() {
        let pdca = PdcaLoop::new();
        let results = vec![
            PdcaIterationResult {
                iteration: 1,
                defects_found: 10,
                defects_fixed: 3,
                defects_skipped: 2,
                metrics_before: ProjectMetrics::default(),
                metrics_after: ProjectMetrics::default(),
                converged: false,
            },
            PdcaIterationResult {
                iteration: 2,
                defects_found: 5, // Reduced from 10
                defects_fixed: 2,
                defects_skipped: 1,
                metrics_before: ProjectMetrics::default(),
                metrics_after: ProjectMetrics::default(),
                converged: false,
            },
        ];

        // Progress = (10 - 5) / 10 = 0.5
        let progress = pdca.calculate_progress(&results);
        assert!((progress - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_calculate_progress_zero_initial_defects() {
        let pdca = PdcaLoop::new();
        let results = vec![
            PdcaIterationResult {
                iteration: 1,
                defects_found: 0,
                defects_fixed: 0,
                defects_skipped: 0,
                metrics_before: ProjectMetrics::default(),
                metrics_after: ProjectMetrics::default(),
                converged: true,
            },
            PdcaIterationResult {
                iteration: 2,
                defects_found: 0,
                defects_fixed: 0,
                defects_skipped: 0,
                metrics_before: ProjectMetrics::default(),
                metrics_after: ProjectMetrics::default(),
                converged: true,
            },
        ];

        // Zero initial defects should return 1.0
        let progress = pdca.calculate_progress(&results);
        assert!((progress - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn test_calculate_progress_no_improvement() {
        let pdca = PdcaLoop::new();
        let results = vec![
            PdcaIterationResult {
                iteration: 1,
                defects_found: 10,
                defects_fixed: 0,
                defects_skipped: 10,
                metrics_before: ProjectMetrics::default(),
                metrics_after: ProjectMetrics::default(),
                converged: false,
            },
            PdcaIterationResult {
                iteration: 2,
                defects_found: 10, // No change
                defects_fixed: 0,
                defects_skipped: 10,
                metrics_before: ProjectMetrics::default(),
                metrics_after: ProjectMetrics::default(),
                converged: false,
            },
        ];

        // No progress = 0.0
        let progress = pdca.calculate_progress(&results);
        assert!((progress - 0.0).abs() < f32::EPSILON);
    }

    // ==================== Run Tests (with temp directory) ====================

    #[tokio::test]
    async fn test_run_with_nonexistent_path() {
        let pdca = PdcaLoop::new();
        let result = pdca.run(Path::new("/nonexistent/path/xyz123")).await;
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Project path does not exist"));
    }

    #[tokio::test]
    async fn test_run_single_with_nonexistent_path() {
        let pdca = PdcaLoop::new();
        let result = pdca.run_single(Path::new("/nonexistent/path/xyz123")).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_run_iterations_with_nonexistent_path() {
        let pdca = PdcaLoop::new();
        let result = pdca
            .run_iterations(Path::new("/nonexistent/path/xyz123"), 5)
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_run_with_empty_directory() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let pdca = PdcaLoop::new();

        // Running on empty directory should work but find no signals
        let result = pdca.run_iterations(temp_dir.path(), 1).await;
        // May succeed or fail depending on whether cargo commands work
        // Just verify it doesn't panic
        let _ = result;
    }

    // ==================== OracleConfig Tests ====================

    #[test]
    fn test_oracle_config_default() {
        let config = OracleConfig::default();
        assert_eq!(config.max_iterations, 100);
        assert!((config.min_progress_per_iteration - 0.001).abs() < f32::EPSILON);
        assert_eq!(config.stagnation_threshold, 5);
        assert!(config.andon_enabled);
        assert_eq!(config.require_human_approval_above, Some(10));
        assert!((config.auto_apply_threshold - 0.9).abs() < f32::EPSILON);
        assert!((config.review_threshold - 0.7).abs() < f32::EPSILON);
        assert_eq!(config.batch_size, 10);
    }

    #[test]
    fn test_oracle_config_custom() {
        let config = OracleConfig {
            max_iterations: 200,
            min_progress_per_iteration: 0.005,
            stagnation_threshold: 10,
            andon_enabled: false,
            require_human_approval_above: Some(50),
            auto_apply_threshold: 0.95,
            review_threshold: 0.75,
            batch_size: 20,
        };

        assert_eq!(config.max_iterations, 200);
        assert_eq!(config.stagnation_threshold, 10);
        assert!(!config.andon_enabled);
        assert_eq!(config.batch_size, 20);
    }

    // ==================== ConvergenceTargets Tests ====================

    #[test]
    fn test_convergence_targets_default() {
        let targets = ConvergenceTargets::default();
        assert!((targets.test_coverage - 0.95).abs() < f32::EPSILON);
        assert!((targets.mutation_score - 0.85).abs() < f32::EPSILON);
        assert_eq!(targets.max_compiler_errors, 0);
        assert_eq!(targets.max_clippy_warnings, 0);
        assert_eq!(targets.max_test_failures, 0);
        assert!((targets.min_tdg_score - 95.0).abs() < f32::EPSILON);
        assert_eq!(targets.min_rust_project_score, 90);
        assert_eq!(targets.max_satd_markers, 0);
        assert_eq!(targets.max_dead_code, 0);
        assert_eq!(targets.max_cyclomatic_complexity, 15);
        assert_eq!(targets.max_cognitive_complexity, 25);
        assert_eq!(targets.max_build_time, Duration::from_secs(60));
    }

    #[test]
    fn test_convergence_targets_check_converged() {
        let targets = ConvergenceTargets::default();
        let metrics = ProjectMetrics {
            test_coverage: 0.96,
            mutation_score: 0.90,
            compiler_errors: 0,
            clippy_warnings: 0,
            test_failures: 0,
            tdg_score: 96.0,
            rust_project_score: 95,
            satd_markers: 0,
            dead_code_items: 0,
            max_cyclomatic_complexity: 10,
            max_cognitive_complexity: 20,
            build_time: Duration::from_secs(30),
        };

        let status = targets.check(&metrics);
        assert!(matches!(status, ConvergenceStatus::Converged));
    }

    #[test]
    fn test_convergence_targets_check_not_converged_coverage() {
        let targets = ConvergenceTargets::default();
        let metrics = ProjectMetrics {
            test_coverage: 0.80, // Below 0.95 target
            mutation_score: 0.90,
            compiler_errors: 0,
            clippy_warnings: 0,
            test_failures: 0,
            tdg_score: 96.0,
            rust_project_score: 95,
            satd_markers: 0,
            dead_code_items: 0,
            max_cyclomatic_complexity: 10,
            max_cognitive_complexity: 20,
            build_time: Duration::from_secs(30),
        };

        let status = targets.check(&metrics);
        if let ConvergenceStatus::NotConverged { remaining } = status {
            assert!(!remaining.is_empty());
            assert!(remaining.iter().any(|s| s.contains("Coverage")));
        } else {
            panic!("Expected NotConverged status");
        }
    }

    #[test]
    fn test_convergence_targets_check_multiple_failures() {
        let targets = ConvergenceTargets::default();
        let metrics = ProjectMetrics {
            test_coverage: 0.80,           // Below target
            mutation_score: 0.70,          // Below target
            compiler_errors: 5,            // Above max
            clippy_warnings: 10,           // Above max
            test_failures: 3,              // Above max
            tdg_score: 80.0,               // Below min
            rust_project_score: 70,        // Below min
            satd_markers: 5,               // Above max
            dead_code_items: 10,           // Above max
            max_cyclomatic_complexity: 25, // Above max
            max_cognitive_complexity: 50,  // Above max
            build_time: Duration::from_secs(30),
        };

        let status = targets.check(&metrics);
        if let ConvergenceStatus::NotConverged { remaining } = status {
            assert!(remaining.len() >= 5);
        } else {
            panic!("Expected NotConverged status");
        }
    }

    // ==================== ProjectMetrics Tests ====================

    #[test]
    fn test_project_metrics_default() {
        let metrics = ProjectMetrics::default();
        assert!((metrics.test_coverage - 0.0).abs() < f32::EPSILON);
        assert!((metrics.mutation_score - 0.0).abs() < f32::EPSILON);
        assert_eq!(metrics.compiler_errors, 0);
        assert_eq!(metrics.clippy_warnings, 0);
        assert_eq!(metrics.test_failures, 0);
        assert!((metrics.tdg_score - 0.0).abs() < f32::EPSILON);
        assert_eq!(metrics.rust_project_score, 0);
        assert_eq!(metrics.satd_markers, 0);
        assert_eq!(metrics.dead_code_items, 0);
        assert_eq!(metrics.max_cyclomatic_complexity, 0);
        assert_eq!(metrics.max_cognitive_complexity, 0);
        assert_eq!(metrics.build_time, Duration::ZERO);
    }

    #[test]
    fn test_project_metrics_custom() {
        let metrics = ProjectMetrics {
            test_coverage: 0.85,
            mutation_score: 0.75,
            compiler_errors: 2,
            clippy_warnings: 5,
            test_failures: 1,
            tdg_score: 88.5,
            rust_project_score: 82,
            satd_markers: 3,
            dead_code_items: 7,
            max_cyclomatic_complexity: 12,
            max_cognitive_complexity: 18,
            build_time: Duration::from_secs(45),
        };

        assert!((metrics.test_coverage - 0.85).abs() < f32::EPSILON);
        assert_eq!(metrics.compiler_errors, 2);
        assert_eq!(metrics.build_time, Duration::from_secs(45));
    }
}