dx-serializer 0.1.0

A token-efficient serialization format for LLM context windows with high-performance binary encoding
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
//! Property-based tests for LLM format round-trip
//!
//! **Feature: serializer-production-hardening, Property 1: LLM Format Round-Trip Preservation**
//! **Validates: Requirements 1.2, 1.3, 1.5, 1.6**
//!
//! For any valid DxDocument, serializing to LLM format and deserializing back
//! SHALL produce an equivalent document (context keys and values preserved).
//!
//! ## Proptest Generators
//!
//! This module provides comprehensive proptest strategies for generating valid
//! DxDocument structures:
//!
//! - [`arb_dx_llm_value`] - Generates all DxLlmValue variants (Str, Num, Bool, Null, Arr, Ref)
//! - [`arb_dx_section`] - Generates valid DxSection with schema and rows
//! - [`arb_dx_document`] - Generates complete DxDocument with context, sections, and refs
//!
//! ### Constraints on Generated Values
//!
//! The generators enforce the following constraints to ensure valid documents:
//!
//! 1. **Keys**: Must be valid identifiers (alphanumeric + underscore, starting with letter)
//! 2. **Strings**: Avoid special characters that could interfere with parsing (`,`, `[`, `]`, `:`)
//! 3. **Numbers**: Use finite f64 values (no NaN or infinity)
//! 4. **Arrays**: Limited depth (max 2 levels) to avoid exponential growth
//! 5. **Refs**: Reference keys must be valid identifiers
//! 6. **Sections**: Row lengths must match schema length
//! 7. **Section IDs**: Single lowercase letters (a-z)

use crate::llm::types::{DxDocument, DxLlmValue, DxSection};
use indexmap::IndexMap;
use proptest::prelude::*;

// =============================================================================
// Public Proptest Strategies for DxDocument Generation
// =============================================================================

/// Generate a valid key/identifier for use in DxDocument context or refs.
///
/// Keys must:
/// - Start with a lowercase letter
/// - Contain only alphanumeric characters and underscores
/// - Be 2-10 characters long
///
/// # Example
///
/// ```ignore
/// proptest! {
///     #[test]
///     fn test_with_key(key in arb_key()) {
///         assert!(key.chars().next().unwrap().is_ascii_lowercase());
///     }
/// }
/// ```
pub fn arb_key() -> impl Strategy<Value = String> {
    "[a-z][a-z0-9_]{1,8}".prop_map(|s| s)
}

/// Generate a simple (non-recursive) DxLlmValue.
///
/// Generates one of:
/// - `Bool(true)` or `Bool(false)`
/// - `Null`
/// - `Num(n)` where n is an integer in range -1000..1000
/// - `Str(s)` where s is a safe alphanumeric string
///
/// This generator does NOT produce `Arr` or `Ref` variants.
/// Use [`arb_dx_llm_value`] for the full set of variants.
pub fn arb_simple_value() -> impl Strategy<Value = DxLlmValue> {
    prop_oneof![
        Just(DxLlmValue::Bool(true)),
        Just(DxLlmValue::Bool(false)),
        Just(DxLlmValue::Null),
        (-1000i64..1000i64).prop_map(|n| DxLlmValue::Num(n as f64)),
        // Use safe strings that won't interfere with parsing
        // Avoid: commas, brackets, colons, newlines
        "[a-zA-Z][a-zA-Z0-9_]{0,10}".prop_map(DxLlmValue::Str),
    ]
}

/// Generate a DxLlmValue covering ALL variants including Arr, Obj, and Ref.
///
/// This is the comprehensive generator for property-based testing that covers:
/// - `Str(s)` - Safe alphanumeric strings
/// - `Num(n)` - Integers in range -1000..1000
/// - `Bool(b)` - true or false
/// - `Null` - null value
/// - `Arr(vec)` - Arrays of simple values (1-5 items, non-empty for round-trip)
/// - `Obj(map)` - Objects with key-value pairs (1-3 fields)
/// - `Ref(key)` - Reference to a key
///
/// # Constraints
///
/// - Strings avoid special characters (`,`, `[`, `]`, `:`, newlines)
/// - Arrays contain only simple values (no nested arrays) to bound complexity
/// - Arrays are non-empty (empty arrays serialize differently)
/// - Objects contain only simple values (no nested objects) to bound complexity
/// - Ref keys are valid identifiers
///
/// **Validates: Requirements 13.1** - Covers all DxLlmValue variants
pub fn arb_dx_llm_value() -> impl Strategy<Value = DxLlmValue> {
    prop_oneof![
        // Weight simple values more heavily (70% of cases)
        7 => arb_simple_value(),
        // Arrays of simple values (10% of cases)
        // Use 1..5 (non-empty) because empty arrays serialize as `arr[0]:` which
        // parses back as an empty string, not an empty array
        1 => proptest::collection::vec(arb_simple_value(), 1..5)
            .prop_map(DxLlmValue::Arr),
        // Objects with simple values (10% of cases)
        1 => proptest::collection::vec((arb_key(), arb_simple_value()), 1..4)
            .prop_map(|v| DxLlmValue::Obj(v.into_iter().collect())),
        // References (10% of cases)
        1 => arb_key().prop_map(DxLlmValue::Ref),
    ]
}

/// Generate a valid section ID (single lowercase letter).
///
/// Section IDs in DxDocument are single characters from 'a' to 'z'.
/// This generator produces IDs from 'a' to 'e' to keep test output manageable.
pub fn arb_section_id() -> impl Strategy<Value = char> {
    prop_oneof![Just('a'), Just('b'), Just('c'), Just('d'), Just('e'),]
}

/// Generate a valid DxSection with consistent schema and rows.
///
/// The generated section has:
/// - A schema of 1-4 column names (valid identifiers)
/// - 0-5 rows where each row has exactly `schema.len()` values
/// - Values are simple (non-recursive) to ensure valid serialization
///
/// # Constraints
///
/// - Schema columns are unique valid identifiers
/// - Each row length matches schema length exactly
/// - Row values are simple types (no nested arrays)
///
/// **Validates: Requirements 13.1** - Generates valid DxSection structures
pub fn arb_dx_section() -> impl Strategy<Value = DxSection> {
    // Generate schema first, then rows that match the schema
    proptest::collection::vec(arb_key(), 1..4).prop_flat_map(|schema| {
        let schema_len = schema.len();
        // Use simple values for rows to ensure valid serialization
        let row_strategy = proptest::collection::vec(arb_simple_value(), schema_len..=schema_len);
        let rows_strategy = proptest::collection::vec(row_strategy, 0..5);

        rows_strategy.prop_map(move |rows| {
            let mut section = DxSection::new(schema.clone());
            for row in rows {
                // This should always succeed since we generate rows with correct length
                let _ = section.add_row(row);
            }
            section
        })
    })
}

/// Generate a random context map (key-value pairs).
///
/// Generates 0-5 key-value pairs where:
/// - Keys are valid identifiers
/// - Values are simple (non-recursive) DxLlmValue
pub fn arb_context() -> impl Strategy<Value = IndexMap<String, DxLlmValue>> {
    proptest::collection::vec((arb_key(), arb_simple_value()), 0..5)
        .prop_map(|v| v.into_iter().collect())
}

/// Generate a random refs map (reference definitions).
///
/// Generates 0-3 reference definitions where:
/// - Keys are valid identifiers (uppercase to distinguish from context keys)
/// - Values are simple strings
pub fn arb_refs() -> impl Strategy<Value = IndexMap<String, String>> {
    proptest::collection::vec(
        (
            "[A-Z][A-Z0-9_]{0,4}".prop_map(|s| s), // Uppercase keys for refs
            "[a-zA-Z0-9_]{1,20}".prop_map(|s| s),  // Simple string values
        ),
        0..3,
    )
    .prop_map(|v| v.into_iter().collect())
}

/// Generate a random sections map.
///
/// Generates 0-2 sections with:
/// - Section IDs from 'a' to 'e'
/// - Each section has valid schema and rows
pub fn arb_sections() -> impl Strategy<Value = IndexMap<char, DxSection>> {
    proptest::collection::vec((arb_section_id(), arb_dx_section()), 0..2)
        .prop_map(|v| v.into_iter().collect())
}

/// Generate a complete DxDocument with context, sections, and refs.
///
/// This is the primary generator for property-based testing of DxDocument
/// round-trip serialization. It generates documents with:
///
/// - **Context**: 0-5 key-value pairs with all value types
/// - **Sections**: 0-2 table sections with schema and rows
/// - **Refs**: 0-3 reference definitions
///
/// # Constraints
///
/// All generated documents are valid and can be serialized without error.
/// The generator enforces:
/// - Valid identifiers for all keys
/// - Consistent row lengths in sections
/// - Safe string values (no parsing-interfering characters)
///
/// **Validates: Requirements 13.1** - Generates valid DxDocument with context, sections, refs
///
/// # Example
///
/// ```ignore
/// use proptest::prelude::*;
/// use serializer::llm::llm_props::arb_dx_document;
///
/// proptest! {
///     #[test]
///     fn test_document_is_valid(doc in arb_dx_document()) {
///         // Document should be serializable
///         let serialized = serialize(&doc);
///         assert!(!serialized.is_empty() || doc.is_empty());
///     }
/// }
/// ```
pub fn arb_dx_document() -> impl Strategy<Value = DxDocument> {
    (arb_context(), arb_sections(), arb_refs()).prop_map(|(context, sections, refs)| {
        let mut doc = DxDocument::new();
        doc.context = context;
        doc.sections = sections;
        doc.refs = refs;
        doc
    })
}

/// Generate a DxDocument with context only (simpler for basic round-trip tests).
///
/// This is a simpler generator that only populates the context field,
/// useful for testing context-level round-trip without the complexity
/// of sections and refs.
pub fn arb_document_context_only() -> impl Strategy<Value = DxDocument> {
    arb_context().prop_map(|context| {
        let mut doc = DxDocument::new();
        doc.context = context;
        doc
    })
}

// =============================================================================
// Property Tests
// =============================================================================

#[cfg(test)]
mod property_tests {
    use super::*;
    use crate::llm::parser::LlmParser;
    use crate::llm::serializer::LlmSerializer;

    /// Compare two DxLlmValues for semantic equality
    /// (handles numeric precision and reference resolution)
    fn values_equal(a: &DxLlmValue, b: &DxLlmValue) -> bool {
        match (a, b) {
            (DxLlmValue::Num(x), DxLlmValue::Num(y)) => (x - y).abs() < 0.0001,
            (DxLlmValue::Str(x), DxLlmValue::Str(y)) => x == y,
            (DxLlmValue::Bool(x), DxLlmValue::Bool(y)) => x == y,
            (DxLlmValue::Null, DxLlmValue::Null) => true,
            (DxLlmValue::Arr(x), DxLlmValue::Arr(y)) => {
                x.len() == y.len() && x.iter().zip(y.iter()).all(|(a, b)| values_equal(a, b))
            }
            (DxLlmValue::Obj(x), DxLlmValue::Obj(y)) => {
                x.len() == y.len()
                    && x.iter()
                        .all(|(k, v)| y.get(k).is_some_and(|yv| values_equal(v, yv)))
            }
            // References may be resolved to strings
            (DxLlmValue::Ref(_), DxLlmValue::Str(_)) => true,
            (DxLlmValue::Str(_), DxLlmValue::Ref(_)) => true,
            // Empty array may serialize/parse as empty string
            (DxLlmValue::Arr(arr), DxLlmValue::Str(s)) if arr.is_empty() && s.is_empty() => true,
            (DxLlmValue::Str(s), DxLlmValue::Arr(arr)) if arr.is_empty() && s.is_empty() => true,
            _ => false,
        }
    }

    /// Compare two sections for semantic equality (ignoring section ID)
    fn sections_equal(a: &DxSection, b: &DxSection) -> bool {
        // Compare schema
        if a.schema != b.schema {
            return false;
        }
        // Compare rows
        if a.rows.len() != b.rows.len() {
            return false;
        }
        for (row_a, row_b) in a.rows.iter().zip(b.rows.iter()) {
            if row_a.len() != row_b.len() {
                return false;
            }
            for (val_a, val_b) in row_a.iter().zip(row_b.iter()) {
                if !values_equal(val_a, val_b) {
                    return false;
                }
            }
        }
        true
    }

    /// Compare two documents for semantic equality
    /// Note: Keys may be abbreviated during serialization, so we compare
    /// using the abbreviation dictionary to normalize keys.
    /// Section IDs may change during round-trip (parser assigns sequential IDs),
    /// so we compare sections by content, not by ID.
    fn documents_equal(a: &DxDocument, b: &DxDocument) -> bool {
        use crate::llm::abbrev::AbbrevDict;
        let abbrev = AbbrevDict::new();

        // Compare context - keys may be abbreviated
        if a.context.len() != b.context.len() {
            return false;
        }
        for (key_a, val_a) in &a.context {
            // Try to find the key in b, accounting for abbreviation
            let compressed_key = abbrev.compress(key_a);
            let val_b = b
                .context
                .get(key_a)
                .or_else(|| b.context.get(&compressed_key));

            if let Some(val_b) = val_b {
                if !values_equal(val_a, val_b) {
                    return false;
                }
            } else {
                return false;
            }
        }

        // Compare sections by content (IDs may change during round-trip)
        // The parser assigns sequential IDs starting from 'a', so we can't
        // compare by ID. Instead, we check that each section in 'a' has a
        // matching section in 'b' with the same content.
        if a.sections.len() != b.sections.len() {
            return false;
        }

        // Collect sections from both documents
        let sections_a: Vec<&DxSection> = a.sections.values().collect();
        let sections_b: Vec<&DxSection> = b.sections.values().collect();

        // For each section in a, find a matching section in b
        let mut matched_b: Vec<bool> = vec![false; sections_b.len()];
        for section_a in &sections_a {
            let mut found = false;
            for (i, section_b) in sections_b.iter().enumerate() {
                if !matched_b[i] && sections_equal(section_a, section_b) {
                    matched_b[i] = true;
                    found = true;
                    break;
                }
            }
            if !found {
                return false;
            }
        }

        true
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        /// Property 1: LLM Format Round-Trip Preservation (Context Only)
        /// For any valid DxDocument with context, serializing to LLM format and parsing back
        /// SHALL produce a semantically equivalent DxDocument.
        ///
        /// **Feature: serializer-production-hardening, Property 1: LLM Format Round-Trip Preservation**
        /// **Validates: Requirements 1.2, 1.3, 1.5, 1.6**
        #[test]
        fn prop_llm_round_trip(doc in arb_document_context_only()) {
            let serializer = LlmSerializer::new();

            // Serialize to LLM format
            let llm_string = serializer.serialize(&doc);

            // Parse back
            let parsed = LlmParser::parse(&llm_string);
            prop_assert!(parsed.is_ok(), "Failed to parse serialized LLM: {}", llm_string);

            let parsed_doc = parsed.unwrap();

            // Verify semantic equality
            prop_assert!(
                documents_equal(&doc, &parsed_doc),
                "Round-trip failed:\nOriginal: {:?}\nSerialized: {}\nParsed: {:?}",
                doc, llm_string, parsed_doc
            );
        }

        /// Property 7: DxDocument Round-Trip (Full Document)
        /// For any valid DxDocument with context, sections, and refs,
        /// serializing to LLM format and deserializing back SHALL produce
        /// an equivalent document (semantic equality).
        ///
        /// **Feature: serializer-production-hardening, Property 7: DxDocument Round-Trip**
        /// **Validates: Requirements 13.1**
        #[test]
        fn prop_dx_document_round_trip(doc in arb_dx_document()) {
            let serializer = LlmSerializer::new();

            // Serialize to LLM format
            let llm_string = serializer.serialize(&doc);

            // Parse back
            let parsed = LlmParser::parse(&llm_string);
            prop_assert!(parsed.is_ok(), "Failed to parse serialized LLM: {}", llm_string);

            let parsed_doc = parsed.unwrap();

            // Verify context equality (sections may have different IDs after round-trip)
            prop_assert!(
                documents_equal(&doc, &parsed_doc),
                "Round-trip failed:\nOriginal: {:?}\nSerialized: {}\nParsed: {:?}",
                doc, llm_string, parsed_doc
            );
        }

        /// Property: All DxLlmValue variants can be serialized and parsed
        /// Tests that each variant type survives round-trip through serialization.
        ///
        /// **Feature: serializer-production-hardening, Property 7: DxDocument Round-Trip**
        /// **Validates: Requirements 13.1**
        #[test]
        fn prop_all_value_variants_round_trip(value in arb_dx_llm_value()) {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            doc.context.insert("val".to_string(), value.clone());

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string);
            prop_assert!(parsed.is_ok(), "Failed to parse: {}", llm_string);

            let parsed_doc = parsed.unwrap();
            let parsed_value = parsed_doc.context.get("val");
            prop_assert!(parsed_value.is_some(), "Value not found after round-trip");

            // Verify semantic equality
            prop_assert!(
                values_equal(&value, parsed_value.unwrap()),
                "Value mismatch:\nOriginal: {:?}\nParsed: {:?}",
                value, parsed_value
            );
        }

        /// Property: Boolean values are preserved through round-trip
        ///
        /// **Feature: serializer-production-hardening, Property 1: LLM Format Round-Trip Preservation**
        /// **Validates: Requirements 1.5, 1.6**
        #[test]
        fn prop_boolean_round_trip(b in proptest::bool::ANY) {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            doc.context.insert("flag".to_string(), DxLlmValue::Bool(b));

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string).unwrap();

            let parsed_value = parsed.context.get("flag").unwrap();
            prop_assert_eq!(parsed_value.as_bool(), Some(b));
        }

        /// Property: Null values are preserved through round-trip
        ///
        /// **Feature: serializer-production-hardening, Property 1: LLM Format Round-Trip Preservation**
        /// **Validates: Requirements 1.3**
        #[test]
        fn prop_null_round_trip(_dummy in Just(())) {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            doc.context.insert("empty".to_string(), DxLlmValue::Null);

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string).unwrap();

            let parsed_value = parsed.context.get("empty").unwrap();
            prop_assert!(parsed_value.is_null());
        }

        /// Property: Numeric values are preserved through round-trip
        ///
        /// **Feature: serializer-production-hardening, Property 1: LLM Format Round-Trip Preservation**
        /// **Validates: Requirements 1.2, 1.3**
        #[test]
        fn prop_numeric_round_trip(n in -10000i64..10000i64) {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            // Use abbreviated key "num" since "number" gets compressed
            doc.context.insert("num".to_string(), DxLlmValue::Num(n as f64));

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string).unwrap();

            let parsed_value = parsed.context.get("num").unwrap();
            prop_assert_eq!(parsed_value.as_num(), Some(n as f64));
        }

        /// Property: String values are preserved through round-trip
        ///
        /// **Feature: serializer-production-hardening, Property 1: LLM Format Round-Trip Preservation**
        /// **Validates: Requirements 1.2, 1.3**
        #[test]
        fn prop_string_round_trip(s in "[a-zA-Z][a-zA-Z0-9_]{0,20}") {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            // Use a key that won't be abbreviated (use the abbreviated form directly)
            doc.context.insert("txt".to_string(), DxLlmValue::Str(s.clone()));

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string).unwrap();

            // The key should remain "txt" since it's already abbreviated
            let parsed_value = parsed.context.get("txt").unwrap();
            prop_assert_eq!(parsed_value.as_str(), Some(s.as_str()));
        }

        /// Property: Array values are preserved through round-trip
        /// Note: Empty arrays serialize as `arr[0]:` which parses back as empty string,
        /// so we test with non-empty arrays only.
        ///
        /// **Feature: serializer-production-hardening, Property 7: DxDocument Round-Trip**
        /// **Validates: Requirements 13.1**
        #[test]
        fn prop_array_round_trip(items in proptest::collection::vec(arb_simple_value(), 1..5)) {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            doc.context.insert("arr".to_string(), DxLlmValue::Arr(items.clone()));

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string);
            prop_assert!(parsed.is_ok(), "Failed to parse: {}", llm_string);

            let parsed_doc = parsed.unwrap();
            let parsed_value = parsed_doc.context.get("arr");
            prop_assert!(parsed_value.is_some(), "Array not found after round-trip");

            if let Some(DxLlmValue::Arr(parsed_items)) = parsed_value {
                prop_assert_eq!(
                    items.len(), parsed_items.len(),
                    "Array length mismatch: {} vs {}",
                    items.len(), parsed_items.len()
                );
                for (orig, parsed) in items.iter().zip(parsed_items.iter()) {
                    prop_assert!(
                        values_equal(orig, parsed),
                        "Array item mismatch: {:?} vs {:?}",
                        orig, parsed
                    );
                }
            } else {
                prop_assert!(false, "Expected array, got: {:?}", parsed_value);
            }
        }

        /// Property: DxSection with schema and rows survives round-trip
        ///
        /// **Feature: serializer-production-hardening, Property 7: DxDocument Round-Trip**
        /// **Validates: Requirements 13.1**
        #[test]
        fn prop_section_round_trip(section in arb_dx_section()) {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            doc.sections.insert('a', section.clone());

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string);
            prop_assert!(parsed.is_ok(), "Failed to parse: {}", llm_string);

            let parsed_doc = parsed.unwrap();

            // Section may be assigned a different ID, so check any section exists
            if !section.rows.is_empty() {
                prop_assert!(
                    !parsed_doc.sections.is_empty(),
                    "Section lost after round-trip. Original: {:?}, Serialized: {}",
                    section, llm_string
                );
            }
        }

        /// Property: Object (Obj) values are preserved through round-trip
        /// Tests that the Obj variant with key-value pairs survives serialization.
        ///
        /// **Feature: serializer-production-hardening, Property 7: DxDocument Round-Trip**
        /// **Validates: Requirements 13.1** - Covers Obj variant specifically
        #[test]
        fn prop_obj_round_trip(fields in proptest::collection::vec((arb_key(), arb_simple_value()), 1..4)) {
            let serializer = LlmSerializer::new();
            let mut doc = DxDocument::new();
            let obj_value = DxLlmValue::Obj(fields.iter().cloned().collect());
            doc.context.insert("obj".to_string(), obj_value.clone());

            let llm_string = serializer.serialize(&doc);
            let parsed = LlmParser::parse(&llm_string);
            prop_assert!(parsed.is_ok(), "Failed to parse Obj: {}", llm_string);

            let parsed_doc = parsed.unwrap();
            let parsed_value = parsed_doc.context.get("obj");
            prop_assert!(parsed_value.is_some(), "Obj not found after round-trip");

            // Verify the parsed value is semantically equal
            prop_assert!(
                values_equal(&obj_value, parsed_value.unwrap()),
                "Obj mismatch:\nOriginal: {:?}\nSerialized: {}\nParsed: {:?}",
                obj_value, llm_string, parsed_value
            );
        }
    }

    #[test]
    fn test_llm_round_trip_basic() {
        let serializer = LlmSerializer::new();
        let mut doc = DxDocument::new();

        doc.context
            .insert("name".to_string(), DxLlmValue::Str("Test".to_string()));
        doc.context
            .insert("count".to_string(), DxLlmValue::Num(42.0));
        doc.context
            .insert("active".to_string(), DxLlmValue::Bool(true));

        let llm_string = serializer.serialize(&doc);
        let parsed = LlmParser::parse(&llm_string).unwrap();

        // Check context values are preserved
        assert_eq!(parsed.context.get("name").unwrap().as_str(), Some("Test"));
        assert_eq!(parsed.context.get("count").unwrap().as_num(), Some(42.0));
        assert_eq!(parsed.context.get("active").unwrap().as_bool(), Some(true));
    }

    #[test]
    fn test_special_values_round_trip() {
        let serializer = LlmSerializer::new();
        let mut doc = DxDocument::new();

        doc.context
            .insert("true_val".to_string(), DxLlmValue::Bool(true));
        doc.context
            .insert("false_val".to_string(), DxLlmValue::Bool(false));
        doc.context.insert("null_val".to_string(), DxLlmValue::Null);

        let llm_string = serializer.serialize(&doc);

        // Verify Dx Serializer format uses true/false/null with :: or : delimiter
        assert!(
            llm_string.contains("true"),
            "Should contain true for boolean true"
        );
        assert!(
            llm_string.contains("false"),
            "Should contain false for boolean false"
        );
        assert!(
            llm_string.contains("null"),
            "Should contain null for null value"
        );

        let parsed = LlmParser::parse(&llm_string).unwrap();

        assert_eq!(
            parsed.context.get("true_val").unwrap().as_bool(),
            Some(true)
        );
        assert_eq!(
            parsed.context.get("false_val").unwrap().as_bool(),
            Some(false)
        );
        assert!(parsed.context.get("null_val").unwrap().is_null());
    }
}