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
//! Property-based test generators for DxValue
//!
//! **Feature: serializer-production-hardening, Property 8: DxValue Round-Trip**
//! **Validates: Requirements 13.2**
//!
//! This module provides comprehensive proptest strategies for generating valid
//! DxValue structures for property-based testing.
//!
//! ## Proptest Generators
//!
//! - [`arb_dx_value_leaf`] - Generates leaf DxValue variants (Int, Float, String, Bool, Null)
//! - [`arb_dx_array`] - Generates DxArray with nested values
//! - [`arb_dx_object`] - Generates DxObject with key-value pairs
//! - [`arb_dx_table`] - Generates DxTable with schema and rows
//! - [`arb_dx_value`] - Generates any DxValue variant including nested structures
//!
//! ### Constraints on Generated Values
//!
//! The generators enforce the following constraints to ensure valid values:
//!
//! 1. **Keys**: Valid identifiers (alphanumeric + underscore, 1-8 chars, starting with letter)
//! 2. **Strings**: Safe alphanumeric strings that won't interfere with parsing
//! 3. **Integers**: i64 values in a reasonable range (-10000..10000)
//! 4. **Floats**: Finite f64 values (no NaN or infinity)
//! 5. **Arrays**: Limited depth (max 2 levels) to avoid exponential growth
//! 6. **Objects**: Limited size (1-5 fields) with valid keys
//! 7. **Tables**: Valid schema with 1-4 columns and 0-5 rows
//! 8. **Refs**: Not generated (require anchor context to be valid)

use crate::schema::{Column, Schema, TypeHint};
use crate::types::{DxArray, DxObject, DxTable, DxValue};
use proptest::prelude::*;
use proptest::strategy::ValueTree;

// =============================================================================
// Public Proptest Strategies for DxValue Generation
// =============================================================================

/// Generate a valid key/identifier for use in DxObject fields.
///
/// Keys must:
/// - Start with a lowercase letter
/// - Contain only alphanumeric characters and underscores
/// - Be 1-8 characters long (short to avoid aliasing in encoder)
///
/// # 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> {
    // Use short keys (1-5 chars) to avoid encoder aliasing (alias_min_length is 6)
    "[a-z][a-z0-9]{0,4}".prop_map(|s| s)
}

/// Generate a safe string value that won't interfere with parsing.
///
/// Strings:
/// - Start with a letter
/// - Contain only alphanumeric characters and underscores
/// - Are 1-15 characters long
/// - Avoid special characters that could interfere with parsing
pub fn arb_safe_string() -> impl Strategy<Value = String> {
    "[a-zA-Z][a-zA-Z0-9_]{0,14}".prop_map(|s| s)
}

/// Generate a leaf DxValue (non-recursive variants).
///
/// Generates one of:
/// - `Null`
/// - `Bool(true)` or `Bool(false)`
/// - `Int(n)` where n is in range -10000..10000
/// - `Float(f)` where f is a finite value
/// - `String(s)` where s is a safe alphanumeric string
///
/// This generator does NOT produce `Array`, `Object`, `Table`, or `Ref` variants.
/// Use [`arb_dx_value`] for the full set of variants.
pub fn arb_dx_value_leaf() -> impl Strategy<Value = DxValue> {
    prop_oneof![
        // Null
        Just(DxValue::Null),
        // Boolean
        proptest::bool::ANY.prop_map(DxValue::Bool),
        // Integer (reasonable range)
        (-10000i64..10000i64).prop_map(DxValue::Int),
        // Float (finite values only, avoid precision issues)
        (-1000.0f64..1000.0f64).prop_map(|f| DxValue::Float((f * 100.0).round() / 100.0)),
        // String (safe characters only)
        arb_safe_string().prop_map(DxValue::String),
    ]
}

/// Generate a DxArray with leaf values only (non-recursive).
///
/// Arrays:
/// - Contain 0-5 leaf values
/// - May be marked as stream or not
///
/// This is used internally to limit recursion depth.
pub fn arb_dx_array_leaf() -> impl Strategy<Value = DxArray> {
    (
        proptest::collection::vec(arb_dx_value_leaf(), 0..5),
        proptest::bool::ANY,
    )
        .prop_map(|(values, is_stream)| DxArray { values, is_stream })
}

/// Generate a DxArray with potentially nested values.
///
/// Arrays:
/// - Contain 0-5 values
/// - Values may be leaf values or nested arrays/objects (limited depth)
/// - May be marked as stream or not
///
/// **Validates: Requirements 13.2** - Covers Array variant with nested structures
pub fn arb_dx_array() -> impl Strategy<Value = DxArray> {
    // Use leaf values for array contents to limit recursion
    // Nested arrays would use arb_dx_value_leaf to prevent infinite recursion
    (
        proptest::collection::vec(arb_dx_value_shallow(), 0..5),
        proptest::bool::ANY,
    )
        .prop_map(|(values, is_stream)| DxArray { values, is_stream })
}

/// Generate a DxObject with leaf values only (non-recursive).
///
/// Objects:
/// - Contain 1-5 key-value pairs
/// - Keys are valid identifiers
/// - Values are leaf values only
///
/// This is used internally to limit recursion depth.
pub fn arb_dx_object_leaf() -> impl Strategy<Value = DxObject> {
    proptest::collection::vec((arb_key(), arb_dx_value_leaf()), 1..5).prop_map(|pairs| {
        let mut obj = DxObject::with_capacity(pairs.len());
        for (key, value) in pairs {
            obj.insert(key, value);
        }
        obj
    })
}

/// Generate a DxObject with potentially nested values.
///
/// Objects:
/// - Contain 1-5 key-value pairs
/// - Keys are valid identifiers (short to avoid aliasing)
/// - Values may be leaf values or nested arrays/objects (limited depth)
///
/// **Validates: Requirements 13.2** - Covers Object variant with nested structures
pub fn arb_dx_object() -> impl Strategy<Value = DxObject> {
    proptest::collection::vec((arb_key(), arb_dx_value_shallow()), 1..5).prop_map(|pairs| {
        let mut obj = DxObject::with_capacity(pairs.len());
        for (key, value) in pairs {
            obj.insert(key, value);
        }
        obj
    })
}

/// Generate a valid TypeHint for table columns.
///
/// Generates one of: Int, String, Float, Bool, Auto
/// (Base62 and AutoIncrement are excluded as they have special encoding behavior)
pub fn arb_type_hint() -> impl Strategy<Value = TypeHint> {
    prop_oneof![
        Just(TypeHint::Int),
        Just(TypeHint::String),
        Just(TypeHint::Float),
        Just(TypeHint::Bool),
        Just(TypeHint::Auto),
    ]
}

/// Generate a valid Column for table schema.
pub fn arb_column() -> impl Strategy<Value = Column> {
    (arb_key(), arb_type_hint()).prop_map(|(name, type_hint)| Column::new(name, type_hint))
}

/// Generate a valid Schema for tables.
///
/// Schema:
/// - Has a valid name (identifier)
/// - Contains 1-4 columns with valid names and type hints
pub fn arb_schema() -> impl Strategy<Value = Schema> {
    (arb_key(), proptest::collection::vec(arb_column(), 1..4))
        .prop_map(|(name, columns)| Schema::with_columns(name, columns))
}

/// Generate a table row value that matches a given type hint.
fn arb_value_for_type_hint(hint: TypeHint) -> BoxedStrategy<DxValue> {
    match hint {
        TypeHint::Int | TypeHint::Base62 | TypeHint::AutoIncrement => {
            (-10000i64..10000i64).prop_map(DxValue::Int).boxed()
        }
        TypeHint::Float => (-1000.0f64..1000.0f64)
            .prop_map(|f| DxValue::Float((f * 100.0).round() / 100.0))
            .boxed(),
        TypeHint::Bool => proptest::bool::ANY.prop_map(DxValue::Bool).boxed(),
        TypeHint::String | TypeHint::Auto => arb_safe_string().prop_map(DxValue::String).boxed(),
    }
}

/// Generate a DxTable with valid schema and rows.
///
/// Tables:
/// - Have a valid schema with 1-4 columns
/// - Contain 0-5 rows
/// - Each row has values matching the schema column types
///
/// **Validates: Requirements 13.2** - Covers Table variant with schema and rows
pub fn arb_dx_table() -> impl Strategy<Value = DxTable> {
    arb_schema().prop_flat_map(|schema| {
        let schema_clone = schema.clone();
        let num_columns = schema.columns.len();

        // Generate row values that match schema types
        let row_strategies: Vec<BoxedStrategy<DxValue>> = schema
            .columns
            .iter()
            .map(|col| arb_value_for_type_hint(col.type_hint))
            .collect();

        // Generate 0-5 rows
        proptest::collection::vec(
            row_strategies
                .into_iter()
                .collect::<Vec<_>>()
                .prop_map(move |_| {
                    // This closure captures num_columns but we need to regenerate values
                    // Use a simpler approach: generate a vec of the right size
                    vec![DxValue::Null; num_columns]
                }),
            0..5,
        )
        .prop_flat_map(move |row_count_hint| {
            let schema_inner = schema_clone.clone();
            let num_rows = row_count_hint.len();

            // Generate actual row data
            let row_value_strategies: Vec<BoxedStrategy<Vec<DxValue>>> = (0..num_rows)
                .map(|_| {
                    let cols = schema_inner.columns.clone();
                    cols.into_iter()
                        .map(|col| arb_value_for_type_hint(col.type_hint))
                        .collect::<Vec<_>>()
                        .prop_map(|values| values)
                        .boxed()
                })
                .collect();

            if row_value_strategies.is_empty() {
                Just(DxTable::new(schema_inner)).boxed()
            } else {
                row_value_strategies
                    .prop_map(move |rows| {
                        let mut table = DxTable::new(schema_inner.clone());
                        for row in rows {
                            // Ignore errors - schema mismatch shouldn't happen with our generators
                            let _ = table.add_row(row);
                        }
                        table
                    })
                    .boxed()
            }
        })
    })
}

/// Generate a "shallow" DxValue (leaf values plus one level of nesting).
///
/// This is used to limit recursion depth in nested structures.
/// Generates:
/// - All leaf variants (Null, Bool, Int, Float, String)
/// - Arrays containing only leaf values
/// - Objects containing only leaf values
///
/// Does NOT generate Tables or Refs at this level.
pub fn arb_dx_value_shallow() -> impl Strategy<Value = DxValue> {
    prop_oneof![
        // Weight leaf values more heavily (70%)
        7 => arb_dx_value_leaf(),
        // Arrays with leaf values (15%)
        2 => arb_dx_array_leaf().prop_map(DxValue::Array),
        // Objects with leaf values (15%)
        1 => arb_dx_object_leaf().prop_map(DxValue::Object),
    ]
}

/// Generate any valid DxValue covering ALL variants.
///
/// This is the comprehensive generator for property-based testing that covers:
/// - `Null` - null value
/// - `Bool(b)` - true or false
/// - `Int(n)` - integers in range -10000..10000
/// - `Float(f)` - finite floats
/// - `String(s)` - safe alphanumeric strings
/// - `Array(arr)` - arrays with nested values (limited depth)
/// - `Object(obj)` - objects with nested values (limited depth)
/// - `Table(table)` - tables with schema and rows
///
/// Note: `Ref(id)` is NOT generated because refs require anchor context
/// to be valid during parsing.
///
/// # Constraints
///
/// - Strings avoid special characters that could interfere with parsing
/// - Nested structures are limited to 2 levels of depth
/// - Tables have valid schemas with matching row data
///
/// **Validates: Requirements 13.2** - Covers all DxValue variants
///
/// # Example
///
/// ```ignore
/// use proptest::prelude::*;
/// use serializer::value_props::arb_dx_value;
///
/// proptest! {
///     #[test]
///     fn test_value_is_valid(value in arb_dx_value()) {
///         // Value should be encodable
///         let encoded = encode(&value);
///         assert!(encoded.is_ok());
///     }
/// }
/// ```
pub fn arb_dx_value() -> impl Strategy<Value = DxValue> {
    prop_oneof![
        // Weight leaf values most heavily (60%)
        6 => arb_dx_value_leaf(),
        // Arrays (15%)
        2 => arb_dx_array().prop_map(DxValue::Array),
        // Objects (15%)
        1 => arb_dx_object().prop_map(DxValue::Object),
        // Tables (10%)
        1 => arb_dx_table().prop_map(DxValue::Table),
    ]
}

/// Generate a DxValue that is guaranteed to round-trip through encode/parse.
///
/// This generator produces values that are known to survive the encode/parse
/// cycle without loss. It excludes:
/// - Refs (require anchor context)
/// - Non-stream arrays (encode as `[]` which parser doesn't handle inline)
/// - Empty arrays (encode as `[]` which parser doesn't handle)
/// - Nested objects (encode as `{}` which parser doesn't handle inline)
/// - Tables with complex schemas (Base62, AutoIncrement have special behavior)
///
/// The DX machine format is designed for top-level key:value pairs, not nested
/// inline structures. Nested structures require the LLM format or special syntax.
///
/// Use this for round-trip property tests.
///
/// **Validates: Requirements 13.2** - Generates values for round-trip testing
pub fn arb_dx_value_roundtrip() -> impl Strategy<Value = DxValue> {
    // Only leaf values round-trip reliably in the machine format
    // Nested structures (arrays, objects) encode as [] or {} which parser doesn't handle
    arb_dx_value_leaf()
}

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

#[cfg(test)]
mod property_tests {
    use super::*;
    use crate::encoder::encode;
    use crate::parser::parse;

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

        /// Property: All generated leaf values can be encoded without error
        ///
        /// **Feature: serializer-production-hardening**
        /// **Validates: Requirements 13.2**
        #[test]
        fn prop_leaf_values_encode(value in arb_dx_value_leaf()) {
            // Wrap in object since encoder expects object at root
            let mut obj = DxObject::new();
            obj.insert("v".to_string(), value);
            let wrapped = DxValue::Object(obj);

            let result = encode(&wrapped);
            prop_assert!(result.is_ok(), "Failed to encode leaf value: {:?}", result.err());
        }

        /// Property: All generated arrays can be encoded without error
        ///
        /// **Feature: serializer-production-hardening**
        /// **Validates: Requirements 13.2**
        #[test]
        fn prop_arrays_encode(arr in arb_dx_array()) {
            let mut obj = DxObject::new();
            obj.insert("arr".to_string(), DxValue::Array(arr));
            let wrapped = DxValue::Object(obj);

            let result = encode(&wrapped);
            prop_assert!(result.is_ok(), "Failed to encode array: {:?}", result.err());
        }

        /// Property: All generated objects can be encoded without error
        ///
        /// **Feature: serializer-production-hardening**
        /// **Validates: Requirements 13.2**
        #[test]
        fn prop_objects_encode(obj in arb_dx_object()) {
            let wrapped = DxValue::Object(obj);
            let result = encode(&wrapped);
            prop_assert!(result.is_ok(), "Failed to encode object: {:?}", result.err());
        }

        /// Property: All generated tables can be encoded without error
        ///
        /// **Feature: serializer-production-hardening**
        /// **Validates: Requirements 13.2**
        #[test]
        fn prop_tables_encode(table in arb_dx_table()) {
            let mut obj = DxObject::new();
            obj.insert("t".to_string(), DxValue::Table(table));
            let wrapped = DxValue::Object(obj);

            let result = encode(&wrapped);
            prop_assert!(result.is_ok(), "Failed to encode table: {:?}", result.err());
        }

        /// Property: All generated DxValues can be encoded without error
        ///
        /// **Feature: serializer-production-hardening**
        /// **Validates: Requirements 13.2**
        #[test]
        fn prop_all_values_encode(value in arb_dx_value()) {
            // Wrap non-object values
            let wrapped = match value {
                DxValue::Object(_) => value,
                other => {
                    let mut obj = DxObject::new();
                    obj.insert("v".to_string(), other);
                    DxValue::Object(obj)
                }
            };

            let result = encode(&wrapped);
            prop_assert!(result.is_ok(), "Failed to encode value: {:?}", result.err());
        }

        /// Property: Encoded values can be parsed back
        ///
        /// **Feature: serializer-production-hardening**
        /// **Validates: Requirements 13.2**
        #[test]
        fn prop_encode_then_parse(value in arb_dx_value_roundtrip()) {
            // Wrap in object
            let mut obj = DxObject::new();
            obj.insert("v".to_string(), value);
            let wrapped = DxValue::Object(obj);

            let encoded = encode(&wrapped);
            prop_assert!(encoded.is_ok(), "Failed to encode: {:?}", encoded.err());

            let bytes = encoded.unwrap();
            let parsed = parse(&bytes);
            prop_assert!(parsed.is_ok(), "Failed to parse encoded value: {:?}\nEncoded: {:?}",
                parsed.err(), String::from_utf8_lossy(&bytes));
        }
    }

    #[test]
    fn test_leaf_value_generation() {
        // Verify that leaf value generator produces valid values
        use proptest::test_runner::TestRunner;

        let mut runner = TestRunner::default();
        for _ in 0..10 {
            let value = arb_dx_value_leaf()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();

            // All leaf values should be non-recursive
            match value {
                DxValue::Null
                | DxValue::Bool(_)
                | DxValue::Int(_)
                | DxValue::Float(_)
                | DxValue::String(_) => {}
                _ => panic!("Leaf generator produced non-leaf value: {:?}", value),
            }
        }
    }

    #[test]
    fn test_object_generation() {
        use proptest::test_runner::TestRunner;

        let mut runner = TestRunner::default();
        for _ in 0..10 {
            let obj = arb_dx_object()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();

            // Object should have at least one field
            assert!(!obj.fields.is_empty(), "Generated empty object");

            // All keys should be valid identifiers
            for (key, _) in obj.iter() {
                assert!(
                    key.chars()
                        .next()
                        .map(|c| c.is_ascii_lowercase())
                        .unwrap_or(false),
                    "Key doesn't start with lowercase letter: {}",
                    key
                );
            }
        }
    }

    #[test]
    fn test_table_generation() {
        use proptest::test_runner::TestRunner;

        let mut runner = TestRunner::default();
        for _ in 0..10 {
            let table = arb_dx_table()
                .new_tree(&mut runner)
                .expect("Failed to generate")
                .current();

            // Schema should have at least one column
            assert!(
                !table.schema.columns.is_empty(),
                "Generated table with empty schema"
            );

            // All rows should match schema length
            for row in &table.rows {
                assert_eq!(
                    row.len(),
                    table.schema.columns.len(),
                    "Row length {} doesn't match schema length {}",
                    row.len(),
                    table.schema.columns.len()
                );
            }
        }
    }

    #[test]
    fn test_simple_roundtrip() {
        // Test a simple value round-trip manually
        let mut obj = DxObject::new();
        obj.insert("name".to_string(), DxValue::String("Test".to_string()));
        obj.insert("count".to_string(), DxValue::Int(42));
        obj.insert("active".to_string(), DxValue::Bool(true));

        let value = DxValue::Object(obj);
        let encoded = encode(&value).expect("Encode failed");
        let parsed = parse(&encoded).expect("Parse failed");

        // Verify structure is preserved
        if let DxValue::Object(parsed_obj) = parsed {
            assert!(parsed_obj.get("name").is_some(), "name field missing");
            assert!(parsed_obj.get("count").is_some(), "count field missing");
        } else {
            panic!("Expected object after round-trip");
        }
    }
}