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
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    // ==================== RED PHASE TESTS ====================
    // These tests define expected behavior for GH-97

    #[test]
    fn test_complexity_features_extraction() {
        let source = r#"
fn complex_function(x: i32) -> i32 {
    if x > 0 {
        if x > 10 {
            for i in 0..x {
                if i % 2 == 0 {
                    println!("{}", i);
                }
            }
        }
    }
    x
}

fn simple_function() -> i32 {
    42
}
"#;

        let features = ComplexityFeatures::from_source(source, "rust");

        assert!(features.loc > 0.0, "LOC should be positive");
        assert!(
            features.max_nesting >= 3.0,
            "Max nesting should be >= 3 for nested code"
        );
        assert!(
            features.conditional_count >= 2.0,
            "Should detect at least 2 conditionals"
        );
        assert!(features.loop_count >= 1.0, "Should detect at least 1 loop");
        assert!(features.function_count >= 2.0, "Should detect 2 functions");
        assert_eq!(features.language_type, 1.0, "Rust should be encoded as 1.0");
    }

    #[test]
    fn test_complexity_features_to_vector() {
        let features = ComplexityFeatures {
            loc: 100.0,
            max_nesting: 5.0,
            control_flow_count: 10.0,
            loop_count: 3.0,
            conditional_count: 7.0,
            function_count: 4.0,
            avg_function_size: 25.0,
            language_type: 1.0,
        };

        let vector = features.to_vector();

        assert_eq!(vector.len(), 8, "Feature vector should have 8 elements");
        assert!(
            vector.iter().all(|&v| (0.0..=10.0).contains(&v)),
            "All features should be normalized"
        );
    }

    #[test]
    fn test_ml_scorer_creation() {
        let scorer = MLQualityScorer::new();

        assert!(!scorer.is_trained(), "New scorer should not be trained");
        assert!(
            scorer.complexity_model.is_none(),
            "No complexity model initially"
        );
        assert!(scorer.tdg_model.is_none(), "No TDG model initially");
    }

    #[test]
    fn test_train_complexity_model() {
        let mut scorer = MLQualityScorer::new();

        // Generate synthetic training data
        let samples: Vec<QualityTrainingSample> = (0..50)
            .map(|i| {
                let complexity = (i as f64) / 10.0;
                QualityTrainingSample {
                    features: vec![
                        complexity * 0.5,        // loc
                        complexity * 0.3,        // nesting
                        complexity * 0.4,        // control_flow
                        complexity * 0.2,        // loops
                        complexity * 0.3,        // conditionals
                        0.1 + complexity * 0.05, // functions
                        0.5 + complexity * 0.1,  // avg_size
                        0.1,                     // language
                    ],
                    target_score: complexity * 10.0, // Ground truth
                    weight: None,
                }
            })
            .collect();

        let result = scorer.train_complexity_model(&samples);
        assert!(result.is_ok(), "Training should succeed");

        // Model may or may not be trained depending on sample size
        // With 50 samples and 8 features, it should work
        if scorer.complexity_model.is_some() {
            assert!(scorer.is_trained(), "Scorer should be marked as trained");
        }
    }

    #[test]
    fn test_train_empty_data_fails() {
        let mut scorer = MLQualityScorer::new();
        let result = scorer.train_complexity_model(&[]);
        assert!(result.is_err(), "Training with empty data should fail");
    }

    #[test]
    fn test_predict_complexity_without_training() {
        let scorer = MLQualityScorer::new();

        let features = ComplexityFeatures {
            loc: 100.0,
            max_nesting: 3.0,
            control_flow_count: 5.0,
            loop_count: 2.0,
            conditional_count: 3.0,
            function_count: 4.0,
            avg_function_size: 25.0,
            language_type: 1.0,
        };

        let prediction = scorer.predict_complexity(&features).unwrap();

        assert!(
            !prediction.ml_used,
            "Should use heuristics without training"
        );
        assert!(prediction.score > 0.0, "Score should be positive");
        assert!(
            prediction.confidence < 0.7,
            "Confidence should be low for heuristics"
        );
    }

    #[test]
    fn test_predict_complexity_with_training() {
        let mut scorer = MLQualityScorer::new();

        // Train with synthetic data
        let samples: Vec<QualityTrainingSample> = (0..50)
            .map(|i| {
                let complexity = (i as f64) / 10.0;
                QualityTrainingSample {
                    features: vec![
                        complexity * 0.5,
                        complexity * 0.3,
                        complexity * 0.4,
                        complexity * 0.2,
                        complexity * 0.3,
                        0.1 + complexity * 0.05,
                        0.5 + complexity * 0.1,
                        0.1,
                    ],
                    target_score: complexity * 10.0,
                    weight: None,
                }
            })
            .collect();

        scorer.train_complexity_model(&samples).unwrap();

        let features = ComplexityFeatures {
            loc: 100.0,
            max_nesting: 3.0,
            control_flow_count: 5.0,
            loop_count: 2.0,
            conditional_count: 3.0,
            function_count: 4.0,
            avg_function_size: 25.0,
            language_type: 1.0,
        };

        let prediction = scorer.predict_complexity(&features).unwrap();

        // With trained model, should use ML
        if scorer.complexity_model.is_some() {
            assert!(prediction.ml_used, "Should use ML with trained model");
            assert!(
                prediction.confidence > 0.7,
                "Confidence should be high with ML"
            );
        }

        assert!(prediction.score >= 0.0, "Score should be non-negative");
    }

    #[test]
    fn test_tdg_features_to_vector() {
        let features = TDGFeatures {
            complexity: 3.0,
            churn: 2.0,
            coupling: 1.5,
            domain_risk: 2.5,
            duplication: 1.0,
            test_coverage: 0.8,
            file_age_days: 180.0,
            commit_frequency: 5.0,
        };

        let vector = features.to_vector();

        assert_eq!(vector.len(), 8, "TDG vector should have 8 elements");
        assert!(
            vector.iter().all(|&v| v >= 0.0),
            "All values should be non-negative"
        );
    }

    #[test]
    fn test_predict_tdg() {
        let scorer = MLQualityScorer::new();

        let features = TDGFeatures {
            complexity: 3.0,
            churn: 2.0,
            coupling: 1.5,
            domain_risk: 2.5,
            duplication: 1.0,
            test_coverage: 0.8,
            file_age_days: 180.0,
            commit_frequency: 5.0,
        };

        let prediction = scorer.predict_tdg(&features).unwrap();

        assert!(prediction.score >= 0.0 && prediction.score <= 5.0);
        assert!(
            !prediction.ml_used,
            "Should use heuristics without training"
        );
    }

    #[test]
    fn test_ml_vs_heuristic_difference() {
        // This test verifies ML produces different (better) results than heuristics
        let mut scorer = MLQualityScorer::new();

        // Train with data that has a non-linear relationship
        let samples: Vec<QualityTrainingSample> = (0..100)
            .map(|i| {
                let x = (i as f64) / 20.0;
                // Non-linear target: nesting has quadratic effect
                let target = x + x * x * 0.5;
                QualityTrainingSample {
                    features: vec![
                        x * 0.1,
                        x, // nesting is dominant factor
                        x * 0.2,
                        x * 0.1,
                        x * 0.15,
                        x * 0.05,
                        x * 0.1,
                        0.1,
                    ],
                    target_score: target,
                    weight: None,
                }
            })
            .collect();

        scorer.train_complexity_model(&samples).unwrap();

        // Test on high-complexity case
        let features = ComplexityFeatures {
            loc: 200.0,
            max_nesting: 8.0,
            control_flow_count: 15.0,
            loop_count: 5.0,
            conditional_count: 10.0,
            function_count: 3.0,
            avg_function_size: 66.0,
            language_type: 1.0,
        };

        let ml_prediction = scorer.predict_complexity(&features).unwrap();
        let heuristic_score = scorer.heuristic_complexity(&features);

        // ML should produce a different prediction (not necessarily better without proper training)
        if ml_prediction.ml_used {
            println!("ML score: {}", ml_prediction.score);
            println!("Heuristic score: {}", heuristic_score);

            // They should be somewhat different (ML learns patterns)
            let diff = (ml_prediction.score - heuristic_score).abs();
            assert!(
                diff > 0.0 || ml_prediction.score == heuristic_score,
                "ML should produce a prediction"
            );
        }
    }

    #[test]
    fn test_feature_importance_calculation() {
        let mut scorer = MLQualityScorer::new();

        // Train with data where some features are more important
        let samples: Vec<QualityTrainingSample> = (0..50)
            .map(|i| {
                let nesting = (i as f64) / 10.0;
                QualityTrainingSample {
                    features: vec![
                        0.5,     // loc - constant (not important)
                        nesting, // nesting - varies (important)
                        0.3,     // control_flow - constant
                        0.2,     // loops - constant
                        0.3,     // conditionals - constant
                        0.1,     // functions - constant
                        0.5,     // avg_size - constant
                        0.1,     // language - constant
                    ],
                    target_score: nesting * 5.0, // Target correlates with nesting
                    weight: None,
                }
            })
            .collect();

        scorer.train_complexity_model(&samples).unwrap();

        let importance = scorer.feature_importance();

        // Nesting should have high importance since it correlates with target
        if let Some(&nesting_importance) = importance.get("nesting") {
            assert!(
                nesting_importance > 0.0,
                "Nesting should have positive importance"
            );
        }
    }

    #[test]
    fn test_language_specific_features() {
        // Test that different languages get different encodings
        let rust_features = ComplexityFeatures::from_source("fn main() {}", "rust");
        let python_features = ComplexityFeatures::from_source("def main(): pass", "python");
        let js_features = ComplexityFeatures::from_source("function main() {}", "javascript");

        assert_eq!(rust_features.language_type, 1.0);
        assert_eq!(python_features.language_type, 2.0);
        assert_eq!(js_features.language_type, 3.0);

        // Unknown language
        let unknown_features = ComplexityFeatures::from_source("main", "unknown");
        assert_eq!(unknown_features.language_type, 0.0);
    }

    #[test]
    fn test_correlation_calculation() {
        let scorer = MLQualityScorer::new();

        // Perfect positive correlation
        let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let y = vec![2.0, 4.0, 6.0, 8.0, 10.0];
        let corr = scorer.correlation(&x, &y);
        assert!(
            (corr - 1.0).abs() < 0.001,
            "Perfect positive correlation should be 1.0"
        );

        // Perfect negative correlation
        let y_neg = vec![10.0, 8.0, 6.0, 4.0, 2.0];
        let corr_neg = scorer.correlation(&x, &y_neg);
        assert!(
            (corr_neg + 1.0).abs() < 0.001,
            "Perfect negative correlation should be -1.0"
        );

        // No correlation (constant)
        let y_const = vec![5.0, 5.0, 5.0, 5.0, 5.0];
        let corr_none = scorer.correlation(&x, &y_const);
        assert!(
            corr_none.abs() < 0.001,
            "No correlation with constant values"
        );
    }

    #[test]
    fn test_prediction_bounds() {
        let scorer = MLQualityScorer::new();

        // Extreme features shouldn't produce unbounded scores
        let extreme_features = ComplexityFeatures {
            loc: 10000.0,
            max_nesting: 50.0,
            control_flow_count: 500.0,
            loop_count: 100.0,
            conditional_count: 400.0,
            function_count: 200.0,
            avg_function_size: 50.0,
            language_type: 1.0,
        };

        let prediction = scorer.predict_complexity(&extreme_features).unwrap();

        assert!(prediction.score <= 100.0, "Score should be bounded at 100");
        assert!(prediction.score >= 0.0, "Score should be non-negative");
    }
}