qcl 0.1.5

A Query Check Language written in Rust
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
#[cfg(test)]
mod test {
    use crate::error::Result;
    #[cfg(feature = "json")]
    use serde_json::json;
    use std::collections::BTreeSet;
    use std::sync::Mutex;

    use crate::{
        expr::{
            Expr, parse_cache_entries_per_shard_for_tests, parse_cache_entry_count_for_tests,
            parse_cache_shard_count_for_tests, parse_cache_shard_idx_for_tests, reset_parse_cache_for_tests,
        },
        val::Val,
    };

    static PARSE_CACHE_TEST_LOCK: Mutex<()> = Mutex::new(());

    #[test]
    #[cfg(feature = "json")]
    fn simple() {
        expect("@pub", true);
        expect("@user.name + 'pt'", "lkpt");
        expect("@user.age + @list.0 == 19", true);

        #[cfg(feature = "adv_arith")]
        expect("@user.name + @user.age", "lk18");

        #[cfg(feature = "adv_arith")]
        expect("@list + @list-2", vec![1, 2, 3, 2]);

        #[cfg(feature = "adv_arith")]
        expect("@list - @list-2", vec![1, 3]);

        #[cfg(feature = "sem_arith")]
        expect("@list.2 / 2", 1.5);

        #[cfg(not(feature = "sem_arith"))]
        expect("@list.2 / 2", 1);

        panic("@user.name + @list");

        #[cfg(not(feature = "adv_arith"))]
        panic("@user.name + @list-2.0");
    }

    #[test]
    #[cfg(feature = "json")]
    fn complex_expressions() {
        // Nested arithmetic operations
        expect("(@user.age + 2) * 3", 60);

        // Parenthesized expressions
        expect("@user.age * (2 + 1)", 54);

        // Multiple operators with precedence
        expect("@user.age + 2 * 3", 24);

        // Comparison operators
        expect("@user.age > 17", true);
        expect("@user.age < 19", true);
        expect("@user.age >= 18", true);
        expect("@user.age <= 18", true);
        expect("@user.age == 18", true);
        expect("@user.age != 19", true);
    }

    #[test]
    #[cfg(feature = "json")]
    fn logical_operators() {
        // AND operator
        expect("@pub && @user.age > 17", true);
        expect("@pub && @user.age > 20", false);

        // OR operator
        expect("@user.age > 20 || @pub", true);
        expect("@user.age > 20 || @user.name == 'john'", false);

        // Complex logical expressions
        expect("@pub && (@user.age > 17 || @user.name == 'john')", true);
        expect("@pub || (@user.age < 17 && @user.name == 'john')", true);

        // Short-circuit evaluation
        expect("false && @nonexistent.field", false);
        expect("true || @nonexistent.field", true);
    }

    #[test]
    #[cfg(feature = "json")]
    fn unary_operations() {
        // Logical NOT
        expect("!@pub", false);
        expect("!false", true);

        // Double negation
        expect("!!@pub", true);

        // NOT with expressions
        expect("!(@user.age > 20)", true);

        // Unary minus on integers
        expect("-1", -1);
        expect("-@user.age", -18);
        expect("-(1 + 2)", -3);

        // Unary minus on floats
        expect("-1.5", -1.5);

        // Double negation with minus
        expect("--1", 1);
        expect("-(-1)", 1);

        // Unary minus in expressions
        expect("-1 + 2", 1);
        expect("1 + -2", -1);
        expect("-@list.0 + @list.2", 2);

        // Unary minus with multiplication
        expect("-2 * 3", -6);
        expect("2 * -3", -6);
    }

    #[test]
    #[cfg(feature = "json")]
    fn map_membership() {
        // String key in map
        expect("'name' in @user", true);
        expect("'age' in @user", true);
        expect("'nonexistent' in @user", false);

        // Map membership with literal maps
        expect(r#""key" in {"key": 1, "other": 2}"#, true);
        expect(r#""missing" in {"key": 1}"#, false);
    }

    #[test]
    #[cfg(feature = "json")]
    fn map_and_list_access() {
        // Nested map access
        expect("@nested.level1.level2", "value");

        // List access with variable index
        expect("@list.(@index)", 2);

        // Access with expressions
        // `index-1` is an Id, but `index - 1` is a BinOp
        expect("@list.(@index - 1)", 1);

        // Access with complex expressions
        #[cfg(feature = "adv_arith")]
        expect("@list-2.(2 - 2) + @user.name", "2lk");
    }

    #[test]
    #[cfg(feature = "json")]
    fn list_literals() {
        // Empty list
        expect("[]", Vec::<Val>::new());

        // Simple list
        expect("[1, 2, 3]", vec![1, 2, 3]);

        // Mixed types
        expect(
            r#"[1, "hello", true]"#,
            vec![Val::Int(1), Val::Str("hello".into()), Val::Bool(true)],
        );

        // Nested lists
        expect("[[1, 2], [3, 4]]", vec![vec![1, 2], vec![3, 4]]);

        // List with expressions
        expect("[1 + 2, 3 * 4]", vec![3, 12]);

        // List with context access
        expect("[@user.age, @list.0]", vec![18, 1]);
    }

    #[test]
    #[cfg(feature = "json")]
    fn map_literals() {
        use hashbrown::HashMap;

        // Empty map
        expect("{}", HashMap::<String, Val>::new());

        // Simple map
        let mut expected = HashMap::new();
        expected.insert("name".to_string(), Val::Str("Alice".into()));
        expected.insert("age".to_string(), Val::Int(30));
        expect(r#"{"name": "Alice", "age": 30}"#, expected);

        // Map with expressions
        let mut expected = HashMap::new();
        expected.insert("sum".to_string(), Val::Int(5));
        expected.insert("product".to_string(), Val::Int(6));
        expect(r#"{"sum": 2 + 3, "product": 2 * 3}"#, expected);

        // Map with context access
        let mut expected = HashMap::new();
        expected.insert("user_name".to_string(), Val::Str("lk".into()));
        expected.insert("user_age".to_string(), Val::Int(18));
        expect(r#"{"user_name": @user.name, "user_age": @user.age}"#, expected);

        // Map with different key types
        let mut expected = HashMap::new();
        expected.insert("42".to_string(), Val::Str("number".into()));
        expected.insert("true".to_string(), Val::Str("bool".into()));
        expected.insert("key".to_string(), Val::Str("string".into()));
        expect(r#"{42: "number", true: "bool", "key": "string"}"#, expected);
    }

    #[test]
    #[cfg(feature = "json")]
    fn map_literal_key_stringification_constant_and_runtime() {
        use hashbrown::HashMap;

        let mut folded_expected = HashMap::new();
        folded_expected.insert("folded".to_string(), Val::Str("string".into()));
        folded_expected.insert("42".to_string(), Val::Str("number".into()));
        folded_expected.insert("true".to_string(), Val::Str("bool".into()));
        expect(r#"{"folded": "string", 42: "number", true: "bool"}"#, folded_expected);

        let mut runtime_expected = HashMap::new();
        runtime_expected.insert("lk".to_string(), Val::Str("string".into()));
        runtime_expected.insert("18".to_string(), Val::Str("number".into()));
        runtime_expected.insert("true".to_string(), Val::Str("bool".into()));
        expect(
            r#"{@user.name: "string", @user.age: "number", @pub: "bool"}"#,
            runtime_expected,
        );
    }

    #[test]
    #[cfg(feature = "json")]
    fn map_literal_invalid_key_types_error() {
        panic(r#"{[1, 2]: "invalid"}"#);
        panic(r#"{{}: "invalid"}"#);
        panic(r#"{@list: "invalid"}"#);
        panic(r#"{@nested: "invalid"}"#);
    }

    #[test]
    #[cfg(feature = "json")]
    fn constant_folding_large_list_and_map() {
        let list_expr = format!("[{}]", (0..128).map(|i| i.to_string()).collect::<Vec<_>>().join(", "));
        let parsed_list = Expr::try_from(list_expr.as_str()).unwrap();
        let expected_list = Val::List((0..128).map(Val::Int).collect::<Vec<_>>().into());
        assert_eq!(parsed_list, Expr::Val(expected_list));

        let map_expr = format!(
            "{{{}}}",
            (0..128).map(|i| format!("{i}: {i}")).collect::<Vec<_>>().join(", ")
        );
        let parsed_map = Expr::try_from(map_expr.as_str()).unwrap();
        let expected_map = Val::Map(
            (0..128)
                .map(|i| (i.to_string(), Val::Int(i)))
                .collect::<hashbrown::HashMap<_, _>>()
                .into(),
        );
        assert_eq!(parsed_map, Expr::Val(expected_map));
    }

    #[test]
    #[cfg(feature = "json")]
    fn nested_structures() {
        use hashbrown::HashMap;

        // List of maps
        let mut map1 = HashMap::new();
        map1.insert("name".to_string(), Val::Str("Alice".into()));
        map1.insert("age".to_string(), Val::Int(30));

        let mut map2 = HashMap::new();
        map2.insert("name".to_string(), Val::Str("Bob".into()));
        map2.insert("age".to_string(), Val::Int(25));

        expect(
            r#"[{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]"#,
            vec![Val::Map(map1.into()), Val::Map(map2.into())],
        );

        // Map with lists
        let mut expected = HashMap::new();
        expected.insert(
            "numbers".to_string(),
            Val::List(vec![Val::Int(1), Val::Int(2), Val::Int(3)].into()),
        );
        expected.insert("active".to_string(), Val::Bool(true));
        expect(r#"{"numbers": [1, 2, 3], "active": true}"#, expected);
    }

    #[test]
    #[cfg(feature = "json")]
    fn literal_access() {
        // Access elements from list literals
        expect("[1, 2, 3].1", 2);
        expect(r#"["hello", "world"].0"#, "hello");

        // Access fields from map literals
        expect(r#"{"name": "Alice", "age": 30}.name"#, "Alice");
        expect(r#"{"name": "Alice", "age": 30}.age"#, 30);

        // Nested access
        expect(r#"[{"name": "Alice"}, {"name": "Bob"}].0.name"#, "Alice");
        expect(r#"{"users": [1, 2, 3]}.users.1"#, 2);
    }

    #[test]
    #[cfg(feature = "json")]
    fn trailing_commas() {
        // List with trailing comma
        expect("[1, 2, 3,]", vec![1, 2, 3]);

        // Map with trailing comma
        use hashbrown::HashMap;
        let mut expected = HashMap::new();
        expected.insert("a".to_string(), Val::Int(1));
        expected.insert("b".to_string(), Val::Int(2));
        expect(r#"{"a": 1, "b": 2,}"#, expected);
    }

    #[test]
    #[cfg(feature = "json")]
    fn error_cases() {
        // Invalid map key types
        panic(r#"{[1, 2]: "invalid"}"#);
        panic(r#"{{}: "invalid"}"#);
    }

    #[test]
    fn test_requested_ctx() {
        let expr = Expr::try_from("@user.props.(@req.service) && @list.0 || @pub").unwrap();
        let names = expr.requested_ctx();

        let mut expected = BTreeSet::new();
        expected.insert("user".to_string());
        expected.insert("req".to_string());
        expected.insert("list".to_string());
        expected.insert("pub".to_string());

        assert_eq!(names, expected);

        // Test with list/map literals containing context access
        let expr = Expr::try_from(r#"[@user.name, @list.0] == {"name": @user.name, "first": @list.0}"#).unwrap();
        let names = expr.requested_ctx();

        let mut expected = BTreeSet::new();
        expected.insert("user".to_string());
        expected.insert("list".to_string());

        assert_eq!(names, expected);
    }

    #[test]
    fn test_is_ctx_independent() {
        assert!(
            Expr::try_from(r#"{"a": [1, 2, 3].0, "b": (1 + 2) * 3}"#)
                .unwrap()
                .is_ctx_independent()
        );
        assert!(!Expr::try_from("@user.name").unwrap().is_ctx_independent());
        assert!(
            !Expr::try_from(r#"[@user.name, {"k": 1}]"#)
                .unwrap()
                .is_ctx_independent()
        );
        assert!(!Expr::try_from(r#"({"k": @user.name})"#).unwrap().is_ctx_independent());
    }

    #[test]
    fn parse_cache_remains_functional_under_pressure() {
        let _guard = PARSE_CACHE_TEST_LOCK.lock().unwrap();
        reset_parse_cache_for_tests();

        for i in 0..5000 {
            let text = format!("{i} == {i}");
            let expr = Expr::parse_cached_arc(text.as_str()).unwrap();
            assert_eq!(expr.eval(&Val::Nil).unwrap(), Val::Bool(true));
        }

        let payload = "x".repeat(5000);
        let large = format!(r#""{payload}" == "{payload}""#);
        let expr = Expr::parse_cached_arc(large.as_str()).unwrap();
        assert_eq!(expr.eval(&Val::Nil).unwrap(), Val::Bool(true));
    }

    #[test]
    fn parse_cache_eviction_stays_local_to_shards() {
        let _guard = PARSE_CACHE_TEST_LOCK.lock().unwrap();
        reset_parse_cache_for_tests();

        let protected_expr = "1 == 1";
        let protected = Expr::parse_cached_arc(protected_expr).unwrap();
        let protected_shard = parse_cache_shard_idx_for_tests(protected_expr);
        let target_shard = (protected_shard + 1) % parse_cache_shard_count_for_tests();
        let hot_count = parse_cache_entries_per_shard_for_tests() + 64;

        let hot_exprs = collect_exprs_for_shard(target_shard, hot_count);
        for expr in &hot_exprs {
            let parsed = Expr::parse_cached_arc(expr).unwrap();
            assert_eq!(parsed.eval(&Val::Nil).unwrap(), Val::Bool(true));
        }

        let protected_again = Expr::parse_cached_arc(protected_expr).unwrap();
        assert!(std::sync::Arc::ptr_eq(&protected, &protected_again));
        assert!(parse_cache_entry_count_for_tests() <= parse_cache_entries_per_shard_for_tests() + 1);
    }

    #[test]
    fn parse_cache_uses_smooth_local_fifo_eviction() {
        let _guard = PARSE_CACHE_TEST_LOCK.lock().unwrap();
        reset_parse_cache_for_tests();

        let target_shard = 0usize;
        let exprs = collect_exprs_for_shard(target_shard, parse_cache_entries_per_shard_for_tests() + 2);

        let first = Expr::parse_cached_arc(exprs[0].as_str()).unwrap();
        for expr in &exprs[1..] {
            let parsed = Expr::parse_cached_arc(expr).unwrap();
            assert_eq!(parsed.eval(&Val::Nil).unwrap(), Val::Bool(true));
        }

        let first_again = Expr::parse_cached_arc(exprs[0].as_str()).unwrap();
        let newest_again = Expr::parse_cached_arc(exprs.last().unwrap().as_str()).unwrap();

        assert!(!std::sync::Arc::ptr_eq(&first, &first_again));
        assert!(std::sync::Arc::ptr_eq(
            &Expr::parse_cached_arc(exprs.last().unwrap().as_str()).unwrap(),
            &newest_again
        ));
        assert!(parse_cache_entry_count_for_tests() <= parse_cache_entries_per_shard_for_tests());
    }

    #[test]
    fn parse_cache_lru_promotes_accessed_entries() {
        let _guard = PARSE_CACHE_TEST_LOCK.lock().unwrap();
        reset_parse_cache_for_tests();

        let target_shard = 0usize;
        let per_shard = parse_cache_entries_per_shard_for_tests();
        // Need: per_shard entries to fill + 1 extra to trigger eviction
        let exprs = collect_exprs_for_shard(target_shard, per_shard + 1);

        // Fill shard to capacity
        for expr in &exprs[..per_shard] {
            Expr::parse_cached_arc(expr).unwrap();
        }
        let first = Expr::parse_cached_arc(exprs[0].as_str()).unwrap();

        // Touch the first entry (LRU promotes it to most-recent)
        let first_touched = Expr::parse_cached_arc(exprs[0].as_str()).unwrap();
        assert!(std::sync::Arc::ptr_eq(&first, &first_touched));

        // Insert one more — should evict exprs[1] (the actual LRU), NOT exprs[0]
        Expr::parse_cached_arc(exprs[per_shard].as_str()).unwrap();

        // exprs[0] should still be cached (was touched/promoted)
        let first_after = Expr::parse_cached_arc(exprs[0].as_str()).unwrap();
        assert!(std::sync::Arc::ptr_eq(&first, &first_after));

        // exprs[1] should have been evicted (it was the least recently used)
        let second_before = Expr::parse_cached_arc(exprs[1].as_str()).unwrap();
        // Re-parsed, so it's a new Arc — can't ptr_eq with anything cached before
        // Just verify it still works
        assert_eq!(second_before.eval(&Val::Nil).unwrap(), Val::Bool(true));
    }

    #[test]
    #[cfg(feature = "json")]
    fn test_nil_handling() {
        expect("@nonexistent", None::<Val>);
        expect("@existing_null == nil", true);
        expect("@nonexistent == nil", true);
        expect("@nonexistent != nil", false);
        expect("@nonexistent != 1", true);
        expect("@nonexistent.field == nil", true);
        expect("nil", None::<Val>);
    }

    #[test]
    #[cfg(feature = "json")]
    fn nil_field_access() {
        let ctx: Val = json!({
            "req": {"user": {"id": 7}},
            "record": {}
        })
        .into();

        let expr = Expr::try_from("@req.user.id == @record.owner.id").unwrap();
        assert_eq!(expr.eval(&ctx).unwrap(), Val::Bool(false));

        let expr = Expr::try_from(r#"@req.user.status != "blocked""#).unwrap();
        assert_eq!(expr.eval(&ctx).unwrap(), Val::Bool(true));

        let expr = Expr::try_from(r#"@req.user.status != "blocked" && @req.user.id == 7"#).unwrap();
        assert_eq!(expr.eval(&ctx).unwrap(), Val::Bool(true));
    }

    #[test]
    #[cfg(feature = "json")]
    fn unsupported_in_membership_fails_closed() {
        panic(r#"@user.age in {"18": true}"#);

        #[cfg(feature = "adv_arith")]
        panic("@user.age in 21");
    }

    #[test]
    #[cfg(feature = "json")]
    fn zero_division_and_modulo_fail_closed() {
        let static_mod = Expr::try_from("1 % 0").unwrap();
        assert!(static_mod.eval(&Val::Nil).is_err());

        let static_div = Expr::try_from("1 / 0").unwrap();
        assert!(static_div.eval(&Val::Nil).is_err());

        let ctx: Val = json!({ "x": 1 }).into();

        let dynamic_mod = Expr::try_from("@x % 0").unwrap();
        assert!(dynamic_mod.eval(&ctx).is_err());

        let dynamic_div = Expr::try_from("@x / 0").unwrap();
        assert!(dynamic_div.eval(&ctx).is_err());
    }

    #[test]
    #[cfg(feature = "json")]
    fn test_quoted_field_access() {
        // Basic quoted field access
        expect(r#"@"with.&=""#, true);

        // Nested quoted field access
        expect(r#"@req."user"."name""#, "lk");

        // Mixed quoted and unquoted access
        expect(r#"@user."name""#, "lk");
        expect(r#"@"user".name"#, "lk");
        expect(r#"@"user"."name""#, "lk");

        // Quoted field with special characters
        expect(r#"@"special-chars""#, "test-value");

        // Quoted field in complex expression
        expect(r#"@"with.&=" && @user.age > 17"#, true);
        expect(r#"@user."name" + "-suffix""#, "lk-suffix");

        // Quoted numeric field name
        expect(r#"@"123""#, "numeric-field");

        // Single quotes vs double quotes
        expect(r#"@'special-chars'"#, "test-value");
        expect(r#"@escaped."field\"name""#, "quoted-field");
        expect(r#"@escaped."path\\segment""#, "backslash-field");
        expect(r#"@escaped.'field\'name'"#, "single-quoted-field");
    }

    #[test]
    #[cfg(feature = "json")]
    fn ternary_operator() {
        // Basic ternary
        expect("true ? 1 : 2", 1);
        expect("false ? 1 : 2", 2);

        // Ternary with context
        expect("@pub ? 'yes' : 'no'", "yes");
        expect("@user.age >= 18 ? 'adult' : 'child'", "adult");

        // Nested ternary (right-associative)
        expect("true ? false ? 1 : 2 : 3", 2);
        expect("false ? 1 : true ? 2 : 3", 2);

        // Ternary with different value types
        expect("true ? 'hello' : 42", "hello");
        expect("false ? 'hello' : 42", 42);

        // Non-bool condition errors
        panic("1 ? 2 : 3");
        panic("'str' ? 2 : 3");
        panic("nil ? 2 : 3");
    }

    #[test]
    #[cfg(feature = "json")]
    fn coalesce_operator() {
        // Basic coalesce - non-nil passes through
        expect("1 ?? 2", 1);
        expect("'hello' ?? 'world'", "hello");
        expect("true ?? false", true);

        // Nil falls through to right
        expect("nil ?? 42", 42);
        expect("nil ?? 'fallback'", "fallback");

        // With context - existing field doesn't coalesce
        expect("@user.name ?? 'anonymous'", "lk");

        // With context - missing field coalesces
        expect("@nonexistent ?? 'default'", "default");

        // Chained coalesce
        expect("nil ?? nil ?? 42", 42);
        expect("nil ?? 1 ?? 2", 1);

        // Existing null value coalesces
        expect("@existing_null ?? 'fallback'", "fallback");
    }

    #[test]
    #[cfg(feature = "json")]
    fn negative_indexing() {
        // -1 = last element
        expect("@list.-1", 3);
        // -2 = second to last
        expect("@list.-2", 2);
        // -3 = first element
        expect("@list.-3", 1);

        // Negative index on literal list
        expect("[10, 20, 30].-1", 30);
    }

    #[test]
    #[cfg(feature = "json")]
    fn hex_octal_in_expressions() {
        expect("0xFF == 255", true);
        expect("0o77 == 63", true);
        expect("0xFF + 1", 256);
        expect("0o10 * 2", 16);
    }

    #[test]
    fn unicode_escape_in_expressions() {
        let expr = Expr::try_from(r#""\u0041" == "A""#).unwrap();
        let val = expr.eval(&Val::Nil).unwrap();
        assert_eq!(val, Val::Bool(true));
    }

    #[test]
    fn block_comments_in_expressions() {
        let expr = Expr::try_from("1 /* add one */ + 2").unwrap();
        let val = expr.eval(&Val::Nil).unwrap();
        assert_eq!(val, Val::Int(3));
    }

    #[test]
    fn ternary_constant_folding() {
        // true ? X : Y folds to X
        let expr = Expr::try_from("true ? 42 : 0").unwrap();
        assert_eq!(expr, Expr::Val(Val::Int(42)));

        // false ? X : Y folds to Y
        let expr = Expr::try_from("false ? 42 : 0").unwrap();
        assert_eq!(expr, Expr::Val(Val::Int(0)));
    }

    #[test]
    fn coalesce_constant_folding() {
        // nil ?? X folds to X
        let expr = Expr::try_from("nil ?? 42").unwrap();
        assert_eq!(expr, Expr::Val(Val::Int(42)));

        // non-nil ?? X folds to the non-nil value
        let expr = Expr::try_from("1 ?? 42").unwrap();
        assert_eq!(expr, Expr::Val(Val::Int(1)));
    }

    #[test]
    #[cfg(feature = "json")]
    fn ternary_coalesce_combined() {
        // Coalesce feeds into ternary
        expect("(@nonexistent ?? true) ? 'yes' : 'no'", "yes");

        // Ternary inside coalesce
        expect("nil ?? (true ? 42 : 0)", 42);
    }

    #[test]
    #[cfg(feature = "json")]
    fn requested_ctx_with_new_exprs() {
        let expr = Expr::try_from("@a ?? @b").unwrap();
        let names = expr.requested_ctx();
        assert!(names.contains("a"));
        assert!(names.contains("b"));

        let expr = Expr::try_from("@a ? @b : @c").unwrap();
        let names = expr.requested_ctx();
        assert!(names.contains("a"));
        assert!(names.contains("b"));
        assert!(names.contains("c"));
    }

    #[test]
    fn is_ctx_independent_new_exprs() {
        let expr = Expr::try_from("true ? 1 : 2").unwrap();
        assert!(expr.is_ctx_independent());

        let expr = Expr::try_from("nil ?? 42").unwrap();
        assert!(expr.is_ctx_independent());

        let expr = Expr::try_from("@a ?? 42").unwrap();
        assert!(!expr.is_ctx_independent());

        let expr = Expr::try_from("@a ? 1 : 2").unwrap();
        assert!(!expr.is_ctx_independent());
    }

    #[cfg(feature = "json")]
    fn with_ctx(rule: &str) -> Result<Val> {
        let ctx: Val = json!({
            "user": {"name": "lk", "age": 18},
            "req": {"user": {"name": "lk"}},
            "list": [1, 2, 3],
            "list-2": [2],
            "pub": true,
            "index": 1,
            "existing_null": null,
            "nested": {
                "level1": {
                    "level2": "value"
                }
            },
            "with.&=": true,
            "special-chars": "test-value",
            "123": "numeric-field",
            "escaped": {
                "field\"name": "quoted-field",
                "path\\segment": "backslash-field",
                "field'name": "single-quoted-field"
            }
        })
        .into();
        let expr = Expr::try_from(rule)?;
        expr.eval(&ctx)
    }

    #[cfg(feature = "json")]
    fn expect<V: Into<Val>>(rule: &str, val: V) {
        let res = with_ctx(rule);
        assert_eq!(res.unwrap(), val.into());
    }

    #[cfg(feature = "json")]
    fn panic(rule: &str) {
        let res = with_ctx(rule);
        assert!(res.is_err());
        let err = res.unwrap_err();
        println!("{}", err);
    }

    fn collect_exprs_for_shard(target_shard: usize, count: usize) -> Vec<String> {
        let mut exprs = Vec::with_capacity(count);
        let mut i = 0usize;
        let mut attempts = 0usize;
        let max_attempts = count.saturating_mul(10_000).max(10_000);

        while exprs.len() < count {
            attempts += 1;
            assert!(
                attempts <= max_attempts,
                "failed to collect expressions for shard {target_shard} after {attempts} attempts"
            );

            let expr = format!("{i} == {i}");
            if parse_cache_shard_idx_for_tests(expr.as_str()) == target_shard {
                exprs.push(expr);
            }
            i += 1;
        }

        exprs
    }
}