relateby-pattern 0.4.2

Core pattern data structures
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
//! Integration tests for StandardGraph and SubjectBuilder.

use pattern_core::graph::StandardGraph;
use pattern_core::subject::{Subject, Symbol, Value};
use pattern_core::Pattern;

// ============================================================================
// SubjectBuilder tests (US2)
// ============================================================================

#[test]
fn subject_builder_basic() {
    let subject = Subject::build("alice")
        .label("Person")
        .property("name", "Alice")
        .done();

    assert_eq!(subject.identity, Symbol("alice".to_string()));
    assert!(subject.labels.contains("Person"));
    assert_eq!(
        subject.properties.get("name"),
        Some(&Value::VString("Alice".to_string()))
    );
}

#[test]
fn subject_builder_multiple_labels_and_properties() {
    let subject = Subject::build("alice")
        .label("Person")
        .label("Employee")
        .property("name", "Alice Smith")
        .property("age", 30i64)
        .property("active", true)
        .done();

    assert_eq!(subject.identity.0, "alice");
    assert_eq!(subject.labels.len(), 2);
    assert!(subject.labels.contains("Person"));
    assert!(subject.labels.contains("Employee"));
    assert_eq!(subject.properties.len(), 3);
    assert_eq!(subject.properties.get("age"), Some(&Value::VInteger(30)));
    assert_eq!(
        subject.properties.get("active"),
        Some(&Value::VBoolean(true))
    );
}

#[test]
fn subject_builder_into_subject() {
    let subject: Subject = Subject::build("bob").label("Person").into();
    assert_eq!(subject.identity.0, "bob");
    assert!(subject.labels.contains("Person"));
}

#[test]
fn subject_builder_empty() {
    let subject = Subject::build("x").done();
    assert_eq!(subject.identity.0, "x");
    assert!(subject.labels.is_empty());
    assert!(subject.properties.is_empty());
}

// ============================================================================
// StandardGraph construction tests (US1)
// ============================================================================

#[test]
fn new_graph_is_empty() {
    let g = StandardGraph::new();
    assert!(g.is_empty());
    assert_eq!(g.node_count(), 0);
    assert_eq!(g.relationship_count(), 0);
    assert_eq!(g.walk_count(), 0);
    assert_eq!(g.annotation_count(), 0);
    assert!(!g.has_conflicts());
}

#[test]
fn add_nodes() {
    let mut g = StandardGraph::new();
    g.add_node(
        Subject::build("alice")
            .label("Person")
            .property("name", "Alice")
            .done(),
    );
    g.add_node(
        Subject::build("bob")
            .label("Person")
            .property("name", "Bob")
            .done(),
    );

    assert_eq!(g.node_count(), 2);
    assert!(!g.is_empty());

    let alice = g.node(&Symbol("alice".to_string())).unwrap();
    assert_eq!(alice.value.identity.0, "alice");
    assert!(alice.value.labels.contains("Person"));
}

#[test]
fn add_relationship_creates_placeholder_nodes() {
    let mut g = StandardGraph::new();
    g.add_relationship(
        Subject::build("r1").label("KNOWS").done(),
        &Subject::from_id("alice"),
        &Subject::from_id("bob"),
    );

    assert_eq!(g.relationship_count(), 1);
    // Placeholder nodes should be auto-created
    assert_eq!(g.node_count(), 2);
    assert!(g.node(&"alice".into()).is_some());
    assert!(g.node(&"bob".into()).is_some());
}

#[test]
fn add_relationship_uses_existing_nodes() {
    let mut g = StandardGraph::new();
    let alice = Subject::build("alice")
        .label("Person")
        .property("name", "Alice")
        .done();
    let bob = Subject::build("bob").label("Person").done();
    g.add_node(alice.clone());
    g.add_node(bob.clone());
    g.add_relationship(Subject::build("r1").label("KNOWS").done(), &alice, &bob);

    assert_eq!(g.node_count(), 2);
    assert_eq!(g.relationship_count(), 1);

    // The relationship's source should have the full alice node data
    let rel = g.relationship(&"r1".into()).unwrap();
    assert!(rel.elements[0].value.labels.contains("Person"));
    assert_eq!(
        rel.elements[0].value.properties.get("name"),
        Some(&Value::VString("Alice".to_string()))
    );
}

#[test]
fn add_node_last_write_wins() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("alice").label("Person").done());
    g.add_node(Subject::build("alice").label("Employee").done());

    assert_eq!(g.node_count(), 1);
    let alice = g.node(&"alice".into()).unwrap();
    // Last write wins — should have Employee label, not Person
    assert!(alice.value.labels.contains("Employee"));
    assert!(!alice.value.labels.contains("Person"));
}

#[test]
fn add_walk() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("a").done());
    g.add_node(Subject::build("b").done());
    g.add_node(Subject::build("c").done());
    g.add_relationship(
        Subject::build("r1").done(),
        &Subject::from_id("a"),
        &Subject::from_id("b"),
    );
    g.add_relationship(
        Subject::build("r2").done(),
        &Subject::from_id("b"),
        &Subject::from_id("c"),
    );
    g.add_walk(
        Subject::build("w1").label("Path").done(),
        &[Subject::from_id("r1"), Subject::from_id("r2")],
    );

    assert_eq!(g.walk_count(), 1);
    let walk = g.walk(&"w1".into()).unwrap();
    assert!(walk.value.labels.contains("Path"));
}

#[test]
fn add_annotation() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("alice").label("Person").done());
    g.add_annotation(
        Subject::build("a1").label("Note").done(),
        &Subject::from_id("alice"),
    );

    assert_eq!(g.annotation_count(), 1);
    let ann = g.annotation(&"a1".into()).unwrap();
    assert!(ann.value.labels.contains("Note"));
    assert_eq!(ann.elements.len(), 1);
}

#[test]
fn element_access_returns_none_for_missing() {
    let g = StandardGraph::new();
    assert!(g.node(&"missing".into()).is_none());
    assert!(g.relationship(&"missing".into()).is_none());
    assert!(g.walk(&"missing".into()).is_none());
    assert!(g.annotation(&"missing".into()).is_none());
}

// ============================================================================
// Pattern ingestion tests (US3)
// ============================================================================

#[test]
fn add_pattern_classifies_node() {
    let mut g = StandardGraph::new();
    let node_pattern = Pattern::point(Subject::build("alice").label("Person").done());
    g.add_pattern(node_pattern);

    assert_eq!(g.node_count(), 1);
    assert!(g.node(&"alice".into()).is_some());
}

#[test]
fn add_pattern_classifies_relationship() {
    let mut g = StandardGraph::new();
    let rel = Pattern::pattern(
        Subject::build("r1").label("KNOWS").done(),
        vec![
            Pattern::point(Subject::build("alice").done()),
            Pattern::point(Subject::build("bob").done()),
        ],
    );
    g.add_pattern(rel);

    assert_eq!(g.relationship_count(), 1);
    // Endpoint nodes should be merged into nodes bucket
    assert!(g.node_count() >= 2);
}

#[test]
fn from_patterns_constructor() {
    let patterns = vec![
        Pattern::point(Subject::build("alice").label("Person").done()),
        Pattern::point(Subject::build("bob").label("Person").done()),
    ];
    let g = StandardGraph::from_patterns(patterns);

    assert_eq!(g.node_count(), 2);
}

#[test]
fn from_pattern_graph_wraps_directly() {
    let mut g1 = StandardGraph::new();
    g1.add_node(Subject::build("alice").done());
    g1.add_node(Subject::build("bob").done());

    let pg = g1.into_pattern_graph();
    let g2 = StandardGraph::from_pattern_graph(pg);

    assert_eq!(g2.node_count(), 2);
}

// ============================================================================
// Iterator tests (US4)
// ============================================================================

#[test]
fn iterators_visit_all_elements() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("a").done());
    g.add_node(Subject::build("b").done());
    g.add_node(Subject::build("c").done());
    g.add_relationship(
        Subject::build("r1").done(),
        &Subject::from_id("a"),
        &Subject::from_id("b"),
    );
    g.add_relationship(
        Subject::build("r2").done(),
        &Subject::from_id("b"),
        &Subject::from_id("c"),
    );

    assert_eq!(g.nodes().count(), 3);
    assert_eq!(g.relationships().count(), 2);
}

// ============================================================================
// Graph-native query tests (US4)
// ============================================================================

#[test]
fn source_and_target() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("alice").done());
    g.add_node(Subject::build("bob").done());
    g.add_relationship(
        Subject::build("r1").done(),
        &Subject::from_id("alice"),
        &Subject::from_id("bob"),
    );

    let src = g.source(&"r1".into()).unwrap();
    assert_eq!(src.value.identity.0, "alice");

    let tgt = g.target(&"r1".into()).unwrap();
    assert_eq!(tgt.value.identity.0, "bob");
}

#[test]
fn source_target_returns_none_for_missing() {
    let g = StandardGraph::new();
    assert!(g.source(&"missing".into()).is_none());
    assert!(g.target(&"missing".into()).is_none());
}

#[test]
fn neighbors_bidirectional() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("a").done());
    g.add_node(Subject::build("b").done());
    g.add_node(Subject::build("c").done());
    g.add_relationship(
        Subject::build("r1").done(),
        &Subject::from_id("a"),
        &Subject::from_id("b"),
    );
    g.add_relationship(
        Subject::build("r2").done(),
        &Subject::from_id("c"),
        &Subject::from_id("b"),
    );

    // b has neighbors: a (via r1 where b is target) and c (via r2 where b is target)
    let neighbors = g.neighbors(&"b".into());
    assert_eq!(neighbors.len(), 2);

    let neighbor_ids: std::collections::HashSet<&str> = neighbors
        .iter()
        .map(|p| p.value.identity.0.as_str())
        .collect();
    assert!(neighbor_ids.contains("a"));
    assert!(neighbor_ids.contains("c"));
}

#[test]
fn degree() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("a").done());
    g.add_node(Subject::build("b").done());
    g.add_node(Subject::build("c").done());
    g.add_relationship(
        Subject::build("r1").done(),
        &Subject::from_id("a"),
        &Subject::from_id("b"),
    );
    g.add_relationship(
        Subject::build("r2").done(),
        &Subject::from_id("b"),
        &Subject::from_id("c"),
    );

    assert_eq!(g.degree(&"a".into()), 1);
    assert_eq!(g.degree(&"b".into()), 2);
    assert_eq!(g.degree(&"c".into()), 1);
    assert_eq!(g.degree(&"missing".into()), 0);
}

// ============================================================================
// Escape hatch tests (US5)
// ============================================================================

#[test]
fn as_pattern_graph() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("alice").done());

    let pg = g.as_pattern_graph();
    assert_eq!(pg.pg_nodes.len(), 1);
}

#[test]
fn into_pattern_graph() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("alice").done());
    g.add_node(Subject::build("bob").done());

    let pg = g.into_pattern_graph();
    assert_eq!(pg.pg_nodes.len(), 2);
}

#[test]
fn as_query() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("alice").done());
    g.add_node(Subject::build("bob").done());
    g.add_relationship(
        Subject::build("r1").done(),
        &Subject::from_id("alice"),
        &Subject::from_id("bob"),
    );

    let query = g.as_query();
    let nodes = (query.query_nodes)();
    assert_eq!(nodes.len(), 2);
    let rels = (query.query_relationships)();
    assert_eq!(rels.len(), 1);
}

#[test]
fn as_snapshot() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("alice").done());
    g.add_node(Subject::build("bob").done());
    g.add_relationship(
        Subject::build("r1").done(),
        &Subject::from_id("alice"),
        &Subject::from_id("bob"),
    );

    let snapshot = g.as_snapshot();
    // 2 nodes + 1 relationship = 3 elements
    assert_eq!(snapshot.view_elements.len(), 3);
}

// ============================================================================
// Edge cases
// ============================================================================

#[test]
fn default_creates_empty_graph() {
    let g = StandardGraph::default();
    assert!(g.is_empty());
}

#[test]
fn chaining_add_methods() {
    let mut g = StandardGraph::new();
    g.add_node(Subject::build("a").done())
        .add_node(Subject::build("b").done())
        .add_relationship(
            Subject::build("r1").done(),
            &Subject::from_id("a"),
            &Subject::from_id("b"),
        );

    assert_eq!(g.node_count(), 2);
    assert_eq!(g.relationship_count(), 1);
}

// ============================================================================
// Scale validation (T018b)
// ============================================================================

#[test]
fn scale_1000_nodes_5000_relationships() {
    let mut g = StandardGraph::new();

    // Add 1000 nodes
    for i in 0..1000 {
        g.add_node(Subject::build(format!("n{}", i)).label("Node").done());
    }
    assert_eq!(g.node_count(), 1000);

    // Add 5000 relationships (cycling through nodes)
    for i in 0..5000 {
        let src = Subject::from_id(format!("n{}", i % 1000));
        let tgt = Subject::from_id(format!("n{}", (i + 1) % 1000));
        g.add_relationship(
            Subject::build(format!("r{}", i)).label("REL").done(),
            &src,
            &tgt,
        );
    }
    assert_eq!(g.relationship_count(), 5000);
    assert_eq!(g.node_count(), 1000);

    // Verify node access
    assert!(g.node(&"n0".into()).is_some());
    assert!(g.node(&"n999".into()).is_some());
    assert!(g.node(&"n1000".into()).is_none());

    // Verify iterator counts
    assert_eq!(g.nodes().count(), 1000);
    assert_eq!(g.relationships().count(), 5000);

    // Verify source/target
    let src = g.source(&"r0".into()).unwrap();
    assert_eq!(src.value.identity.0, "n0");
    let tgt = g.target(&"r0".into()).unwrap();
    assert_eq!(tgt.value.identity.0, "n1");

    // Verify degree (n0 has: r0 as source, r4999 as target, r999 as source, r1999 as source, etc.)
    let deg = g.degree(&"n0".into());
    assert!(deg > 0);

    // Verify neighbors
    let neighbors = g.neighbors(&"n0".into());
    assert!(!neighbors.is_empty());
}

// ============================================================================
// Back-reference label preservation tests (issue: overwrite bug)
// ============================================================================

/// A relationship pattern whose endpoint nodes are back-references (no labels)
/// must not overwrite labels established by an earlier pattern.
#[test]
fn from_patterns_back_reference_preserves_labels() {
    // Pattern 1: (red:Red)-[:GO]->(blue:Blue)
    let red_labeled = Subject::build("red").label("Red").done();
    let blue_labeled = Subject::build("blue").label("Blue").done();
    let p1 = Pattern::pattern(
        Subject::build("go1").label("GO").done(),
        vec![Pattern::point(red_labeled), Pattern::point(blue_labeled)],
    );
    // Pattern 2: (blue)-[:GO]->(red)  — back-references, no labels
    let p2 = Pattern::pattern(
        Subject::build("go2").label("GO").done(),
        vec![
            Pattern::point(Subject::from_id("blue")),
            Pattern::point(Subject::from_id("red")),
        ],
    );

    let g = StandardGraph::from_patterns(vec![p1, p2]);

    let red = g.node(&"red".into()).unwrap();
    let blue = g.node(&"blue".into()).unwrap();

    assert!(
        red.value.labels.contains("Red"),
        "red should keep label Red"
    );
    assert!(
        blue.value.labels.contains("Blue"),
        "blue should keep label Blue"
    );
    assert_eq!(g.relationship_count(), 2);
}

/// add_pattern should apply the same union semantics as from_patterns.
#[test]
fn add_pattern_back_reference_preserves_labels() {
    let mut g = StandardGraph::new();

    let p1 = Pattern::pattern(
        Subject::build("r1").label("LINK").done(),
        vec![
            Pattern::point(Subject::build("a").label("A").done()),
            Pattern::point(Subject::build("b").label("B").done()),
        ],
    );
    let p2 = Pattern::pattern(
        Subject::build("r2").label("LINK").done(),
        vec![
            Pattern::point(Subject::from_id("b")),
            Pattern::point(Subject::from_id("a")),
        ],
    );

    g.add_pattern(p1);
    g.add_pattern(p2);

    let a = g.node(&"a".into()).unwrap();
    let b = g.node(&"b".into()).unwrap();

    assert!(a.value.labels.contains("A"), "a should keep label A");
    assert!(b.value.labels.contains("B"), "b should keep label B");
}

/// Three-node cycle: every node has its label only in the first pattern.
#[test]
fn from_patterns_three_node_cycle_preserves_all_labels() {
    // (green:Green)-[:GO]->(red:Red)
    let p1 = Pattern::pattern(
        Subject::build("go1").label("GO").done(),
        vec![
            Pattern::point(Subject::build("green").label("Green").done()),
            Pattern::point(Subject::build("red").label("Red").done()),
        ],
    );
    // (red)-[:GO]->(blue:Blue)
    let p2 = Pattern::pattern(
        Subject::build("go2").label("GO").done(),
        vec![
            Pattern::point(Subject::from_id("red")),
            Pattern::point(Subject::build("blue").label("Blue").done()),
        ],
    );
    // (blue)-[:GO]->(green)
    let p3 = Pattern::pattern(
        Subject::build("go3").label("GO").done(),
        vec![
            Pattern::point(Subject::from_id("blue")),
            Pattern::point(Subject::from_id("green")),
        ],
    );

    let g = StandardGraph::from_patterns(vec![p1, p2, p3]);

    assert!(g
        .node(&"green".into())
        .unwrap()
        .value
        .labels
        .contains("Green"));
    assert!(g.node(&"red".into()).unwrap().value.labels.contains("Red"));
    assert!(g
        .node(&"blue".into())
        .unwrap()
        .value
        .labels
        .contains("Blue"));
    assert_eq!(g.relationship_count(), 3);
}