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
use json_eval_rs::JSONEval;
use serde_json::Value;
use std::fs;
use std::path::PathBuf;
fn load_fixture_schema() -> String {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests/fixtures/minimal_form.json");
fs::read_to_string(path).expect("Failed to read minimal_form.json fixture")
}
#[test]
fn test_evaluate_dependents_recursive_chain() {
let schema = load_fixture_schema();
// Chain: occupation -> occupation_class -> risk_category
// Logic from fixture:
// occupation: "OFFICE" -> occupation_class: "1" -> risk_category: "Low"
// occupation: "MANUAL" -> occupation_class: "2" -> risk_category: "Medium"
// occupation: "HIGH_RISK" -> occupation_class: "3" -> risk_category: "High"
// Initial state: OFFICE
let data = r#"{
"illustration": {
"insured": {
"occupation": "OFFICE",
"occupation_class": "1",
"risk_category": "Low"
}
}
}"#;
let mut je = JSONEval::new(&schema, None, Some(data)).unwrap();
// Update occupation to MANUAL
let new_data_snippet = r#"{
"illustration": {
"insured": {
"occupation": "MANUAL"
}
}
}"#;
let changed_paths = vec!["illustration.insured.occupation".to_string()];
let result = je
.evaluate_dependents(
&changed_paths,
Some(new_data_snippet),
None,
true,
None,
None,
true,
)
.unwrap();
let changes = result.as_array().unwrap();
// Verify occupation_class changed to "2"
let class_change = changes
.iter()
.find(|c| {
c.get("$ref").and_then(|r| r.as_str()) == Some("illustration.insured.occupation_class")
})
.expect("occupation_class should change");
assert_eq!(
class_change.get("value"),
Some(&Value::String("2".to_string()))
);
// Verify risk_category changed to "Medium" (recursive side effect)
let risk_change = changes
.iter()
.find(|c| {
c.get("$ref").and_then(|r| r.as_str()) == Some("illustration.insured.risk_category")
})
.expect("risk_category should change recursively");
assert_eq!(
risk_change.get("value"),
Some(&Value::String("Medium".to_string()))
);
// Check final data state
let data = je.eval_data.data();
assert_eq!(
data.pointer("/illustration/insured/occupation_class"),
Some(&Value::String("2".to_string()))
);
assert_eq!(
data.pointer("/illustration/insured/risk_category"),
Some(&Value::String("Medium".to_string()))
);
}
#[test]
fn test_evaluate_dependents_keep_hidden_value() {
let schema = load_fixture_schema();
// Field: illustration.header.extra_comments
// Condition: Hidden if form_number == "HIDE"
// Config: keepHiddenValue = true
// Initial state: form_number="SHOW", extra_comments="Existing Data"
let data = r#"{
"illustration": {
"header": {
"form_number": "SHOW",
"extra_comments": "Existing Data"
}
}
}"#;
let mut je = JSONEval::new(&schema, None, Some(data)).unwrap();
// Change form_number to HIDE
// Note: replace_data_and_context replaces top-level objects, so we must provide the full object or use a different update method.
// For this test, we provide the full header to ensure extra_comments isn't lost due to data loading.
let new_data_snippet = r#"{
"illustration": {
"header": {
"form_number": "HIDE",
"extra_comments": "Existing Data"
}
}
}"#;
let changed_paths = vec!["illustration.header.form_number".to_string()];
let result = je
.evaluate_dependents(
&changed_paths,
Some(new_data_snippet),
None,
true,
None,
None,
true,
)
.unwrap();
let changes = result.as_array().unwrap();
// We might accept a change event saying it's hidden, but crucially we must NOT see a "clear": true or a null value update
// Note: implementation details might vary. The key requirement is DATA PRESERVATION.
let comments_change = changes.iter().find(|c| {
c.get("$ref").and_then(|r| r.as_str()) == Some("illustration.header.extra_comments")
});
if let Some(change) = comments_change {
// If there IS a change event, make sure it's not clearing the value
assert_ne!(
change.get("clear"),
Some(&Value::Bool(true)),
"Should not clear value due to keepHiddenValue"
);
assert_ne!(
change.get("value"),
Some(&Value::Null),
"Should not set value to null"
);
// It SHOULD report hidden state though (if logic emits it)
if let Some(hidden) = change.get("$hidden") {
assert_eq!(hidden, &Value::Bool(true), "Should report hidden state");
}
}
// Verify Data is STILL there
let data = je.eval_data.data();
let comments_val = data.pointer("/illustration/header/extra_comments");
assert_eq!(
comments_val,
Some(&Value::String("Existing Data".to_string())),
"Data should be preserved"
);
}
#[test]
fn test_recursive_clearing() {
// Test case: C changes -> B hidden -> B cleared -> A hidden -> A cleared
let schema = load_fixture_schema();
// Initial state: C=false, B=false, A="Initial"
// All visible.
let data = r#"{
"illustration": {
"header": {
"recursive_test": {
"field_c": false,
"field_b": false,
"field_a": "Initial"
}
}
}
}"#;
let mut je = JSONEval::new(&schema, None, Some(data)).unwrap();
// Set C = true, but keep B and A to verify strict clearing logic
// Logic:
// B hidden if C==true. So B becomes hidden.
// A hidden if B is EMPTY. B gets cleared (null). So A becomes hidden.
let new_data_snippet = r#"{
"illustration": {
"header": {
"recursive_test": {
"field_c": true,
"field_b": false,
"field_a": "Initial"
}
}
}
}"#;
let changed_paths = vec!["illustration.header.recursive_test.field_c".to_string()];
let result = je
.evaluate_dependents(
&changed_paths,
Some(new_data_snippet),
None,
true,
None,
None,
true,
)
.unwrap();
let changes = result.as_array().unwrap();
let data = je.eval_data.data();
// Verify B is cleared
let val_b = data.pointer("/illustration/header/recursive_test/field_b");
assert!(
val_b.is_none() || val_b == Some(&Value::Null),
"Field B should be cleared"
);
// Verify A is cleared
let val_a = data.pointer("/illustration/header/recursive_test/field_a");
assert!(
val_a.is_none() || val_a == Some(&Value::Null),
"Field A should be cleared recursively"
);
// Verify changes
// Should see clear events for B and A
let b_change = changes
.iter()
.find(|c| {
c.get("$ref").and_then(|r| r.as_str())
== Some("illustration.header.recursive_test.field_b")
})
.expect("Should report field_b change");
assert_eq!(b_change.get("$hidden"), Some(&Value::Bool(true)));
// A might be in the changes list, or just implicit. But structurally it should be reported.
let a_change = changes.iter().find(|c| {
c.get("$ref").and_then(|r| r.as_str()) == Some("illustration.header.recursive_test.field_a")
});
// depending on depth of recursion, it should be there because we iterate until stable or process queue
assert!(a_change.is_some(), "Field A change should be reported");
if let Some(c) = a_change {
assert_eq!(c.get("$hidden"), Some(&Value::Bool(true)));
}
}