icydb-core 0.94.3

IcyDB — A schema-first typed query engine and persistence runtime for Internet Computer canisters
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
//! Module: value::tests
//! Covers value serialization, coercion, comparison, and typed wrapper
//! roundtrip behavior.

use crate::{
    types::{
        Account, Date, Decimal, Duration, Float32 as F32, Float64 as F64, Int, Int128, Nat, Nat128,
        Principal, Subaccount, Timestamp, Ulid,
    },
    value::{
        CoercionFamily, CoercionFamilyExt, SchemaInvariantError, TextMode, Value, ValueEnum,
        hash_value,
    },
};
use std::cmp::Ordering;

// ---- helpers -----------------------------------------------------------

fn v_f64(x: f64) -> Value {
    Value::Float64(F64::try_new(x).expect("finite f64"))
}
fn v_f32(x: f32) -> Value {
    Value::Float32(F32::try_new(x).expect("finite f32"))
}
fn v_i(x: i64) -> Value {
    Value::Int(x)
}
fn v_u(x: u64) -> Value {
    Value::Uint(x)
}
fn v_d_i(x: i64) -> Value {
    Value::Decimal(Decimal::from_i64(x).unwrap())
}
fn v_txt(s: &str) -> Value {
    Value::Text(s.to_string())
}

macro_rules! sample_value_for_scalar {
    (Account) => {
        Value::Account(Account::dummy(7))
    };
    (Blob) => {
        Value::Blob(vec![1u8, 2u8, 3u8])
    };
    (Bool) => {
        Value::Bool(true)
    };
    (Date) => {
        Value::Date(Date::new(2024, 1, 2))
    };
    (Decimal) => {
        Value::Decimal(Decimal::new(123, 2))
    };
    (Duration) => {
        Value::Duration(Duration::from_secs(1))
    };
    (Enum) => {
        Value::Enum(ValueEnum::loose("example"))
    };
    (Float32) => {
        Value::Float32(F32::try_new(1.25).expect("Float32 sample should be finite"))
    };
    (Float64) => {
        Value::Float64(F64::try_new(2.5).expect("Float64 sample should be finite"))
    };
    (Int) => {
        Value::Int(-7)
    };
    (Int128) => {
        Value::Int128(Int128::from(123i128))
    };
    (IntBig) => {
        Value::IntBig(Int::from(99i32))
    };
    (Principal) => {
        Value::Principal(Principal::from_slice(&[1u8, 2u8, 3u8]))
    };
    (Subaccount) => {
        Value::Subaccount(Subaccount::new([1u8; 32]))
    };
    (Text) => {
        Value::Text("example".to_string())
    };
    (Timestamp) => {
        Value::Timestamp(Timestamp::from_secs(1))
    };
    (Uint) => {
        Value::Uint(7)
    };
    (Uint128) => {
        Value::Uint128(Nat128::from(9u128))
    };
    (UintBig) => {
        Value::UintBig(Nat::from(11u64))
    };
    (Ulid) => {
        Value::Ulid(Ulid::from_u128(42))
    };
    (Unit) => {
        Value::Unit
    };
}

/// Build scalar-backed values paired with their registry numeric flag.
fn registry_numeric_cases() -> Vec<(Value, bool)> {
    macro_rules! collect_cases {
        ( @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $is_numeric) ),* ]
        };
        ( @args $($ignore:tt)*; @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $is_numeric) ),* ]
        };
    }

    let cases = scalar_registry!(collect_cases);

    cases
}

/// Build scalar-backed values paired with their registry numeric-coercion flag.
fn registry_numeric_coercion_cases() -> Vec<(Value, bool)> {
    macro_rules! collect_cases {
        ( @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $supports_numeric_coercion) ),* ]
        };
        ( @args $($ignore:tt)*; @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $supports_numeric_coercion) ),* ]
        };
    }

    scalar_registry!(collect_cases)
}

/// Build scalar-backed values paired with their registry keyable flag.
fn registry_keyable_cases() -> Vec<(Value, bool)> {
    macro_rules! collect_cases {
        ( @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $is_keyable) ),* ]
        };
        ( @args $($ignore:tt)*; @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $is_keyable) ),* ]
        };
    }

    let cases = scalar_registry!(collect_cases);

    cases
}

/// Build scalar-backed values paired with their registry coercion family.
fn registry_coercion_family_cases() -> Vec<(Value, CoercionFamily)> {
    macro_rules! collect_cases {
        ( @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $coercion_family) ),* ]
        };
        ( @args $($ignore:tt)*; @entries $( ($scalar:ident, $coercion_family:expr, $value_pat:pat, is_numeric_value = $is_numeric:expr, supports_numeric_coercion = $supports_numeric_coercion:expr, supports_arithmetic = $supports_arithmetic:expr, supports_equality = $supports_equality:expr, supports_ordering = $supports_ordering:expr, is_keyable = $is_keyable:expr, is_storage_key_encodable = $is_storage_key_encodable:expr) ),* $(,)? ) => {
            vec![ $( (sample_value_for_scalar!($scalar), $coercion_family) ),* ]
        };
    }

    let cases = scalar_registry!(collect_cases);

    cases
}

// ---- keys --------------------------------------------------------------

#[test]
fn as_storage_key_some_for_keyable_variants() {
    assert!(Value::Int(7).as_storage_key().is_some());
    assert!(Value::Uint(7).as_storage_key().is_some());
    assert!(Value::Ulid(Ulid::MIN).as_storage_key().is_some());
    assert!(Value::Unit.as_storage_key().is_some());

    // Non-key / non-orderable variants
    assert!(v_txt("x").as_storage_key().is_none());
    assert!(
        Value::Decimal(Decimal::new(1, 0))
            .as_storage_key()
            .is_none()
    );
    assert!(Value::List(vec![]).as_storage_key().is_none());
    assert!(Value::Null.as_storage_key().is_none());
}

#[test]
fn storage_key_round_trips_through_value() {
    let values = [
        Value::Int(-9),
        Value::Uint(9),
        Value::Ulid(Ulid::MAX),
        Value::Unit,
    ];

    for v in values {
        let key = v
            .as_storage_key()
            .expect("value should be convertible to storage key");

        let back = key.as_value();

        assert_eq!(
            v, back,
            "Value <-> StorageKey round trip failed: {v:?} -> {key:?} -> {back:?}"
        );
    }
}

#[test]
fn canonical_tag_and_rank_are_stable() {
    let map = Value::Map(vec![]);
    let list = Value::List(vec![]);
    let cases = vec![
        (Value::Account(Account::dummy(7)), 1u8),
        (Value::Blob(vec![1u8]), 2),
        (Value::Bool(true), 3),
        (Value::Date(Date::new(2024, 1, 2)), 4),
        (Value::Decimal(Decimal::new(123, 2)), 5),
        (Value::Duration(Duration::from_secs(1)), 6),
        (Value::Enum(ValueEnum::loose("example")), 7),
        (
            Value::Float32(F32::try_new(1.25).expect("Float32 sample should be finite")),
            8,
        ),
        (
            Value::Float64(F64::try_new(2.5).expect("Float64 sample should be finite")),
            9,
        ),
        (Value::Int(-7), 10),
        (Value::Int128(Int128::from(123i128)), 11),
        (Value::IntBig(Int::from(99i32)), 12),
        (list, 13),
        (map, 14),
        (Value::Null, 15),
        (
            Value::Principal(Principal::from_slice(&[1u8, 2u8, 3u8])),
            16,
        ),
        (Value::Subaccount(Subaccount::new([1u8; 32])), 17),
        (Value::Text("example".to_string()), 18),
        (Value::Timestamp(Timestamp::from_secs(1)), 19),
        (Value::Uint(7), 20),
        (Value::Uint128(Nat128::from(9u128)), 21),
        (Value::UintBig(Nat::from(11u64)), 22),
        (Value::Ulid(Ulid::from_u128(42)), 23),
        (Value::Unit, 24),
    ];

    for (value, expected_tag) in cases {
        assert_eq!(
            value.canonical_tag().to_u8(),
            expected_tag,
            "value: {value:?}"
        );
        assert_eq!(value.canonical_rank(), expected_tag - 1, "value: {value:?}");
    }
}

// ---- numeric coercion & comparison ------------------------------------

#[test]
fn value_is_numeric_matches_registry_flag() {
    for (value, expected) in registry_numeric_cases() {
        assert_eq!(value.is_numeric(), expected, "value: {value:?}");
    }
}

#[test]
fn value_supports_numeric_coercion_matches_registry_flag() {
    for (value, expected) in registry_numeric_coercion_cases() {
        assert_eq!(
            value.supports_numeric_coercion(),
            expected,
            "value: {value:?}"
        );
    }
}

#[test]
fn value_as_storage_key_matches_registry_flag() {
    for (value, is_keyable) in registry_keyable_cases() {
        assert_eq!(
            value.as_storage_key().is_some(),
            is_keyable,
            "value: {value:?}"
        );
    }
}

#[test]
fn value_coercion_family_matches_registry_flag() {
    for (value, expected_coercion_family) in registry_coercion_family_cases() {
        assert_eq!(
            value.coercion_family(),
            expected_coercion_family,
            "value: {value:?}"
        );
    }
}

#[test]
fn coercion_family_surface_is_stable_for_core_variants() {
    assert_eq!(Value::Int(1).coercion_family(), CoercionFamily::Numeric);
    assert_eq!(
        Value::Decimal(Decimal::new(5, 1)).coercion_family(),
        CoercionFamily::Numeric
    );
    assert_eq!(
        Value::Text("x".to_string()).coercion_family(),
        CoercionFamily::Textual
    );
    assert_eq!(
        Value::Principal(Principal::from_slice(&[1u8, 2u8])).coercion_family(),
        CoercionFamily::Identifier
    );
    assert_eq!(
        Value::Enum(ValueEnum::loose("V")).coercion_family(),
        CoercionFamily::Enum
    );
    assert_eq!(
        Value::Blob(vec![1u8, 2u8]).coercion_family(),
        CoercionFamily::Blob
    );
    assert_eq!(Value::Bool(true).coercion_family(), CoercionFamily::Bool);
    assert_eq!(
        Value::List(vec![Value::Int(1)]).coercion_family(),
        CoercionFamily::Collection
    );
    assert_eq!(
        Value::from_map(vec![(Value::Text("k".to_string()), Value::Int(1))])
            .expect("map")
            .coercion_family(),
        CoercionFamily::Collection
    );
    assert_eq!(Value::Null.coercion_family(), CoercionFamily::Null);
    assert_eq!(Value::Unit.coercion_family(), CoercionFamily::Unit);
}

#[test]
fn value_unit_has_unit_coercion_family() {
    assert_eq!(Value::Unit.coercion_family(), CoercionFamily::Unit);
}

#[test]
fn cmp_numeric_int_nat_eq_and_order() {
    assert_eq!(v_i(10).cmp_numeric(&v_u(10)), Some(Ordering::Equal));
    assert_eq!(v_i(9).cmp_numeric(&v_u(10)), Some(Ordering::Less));
    // negative int vs nat: not comparable via f64 path; decimal path handles it
    assert_eq!(v_i(-1).cmp_numeric(&v_u(0)), Some(Ordering::Less));
}

#[test]
fn cmp_numeric_int_float_eq() {
    assert_eq!(v_i(42).cmp_numeric(&v_f64(42.0)), Some(Ordering::Equal));
    assert_eq!(v_i(42).cmp_numeric(&v_f32(42.0)), Some(Ordering::Equal));
}

#[test]
fn cmp_numeric_decimal_int_and_float() {
    assert_eq!(v_d_i(10).cmp_numeric(&v_i(10)), Some(Ordering::Equal));
    assert_eq!(v_d_i(10).cmp_numeric(&v_f64(10.0)), Some(Ordering::Equal));
    assert_eq!(v_d_i(11).cmp_numeric(&v_f64(10.5)), Some(Ordering::Greater));
}

#[test]
#[expect(clippy::cast_precision_loss)]
fn cmp_numeric_safe_int_boundary() {
    // 2^53 is exactly representable in f64
    let safe: i64 = 9_007_199_254_740_992; // 1 << 53
    let int_safe = v_i(safe);
    let float_safe = v_f64(safe as f64);
    assert_eq!(int_safe.cmp_numeric(&float_safe), Some(Ordering::Equal));

    // one above 2^53 is not exactly representable; decimal path should see it as greater
    let int_unsafe = v_i(safe + 1);
    assert_eq!(int_unsafe.cmp_numeric(&float_safe), Some(Ordering::Greater));
}

#[test]
fn cmp_numeric_neg_zero_equals_zero() {
    let neg_zero = Value::Float64(F64::try_new(-0.0).unwrap());
    assert_eq!(neg_zero.cmp_numeric(&v_i(0)), Some(Ordering::Equal));
    let neg_zero32 = Value::Float32(F32::try_new(-0.0).unwrap());
    assert_eq!(neg_zero32.cmp_numeric(&v_i(0)), Some(Ordering::Equal));
}

#[test]
fn cmp_numeric_respects_registry_numeric_coercion_flag() {
    for (value, supports_numeric_coercion) in registry_numeric_coercion_cases() {
        let cmp = value.cmp_numeric(&value);
        if supports_numeric_coercion {
            assert_eq!(cmp, Some(Ordering::Equal), "value: {value:?}");
        } else {
            assert!(cmp.is_none(), "value: {value:?}");
        }
    }
}

#[test]
fn cmp_numeric_rejects_date_and_bigints() {
    let date = Value::Date(Date::new(2024, 1, 2));
    let int_big = Value::IntBig(Int::from(10i32));
    let uint_big = Value::UintBig(Nat::from(10u64));
    let one = Value::Int(1);

    assert!(!date.supports_numeric_coercion());
    assert!(!int_big.supports_numeric_coercion());
    assert!(!uint_big.supports_numeric_coercion());

    assert!(date.cmp_numeric(&one).is_none());
    assert!(int_big.cmp_numeric(&one).is_none());
    assert!(uint_big.cmp_numeric(&one).is_none());
}

#[test]
fn cmp_numeric_is_unreachable_for_non_numeric_coercible_values() {
    let left = Value::Date(Date::EPOCH);
    let right = Value::Int(0);

    assert!(!left.supports_numeric_coercion());
    assert!(left.cmp_numeric(&right).is_none());
    assert!(left.partial_cmp(&right).is_none());
}

#[test]
fn partial_ord_cross_variant_is_none() {
    // PartialOrd stays within same variant; cross-variant returns None
    assert!(v_i(1).partial_cmp(&v_f64(1.0)).is_none());
    assert!(v_txt("a").partial_cmp(&v_txt("b")).is_some());
}

#[test]
fn from_map_is_canonical_and_order_independent() {
    let map_a = Value::from_map(vec![
        (v_txt("c"), v_u(3)),
        (v_txt("a"), v_u(1)),
        (v_txt("b"), v_u(2)),
    ])
    .expect("map_a should normalize");
    let map_b = Value::from_map(vec![
        (v_txt("a"), v_u(1)),
        (v_txt("b"), v_u(2)),
        (v_txt("c"), v_u(3)),
    ])
    .expect("map_b should normalize");

    assert_eq!(map_a, map_b);

    let hash_a = hash_value(&map_a).expect("hash map_a");
    let hash_b = hash_value(&map_b).expect("hash map_b");
    assert_eq!(hash_a, hash_b);
}

#[test]
fn try_from_map_vec_is_canonical_and_order_independent() {
    let map_a = Value::try_from(vec![
        (v_txt("c"), v_u(3)),
        (v_txt("a"), v_u(1)),
        (v_txt("b"), v_u(2)),
    ])
    .expect("map_a should normalize");
    let map_b = Value::try_from(vec![
        (v_txt("a"), v_u(1)),
        (v_txt("b"), v_u(2)),
        (v_txt("c"), v_u(3)),
    ])
    .expect("map_b should normalize");

    assert_eq!(map_a, map_b);
}

#[test]
fn try_from_map_vec_returns_schema_invariant_error() {
    let err = Value::try_from(vec![(v_txt("a"), v_u(1)), (v_txt("a"), v_u(2))])
        .expect_err("duplicate map keys should fail");

    assert!(matches!(
        err,
        SchemaInvariantError::InvalidMapValue(crate::value::MapValueError::DuplicateKey { .. })
    ));
}

#[test]
fn canonical_cmp_key_is_total_for_enum_payloads() {
    let left = Value::Enum(
        ValueEnum::new("Any", Some("test::Enum")).with_payload(Value::from_slice(&[v_i(1)])),
    );
    let right = Value::Enum(
        ValueEnum::new("Any", Some("test::Enum"))
            .with_payload(Value::from_map(vec![(v_txt("k"), v_i(1))]).expect("map payload")),
    );

    let forward = Value::canonical_cmp_key(&left, &right);
    let reverse = Value::canonical_cmp_key(&right, &left);
    assert_eq!(forward, reverse.reverse());
    assert_ne!(forward, Ordering::Equal);
}

#[test]
fn canonical_cmp_mixed_variant_follows_rank() {
    let low = Value::Account(Account::dummy(1));
    let high = Value::Text("x".to_string());

    assert_eq!(Value::canonical_cmp(&low, &high), Ordering::Less);
    assert_eq!(Value::canonical_cmp(&high, &low), Ordering::Greater);
}

#[test]
fn canonical_cmp_key_matches_canonical_cmp() {
    let left = Value::from_map(vec![(v_txt("a"), v_i(1))]).expect("map");
    let right = Value::from_map(vec![(v_txt("a"), v_i(2))]).expect("map");

    assert_eq!(
        Value::canonical_cmp_key(&left, &right),
        Value::canonical_cmp(&left, &right),
    );
}

#[test]
fn canonical_cmp_map_entry_orders_by_key_then_value() {
    assert_eq!(
        Value::canonical_cmp_map_entry(&v_txt("a"), &v_u(9), &v_txt("b"), &v_u(1)),
        Ordering::Less,
        "map entry comparison must prioritize canonical key ordering",
    );
    assert_eq!(
        Value::canonical_cmp_map_entry(&v_txt("k"), &v_u(1), &v_txt("k"), &v_u(2)),
        Ordering::Less,
        "map entry comparison must use canonical value ordering when keys tie",
    );
    assert_eq!(
        Value::canonical_cmp_map_entry(&v_txt("k"), &v_u(2), &v_txt("k"), &v_u(1)),
        Ordering::Greater,
        "map entry comparison must keep value tie-break deterministic",
    );
}

// ---- list membership ---------------------------------------------------

#[test]
fn list_contains_scalar() {
    let l = Value::from_slice(&[v_i(1), v_txt("a")]);
    assert_eq!(l.contains(&v_i(1)), Some(true));
    assert_eq!(l.contains(&v_i(2)), Some(false));
}

#[test]
fn list_contains_any_all_and_vacuous_truth() {
    let l = Value::from_slice(&[v_txt("x"), v_txt("y")]);
    let needles_any = Value::from_slice(&[v_txt("z"), v_txt("y")]);
    let needles_all = Value::from_slice(&[v_txt("x"), v_txt("y")]);
    let empty = Value::from_slice::<Value>(&[]);
    assert_eq!(l.contains_any(&needles_any), Some(true));
    assert_eq!(l.contains_all(&needles_all), Some(true));
    assert_eq!(l.contains_any(&empty), Some(false), "AnyIn([]) == false");
    assert_eq!(l.contains_all(&empty), Some(true), "AllIn([]) == true");
}

// ---- list any/all ------------------------------------------------------

#[test]
fn contains_any_list_vs_list() {
    let haystack = Value::from_slice(&[v_i(1), v_i(2), v_i(3)]);
    let needles = Value::from_slice(&[v_i(4), v_i(2)]);
    assert_eq!(haystack.contains_any(&needles), Some(true));

    let needles_none = Value::from_slice(&[v_i(4), v_i(5)]);
    assert_eq!(haystack.contains_any(&needles_none), Some(false));

    let empty = Value::from_slice::<Value>(&[]);
    assert_eq!(
        haystack.contains_any(&empty),
        Some(false),
        "AnyIn([]) == false"
    );
}

#[test]
fn contains_all_list_vs_list() {
    let haystack = Value::from_slice(&[v_txt("a"), v_txt("b"), v_txt("c")]);
    let needles = Value::from_slice(&[v_txt("a"), v_txt("c")]);
    assert_eq!(haystack.contains_all(&needles), Some(true));

    let needles_missing = Value::from_slice(&[v_txt("a"), v_txt("z")]);
    assert_eq!(haystack.contains_all(&needles_missing), Some(false));

    let empty = Value::from_slice::<Value>(&[]);
    assert_eq!(
        haystack.contains_all(&empty),
        Some(true),
        "AllIn([]) == true"
    );
}

#[test]
fn contains_any_list_vs_scalar() {
    let haystack = Value::from_slice(&[v_i(10), v_i(20)]);
    assert_eq!(haystack.contains_any(&v_i(20)), Some(true));
    assert_eq!(haystack.contains_any(&v_i(99)), Some(false));
}

#[test]
fn contains_all_list_vs_scalar() {
    let haystack = Value::from_slice(&[v_i(10), v_i(20)]);
    assert_eq!(haystack.contains_all(&v_i(20)), Some(true));
    assert_eq!(haystack.contains_all(&v_i(99)), Some(false));
}

#[test]
fn contains_any_scalar_vs_list() {
    let scalar = v_txt("hello");
    let needles_yes = Value::from_slice(&[v_txt("x"), v_txt("hello")]);
    let needles_no = Value::from_slice(&[v_txt("x"), v_txt("y")]);

    assert_eq!(scalar.contains_any(&needles_yes), Some(true));
    assert_eq!(scalar.contains_any(&needles_no), Some(false));
}

#[test]
fn contains_all_scalar_vs_list() {
    let scalar = v_txt("hello");
    let needles_yes = Value::from_slice(&[v_txt("hello")]);
    let needles_extra = Value::from_slice(&[v_txt("hello"), v_txt("world")]);
    let empty = Value::from_slice::<Value>(&[]);

    assert_eq!(scalar.contains_all(&needles_yes), Some(true));
    assert_eq!(scalar.contains_all(&needles_extra), Some(false));
    assert_eq!(
        scalar.contains_all(&empty),
        Some(false),
        "Scalar all-in empty should be false"
    );
}

#[test]
fn contains_any_scalar_vs_scalar() {
    let scalar = v_u(5);
    assert_eq!(scalar.contains_any(&v_u(5)), Some(true));
    assert_eq!(scalar.contains_any(&v_u(6)), Some(false));
}

#[test]
fn contains_all_scalar_vs_scalar() {
    let scalar = v_u(5);
    assert_eq!(scalar.contains_all(&v_u(5)), Some(true));
    assert_eq!(scalar.contains_all(&v_u(6)), Some(false));
}

#[test]
fn in_list_ci_text_vs_list() {
    let haystack = Value::from_slice(&[v_txt("Alpha"), v_txt("Beta")]);
    assert_eq!(v_txt("alpha").in_list_ci(&haystack), Some(true));
    assert_eq!(v_txt("BETA").in_list_ci(&haystack), Some(true));
    assert_eq!(v_txt("gamma").in_list_ci(&haystack), Some(false));
}

#[test]
fn list_contains_ci_scalar() {
    let list = Value::from_slice(&[v_txt("Foo"), v_txt("Bar")]);
    assert_eq!(list.contains_ci(&v_txt("foo")), Some(true));
    assert_eq!(list.contains_ci(&v_txt("BAR")), Some(true));
    assert_eq!(list.contains_ci(&v_txt("baz")), Some(false));
}

#[test]
fn list_contains_any_ci() {
    let haystack = Value::from_slice(&[v_txt("Apple"), v_txt("Banana")]);
    let needles_yes = Value::from_slice(&[v_txt("banana"), v_txt("Cherry")]);
    let needles_no = Value::from_slice(&[v_txt("pear"), v_txt("cherry")]);

    assert_eq!(haystack.contains_any_ci(&needles_yes), Some(true));
    assert_eq!(haystack.contains_any_ci(&needles_no), Some(false));
}

#[test]
fn list_contains_all_ci() {
    let haystack = Value::from_slice(&[v_txt("Dog"), v_txt("Cat"), v_txt("Bird")]);
    let needles_yes = Value::from_slice(&[v_txt("dog"), v_txt("cat")]);
    let needles_no = Value::from_slice(&[v_txt("dog"), v_txt("lion")]);

    assert_eq!(haystack.contains_all_ci(&needles_yes), Some(true));
    assert_eq!(haystack.contains_all_ci(&needles_no), Some(false));
}

#[test]
fn scalar_vs_list_ci() {
    let scalar = v_txt("Hello");
    let list = Value::from_slice(&[v_txt("HELLO"), v_txt("World")]);

    assert_eq!(scalar.in_list_ci(&list), Some(true));
    assert_eq!(scalar.contains_any_ci(&list), Some(true));

    let list2 = Value::from_slice(&[v_txt("World")]);
    assert_eq!(scalar.contains_any_ci(&list2), Some(false));
}

#[test]
fn ci_membership_with_empty_lists() {
    let empty = Value::from_slice::<Value>(&[]);
    let scalar = v_txt("alpha");

    assert_eq!(scalar.in_list_ci(&empty), Some(false));
    assert_eq!(scalar.contains_any_ci(&empty), Some(false));
    assert_eq!(scalar.contains_all_ci(&empty), Some(false));
}

#[test]
fn ci_equality_parses_identifier_text() {
    let ulid = Ulid::generate();
    let ulid_text = Value::Text(ulid.to_string());

    assert!(Value::Ulid(ulid).contains_ci(&ulid_text).unwrap());
    assert!(
        Value::Ulid(ulid)
            .in_list_ci(&Value::from_slice(&[ulid_text]))
            .unwrap()
    );
}

#[test]
fn ci_membership_handles_ulid_strings() {
    let target = Ulid::generate();
    let actual = Value::from_slice(&[Value::Ulid(target)]);
    let needles = Value::from_slice(&[Value::Text(target.to_string())]);

    assert_eq!(actual.contains_any_ci(&needles), Some(true));
    assert_eq!(actual.contains_all_ci(&needles), Some(true));
}

// ---- text CS/CI --------------------------------------------------------

#[test]
fn text_eq_cs_vs_ci() {
    let a = v_txt("Alpha");
    let b = v_txt("alpha");
    assert_eq!(a.text_eq(&b, TextMode::Cs), Some(false));
    assert_eq!(a.text_eq(&b, TextMode::Ci), Some(true));
}

#[test]
fn text_contains_starts_ends_cs_ci() {
    let a = v_txt("Hello Alpha World");
    assert_eq!(a.text_contains(&v_txt("alpha"), TextMode::Cs), Some(false));
    assert_eq!(a.text_contains(&v_txt("alpha"), TextMode::Ci), Some(true));

    assert_eq!(
        a.text_starts_with(&v_txt("hello"), TextMode::Cs),
        Some(false)
    );
    assert_eq!(
        a.text_starts_with(&v_txt("hello"), TextMode::Ci),
        Some(true)
    );

    assert_eq!(a.text_ends_with(&v_txt("WORLD"), TextMode::Cs), Some(false));
    assert_eq!(a.text_ends_with(&v_txt("WORLD"), TextMode::Ci), Some(true));
}

// ----------- eq and none

#[test]
fn eq_and_ne_none_semantics() {
    let some_val = v_i(42);
    let none_val = Value::Null;

    // eq(None) only true if both sides are None
    assert!(none_val == Value::Null);
    assert!(some_val != Value::Null);

    // ne(None) true if left is not None
    assert!(none_val == Value::Null);
    assert!(some_val != Value::Null);
}