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
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
//! Integration tests for the complete unified quality system
//!
//! Tests the entire system end-to-end with realistic scenarios

use crate::unified_quality::automation::ConservativeAutomator;
use crate::unified_quality::enforcement::ErrorBudgetEnforcer;
use crate::unified_quality::foundation::QualityMonitor;
use crate::unified_quality::intelligence::QualityAssistant;

/// Production system structure for integration testing
pub struct ProductionSystem {
    pub monitor: QualityMonitor,
    pub assistant: QualityAssistant,
    pub enforcer: ErrorBudgetEnforcer,
    pub automator: ConservativeAutomator,
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::super::*;
    use super::ProductionSystem;
    use crate::unified_quality::automation::ConservativeAutomator;
    use crate::unified_quality::enforcement::{EnforcerConfig, ErrorBudgetEnforcer};
    use crate::unified_quality::foundation::{MonitorConfig, QualityMonitor};
    use crate::unified_quality::github_actions::{GitHubActionsIntegration, GitHubConfig};
    use crate::unified_quality::intelligence::QualityAssistant;
    use crate::unified_quality::onboarding::{OnboardingConfig, TeamOnboarding};
    use crate::unified_quality::performance::{PerformanceConfig, PerformanceMonitor};
    use anyhow::Result;
    use std::path::PathBuf;
    use std::time::Duration;
    use tempfile::TempDir;
    use tokio::fs;

    /// Complete system integration test
    #[tokio::test]
    async fn test_complete_system_integration() {
        // Create temporary directory for testing
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let project_path = temp_dir.path().to_path_buf();

        // Create test Rust file
        let test_file = project_path.join("test.rs");
        fs::write(
            &test_file,
            r#"
            fn simple_function() -> i32 {
                42
            }

            // TODO: Refactor complex function for better maintainability
            fn complex_function(x: i32) -> i32 {
                if x > 0 {
                    if x > 10 {
                        if x > 100 {
                            x * 3
                        } else {
                            x * 2
                        }
                    } else {
                        x + 1
                    }
                } else {
                    0
                }
            }
        "#,
        )
        .await
        .expect("Failed to write test file");

        // Initialize production system components
        let mut production_system =
            create_production_system().expect("Failed to create production system");

        // Test Phase 1: Real-time monitoring
        test_monitoring_phase(&mut production_system.monitor, &project_path).await;

        // Test Phase 2: Intelligence assistance
        test_intelligence_phase(&mut production_system.assistant, &test_file).await;

        // Test Phase 3: Error budget enforcement
        test_enforcement_phase(&mut production_system.enforcer).await;

        // Test Phase 4: Conservative automation
        test_automation_phase(&mut production_system.automator, &test_file).await;

        // Test integrated workflows
        test_integrated_workflow(&mut production_system, &project_path).await;

        // Test external integrations
        test_external_integrations(&project_path).await;

        println!("✅ Complete system integration test passed!");
    }

    /// Test progressive quality mode adoption
    #[tokio::test]
    #[ignore = "requires quality framework setup"]
    async fn test_progressive_quality_adoption() {
        let mut onboarding = create_onboarding_system();

        // Start team onboarding
        let team_preferences = create_test_team_preferences();
        let session = onboarding
            .start_onboarding("test-team".to_string(), team_preferences)
            .expect("Failed to start onboarding");

        // Test progression through quality modes
        assert_eq!(session.quality_mode, QualityMode::Observe);

        // Simulate tutorial completion and phase progression
        let team_id: crate::unified_quality::enforcement::TeamId = "test-team".to_string();
        onboarding
            .complete_tutorial(&team_id, "quality_philosophy".to_string(), 1)
            .expect("Failed to complete tutorial");

        // Generate progress report
        let report = onboarding
            .generate_progress_report(&team_id)
            .expect("Failed to generate progress report");

        assert!(report.overall_completion > 0.0);
        assert!(!report.achievements.is_empty());

        println!("✅ Progressive quality adoption test passed!");
    }

    /// Test performance monitoring and optimization
    #[tokio::test]
    async fn test_performance_monitoring() {
        let config = create_performance_config();
        let mut monitor = PerformanceMonitor::new(config);

        // Establish performance baseline
        let baseline = monitor
            .establish_baseline("test-baseline".to_string())
            .await
            .expect("Failed to establish baseline");

        assert!(!baseline.measurements.is_empty());
        assert!(baseline.measurements.contains_key("analysis_time_ms"));

        // Generate performance report
        let report = monitor.generate_performance_report();

        assert!(report.current_statistics.analysis.avg_analysis_time_ms > 0.0);
        assert!(report.current_statistics.memory.avg_memory_mb > 0.0);

        println!("✅ Performance monitoring test passed!");
    }

    /// Test GitHub Actions integration
    #[tokio::test]
    async fn test_github_actions_workflow() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let project_path = temp_dir.path().to_path_buf();

        // Create test files
        create_test_project(&project_path).await;

        // Set up GitHub Actions integration
        let monitor =
            QualityMonitor::new(MonitorConfig::default()).expect("Failed to create monitor");
        let enforcer = ErrorBudgetEnforcer::new(EnforcerConfig::default());
        let github_config = create_github_config();

        let mut integration = GitHubActionsIntegration::new(monitor, enforcer, github_config);

        // Simulate pull request analysis
        let changed_files = vec![project_path.join("src/main.rs")];
        let result = integration
            .analyze_pull_request(
                123,
                "main".to_string(),
                "feature-branch".to_string(),
                changed_files,
            )
            .await
            .expect("Failed to analyze pull request");

        assert!(matches!(
            result.status,
            crate::unified_quality::github_actions::WorkflowStatus::Success
        ));
        assert!(result.comment.is_some());
        assert!(!result.outputs.is_empty());

        // Test workflow YAML generation
        let yaml = integration.generate_workflow_yaml();
        assert!(yaml.contains("name: Quality Gate"));
        assert!(yaml.contains("pmat unified-quality analyze"));

        println!("✅ GitHub Actions integration test passed!");
    }

    /// Test cross-team budget sharing
    #[tokio::test]
    async fn test_cross_team_budget_sharing() {
        let mut enforcer = ErrorBudgetEnforcer::new(EnforcerConfig::default());

        // Register multiple teams
        enforcer.register_team("team-a".to_string(), None);
        enforcer.register_team("team-b".to_string(), None);

        // Test budget sharing scenario
        let diff_a = crate::unified_quality::enforcement::DiffAnalysis {
            complexity_change: 30,
            satd_change: 2,
            coverage_change: -0.01,
            files_changed: vec!["file1.rs".to_string()],
        };

        let decision_a = enforcer.check_commit(&"team-a".to_string(), &diff_a);
        assert!(matches!(
            decision_a,
            crate::unified_quality::enforcement::Decision::Approved
        ));

        println!("✅ Cross-team budget sharing test passed!");
    }

    /// Test ML-driven refactoring suggestions  
    #[tokio::test]
    #[ignore = "requires quality framework setup"]
    async fn test_ml_refactoring_integration() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let complex_file = temp_dir.path().join("complex.rs");

        // Create a complex file that needs refactoring
        fs::write(
            &complex_file,
            r#"
// TODO: This function is too complex
fn very_complex_function(a: i32, b: i32, c: i32) -> i32 {
    if a > 0 {
        if b > 0 {
            if c > 0 {
                if a > b {
                    if b > c {
                        // FIXME: Nested conditions are hard to read
                        match (a, b, c) {
                            (1, 2, 3) => 1,
                            (2, 3, 4) => 2,
                            (3, 4, 5) => 3,
                            _ => {
                                let mut result = 0;
                                for i in 0..a {
                                for j in 0..b {
                                for k in 0..c {
                                    if i+j+k > 10 { result += 1; }
                                }}}
                                result
                            }
                        }
                    } else {
                        a + b - c
                    }
                } else {
                    b + c - a
                }
            } else {
                a + b
            }
        } else {
            a
        }
    } else {
        0
    }
}
"#,
        )
        .await
        .expect("Failed to write complex file");

        let assistant = QualityAssistant::new();

        // Analyze file and get suggestions
        let suggestions = assistant
            .analyze_file(&complex_file)
            .await
            .expect("Failed to analyze file");

        assert!(!suggestions.is_empty());

        // Verify suggestions include refactoring recommendations
        let has_complexity_suggestion = suggestions
            .iter()
            .any(|s| s.pattern.name.contains("Extract") || s.preview.contains("Extract"));
        assert!(has_complexity_suggestion);

        println!("✅ ML-driven refactoring integration test passed!");
    }

    /// Test real-time monitoring with file changes
    #[tokio::test]
    async fn test_realtime_monitoring() {
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let project_path = temp_dir.path().to_path_buf();

        // Create initial file
        let test_file = project_path.join("monitored.rs");
        fs::write(&test_file, "fn simple() { }")
            .await
            .expect("Failed to write initial file");

        let config = MonitorConfig::default();
        let mut monitor = QualityMonitor::new(config).expect("Failed to create monitor");

        // Start monitoring (in a real scenario this would run continuously)
        monitor
            .start_monitoring(project_path.clone())
            .await
            .expect("Failed to start monitoring");

        // Simulate file change
        fs::write(
            &test_file,
            r#"
            fn more_complex() -> i32 {
                if true {
                    42
                } else {
                    0
                }
            }
        "#,
        )
        .await
        .expect("Failed to update file");

        // Give monitoring time to detect change (in practice this would be event-driven)
        tokio::time::sleep(Duration::from_millis(100)).await;

        // Check that metrics were updated
        let metrics = monitor.get_all_metrics();
        assert!(!metrics.is_empty());

        println!("✅ Real-time monitoring test passed!");
    }

    /// Test complete production deployment scenario
    #[tokio::test]
    async fn test_production_deployment_scenario() {
        // This test simulates a complete production deployment workflow

        // 1. Team starts with onboarding
        let mut onboarding = create_onboarding_system();
        let team_prefs = create_test_team_preferences();
        let _session = onboarding
            .start_onboarding("prod-team".to_string(), team_prefs)
            .expect("Failed to start onboarding");

        // 2. Set up monitoring for production codebase
        let temp_dir = TempDir::new().expect("Failed to create temp dir");
        let prod_path = temp_dir.path().to_path_buf();
        create_production_codebase(&prod_path).await;

        let mut monitor =
            QualityMonitor::new(MonitorConfig::default()).expect("Failed to create monitor");
        monitor
            .start_monitoring(prod_path.clone())
            .await
            .expect("Failed to start monitoring");

        // 3. Configure error budget enforcement
        let mut enforcer = ErrorBudgetEnforcer::new(EnforcerConfig::default());
        enforcer.register_team("prod-team".to_string(), None);

        // 4. Set up performance monitoring
        let perf_config = create_performance_config();
        let mut perf_monitor = PerformanceMonitor::new(perf_config);
        let _baseline = perf_monitor
            .establish_baseline("prod-baseline".to_string())
            .await
            .expect("Failed to establish performance baseline");

        // 5. Simulate production changes and quality enforcement
        let prod_diff = crate::unified_quality::enforcement::DiffAnalysis {
            complexity_change: 15,
            satd_change: 1,
            coverage_change: 0.02,
            files_changed: vec!["src/critical_module.rs".to_string()],
        };

        let decision = enforcer.check_commit(&"prod-team".to_string(), &prod_diff);
        assert!(matches!(
            decision,
            crate::unified_quality::enforcement::Decision::Approved
        ));

        // 6. Generate production quality report
        let all_metrics = monitor.get_all_metrics();
        assert!(
            !all_metrics.is_empty(),
            "Production monitoring should have collected metrics"
        );

        let perf_report = perf_monitor.generate_performance_report();
        assert!(perf_report.current_statistics.analysis.avg_analysis_time_ms > 0.0);

        println!("✅ Production deployment scenario test passed!");
        println!(
            "📊 Production metrics collected: {} files",
            all_metrics.len()
        );
    }

    // Helper functions for creating test components

    fn create_production_system() -> Result<ProductionSystem> {
        let monitor = QualityMonitor::new(MonitorConfig::default())?;
        let assistant = QualityAssistant::new();
        let enforcer = ErrorBudgetEnforcer::new(EnforcerConfig::default());
        let automator = ConservativeAutomator::new(
            crate::unified_quality::automation::AutomatorConfig::default(),
        );

        Ok(ProductionSystem {
            monitor,
            assistant,
            enforcer,
            automator,
        })
    }

    fn create_onboarding_system() -> TeamOnboarding {
        let config = OnboardingConfig {
            interactive_mode: true,
            personalization: true,
            track_progress: true,
            gamification: crate::unified_quality::onboarding::GamificationConfig {
                achievements: true,
                badges: true,
                leaderboards: false,
                points: true,
            },
        };

        TeamOnboarding::new(config)
    }

    fn create_test_team_preferences() -> crate::unified_quality::onboarding::TeamPreferences {
        crate::unified_quality::onboarding::TeamPreferences {
            languages: vec!["rust".to_string(), "python".to_string()],
            learning_style: crate::unified_quality::onboarding::LearningStyle::Practical,
            notifications: crate::unified_quality::onboarding::NotificationPreference {
                daily_updates: true,
                weekly_summaries: true,
                achievements: true,
                celebrations: true,
            },
            philosophy: QualityPhilosophy::default(),
            team_info: crate::unified_quality::onboarding::TeamInfo {
                size: 5,
                experience_level: crate::unified_quality::onboarding::ExperienceLevel::Intermediate,
                project_type: crate::unified_quality::onboarding::ProjectType::WebApplication,
                quality_maturity: crate::unified_quality::onboarding::QualityMaturity::Basic,
            },
        }
    }

    fn create_performance_config() -> PerformanceConfig {
        use crate::unified_quality::performance::*;

        PerformanceConfig {
            continuous_monitoring: false, // Disabled for testing
            benchmark_interval: Duration::from_secs(60),
            thresholds: PerformanceThresholds::default(),
            optimization: OptimizationConfig {
                auto_optimize: false,
                strategies: vec![OptimizationStrategy::CacheOptimization],
                min_improvement_percent: 5.0,
                experimental: false,
            },
            retention: RetentionConfig::default(),
        }
    }

    fn create_github_config() -> GitHubConfig {
        use crate::unified_quality::github_actions::*;

        GitHubConfig {
            repository: "test-org/test-repo".to_string(),
            token: "test-token".to_string(),
            quality_thresholds: QualityThresholds::default(),
            triggers: WorkflowTriggers::default(),
            comments: CommentConfig::default(),
        }
    }

    async fn create_test_project(path: &PathBuf) {
        let src_dir = path.join("src");
        fs::create_dir_all(&src_dir)
            .await
            .expect("Failed to create src directory");

        fs::write(
            src_dir.join("main.rs"),
            r#"
            fn main() {
                println!("Hello, world!");
            }
            
            fn add(a: i32, b: i32) -> i32 {
                a + b
            }
        "#,
        )
        .await
        .expect("Failed to write main.rs");
    }

    async fn create_production_codebase(path: &PathBuf) {
        let src_dir = path.join("src");
        fs::create_dir_all(&src_dir)
            .await
            .expect("Failed to create src directory");

        // Create multiple modules to simulate production codebase
        fs::write(
            src_dir.join("lib.rs"),
            r#"
            pub mod auth;
            pub mod database; 
            pub mod api;
            pub mod utils;
        "#,
        )
        .await
        .expect("Failed to write lib.rs");

        fs::write(
            src_dir.join("auth.rs"),
            r#"
            /// User.
            pub struct User {
                pub id: u64,
                pub username: String,
            }
            
            /// Authenticate user.
            pub fn authenticate_user(username: &str, password: &str) -> Option<User> {
                if username == "admin" && password == "secret" {
                    Some(User {
                        id: 1,
                        username: username.to_string(),
                    })
                } else {
                    None
                }
            }
        "#,
        )
        .await
        .expect("Failed to write auth.rs");

        fs::write(
            src_dir.join("critical_module.rs"),
            r#"
            /// Critical business logic module
            pub fn process_payment(amount: f64) -> Result<String, String> {
                if amount <= 0.0 {
                    Err("Invalid amount".to_string())
                } else if amount > 10000.0 {
                    Err("Amount too large".to_string())
                } else {
                    Ok(format!("Payment of ${:.2} processed", amount))
                }
            }
        "#,
        )
        .await
        .expect("Failed to write critical_module.rs");
    }

    // Individual phase test functions

    async fn test_monitoring_phase(monitor: &mut QualityMonitor, project_path: &PathBuf) {
        monitor
            .start_monitoring(project_path.clone())
            .await
            .expect("Failed to start monitoring");

        // Allow time for monitoring to initialize
        tokio::time::sleep(Duration::from_millis(50)).await;

        let metrics = monitor.get_all_metrics();
        // Metrics might be empty if files haven't been processed yet, which is OK for this test
        println!("📊 Monitoring phase: {} files tracked", metrics.len());
    }

    async fn test_intelligence_phase(assistant: &mut QualityAssistant, test_file: &PathBuf) {
        let suggestions = assistant
            .generate_suggestions(test_file)
            .expect("Failed to get intelligence suggestions");

        // Should have at least some suggestions for the complex function
        assert!(
            !suggestions.is_empty(),
            "Intelligence should provide suggestions"
        );
        println!(
            "🧠 Intelligence phase: {} suggestions generated",
            suggestions.len()
        );
    }

    async fn test_enforcement_phase(enforcer: &mut ErrorBudgetEnforcer) {
        enforcer.register_team("test-team".to_string(), None);

        let diff = crate::unified_quality::enforcement::DiffAnalysis {
            complexity_change: 5,
            satd_change: 0,
            coverage_change: 0.01,
            files_changed: vec!["test.rs".to_string()],
        };

        let decision = enforcer.check_commit(&"test-team".to_string(), &diff);
        assert!(matches!(
            decision,
            crate::unified_quality::enforcement::Decision::Approved
        ));
        println!("⚖️ Enforcement phase: Decision rendered");
    }

    async fn test_automation_phase(automator: &mut ConservativeAutomator, _test_file: &PathBuf) {
        let transforms = automator.get_safe_transforms();

        // Might be empty if no safe transforms are available, which is OK
        println!(
            "🤖 Automation phase: {} safe transforms suggested",
            transforms.len()
        );
    }

    async fn test_integrated_workflow(
        production_system: &mut ProductionSystem,
        _project_path: &PathBuf,
    ) {
        // Test that all components work together
        let all_metrics = production_system.monitor.get_all_metrics();

        // Test enforcement with current metrics
        if !all_metrics.is_empty() {
            let total_complexity: u32 = all_metrics.values().map(|m| m.complexity).sum();

            let diff = crate::unified_quality::enforcement::DiffAnalysis {
                complexity_change: total_complexity as i32,
                satd_change: 0,
                coverage_change: 0.0,
                files_changed: all_metrics
                    .keys()
                    .map(|p| p.to_string_lossy().to_string())
                    .collect(),
            };

            let _decision = production_system
                .enforcer
                .check_commit(&"integrated-test".to_string(), &diff);
        }

        println!("🔗 Integrated workflow: All components working together");
    }

    async fn test_external_integrations(_project_path: &PathBuf) {
        // Test that external integrations are properly configured
        // (GitHub Actions, Prometheus, etc. would be tested with actual services)
        println!("🌐 External integrations: Configuration validated");
    }
}