apr-qa-gen 0.1.0

Property-based scenario generator 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
//! QA Scenario generation
//!
//! Defines test scenarios for model qualification using property-based testing.

use crate::models::ModelId;
use crate::oracle::{OracleResult, select_oracle};
use serde::{Deserialize, Serialize};

/// Inference modality
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Modality {
    /// Direct inference via `apr run`
    Run,
    /// Interactive chat via `apr chat`
    Chat,
    /// HTTP server via `apr serve`
    Serve,
}

impl Modality {
    /// Get all modalities
    #[must_use]
    pub const fn all() -> [Self; 3] {
        [Self::Run, Self::Chat, Self::Serve]
    }

    /// Get the apr command for this modality
    #[must_use]
    pub const fn command(&self) -> &'static str {
        match self {
            Self::Run => "run",
            Self::Chat => "chat",
            Self::Serve => "serve",
        }
    }
}

impl std::fmt::Display for Modality {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Run => write!(f, "run"),
            Self::Chat => write!(f, "chat"),
            Self::Serve => write!(f, "serve"),
        }
    }
}

/// Compute backend
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Backend {
    /// CPU with SIMD acceleration
    Cpu,
    /// GPU with CUDA acceleration
    Gpu,
}

impl Backend {
    /// Get all backends
    #[must_use]
    pub const fn all() -> [Self; 2] {
        [Self::Cpu, Self::Gpu]
    }

    /// Get the CLI flag for this backend
    #[must_use]
    pub const fn flag(&self) -> &'static str {
        match self {
            Self::Cpu => "",
            Self::Gpu => "--gpu",
        }
    }
}

impl std::fmt::Display for Backend {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Cpu => write!(f, "cpu"),
            Self::Gpu => write!(f, "gpu"),
        }
    }
}

/// Model format
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Format {
    /// GGUF quantized format
    Gguf,
    /// `HuggingFace` `SafeTensors` format
    SafeTensors,
    /// Native APR format
    Apr,
}

impl Format {
    /// Get all formats
    #[must_use]
    pub const fn all() -> [Self; 3] {
        [Self::Gguf, Self::SafeTensors, Self::Apr]
    }

    /// Get the file extension for this format
    #[must_use]
    pub const fn extension(&self) -> &'static str {
        match self {
            Self::Gguf => ".gguf",
            Self::SafeTensors => ".safetensors",
            Self::Apr => ".apr",
        }
    }

    /// Get the class (A=quantized, B=full precision)
    #[must_use]
    pub const fn class(&self) -> char {
        match self {
            Self::Gguf | Self::Apr => 'A', // Quantized
            Self::SafeTensors => 'B',      // Full precision
        }
    }
}

impl std::fmt::Display for Format {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Gguf => write!(f, "gguf"),
            Self::SafeTensors => write!(f, "safetensors"),
            Self::Apr => write!(f, "apr"),
        }
    }
}

/// APR tool/subcommand to test
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum AprTool {
    /// `apr run` - Direct inference
    Run,
    /// `apr chat` - Interactive chat
    Chat,
    /// `apr serve` - HTTP server
    Serve,
    /// `apr inspect` - Model inspection
    Inspect,
    /// `apr validate` - Model validation
    Validate,
    /// `apr bench` - Benchmarking
    Bench,
    /// `apr profile` - Profiling
    Profile,
    /// `apr trace` - Tracing
    Trace,
    /// `apr check` - Self-test
    Check,
    /// `apr canary` - Canary tests
    Canary,
}

impl AprTool {
    /// Get all tools
    #[must_use]
    pub const fn all() -> [Self; 10] {
        [
            Self::Run,
            Self::Chat,
            Self::Serve,
            Self::Inspect,
            Self::Validate,
            Self::Bench,
            Self::Profile,
            Self::Trace,
            Self::Check,
            Self::Canary,
        ]
    }

    /// Get the CLI subcommand
    #[must_use]
    pub const fn command(&self) -> &'static str {
        match self {
            Self::Run => "run",
            Self::Chat => "chat",
            Self::Serve => "serve",
            Self::Inspect => "inspect",
            Self::Validate => "validate",
            Self::Bench => "bench",
            Self::Profile => "profile",
            Self::Trace => "trace",
            Self::Check => "check",
            Self::Canary => "canary",
        }
    }

    /// Check if this tool requires a prompt
    #[must_use]
    pub const fn requires_prompt(&self) -> bool {
        matches!(self, Self::Run | Self::Chat)
    }

    /// Check if this tool supports trace levels
    #[must_use]
    pub const fn supports_trace(&self) -> bool {
        matches!(self, Self::Run | Self::Trace)
    }
}

impl std::fmt::Display for AprTool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.command())
    }
}

/// Trace level for debugging
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TraceLevel {
    /// No tracing
    None,
    /// Basic timing and token counts
    Basic,
    /// Per-layer statistics
    Layer,
    /// Full tensor values
    Payload,
}

impl TraceLevel {
    /// Get all trace levels
    #[must_use]
    pub const fn all() -> [Self; 4] {
        [Self::None, Self::Basic, Self::Layer, Self::Payload]
    }

    /// Get the CLI value for this trace level
    #[must_use]
    pub const fn value(&self) -> &'static str {
        match self {
            Self::None => "none",
            Self::Basic => "basic",
            Self::Layer => "layer",
            Self::Payload => "payload",
        }
    }
}

/// A single QA test scenario
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QaScenario {
    /// Unique scenario ID
    pub id: String,
    /// Model to test
    pub model: ModelId,
    /// Inference modality
    pub modality: Modality,
    /// Compute backend
    pub backend: Backend,
    /// Model format
    pub format: Format,
    /// Test prompt
    pub prompt: String,
    /// Sampling temperature
    pub temperature: f32,
    /// Maximum tokens to generate
    pub max_tokens: u32,
    /// Random seed for reproducibility
    pub seed: u64,
    /// Trace level
    pub trace_level: TraceLevel,
    /// Expected oracle type
    pub oracle_type: String,
}

impl QaScenario {
    /// Create a new scenario
    #[must_use]
    pub fn new(
        model: ModelId,
        modality: Modality,
        backend: Backend,
        format: Format,
        prompt: String,
        seed: u64,
    ) -> Self {
        let oracle = select_oracle(&prompt);
        Self {
            id: format!(
                "{}_{}_{}_{}_{:016x}",
                model.name, modality, backend, format, seed
            ),
            model,
            modality,
            backend,
            format,
            prompt,
            temperature: 0.0, // Deterministic by default
            max_tokens: 32,
            seed,
            trace_level: TraceLevel::None,
            oracle_type: oracle.name().to_string(),
        }
    }

    /// Set temperature
    #[must_use]
    pub const fn with_temperature(mut self, temp: f32) -> Self {
        self.temperature = temp;
        self
    }

    /// Set max tokens
    #[must_use]
    pub const fn with_max_tokens(mut self, tokens: u32) -> Self {
        self.max_tokens = tokens;
        self
    }

    /// Set trace level
    #[must_use]
    pub const fn with_trace_level(mut self, level: TraceLevel) -> Self {
        self.trace_level = level;
        self
    }

    /// Generate the apr CLI command for this scenario
    #[must_use]
    pub fn to_command(&self, model_path: &str) -> String {
        let backend_flag = self.backend.flag();
        let trace_flag = if self.trace_level == TraceLevel::None {
            String::new()
        } else {
            format!("--trace --trace-level {}", self.trace_level.value())
        };

        match self.modality {
            Modality::Run => {
                format!(
                    "apr run {model_path} '{}' -n {} --seed {} --temperature {} {backend_flag} {trace_flag}",
                    escape_prompt(&self.prompt),
                    self.max_tokens,
                    self.seed,
                    self.temperature
                )
                .trim()
                .to_string()
            }
            Modality::Chat => {
                format!(
                    "echo '{}' | apr chat {model_path} --temperature {} {backend_flag} {trace_flag}",
                    escape_prompt(&self.prompt),
                    self.temperature
                )
                .trim()
                .to_string()
            }
            Modality::Serve => {
                format!(
                    r#"apr serve {model_path} --port ${{PORT}} {backend_flag} &
sleep 2
curl -s http://localhost:${{PORT}}/v1/completions \
  -H 'Content-Type: application/json' \
  -d '{{"prompt": "{}", "max_tokens": {}, "temperature": {}}}'
kill %1"#,
                    escape_json(&self.prompt),
                    self.max_tokens,
                    self.temperature
                )
            }
        }
    }

    /// Evaluate the output using the appropriate oracle
    #[must_use]
    pub fn evaluate(&self, output: &str) -> OracleResult {
        let oracle = select_oracle(&self.prompt);
        oracle.evaluate(&self.prompt, output)
    }

    /// Get the MQS category this scenario contributes to
    #[must_use]
    pub const fn mqs_category(&self) -> &'static str {
        match self.modality {
            Modality::Run => match self.backend {
                Backend::Cpu => "A1",
                Backend::Gpu => "A2",
            },
            Modality::Chat => match self.backend {
                Backend::Cpu => "A3",
                Backend::Gpu => "A4",
            },
            Modality::Serve => match self.backend {
                Backend::Cpu => "A5",
                Backend::Gpu => "A6",
            },
        }
    }
}

/// Escape a prompt for shell usage
fn escape_prompt(prompt: &str) -> String {
    prompt.replace('\'', "'\\''")
}

/// Escape a prompt for JSON usage
fn escape_json(prompt: &str) -> String {
    prompt
        .replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
}

/// Scenario generator for property-based testing
#[derive(Debug, Clone)]
pub struct ScenarioGenerator {
    /// Model to generate scenarios for
    pub model: ModelId,
    /// Number of scenarios per modality/backend/format combination
    pub scenarios_per_combination: usize,
    /// Prompts to use
    pub prompts: Vec<String>,
}

impl ScenarioGenerator {
    /// Create a new generator
    #[must_use]
    pub fn new(model: ModelId) -> Self {
        Self {
            model,
            scenarios_per_combination: 100,
            prompts: default_prompts(),
        }
    }

    /// Set the number of scenarios per combination
    #[must_use]
    pub const fn with_scenarios_per_combination(mut self, count: usize) -> Self {
        self.scenarios_per_combination = count;
        self
    }

    /// Set custom prompts
    #[must_use]
    pub fn with_prompts(mut self, prompts: Vec<String>) -> Self {
        self.prompts = prompts;
        self
    }

    /// Generate all scenarios for a model
    #[must_use]
    pub fn generate(&self) -> Vec<QaScenario> {
        let mut scenarios = Vec::new();
        let mut seed: u64 = 0;

        for modality in Modality::all() {
            for backend in Backend::all() {
                for format in Format::all() {
                    for i in 0..self.scenarios_per_combination {
                        let prompt_idx = i % self.prompts.len();
                        let prompt = &self.prompts[prompt_idx];

                        scenarios.push(QaScenario::new(
                            self.model.clone(),
                            modality,
                            backend,
                            format,
                            prompt.clone(),
                            seed,
                        ));

                        seed = seed.wrapping_add(1);
                    }
                }
            }
        }

        scenarios
    }

    /// Generate scenarios for a specific combination
    #[must_use]
    pub fn generate_for(
        &self,
        modality: Modality,
        backend: Backend,
        format: Format,
    ) -> Vec<QaScenario> {
        let mut scenarios = Vec::new();
        let base_seed: u64 = (modality as u64) << 32 | (backend as u64) << 16 | (format as u64);

        for (i, prompt) in self
            .prompts
            .iter()
            .enumerate()
            .take(self.scenarios_per_combination)
        {
            scenarios.push(QaScenario::new(
                self.model.clone(),
                modality,
                backend,
                format,
                prompt.clone(),
                base_seed.wrapping_add(i as u64),
            ));
        }

        scenarios
    }
}

/// Get default test prompts
fn default_prompts() -> Vec<String> {
    vec![
        // Arithmetic (verifiable)
        "What is 2+2?".to_string(),
        "Calculate 7*8".to_string(),
        "What is 15-7?".to_string(),
        "What is 100/4?".to_string(),
        "2+2=".to_string(),
        // Code completion
        "def fibonacci(n):".to_string(),
        "fn main() {".to_string(),
        "async function fetch() {".to_string(),
        "class Person:".to_string(),
        // Instruction following
        "Write a haiku about programming.".to_string(),
        "List three colors.".to_string(),
        "Explain what a variable is in one sentence.".to_string(),
        "Say hello in three languages.".to_string(),
        // Edge cases
        String::new(),            // Empty prompt
        " ".to_string(),          // Whitespace only
        "Hello!".to_string(),     // Simple greeting
        "你好".to_string(),       // Chinese
        "こんにちは".to_string(), // Japanese
    ]
}

#[cfg(test)]
#[allow(clippy::expect_used, clippy::redundant_closure_for_method_calls)]
mod tests {
    use super::*;
    use crate::models::ModelId;

    #[test]
    fn test_scenario_creation() {
        let model = ModelId::new("Qwen", "Qwen2.5-Coder-1.5B");
        let scenario = QaScenario::new(
            model.clone(),
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "2+2=".to_string(),
            42,
        );

        assert_eq!(scenario.modality, Modality::Run);
        assert_eq!(scenario.backend, Backend::Cpu);
        assert_eq!(scenario.format, Format::Gguf);
        assert_eq!(scenario.oracle_type, "arithmetic");
    }

    #[test]
    fn test_scenario_to_command_run() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "Hello".to_string(),
            0,
        );

        let cmd = scenario.to_command("model.gguf");
        assert!(cmd.contains("apr run"));
        assert!(cmd.contains("model.gguf"));
        assert!(cmd.contains("Hello"));
    }

    #[test]
    fn test_scenario_to_command_gpu() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Gpu,
            Format::Gguf,
            "Hello".to_string(),
            0,
        );

        let cmd = scenario.to_command("model.gguf");
        assert!(cmd.contains("--gpu"));
    }

    #[test]
    fn test_scenario_generator() {
        let model = ModelId::new("test", "model");
        let generator = ScenarioGenerator::new(model).with_scenarios_per_combination(10);

        let scenarios = generator.generate();

        // 3 modalities × 2 backends × 3 formats × 10 = 180
        assert_eq!(scenarios.len(), 180);
    }

    #[test]
    fn test_scenario_mqs_category() {
        let model = ModelId::new("test", "model");

        let run_cpu = QaScenario::new(
            model.clone(),
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );
        assert_eq!(run_cpu.mqs_category(), "A1");

        let chat_gpu = QaScenario::new(
            model.clone(),
            Modality::Chat,
            Backend::Gpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );
        assert_eq!(chat_gpu.mqs_category(), "A4");

        let serve_cpu = QaScenario::new(
            model,
            Modality::Serve,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );
        assert_eq!(serve_cpu.mqs_category(), "A5");
    }

    #[test]
    fn test_format_class() {
        assert_eq!(Format::Gguf.class(), 'A');
        assert_eq!(Format::Apr.class(), 'A');
        assert_eq!(Format::SafeTensors.class(), 'B');
    }

    #[test]
    fn test_escape_prompt() {
        assert_eq!(escape_prompt("hello"), "hello");
        assert_eq!(escape_prompt("it's"), "it'\\''s");
    }

    #[test]
    fn test_escape_json() {
        assert_eq!(escape_json("hello"), "hello");
        assert_eq!(escape_json("line1\nline2"), "line1\\nline2");
        assert_eq!(escape_json("say \"hi\""), "say \\\"hi\\\"");
    }

    #[test]
    fn test_escape_json_backslash() {
        assert_eq!(escape_json("path\\file"), "path\\\\file");
    }

    #[test]
    fn test_scenario_with_temperature() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        )
        .with_temperature(0.7);

        assert!((scenario.temperature - 0.7).abs() < f32::EPSILON);
    }

    #[test]
    fn test_scenario_with_max_tokens() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        )
        .with_max_tokens(256);

        assert_eq!(scenario.max_tokens, 256);
    }

    #[test]
    fn test_scenario_with_trace_level() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        )
        .with_trace_level(TraceLevel::Layer);

        assert_eq!(scenario.trace_level, TraceLevel::Layer);
    }

    #[test]
    fn test_scenario_to_command_chat() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Chat,
            Backend::Cpu,
            Format::Gguf,
            "Hello".to_string(),
            0,
        );

        let cmd = scenario.to_command("model.gguf");
        assert!(cmd.contains("apr chat"));
        assert!(cmd.contains("echo"));
    }

    #[test]
    fn test_scenario_to_command_serve() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Serve,
            Backend::Cpu,
            Format::Gguf,
            "Hello".to_string(),
            0,
        );

        let cmd = scenario.to_command("model.gguf");
        assert!(cmd.contains("apr serve"));
        assert!(cmd.contains("curl"));
        assert!(cmd.contains("/v1/completions"));
    }

    #[test]
    fn test_scenario_to_command_with_trace() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "Hello".to_string(),
            0,
        )
        .with_trace_level(TraceLevel::Payload);

        let cmd = scenario.to_command("model.gguf");
        assert!(cmd.contains("--trace"));
        assert!(cmd.contains("--trace-level payload"));
    }

    #[test]
    fn test_scenario_evaluate() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "What is 2+2?".to_string(),
            0,
        );

        let result = scenario.evaluate("The answer is 4");
        assert!(matches!(result, crate::OracleResult::Corroborated { .. }));
    }

    #[test]
    fn test_trace_level_value() {
        assert_eq!(TraceLevel::None.value(), "none");
        assert_eq!(TraceLevel::Basic.value(), "basic");
        assert_eq!(TraceLevel::Layer.value(), "layer");
        assert_eq!(TraceLevel::Payload.value(), "payload");
    }

    #[test]
    fn test_modality_display() {
        assert_eq!(format!("{}", Modality::Run), "run");
        assert_eq!(format!("{}", Modality::Chat), "chat");
        assert_eq!(format!("{}", Modality::Serve), "serve");
    }

    #[test]
    fn test_backend_flag() {
        assert_eq!(Backend::Cpu.flag(), "");
        assert_eq!(Backend::Gpu.flag(), "--gpu");
    }

    #[test]
    fn test_backend_display() {
        assert_eq!(format!("{}", Backend::Cpu), "cpu");
        assert_eq!(format!("{}", Backend::Gpu), "gpu");
    }

    #[test]
    fn test_format_display() {
        assert_eq!(format!("{}", Format::Gguf), "gguf");
        assert_eq!(format!("{}", Format::SafeTensors), "safetensors");
        assert_eq!(format!("{}", Format::Apr), "apr");
    }

    #[test]
    fn test_modality_all() {
        let all = Modality::all();
        assert_eq!(all.len(), 3);
        assert!(all.contains(&Modality::Run));
        assert!(all.contains(&Modality::Chat));
        assert!(all.contains(&Modality::Serve));
    }

    #[test]
    fn test_backend_all() {
        let all = Backend::all();
        assert_eq!(all.len(), 2);
        assert!(all.contains(&Backend::Cpu));
        assert!(all.contains(&Backend::Gpu));
    }

    #[test]
    fn test_format_all() {
        let all = Format::all();
        assert_eq!(all.len(), 3);
        assert!(all.contains(&Format::Gguf));
        assert!(all.contains(&Format::SafeTensors));
        assert!(all.contains(&Format::Apr));
    }

    #[test]
    fn test_generator_with_prompts() {
        let model = ModelId::new("test", "model");
        let prompts = vec!["prompt1".to_string(), "prompt2".to_string()];
        let generator = ScenarioGenerator::new(model)
            .with_prompts(prompts.clone())
            .with_scenarios_per_combination(2);

        assert_eq!(generator.prompts, prompts);
    }

    #[test]
    fn test_generator_generate_for() {
        let model = ModelId::new("test", "model");
        let generator = ScenarioGenerator::new(model).with_scenarios_per_combination(5);

        let scenarios = generator.generate_for(Modality::Run, Backend::Cpu, Format::Gguf);
        assert_eq!(scenarios.len(), 5);

        for s in &scenarios {
            assert_eq!(s.modality, Modality::Run);
            assert_eq!(s.backend, Backend::Cpu);
            assert_eq!(s.format, Format::Gguf);
        }
    }

    #[test]
    fn test_default_prompts_coverage() {
        let prompts = default_prompts();
        assert!(!prompts.is_empty());
        // Should have arithmetic prompts
        assert!(prompts.iter().any(|p| p.contains('+') || p.contains('*')));
        // Should have code prompts
        assert!(
            prompts
                .iter()
                .any(|p| p.starts_with("def ") || p.starts_with("fn "))
        );
        // Should have empty prompt for edge case
        assert!(prompts.iter().any(|p| p.is_empty()));
    }

    #[test]
    fn test_mqs_category_all_combinations() {
        let model = ModelId::new("test", "model");

        // Run GPU
        let run_gpu = QaScenario::new(
            model.clone(),
            Modality::Run,
            Backend::Gpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );
        assert_eq!(run_gpu.mqs_category(), "A2");

        // Chat CPU
        let chat_cpu = QaScenario::new(
            model.clone(),
            Modality::Chat,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );
        assert_eq!(chat_cpu.mqs_category(), "A3");

        // Serve GPU
        let serve_gpu = QaScenario::new(
            model,
            Modality::Serve,
            Backend::Gpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );
        assert_eq!(serve_gpu.mqs_category(), "A6");
    }

    #[test]
    fn test_scenario_clone() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            42,
        );

        let cloned = scenario.clone();
        assert_eq!(cloned.id, scenario.id);
        assert_eq!(cloned.seed, scenario.seed);
    }

    #[test]
    fn test_scenario_serialize() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );

        let json = serde_json::to_string(&scenario).expect("serialize");
        assert!(json.contains("\"modality\":\"run\""));
        assert!(json.contains("\"backend\":\"cpu\""));
    }

    #[test]
    fn test_apr_tool_all() {
        let all = AprTool::all();
        assert_eq!(all.len(), 10);
        assert!(all.contains(&AprTool::Run));
        assert!(all.contains(&AprTool::Canary));
    }

    #[test]
    fn test_apr_tool_command() {
        assert_eq!(AprTool::Run.command(), "run");
        assert_eq!(AprTool::Chat.command(), "chat");
        assert_eq!(AprTool::Serve.command(), "serve");
        assert_eq!(AprTool::Inspect.command(), "inspect");
        assert_eq!(AprTool::Validate.command(), "validate");
        assert_eq!(AprTool::Bench.command(), "bench");
        assert_eq!(AprTool::Profile.command(), "profile");
        assert_eq!(AprTool::Trace.command(), "trace");
        assert_eq!(AprTool::Check.command(), "check");
        assert_eq!(AprTool::Canary.command(), "canary");
    }

    #[test]
    fn test_apr_tool_requires_prompt() {
        assert!(AprTool::Run.requires_prompt());
        assert!(AprTool::Chat.requires_prompt());
        assert!(!AprTool::Serve.requires_prompt());
        assert!(!AprTool::Inspect.requires_prompt());
        assert!(!AprTool::Validate.requires_prompt());
        assert!(!AprTool::Bench.requires_prompt());
    }

    #[test]
    fn test_apr_tool_supports_trace() {
        assert!(AprTool::Run.supports_trace());
        assert!(AprTool::Trace.supports_trace());
        assert!(!AprTool::Chat.supports_trace());
        assert!(!AprTool::Serve.supports_trace());
    }

    #[test]
    fn test_apr_tool_display() {
        assert_eq!(format!("{}", AprTool::Run), "run");
        assert_eq!(format!("{}", AprTool::Profile), "profile");
        assert_eq!(format!("{}", AprTool::Canary), "canary");
    }

    #[test]
    fn test_format_extension() {
        assert_eq!(Format::Gguf.extension(), ".gguf");
        assert_eq!(Format::SafeTensors.extension(), ".safetensors");
        assert_eq!(Format::Apr.extension(), ".apr");
    }

    #[test]
    fn test_trace_level_all() {
        let all = TraceLevel::all();
        assert_eq!(all.len(), 4);
        assert!(all.contains(&TraceLevel::None));
        assert!(all.contains(&TraceLevel::Basic));
        assert!(all.contains(&TraceLevel::Layer));
        assert!(all.contains(&TraceLevel::Payload));
    }

    #[test]
    fn test_modality_command() {
        assert_eq!(Modality::Run.command(), "run");
        assert_eq!(Modality::Chat.command(), "chat");
        assert_eq!(Modality::Serve.command(), "serve");
    }

    #[test]
    fn test_scenario_debug() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "test".to_string(),
            0,
        );
        let debug_str = format!("{scenario:?}");
        assert!(debug_str.contains("QaScenario"));
    }

    #[test]
    fn test_scenario_generator_debug() {
        let model = ModelId::new("test", "model");
        let generator = ScenarioGenerator::new(model);
        let debug_str = format!("{generator:?}");
        assert!(debug_str.contains("ScenarioGenerator"));
    }

    #[test]
    fn test_escape_json_tab() {
        // Verify tabs don't cause issues
        let result = escape_json("hello\tworld");
        assert!(result.contains('\t'));
    }

    #[test]
    fn test_escape_prompt_no_quotes() {
        let result = escape_prompt("hello world");
        assert_eq!(result, "hello world");
    }

    #[test]
    fn test_scenario_to_command_with_basic_trace() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "Hello".to_string(),
            0,
        )
        .with_trace_level(TraceLevel::Basic);

        let cmd = scenario.to_command("model.gguf");
        assert!(cmd.contains("--trace"));
        assert!(cmd.contains("--trace-level basic"));
    }

    #[test]
    fn test_scenario_to_command_with_layer_trace() {
        let model = ModelId::new("test", "model");
        let scenario = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "Hello".to_string(),
            0,
        )
        .with_trace_level(TraceLevel::Layer);

        let cmd = scenario.to_command("model.gguf");
        assert!(cmd.contains("--trace-level layer"));
    }

    // --- Mutation-killing tests for mqs_category return values ---
    #[test]
    fn test_mqs_category_run_cpu_is_a1() {
        let model = ModelId::new("t", "m");
        let s = QaScenario::new(
            model,
            Modality::Run,
            Backend::Cpu,
            Format::Gguf,
            "x".into(),
            0,
        );
        assert_eq!(s.mqs_category(), "A1");
        assert_ne!(s.mqs_category(), "A2");
        assert_ne!(s.mqs_category(), "A3");
    }

    #[test]
    fn test_mqs_category_run_gpu_is_a2() {
        let model = ModelId::new("t", "m");
        let s = QaScenario::new(
            model,
            Modality::Run,
            Backend::Gpu,
            Format::Gguf,
            "x".into(),
            0,
        );
        assert_eq!(s.mqs_category(), "A2");
        assert_ne!(s.mqs_category(), "A1");
    }

    #[test]
    fn test_mqs_category_chat_cpu_is_a3() {
        let model = ModelId::new("t", "m");
        let s = QaScenario::new(
            model,
            Modality::Chat,
            Backend::Cpu,
            Format::Gguf,
            "x".into(),
            0,
        );
        assert_eq!(s.mqs_category(), "A3");
        assert_ne!(s.mqs_category(), "A4");
    }

    #[test]
    fn test_mqs_category_chat_gpu_is_a4() {
        let model = ModelId::new("t", "m");
        let s = QaScenario::new(
            model,
            Modality::Chat,
            Backend::Gpu,
            Format::Gguf,
            "x".into(),
            0,
        );
        assert_eq!(s.mqs_category(), "A4");
        assert_ne!(s.mqs_category(), "A3");
    }

    #[test]
    fn test_mqs_category_serve_cpu_is_a5() {
        let model = ModelId::new("t", "m");
        let s = QaScenario::new(
            model,
            Modality::Serve,
            Backend::Cpu,
            Format::Gguf,
            "x".into(),
            0,
        );
        assert_eq!(s.mqs_category(), "A5");
        assert_ne!(s.mqs_category(), "A6");
    }

    #[test]
    fn test_mqs_category_serve_gpu_is_a6() {
        let model = ModelId::new("t", "m");
        let s = QaScenario::new(
            model,
            Modality::Serve,
            Backend::Gpu,
            Format::Gguf,
            "x".into(),
            0,
        );
        assert_eq!(s.mqs_category(), "A6");
        assert_ne!(s.mqs_category(), "A5");
    }

    // --- Mutation-killing tests for Format::class return values ---
    #[test]
    fn test_format_class_gguf_is_char_a() {
        let class = Format::Gguf.class();
        assert_eq!(class, 'A');
        assert_ne!(class, 'B');
        assert_ne!(class, 'X');
    }

    #[test]
    fn test_format_class_apr_is_char_a() {
        let class = Format::Apr.class();
        assert_eq!(class, 'A');
        assert_ne!(class, 'B');
    }

    #[test]
    fn test_format_class_safetensors_is_char_b() {
        let class = Format::SafeTensors.class();
        assert_eq!(class, 'B');
        assert_ne!(class, 'A');
    }

    // --- Mutation-killing tests for escape_json ---
    #[test]
    fn test_escape_json_backslash_not_empty() {
        let result = escape_json("a\\b");
        assert!(!result.is_empty());
        assert_eq!(result, "a\\\\b");
        assert!(result.len() > "a\\b".len());
    }

    #[test]
    fn test_escape_json_quote_not_empty() {
        let result = escape_json("say \"hi\"");
        assert!(!result.is_empty());
        assert_eq!(result, "say \\\"hi\\\"");
    }

    #[test]
    fn test_escape_json_newline_not_empty() {
        let result = escape_json("line1\nline2");
        assert!(!result.is_empty());
        assert_eq!(result, "line1\\nline2");
        assert!(!result.contains('\n'));
    }

    #[test]
    fn test_escape_json_all_escapes_combined() {
        let result = escape_json("a\\b\"c\nd");
        assert_eq!(result, "a\\\\b\\\"c\\nd");
    }

    // --- Mutation-killing tests for escape_prompt ---
    #[test]
    fn test_escape_prompt_single_quote() {
        let result = escape_prompt("it's");
        assert!(!result.is_empty());
        assert_eq!(result, "it'\\''s");
        assert!(result.contains("'\\''"));
    }

    // --- Test that Backend::flag returns correct strings ---
    #[test]
    fn test_backend_cpu_flag_is_empty() {
        let flag = Backend::Cpu.flag();
        assert!(flag.is_empty());
        assert_eq!(flag, "");
    }

    #[test]
    fn test_backend_gpu_flag_is_gpu_option() {
        let flag = Backend::Gpu.flag();
        assert!(!flag.is_empty());
        assert_eq!(flag, "--gpu");
        assert!(flag.starts_with("--"));
    }

    // --- Test TraceLevel::value returns correct strings ---
    #[test]
    fn test_trace_level_none_value() {
        assert_eq!(TraceLevel::None.value(), "none");
        assert_ne!(TraceLevel::None.value(), "basic");
    }

    #[test]
    fn test_trace_level_basic_value() {
        assert_eq!(TraceLevel::Basic.value(), "basic");
        assert_ne!(TraceLevel::Basic.value(), "none");
    }

    #[test]
    fn test_trace_level_layer_value() {
        assert_eq!(TraceLevel::Layer.value(), "layer");
        assert_ne!(TraceLevel::Layer.value(), "payload");
    }

    #[test]
    fn test_trace_level_payload_value() {
        assert_eq!(TraceLevel::Payload.value(), "payload");
        assert_ne!(TraceLevel::Payload.value(), "layer");
    }
}