relate-engine-api 1.5.0

Shared contracts for swappable query and write engines
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
use super::*;
use crate::graph_type::GraphTypeSpec;
use crate::redo::wal_format_json::value_json;
use relate_catalog::ConstraintDef;
use relate_core::Value;
use std::collections::BTreeMap;
use std::sync::Arc;

fn sample_ops() -> Vec<RedoOp> {
    vec![
        RedoOp::CatalogIndexDetail {
            name: "vec_idx".into(),
            detail: IndexDetailSpec {
                kind: IndexKindSpec::Vector,
                relationship: false,
                labels: vec!["Doc".into()],
                properties: vec!["embedding".into()],
                with_properties: vec!["ttl".into()],
            },
            options: props_json(&BTreeMap::from([(
                "indexConfig".to_string(),
                Value::Map(
                    BTreeMap::from([("vector.dimensions".to_string(), Value::Int(1536))]).into(),
                ),
            )])),
        },
        RedoOp::PutGraphType {
            name: "gt".into(),
            body: GraphTypeSpec {
                node_types: vec![],
                edge_types: vec![],
                constraints: vec![],
                drop_constraint_names: vec![],
            },
        },
        RedoOp::SetCurrentGraphType {
            body: GraphTypeSpec::default(),
        },
        RedoOp::PullIdentityPut {
            source: "http://example/db/neo4j/query/v2".into(),
            kind: PullKind::Node,
            remote_id: "4:aaa:0".into(),
            local_id: 1,
        },
        RedoOp::OntologySet {
            database: "neo4j".into(),
            spec: "{\"classes\":[]}".into(),
        },
        RedoOp::OntologyDrop {
            database: "neo4j".into(),
        },
    ]
}

/// `is_catalog`, derived from `write_target`, keeps the ADR-0172 §8 split:
/// the ten entity ops, `PullIdentityPut` and the store-level ontology ops
/// (ADR-0120 §3) are not catalog ops, every other op is.
#[test]
fn is_catalog_splits_entity_ops_from_catalog_ops() {
    let entity = [
        RedoOp::create_node(1, vec!["L".into()], BTreeMap::new()),
        RedoOp::create_rel(2, 1, 1, "R".into(), BTreeMap::new()),
        RedoOp::delete_node(1),
        RedoOp::delete_rel(2),
        RedoOp::set_node_prop(1, "k".into(), Value::Int(1)),
        RedoOp::remove_node_prop(1, "k".into()),
        RedoOp::set_rel_prop(2, "k".into(), Value::Int(1)),
        RedoOp::remove_rel_prop(2, "k".into()),
        RedoOp::add_labels(1, vec!["L".into()]),
        RedoOp::remove_labels(1, vec!["L".into()]),
    ];
    // PullIdentityPut, OntologySet, OntologyDrop.
    let non_catalog: Vec<RedoOp> = entity
        .into_iter()
        .chain(sample_ops().into_iter().skip(3))
        .collect();
    let mut catalog = vec![
        RedoOp::CreateIndex {
            name: "i".into(),
            label: "L".into(),
            property: "k".into(),
        },
        RedoOp::DropIndex { name: "i".into() },
        RedoOp::create_constraint(ConstraintDef {
            name: "c".into(),
            relationship: false,
            label: "L".into(),
            properties: vec!["k".into()],
            kind: "unique".into(),
            property_type: None,
            enforced_label: None,
        }),
        RedoOp::DropConstraint { name: "c".into() },
        RedoOp::CatalogDerivedConstraint {
            constraint: relate_model::Constraint {
                name: "c".into(),
                label: "L".into(),
                properties: vec!["k".into()],
                constraint_type: relate_model::ConstraintType::Unique,
                property_type: None,
                entity_type: "NODE".into(),
                enforced_label: None,
            },
        },
        RedoOp::PutDatabase {
            name: "neo4j".into(),
            language: 5,
            composite: false,
        },
        RedoOp::DropDatabase {
            name: "analytics".into(),
        },
    ];
    // CatalogIndexDetail, PutGraphType, SetCurrentGraphType.
    catalog.extend(sample_ops().into_iter().take(3));
    for op in &non_catalog {
        assert!(!op.is_catalog(), "{op:?}");
    }
    for op in &catalog {
        assert!(op.is_catalog(), "{op:?}");
    }
    assert_eq!(
        non_catalog.len() + catalog.len(),
        23,
        "every RedoOp variant"
    );
}

#[test]
fn catalog_ddl_ops_serde_roundtrip() {
    let ops = sample_ops();
    let json = serde_json::to_vec(&ops).expect("ops serialize");
    let decoded: Vec<RedoOp> = serde_json::from_slice(&json).expect("ops deserialize");
    assert_eq!(decoded, ops);
}

/// `catalog_names` derives every name an op mentions: the data writes, the
/// DDL ops that name a label/type/key without writing data, and the monotone
/// rule — a remove still mentions the name it removes. Store metadata (an
/// index, constraint or database name) is not a graph catalog name.
#[test]
fn catalog_names_cover_data_and_ddl_names_monotonically() {
    fn mentions(op: &RedoOp, wanted: CatalogName<'_>) -> bool {
        op.catalog_names().contains(&wanted)
    }

    let node = RedoOp::create_node(
        1,
        vec!["L".into()],
        BTreeMap::from([("k".to_string(), Value::Int(1))]),
    );
    assert!(mentions(&node, CatalogName::Label("L")));
    assert!(mentions(&node, CatalogName::PropertyKey("k")));

    let rel = RedoOp::create_rel(2, 1, 1, "T".into(), BTreeMap::new());
    assert!(mentions(&rel, CatalogName::RelType("T")));

    // Monotone: the remove ops mention the very name they take away.
    assert!(mentions(
        &RedoOp::remove_labels(1, vec!["L".into()]),
        CatalogName::Label("L")
    ));
    assert!(mentions(
        &RedoOp::remove_node_prop(1, "k".into()),
        CatalogName::PropertyKey("k")
    ));

    // DDL names: the index's label and key (bare or legacy-encoded), the
    // constraint's subject and properties, and the typed detail's
    // properties plus `WITH` keys.
    let index = RedoOp::CreateIndex {
        name: "i".into(),
        label: "L".into(),
        property: legacy_property_encoding("k", IndexKindSpec::Range, "i"),
    };
    assert!(mentions(&index, CatalogName::Label("L")));
    assert!(mentions(&index, CatalogName::PropertyKey("k")));

    let constraint = RedoOp::create_constraint(ConstraintDef {
        name: "c".into(),
        relationship: true,
        label: "T".into(),
        properties: vec!["k".into()],
        kind: "unique".into(),
        property_type: None,
        enforced_label: None,
    });
    assert!(mentions(&constraint, CatalogName::RelType("T")));
    assert!(mentions(&constraint, CatalogName::PropertyKey("k")));

    let detail = RedoOp::CatalogIndexDetail {
        name: "vi".into(),
        detail: IndexDetailSpec {
            kind: IndexKindSpec::Vector,
            relationship: false,
            labels: vec!["L".into()],
            properties: vec!["embedding".into()],
            with_properties: vec!["ttl".into()],
        },
        options: props_json(&BTreeMap::new()),
    };
    assert!(mentions(&detail, CatalogName::Label("L")));
    assert!(mentions(&detail, CatalogName::PropertyKey("embedding")));
    assert!(mentions(&detail, CatalogName::PropertyKey("ttl")));

    let derived = RedoOp::CatalogDerivedConstraint {
        constraint: relate_model::Constraint {
            name: "c".into(),
            label: "L".into(),
            properties: vec!["k".into()],
            constraint_type: relate_model::ConstraintType::Exists,
            property_type: None,
            entity_type: "NODE".into(),
            enforced_label: Some("Owner".into()),
        },
    };
    assert!(mentions(&derived, CatalogName::Label("L")));
    assert!(mentions(&derived, CatalogName::Label("Owner")));
    assert!(mentions(&derived, CatalogName::PropertyKey("k")));

    // A drop and the store-metadata ops mention no catalog name.
    for op in [
        RedoOp::DropIndex { name: "i".into() },
        RedoOp::DropConstraint { name: "c".into() },
        RedoOp::delete_node(1),
        RedoOp::PutDatabase {
            name: "neo4j".into(),
            language: 5,
            composite: false,
        },
        RedoOp::OntologyDrop {
            database: "neo4j".into(),
        },
    ] {
        assert!(op.catalog_names().is_empty(), "{op:?}");
    }
}

/// ADR-0120 §3: the document is limited to 16 MiB, and the limit is
/// checked by the one pre-persist validation entry point
/// ([`RedoOp::validate_ids`]) before the commit is written. The boundary
/// is exact.
#[test]
fn ontology_document_size_gate_is_the_16_mib_envelope() {
    let set = |len: usize| RedoOp::OntologySet {
        database: "neo4j".into(),
        spec: "x".repeat(len),
    };
    assert!(set(MAX_ONTOLOGY_SPEC_BYTES).validate_ids().is_ok());
    let err = set(MAX_ONTOLOGY_SPEC_BYTES + 1)
        .validate_ids()
        .expect_err("one byte over the envelope fails");
    assert!(matches!(err, relate_core::Error::Storage(_)), "{err}");
    assert!(err.to_string().contains("16 MiB"), "{err}");
    // OntologyDrop carries no document; non-ontology ops pass too.
    assert!(RedoOp::OntologyDrop {
        database: "neo4j".into()
    }
    .validate_ids()
    .is_ok());
    assert!(RedoOp::delete_node(1).validate_ids().is_ok());
}

/// The typed ontology classifier: `Set` borrows the verbatim document,
/// `Drop` carries nothing, every other op classifies as `None` — and
/// `is_ontology_op` derives from it.
#[test]
fn ontology_op_classifies_the_two_store_ops() {
    let set = RedoOp::OntologySet {
        database: "neo4j".into(),
        spec: "doc".into(),
    };
    assert_eq!(set.ontology_op(), Some(OntologyOp::Set("doc")));
    assert!(set.is_ontology_op());
    let drop = RedoOp::OntologyDrop {
        database: "neo4j".into(),
    };
    assert_eq!(drop.ontology_op(), Some(OntologyOp::Drop));
    assert!(drop.is_ontology_op());
    let node = RedoOp::delete_node(1);
    assert_eq!(node.ontology_op(), None);
    assert!(!node.is_ontology_op());
}

/// A WAL/snapshot written while `DROP DATABASE` was still executable
/// may carry `{"op":"DropDatabase",...}`; the variant is kept
/// decode-only so those stores keep opening (ADR-0114).
#[test]
fn legacy_drop_database_op_still_decodes() {
    let raw = br#"[{"op":"DropDatabase","name":"analytics"}]"#;
    let decoded: Vec<RedoOp> = serde_json::from_slice(raw).expect("legacy op decodes");
    assert_eq!(
        decoded,
        vec![RedoOp::DropDatabase {
            name: "analytics".into()
        }]
    );
}

#[test]
fn legacy_string_payload_no_longer_decodes() {
    // The pre-typed shape carried a `specification` Cypher string that
    // replay re-parsed. A record in that shape is now rejected at
    // decode time instead of being silently re-parsed-or-dropped.
    let raw = br#"[{"op":"PutGraphType","name":"gt","specification":"(:`A`)"}]"#;
    assert!(serde_json::from_slice::<Vec<RedoOp>>(raw).is_err());
    let raw = br#"[{"op":"SetCurrentGraphType","specification":"(:`A`)"}]"#;
    assert!(serde_json::from_slice::<Vec<RedoOp>>(raw).is_err());
}

/// Mirror of the pre-`bd3836d` on-disk shape. Before that commit the
/// property-carrying `RedoOp` fields were plain tagged-JSON
/// `serde_json::Value`s (produced by [`props_json`] / [`value_json`]);
/// these mirrors reproduce that shape -- tag first, then fields in
/// declaration order -- so the golden tests can compare the exact WAL
/// bytes the old representation emitted.
mod legacy_shape {
    use relate_core::{NodeId, RelId};
    use serde::Serialize;

    #[derive(Serialize)]
    pub(super) struct CreateNode {
        pub op: &'static str,
        pub id: NodeId,
        pub labels: Vec<String>,
        pub properties: serde_json::Value,
    }

    #[derive(Serialize)]
    pub(super) struct CreateRel {
        pub op: &'static str,
        pub id: RelId,
        pub start: NodeId,
        pub end: NodeId,
        pub rel_type: String,
        pub properties: serde_json::Value,
    }

    #[derive(Serialize)]
    pub(super) struct SetNodeProp {
        pub op: &'static str,
        pub id: NodeId,
        pub key: String,
        pub value: serde_json::Value,
    }

    #[derive(Serialize)]
    pub(super) struct SetRelProp {
        pub op: &'static str,
        pub id: RelId,
        pub key: String,
        pub value: serde_json::Value,
    }
}

/// Representative tagged values: every scalar encoding plus the tagged
/// ones (NaN/Infinity/-Infinity, temporal, point) and nesting.
fn golden_values() -> Vec<(&'static str, Value)> {
    vec![
        ("string", Value::String("hi".into())),
        ("int", Value::Int(42)),
        ("float", Value::Float(1.5)),
        ("nan", Value::Float(f64::NAN)),
        ("infinity", Value::Float(f64::INFINITY)),
        ("neg_infinity", Value::Float(f64::NEG_INFINITY)),
        ("neg_zero", Value::Float(-0.0)),
        (
            "date",
            Value::Temporal(relate_core::temporal::parse_iso("date", "2020-01-02").unwrap()),
        ),
        (
            "datetime",
            Value::Temporal(
                relate_core::temporal::parse_iso("datetime", "2020-01-02T03:04:05Z").unwrap(),
            ),
        ),
        (
            "duration",
            Value::Temporal(
                relate_core::temporal::parse_iso("duration", "P1Y2M3DT4H5M6S").unwrap(),
            ),
        ),
        (
            "point",
            Value::Point(relate_core::PointValue::cartesian_3d(1.0, 2.0, 3.0)),
        ),
        ("bool", Value::Bool(true)),
        ("null", Value::Null),
        // Homogeneous coordinates (ADR-0706): a family mix inside one
        // vector is unconstructible — the decode side rejects it
        // fail-closed.
        (
            "vector",
            Value::vector(
                relate_core::VectorCoordKind::Float64,
                relate_model::VectorCoords::from_f64(vec![1.0, 2.5]),
            ),
        ),
        (
            "nested_list",
            Value::List(
                vec![
                    Value::Int(1),
                    Value::List(vec![Value::String("x".into()), Value::Null].into()),
                ]
                .into(),
            ),
        ),
        (
            "nested_map",
            Value::Map(
                BTreeMap::from([(
                    "a".to_string(),
                    Value::Map(BTreeMap::from([("b".to_string(), Value::Int(1))]).into()),
                )])
                .into(),
            ),
        ),
    ]
}

fn golden_props() -> BTreeMap<String, Value> {
    golden_values()
        .into_iter()
        .map(|(k, v)| (k.to_string(), v))
        .collect()
}

/// The typed op must emit exactly the bytes the pre-typed representation
/// (a `serde_json::Value` from `value_json`) emitted.
#[test]
fn set_prop_bytes_match_legacy_value_json() {
    for (name, value) in golden_values() {
        let op = RedoOp::SetNodeProp {
            id: 7,
            key: "k".into(),
            value: value.clone(),
        };
        let legacy = legacy_shape::SetNodeProp {
            op: "SetNodeProp",
            id: 7,
            key: "k".into(),
            value: value_json(&value),
        };
        assert_eq!(
            serde_json::to_vec(&op).unwrap(),
            serde_json::to_vec(&legacy).unwrap(),
            "SetNodeProp byte mismatch for {name}"
        );

        let op = RedoOp::SetRelProp {
            id: 8,
            key: "k".into(),
            value: value.clone(),
        };
        let legacy = legacy_shape::SetRelProp {
            op: "SetRelProp",
            id: 8,
            key: "k".into(),
            value: value_json(&value),
        };
        assert_eq!(
            serde_json::to_vec(&op).unwrap(),
            serde_json::to_vec(&legacy).unwrap(),
            "SetRelProp byte mismatch for {name}"
        );
    }
}

/// The typed property map must emit exactly the bytes `props_json`
/// produced for the same logical map.
#[test]
fn create_ops_bytes_match_legacy_props_json() {
    let props = golden_props();

    let op = RedoOp::CreateNode {
        id: 1,
        labels: Arc::new(vec!["N".into()]),
        properties: relate_core::PropertyMap::share(props.clone()),
    };
    let legacy = legacy_shape::CreateNode {
        op: "CreateNode",
        id: 1,
        labels: vec!["N".into()],
        properties: props_json(&props),
    };
    assert_eq!(
        serde_json::to_vec(&op).unwrap(),
        serde_json::to_vec(&legacy).unwrap(),
        "CreateNode byte mismatch"
    );

    let op = RedoOp::CreateRel {
        id: 2,
        start: 1,
        end: 3,
        rel_type: "R".into(),
        properties: relate_core::PropertyMap::share(props.clone()),
    };
    let legacy = legacy_shape::CreateRel {
        op: "CreateRel",
        id: 2,
        start: 1,
        end: 3,
        rel_type: "R".into(),
        properties: props_json(&props),
    };
    assert_eq!(
        serde_json::to_vec(&op).unwrap(),
        serde_json::to_vec(&legacy).unwrap(),
        "CreateRel byte mismatch"
    );
}

/// Pin the exact tagged bytes so any codec drift (a renamed tag, a
/// different NaN rendering, a changed temporal/point shape) fails loudly.
#[test]
fn tagged_encodings_are_byte_pinned() {
    fn pinned(value: Value) -> String {
        serde_json::to_string(&RedoOp::SetNodeProp {
            id: 7,
            key: "k".into(),
            value,
        })
        .unwrap()
    }

    assert_eq!(
        pinned(Value::Float(f64::NAN)),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"float","v":"NaN"}}"#
    );
    assert_eq!(
        pinned(Value::Float(f64::INFINITY)),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"float","v":"Infinity"}}"#
    );
    assert_eq!(
        pinned(Value::Float(f64::NEG_INFINITY)),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"float","v":"-Infinity"}}"#
    );
    assert_eq!(
        pinned(Value::Float(-0.0)),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"float","v":-0.0}}"#
    );
    assert_eq!(
        pinned(Value::Temporal(
            relate_core::temporal::parse_iso("date", "2020-01-02").unwrap()
        )),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"date","v":"2020-01-02"}}"#
    );
    assert_eq!(
        pinned(Value::Temporal(
            relate_core::temporal::parse_iso("datetime", "2020-01-02T03:04:05Z").unwrap()
        )),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"datetime","v":"2020-01-02T03:04:05Z"}}"#
    );
    assert_eq!(
        pinned(Value::Temporal(
            relate_core::temporal::parse_iso("duration", "P1Y2M3DT4H5M6S").unwrap()
        )),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"duration","v":"P1Y2M3DT4H5M6S"}}"#
    );
    assert_eq!(
        pinned(Value::Point(relate_core::PointValue::cartesian_3d(
            1.0, 2.0, 3.0
        ))),
        r#"{"op":"SetNodeProp","id":7,"key":"k","value":{"t":"point","v":{"srid":9157,"x":1.0,"y":2.0,"z":3.0}}}"#
    );
}

/// Round-trip guard: replaying the pinned bytes yields the original op.
#[test]
fn tagged_encodings_roundtrip_through_codec() {
    for (name, value) in golden_values() {
        let op = RedoOp::SetNodeProp {
            id: 7,
            key: "k".into(),
            value: value.clone(),
        };
        let bytes = serde_json::to_vec(&op).unwrap();
        let decoded: RedoOp = serde_json::from_slice(&bytes).unwrap();
        let RedoOp::SetNodeProp { value: got, .. } = decoded else {
            panic!("wrong variant for {name}");
        };
        if let Value::Float(f) = &value {
            assert_eq!(
                f.is_nan(),
                matches!(got, Value::Float(g) if g.is_nan()),
                "{name}"
            );
            if !f.is_nan() {
                assert_eq!(got, value, "{name}");
            }
        } else {
            assert_eq!(got, value, "{name}");
        }
    }
}

#[test]
fn props_from_json_rejects_non_object_payloads() {
    assert!(props_from_json(&serde_json::json!("corrupted")).is_err());
    assert!(props_from_json(&serde_json::json!([1, 2])).is_err());
    assert!(props_from_json(&serde_json::json!({"k": {"x": 1}})).is_err());
    assert!(props_from_json(&serde_json::json!({})).unwrap().is_empty());
}

#[test]
fn index_kind_display_name_round_trips() {
    for kind in IndexKindSpec::ALL {
        assert_eq!(
            IndexKindSpec::from_display_name(kind.display_name()),
            Some(kind)
        );
    }
    assert_eq!(IndexKindSpec::from_display_name("range"), None);
    assert_eq!(IndexKindSpec::from_display_name("NOT A KIND"), None);
}

#[test]
fn legacy_property_encoding_round_trips() {
    for kind in IndexKindSpec::ALL {
        let encoded = legacy_property_encoding("name", kind, "idx_name");
        assert_eq!(encoded, format!("name#{}#idx_name", kind.display_name()));
        assert_eq!(
            split_legacy_property_encoding(&encoded),
            ("name", Some(kind))
        );
    }
}

#[test]
fn split_legacy_property_encoding_decodes_bare_and_unknown_kind() {
    // A bare property name (no `#`) is its own head with no kind.
    assert_eq!(split_legacy_property_encoding("name"), ("name", None));
    // An unrecognized KIND segment decodes to no kind, head intact.
    assert_eq!(
        split_legacy_property_encoding("name#NOTAKIND#idx"),
        ("name", None)
    );
    // Comma-joined multi-property heads survive the split.
    assert_eq!(
        split_legacy_property_encoding("a,b#RANGE#idx"),
        ("a,b", Some(IndexKindSpec::Range))
    );
}

#[test]
fn legacy_property_names_decodes_head_bare_and_comma_joined() {
    // A bare property name (no `#`) is its own one-element list.
    assert_eq!(legacy_property_names("name"), vec!["name".to_string()]);
    // A comma-join head (with or without a KIND segment) splits back
    // into its names.
    assert_eq!(
        legacy_property_names("a,b,c#RANGE#idx"),
        vec!["a".to_string(), "b".to_string(), "c".to_string()]
    );
    assert_eq!(
        legacy_property_names("a,b"),
        vec!["a".to_string(), "b".to_string()]
    );
}

fn detail(
    relationship: bool,
    labels: &[&str],
    properties: &[&str],
    kind: IndexKindSpec,
) -> IndexDetailSpec {
    IndexDetailSpec {
        kind,
        relationship,
        labels: labels.iter().map(|label| label.to_string()).collect(),
        properties: properties.iter().map(|p| p.to_string()).collect(),
        with_properties: Vec::new(),
    }
}

/// Every component of a composite range index serves an equality seek: the
/// executor materializes and seeks each covered pair, so a trailing property
/// is covered too.
#[test]
fn range_seek_pairs_cover_every_composite_component() {
    let composite = detail(false, &["Person"], &["first", "last"], IndexKindSpec::Range);
    assert_eq!(
        composite.range_seek_pairs(),
        [("Person", "first"), ("Person", "last")]
    );
    assert_eq!(
        composite.covered_pairs().len(),
        2,
        "every component still materializes a posting"
    );
}

/// A relationship index never contributes a node `(label, property)` pair,
/// even though its type name could collide with a node label.
#[test]
fn range_seek_pairs_exclude_relationship_indexes() {
    let relationship = detail(true, &["Person"], &["x"], IndexKindSpec::Range);
    assert!(relationship.range_seek_pairs().is_empty());
}

/// A property name is one name: a comma inside it is never split, and a
/// non-range kind serves no equality seek.
#[test]
fn range_seek_pairs_keep_a_comma_bearing_property_name_whole() {
    let comma = detail(false, &["Order"], &["ship,date"], IndexKindSpec::Range);
    assert_eq!(comma.range_seek_pairs(), [("Order", "ship,date")]);
    let text = detail(false, &["Order"], &["ship,date"], IndexKindSpec::Text);
    assert!(text.range_seek_pairs().is_empty());
}