#[cfg(test)]
mod tests {
use super::super::common::vm_query;
use serde_json::json;
#[test]
fn test_let_alias_two_levels_fuses_to_root() {
let doc = json!({"a": 1});
let r = vm_query(
r#"let x = $ in let y = x in patch y { c: 3 }"#,
&doc,
)
.unwrap();
assert_eq!(r, json!({"a": 1, "c": 3}));
}
#[test]
fn test_let_alias_three_levels_fuses_to_root() {
let doc = json!({"a": 1});
let r = vm_query(
r#"let x = $ in let y = x in let z = y in patch z { c: 3 }"#,
&doc,
)
.unwrap();
assert_eq!(r, json!({"a": 1, "c": 3}));
}
#[test]
fn test_let_alias_chain_with_root_write_pair() {
let doc = json!({});
let r = vm_query(
r#"let x = $ in let y = x in $.a.set(1) | $.b.set(2)"#,
&doc,
)
.unwrap();
assert_eq!(r, json!({"a": 1, "b": 2}));
}
#[test]
fn test_lambda_body_pre_flush() {
let doc = json!({"items": [1, 2, 3]});
let r = vm_query(
r#"$.added.set(true) | $.items.map(lambda x: x + 1)"#,
&doc,
)
.unwrap();
assert_eq!(r, json!([2, 3, 4]));
}
#[test]
fn test_comprehension_source_writes_flush_before_iter() {
let doc = json!({"list": [{"n": 10}, {"n": 20}, {"n": 30}]});
let r = vm_query(
r#"$.touched.set(true) | [x.n + 1 for x in $.list]"#,
&doc,
)
.unwrap();
assert_eq!(r, json!([11, 21, 31]));
}
#[test]
fn test_if_branch_writes_dont_leak_across_branches() {
let doc = json!({"flag": true});
let r1 = vm_query(r#""then-result" if $.flag else "else-result""#, &doc).unwrap();
assert_eq!(r1, json!("then-result"));
let doc = json!({"flag": false});
let r2 = vm_query(r#""then-result" if $.flag else "else-result""#, &doc).unwrap();
assert_eq!(r2, json!("else-result"));
}
#[test]
fn test_try_default_flushes_before_handler() {
let doc = json!({});
let r = vm_query(r#"try $.missing.field else "fallback""#, &doc).unwrap();
assert_eq!(r, json!("fallback"));
}
#[test]
fn test_read_of_root_flushes_pending() {
let doc = json!({"a": 5});
let r = vm_query(
r#"$.a.set(10) | @.a"#,
&doc,
)
.unwrap();
assert_eq!(r, json!(10));
}
#[test]
fn test_read_of_aliased_local_flushes_root_batch() {
let doc = json!({"a": 0});
let r = vm_query(
r#"let x = $.a.set(42) in x.a"#,
&doc,
)
.unwrap();
assert_eq!(r, json!(42));
}
#[test]
fn test_complex_query_with_lets_objects_pipes_fuses_correctly() {
let doc = json!({});
let r = vm_query(
r#"let x = $ in $.a.set(1) | $.b.set(2) | $.c.set(3)"#,
&doc,
)
.unwrap();
assert_eq!(r, json!({"a": 1, "b": 2, "c": 3}));
}
#[test]
fn test_lambda_inner_writes_are_scope_isolated() {
let doc = json!({"list": [{"id": 1}, {"id": 2}]});
let r = vm_query(
r#"$.list.map(lambda o: o.id.set(99))"#,
&doc,
)
.unwrap();
assert_eq!(r, json!([99, 99]));
}
#[test]
fn test_alias_chain_depth_safe_no_hang() {
let doc = json!({"v": 7});
let r = vm_query(
r#"let a = $ in let b = a in let c = b in let d = c in let e = d in e.v"#,
&doc,
)
.unwrap();
assert_eq!(r, json!(7));
}
}