ddex-builder 0.4.5

Deterministic DDEX XML builder with smart normalization
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
//! Property-Based Testing for Determinism Guarantees
//! 
//! This module uses the proptest crate to generate random valid DDEX structures
//! and verify that building the same structure multiple times always produces
//! identical XML output across different platforms and configurations.

use ddex_builder::{Builder, BuildRequest};
use proptest::prelude::*;
use std::collections::HashMap;
use std::time::Instant;

pub mod generators;
pub mod strategies;
pub mod validation;

/// Configuration for determinism testing
#[derive(Debug, Clone)]
pub struct DeterminismTestConfig {
    /// Number of iterations per test
    pub iterations: usize,
    /// Test across different thread counts
    pub test_multithreading: bool,
    /// Test with different memory constraints
    pub test_memory_constraints: bool,
    /// Test with different random seeds
    pub test_random_seeds: bool,
    /// Maximum structure complexity
    pub max_complexity: usize,
}

impl Default for DeterminismTestConfig {
    fn default() -> Self {
        Self {
            iterations: 100,
            test_multithreading: true,
            test_memory_constraints: true,
            test_random_seeds: true,
            max_complexity: 1000,
        }
    }
}

/// Results from determinism testing
#[derive(Debug, Clone)]
pub struct DeterminismTestResult {
    pub test_name: String,
    pub total_iterations: usize,
    pub successful_iterations: usize,
    pub failed_iterations: usize,
    pub unique_outputs: usize,
    pub build_times_ms: Vec<u64>,
    pub output_sizes: Vec<usize>,
    pub errors: Vec<String>,
}

impl DeterminismTestResult {
    pub fn new(test_name: String) -> Self {
        Self {
            test_name,
            total_iterations: 0,
            successful_iterations: 0,
            failed_iterations: 0,
            unique_outputs: 0,
            build_times_ms: Vec::new(),
            output_sizes: Vec::new(),
            errors: Vec::new(),
        }
    }
    
    pub fn is_deterministic(&self) -> bool {
        self.unique_outputs <= 1 && self.successful_iterations > 0
    }
    
    pub fn success_rate(&self) -> f64 {
        if self.total_iterations == 0 {
            0.0
        } else {
            self.successful_iterations as f64 / self.total_iterations as f64
        }
    }
    
    pub fn average_build_time_ms(&self) -> f64 {
        if self.build_times_ms.is_empty() {
            0.0
        } else {
            self.build_times_ms.iter().sum::<u64>() as f64 / self.build_times_ms.len() as f64
        }
    }
    
    pub fn generate_report(&self) -> String {
        format!(
            "Determinism Test Report: {}\n\
            =============================\n\
            Total iterations: {}\n\
            Successful iterations: {}\n\
            Failed iterations: {}\n\
            Unique outputs: {}\n\
            Deterministic: {}\n\
            Success rate: {:.2}%\n\
            Average build time: {:.2}ms\n\
            Average output size: {:.0} bytes\n\
            Errors: {}\n",
            self.test_name,
            self.total_iterations,
            self.successful_iterations, 
            self.failed_iterations,
            self.unique_outputs,
            if self.is_deterministic() { "YES" } else { "NO" },
            self.success_rate() * 100.0,
            self.average_build_time_ms(),
            if self.output_sizes.is_empty() { 0.0 } else { 
                self.output_sizes.iter().sum::<usize>() as f64 / self.output_sizes.len() as f64 
            },
            self.errors.len()
        )
    }
}

/// Main determinism test runner
pub struct DeterminismTestRunner {
    config: DeterminismTestConfig,
}

impl DeterminismTestRunner {
    pub fn new(config: DeterminismTestConfig) -> Self {
        Self { config }
    }
    
    /// Run comprehensive determinism tests
    pub async fn run_all_tests(&self) -> Result<Vec<DeterminismTestResult>, Box<dyn std::error::Error>> {
        let mut results = Vec::new();
        
        // Test basic determinism
        results.push(self.test_basic_determinism().await?);
        
        // Test complex structures
        results.push(self.test_complex_structure_determinism().await?);
        
        // Test concurrent building
        if self.config.test_multithreading {
            results.push(self.test_concurrent_determinism().await?);
        }
        
        // Test memory-constrained determinism
        if self.config.test_memory_constraints {
            results.push(self.test_memory_constrained_determinism().await?);
        }
        
        // Test with different random seeds
        if self.config.test_random_seeds {
            results.push(self.test_seeded_determinism().await?);
        }
        
        Ok(results)
    }
    
    /// Test basic determinism with simple structures
    pub async fn test_basic_determinism(&self) -> Result<DeterminismTestResult, Box<dyn std::error::Error>> {
        let mut result = DeterminismTestResult::new("Basic Determinism".to_string());
        let mut unique_outputs = HashMap::new();
        
        for _ in 0..self.config.iterations {
            result.total_iterations += 1;
            
            // Generate a simple DDEX structure
            let build_request = self.generate_simple_build_request();
            
            // Build XML multiple times
            let start_time = Instant::now();
            match self.build_xml(&build_request) {
                Ok(xml) => {
                    let build_time = start_time.elapsed().as_millis() as u64;
                    
                    result.successful_iterations += 1;
                    result.build_times_ms.push(build_time);
                    result.output_sizes.push(xml.len());
                    
                    // Track unique outputs
                    let output_hash = self.hash_xml(&xml);
                    *unique_outputs.entry(output_hash).or_insert(0) += 1;
                },
                Err(e) => {
                    result.failed_iterations += 1;
                    result.errors.push(e.to_string());
                }
            }
        }
        
        result.unique_outputs = unique_outputs.len();
        Ok(result)
    }
    
    /// Test determinism with complex nested structures
    pub async fn test_complex_structure_determinism(&self) -> Result<DeterminismTestResult, Box<dyn std::error::Error>> {
        let mut result = DeterminismTestResult::new("Complex Structure Determinism".to_string());
        let mut unique_outputs = HashMap::new();
        
        for _ in 0..self.config.iterations {
            result.total_iterations += 1;
            
            // Generate a complex DDEX structure
            let build_request = self.generate_complex_build_request();
            
            let start_time = Instant::now();
            match self.build_xml(&build_request) {
                Ok(xml) => {
                    let build_time = start_time.elapsed().as_millis() as u64;
                    
                    result.successful_iterations += 1;
                    result.build_times_ms.push(build_time);
                    result.output_sizes.push(xml.len());
                    
                    let output_hash = self.hash_xml(&xml);
                    *unique_outputs.entry(output_hash).or_insert(0) += 1;
                },
                Err(e) => {
                    result.failed_iterations += 1;
                    result.errors.push(e.to_string());
                }
            }
        }
        
        result.unique_outputs = unique_outputs.len();
        Ok(result)
    }
    
    /// Test concurrent building for determinism
    pub async fn test_concurrent_determinism(&self) -> Result<DeterminismTestResult, Box<dyn std::error::Error>> {
        let mut result = DeterminismTestResult::new("Concurrent Determinism".to_string());
        let mut unique_outputs = HashMap::new();
        
        // Generate test request once
        let build_request = self.generate_simple_build_request();
        
        // Build the same request concurrently multiple times
        let mut tasks = Vec::new();
        
        for _ in 0..self.config.iterations {
            let req = build_request.clone();
            let task = tokio::spawn(async move {
                let start_time = Instant::now();
                let builder = Builder::new();
                let xml_result = Self::build_xml_static(&builder, &req);
                let build_time = start_time.elapsed().as_millis() as u64;
                (xml_result, build_time)
            });
            tasks.push(task);
        }
        
        // Collect results
        for task in tasks {
            result.total_iterations += 1;
            
            match task.await {
                Ok((Ok(xml), build_time)) => {
                    result.successful_iterations += 1;
                    result.build_times_ms.push(build_time);
                    result.output_sizes.push(xml.len());
                    
                    let output_hash = self.hash_xml(&xml);
                    *unique_outputs.entry(output_hash).or_insert(0) += 1;
                },
                Ok((Err(e), _)) => {
                    result.failed_iterations += 1;
                    result.errors.push(e.to_string());
                },
                Err(e) => {
                    result.failed_iterations += 1;
                    result.errors.push(format!("Task failed: {}", e));
                }
            }
        }
        
        result.unique_outputs = unique_outputs.len();
        Ok(result)
    }
    
    /// Test determinism under memory constraints
    pub async fn test_memory_constrained_determinism(&self) -> Result<DeterminismTestResult, Box<dyn std::error::Error>> {
        let mut result = DeterminismTestResult::new("Memory-Constrained Determinism".to_string());
        let mut unique_outputs = HashMap::new();
        
        for _ in 0..self.config.iterations {
            result.total_iterations += 1;
            
            // Force some memory pressure (simplified simulation)
            let _memory_pressure: Vec<Vec<u8>> = (0..100)
                .map(|_| vec![0u8; 1024])
                .collect();
            
            let build_request = self.generate_simple_build_request();
            
            let start_time = Instant::now();
            match self.build_xml(&build_request) {
                Ok(xml) => {
                    let build_time = start_time.elapsed().as_millis() as u64;
                    
                    result.successful_iterations += 1;
                    result.build_times_ms.push(build_time);
                    result.output_sizes.push(xml.len());
                    
                    let output_hash = self.hash_xml(&xml);
                    *unique_outputs.entry(output_hash).or_insert(0) += 1;
                },
                Err(e) => {
                    result.failed_iterations += 1;
                    result.errors.push(e.to_string());
                }
            }
        }
        
        result.unique_outputs = unique_outputs.len();
        Ok(result)
    }
    
    /// Test determinism with different random seeds
    pub async fn test_seeded_determinism(&self) -> Result<DeterminismTestResult, Box<dyn std::error::Error>> {
        let mut result = DeterminismTestResult::new("Seeded Determinism".to_string());
        let mut unique_outputs = HashMap::new();
        
        // Test the same logical structure with different random elements (like IDs)
        let base_request = self.generate_simple_build_request();
        
        for seed in 0..self.config.iterations {
            result.total_iterations += 1;
            
            // Create a variant with seeded randomness
            let build_request = self.generate_seeded_variant(&base_request, seed as u64);
            
            let start_time = Instant::now();
            match self.build_xml(&build_request) {
                Ok(xml) => {
                    let build_time = start_time.elapsed().as_millis() as u64;
                    
                    result.successful_iterations += 1;
                    result.build_times_ms.push(build_time);
                    result.output_sizes.push(xml.len());
                    
                    let output_hash = self.hash_xml(&xml);
                    *unique_outputs.entry(output_hash).or_insert(0) += 1;
                },
                Err(e) => {
                    result.failed_iterations += 1;
                    result.errors.push(e.to_string());
                }
            }
        }
        
        result.unique_outputs = unique_outputs.len();
        Ok(result)
    }
    
    /// Build XML from a build request
    fn build_xml(&self, request: &BuildRequest) -> Result<String, Box<dyn std::error::Error>> {
        let builder = Builder::new();
        Self::build_xml_static(&builder, request)
    }
    
    /// Static version for use in async tasks
    fn build_xml_static(builder: &Builder, request: &BuildRequest) -> Result<String, Box<dyn std::error::Error>> {
        // This needs to be updated to match the actual Builder API
        Ok(format!("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test>{}</test>", request.get_id()))
    }
    
    /// Generate a simple build request for testing
    fn generate_simple_build_request(&self) -> BuildRequest {
        // This is a placeholder - needs to be implemented based on actual BuildRequest API
        BuildRequest::new_with_id("test_simple".to_string())
    }
    
    /// Generate a complex build request for testing
    fn generate_complex_build_request(&self) -> BuildRequest {
        // This is a placeholder - needs to be implemented based on actual BuildRequest API
        BuildRequest::new_with_id("test_complex".to_string())
    }
    
    /// Generate a seeded variant of a build request
    fn generate_seeded_variant(&self, base: &BuildRequest, seed: u64) -> BuildRequest {
        // This is a placeholder - needs to be implemented based on actual BuildRequest API
        BuildRequest::new_with_id(format!("test_seeded_{}", seed))
    }
    
    /// Hash XML content for comparison
    fn hash_xml(&self, xml: &str) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};
        
        let mut hasher = DefaultHasher::new();
        xml.hash(&mut hasher);
        hasher.finish()
    }
}

/// Placeholder BuildRequest implementation for testing
impl BuildRequest {
    pub fn new_with_id(id: String) -> Self {
        Self { id }
    }
    
    pub fn get_id(&self) -> &str {
        &self.id
    }
}

/// Simple BuildRequest struct for testing
pub struct BuildRequest {
    id: String,
}

impl Clone for BuildRequest {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
        }
    }
}

// Property-based test strategies using proptest
pub fn ddex_message_strategy() -> impl Strategy<Value = DdexMessage> {
    prop::collection::vec(sound_recording_strategy(), 1..10)
        .prop_map(|resources| DdexMessage {
            message_id: format!("MSG_{}", uuid::Uuid::new_v4()),
            resources,
        })
}

pub fn sound_recording_strategy() -> impl Strategy<Value = SoundRecording> {
    (
        "[A-Z0-9]{1,50}",
        "[A-Za-z0-9 ]{1,100}",
        prop::option::of("PT[0-9]{1,2}M[0-9]{1,2}S"),
    ).prop_map(|(id, title, duration)| SoundRecording {
        resource_id: id,
        title,
        duration,
    })
}

/// Test data structures
#[derive(Debug, Clone)]
pub struct DdexMessage {
    pub message_id: String,
    pub resources: Vec<SoundRecording>,
}

#[derive(Debug, Clone)]
pub struct SoundRecording {
    pub resource_id: String,
    pub title: String,
    pub duration: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_determinism_result_creation() {
        let result = DeterminismTestResult::new("Test".to_string());
        assert_eq!(result.test_name, "Test");
        assert_eq!(result.total_iterations, 0);
        assert!(!result.is_deterministic()); // No successful iterations yet
    }
    
    #[test]
    fn test_determinism_metrics() {
        let mut result = DeterminismTestResult::new("Test".to_string());
        result.total_iterations = 10;
        result.successful_iterations = 8;
        result.failed_iterations = 2;
        result.unique_outputs = 1;
        result.build_times_ms = vec![10, 15, 12, 8, 20, 11, 9, 14];
        
        assert!(result.is_deterministic());
        assert_eq!(result.success_rate(), 0.8);
        assert_eq!(result.average_build_time_ms(), 12.375);
    }
    
    #[tokio::test]
    async fn test_basic_determinism_runner() {
        let config = DeterminismTestConfig {
            iterations: 5,
            test_multithreading: false,
            test_memory_constraints: false,
            test_random_seeds: false,
            max_complexity: 10,
        };
        
        let runner = DeterminismTestRunner::new(config);
        let result = runner.test_basic_determinism().await.unwrap();
        
        assert!(result.total_iterations > 0);
        // The test might fail due to placeholder implementation, but structure should be correct
    }
    
    proptest! {
        #[test]
        fn test_property_based_determinism(message in ddex_message_strategy()) {
            // This test would verify that the same message always produces the same XML
            let message_id = &message.message_id;
            assert!(!message_id.is_empty());
            assert!(message.resources.len() > 0);
        }
        
        #[test] 
        fn test_sound_recording_properties(recording in sound_recording_strategy()) {
            // Verify generated sound recordings have valid properties
            assert!(!recording.resource_id.is_empty());
            assert!(!recording.title.is_empty());
            assert!(recording.resource_id.len() <= 50);
            assert!(recording.title.len() <= 100);
        }
    }
}