Skip to main content

aprender_qa_certify/
lib_scoring.rs

1/// Certification tier for tier-aware scoring.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
3pub enum CertificationTier {
4    /// MVP tier: Tests 18 combinations (3 formats × 2 backends × 3 modalities).
5    /// Pass = B grade (800 score), PROVISIONAL status.
6    #[default]
7    Mvp,
8    /// Full tier: Complete 170-point verification matrix.
9    /// Pass = A+ grade (950+ score), CERTIFIED status.
10    Full,
11}
12
13/// MVP tier pass threshold (90% pass rate).
14pub const MVP_PASS_THRESHOLD: f64 = 0.90;
15
16/// MVP tier pass score (B grade).
17pub const MVP_PASS_SCORE: u32 = 800;
18
19/// Full tier pass threshold (95% on verification matrix).
20pub const FULL_PASS_THRESHOLD: f64 = 0.95;
21
22/// Full tier pass score (A+ grade).
23pub const FULL_PASS_SCORE: u32 = 950;
24
25/// Calculate certification status from MQS score.
26///
27/// Threshold: `MVP_PASS_SCORE` (800) for Certified, 700 for Provisional.
28/// Must stay aligned with `CertificationRow::derive_status()`.
29#[must_use]
30pub const fn status_from_score(mqs_score: u32, has_p0_failure: bool) -> CertificationStatus {
31    if has_p0_failure {
32        CertificationStatus::Blocked
33    } else if mqs_score >= MVP_PASS_SCORE {
34        CertificationStatus::Certified
35    } else if mqs_score >= 700 {
36        CertificationStatus::Provisional
37    } else {
38        CertificationStatus::Blocked
39    }
40}
41
42/// Calculate certification status for a specific tier.
43///
44/// # Arguments
45/// * `tier` - The certification tier (MVP or Full)
46/// * `pass_rate` - The pass rate from test execution (0.0 to 1.0)
47/// * `has_p0_failure` - Whether any P0 (critical) test failed
48#[must_use]
49pub fn status_from_tier(
50    tier: CertificationTier,
51    pass_rate: f64,
52    has_p0_failure: bool,
53) -> CertificationStatus {
54    if has_p0_failure {
55        return CertificationStatus::Blocked;
56    }
57
58    match tier {
59        CertificationTier::Mvp => {
60            if pass_rate >= MVP_PASS_THRESHOLD {
61                CertificationStatus::Provisional
62            } else {
63                CertificationStatus::Blocked
64            }
65        }
66        CertificationTier::Full => {
67            if pass_rate >= FULL_PASS_THRESHOLD {
68                CertificationStatus::Certified
69            } else if pass_rate >= MVP_PASS_THRESHOLD {
70                CertificationStatus::Provisional
71            } else {
72                CertificationStatus::Blocked
73            }
74        }
75    }
76}
77
78/// Convert pass rate to a scaled score, clamping to valid range.
79#[inline]
80fn scale_to_f_grade(pass_rate: f64) -> u32 {
81    // Clamp pass_rate to [0.0, 1.0] and scale to [0, 699]
82    let clamped = pass_rate.clamp(0.0, 1.0);
83    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
84    let score = (clamped * 699.0) as u32;
85    score.min(699)
86}
87
88/// Calculate MQS score for a specific tier.
89///
90/// # Arguments
91/// * `tier` - The certification tier (MVP or Full)
92/// * `pass_rate` - The pass rate from test execution (0.0 to 1.0)
93/// * `has_p0_failure` - Whether any P0 (critical) test failed
94#[must_use]
95pub fn score_from_tier(tier: CertificationTier, pass_rate: f64, has_p0_failure: bool) -> u32 {
96    if has_p0_failure {
97        // Scale score based on pass rate, max 699 (F grade)
98        return scale_to_f_grade(pass_rate);
99    }
100
101    match tier {
102        CertificationTier::Mvp => {
103            if pass_rate >= MVP_PASS_THRESHOLD {
104                // MVP pass: B grade (800)
105                MVP_PASS_SCORE
106            } else {
107                // Scale score based on pass rate, max 699 (F grade)
108                scale_to_f_grade(pass_rate)
109            }
110        }
111        CertificationTier::Full => {
112            if pass_rate >= FULL_PASS_THRESHOLD {
113                // Full pass: A+ grade (950+)
114                let bonus = ((pass_rate - FULL_PASS_THRESHOLD) * 1000.0).clamp(0.0, 50.0);
115                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
116                let bonus_u32 = bonus as u32;
117                FULL_PASS_SCORE + bonus_u32
118            } else if pass_rate >= MVP_PASS_THRESHOLD {
119                // Between MVP and Full threshold: B to B+ range (800-899)
120                let ratio =
121                    (pass_rate - MVP_PASS_THRESHOLD) / (FULL_PASS_THRESHOLD - MVP_PASS_THRESHOLD);
122                let bonus = (ratio * 99.0).clamp(0.0, 99.0);
123                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
124                let bonus_u32 = bonus as u32;
125                800 + bonus_u32
126            } else {
127                // Below MVP threshold: F grade
128                scale_to_f_grade(pass_rate)
129            }
130        }
131    }
132}
133
134/// Calculate letter grade from MQS score.
135#[must_use]
136pub const fn grade_from_score(mqs_score: u32) -> &'static str {
137    match mqs_score {
138        950..=1000 => "A+",
139        900..=949 => "A",
140        850..=899 => "B+",
141        800..=849 => "B",
142        700..=799 => "C",
143        _ => "F",
144    }
145}
146
147/// Calculate grade for a specific tier.
148///
149/// # Arguments
150/// * `tier` - The certification tier (MVP or Full)
151/// * `pass_rate` - The pass rate from test execution (0.0 to 1.0)
152/// * `has_p0_failure` - Whether any P0 (critical) test failed
153#[must_use]
154pub fn grade_from_tier(
155    tier: CertificationTier,
156    pass_rate: f64,
157    has_p0_failure: bool,
158) -> &'static str {
159    let score = score_from_tier(tier, pass_rate, has_p0_failure);
160    grade_from_score(score)
161}
162
163/// Update README content with new certification table.
164///
165/// # Errors
166///
167/// Returns `CertifyError::MarkerNotFound` if the markers are not found.
168pub fn update_readme(readme: &str, table_content: &str) -> Result<String> {
169    let start_idx = readme
170        .find(START_MARKER)
171        .ok_or_else(|| CertifyError::MarkerNotFound(START_MARKER.to_string()))?;
172    let end_idx = readme
173        .find(END_MARKER)
174        .ok_or_else(|| CertifyError::MarkerNotFound(END_MARKER.to_string()))?;
175
176    if start_idx >= end_idx {
177        return Err(CertifyError::MarkerNotFound(format!(
178            "START marker (pos {start_idx}) must appear before END marker (pos {end_idx})"
179        )));
180    }
181
182    let before = &readme[..start_idx + START_MARKER.len()];
183    let after = &readme[end_idx..];
184
185    Ok(format!("{before}\n{table_content}\n{after}"))
186}