interstellar 0.2.0

A high-performance graph database with Gremlin-style traversals and GQL query language
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
775
776
777
778
779
780
//! Integration tests for full-text search (Phase 2 of spec-55, edges from
//! spec-55b).
//!
//! Covers:
//!   - vertex side: `Graph::create_text_index_v` lifecycle, mutation hooks
//!     (`add_vertex` / `set_vertex_property` / `remove_vertex`), and the
//!     `search_text` / `search_text_query` traversal source steps including
//!     BM25 score propagation via the traverser sack;
//!   - edge side: identical surface area mirrored through `_e` helpers
//!     (`create_text_index_e`, `search_text_e`, `search_text_query_e`,
//!     `add_edge` / `set_edge_property` / `remove_edge` mutation hooks);
//!   - cross-element invariants: globally-unique property-name namespace;
//!     vertex and edge indexes coexist without bleed-through.

use std::collections::HashMap;
use std::sync::Arc;

use interstellar::storage::text::{TextIndexConfig, TextIndexError, TextQuery};
use interstellar::storage::Graph;
use interstellar::value::{EdgeId, Value, VertexId};

// =============================================================================
// Helpers
// =============================================================================

fn graph_with_body_index() -> Arc<Graph> {
    let graph = Arc::new(Graph::new());
    graph
        .create_text_index_v("body", TextIndexConfig::default())
        .unwrap();
    graph
}

fn add_doc(graph: &Graph, label: &str, body: &str) -> VertexId {
    let mut props = HashMap::new();
    props.insert("body".to_string(), Value::String(body.to_string()));
    graph.add_vertex(label, props)
}

// =============================================================================
// Index lifecycle
// =============================================================================

#[test]
fn create_then_drop_text_index_round_trips() {
    let graph = Graph::new();
    assert_eq!(graph.text_index_count_v(), 0);
    assert!(!graph.has_text_index_v("body"));

    graph
        .create_text_index_v("body", TextIndexConfig::default())
        .unwrap();
    assert!(graph.has_text_index_v("body"));
    assert_eq!(graph.text_index_count_v(), 1);
    assert_eq!(graph.list_text_indexes_v(), vec!["body".to_string()]);

    graph.drop_text_index_v("body").unwrap();
    assert!(!graph.has_text_index_v("body"));
    assert_eq!(graph.text_index_count_v(), 0);
}

#[test]
fn create_text_index_rejects_duplicate_property() {
    let graph = Graph::new();
    graph
        .create_text_index_v("body", TextIndexConfig::default())
        .unwrap();
    let err = graph
        .create_text_index_v("body", TextIndexConfig::default())
        .unwrap_err();
    assert!(matches!(err, TextIndexError::Storage(_)));
}

#[test]
fn drop_text_index_returns_error_for_unknown_property() {
    let graph = Graph::new();
    let err = graph.drop_text_index_v("missing").unwrap_err();
    assert!(matches!(err, TextIndexError::Storage(_)));
}

#[test]
fn create_text_index_backfills_existing_string_values() {
    let graph = Arc::new(Graph::new());

    add_doc(&graph, "doc", "the raft consensus protocol");
    add_doc(&graph, "doc", "paxos consensus");
    add_doc(&graph, "doc", "completely unrelated content about cats");

    // Index created AFTER vertices exist must back-fill them.
    graph
        .create_text_index_v("body", TextIndexConfig::default())
        .unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text("body", "consensus", 10)
        .unwrap()
        .to_value_list();
    assert_eq!(hits.len(), 2);
}

// =============================================================================
// Mutation hooks
// =============================================================================

#[test]
fn add_vertex_indexes_string_body_property() {
    let graph = graph_with_body_index();
    let v = add_doc(&graph, "doc", "raft consensus algorithm");

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g.search_text("body", "raft", 10).unwrap().to_value_list();
    assert_eq!(hits, vec![Value::Vertex(v)]);
}

#[test]
fn set_vertex_property_updates_text_index() {
    let graph = graph_with_body_index();
    let v = add_doc(&graph, "doc", "original text about apples");

    let g = graph.gremlin(Arc::clone(&graph));
    assert_eq!(
        g.search_text("body", "apples", 10)
            .unwrap()
            .to_value_list()
            .len(),
        1
    );

    // Replace property; old token "apples" must disappear, "bananas" appears.
    graph
        .set_vertex_property(v, "body", Value::String("now about bananas".into()))
        .unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text("body", "apples", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
    assert_eq!(
        g.search_text("body", "bananas", 10)
            .unwrap()
            .to_value_list()
            .len(),
        1
    );
}

#[test]
fn set_vertex_property_to_non_string_removes_from_index() {
    let graph = graph_with_body_index();
    let v = add_doc(&graph, "doc", "indexable text");

    // Overwrite the body property with a non-string value; the indexed
    // tokens must be removed so the document no longer matches.
    graph
        .set_vertex_property(v, "body", Value::Int(42))
        .unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text("body", "indexable", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
}

#[test]
fn remove_vertex_removes_from_text_index() {
    let graph = graph_with_body_index();
    let v = add_doc(&graph, "doc", "ephemeral content");

    graph.remove_vertex(v).unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text("body", "ephemeral", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
}

// =============================================================================
// Traversal source: search_text / search_text_query
// =============================================================================

#[test]
fn search_text_returns_top_k_in_score_order() {
    let graph = graph_with_body_index();
    add_doc(&graph, "doc", "raft raft raft consensus consensus");
    add_doc(&graph, "doc", "raft consensus");
    add_doc(&graph, "doc", "consensus only");
    add_doc(&graph, "doc", "totally unrelated content");

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text("body", "raft consensus", 10)
        .unwrap()
        .to_value_list();
    assert!(
        hits.len() >= 2,
        "expected at least two matches, got {hits:?}"
    );

    // Top-k must respect k.
    let g = graph.gremlin(Arc::clone(&graph));
    let top1 = g
        .search_text("body", "raft consensus", 1)
        .unwrap()
        .to_value_list();
    assert_eq!(top1.len(), 1);
}

#[test]
fn search_text_query_supports_phrase_query() {
    let graph = graph_with_body_index();
    let phrase_match = add_doc(&graph, "doc", "the quick brown fox jumps");
    let _scattered = add_doc(&graph, "doc", "brown sugar and quick recipes for fox tacos");

    let g = graph.gremlin(Arc::clone(&graph));
    let q = TextQuery::Phrase {
        text: "quick brown fox".to_string(),
        slop: 0,
    };
    let hits = g.search_text_query("body", &q, 10).unwrap().to_value_list();
    assert_eq!(hits, vec![Value::Vertex(phrase_match)]);
}

#[test]
fn search_text_returns_error_for_unknown_property() {
    let graph = Arc::new(Graph::new());
    let g = graph.gremlin(Arc::clone(&graph));
    match g.search_text("missing", "anything", 10) {
        Err(TextIndexError::Storage(_)) => {}
        Err(other) => panic!("unexpected error variant: {other:?}"),
        Ok(_) => panic!("expected error for missing index"),
    }
}

#[test]
fn search_text_with_zero_k_returns_no_hits() {
    let graph = graph_with_body_index();
    add_doc(&graph, "doc", "anything");
    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text("body", "anything", 0)
        .unwrap()
        .to_value_list();
    assert!(hits.is_empty());
}

#[test]
fn search_text_chains_with_filter_steps() {
    let graph = graph_with_body_index();
    let _doc = add_doc(&graph, "doc", "raft consensus");
    let _note = add_doc(&graph, "note", "raft consensus");

    let g = graph.gremlin(Arc::clone(&graph));
    let docs_only = g
        .search_text("body", "raft", 10)
        .unwrap()
        .has_label("doc")
        .to_value_list();
    assert_eq!(docs_only.len(), 1);
}

// =============================================================================
// Score propagation via traverser sack
//
// The `VerticesWithTextScore` source variant attaches each hit's BM25 score
// to the traverser's sack at construction time (verified directly in the
// source-step match arm). At the user-visible API level we observe this
// indirectly via score ordering: Tantivy returns hits sorted by descending
// relevance, so the order of `to_value_list()` output reflects the scores
// that were stamped into each traverser's sack.
// =============================================================================

#[test]
fn search_text_preserves_descending_score_order() {
    let graph = graph_with_body_index();
    let strong = add_doc(
        &graph,
        "doc",
        "raft raft raft consensus consensus consensus",
    );
    let weak = add_doc(&graph, "doc", "raft");

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text("body", "raft consensus", 10)
        .unwrap()
        .to_value_list();

    // Stronger BM25 match must come first.
    assert_eq!(hits.first(), Some(&Value::Vertex(strong)));
    assert!(hits.contains(&Value::Vertex(weak)));
}

// =============================================================================
// Edge-side helpers
// =============================================================================

/// Build a graph with two anchor vertices (so we have somewhere to attach
/// edges) and an edge text index registered on the `body` property.
fn graph_with_edge_body_index() -> (Arc<Graph>, VertexId, VertexId) {
    let graph = Arc::new(Graph::new());
    graph
        .create_text_index_e("body", TextIndexConfig::default())
        .unwrap();
    let a = graph.add_vertex("anchor", HashMap::new());
    let b = graph.add_vertex("anchor", HashMap::new());
    (graph, a, b)
}

fn add_edge_with_body(
    graph: &Graph,
    src: VertexId,
    dst: VertexId,
    label: &str,
    body: &str,
) -> EdgeId {
    let mut props = HashMap::new();
    props.insert("body".to_string(), Value::String(body.to_string()));
    graph.add_edge(src, dst, label, props).unwrap()
}

// =============================================================================
// Edge-side: index lifecycle
// =============================================================================

#[test]
fn create_then_drop_edge_text_index_round_trips() {
    let graph = Graph::new();
    assert_eq!(graph.text_index_count_e(), 0);
    assert!(!graph.has_text_index_e("body"));

    graph
        .create_text_index_e("body", TextIndexConfig::default())
        .unwrap();
    assert!(graph.has_text_index_e("body"));
    assert_eq!(graph.text_index_count_e(), 1);
    assert_eq!(graph.list_text_indexes_e(), vec!["body".to_string()]);

    graph.drop_text_index_e("body").unwrap();
    assert!(!graph.has_text_index_e("body"));
    assert_eq!(graph.text_index_count_e(), 0);
}

#[test]
fn create_edge_text_index_rejects_duplicate_property() {
    let graph = Graph::new();
    graph
        .create_text_index_e("body", TextIndexConfig::default())
        .unwrap();
    let err = graph
        .create_text_index_e("body", TextIndexConfig::default())
        .unwrap_err();
    assert!(matches!(err, TextIndexError::Storage(_)));
}

#[test]
fn drop_edge_text_index_returns_error_for_unknown_property() {
    let graph = Graph::new();
    let err = graph.drop_text_index_e("missing").unwrap_err();
    assert!(matches!(err, TextIndexError::Storage(_)));
}

#[test]
fn create_edge_text_index_backfills_existing_string_values() {
    let graph = Arc::new(Graph::new());
    let a = graph.add_vertex("anchor", HashMap::new());
    let b = graph.add_vertex("anchor", HashMap::new());

    add_edge_with_body(&graph, a, b, "comment", "the raft consensus protocol");
    add_edge_with_body(&graph, a, b, "comment", "paxos consensus");
    add_edge_with_body(&graph, a, b, "comment", "completely unrelated content");

    // Index created AFTER edges exist must back-fill them.
    graph
        .create_text_index_e("body", TextIndexConfig::default())
        .unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text_e("body", "consensus", 10)
        .unwrap()
        .to_value_list();
    assert_eq!(hits.len(), 2);
}

// =============================================================================
// Edge-side: mutation hooks
// =============================================================================

#[test]
fn add_edge_indexes_string_body_property() {
    let (graph, a, b) = graph_with_edge_body_index();
    let e = add_edge_with_body(&graph, a, b, "comment", "raft consensus algorithm");

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g.search_text_e("body", "raft", 10).unwrap().to_value_list();
    assert_eq!(hits, vec![Value::Edge(e)]);
}

#[test]
fn set_edge_property_updates_text_index() {
    let (graph, a, b) = graph_with_edge_body_index();
    let e = add_edge_with_body(&graph, a, b, "comment", "original text about apples");

    let g = graph.gremlin(Arc::clone(&graph));
    assert_eq!(
        g.search_text_e("body", "apples", 10)
            .unwrap()
            .to_value_list()
            .len(),
        1
    );

    // Replace property; old token "apples" must disappear, "bananas" appears.
    graph
        .set_edge_property(e, "body", Value::String("now about bananas".into()))
        .unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text_e("body", "apples", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
    assert_eq!(
        g.search_text_e("body", "bananas", 10)
            .unwrap()
            .to_value_list()
            .len(),
        1
    );
}

#[test]
fn set_edge_property_to_non_string_removes_from_index() {
    let (graph, a, b) = graph_with_edge_body_index();
    let e = add_edge_with_body(&graph, a, b, "comment", "indexable text");

    graph.set_edge_property(e, "body", Value::Int(42)).unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text_e("body", "indexable", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
}

#[test]
fn remove_edge_removes_from_text_index() {
    let (graph, a, b) = graph_with_edge_body_index();
    let e = add_edge_with_body(&graph, a, b, "comment", "ephemeral content");

    graph.remove_edge(e).unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text_e("body", "ephemeral", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
}

#[test]
fn removing_vertex_cascades_edge_text_index_cleanup() {
    let (graph, a, b) = graph_with_edge_body_index();
    add_edge_with_body(&graph, a, b, "comment", "doomed payload");

    // Removing the source vertex cascades to its incident edges; the edge
    // text index must reflect that removal.
    graph.remove_vertex(a).unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text_e("body", "doomed", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
}

// =============================================================================
// Edge-side: traversal source (search_text_e / search_text_query_e)
// =============================================================================

#[test]
fn search_text_e_returns_top_k_in_score_order() {
    let (graph, a, b) = graph_with_edge_body_index();
    add_edge_with_body(
        &graph,
        a,
        b,
        "comment",
        "raft raft raft consensus consensus",
    );
    add_edge_with_body(&graph, a, b, "comment", "raft consensus");
    add_edge_with_body(&graph, a, b, "comment", "consensus only");
    add_edge_with_body(&graph, a, b, "comment", "totally unrelated content");

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text_e("body", "raft consensus", 10)
        .unwrap()
        .to_value_list();
    assert!(hits.len() >= 2);

    let g = graph.gremlin(Arc::clone(&graph));
    let top1 = g
        .search_text_e("body", "raft consensus", 1)
        .unwrap()
        .to_value_list();
    assert_eq!(top1.len(), 1);
}

#[test]
fn search_text_query_e_supports_phrase_query() {
    let (graph, a, b) = graph_with_edge_body_index();
    let phrase_match = add_edge_with_body(&graph, a, b, "comment", "the quick brown fox jumps");
    let _scattered = add_edge_with_body(
        &graph,
        a,
        b,
        "comment",
        "brown sugar and quick recipes for fox tacos",
    );

    let g = graph.gremlin(Arc::clone(&graph));
    let q = TextQuery::Phrase {
        text: "quick brown fox".to_string(),
        slop: 0,
    };
    let hits = g
        .search_text_query_e("body", &q, 10)
        .unwrap()
        .to_value_list();
    assert_eq!(hits, vec![Value::Edge(phrase_match)]);
}

#[test]
fn search_text_e_returns_error_for_unknown_property() {
    let graph = Arc::new(Graph::new());
    let g = graph.gremlin(Arc::clone(&graph));
    match g.search_text_e("missing", "anything", 10) {
        Err(TextIndexError::Storage(_)) => {}
        Err(other) => panic!("unexpected error variant: {other:?}"),
        Ok(_) => panic!("expected error for missing index"),
    }
}

#[test]
fn search_text_e_with_zero_k_returns_no_hits() {
    let (graph, a, b) = graph_with_edge_body_index();
    add_edge_with_body(&graph, a, b, "comment", "anything");
    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text_e("body", "anything", 0)
        .unwrap()
        .to_value_list();
    assert!(hits.is_empty());
}

#[test]
fn search_text_e_chains_with_filter_steps() {
    let (graph, a, b) = graph_with_edge_body_index();
    let _comment = add_edge_with_body(&graph, a, b, "comment", "raft consensus");
    let _endorses = add_edge_with_body(&graph, a, b, "endorses", "raft consensus");

    let g = graph.gremlin(Arc::clone(&graph));
    let comments_only = g
        .search_text_e("body", "raft", 10)
        .unwrap()
        .has_label("comment")
        .to_value_list();
    assert_eq!(comments_only.len(), 1);
}

#[test]
fn search_text_e_preserves_descending_score_order() {
    let (graph, a, b) = graph_with_edge_body_index();
    let strong = add_edge_with_body(
        &graph,
        a,
        b,
        "comment",
        "raft raft raft consensus consensus consensus",
    );
    let weak = add_edge_with_body(&graph, a, b, "comment", "raft");

    let g = graph.gremlin(Arc::clone(&graph));
    let hits = g
        .search_text_e("body", "raft consensus", 10)
        .unwrap()
        .to_value_list();

    assert_eq!(hits.first(), Some(&Value::Edge(strong)));
    assert!(hits.contains(&Value::Edge(weak)));
}

// =============================================================================
// Cross-element invariants
//
// Two parallel maps mean vertex and edge indexes are independent storage,
// but the spec mandates a globally-unique property-name namespace and
// guarantees zero bleed-through between the two.
// =============================================================================

#[test]
fn vertex_and_edge_indexes_on_different_properties_are_independent() {
    let graph = Arc::new(Graph::new());
    graph
        .create_text_index_v("bio", TextIndexConfig::default())
        .unwrap();
    graph
        .create_text_index_e("note", TextIndexConfig::default())
        .unwrap();

    // Add a vertex with `bio` and an edge with `note`. Neither index should
    // see the other's tokens; cross-search must miss.
    let mut vp = HashMap::new();
    vp.insert("bio".into(), Value::String("alice loves raft".into()));
    let alice = graph.add_vertex("person", vp);
    let bob = graph.add_vertex("person", HashMap::new());

    let mut ep = HashMap::new();
    ep.insert("note".into(), Value::String("paxos is fine too".into()));
    let edge = graph.add_edge(alice, bob, "comment", ep).unwrap();

    let g = graph.gremlin(Arc::clone(&graph));
    let v_hits = g.search_text("bio", "raft", 10).unwrap().to_value_list();
    assert_eq!(v_hits, vec![Value::Vertex(alice)]);

    let g = graph.gremlin(Arc::clone(&graph));
    let e_hits = g
        .search_text_e("note", "paxos", 10)
        .unwrap()
        .to_value_list();
    assert_eq!(e_hits, vec![Value::Edge(edge)]);

    // Edge-side search for a token that only the vertex index has must miss.
    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text_e("note", "raft", 10)
        .unwrap()
        .to_value_list()
        .is_empty());

    // And the symmetric direction.
    let g = graph.gremlin(Arc::clone(&graph));
    assert!(g
        .search_text("bio", "paxos", 10)
        .unwrap()
        .to_value_list()
        .is_empty());
}

#[test]
fn property_name_uniqueness_is_global_across_vertex_and_edge_indexes() {
    let graph = Graph::new();

    // Register a vertex index on `body`; a subsequent edge index on the same
    // property must be rejected per the global-uniqueness invariant.
    graph
        .create_text_index_v("body", TextIndexConfig::default())
        .unwrap();
    let err = graph
        .create_text_index_e("body", TextIndexConfig::default())
        .unwrap_err();
    assert!(matches!(err, TextIndexError::Storage(_)));

    // And the symmetric direction: drop the vertex index, register on edge,
    // then a vertex re-registration must be rejected.
    graph.drop_text_index_v("body").unwrap();
    graph
        .create_text_index_e("body", TextIndexConfig::default())
        .unwrap();
    let err = graph
        .create_text_index_v("body", TextIndexConfig::default())
        .unwrap_err();
    assert!(matches!(err, TextIndexError::Storage(_)));

    // Sanity: only one of the two maps holds the index at any given time.
    assert_eq!(graph.text_index_count_v(), 0);
    assert_eq!(graph.text_index_count_e(), 1);
}

// =============================================================================
// spec-55c Layer 1h: bridge contract for query-language entry points
//
// These tests pin the invariant that the FTS bridge (`from_snapshot_with_graph`
// vs `from_snapshot`) is wired into the four query-language entry points. The
// Gremlin script and GQL surface syntax for FTS lands in Layers 2-5; until
// then, these tests cover the underlying bridge so regressions surface early.
// =============================================================================

mod fts_bridge_contract {
    use super::*;
    use interstellar::traversal::GraphTraversalSource;

    /// A `GraphTraversalSource` built via `from_snapshot` (no `Graph` handle)
    /// must reject FTS calls with a clear, actionable error.
    #[test]
    fn from_snapshot_without_graph_handle_rejects_search_text() {
        let graph = graph_with_body_index();
        add_doc(&graph, "doc", "raft consensus algorithm");
        let snapshot = graph.snapshot();
        let g = GraphTraversalSource::from_snapshot(&snapshot);

        let err = g.search_text("body", "raft", 10).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("requires a live Graph handle"),
            "expected guidance about Graph handle, got: {msg}"
        );
    }

    /// `from_snapshot_with_graph` must accept FTS calls and route through the
    /// live registry. This is the exact path used by `Graph::query`,
    /// `Graph::execute_script_with_context`, `Graph::gql`, and
    /// `Graph::gql_with_params`.
    #[test]
    fn from_snapshot_with_graph_routes_to_text_index() {
        let graph = graph_with_body_index();
        add_doc(&graph, "doc", "raft consensus algorithm");
        add_doc(&graph, "doc", "paxos consensus");
        let snapshot = graph.snapshot();
        let g = GraphTraversalSource::from_snapshot_with_graph(&snapshot, Arc::clone(&graph));

        let hits = g.search_text("body", "consensus", 10).unwrap().to_list();
        assert_eq!(hits.len(), 2);
    }

    /// `Graph::query` must not panic and must return a successful result for a
    /// non-FTS Gremlin script run on a graph that *does* carry text indexes.
    /// This guards against any accidental coupling between text-index presence
    /// and the bridge plumbing.
    #[cfg(feature = "gremlin")]
    #[test]
    fn graph_query_succeeds_when_text_indexes_present() {
        let graph = graph_with_body_index();
        add_doc(&graph, "doc", "raft consensus algorithm");
        add_doc(&graph, "doc", "paxos consensus");

        let result = graph.query("g.V().count().toList()").unwrap();
        // The exact ExecutionResult shape varies; we only require non-error.
        let _ = result;
    }

    /// `Graph::gql` similarly must not panic when text indexes are registered;
    /// the new `Compiler.graph_handle` plumbing must remain harmless when no
    /// FTS CALL procedure is invoked. Layer 5 will add positive FTS coverage.
    #[cfg(feature = "gql")]
    #[test]
    fn graph_gql_succeeds_when_text_indexes_present() {
        let graph = graph_with_body_index();
        add_doc(&graph, "doc", "raft consensus algorithm");

        let results = graph.gql("MATCH (n) RETURN count(n)").unwrap();
        assert_eq!(results.len(), 1);
    }

    /// The same invariant for edges: a snapshot built without a graph handle
    /// must reject `search_text_e` with the same actionable error.
    #[test]
    fn from_snapshot_without_graph_handle_rejects_search_text_e() {
        let graph = Arc::new(Graph::new());
        graph
            .create_text_index_e("relation", TextIndexConfig::default())
            .unwrap();
        let snapshot = graph.snapshot();
        let g = GraphTraversalSource::from_snapshot(&snapshot);

        let err = g.search_text_e("relation", "anything", 10).unwrap_err();
        assert!(err.to_string().contains("requires a live Graph handle"));
    }
}