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
//! Proptest implementations for property-based testing
//!
//! Implements `Arbitrary` for QA types to enable fuzzing.

use crate::models::ModelId;
use crate::scenario::{Backend, Format, Modality, QaScenario, TraceLevel};
use proptest::prelude::*;

/// Strategy for generating model IDs
pub fn model_id_strategy() -> impl Strategy<Value = ModelId> {
    (
        prop::sample::select(vec![
            "Qwen",
            "meta-llama",
            "microsoft",
            "google",
            "mistralai",
            "deepseek-ai",
            "TinyLlama",
        ]),
        prop::sample::select(vec![
            "Qwen2.5-Coder-1.5B-Instruct",
            "Llama-3.2-1B-Instruct",
            "Phi-3-mini-4k-instruct",
            "gemma-2b-it",
            "Mistral-7B-Instruct-v0.3",
            "deepseek-coder-1.3b-instruct",
            "TinyLlama-1.1B-Chat-v1.0",
        ]),
    )
        .prop_map(|(org, name)| ModelId::new(org, name))
}

/// Strategy for generating modalities
pub fn modality_strategy() -> impl Strategy<Value = Modality> {
    prop_oneof![
        Just(Modality::Run),
        Just(Modality::Chat),
        Just(Modality::Serve),
    ]
}

/// Strategy for generating backends
pub fn backend_strategy() -> impl Strategy<Value = Backend> {
    prop_oneof![Just(Backend::Cpu), Just(Backend::Gpu),]
}

/// Strategy for generating formats
pub fn format_strategy() -> impl Strategy<Value = Format> {
    prop_oneof![
        Just(Format::Gguf),
        Just(Format::SafeTensors),
        Just(Format::Apr),
    ]
}

/// Strategy for generating trace levels
pub fn trace_level_strategy() -> impl Strategy<Value = TraceLevel> {
    prop_oneof![
        Just(TraceLevel::None),
        Just(TraceLevel::Basic),
        Just(TraceLevel::Layer),
        Just(TraceLevel::Payload),
    ]
}

/// Strategy for generating arithmetic prompts (verifiable)
pub fn arithmetic_prompt_strategy() -> impl Strategy<Value = String> {
    (
        1i32..100,
        1i32..100,
        prop::sample::select(vec!['+', '-', '*']),
    )
        .prop_map(|(a, b, op)| format!("What is {a}{op}{b}?"))
}

/// Strategy for generating code prompts
pub fn code_prompt_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        Just("def fibonacci(n):".to_string()),
        Just("fn main() {".to_string()),
        Just("async function fetch() {".to_string()),
        Just("class Person:".to_string()),
        Just("impl Iterator for".to_string()),
        Just("pub struct Config {".to_string()),
    ]
}

/// Strategy for generating edge case prompts
pub fn edge_case_prompt_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        Just(String::new()),                               // Empty
        Just(" ".to_string()),                             // Whitespace
        Just("\n\n\n".to_string()),                        // Newlines only
        Just("你好世界".to_string()),                      // Chinese
        Just("مرحبا بالعالم".to_string()),                 // Arabic (RTL)
        Just("🎉🚀💻".to_string()),                        // Emoji
        Just("a".repeat(10000)),                           // Very long
        Just("<script>alert('xss')</script>".to_string()), // XSS attempt
        Just("'; DROP TABLE users; --".to_string()),       // SQL injection
    ]
}

/// Strategy for generating any prompt
pub fn any_prompt_strategy() -> impl Strategy<Value = String> {
    prop_oneof![
        3 => arithmetic_prompt_strategy(),
        2 => code_prompt_strategy(),
        1 => edge_case_prompt_strategy(),
        2 => "[a-zA-Z0-9 ]{1,100}".prop_map(|s| s),
    ]
}

/// Strategy for generating complete scenarios
pub fn scenario_strategy() -> impl Strategy<Value = QaScenario> {
    (
        model_id_strategy(),
        modality_strategy(),
        backend_strategy(),
        format_strategy(),
        any_prompt_strategy(),
        0u64..1000,
    )
        .prop_map(|(model, modality, backend, format, prompt, seed)| {
            QaScenario::new(model, modality, backend, format, prompt, seed)
        })
}

/// Strategy for scenarios with specific model
pub fn scenario_for_model_strategy(model: ModelId) -> impl Strategy<Value = QaScenario> {
    (
        modality_strategy(),
        backend_strategy(),
        format_strategy(),
        any_prompt_strategy(),
        0u64..1000,
    )
        .prop_map(move |(modality, backend, format, prompt, seed)| {
            QaScenario::new(model.clone(), modality, backend, format, prompt, seed)
        })
}

/// Strategy for temperature values
pub fn temperature_strategy() -> impl Strategy<Value = f32> {
    prop_oneof![
        Just(0.0),   // Deterministic
        Just(0.7),   // Default
        Just(1.0),   // Creative
        0.0f32..2.0, // Random range
    ]
}

/// Strategy for max token counts
pub fn max_tokens_strategy() -> impl Strategy<Value = u32> {
    prop_oneof![
        Just(1u32), // Minimum
        Just(32),   // Default
        Just(128),  // Medium
        Just(512),  // Large
        Just(2048), // Very large
        1u32..4096, // Random range
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::strategy::ValueTree;
    use proptest::test_runner::TestRunner;

    #[test]
    fn test_model_id_strategy_generates_valid() {
        let mut runner = TestRunner::default();
        for _ in 0..100 {
            let model = model_id_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            assert!(!model.org.is_empty());
            assert!(!model.name.is_empty());
        }
    }

    #[test]
    fn test_scenario_strategy_generates_valid() {
        let mut runner = TestRunner::default();
        for _ in 0..100 {
            let scenario = scenario_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            assert!(!scenario.id.is_empty());
        }
    }

    proptest! {
        // Configure proptest with regression file support (F-PROP-006)
        // Failing cases are stored in proptest-regressions/ for reproducibility
        #![proptest_config(ProptestConfig {
            cases: 100,
            failure_persistence: Some(Box::new(proptest::test_runner::FileFailurePersistence::WithSource("proptest-regressions"))),
            ..ProptestConfig::default()
        })]

        #[test]
        fn prop_arithmetic_prompts_contain_operator(prompt in arithmetic_prompt_strategy()) {
            prop_assert!(
                prompt.contains('+') || prompt.contains('-') || prompt.contains('*'),
                "Prompt should contain arithmetic operator: {}", prompt
            );
        }

        #[test]
        fn prop_scenarios_have_valid_id(scenario in scenario_strategy()) {
            prop_assert!(!scenario.id.is_empty());
            prop_assert!(scenario.id.contains('_'));
        }

        #[test]
        fn prop_temperature_in_range(temp in temperature_strategy()) {
            prop_assert!(temp >= 0.0);
            prop_assert!(temp <= 2.0);
        }

        #[test]
        fn prop_max_tokens_positive(tokens in max_tokens_strategy()) {
            prop_assert!(tokens >= 1);
            prop_assert!(tokens <= 4096);
        }

        #[test]
        fn prop_scenario_command_is_valid(scenario in scenario_strategy()) {
            let cmd = scenario.to_command("model.gguf");
            prop_assert!(!cmd.is_empty());
            prop_assert!(cmd.contains("apr"));
        }
    }

    #[test]
    fn test_modality_strategy_generates_valid() {
        let mut runner = TestRunner::default();
        for _ in 0..50 {
            let modality = modality_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            // Should be one of the valid modalities
            assert!(matches!(
                modality,
                Modality::Run | Modality::Chat | Modality::Serve
            ));
        }
    }

    #[test]
    fn test_backend_strategy_generates_valid() {
        let mut runner = TestRunner::default();
        for _ in 0..50 {
            let backend = backend_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            assert!(matches!(backend, Backend::Cpu | Backend::Gpu));
        }
    }

    #[test]
    fn test_format_strategy_generates_valid() {
        let mut runner = TestRunner::default();
        for _ in 0..50 {
            let format = format_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            assert!(matches!(
                format,
                Format::Gguf | Format::SafeTensors | Format::Apr
            ));
        }
    }

    #[test]
    fn test_trace_level_strategy_generates_valid() {
        let mut runner = TestRunner::default();
        for _ in 0..50 {
            let level = trace_level_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            assert!(matches!(
                level,
                TraceLevel::None | TraceLevel::Basic | TraceLevel::Layer | TraceLevel::Payload
            ));
        }
    }

    #[test]
    fn test_code_prompt_strategy_generates_code() {
        let mut runner = TestRunner::default();
        for _ in 0..50 {
            let prompt = code_prompt_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            // Should be a code-like prompt
            assert!(
                prompt.contains("def ")
                    || prompt.contains("fn ")
                    || prompt.contains("async ")
                    || prompt.contains("class ")
                    || prompt.contains("impl ")
                    || prompt.contains("pub ")
            );
        }
    }

    #[test]
    fn test_edge_case_prompt_strategy_generates_edge_cases() {
        let mut runner = TestRunner::default();
        let mut seen_empty = false;
        let mut seen_unicode = false;
        let mut seen_long = false;

        for _ in 0..100 {
            let prompt = edge_case_prompt_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();

            if prompt.is_empty() || prompt.trim().is_empty() {
                seen_empty = true;
            }
            if prompt.contains("你好") || prompt.contains("مرحبا") || prompt.contains("🎉")
            {
                seen_unicode = true;
            }
            if prompt.len() > 1000 {
                seen_long = true;
            }
        }

        // Should have seen at least some variety
        assert!(seen_empty || seen_unicode || seen_long);
    }

    #[test]
    fn test_scenario_for_model_strategy() {
        let model = ModelId::new("test", "model");
        let mut runner = TestRunner::default();

        for _ in 0..50 {
            let scenario = scenario_for_model_strategy(model.clone())
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();

            // Model should match
            assert_eq!(scenario.model.org, "test");
            assert_eq!(scenario.model.name, "model");
        }
    }

    #[test]
    fn test_temperature_strategy_generates_valid_range() {
        let mut runner = TestRunner::default();
        for _ in 0..50 {
            let temp = temperature_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            assert!(temp >= 0.0);
            assert!(temp <= 2.0);
        }
    }

    #[test]
    fn test_max_tokens_strategy_generates_valid_range() {
        let mut runner = TestRunner::default();
        for _ in 0..50 {
            let tokens = max_tokens_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();
            assert!(tokens >= 1);
            assert!(tokens <= 4096);
        }
    }

    #[test]
    fn test_any_prompt_strategy_generates_variety() {
        let mut runner = TestRunner::default();
        let mut has_arithmetic = false;
        let mut has_code = false;
        let mut has_other = false;

        for _ in 0..100 {
            let prompt = any_prompt_strategy()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();

            if prompt.contains('+') || prompt.contains('-') || prompt.contains('*') {
                has_arithmetic = true;
            } else if prompt.contains("def ") || prompt.contains("fn ") || prompt.contains("class ")
            {
                has_code = true;
            } else {
                has_other = true;
            }
        }

        // Should generate variety
        assert!(has_arithmetic || has_code || has_other);
    }
}