pmat 3.14.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
//! ML Integration Tests - Phase 4.2
//!
//! End-to-end tests for ML-enhanced mutation testing pipeline

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod ml_integration_tests {
    use crate::services::mutation::{
        EquivalenceTrainingData, EquivalentMutantDetector, Mutant, MutantStatus,
        MutationOperatorType, SourceLocation, SurvivabilityPredictor, TrainingData,
    };

    #[test]
    fn test_end_to_end_ml_mutation_pipeline() {
        // Create training data from historical mutation runs
        let survivability_training = create_historical_mutation_data();
        let equivalence_training = create_historical_equivalence_data();

        // Train models
        let mut predictor = SurvivabilityPredictor::new();
        predictor.train(&survivability_training).unwrap();
        assert!(predictor.is_trained());

        let mut detector = EquivalentMutantDetector::new();
        detector.train(&equivalence_training).unwrap();
        assert!(detector.is_trained());

        // Generate new mutants
        let mutants = create_test_mutant_set();
        let original_sources = create_original_sources();

        // Step 1: Filter equivalent mutants
        let (non_equiv, _filtered_count) =
            filter_equivalent_mutants(&detector, &mutants, &original_sources);
        assert!(
            non_equiv.len() <= mutants.len(),
            "Should have same or fewer mutants after filtering"
        );

        // Step 2: Prioritize remaining mutants by kill probability
        let prioritized = predictor.prioritize_mutants(&non_equiv).unwrap();
        assert_eq!(prioritized.len(), non_equiv.len());

        // Verify prioritization is descending
        for i in 1..prioritized.len() {
            assert!(
                prioritized[i - 1].1.kill_probability >= prioritized[i].1.kill_probability,
                "Mutants should be sorted by kill probability"
            );
        }

        // Step 3: Select top N mutants for testing (smart sampling)
        let top_n = if non_equiv.len() >= 3 {
            3
        } else {
            non_equiv.len()
        };
        let smart_sample: Vec<_> = prioritized.iter().take(top_n).collect();

        assert_eq!(smart_sample.len(), top_n);

        // Verify we can make predictions (Phase 1: simple statistical model)
        for (_, prediction) in &smart_sample {
            assert!(prediction.kill_probability >= 0.0);
            assert!(prediction.kill_probability <= 1.0);
        }

        // This demonstrates: equivalence filtering + smart prioritization
        // Expected outcome: Pipeline works end-to-end
        assert!(!prioritized.is_empty(), "Should have mutants to test");
    }

    #[test]
    fn test_ml_model_persistence() {
        // Train models
        let mut predictor = SurvivabilityPredictor::new();
        predictor.train(&create_historical_mutation_data()).unwrap();

        let mut detector = EquivalentMutantDetector::new();
        detector
            .train(&create_historical_equivalence_data())
            .unwrap();

        // Save models
        let pred_path = std::path::PathBuf::from("/tmp/test_predictor.bin");
        let det_path = std::path::PathBuf::from("/tmp/test_detector.bin");

        predictor.save(&pred_path).unwrap();
        detector.save(&det_path).unwrap();

        // Load models
        let loaded_pred = SurvivabilityPredictor::load(&pred_path).unwrap();
        let loaded_det = EquivalentMutantDetector::load(&det_path).unwrap();

        assert!(loaded_pred.is_trained());
        assert!(loaded_det.is_trained());

        // Verify loaded models produce valid results
        // NOTE: Predictions may differ (DecisionTree not serialized), but should be valid
        let mutant = create_test_mutant();
        let original = "fn test(a: i32) -> i32 { a + b }";

        let pred1 = predictor.predict(&mutant).unwrap();
        let pred2 = loaded_pred.predict(&mutant).unwrap();

        // Both predictions should be valid probabilities
        assert!(pred1.kill_probability >= 0.0 && pred1.kill_probability <= 1.0);
        assert!(pred2.kill_probability >= 0.0 && pred2.kill_probability <= 1.0);

        let det1 = detector.detect_equivalent(&mutant, original).unwrap();
        let det2 = loaded_det.detect_equivalent(&mutant, original).unwrap();
        assert_eq!(det1.is_equivalent, det2.is_equivalent);
    }

    #[test]
    fn test_incremental_learning_pipeline() {
        // Initial training
        let mut predictor = SurvivabilityPredictor::new();
        let initial_data = create_historical_mutation_data();
        predictor.train(&initial_data).unwrap();

        let test_mutant = create_arithmetic_mutant();
        let _initial_prediction = predictor.predict(&test_mutant).unwrap();

        // Simulate mutation testing run - collect new data
        let new_results = vec![
            create_training_sample(MutationOperatorType::ArithmeticReplacement, true),
            create_training_sample(MutationOperatorType::ArithmeticReplacement, true),
            create_training_sample(MutationOperatorType::ArithmeticReplacement, false),
        ];

        // Update model with new data
        predictor.update(&new_results).unwrap();

        let updated_prediction = predictor.predict(&test_mutant).unwrap();

        // Prediction should still be valid
        assert!(updated_prediction.kill_probability >= 0.0);
        assert!(updated_prediction.kill_probability <= 1.0);

        // Model should still be trained
        assert!(predictor.is_trained());
    }

    #[test]
    fn test_combined_ml_effectiveness() {
        // Simulate real mutation testing scenario
        let mut predictor = SurvivabilityPredictor::new();
        predictor.train(&create_diverse_training_data()).unwrap();

        let mut detector = EquivalentMutantDetector::new();
        detector.train(&create_diverse_equivalence_data()).unwrap();

        // Create diverse mutant set
        let all_mutants = vec![
            create_arithmetic_mutant(),  // Likely killed
            create_relational_mutant(),  // Medium probability
            create_conditional_mutant(), // Lower probability
            create_identity_mutant(),    // Equivalent (should filter)
            create_tautology_mutant(),   // Equivalent (should filter)
        ];

        let original_sources = vec![
            ("test1.rs", "fn add(a: i32, b: i32) -> i32 { a + b }"),
            ("test2.rs", "fn compare(a: i32) -> bool { a > 5 }"),
            ("test3.rs", "fn check(x: bool) -> bool { x && true }"),
            ("test4.rs", "fn identity(x: i32) -> i32 { x + 0 }"),
            ("test5.rs", "fn always(x: bool) -> bool { x || true }"),
        ];

        // Filter equivalents
        let non_equiv: Vec<Mutant> = all_mutants
            .iter()
            .zip(&original_sources)
            .filter_map(|(m, (_, orig))| {
                if !detector.detect_equivalent(m, orig).unwrap().is_equivalent {
                    Some(m.clone())
                } else {
                    None
                }
            })
            .collect();

        // Should filter at least some equivalent mutants (Phase 1: pattern-based)
        assert!(
            non_equiv.len() <= all_mutants.len(),
            "Should filter or keep same count"
        );

        // Prioritize remaining
        let prioritized = predictor.prioritize_mutants(&non_equiv).unwrap();

        // Verify we have prioritized mutants
        assert!(!prioritized.is_empty(), "Should have mutants to prioritize");

        // Verify prioritization works (descending order)
        for i in 1..prioritized.len() {
            assert!(
                prioritized[i - 1].1.kill_probability >= prioritized[i].1.kill_probability,
                "Should be sorted by kill probability"
            );
        }

        // This demonstrates the ML pipeline works end-to-end
        assert!(
            !prioritized.is_empty(),
            "ML pipeline completes successfully"
        );
    }

    #[test]
    fn test_prediction_confidence_calibration() {
        let mut predictor = SurvivabilityPredictor::new();
        predictor.train(&create_calibrated_training_data()).unwrap();

        // Test on mutants with known outcomes
        let high_kill_mutant = create_arithmetic_mutant(); // Historically 80% kill rate
        let low_kill_mutant = create_conditional_mutant(); // Historically 20% kill rate

        let high_pred = predictor.predict(&high_kill_mutant).unwrap();
        let low_pred = predictor.predict(&low_kill_mutant).unwrap();

        // High kill rate mutant should have higher probability
        assert!(high_pred.kill_probability > low_pred.kill_probability);

        // Confidence should be reasonable
        assert!(high_pred.confidence >= 0.5);
        assert!(low_pred.confidence >= 0.5);
    }

    // Helper functions

    fn create_test_mutant() -> Mutant {
        Mutant {
            id: "test_1".to_string(),
            original_file: std::path::PathBuf::from("test.rs"),
            mutated_source: "fn add(a: i32, b: i32) -> i32 { a - b }".to_string(),
            location: SourceLocation {
                line: 5,
                column: 10,
                end_line: 5,
                end_column: 20,
            },
            operator: MutationOperatorType::ArithmeticReplacement,
            hash: "hash1".to_string(),
            status: MutantStatus::Pending,
        }
    }

    fn create_arithmetic_mutant() -> Mutant {
        Mutant {
            operator: MutationOperatorType::ArithmeticReplacement,
            mutated_source: "fn add(a: i32, b: i32) -> i32 { a - b }".to_string(),
            ..create_test_mutant()
        }
    }

    fn create_relational_mutant() -> Mutant {
        Mutant {
            operator: MutationOperatorType::RelationalReplacement,
            mutated_source: "fn compare(a: i32) -> bool { a >= 5 }".to_string(),
            ..create_test_mutant()
        }
    }

    fn create_conditional_mutant() -> Mutant {
        Mutant {
            operator: MutationOperatorType::ConditionalReplacement,
            mutated_source: "fn check(x: bool) -> bool { x || true }".to_string(),
            ..create_test_mutant()
        }
    }

    fn create_identity_mutant() -> Mutant {
        Mutant {
            operator: MutationOperatorType::ArithmeticReplacement,
            mutated_source: "fn identity(x: i32) -> i32 { x }".to_string(),
            ..create_test_mutant()
        }
    }

    fn create_tautology_mutant() -> Mutant {
        Mutant {
            operator: MutationOperatorType::ConditionalReplacement,
            mutated_source: "fn always(x: bool) -> bool { true }".to_string(),
            ..create_test_mutant()
        }
    }

    fn create_training_sample(operator: MutationOperatorType, was_killed: bool) -> TrainingData {
        TrainingData {
            mutant: Mutant {
                operator,
                status: if was_killed {
                    MutantStatus::Killed
                } else {
                    MutantStatus::Survived
                },
                ..create_test_mutant()
            },
            was_killed,
            test_failures: if was_killed {
                vec!["test1".to_string()]
            } else {
                vec![]
            },
            execution_time_ms: 100,
        }
    }

    fn create_historical_mutation_data() -> Vec<TrainingData> {
        vec![
            create_training_sample(MutationOperatorType::ArithmeticReplacement, true),
            create_training_sample(MutationOperatorType::ArithmeticReplacement, true),
            create_training_sample(MutationOperatorType::ArithmeticReplacement, false),
            create_training_sample(MutationOperatorType::RelationalReplacement, true),
            create_training_sample(MutationOperatorType::RelationalReplacement, false),
            create_training_sample(MutationOperatorType::ConditionalReplacement, false),
        ]
    }

    fn create_historical_equivalence_data() -> Vec<EquivalenceTrainingData> {
        vec![
            create_equivalence_sample("a + 0", "a", true),
            create_equivalence_sample("a * 1", "a", true),
            create_equivalence_sample("x || true", "true", true),
            create_equivalence_sample("x && true", "x", true),
            create_equivalence_sample("a + b", "a - b", false),
        ]
    }

    fn create_equivalence_sample(
        original: &str,
        mutated: &str,
        is_equivalent: bool,
    ) -> EquivalenceTrainingData {
        EquivalenceTrainingData {
            mutant: Mutant {
                mutated_source: mutated.to_string(),
                ..create_test_mutant()
            },
            original_source: original.to_string(),
            is_equivalent,
            verified_manually: true,
            detection_method: "test".to_string(),
        }
    }

    fn create_test_mutant_set() -> Vec<Mutant> {
        vec![
            create_arithmetic_mutant(),
            create_relational_mutant(),
            create_conditional_mutant(),
            create_identity_mutant(),
            create_tautology_mutant(),
        ]
    }

    fn create_original_sources() -> Vec<(&'static str, &'static str)> {
        vec![
            ("test1.rs", "fn add(a: i32, b: i32) -> i32 { a + b }"),
            ("test2.rs", "fn compare(a: i32) -> bool { a > 5 }"),
            ("test3.rs", "fn check(x: bool) -> bool { x && true }"),
            ("test4.rs", "fn identity(x: i32) -> i32 { x + 0 }"),
            ("test5.rs", "fn always(x: bool) -> bool { x || true }"),
        ]
    }

    fn filter_equivalent_mutants(
        detector: &EquivalentMutantDetector,
        mutants: &[Mutant],
        original_sources: &[(&str, &str)],
    ) -> (Vec<Mutant>, usize) {
        let mut non_equiv = Vec::new();
        let mut filtered = 0;

        for (i, mutant) in mutants.iter().enumerate() {
            let original = if i < original_sources.len() {
                original_sources[i].1
            } else {
                ""
            };

            if !detector
                .detect_equivalent(mutant, original)
                .unwrap()
                .is_equivalent
            {
                non_equiv.push(mutant.clone());
            } else {
                filtered += 1;
            }
        }

        (non_equiv, filtered)
    }

    fn create_diverse_training_data() -> Vec<TrainingData> {
        let mut data = Vec::new();

        // Arithmetic: 80% kill rate
        for i in 0..10 {
            data.push(create_training_sample(
                MutationOperatorType::ArithmeticReplacement,
                i < 8,
            ));
        }

        // Relational: 50% kill rate
        for i in 0..10 {
            data.push(create_training_sample(
                MutationOperatorType::RelationalReplacement,
                i < 5,
            ));
        }

        // Conditional: 20% kill rate
        for i in 0..10 {
            data.push(create_training_sample(
                MutationOperatorType::ConditionalReplacement,
                i < 2,
            ));
        }

        data
    }

    fn create_diverse_equivalence_data() -> Vec<EquivalenceTrainingData> {
        vec![
            create_equivalence_sample("a + 0", "a", true),
            create_equivalence_sample("a * 1", "a", true),
            create_equivalence_sample("a - 0", "a", true),
            create_equivalence_sample("x || true", "true", true),
            create_equivalence_sample("x && true", "x", true),
            create_equivalence_sample("a + b", "b + a", true),
            create_equivalence_sample("a + b", "a - b", false),
            create_equivalence_sample("a * b", "a / b", false),
        ]
    }

    fn create_calibrated_training_data() -> Vec<TrainingData> {
        create_diverse_training_data()
    }
}