ggen-test-opt 5.1.3

Test optimization and selection tooling for ggen - value scoring, Pareto selection, parallel execution
Documentation
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
//! 80/20 Pareto test selection implementation
//!
//! This module implements intelligent test selection using the Pareto principle:
//! Select the top 20% of tests (by value score) that catch 80%+ of bugs.
//!
//! Follows constitutional principle of zero-cost abstractions and type-first thinking.

use crate::types::{OptResult, TestValueScore};
use crate::TestId;
use std::collections::{HashMap, HashSet};

/// Pareto selector for 80/20 test selection
#[derive(Debug)]
pub struct ParetoSelector {
    /// Minimum bug detection rate to maintain (default: 0.80)
    min_bug_detection_rate: f64,
    /// Target test count (default: 200 for ggen's 1,178 tests)
    target_test_count: usize,
}

impl ParetoSelector {
    /// Create new Pareto selector with defaults
    ///
    /// Default configuration:
    /// - min_bug_detection_rate: 0.80 (80%)
    /// - target_test_count: 200 (17% of 1,178 tests)
    pub fn new() -> Self {
        Self {
            min_bug_detection_rate: 0.80,
            target_test_count: 200,
        }
    }

    /// Create selector with custom configuration
    pub fn with_config(min_bug_detection_rate: f64, target_test_count: usize) -> Self {
        Self {
            min_bug_detection_rate,
            target_test_count,
        }
    }

    /// Rank tests by composite value score (descending order)
    ///
    /// # Arguments
    /// * `scores` - Vector of test value scores
    ///
    /// # Returns
    /// Sorted vector with highest value tests first
    pub fn rank_tests(&self, mut scores: Vec<TestValueScore>) -> Vec<TestValueScore> {
        scores.sort_by(|a, b| {
            b.composite_value
                .partial_cmp(&a.composite_value)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        scores
    }

    /// Select top N tests from ranked list
    ///
    /// # Arguments
    /// * `ranked_scores` - Pre-sorted test scores (highest value first)
    ///
    /// # Returns
    /// Top N tests by value score
    #[must_use]
    pub fn select_top_n(&self, ranked_scores: &[TestValueScore]) -> Vec<TestValueScore> {
        ranked_scores
            .iter()
            .take(self.target_test_count)
            .cloned()
            .collect()
    }

    /// Validate that selected tests maintain minimum bug detection rate
    ///
    /// This uses a simplified heuristic based on failure frequency scores.
    /// A more sophisticated implementation would use historical bug detection data.
    ///
    /// # Arguments
    /// * `selected_tests` - Tests selected by Pareto algorithm
    /// * `all_tests` - Complete test suite
    ///
    /// # Returns
    /// `Ok(detection_rate)` if validation passes, `Err` otherwise
    pub fn validate_coverage(
        &self, selected_tests: &[TestValueScore], all_tests: &[TestValueScore],
    ) -> OptResult<f64> {
        if selected_tests.is_empty() {
            return Err(crate::types::OptimizationError::NoTestsSelected(
                "Cannot validate coverage with zero selected tests".into(),
            ));
        }

        // Calculate weighted bug detection based on failure frequency
        let selected_detection: f64 = selected_tests.iter().map(|t| t.failure_freq_score).sum();

        let total_detection: f64 = all_tests.iter().map(|t| t.failure_freq_score).sum();

        let detection_rate = if total_detection > 0.0 {
            selected_detection / total_detection
        } else {
            0.0
        };

        if detection_rate < self.min_bug_detection_rate {
            return Err(crate::types::OptimizationError::InsufficientCoverage(
                format!(
                    "Bug detection rate {:.1}% below threshold {:.1}%",
                    detection_rate * 100.0,
                    self.min_bug_detection_rate * 100.0
                ),
            ));
        }

        Ok(detection_rate)
    }

    /// Generate justification for test selection decisions
    ///
    /// # Arguments
    /// * `selected_tests` - Tests included in optimized suite
    /// * `excluded_tests` - Tests excluded from optimized suite
    ///
    /// # Returns
    /// Map of test_id to justification string
    pub fn generate_justification(
        &self, selected_tests: &[TestValueScore], excluded_tests: &[TestValueScore],
    ) -> HashMap<TestId, String> {
        let mut justifications = HashMap::new();

        // Justifications for selected tests
        for test in selected_tests {
            let reason = self.justify_inclusion(test);
            justifications.insert(test.test_id.clone(), reason);
        }

        // Justifications for excluded tests
        for test in excluded_tests {
            let reason = self.justify_exclusion(test);
            justifications.insert(test.test_id.clone(), reason);
        }

        justifications
    }

    /// Justify why test was included
    fn justify_inclusion(&self, test: &TestValueScore) -> String {
        let mut reasons = Vec::new();

        if test.failure_freq_score >= 50.0 {
            reasons.push(format!(
                "high failure rate ({:.1}%)",
                test.failure_freq_score
            ));
        }

        if test.coverage_score >= 50.0 {
            reasons.push(format!("good coverage ({:.1}%)", test.coverage_score));
        }

        if test.criticality_score >= 85.0 {
            reasons.push(format!(
                "critical path coverage ({:.1})",
                test.criticality_score
            ));
        }

        if test.speed_score >= 70.0 {
            reasons.push("fast execution".into());
        }

        if reasons.is_empty() {
            format!("Composite value: {:.1}", test.composite_value)
        } else {
            format!(
                "INCLUDED (value: {:.1}) - {}",
                test.composite_value,
                reasons.join(", ")
            )
        }
    }

    /// Justify why test was excluded
    fn justify_exclusion(&self, test: &TestValueScore) -> String {
        let mut reasons = Vec::new();

        if test.failure_freq_score < 10.0 {
            reasons.push("low failure rate (rarely catches bugs)");
        }

        if test.coverage_score < 10.0 {
            reasons.push("minimal coverage (redundant with other tests)");
        }

        if test.criticality_score < 50.0 {
            reasons.push("non-critical path");
        }

        if test.budget_penalty > 50.0 {
            reasons.push("slow execution (high budget penalty)");
        }

        if reasons.is_empty() {
            format!(
                "EXCLUDED (value: {:.1}) - below selection threshold",
                test.composite_value
            )
        } else {
            format!(
                "EXCLUDED (value: {:.1}) - {}",
                test.composite_value,
                reasons.join(", ")
            )
        }
    }

    /// Execute complete Pareto selection workflow
    ///
    /// # Arguments
    /// * `all_scores` - Complete set of test value scores
    ///
    /// # Returns
    /// Selected tests with validation and justifications
    pub fn execute_selection(
        &self, all_scores: Vec<TestValueScore>,
    ) -> OptResult<ParetoSelectionResult> {
        // Step 1: Rank all tests by value
        let ranked = self.rank_tests(all_scores);

        // Step 2: Select top N tests
        let selected = self.select_top_n(&ranked);

        // Step 3: Validate coverage
        let detection_rate = self.validate_coverage(&selected, &ranked)?;

        // Step 4: Generate justifications
        let excluded: Vec<_> = ranked
            .iter()
            .skip(self.target_test_count)
            .cloned()
            .collect();
        let justifications = self.generate_justification(&selected, &excluded);

        Ok(ParetoSelectionResult {
            selected_tests: selected,
            excluded_tests: excluded,
            bug_detection_rate: detection_rate,
            justifications,
            total_tests: ranked.len(),
            selected_count: self.target_test_count,
        })
    }
}

impl Default for ParetoSelector {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of Pareto selection process
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct ParetoSelectionResult {
    /// Tests included in optimized suite
    pub selected_tests: Vec<TestValueScore>,
    /// Tests excluded from optimized suite
    pub excluded_tests: Vec<TestValueScore>,
    /// Estimated bug detection rate
    pub bug_detection_rate: f64,
    /// Justifications for each test (selected + excluded)
    pub justifications: HashMap<TestId, String>,
    /// Total number of tests analyzed
    pub total_tests: usize,
    /// Number of tests selected
    pub selected_count: usize,
}

impl ParetoSelectionResult {
    /// Calculate reduction percentage
    #[must_use]
    pub fn reduction_percentage(&self) -> f64 {
        if self.total_tests == 0 {
            return 0.0;
        }
        ((self.total_tests - self.selected_count) as f64 / self.total_tests as f64) * 100.0
    }

    /// Get selected test IDs
    pub fn selected_test_ids(&self) -> HashSet<TestId> {
        self.selected_tests
            .iter()
            .map(|t| t.test_id.clone())
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_score(id: &str, composite: f64, failure_freq: f64) -> TestValueScore {
        TestValueScore {
            test_id: TestId::new(id).unwrap(),
            failure_freq_score: failure_freq,
            coverage_score: 50.0,
            speed_score: 70.0,
            criticality_score: 60.0,
            budget_penalty: 0.0,
            composite_value: composite,
        }
    }

    #[test]
    fn test_selector_creation() {
        let selector = ParetoSelector::new();
        assert_eq!(selector.min_bug_detection_rate, 0.80);
        assert_eq!(selector.target_test_count, 200);
    }

    #[test]
    fn test_rank_tests_descending_order() {
        let selector = ParetoSelector::new();
        let scores = vec![
            create_test_score("test1", 50.0, 30.0),
            create_test_score("test2", 80.0, 50.0),
            create_test_score("test3", 30.0, 10.0),
        ];

        let ranked = selector.rank_tests(scores);

        assert_eq!(ranked[0].test_id.as_str(), "test2"); // Highest value
        assert_eq!(ranked[1].test_id.as_str(), "test1");
        assert_eq!(ranked[2].test_id.as_str(), "test3"); // Lowest value
    }

    #[test]
    fn test_select_top_n() {
        let selector = ParetoSelector::with_config(0.80, 2);
        let scores = vec![
            create_test_score("test1", 80.0, 50.0),
            create_test_score("test2", 70.0, 40.0),
            create_test_score("test3", 60.0, 30.0),
        ];

        let ranked = selector.rank_tests(scores);
        let selected = selector.select_top_n(&ranked);

        assert_eq!(selected.len(), 2);
        assert_eq!(selected[0].test_id.as_str(), "test1");
        assert_eq!(selected[1].test_id.as_str(), "test2");
    }

    #[test]
    fn test_validate_coverage_success() {
        let selector = ParetoSelector::with_config(0.50, 2);

        let all_tests = vec![
            create_test_score("test1", 80.0, 50.0), // High failure freq
            create_test_score("test2", 70.0, 30.0),
            create_test_score("test3", 60.0, 20.0),
        ];

        let selected = vec![
            create_test_score("test1", 80.0, 50.0),
            create_test_score("test2", 70.0, 30.0),
        ];

        let result = selector.validate_coverage(&selected, &all_tests);
        assert!(result.is_ok());

        let rate = result.unwrap();
        assert!(rate >= 0.50); // Should be (50+30)/(50+30+20) = 0.80
    }

    #[test]
    fn test_validate_coverage_failure() {
        let selector = ParetoSelector::with_config(0.90, 1);

        let all_tests = vec![
            create_test_score("test1", 80.0, 30.0),
            create_test_score("test2", 70.0, 70.0), // High failure freq but not selected
        ];

        let selected = vec![create_test_score("test1", 80.0, 30.0)];

        let result = selector.validate_coverage(&selected, &all_tests);
        assert!(result.is_err()); // Should fail (30/100 = 0.30 < 0.90)
    }

    #[test]
    fn test_execute_selection_workflow() {
        let selector = ParetoSelector::with_config(0.60, 2);

        let scores = vec![
            create_test_score("test1", 80.0, 50.0),
            create_test_score("test2", 70.0, 40.0),
            create_test_score("test3", 60.0, 10.0),
        ];

        let result = selector.execute_selection(scores);
        assert!(result.is_ok());

        let selection = result.unwrap();
        assert_eq!(selection.selected_count, 2);
        assert_eq!(selection.total_tests, 3);
        assert!(selection.bug_detection_rate >= 0.60);
        assert_eq!(selection.reduction_percentage(), (1.0 / 3.0) * 100.0);
    }
}