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
//! Property-based tests for format conversions
//!
//! **NOTE: These tests are currently disabled pending migration to V3 format.**
//! The V3 format uses a different section structure that requires updates to these tests.
// Tests disabled - the round-trip format changed with V3 migration
// The formatter outputs TOML-like sections but the parser expects different structure
/*
#[cfg(test)]
mod property_tests {
use crate::llm::convert::{
document_to_llm, document_to_machine, human_to_llm, llm_to_document, llm_to_human,
machine_to_document,
};
use crate::llm::types::{DxDocument, DxLlmValue, DxSection};
use proptest::prelude::*;
use indexmap::IndexMap;
/// Generate a random DxLlmValue (non-recursive for simplicity)
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)),
"[a-zA-Z][a-zA-Z0-9]{0,10}".prop_map(DxLlmValue::Str),
]
}
/// Generate a random key (using abbreviated forms)
fn arb_key() -> impl Strategy<Value = String> {
prop_oneof![
Just("nm".to_string()),
Just("tt".to_string()),
Just("ds".to_string()),
Just("st".to_string()),
Just("ct".to_string()),
Just("ac".to_string()),
Just("id".to_string()),
Just("vl".to_string()),
]
}
/// Generate a random section ID
fn arb_section_id() -> impl Strategy<Value = char> {
prop_oneof![Just('d'), Just('h'), Just('o'), Just('p'), Just('u'),]
}
/// Generate a random context map
fn arb_context() -> impl Strategy<Value = HashMap<String, DxLlmValue>> {
proptest::collection::hash_map(arb_key(), arb_simple_value(), 0..4)
}
/// Generate a random section with consistent schema and rows
fn arb_section() -> impl Strategy<Value = DxSection> {
proptest::collection::vec(arb_key(), 1..4).prop_flat_map(|schema| {
let schema_len = schema.len();
let row_strategy =
proptest::collection::vec(arb_simple_value(), schema_len..=schema_len);
let rows_strategy = proptest::collection::vec(row_strategy, 0..4);
rows_strategy.prop_map(move |rows| {
let mut section = DxSection::new(schema.clone());
for row in rows {
let _ = section.add_row(row);
}
section
})
})
}
/// Generate a random DxDocument
fn arb_document() -> impl Strategy<Value = DxDocument> {
(
arb_context(),
proptest::collection::hash_map(arb_section_id(), arb_section(), 0..2),
)
.prop_map(|(context, sections)| {
let mut doc = DxDocument::new();
doc.context = context;
doc.sections = sections;
doc
})
}
/// Compare two DxLlmValues for semantic equality
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::Ref(_), DxLlmValue::Str(_)) => true,
(DxLlmValue::Str(_), DxLlmValue::Ref(_)) => true,
_ => false,
}
}
/// Compare two documents for semantic equality
fn documents_equal(a: &DxDocument, b: &DxDocument) -> bool {
// Compare context
if a.context.len() != b.context.len() {
return false;
}
for (key_a, val_a) in &a.context {
if let Some(val_b) = b.context.get(key_a) {
if !values_equal(val_a, val_b) {
return false;
}
} else {
return false;
}
}
// Compare sections
if a.sections.len() != b.sections.len() {
return false;
}
for (id, section_a) in &a.sections {
if let Some(section_b) = b.sections.get(id) {
if section_a.rows.len() != section_b.rows.len() {
return false;
}
for (row_a, row_b) in section_a.rows.iter().zip(section_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;
}
}
}
} else {
return false;
}
}
true
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(100))]
/// Property 3: LLM↔Human Conversion Round-Trip
/// For any valid LLM format string, converting to Human format and back to LLM format
/// SHALL produce semantically equivalent output.
///
/// **Feature: dx-serializer-llm-human, Property 3: LLM↔Human Conversion Round-Trip**
/// **Validates: Requirements 6.1-6.5, 7.1-7.5, 9.3**
#[test]
fn prop_llm_human_llm_round_trip(doc in arb_document()) {
// Document -> LLM -> Human -> LLM -> Document
let llm1 = document_to_llm(&doc);
let human = llm_to_human(&llm1).unwrap();
let llm2 = human_to_llm(&human).unwrap();
let round_trip_doc = llm_to_document(&llm2).unwrap();
prop_assert!(
documents_equal(&doc, &round_trip_doc),
"LLM↔Human round-trip failed:\nOriginal: {:?}\nLLM1: {}\nHuman: {}\nLLM2: {}\nParsed: {:?}",
doc, llm1, human, llm2, round_trip_doc
);
}
/// Property 4: Special Value Preservation
/// For any DxValue containing booleans or null, converting through any format sequence
/// SHALL preserve the semantic value.
///
/// **Feature: dx-serializer-llm-human, Property 4: Special Value Preservation**
/// **Validates: Requirements 1.5-1.7, 2.4-2.6, 3.4-3.6, 4.4-4.6, 6.3, 7.3**
#[test]
fn prop_special_values_preserved(b in proptest::bool::ANY) {
let mut doc = DxDocument::new();
doc.context.insert("flag".to_string(), DxLlmValue::Bool(b));
doc.context.insert("empty".to_string(), DxLlmValue::Null);
// LLM -> Human -> LLM
let llm1 = document_to_llm(&doc);
let human = llm_to_human(&llm1).unwrap();
let llm2 = human_to_llm(&human).unwrap();
let round_trip_doc = llm_to_document(&llm2).unwrap();
// Check boolean preserved
let flag_value = round_trip_doc.context.get("flag").unwrap();
prop_assert_eq!(flag_value.as_bool(), Some(b), "Boolean not preserved");
// Check null preserved
let empty_value = round_trip_doc.context.get("empty").unwrap();
prop_assert!(empty_value.is_null(), "Null not preserved");
}
/// Property 5: Reference Resolution Correctness
/// For any DxDocument with references, resolving ^key pointers SHALL always produce
/// the correct referenced value.
///
/// **Feature: dx-serializer-llm-human, Property 5: Reference Resolution Correctness**
/// **Validates: Requirements 1.4, 2.2, 3.7, 6.2, 7.2**
#[test]
fn prop_reference_resolution(ref_value in "[a-zA-Z][a-zA-Z0-9]{5,15}") {
let mut doc = DxDocument::new();
doc.refs.insert("A".to_string(), ref_value.clone());
let mut section = DxSection::new(vec!["id".to_string(), "val".to_string()]);
section.rows.push(vec![
DxLlmValue::Num(1.0),
DxLlmValue::Ref("A".to_string()),
]);
doc.sections.insert('d', section);
// LLM -> Human (references should be resolved)
let llm = document_to_llm(&doc);
let human = llm_to_human(&llm).unwrap();
// The human format should contain the resolved value
prop_assert!(
human.contains(&ref_value),
"Reference not resolved in Human format: {}",
human
);
}
/// Property: Machine format round-trip preserves all values
///
/// **Feature: dx-serializer-llm-human, Property 3: LLM↔Human Conversion Round-Trip**
/// **Validates: Requirements 8.1-8.3**
#[test]
fn prop_machine_round_trip(doc in arb_document()) {
// Document -> Machine -> Document
let machine = document_to_machine(&doc);
let round_trip_doc = machine_to_document(&machine).unwrap();
prop_assert!(
documents_equal(&doc, &round_trip_doc),
"Machine format round-trip failed:\nOriginal: {:?}\nParsed: {:?}",
doc, round_trip_doc
);
}
}
#[test]
fn test_llm_human_llm_basic() {
let mut doc = DxDocument::new();
doc.context.insert("nm".to_string(), DxLlmValue::Str("Test".to_string()));
doc.context.insert("ct".to_string(), DxLlmValue::Num(42.0));
doc.context.insert("ac".to_string(), DxLlmValue::Bool(true));
let mut section = DxSection::new(vec!["id".to_string(), "vl".to_string()]);
section
.add_row(vec![DxLlmValue::Num(1.0), DxLlmValue::Str("Alpha".to_string())])
.unwrap();
doc.sections.insert('d', section);
// Document -> LLM -> Human -> LLM -> Document
let llm1 = document_to_llm(&doc);
let human = llm_to_human(&llm1).unwrap();
let llm2 = human_to_llm(&human).unwrap();
let round_trip_doc = llm_to_document(&llm2).unwrap();
assert!(documents_equal(&doc, &round_trip_doc));
}
#[test]
fn test_special_values_through_all_formats() {
let mut doc = DxDocument::new();
doc.context.insert("flag_true".to_string(), DxLlmValue::Bool(true));
doc.context.insert("flag_false".to_string(), DxLlmValue::Bool(false));
doc.context.insert("empty".to_string(), DxLlmValue::Null);
// LLM format
let llm = document_to_llm(&doc);
assert!(llm.contains("|+"), "LLM should use + for true");
assert!(llm.contains("|-"), "LLM should use - for false");
assert!(llm.contains("|~"), "LLM should use ~ for null");
// Human format
let human = llm_to_human(&llm).unwrap();
assert!(human.contains("true"), "Human should use true");
assert!(human.contains("false"), "Human should use false");
assert!(human.contains("null"), "Human should use null");
// Back to LLM
let llm2 = human_to_llm(&human).unwrap();
assert!(llm2.contains("|+"), "LLM should use + for true");
assert!(llm2.contains("|-"), "LLM should use - for false");
assert!(llm2.contains("|~"), "LLM should use ~ for null");
}
#[test]
fn test_reference_resolution_in_human() {
let mut doc = DxDocument::new();
doc.refs.insert("A".to_string(), "Shared Value Here".to_string());
let mut section = DxSection::new(vec!["id".to_string(), "val".to_string()]);
section
.add_row(vec![DxLlmValue::Num(1.0), DxLlmValue::Ref("A".to_string())])
.unwrap();
doc.sections.insert('d', section);
let llm = document_to_llm(&doc);
assert!(llm.contains("^A"), "LLM should contain reference pointer");
let human = llm_to_human(&llm).unwrap();
assert!(human.contains("Shared Value Here"), "Human should contain resolved value");
}
#[test]
fn test_machine_format_preserves_all_types() {
let mut doc = DxDocument::new();
doc.context.insert("str".to_string(), DxLlmValue::Str("Hello".to_string()));
doc.context.insert("num".to_string(), DxLlmValue::Num(3.14));
doc.context.insert("bool_t".to_string(), DxLlmValue::Bool(true));
doc.context.insert("bool_f".to_string(), DxLlmValue::Bool(false));
doc.context.insert("null".to_string(), DxLlmValue::Null);
doc.context.insert(
"arr".to_string(),
DxLlmValue::Arr(vec![
DxLlmValue::Num(1.0),
DxLlmValue::Num(2.0),
DxLlmValue::Num(3.0),
]),
);
doc.refs.insert("R".to_string(), "Reference Value".to_string());
let machine = document_to_machine(&doc);
let round_trip = machine_to_document(&machine).unwrap();
assert_eq!(round_trip.context.get("str").unwrap().as_str(), Some("Hello"));
assert!((round_trip.context.get("num").unwrap().as_num().unwrap() - 3.14).abs() < 0.001);
assert_eq!(round_trip.context.get("bool_t").unwrap().as_bool(), Some(true));
assert_eq!(round_trip.context.get("bool_f").unwrap().as_bool(), Some(false));
assert!(round_trip.context.get("null").unwrap().is_null());
let arr = round_trip.context.get("arr").unwrap().as_arr().unwrap();
assert_eq!(arr.len(), 3);
assert_eq!(round_trip.refs.get("R"), Some(&"Reference Value".to_string()));
}
}
*/