bc-envelope 0.43.0

Gordian Envelope for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
#![cfg(feature = "edge")]

use bc_components::DigestProvider;
use bc_envelope::prelude::*;
use indoc::indoc;

mod common;
use crate::common::test_data::*;

/// Helper to create a basic edge envelope with the three required assertions.
fn make_edge(
    subject: &str,
    is_a: &str,
    source: &Envelope,
    target: &Envelope,
) -> Envelope {
    Envelope::new(subject)
        .add_assertion(known_values::IS_A, is_a)
        .add_assertion(known_values::SOURCE, source.clone())
        .add_assertion(known_values::TARGET, target.clone())
}

/// Helper to create an XID-like identifier envelope.
fn xid_like(name: &str) -> Envelope { Envelope::new(name) }

// -------------------------------------------------------------------
// Edge construction and format
// -------------------------------------------------------------------

#[test]
fn test_edge_basic_format() {
    let alice = xid_like("Alice");
    let edge = make_edge("credential-1", "foaf:Person", &alice, &alice);

    // expected-text-output-rubric:
    #[rustfmt::skip]
    assert_actual_expected!(edge.format(), indoc! {r#"
        "credential-1" [
            'isA': "foaf:Person"
            'source': "Alice"
            'target': "Alice"
        ]
    "#}.trim());
}

#[test]
fn test_edge_relationship_format() {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge = make_edge("knows-bob", "schema:colleague", &alice, &bob);

    // expected-text-output-rubric:
    #[rustfmt::skip]
    assert_actual_expected!(edge.format(), indoc! {r#"
        "knows-bob" [
            'isA': "schema:colleague"
            'source': "Alice"
            'target': "Bob"
        ]
    "#}.trim());
}

// -------------------------------------------------------------------
// Edge validation
// -------------------------------------------------------------------

#[test]
fn test_validate_edge_valid() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);
    assert!(edge.validate_edge().is_ok());
}

#[test]
fn test_validate_edge_missing_is_a() {
    let alice = xid_like("Alice");
    let edge = Envelope::new("cred-1")
        .add_assertion(known_values::SOURCE, alice.clone())
        .add_assertion(known_values::TARGET, alice.clone());
    let result = edge.validate_edge();
    assert!(matches!(result, Err(EnvelopeError::EdgeMissingIsA)));
}

#[test]
fn test_validate_edge_missing_source() {
    let alice = xid_like("Alice");
    let edge = Envelope::new("cred-1")
        .add_assertion(known_values::IS_A, "foaf:Person")
        .add_assertion(known_values::TARGET, alice.clone());
    let result = edge.validate_edge();
    assert!(matches!(result, Err(EnvelopeError::EdgeMissingSource)));
}

#[test]
fn test_validate_edge_missing_target() {
    let alice = xid_like("Alice");
    let edge = Envelope::new("cred-1")
        .add_assertion(known_values::IS_A, "foaf:Person")
        .add_assertion(known_values::SOURCE, alice.clone());
    let result = edge.validate_edge();
    assert!(matches!(result, Err(EnvelopeError::EdgeMissingTarget)));
}

#[test]
fn test_validate_edge_no_assertions() {
    let edge = Envelope::new("cred-1");
    let result = edge.validate_edge();
    assert!(matches!(result, Err(EnvelopeError::EdgeMissingIsA)));
}

#[test]
fn test_validate_edge_duplicate_is_a() {
    let alice = xid_like("Alice");
    let edge = Envelope::new("cred-1")
        .add_assertion(known_values::IS_A, "foaf:Person")
        .add_assertion(known_values::IS_A, "schema:Thing")
        .add_assertion(known_values::SOURCE, alice.clone())
        .add_assertion(known_values::TARGET, alice.clone());
    let result = edge.validate_edge();
    assert!(matches!(result, Err(EnvelopeError::EdgeDuplicateIsA)));
}

#[test]
fn test_validate_edge_duplicate_source() {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge = Envelope::new("cred-1")
        .add_assertion(known_values::IS_A, "foaf:Person")
        .add_assertion(known_values::SOURCE, alice.clone())
        .add_assertion(known_values::SOURCE, bob.clone())
        .add_assertion(known_values::TARGET, alice.clone());
    let result = edge.validate_edge();
    assert!(matches!(result, Err(EnvelopeError::EdgeDuplicateSource)));
}

#[test]
fn test_validate_edge_duplicate_target() {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge = Envelope::new("cred-1")
        .add_assertion(known_values::IS_A, "foaf:Person")
        .add_assertion(known_values::SOURCE, alice.clone())
        .add_assertion(known_values::TARGET, alice.clone())
        .add_assertion(known_values::TARGET, bob.clone());
    let result = edge.validate_edge();
    assert!(matches!(result, Err(EnvelopeError::EdgeDuplicateTarget)));
}

#[test]
fn test_validate_edge_wrapped_signed() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    // Wrap and sign the edge
    let signed_edge = edge.wrap().add_signature(&alice_private_key());

    // Signed (wrapped) edge should still validate
    assert!(signed_edge.validate_edge().is_ok());
}

// -------------------------------------------------------------------
// Edge accessor methods
// -------------------------------------------------------------------

#[test]
fn test_edge_is_a() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    let is_a = edge.edge_is_a()?;
    assert_actual_expected!(is_a.format(), r#""foaf:Person""#);
    Ok(())
}

#[test]
fn test_edge_source() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    let source = edge.edge_source()?;
    assert_actual_expected!(source.format(), r#""Alice""#);
    Ok(())
}

#[test]
fn test_edge_target() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge = make_edge("knows-bob", "schema:colleague", &alice, &bob);

    let target = edge.edge_target()?;
    assert_actual_expected!(target.format(), r#""Bob""#);
    Ok(())
}

#[test]
fn test_edge_subject() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge = make_edge("my-credential", "foaf:Person", &alice, &alice);

    let subject = edge.edge_subject()?;
    assert_actual_expected!(subject.format(), r#""my-credential""#);
    Ok(())
}

#[test]
fn test_edge_accessors_on_signed_edge() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &bob);

    let signed_edge = edge.wrap().add_signature(&alice_private_key());

    // Accessors should work through the wrapped/signed layer
    let is_a = signed_edge.edge_is_a()?;
    assert_actual_expected!(is_a.format(), r#""foaf:Person""#);

    let source = signed_edge.edge_source()?;
    assert_actual_expected!(source.format(), r#""Alice""#);

    let target = signed_edge.edge_target()?;
    assert_actual_expected!(target.format(), r#""Bob""#);

    let subject = signed_edge.edge_subject()?;
    assert_actual_expected!(subject.format(), r#""cred-1""#);

    Ok(())
}

// -------------------------------------------------------------------
// Adding edges to envelopes
// -------------------------------------------------------------------

#[test]
fn test_add_edge_envelope() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    let doc = Envelope::new("Alice").add_edge_envelope(edge);

    // expected-text-output-rubric:
    #[rustfmt::skip]
    assert_actual_expected!(doc.format(), indoc! {r#"
        "Alice" [
            'edge': "cred-1" [
                'isA': "foaf:Person"
                'source': "Alice"
                'target': "Alice"
            ]
        ]
    "#}.trim());
}

#[test]
fn test_add_multiple_edges() {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge1 = make_edge("self-desc", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("knows-bob", "schema:colleague", &alice, &bob);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2);

    let edges = doc.edges().unwrap();
    assert_eq!(edges.len(), 2);

    let formatted = doc.format();
    assert!(formatted.contains("'edge'"));
    assert!(formatted.contains("\"self-desc\""));
    assert!(formatted.contains("\"knows-bob\""));
}

// -------------------------------------------------------------------
// Edges retrieval via envelope
// -------------------------------------------------------------------

#[test]
fn test_edges_empty() -> Result<(), EnvelopeError> {
    let doc = Envelope::new("Alice");
    let edges = doc.edges()?;
    assert_eq!(edges.len(), 0);
    Ok(())
}

#[test]
fn test_edges_retrieval() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge1 = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("cred-2", "schema:Thing", &alice, &alice);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2);

    let edges = doc.edges()?;
    assert_eq!(edges.len(), 2);

    // Each retrieved edge should be a valid edge
    for edge in &edges {
        edge.validate_edge()?;
    }

    Ok(())
}

// -------------------------------------------------------------------
// Edges container (add / get / remove / clear / len)
// -------------------------------------------------------------------

#[test]
fn test_edges_container_new_is_empty() {
    let edges = Edges::new();
    assert!(edges.is_empty());
    assert_eq!(edges.len(), 0);
}

#[test]
fn test_edges_container_add_and_get() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let digest = edge.digest();

    let mut edges = Edges::new();
    edges.add(edge.clone());

    assert!(!edges.is_empty());
    assert_eq!(edges.len(), 1);
    assert!(edges.get(digest).is_some());
    assert!(edges.get(digest).unwrap().is_equivalent_to(&edge));
}

#[test]
fn test_edges_container_remove() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let digest = edge.digest();

    let mut edges = Edges::new();
    edges.add(edge);

    let removed = edges.remove(digest);
    assert!(removed.is_some());
    assert!(edges.is_empty());
}

#[test]
fn test_edges_container_remove_nonexistent() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    let mut edges = Edges::new();
    let removed = edges.remove(edge.digest());
    assert!(removed.is_none());
}

#[test]
fn test_edges_container_clear() {
    let alice = xid_like("Alice");
    let edge1 = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("cred-2", "schema:Thing", &alice, &alice);

    let mut edges = Edges::new();
    edges.add(edge1);
    edges.add(edge2);
    assert_eq!(edges.len(), 2);

    edges.clear();
    assert!(edges.is_empty());
    assert_eq!(edges.len(), 0);
}

#[test]
fn test_edges_container_iter() {
    let alice = xid_like("Alice");
    let edge1 = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("cred-2", "schema:Thing", &alice, &alice);

    let mut edges = Edges::new();
    edges.add(edge1);
    edges.add(edge2);

    let count = edges.iter().count();
    assert_eq!(count, 2);
}

// -------------------------------------------------------------------
// Edges container round-trip: add_to_envelope / try_from_envelope
// -------------------------------------------------------------------

#[test]
fn test_edges_container_roundtrip() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge1 = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("cred-2", "schema:Thing", &alice, &alice);

    let mut edges = Edges::new();
    edges.add(edge1.clone());
    edges.add(edge2.clone());

    // Serialize to envelope
    let doc = Envelope::new("Alice");
    let doc_with_edges = edges.add_to_envelope(doc);

    // Deserialize back
    let recovered = Edges::try_from_envelope(&doc_with_edges)?;
    assert_eq!(recovered.len(), 2);
    assert!(recovered.get(edge1.digest()).is_some());
    assert!(recovered.get(edge2.digest()).is_some());

    Ok(())
}

#[test]
fn test_edges_container_roundtrip_empty() -> Result<(), EnvelopeError> {
    let edges = Edges::new();
    let doc = Envelope::new("Alice");
    let doc_with_edges = edges.add_to_envelope(doc);

    let recovered = Edges::try_from_envelope(&doc_with_edges)?;
    assert!(recovered.is_empty());

    Ok(())
}

#[test]
fn test_edges_container_roundtrip_preserves_format() -> Result<(), EnvelopeError>
{
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge = make_edge("knows-bob", "schema:colleague", &alice, &bob);

    let mut edges = Edges::new();
    edges.add(edge);

    let doc = edges.add_to_envelope(Envelope::new("Alice"));

    // expected-text-output-rubric:
    #[rustfmt::skip]
    assert_actual_expected!(doc.format(), indoc! {r#"
        "Alice" [
            'edge': "knows-bob" [
                'isA': "schema:colleague"
                'source': "Alice"
                'target': "Bob"
            ]
        ]
    "#}.trim());

    let recovered = Edges::try_from_envelope(&doc)?;
    assert_eq!(recovered.len(), 1);

    Ok(())
}

// -------------------------------------------------------------------
// Edgeable trait
// -------------------------------------------------------------------

#[test]
fn test_edgeable_default_methods() {
    // Use a struct that we can manually wrap — in practice this is used
    // via impl_edgeable! on XIDDocument, but we test the trait methods
    // directly on the Edges container to verify behavior.
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let digest = edge.digest();

    let mut edges = Edges::new();
    edges.add(edge);

    assert!(!edges.is_empty());
    assert_eq!(edges.len(), 1);
    assert!(edges.get(digest).is_some());

    let removed = edges.remove(digest);
    assert!(removed.is_some());
    assert!(edges.is_empty());
}

// -------------------------------------------------------------------
// edges_matching — filtering by criteria
// -------------------------------------------------------------------

#[test]
fn test_edges_matching_no_filters() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge1 = make_edge("self-desc", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("knows-bob", "schema:colleague", &alice, &bob);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2);

    // No filters => all edges
    let matching = doc.edges_matching(None, None, None, None)?;
    assert_eq!(matching.len(), 2);

    Ok(())
}

#[test]
fn test_edges_matching_by_is_a() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge1 = make_edge("self-desc", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("knows-bob", "schema:colleague", &alice, &bob);
    let edge3 = make_edge("self-thing", "foaf:Person", &alice, &alice);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2)
        .add_edge_envelope(edge3);

    let is_a_person = Envelope::new("foaf:Person");
    let matching = doc.edges_matching(Some(&is_a_person), None, None, None)?;
    assert_eq!(matching.len(), 2);

    let is_a_colleague = Envelope::new("schema:colleague");
    let matching =
        doc.edges_matching(Some(&is_a_colleague), None, None, None)?;
    assert_eq!(matching.len(), 1);

    let is_a_none = Envelope::new("nonexistent");
    let matching = doc.edges_matching(Some(&is_a_none), None, None, None)?;
    assert_eq!(matching.len(), 0);

    Ok(())
}

#[test]
fn test_edges_matching_by_source() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge1 = make_edge("alice-claim", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("bob-claim", "foaf:Person", &bob, &alice);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2);

    let matching = doc.edges_matching(None, Some(&alice), None, None)?;
    assert_eq!(matching.len(), 1);

    let matching = doc.edges_matching(None, Some(&bob), None, None)?;
    assert_eq!(matching.len(), 1);

    let carol = xid_like("Carol");
    let matching = doc.edges_matching(None, Some(&carol), None, None)?;
    assert_eq!(matching.len(), 0);

    Ok(())
}

#[test]
fn test_edges_matching_by_target() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge1 = make_edge("self-desc", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("knows-bob", "schema:colleague", &alice, &bob);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2);

    let matching = doc.edges_matching(None, None, Some(&alice), None)?;
    assert_eq!(matching.len(), 1);

    let matching = doc.edges_matching(None, None, Some(&bob), None)?;
    assert_eq!(matching.len(), 1);

    Ok(())
}

#[test]
fn test_edges_matching_by_subject() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge1 = make_edge("self-desc", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("cred-2", "schema:Thing", &alice, &alice);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2);

    let subject_filter = Envelope::new("self-desc");
    let matching =
        doc.edges_matching(None, None, None, Some(&subject_filter))?;
    assert_eq!(matching.len(), 1);

    let subject_filter = Envelope::new("nonexistent");
    let matching =
        doc.edges_matching(None, None, None, Some(&subject_filter))?;
    assert_eq!(matching.len(), 0);

    Ok(())
}

#[test]
fn test_edges_matching_combined_filters() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge1 = make_edge("self-desc", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("self-thing", "foaf:Person", &alice, &alice);
    let edge3 = make_edge("knows-bob", "foaf:Person", &alice, &bob);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2)
        .add_edge_envelope(edge3);

    // All three are foaf:Person
    let is_a = Envelope::new("foaf:Person");
    let matching = doc.edges_matching(Some(&is_a), None, None, None)?;
    assert_eq!(matching.len(), 3);

    // foaf:Person + target Alice => 2 (self-desc, self-thing)
    let matching = doc.edges_matching(Some(&is_a), None, Some(&alice), None)?;
    assert_eq!(matching.len(), 2);

    // foaf:Person + target Bob => 1 (knows-bob)
    let matching = doc.edges_matching(Some(&is_a), None, Some(&bob), None)?;
    assert_eq!(matching.len(), 1);

    // foaf:Person + target Alice + subject "self-desc" => 1
    let subj = Envelope::new("self-desc");
    let matching =
        doc.edges_matching(Some(&is_a), None, Some(&alice), Some(&subj))?;
    assert_eq!(matching.len(), 1);

    // foaf:Person + source Alice + target Bob + subject "knows-bob" => 1
    let subj = Envelope::new("knows-bob");
    let matching =
        doc.edges_matching(Some(&is_a), Some(&alice), Some(&bob), Some(&subj))?;
    assert_eq!(matching.len(), 1);

    // All filters that match nothing
    let subj = Envelope::new("nonexistent");
    let matching = doc.edges_matching(
        Some(&is_a),
        Some(&alice),
        Some(&alice),
        Some(&subj),
    )?;
    assert_eq!(matching.len(), 0);

    Ok(())
}

// -------------------------------------------------------------------
// Signed edges with format verification
// -------------------------------------------------------------------

#[test]
fn test_signed_edge_format() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    let signed_edge = edge.wrap().add_signature(&alice_private_key());

    // expected-text-output-rubric:
    #[rustfmt::skip]
    assert_actual_expected!(signed_edge.format(), indoc! {r#"
        {
            "cred-1" [
                'isA': "foaf:Person"
                'source': "Alice"
                'target': "Alice"
            ]
        } [
            'signed': Signature
        ]
    "#}.trim());
}

#[test]
fn test_signed_edge_on_document_format() {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);
    let signed_edge = edge.wrap().add_signature(&alice_private_key());

    let doc = Envelope::new("Alice")
        .add_assertion("knows", "Bob")
        .add_edge_envelope(signed_edge);

    let formatted = doc.format();
    assert!(formatted.contains("'edge': {"));
    assert!(formatted.contains("'signed': Signature"));
    assert!(formatted.contains("'isA': \"foaf:Person\""));
}

// -------------------------------------------------------------------
// Edge coexistence with attachments
// -------------------------------------------------------------------

#[test]
fn test_edges_coexist_with_attachments() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    let doc = Envelope::new("Alice")
        .add_attachment(
            "Metadata",
            "com.example",
            Some("https://example.com/v1"),
        )
        .add_edge_envelope(edge);

    // Both should be present
    assert_eq!(doc.edges()?.len(), 1);
    assert_eq!(doc.attachments()?.len(), 1);

    let formatted = doc.format();
    assert!(formatted.contains("'edge'"));
    assert!(formatted.contains("'attachment'"));

    Ok(())
}

// -------------------------------------------------------------------
// Edge UR round-trip
// -------------------------------------------------------------------

#[test]
fn test_edge_ur_roundtrip() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let edge = make_edge("cred-1", "foaf:Person", &alice, &alice);

    let doc = Envelope::new("Alice").add_edge_envelope(edge.clone());

    // Round-trip through UR
    let ur = doc.ur();
    let recovered = Envelope::from_ur(&ur).unwrap();
    assert!(recovered.is_equivalent_to(&doc));

    let recovered_edges = recovered.edges()?;
    assert_eq!(recovered_edges.len(), 1);
    assert!(recovered_edges[0].is_equivalent_to(&edge));

    Ok(())
}

#[test]
fn test_multiple_edges_ur_roundtrip() -> Result<(), EnvelopeError> {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");
    let edge1 = make_edge("self-desc", "foaf:Person", &alice, &alice);
    let edge2 = make_edge("knows-bob", "schema:colleague", &alice, &bob);
    let edge3 = make_edge("project", "schema:CreativeWork", &alice, &bob);

    let doc = Envelope::new("Alice")
        .add_edge_envelope(edge1)
        .add_edge_envelope(edge2)
        .add_edge_envelope(edge3);

    let ur = doc.ur();
    let recovered = Envelope::from_ur(&ur).unwrap();
    assert!(recovered.is_equivalent_to(&doc));

    let recovered_edges = recovered.edges()?;
    assert_eq!(recovered_edges.len(), 3);

    Ok(())
}

// -------------------------------------------------------------------
// Edge with extra assertions beyond the required three
// -------------------------------------------------------------------

#[test]
fn test_edge_with_additional_assertions() {
    let alice = xid_like("Alice");
    let bob = xid_like("Bob");

    // An edge with extra detail assertions beyond isA/source/target
    // should fail validation per BCR-2026-003: only the three required
    // assertions are permitted on the edge subject.
    let edge = Envelope::new("knows-bob")
        .add_assertion(known_values::IS_A, "schema:colleague")
        .add_assertion(known_values::SOURCE, alice.clone())
        .add_assertion(known_values::TARGET, bob.clone())
        .add_assertion("department", "Engineering")
        .add_assertion("since", "2024-01-15");

    let result = edge.validate_edge();
    assert!(matches!(
        result,
        Err(EnvelopeError::EdgeUnexpectedAssertion)
    ));
}

#[test]
fn test_edge_with_claim_detail_on_target() {
    // Per BCR-2026-003, claim detail goes as assertions on the *target*
    // object, not on the edge subject itself.
    let alice = xid_like("Alice");
    let target = xid_like("Bob")
        .add_assertion("department", "Engineering")
        .add_assertion("since", "2024-01-15");
    let edge = make_edge("knows-bob", "schema:colleague", &alice, &target);
    assert!(edge.validate_edge().is_ok());
}

#[test]
fn test_edge_with_claim_detail_on_source() {
    // The source XID may also carry assertions such as 'dereferenceVia'.
    let source = xid_like("Alice").add_assertion(
        known_values::DEREFERENCE_VIA,
        bc_components::URI::try_from("https://example.com/xid/").unwrap(),
    );
    let target = xid_like("Bob");
    let edge = make_edge("knows-bob", "schema:colleague", &source, &target);
    assert!(edge.validate_edge().is_ok());
}