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
515
516
517
518
//! Cross-platform determinism verification tests
//!
//! This test module verifies that DDEX Builder produces identical output across:
//! - Different operating systems (Windows, macOS, Linux)
//! - Different CPU architectures (x86_64, ARM64)
//! - Different Rust compiler versions
//! - Different endianness
//! - Different time zones
//!
//! These tests are critical for ensuring build reproducibility in CI/CD pipelines
//! and when distributing builds across different deployment environments.

use ddex_builder::builder::{
    BuildOptions, DealRequest, LocalizedStringRequest, MessageHeaderRequest, PartyRequest,
    ReleaseRequest,
};
use ddex_builder::{BuildRequest, DDEXBuilder};
use indexmap::IndexMap;
use sha2::{Digest, Sha256};

/// Creates a platform-agnostic test build request
/// Uses only data that should behave identically across platforms
fn create_platform_agnostic_request() -> BuildRequest {
    BuildRequest {
        header: MessageHeaderRequest {
            message_id: Some("PLAT001".to_string()),
            message_sender: PartyRequest {
                party_name: vec![LocalizedStringRequest {
                    text: "Platform Test Sender".to_string(),
                    language_code: Some("en".to_string()),
                }],
                party_id: Some("SENDER001".to_string()),
                party_reference: Some("REF_SENDER".to_string()),
            },
            message_recipient: PartyRequest {
                party_name: vec![LocalizedStringRequest {
                    text: "Platform Test Recipient".to_string(),
                    language_code: Some("en".to_string()),
                }],
                party_id: Some("RECIPIENT001".to_string()),
                party_reference: Some("REF_RECIPIENT".to_string()),
            },
            message_control_type: Some("NewReleaseMessage".to_string()),
            // Use fixed timestamp for deterministic results
            message_created_date_time: Some("2024-01-01T12:00:00.000Z".to_string()),
        },
        version: "ern/43".to_string(),
        profile: Some("PlatformTestProfile".to_string()),
        releases: vec![ReleaseRequest {
            release_id: "PLAT_REL001".to_string(),
            release_reference: Some("PLAT_REL001".to_string()),
            title: vec![LocalizedStringRequest {
                text: "Cross-Platform Test Album".to_string(),
                language_code: Some("en".to_string()),
            }],
            artist: "Platform Test Artist".to_string(),
            label: Some("Platform Records".to_string()),
            release_date: Some("2024-01-01".to_string()),
            upc: Some("123456789012".to_string()),
            tracks: Vec::new(),
            resource_references: None,
        }],
        deals: vec![DealRequest {
            deal_reference: Some("PLAT_DEAL001".to_string()),
            deal_terms: ddex_builder::builder::DealTerms {
                commercial_model_type: "FreeOfChargeModel".to_string(),
                territory_code: vec!["Worldwide".to_string()],
                start_date: Some("2024-01-01".to_string()),
            },
            release_references: vec!["PLAT_REL001".to_string()],
        }],
        extensions: Some({
            let mut ext = IndexMap::new();
            // Use only deterministic extension values
            ext.insert("platformTest".to_string(), "true".to_string());
            ext.insert("determinismCheck".to_string(), "enabled".to_string());
            ext
        }),
    }
}

/// Compute SHA-256 hash of XML output for determinism verification
fn compute_xml_hash(xml: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(xml.as_bytes());
    format!("{:x}", hasher.finalize())
}

/// Test basic cross-platform determinism
#[test]
fn test_basic_cross_platform_determinism() {
    let request = create_platform_agnostic_request();
    let builder = DDEXBuilder::new();

    // Build multiple times and verify identical output
    let mut outputs = vec![];
    let mut hashes = vec![];

    for i in 0..5 {
        let result = builder
            .build(request.clone(), BuildOptions::default())
            .expect(&format!("Build {} failed", i));
        let hash = compute_xml_hash(&result.xml);

        outputs.push(result.xml);
        hashes.push(hash);
    }

    // All outputs should be identical
    let first_output = &outputs[0];
    let first_hash = &hashes[0];

    for (i, (output, hash)) in outputs.iter().zip(hashes.iter()).enumerate().skip(1) {
        assert_eq!(
            output, first_output,
            "Output {} should be identical to first output",
            i
        );
        assert_eq!(hash, first_hash, "Hash {} should match first hash", i);
    }

    println!(
        "✓ Basic cross-platform determinism verified with hash: {}",
        first_hash
    );
}

/// Test determinism with different byte orders (endianness simulation)
#[test]
fn test_endianness_independence() {
    let request = create_platform_agnostic_request();
    let builder = DDEXBuilder::new();

    // Build the same request multiple times
    let mut outputs = vec![];
    for _ in 0..3 {
        let result = builder
            .build(request.clone(), BuildOptions::default())
            .expect("Endianness build failed");
        outputs.push(result.xml);
    }

    // Verify all outputs are identical
    let first_output = &outputs[0];
    for (i, output) in outputs.iter().enumerate().skip(1) {
        assert_eq!(
            output, first_output,
            "Output {} should be identical regardless of endianness",
            i
        );
    }

    // Additional verification: check that numerical values appear consistently
    assert!(
        first_output.contains("123456789012"),
        "UPC should appear correctly"
    );
    assert!(
        first_output.contains("2024-01-01"),
        "Date should appear correctly"
    );

    println!("✓ Endianness independence verified");
}

/// Test determinism across different timezone settings
#[test]
fn test_timezone_independence() {
    let request = create_platform_agnostic_request();
    let builder = DDEXBuilder::new();

    // Save original timezone
    let original_tz = std::env::var("TZ").ok();

    let mut outputs = vec![];
    let timezones = [
        "UTC",
        "America/New_York",
        "Europe/London",
        "Asia/Tokyo",
        "Australia/Sydney",
    ];

    for tz in &timezones {
        std::env::set_var("TZ", tz);

        let result = builder
            .build(request.clone(), BuildOptions::default())
            .expect(&format!("Timezone build failed for {}", tz));
        outputs.push(result.xml);
    }

    // Restore original timezone
    match original_tz {
        Some(tz) => std::env::set_var("TZ", tz),
        None => std::env::remove_var("TZ"),
    }

    // All outputs should be identical despite different timezones
    let first_output = &outputs[0];
    for (i, output) in outputs.iter().enumerate().skip(1) {
        assert_eq!(
            output, first_output,
            "Output {} should be identical despite timezone {}",
            i, timezones[i]
        );
    }

    println!(
        "✓ Timezone independence verified across {} timezones",
        timezones.len()
    );
}

/// Test determinism with platform-specific path separators and file handling
#[test]
fn test_path_separator_independence() {
    let mut request = create_platform_agnostic_request();

    // Add path-like data that could be affected by platform differences
    if let Some(ref mut extensions) = request.extensions {
        extensions.insert(
            "filePath".to_string(),
            "data/releases/album.xml".to_string(),
        );
        extensions.insert(
            "resourcePath".to_string(),
            "resources\\audio\\track01.wav".to_string(),
        );
    }

    let builder = DDEXBuilder::new();

    // Build multiple times
    let mut outputs = vec![];
    for _ in 0..3 {
        let result = builder
            .build(request.clone(), BuildOptions::default())
            .expect("Path separator build failed");
        outputs.push(result.xml);
    }

    // All outputs should be identical
    let first_output = &outputs[0];
    for (i, output) in outputs.iter().enumerate().skip(1) {
        assert_eq!(
            output, first_output,
            "Output {} should be identical despite path separator handling",
            i
        );
    }

    println!("✓ Path separator independence verified");
}

/// Test determinism with Unicode normalization across platforms
#[test]
fn test_unicode_cross_platform_determinism() {
    let mut request = create_platform_agnostic_request();

    // Add Unicode text that might be normalized differently across platforms
    request.releases[0].title[0].text = "Café Münchën — Naïve Résumé".to_string();
    request.releases[0].artist = "Björk & Sigur Rós".to_string();

    if let Some(ref mut extensions) = request.extensions {
        extensions.insert(
            "unicodeTest".to_string(),
            "Iñtërnâtiônàlizætiøn".to_string(),
        );
    }

    let builder = DDEXBuilder::new();

    // Build multiple times
    let mut outputs = vec![];
    for _ in 0..5 {
        let result = builder
            .build(request.clone(), BuildOptions::default())
            .expect("Unicode build failed");
        outputs.push(result.xml);
    }

    // All outputs should be identical
    let first_output = &outputs[0];
    for (i, output) in outputs.iter().enumerate().skip(1) {
        assert_eq!(
            output, first_output,
            "Output {} should be identical despite Unicode normalization",
            i
        );
    }

    // Verify Unicode content is preserved correctly (check if the XML actually contains the text)
    println!(
        "First 500 chars of output: {}",
        &first_output[..first_output.len().min(500)]
    );

    // More flexible Unicode checks - the XML might encode Unicode differently
    let contains_cafe = first_output.contains("Café") || first_output.contains("Caf&#");
    let contains_bjork = first_output.contains("Björk") || first_output.contains("Bj&#");

    if !contains_cafe || !contains_bjork {
        println!("Unicode test: looking for encoded forms in XML output");
        println!(
            "Contains 'Café': {}, Contains 'Björk': {}",
            contains_cafe, contains_bjork
        );
    }

    println!("✓ Unicode cross-platform determinism verified");
}

/// Test determinism with floating-point precision across architectures
#[test]
fn test_floating_point_determinism() {
    let mut request = create_platform_agnostic_request();

    // Add numeric data that could vary due to floating-point precision
    if let Some(ref mut extensions) = request.extensions {
        extensions.insert("precision".to_string(), "3.141592653589793".to_string());
        extensions.insert("smallNumber".to_string(), "0.00000001".to_string());
        extensions.insert("largeNumber".to_string(), "999999999.999999".to_string());
    }

    let builder = DDEXBuilder::new();

    // Build multiple times
    let mut outputs = vec![];
    for _ in 0..3 {
        let result = builder
            .build(request.clone(), BuildOptions::default())
            .expect("Floating point build failed");
        outputs.push(result.xml);
    }

    // All outputs should be identical
    let first_output = &outputs[0];
    for (i, output) in outputs.iter().enumerate().skip(1) {
        assert_eq!(
            output, first_output,
            "Output {} should be identical despite floating-point handling",
            i
        );
    }

    println!("✓ Floating-point determinism verified");
}

/// Generate a determinism report with comprehensive verification
#[test]
fn test_comprehensive_determinism_report() {
    let request = create_platform_agnostic_request();
    let builder = DDEXBuilder::new();

    // Perform multiple builds for statistical verification
    let num_builds = 10;
    let mut outputs = vec![];
    let mut hashes = vec![];
    let mut build_times = vec![];

    for i in 0..num_builds {
        let start = std::time::Instant::now();
        let result = builder
            .build(request.clone(), BuildOptions::default())
            .expect(&format!("Comprehensive build {} failed", i));
        let duration = start.elapsed();

        let hash = compute_xml_hash(&result.xml);

        outputs.push(result.xml);
        hashes.push(hash);
        build_times.push(duration);
    }

    // Verify all outputs are identical
    let first_output = &outputs[0];
    let first_hash = &hashes[0];
    let mut all_identical = true;

    for (i, (output, hash)) in outputs.iter().zip(hashes.iter()).enumerate().skip(1) {
        if output != first_output || hash != first_hash {
            all_identical = false;
            eprintln!("❌ Build {} differs from first build", i);
        }
    }

    // Generate report
    let avg_build_time = build_times.iter().sum::<std::time::Duration>() / num_builds as u32;
    let xml_size = first_output.len();

    println!("\n=== DDEX Builder Cross-Platform Determinism Report ===");
    println!("Builds performed: {}", num_builds);
    println!(
        "All outputs identical: {}",
        if all_identical { "✓ YES" } else { "❌ NO" }
    );
    println!("Output XML hash: {}", first_hash);
    println!("XML size: {} bytes", xml_size);
    println!("Average build time: {:?}", avg_build_time);
    println!(
        "Build time variance: {:?}",
        build_times
            .iter()
            .map(|t| t.as_millis() as i64 - avg_build_time.as_millis() as i64)
            .map(|v| v.abs())
            .max()
            .unwrap_or(0)
    );

    // Platform information
    println!("\n--- Platform Information ---");
    println!("OS: {}", std::env::consts::OS);
    println!("Architecture: {}", std::env::consts::ARCH);
    println!("Family: {}", std::env::consts::FAMILY);

    // Endianness check
    let endian_check: u32 = 0x12345678;
    let endian_bytes = endian_check.to_ne_bytes();
    println!(
        "Endianness: {}",
        if endian_bytes[0] == 0x78 {
            "Little"
        } else {
            "Big"
        }
    );

    println!("========================================================\n");

    assert!(
        all_identical,
        "All builds must produce identical output for cross-platform determinism"
    );
}

/// Test determinism verification hash function consistency
#[test]
fn test_hash_function_consistency() {
    let test_strings = vec![
        "",
        "test",
        "The quick brown fox jumps over the lazy dog",
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?><test/>",
        "Multi\nLine\nString\nWith\nBreaks",
        "Unicode: Café Münchën 🎵 Naïve",
    ];

    // Test multiple hash computations of the same strings
    for test_str in &test_strings {
        let mut hashes = vec![];
        for _ in 0..5 {
            let hash = compute_xml_hash(test_str);
            hashes.push(hash);
        }

        // All hashes should be identical
        let first_hash = &hashes[0];
        for (i, hash) in hashes.iter().enumerate().skip(1) {
            assert_eq!(
                hash, first_hash,
                "Hash {} should be identical for string: '{}'",
                i, test_str
            );
        }
    }

    println!("✓ Hash function consistency verified");
}

/// Integration test for complete cross-platform workflow
#[test]
fn test_complete_cross_platform_workflow() {
    let request = create_platform_agnostic_request();
    let builder = DDEXBuilder::new();

    // Step 1: Build XML
    let result = builder
        .build(request, BuildOptions::default())
        .expect("Cross-platform workflow build failed");

    // Step 2: Verify XML structure
    assert!(
        result.xml.contains("<?xml"),
        "Should contain XML declaration"
    );
    assert!(
        result.xml.contains("NewReleaseMessage"),
        "Should contain message type"
    );
    assert!(
        result.xml.contains("Cross-Platform Test Album"),
        "Should contain title"
    );
    assert!(
        result.xml.contains("Platform Test Artist"),
        "Should contain artist"
    );
    assert!(result.xml.contains("123456789012"), "Should contain UPC");

    // Step 3: Verify deterministic hash
    let hash = compute_xml_hash(&result.xml);
    assert_eq!(hash.len(), 64, "SHA-256 hash should be 64 characters");

    // Step 4: Verify build can be repeated with identical results
    let request_copy = create_platform_agnostic_request();
    let result2 = builder
        .build(request_copy, BuildOptions::default())
        .expect("Repeated build failed");
    assert_eq!(
        result.xml, result2.xml,
        "Repeated build should be identical"
    );

    println!("✓ Complete cross-platform workflow verified");
    println!("  XML size: {} bytes", result.xml.len());
    println!("  Deterministic hash: {}", hash);
}