apr-qa-report 0.1.0

Popperian report generator and MQS scoring for APR model qualification
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
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
//! Model Qualification Score (MQS) Calculator
//!
//! Implements Toyota-style gateway checks and Popperian falsification scoring.
//!
//! ## Scoring System
//!
//! - **Raw score**: 0-1000 points across 6 categories
//! - **Normalized score**: 0-100 (logarithmic scaling, 100 is extremely hard)
//! - **Gateway checks**: G1-G4 failures zero the entire score
//!
//! ## Categories (1000 raw points total)
//!
//! | Category | Points | Description |
//! |----------|--------|-------------|
//! | QUAL     | 200    | Basic quality (loads, responds) |
//! | PERF     | 150    | Performance metrics |
//! | STAB     | 200    | Stability under stress |
//! | COMP     | 150    | Compatibility (formats, backends) |
//! | EDGE     | 150    | Edge case handling |
//! | REGR     | 150    | Regression resistance |

use apr_qa_runner::{Evidence, EvidenceCollector, Outcome};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::error::Result;

/// Gateway check result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GatewayResult {
    /// Gateway ID (G1, G2, G3, G4)
    pub id: String,
    /// Whether the gateway passed
    pub passed: bool,
    /// Description of the check
    pub description: String,
    /// Failure reason (if any)
    pub failure_reason: Option<String>,
}

impl GatewayResult {
    /// Create a passed gateway result
    #[must_use]
    pub fn passed(id: impl Into<String>, description: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            passed: true,
            description: description.into(),
            failure_reason: None,
        }
    }

    /// Create a failed gateway result
    #[must_use]
    pub fn failed(
        id: impl Into<String>,
        description: impl Into<String>,
        reason: impl Into<String>,
    ) -> Self {
        Self {
            id: id.into(),
            passed: false,
            description: description.into(),
            failure_reason: Some(reason.into()),
        }
    }
}

/// MQS category scores
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CategoryScores {
    /// Quality score (0-200)
    pub qual: u32,
    /// Performance score (0-150)
    pub perf: u32,
    /// Stability score (0-200)
    pub stab: u32,
    /// Compatibility score (0-150)
    pub comp: u32,
    /// Edge case score (0-150)
    pub edge: u32,
    /// Regression score (0-150)
    pub regr: u32,
}

impl CategoryScores {
    /// Maximum points per category
    pub const MAX_QUAL: u32 = 200;
    /// Maximum performance points
    pub const MAX_PERF: u32 = 150;
    /// Maximum stability points
    pub const MAX_STAB: u32 = 200;
    /// Maximum compatibility points
    pub const MAX_COMP: u32 = 150;
    /// Maximum edge case points
    pub const MAX_EDGE: u32 = 150;
    /// Maximum regression points
    pub const MAX_REGR: u32 = 150;
    /// Total maximum raw score
    pub const MAX_TOTAL: u32 = 1000;

    /// Calculate total raw score
    #[must_use]
    pub fn total(&self) -> u32 {
        self.qual + self.perf + self.stab + self.comp + self.edge + self.regr
    }

    /// Get category breakdown as HashMap
    #[must_use]
    pub fn breakdown(&self) -> HashMap<String, (u32, u32)> {
        let mut map = HashMap::new();
        map.insert("QUAL".to_string(), (self.qual, Self::MAX_QUAL));
        map.insert("PERF".to_string(), (self.perf, Self::MAX_PERF));
        map.insert("STAB".to_string(), (self.stab, Self::MAX_STAB));
        map.insert("COMP".to_string(), (self.comp, Self::MAX_COMP));
        map.insert("EDGE".to_string(), (self.edge, Self::MAX_EDGE));
        map.insert("REGR".to_string(), (self.regr, Self::MAX_REGR));
        map
    }
}

/// Final MQS score with all details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MqsScore {
    /// Model identifier
    pub model_id: String,
    /// Raw score (0-1000)
    pub raw_score: u32,
    /// Normalized score (0-100)
    pub normalized_score: f64,
    /// Letter grade (A+, A, B, C, D, F)
    pub grade: String,
    /// Gateway results
    pub gateways: Vec<GatewayResult>,
    /// Whether all gateways passed
    pub gateways_passed: bool,
    /// Category breakdown
    pub categories: CategoryScores,
    /// Total tests run
    pub total_tests: usize,
    /// Tests passed
    pub tests_passed: usize,
    /// Tests failed
    pub tests_failed: usize,
    /// Penalty deductions applied
    pub penalties: Vec<Penalty>,
    /// Total penalty points deducted
    pub total_penalty: u32,
}

impl MqsScore {
    /// Check if model qualifies (normalized score >= 70)
    #[must_use]
    pub fn qualifies(&self) -> bool {
        self.gateways_passed && self.normalized_score >= 70.0
    }

    /// Check if model is production-ready (normalized score >= 90)
    #[must_use]
    pub fn is_production_ready(&self) -> bool {
        self.gateways_passed && self.normalized_score >= 90.0
    }
}

/// Penalty applied to score
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Penalty {
    /// Penalty code
    pub code: String,
    /// Description
    pub description: String,
    /// Points deducted
    pub points: u32,
}

/// MQS Calculator
#[derive(Debug)]
pub struct MqsCalculator {
    /// Penalty multiplier for repeated failures
    failure_multiplier: f64,
    /// Minimum tests required per category
    #[allow(dead_code)]
    min_tests_per_category: usize,
}

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

impl MqsCalculator {
    /// Create a new calculator with default settings
    #[must_use]
    pub fn new() -> Self {
        Self {
            failure_multiplier: 1.5,
            min_tests_per_category: 10,
        }
    }

    /// Set failure multiplier
    #[must_use]
    pub fn with_failure_multiplier(mut self, multiplier: f64) -> Self {
        self.failure_multiplier = multiplier;
        self
    }

    /// Calculate MQS from evidence
    ///
    /// # Errors
    ///
    /// Returns an error if score calculation fails.
    pub fn calculate(&self, model_id: &str, evidence: &EvidenceCollector) -> Result<MqsScore> {
        let all_evidence = evidence.all();

        // Run gateway checks
        let gateways = self.check_gateways(all_evidence);
        let gateways_passed = gateways.iter().all(|g| g.passed);

        // If gateways fail, score is zero
        if !gateways_passed {
            return Ok(MqsScore {
                model_id: model_id.to_string(),
                raw_score: 0,
                normalized_score: 0.0,
                grade: "F".to_string(),
                gateways,
                gateways_passed: false,
                categories: CategoryScores::default(),
                total_tests: all_evidence.len(),
                tests_passed: evidence.pass_count(),
                tests_failed: evidence.fail_count(),
                penalties: vec![Penalty {
                    code: "GATEWAY".to_string(),
                    description: "Gateway check failed - score zeroed".to_string(),
                    points: 1000,
                }],
                total_penalty: 1000,
            });
        }

        // Calculate category scores
        let categories = self.calculate_categories(all_evidence);
        let mut penalties = Vec::new();
        let mut total_penalty: u32 = 0;

        // Apply penalties
        let crash_count = all_evidence
            .iter()
            .filter(|e| e.outcome == Outcome::Crashed)
            .count();
        if crash_count > 0 {
            let penalty = (crash_count as u32) * 20;
            penalties.push(Penalty {
                code: "CRASH".to_string(),
                description: format!("{crash_count} crash(es) detected"),
                points: penalty,
            });
            total_penalty += penalty;
        }

        let timeout_count = all_evidence
            .iter()
            .filter(|e| e.outcome == Outcome::Timeout)
            .count();
        if timeout_count > 0 {
            let penalty = (timeout_count as u32) * 10;
            penalties.push(Penalty {
                code: "TIMEOUT".to_string(),
                description: format!("{timeout_count} timeout(s) detected"),
                points: penalty,
            });
            total_penalty += penalty;
        }

        // Calculate raw score with penalties
        let raw_score = categories.total().saturating_sub(total_penalty);

        // Normalize to 0-100 using logarithmic scaling
        // This makes 100/100 extremely difficult to achieve
        let normalized = self.normalize_score(raw_score, categories.total());

        let grade = Self::calculate_grade(normalized);

        Ok(MqsScore {
            model_id: model_id.to_string(),
            raw_score,
            normalized_score: normalized,
            grade,
            gateways,
            gateways_passed: true,
            categories,
            total_tests: all_evidence.len(),
            tests_passed: evidence.pass_count(),
            tests_failed: evidence.fail_count(),
            penalties,
            total_penalty,
        })
    }

    /// Check gateway conditions (G0-G4)
    fn check_gateways(&self, evidence: &[Evidence]) -> Vec<GatewayResult> {
        let mut results = Vec::new();

        // G0: Model integrity (config/tensor consistency)
        // Checks for G0-INTEGRITY-* gate IDs
        let integrity_failures: Vec<&Evidence> = evidence
            .iter()
            .filter(|e| e.gate_id.starts_with("G0-INTEGRITY") && e.outcome.is_fail())
            .collect();
        if integrity_failures.is_empty() {
            results.push(GatewayResult::passed(
                "G0",
                "Model integrity (config/tensor match)",
            ));
        } else {
            let error_details: Vec<&str> = integrity_failures
                .iter()
                .map(|e| e.reason.as_str())
                .collect();
            results.push(GatewayResult::failed(
                "G0",
                "Model integrity (config/tensor match)",
                format!(
                    "{} integrity check(s) failed: {}",
                    integrity_failures.len(),
                    error_details.join("; ")
                ),
            ));
        }

        // G1: Model loads successfully
        let has_load_failure = evidence
            .iter()
            .any(|e| e.gate_id.contains("G1") && e.outcome.is_fail());
        if has_load_failure {
            results.push(GatewayResult::failed(
                "G1",
                "Model loads successfully",
                "Model failed to load",
            ));
        } else {
            results.push(GatewayResult::passed("G1", "Model loads successfully"));
        }

        // G2: Basic inference works
        let has_inference_failure = evidence
            .iter()
            .any(|e| e.gate_id.contains("G2") && e.outcome.is_fail());
        if has_inference_failure {
            results.push(GatewayResult::failed(
                "G2",
                "Basic inference works",
                "Inference failed",
            ));
        } else {
            results.push(GatewayResult::passed("G2", "Basic inference works"));
        }

        // G3: No crashes
        let crash_count = evidence
            .iter()
            .filter(|e| e.outcome == Outcome::Crashed)
            .count();
        if crash_count > 0 {
            results.push(GatewayResult::failed(
                "G3",
                "No crashes",
                format!("{crash_count} crash(es) detected"),
            ));
        } else {
            results.push(GatewayResult::passed("G3", "No crashes"));
        }

        // G4: Output is not garbage
        let garbage_failures = evidence
            .iter()
            .filter(|e| e.gate_id.contains("G4") && e.outcome.is_fail())
            .count();
        if garbage_failures > evidence.len() / 4 {
            // More than 25% garbage output
            results.push(GatewayResult::failed(
                "G4",
                "Output is not garbage",
                format!("{garbage_failures} garbage outputs detected"),
            ));
        } else {
            results.push(GatewayResult::passed("G4", "Output is not garbage"));
        }

        results
    }

    /// Calculate category scores from evidence
    fn calculate_categories(&self, evidence: &[Evidence]) -> CategoryScores {
        let mut scores = CategoryScores::default();

        // Group evidence by category (based on gate_id prefix)
        let mut qual_pass = 0;
        let mut qual_total = 0;
        let mut perf_pass = 0;
        let mut perf_total = 0;
        let mut stab_pass = 0;
        let mut stab_total = 0;
        let mut comp_pass = 0;
        let mut comp_total = 0;
        let mut edge_pass = 0;
        let mut edge_total = 0;
        let mut regr_pass = 0;
        let mut regr_total = 0;

        for e in evidence {
            let category = Self::extract_category(&e.gate_id);
            let passed = e.outcome.is_pass();

            match category.as_str() {
                "QUAL" => {
                    qual_total += 1;
                    if passed {
                        qual_pass += 1;
                    }
                }
                "PERF" => {
                    perf_total += 1;
                    if passed {
                        perf_pass += 1;
                    }
                }
                "STAB" => {
                    stab_total += 1;
                    if passed {
                        stab_pass += 1;
                    }
                }
                "COMP" => {
                    comp_total += 1;
                    if passed {
                        comp_pass += 1;
                    }
                }
                "EDGE" => {
                    edge_total += 1;
                    if passed {
                        edge_pass += 1;
                    }
                }
                "REGR" => {
                    regr_total += 1;
                    if passed {
                        regr_pass += 1;
                    }
                }
                _ => {
                    // Default to QUAL for unknown categories
                    qual_total += 1;
                    if passed {
                        qual_pass += 1;
                    }
                }
            }
        }

        // Calculate proportional scores
        scores.qual = Self::proportional_score(qual_pass, qual_total, CategoryScores::MAX_QUAL);
        scores.perf = Self::proportional_score(perf_pass, perf_total, CategoryScores::MAX_PERF);
        scores.stab = Self::proportional_score(stab_pass, stab_total, CategoryScores::MAX_STAB);
        scores.comp = Self::proportional_score(comp_pass, comp_total, CategoryScores::MAX_COMP);
        scores.edge = Self::proportional_score(edge_pass, edge_total, CategoryScores::MAX_EDGE);
        scores.regr = Self::proportional_score(regr_pass, regr_total, CategoryScores::MAX_REGR);

        scores
    }

    /// Extract category from gate ID (e.g., "F-QUAL-001" -> "QUAL")
    fn extract_category(gate_id: &str) -> String {
        gate_id.split('-').nth(1).unwrap_or("QUAL").to_string()
    }

    /// Calculate proportional score
    fn proportional_score(passed: usize, total: usize, max: u32) -> u32 {
        if total == 0 {
            return 0;
        }
        let ratio = passed as f64 / total as f64;
        (ratio * f64::from(max)).round() as u32
    }

    /// Normalize raw score to 0-100 using logarithmic scaling
    /// This makes achieving 100/100 extremely difficult
    fn normalize_score(&self, raw: u32, pre_penalty: u32) -> f64 {
        if pre_penalty == 0 {
            return 0.0;
        }

        let ratio = f64::from(raw) / f64::from(CategoryScores::MAX_TOTAL);

        // Apply logarithmic scaling to make high scores harder
        // f(x) = 100 * (log(1 + 9x) / log(10))
        // This maps [0,1] to [0,100] with diminishing returns
        let normalized = 100.0 * (1.0 + 9.0 * ratio).ln() / 10_f64.ln();

        // Clamp to valid range
        normalized.clamp(0.0, 100.0)
    }

    /// Calculate letter grade from normalized score
    fn calculate_grade(score: f64) -> String {
        match score {
            s if s >= 97.0 => "A+".to_string(),
            s if s >= 93.0 => "A".to_string(),
            s if s >= 90.0 => "A-".to_string(),
            s if s >= 87.0 => "B+".to_string(),
            s if s >= 83.0 => "B".to_string(),
            s if s >= 80.0 => "B-".to_string(),
            s if s >= 77.0 => "C+".to_string(),
            s if s >= 73.0 => "C".to_string(),
            s if s >= 70.0 => "C-".to_string(),
            s if s >= 67.0 => "D+".to_string(),
            s if s >= 63.0 => "D".to_string(),
            s if s >= 60.0 => "D-".to_string(),
            _ => "F".to_string(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use apr_qa_gen::{Backend, Format, Modality, ModelId, QaScenario};

    fn test_scenario() -> QaScenario {
        QaScenario::new(
            ModelId::new("test", "model"),
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "2+2=".to_string(),
            42,
        )
    }

    fn test_evidence_passed(gate_id: &str) -> Evidence {
        Evidence::corroborated(gate_id, test_scenario(), "4", 100)
    }

    fn test_evidence_failed(gate_id: &str) -> Evidence {
        Evidence::falsified(gate_id, test_scenario(), "Wrong answer", "5", 100)
    }

    #[test]
    fn test_gateway_result_passed() {
        let result = GatewayResult::passed("G1", "Model loads");
        assert!(result.passed);
        assert!(result.failure_reason.is_none());
    }

    #[test]
    fn test_gateway_result_failed() {
        let result = GatewayResult::failed("G1", "Model loads", "OOM");
        assert!(!result.passed);
        assert_eq!(result.failure_reason, Some("OOM".to_string()));
    }

    #[test]
    fn test_category_scores_total() {
        let scores = CategoryScores {
            qual: 150,
            perf: 100,
            stab: 150,
            comp: 100,
            edge: 100,
            regr: 100,
        };
        assert_eq!(scores.total(), 700);
    }

    #[test]
    fn test_category_scores_max() {
        assert_eq!(CategoryScores::MAX_TOTAL, 1000);
    }

    #[test]
    fn test_mqs_calculator_all_pass() {
        let calculator = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add passing evidence for each category
        for i in 0..10 {
            collector.add(test_evidence_passed(&format!("F-QUAL-{i:03}")));
            collector.add(test_evidence_passed(&format!("F-PERF-{i:03}")));
            collector.add(test_evidence_passed(&format!("F-STAB-{i:03}")));
            collector.add(test_evidence_passed(&format!("F-COMP-{i:03}")));
            collector.add(test_evidence_passed(&format!("F-EDGE-{i:03}")));
            collector.add(test_evidence_passed(&format!("F-REGR-{i:03}")));
        }

        let score = calculator
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        assert!(score.gateways_passed);
        assert_eq!(score.raw_score, 1000);
        assert!(score.normalized_score > 99.0);
        assert_eq!(score.grade, "A+");
    }

    #[test]
    fn test_mqs_calculator_gateway_failure() {
        let calculator = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add a crash (fails G3 gateway)
        collector.add(Evidence::crashed(
            "F-QUAL-001",
            test_scenario(),
            "SIGSEGV",
            -11,
            0,
        ));

        let score = calculator
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        assert!(!score.gateways_passed);
        assert_eq!(score.raw_score, 0);
        assert_eq!(score.normalized_score, 0.0);
        assert_eq!(score.grade, "F");
    }

    #[test]
    fn test_mqs_calculator_with_penalties() {
        let calculator = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add mostly passing tests
        for i in 0..50 {
            collector.add(test_evidence_passed(&format!("F-QUAL-{i:03}")));
        }

        // Add some timeouts (but not crashes to keep gateways passing)
        for i in 0..5 {
            collector.add(Evidence::timeout(
                &format!("F-PERF-{i:03}"),
                test_scenario(),
                30000,
            ));
        }

        let score = calculator
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        // Should have timeout penalty
        assert!(score.total_penalty > 0);
        assert!(score.penalties.iter().any(|p| p.code == "TIMEOUT"));
    }

    #[test]
    fn test_extract_category() {
        assert_eq!(MqsCalculator::extract_category("F-QUAL-001"), "QUAL");
        assert_eq!(MqsCalculator::extract_category("F-PERF-042"), "PERF");
        assert_eq!(MqsCalculator::extract_category("UNKNOWN"), "QUAL");
    }

    #[test]
    fn test_proportional_score() {
        assert_eq!(MqsCalculator::proportional_score(10, 10, 200), 200);
        assert_eq!(MqsCalculator::proportional_score(5, 10, 200), 100);
        assert_eq!(MqsCalculator::proportional_score(0, 10, 200), 0);
        assert_eq!(MqsCalculator::proportional_score(0, 0, 200), 0);
    }

    #[test]
    fn test_grade_calculation() {
        assert_eq!(MqsCalculator::calculate_grade(100.0), "A+");
        assert_eq!(MqsCalculator::calculate_grade(97.0), "A+");
        assert_eq!(MqsCalculator::calculate_grade(93.0), "A");
        assert_eq!(MqsCalculator::calculate_grade(90.0), "A-");
        assert_eq!(MqsCalculator::calculate_grade(83.0), "B");
        assert_eq!(MqsCalculator::calculate_grade(73.0), "C");
        assert_eq!(MqsCalculator::calculate_grade(50.0), "F");
    }

    #[test]
    fn test_mqs_score_qualifies() {
        let score = MqsScore {
            model_id: "test".to_string(),
            raw_score: 800,
            normalized_score: 75.0,
            grade: "C".to_string(),
            gateways: vec![],
            gateways_passed: true,
            categories: CategoryScores::default(),
            total_tests: 100,
            tests_passed: 80,
            tests_failed: 20,
            penalties: vec![],
            total_penalty: 0,
        };

        assert!(score.qualifies());
        assert!(!score.is_production_ready());
    }

    #[test]
    fn test_normalize_score_scaling() {
        let calc = MqsCalculator::new();

        // Test that normalization provides diminishing returns
        let low = calc.normalize_score(200, 200);
        let mid = calc.normalize_score(500, 500);
        let high = calc.normalize_score(900, 900);
        let perfect = calc.normalize_score(1000, 1000);

        // Each increment should be harder
        assert!(low < mid);
        assert!(mid < high);
        assert!(high < perfect);

        // Perfect score should be 100
        assert!((perfect - 100.0).abs() < 0.01);
    }

    #[test]
    fn test_grade_all_levels() {
        assert_eq!(MqsCalculator::calculate_grade(98.0), "A+");
        assert_eq!(MqsCalculator::calculate_grade(95.0), "A");
        assert_eq!(MqsCalculator::calculate_grade(91.0), "A-");
        assert_eq!(MqsCalculator::calculate_grade(88.0), "B+");
        assert_eq!(MqsCalculator::calculate_grade(85.0), "B");
        assert_eq!(MqsCalculator::calculate_grade(81.0), "B-");
        assert_eq!(MqsCalculator::calculate_grade(78.0), "C+");
        assert_eq!(MqsCalculator::calculate_grade(75.0), "C");
        assert_eq!(MqsCalculator::calculate_grade(71.0), "C-");
        assert_eq!(MqsCalculator::calculate_grade(68.0), "D+");
        assert_eq!(MqsCalculator::calculate_grade(65.0), "D");
        assert_eq!(MqsCalculator::calculate_grade(61.0), "D-");
        assert_eq!(MqsCalculator::calculate_grade(55.0), "F");
    }

    #[test]
    fn test_mqs_score_is_production_ready() {
        let score = MqsScore {
            model_id: "test".to_string(),
            raw_score: 950,
            normalized_score: 95.0,
            grade: "A".to_string(),
            gateways: vec![],
            gateways_passed: true,
            categories: CategoryScores::default(),
            total_tests: 100,
            tests_passed: 95,
            tests_failed: 5,
            penalties: vec![],
            total_penalty: 0,
        };
        assert!(score.is_production_ready());
    }

    #[test]
    fn test_mqs_score_not_qualifies() {
        let score = MqsScore {
            model_id: "test".to_string(),
            raw_score: 500,
            normalized_score: 50.0,
            grade: "F".to_string(),
            gateways: vec![],
            gateways_passed: true,
            categories: CategoryScores::default(),
            total_tests: 100,
            tests_passed: 50,
            tests_failed: 50,
            penalties: vec![],
            total_penalty: 0,
        };
        assert!(!score.qualifies());
    }

    #[test]
    fn test_mqs_score_gateway_failed_not_qualifies() {
        let score = MqsScore {
            model_id: "test".to_string(),
            raw_score: 900,
            normalized_score: 90.0,
            grade: "A-".to_string(),
            gateways: vec![],
            gateways_passed: false,
            categories: CategoryScores::default(),
            total_tests: 100,
            tests_passed: 90,
            tests_failed: 10,
            penalties: vec![],
            total_penalty: 0,
        };
        assert!(!score.qualifies());
    }

    #[test]
    fn test_category_scores_default() {
        let scores = CategoryScores::default();
        assert_eq!(scores.total(), 0);
    }

    #[test]
    fn test_category_scores_breakdown() {
        let scores = CategoryScores {
            qual: 180,
            perf: 150,
            stab: 160,
            comp: 140,
            edge: 130,
            regr: 120,
        };
        let breakdown = scores.breakdown();
        assert_eq!(breakdown.get("QUAL"), Some(&(180, 200)));
        assert_eq!(breakdown.get("PERF"), Some(&(150, 150)));
        assert_eq!(breakdown.get("STAB"), Some(&(160, 200)));
        assert_eq!(breakdown.get("COMP"), Some(&(140, 150)));
        assert_eq!(breakdown.get("EDGE"), Some(&(130, 150)));
        assert_eq!(breakdown.get("REGR"), Some(&(120, 150)));
    }

    #[test]
    fn test_penalty_clone() {
        let penalty = Penalty {
            code: "TEST".to_string(),
            description: "Test penalty".to_string(),
            points: 10,
        };
        let cloned = penalty.clone();
        assert_eq!(cloned.code, penalty.code);
        assert_eq!(cloned.points, penalty.points);
    }

    #[test]
    fn test_gateway_result_clone() {
        let result = GatewayResult::passed("G1", "Test");
        let cloned = result.clone();
        assert_eq!(cloned.id, result.id);
        assert_eq!(cloned.passed, result.passed);
    }

    #[test]
    fn test_mqs_score_serialize() {
        let score = MqsScore {
            model_id: "test".to_string(),
            raw_score: 800,
            normalized_score: 80.0,
            grade: "B".to_string(),
            gateways: vec![],
            gateways_passed: true,
            categories: CategoryScores::default(),
            total_tests: 100,
            tests_passed: 80,
            tests_failed: 20,
            penalties: vec![],
            total_penalty: 0,
        };
        let json = serde_json::to_string(&score).expect("serialize");
        assert!(json.contains("test"));
        assert!(json.contains("800"));
    }

    #[test]
    fn test_extract_category_stab() {
        assert_eq!(MqsCalculator::extract_category("F-STAB-001"), "STAB");
    }

    #[test]
    fn test_extract_category_comp() {
        assert_eq!(MqsCalculator::extract_category("F-COMP-001"), "COMP");
    }

    #[test]
    fn test_extract_category_edge() {
        assert_eq!(MqsCalculator::extract_category("F-EDGE-001"), "EDGE");
    }

    #[test]
    fn test_extract_category_regr() {
        assert_eq!(MqsCalculator::extract_category("F-REGR-001"), "REGR");
    }

    #[test]
    fn test_normalize_score_zero() {
        let calc = MqsCalculator::new();
        let score = calc.normalize_score(0, 0);
        assert_eq!(score, 0.0);
    }

    #[test]
    fn test_mqs_calculator_check_gateways() {
        let calc = MqsCalculator::new();
        let collector = EvidenceCollector::new();

        let gateways = calc.check_gateways(collector.all());
        // Should have 5 gateways (G0-G4)
        assert_eq!(gateways.len(), 5);
    }

    #[test]
    fn test_mqs_calculator_with_failure_multiplier() {
        let calc = MqsCalculator::new().with_failure_multiplier(2.0);
        assert_eq!(calc.failure_multiplier, 2.0);
    }

    #[test]
    fn test_mqs_calculator_default() {
        let calc = MqsCalculator::default();
        assert_eq!(calc.failure_multiplier, 1.5);
    }

    #[test]
    fn test_mqs_calculator_debug() {
        let calc = MqsCalculator::new();
        let debug_str = format!("{calc:?}");
        assert!(debug_str.contains("MqsCalculator"));
    }

    #[test]
    fn test_gateway_g1_failure() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add a G1 failure (model load failure)
        collector.add(Evidence::falsified(
            "G1-LOAD",
            test_scenario(),
            "Model failed to load",
            "",
            100,
        ));

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        // G1 failed should fail all gateways
        assert!(!score.gateways_passed);
        let g1 = score.gateways.iter().find(|g| g.id == "G1").unwrap();
        assert!(!g1.passed);
    }

    #[test]
    fn test_gateway_g2_failure() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add a G2 failure (basic inference failure)
        collector.add(Evidence::falsified(
            "G2-INFERENCE",
            test_scenario(),
            "Inference failed",
            "",
            100,
        ));

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        let g2 = score.gateways.iter().find(|g| g.id == "G2").unwrap();
        assert!(!g2.passed);
    }

    #[test]
    fn test_gateway_g4_failure_garbage_output() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add many G4 failures (more than 25% garbage)
        for i in 0..10 {
            collector.add(Evidence::falsified(
                &format!("G4-GARBAGE-{i:03}"),
                test_scenario(),
                "Garbage output",
                "###$$@@!!",
                100,
            ));
        }

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        let g4 = score.gateways.iter().find(|g| g.id == "G4").unwrap();
        assert!(!g4.passed);
    }

    #[test]
    fn test_mqs_with_crash_penalty() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add mostly passing evidence first (so gateways pass)
        for i in 0..50 {
            collector.add(test_evidence_passed(&format!("F-QUAL-{i:03}")));
        }

        // Now the crash count will fail G3 gateway
        // So we need to test crash penalty separately without actual crashes

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");
        assert!(score.gateways_passed);
    }

    #[test]
    fn test_calculate_categories_all_types() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add one of each category
        collector.add(test_evidence_passed("F-QUAL-001"));
        collector.add(test_evidence_passed("F-PERF-001"));
        collector.add(test_evidence_passed("F-STAB-001"));
        collector.add(test_evidence_passed("F-COMP-001"));
        collector.add(test_evidence_passed("F-EDGE-001"));
        collector.add(test_evidence_passed("F-REGR-001"));

        let categories = calc.calculate_categories(collector.all());

        assert!(categories.qual > 0);
        assert!(categories.perf > 0);
        assert!(categories.stab > 0);
        assert!(categories.comp > 0);
        assert!(categories.edge > 0);
        assert!(categories.regr > 0);
    }

    #[test]
    fn test_calculate_categories_with_failures() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add passing and failing evidence
        collector.add(test_evidence_passed("F-QUAL-001"));
        collector.add(test_evidence_failed("F-QUAL-002"));
        collector.add(test_evidence_passed("F-QUAL-003"));

        let categories = calc.calculate_categories(collector.all());

        // 2 out of 3 passed, so qual should be ~133 (2/3 of 200)
        assert!(categories.qual > 100);
        assert!(categories.qual < 200);
    }

    #[test]
    fn test_calculate_categories_unknown_category() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add evidence with unknown category - should default to QUAL
        collector.add(test_evidence_passed("UNKNOWN"));

        let categories = calc.calculate_categories(collector.all());
        assert!(categories.qual > 0);
    }

    #[test]
    fn test_gateway_result_debug() {
        let result = GatewayResult::passed("G1", "Test");
        let debug_str = format!("{result:?}");
        assert!(debug_str.contains("GatewayResult"));
    }

    #[test]
    fn test_category_scores_debug() {
        let scores = CategoryScores::default();
        let debug_str = format!("{scores:?}");
        assert!(debug_str.contains("CategoryScores"));
    }

    #[test]
    fn test_penalty_debug() {
        let penalty = Penalty {
            code: "TEST".to_string(),
            description: "Test".to_string(),
            points: 10,
        };
        let debug_str = format!("{penalty:?}");
        assert!(debug_str.contains("Penalty"));
    }

    #[test]
    fn test_mqs_score_debug() {
        let score = MqsScore {
            model_id: "test".to_string(),
            raw_score: 800,
            normalized_score: 80.0,
            grade: "B".to_string(),
            gateways: vec![],
            gateways_passed: true,
            categories: CategoryScores::default(),
            total_tests: 100,
            tests_passed: 80,
            tests_failed: 20,
            penalties: vec![],
            total_penalty: 0,
        };
        let debug_str = format!("{score:?}");
        assert!(debug_str.contains("MqsScore"));
    }

    #[test]
    fn test_mqs_score_clone() {
        let score = MqsScore {
            model_id: "test".to_string(),
            raw_score: 800,
            normalized_score: 80.0,
            grade: "B".to_string(),
            gateways: vec![],
            gateways_passed: true,
            categories: CategoryScores::default(),
            total_tests: 100,
            tests_passed: 80,
            tests_failed: 20,
            penalties: vec![],
            total_penalty: 0,
        };
        let cloned = score.clone();
        assert_eq!(cloned.model_id, score.model_id);
        assert_eq!(cloned.raw_score, score.raw_score);
    }

    #[test]
    fn test_gateway_result_serialize() {
        let result = GatewayResult::passed("G1", "Test");
        let json = serde_json::to_string(&result).expect("serialize");
        assert!(json.contains("G1"));
    }

    #[test]
    fn test_category_scores_serialize() {
        let scores = CategoryScores {
            qual: 100,
            perf: 50,
            stab: 75,
            comp: 60,
            edge: 40,
            regr: 30,
        };
        let json = serde_json::to_string(&scores).expect("serialize");
        assert!(json.contains("100"));
    }

    #[test]
    fn test_penalty_serialize() {
        let penalty = Penalty {
            code: "CRASH".to_string(),
            description: "Crash detected".to_string(),
            points: 20,
        };
        let json = serde_json::to_string(&penalty).expect("serialize");
        assert!(json.contains("CRASH"));
    }

    #[test]
    fn test_mqs_calculator_calculate_empty() {
        let calc = MqsCalculator::new();
        let collector = EvidenceCollector::new();

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        // Empty collector should pass all gateways (no failures)
        assert!(score.gateways_passed);
        assert_eq!(score.total_tests, 0);
    }

    #[test]
    fn test_category_scores_clone() {
        let scores = CategoryScores {
            qual: 100,
            perf: 50,
            stab: 75,
            comp: 60,
            edge: 40,
            regr: 30,
        };
        let cloned = scores.clone();
        assert_eq!(cloned.qual, scores.qual);
        assert_eq!(cloned.total(), scores.total());
    }

    #[test]
    fn test_mqs_score_deserialize() {
        let json = r#"{
            "model_id": "test",
            "raw_score": 800,
            "normalized_score": 80.0,
            "grade": "B",
            "gateways": [],
            "gateways_passed": true,
            "categories": {"qual": 0, "perf": 0, "stab": 0, "comp": 0, "edge": 0, "regr": 0},
            "total_tests": 100,
            "tests_passed": 80,
            "tests_failed": 20,
            "penalties": [],
            "total_penalty": 0
        }"#;
        let score: MqsScore = serde_json::from_str(json).expect("deserialize");
        assert_eq!(score.model_id, "test");
        assert_eq!(score.raw_score, 800);
    }

    #[test]
    fn test_gateway_g0_integrity_failure() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add a G0 integrity failure (layer count mismatch)
        collector.add(Evidence::falsified(
            "G0-INTEGRITY-LAYERS",
            test_scenario(),
            "config says 14 layers but tensors have 24",
            "",
            100,
        ));

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        // G0 failed should fail all gateways and zero score
        assert!(!score.gateways_passed);
        assert_eq!(score.raw_score, 0);
        assert_eq!(score.normalized_score, 0.0);
        let g0 = score.gateways.iter().find(|g| g.id == "G0").unwrap();
        assert!(!g0.passed);
        assert!(g0.failure_reason.as_ref().unwrap().contains("integrity"));
    }

    #[test]
    fn test_gateway_g0_integrity_multiple_failures() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add multiple G0 integrity failures (corrupted config scenario)
        collector.add(Evidence::falsified(
            "G0-INTEGRITY-LAYERS",
            test_scenario(),
            "config says 14 layers but tensors have 24",
            "",
            100,
        ));
        collector.add(Evidence::falsified(
            "G0-INTEGRITY-HIDDEN",
            test_scenario(),
            "config says hidden_size=4096 but embedding has 896",
            "",
            100,
        ));
        collector.add(Evidence::falsified(
            "G0-INTEGRITY-VOCAB",
            test_scenario(),
            "config says vocab_size=896 but embedding has 151936",
            "",
            100,
        ));

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        assert!(!score.gateways_passed);
        assert_eq!(score.raw_score, 0);
        let g0 = score.gateways.iter().find(|g| g.id == "G0").unwrap();
        assert!(!g0.passed);
        // Should mention all 3 failures
        assert!(g0.failure_reason.as_ref().unwrap().contains("3 integrity"));
    }

    #[test]
    fn test_gateway_g0_passes_when_no_integrity_failures() {
        let calc = MqsCalculator::new();
        let mut collector = EvidenceCollector::new();

        // Add only regular test evidence, no G0 failures
        collector.add(test_evidence_passed("F-QUAL-001"));
        collector.add(test_evidence_passed("F-PERF-001"));

        let score = calc
            .calculate("test/model", &collector)
            .expect("Calculation failed");

        assert!(score.gateways_passed);
        let g0 = score.gateways.iter().find(|g| g.id == "G0").unwrap();
        assert!(g0.passed);
    }

    #[test]
    fn test_gateway_order_g0_first() {
        let calc = MqsCalculator::new();
        let collector = EvidenceCollector::new();

        let gateways = calc.check_gateways(collector.all());
        // G0 should be first
        assert_eq!(gateways[0].id, "G0");
        assert_eq!(gateways[1].id, "G1");
        assert_eq!(gateways[2].id, "G2");
        assert_eq!(gateways[3].id, "G3");
        assert_eq!(gateways[4].id, "G4");
    }
}