mere-kernel 0.0.3

Portable identity, authority, and mutation kernel for the Mere browser. Renamed from graphshell-core.
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
// Copyright 2026 Mark Alan Boykin
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0

use euclid::default::Point2D;
use strum::IntoEnumIterator;
use uuid::Uuid;

use super::from::restore_persisted_payload;
use super::{ResourceSnapshotError, payload_from_persisted, persisted_edge_for_ids};
use crate::graph::*;
use crate::persistence::{
    GraphSnapshot, PersistedEdge, PersistedResourceFacet, PersistedResourceRecord,
    PersistedSemanticEdgeData, PersistedSemanticSubKind, PersistedShownResource,
};
use crate::types::GraphScope;

pub(super) fn complete_payload() -> EdgePayload {
    let mut payload = EdgePayload::new();
    for sub_kind in all_semantic_sub_kinds() {
        for (asserter, time) in [("https://alice.test/", 11), ("https://bob.test/", 22)] {
            payload.push_persisted_semantic_statement(SemanticStatement {
                statement_id: format!("claim-{sub_kind:?}-{time}"),
                predicate: predicate_iri(sub_kind).to_owned(),
                recognized_sub_kind: Some(sub_kind),
                label: Some(format!("label-{sub_kind:?}-{time}")),
                graph_scope: GraphScope::Custom("https://scope.test/claims".into()),
                provenance_iri: Some(asserter.into()),
                asserted_at_ms: Some(time),
                first_asserted_ms: Some(time),
                withdrawn: None,
                basis: Default::default(),
            });
        }
    }
    payload.push_persisted_semantic_statement(SemanticStatement {
        statement_id: "raw-claim".into(),
        predicate: "https://vocabulary.test/ns#refutes".into(),
        recognized_sub_kind: None,
        label: Some("raw label".into()),
        graph_scope: GraphScope::Source,
        provenance_iri: None,
        asserted_at_ms: Some(33),
        first_asserted_ms: Some(33),
        withdrawn: None,
        basis: Default::default(),
    });
    for sub_kind in ArrangementSubKind::iter() {
        payload.assert_relation(EdgeAssertion::Arrangement { sub_kind });
    }
    for sub_kind in ContainmentSubKind::iter() {
        payload.assert_relation(EdgeAssertion::Containment { sub_kind });
    }
    for sub_kind in ImportedSubKind::iter() {
        payload.assert_relation(EdgeAssertion::Imported { sub_kind });
    }
    for sub_kind in ProvenanceSubKind::iter() {
        payload.assert_relation(EdgeAssertion::Provenance { sub_kind });
    }
    for trigger in [
        NavigationTrigger::Unknown,
        NavigationTrigger::LinkClick,
        NavigationTrigger::Back,
        NavigationTrigger::Forward,
        NavigationTrigger::AddressBarEntry,
        NavigationTrigger::PanePromotion,
        NavigationTrigger::Programmatic,
        NavigationTrigger::Redirect,
        NavigationTrigger::ReopenSession,
        NavigationTrigger::JumpAnchor,
        NavigationTrigger::InPageSearchJump,
        NavigationTrigger::ImportedHistory,
    ] {
        payload.push_traversal(Traversal {
            timestamp_ms: 55,
            trigger,
        });
    }
    payload
}

#[test]
fn shared_payload_conversion_preserves_every_sidecar_and_separate_assertions() {
    let from = Uuid::from_u128(1);
    let to = Uuid::from_u128(2);
    let payload = complete_payload();
    let encoded = persisted_edge_for_ids(from, to, &payload);
    assert_eq!(encoded.from_node_id, from.to_string());
    assert_eq!(encoded.to_node_id, to.to_string());
    assert_eq!(encoded.families.len(), 6);
    let decoded = payload_from_persisted(&encoded);
    for sub_kind in [ArrangementSubKind::TileGroup, ArrangementSubKind::SplitPair] {
        assert!(payload.has_relation(RelationSelector::Arrangement(sub_kind)));
        assert!(!decoded.has_relation(RelationSelector::Arrangement(sub_kind)));
    }
    assert!(decoded.has_relation(RelationSelector::Arrangement(
        ArrangementSubKind::FrameMember
    )));
    let mut durable = payload.clone();
    durable.assertions.statements.retain(|statement| !matches!(
        statement.relation_kind(), RelationKind::Arrangement(kind) if kind.durability() != RelationDurability::Durable
    ));
    durable.assertions.rebuild_compat();
    assert_eq!(decoded, durable);
    assert_eq!(persisted_edge_for_ids(from, to, &decoded), encoded);
    let cites = decoded
        .semantic_statements()
        .iter()
        .filter(|claim| claim.recognized_sub_kind == Some(SemanticSubKind::Cites))
        .collect::<Vec<_>>();
    assert_eq!(cites.len(), 2);
    assert_ne!(cites[0].statement_id, cites[1].statement_id);
    assert_ne!(cites[0].provenance_iri, cites[1].provenance_iri);
    assert_ne!(cites[0].asserted_at_ms, cites[1].asserted_at_ms);
    assert!(
        decoded
            .semantic_statements()
            .iter()
            .any(|claim| claim.statement_id == "raw-claim" && claim.provenance_iri.is_none())
    );
    assert!(
        payload_from_persisted(&persisted_edge_for_ids(from, to, &EdgePayload::new())).is_empty()
    );
    let mut merged = EdgePayload::new();
    assert!(restore_persisted_payload(&mut merged, &encoded) > 0);
    assert_eq!(restore_persisted_payload(&mut merged, &encoded), 0);
    assert_eq!(merged, durable);
}

#[test]
fn surface_restore_retains_revision_bumps_and_normalization_boundary() {
    let mut graph = Graph::new();
    let from = graph.add_node("https://a.test/".into(), Point2D::new(0.0, 0.0));
    let to = graph.add_node("https://b.test/".into(), Point2D::new(0.0, 0.0));
    let from_id = graph.get_node(from).unwrap().id;
    let to_id = graph.get_node(to).unwrap().id;
    let encoded = persisted_edge_for_ids(from_id, to_id, &complete_payload());
    let mut expected = EdgePayload::new();
    let changes = restore_persisted_payload(&mut expected, &encoded);
    let revision = graph.revision();
    graph.restore_persisted_edge(from, to, &encoded);
    assert_eq!(graph.revision(), revision + changes as u64);
    assert_eq!(
        graph.get_edge(graph.find_edge_key(from, to).unwrap()),
        Some(&expected)
    );
    graph.restore_persisted_edge(from, to, &encoded);
    assert_eq!(graph.revision(), revision + changes as u64);
    let restored = Graph::from_snapshot(&graph.to_snapshot());
    let claims = restored
        .get_edge(restored.find_edge_key(from, to).unwrap())
        .unwrap()
        .semantic_statements();
    assert!(claims.iter().any(|claim| claim.statement_id == "raw-claim"

        && claim.provenance_iri.as_deref() == Some(edge_data::UNKNOWN_LEGACY_ASSERTER_IRI)));
    assert!(
        claims
            .iter()
            .any(|claim| claim.provenance_iri.as_deref() == Some("https://alice.test/"))
    );
}

pub(super) fn resource_snapshot_fixture() -> (GraphSnapshot, Uuid, Uuid, Uuid) {
    let mut graph = Graph::new();
    let surface = graph.add_node("https://surface.test/".into(), Point2D::new(0.0, 0.0));
    let surface_id = graph.get_node(surface).unwrap().id;
    let first = PersistedResourceRecord {
        canonical_iri: "https://vocabulary.test/ns#Topic".into(),
        facets: vec![PersistedResourceFacet {
            facet: "foreign.review".into(),
            value_json: r#"{"review":"suggested","nested":[1,true,null]}"#.into(),
        }],
    };
    let second = PersistedResourceRecord {
        canonical_iri: "https://target.test/item?id=7".into(),
        facets: Vec::new(),
    };
    let from = chartulary::resource_id_from_canonical_iri(&first.canonical_iri);
    let to = chartulary::resource_id_from_canonical_iri(&second.canonical_iri);
    assert_ne!(from, chartulary::resource_id(&first.canonical_iri));
    assert!(graph.set_resource_record(from, Some(first)));
    assert!(graph.set_resource_record(to, Some(second)));
    let mut payload = complete_payload();
    for claim in &mut payload.assertions.statements {
        claim.normalize_legacy_asserter();
    }
    let first_edge = persisted_edge_for_ids(from, to, &payload);
    let mut other = EdgePayload::new();
    other.assert_relation(EdgeAssertion::Provenance {
        sub_kind: ProvenanceSubKind::ExtractedFrom,
    });
    let second_edge = persisted_edge_for_ids(from, to, &other);
    assert!(graph.set_resource_edges_between(from, to, &[first_edge, second_edge]));
    assert!(graph.set_shown_resource(surface_id, Some(from)));
    (graph.to_snapshot(), surface_id, from, to)
}

fn assert_resource_snapshot_restores(
    snapshot: &GraphSnapshot,
    surface: Uuid,
    from: Uuid,
    to: Uuid,
) {
    let mut graph = Graph::from_snapshot(snapshot);
    assert_eq!(graph.node_count(), 1);
    assert_eq!(graph.resources.node_count(), 2);
    assert_eq!(graph.resources.edge_count(), 2);
    assert_eq!(graph.shown_resources.get(&surface), Some(&from));
    assert_eq!(
        graph.resource_record(from).unwrap().canonical_iri,
        "https://vocabulary.test/ns#Topic"
    );
    assert_eq!(
        graph.resource_record(from).unwrap().facets,
        snapshot.resources[0].facets
    );
    assert_eq!(
        graph.persisted_resource_edges_between(from, to),
        snapshot.resource_edges
    );
    assert!(graph.persisted_resource_edges_between(to, from).is_empty());
    assert!(graph.resource_record(Uuid::from_u128(123)).is_none());
    let saved = graph.to_snapshot();
    assert_eq!(saved.resources, snapshot.resources);
    assert_eq!(saved.resource_edges, snapshot.resource_edges);
    assert_eq!(saved.shown_resources, snapshot.shown_resources);
    let revision = graph.revision();
    for id in [from, to] {
        let record = graph.resource_record(id).unwrap();
        assert!(!graph.set_resource_record(id, Some(record)));
    }
    let edges = graph.persisted_resource_edges_between(from, to);
    assert!(!graph.set_resource_edges_between(from, to, &edges));
    assert!(!graph.set_shown_resource(surface, Some(from)));
    assert_eq!(graph.revision(), revision);
}

#[test]
fn resource_snapshot_json_and_rkyv_preserve_records_pairs_and_shown_resources() {
    let (snapshot, surface, from, to) = resource_snapshot_fixture();
    let json = serde_json::to_string(&snapshot).unwrap();
    let decoded: GraphSnapshot = serde_json::from_str(&json).unwrap();
    assert_resource_snapshot_restores(&decoded, surface, from, to);
    let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&snapshot).unwrap();
    let decoded = rkyv::from_bytes::<GraphSnapshot, rkyv::rancor::Error>(&bytes).unwrap();
    assert_resource_snapshot_restores(&decoded, surface, from, to);
}

#[test]
fn resource_snapshot_columns_and_assertion_metadata_are_postcard_compatible() {
    type Columns = (
        Vec<PersistedResourceRecord>,
        Vec<PersistedEdge>,
        Vec<PersistedShownResource>,
    );
    let (snapshot, surface, from, to) = resource_snapshot_fixture();
    let columns = (
        snapshot.resources.clone(),
        snapshot.resource_edges.clone(),
        snapshot.shown_resources.clone(),
    );
    let bytes = postcard::to_allocvec(&columns).unwrap();
    let decoded: Columns = postcard::from_bytes(&bytes).unwrap();
    assert_eq!(decoded, columns);
    let rebuilt = GraphSnapshot {
        resources: decoded.0,
        resource_edges: decoded.1,
        shown_resources: decoded.2,
        ..snapshot
    };
    assert_resource_snapshot_restores(&rebuilt, surface, from, to);
}

#[test]
fn legacy_json_keeps_surfaces_and_has_no_resource_auto_population() {
    let (snapshot, surface, from, to) = resource_snapshot_fixture();
    assert_resource_snapshot_restores(&snapshot, surface, from, to);
    let mut legacy = serde_json::to_value(&snapshot).unwrap();
    let object = legacy.as_object_mut().unwrap();
    for column in ["resources", "resource_edges", "shown_resources"] {
        assert!(object.remove(column).is_some());
    }
    let decoded: GraphSnapshot = serde_json::from_value(legacy).unwrap();
    assert!(decoded.resources.is_empty());
    assert!(decoded.resource_edges.is_empty());
    assert!(decoded.shown_resources.is_empty());
    let graph = Graph::from_snapshot(&decoded);
    assert!(graph.get_node_key_by_id(surface).is_some());
    assert_eq!(graph.node_count(), 1);
    assert_eq!(graph.resources.node_count(), 0);
    assert_eq!(graph.resources.edge_count(), 0);
    assert!(graph.shown_resources.is_empty());
}

#[test]
fn resource_snapshot_normalizes_only_missing_legacy_attribution() {
    let (mut snapshot, _, from, to) = resource_snapshot_fixture();
    // Exercise the schema-0 semantic sidecar decoder explicitly. Canonical
    // snapshots carry all-family assertion records and do not use this field.
    snapshot.assertion_schema = 0;
    for edge in &mut snapshot.resource_edges {
        let statements: Vec<_> = edge
            .assertions
            .take()
            .unwrap_or_default()
            .into_iter()
            .filter(|statement| {
                statement.recognized_sub_kind.is_some()
                    || statement.predicate == "https://vocabulary.test/ns#refutes"
            })
            .collect();
        if statements.is_empty() {
            continue;
        }
        edge.semantic = Some(PersistedSemanticEdgeData {
            sub_kinds: all_semantic_sub_kinds()
                .map(|kind| match kind {
                    SemanticSubKind::Hyperlink => PersistedSemanticSubKind::Hyperlink,
                    SemanticSubKind::UserGrouped => PersistedSemanticSubKind::UserGrouped,
                    SemanticSubKind::AgentDerived => PersistedSemanticSubKind::AgentDerived,
                    SemanticSubKind::Cites => PersistedSemanticSubKind::Cites,
                    SemanticSubKind::Quotes => PersistedSemanticSubKind::Quotes,
                    SemanticSubKind::Summarizes => PersistedSemanticSubKind::Summarizes,
                    SemanticSubKind::Elaborates => PersistedSemanticSubKind::Elaborates,
                    SemanticSubKind::ExampleOf => PersistedSemanticSubKind::ExampleOf,
                    SemanticSubKind::Supports => PersistedSemanticSubKind::Supports,
                    SemanticSubKind::Contradicts => PersistedSemanticSubKind::Contradicts,
                    SemanticSubKind::Questions => PersistedSemanticSubKind::Questions,
                    SemanticSubKind::SameEntityAs => PersistedSemanticSubKind::SameEntityAs,
                    SemanticSubKind::DuplicateOf => PersistedSemanticSubKind::DuplicateOf,
                    SemanticSubKind::CanonicalMirrorOf => {
                        PersistedSemanticSubKind::CanonicalMirrorOf
                    },
                    SemanticSubKind::DependsOn => PersistedSemanticSubKind::DependsOn,
                    SemanticSubKind::Blocks => PersistedSemanticSubKind::Blocks,
                    SemanticSubKind::NextStep => PersistedSemanticSubKind::NextStep,
                })
                .collect(),
            statements,
            ..Default::default()
        });
    }
    let raw = snapshot
        .resource_edges
        .iter_mut()
        .filter_map(|edge| edge.semantic.as_mut())
        .flat_map(|semantic| semantic.statements.iter_mut())
        .find(|statement| statement.statement_id == "raw-claim")
        .unwrap();
    raw.provenance_iri = None;
    let graph = Graph::from_snapshot(&snapshot);
    let claims = graph
        .persisted_resource_edges_between(from, to)
        .into_iter()
        .flat_map(|edge| edge.retained_assertions().to_vec())
        .collect::<Vec<_>>();
    assert!(claims.iter().any(|claim| claim.statement_id == "raw-claim"

        && claim.provenance_iri.as_deref() == Some(edge_data::UNKNOWN_LEGACY_ASSERTER_IRI)));
    assert!(
        claims
            .iter()
            .any(|claim| claim.provenance_iri.as_deref() == Some("https://alice.test/"))
    );
    assert_eq!(claims.len(), all_semantic_sub_kinds().count() * 2 + 1);
}

fn checked_rejection(snapshot: &GraphSnapshot) -> ResourceSnapshotError {
    let before = serde_json::to_value(snapshot).unwrap();
    let error = match Graph::try_from_snapshot(snapshot) {
        Err(error) => error,
        Ok(_) => panic!("invalid resource columns accepted"),
    };
    assert_eq!(serde_json::to_value(snapshot).unwrap(), before);
    assert!(!error.to_string().is_empty());
    error
}

#[test]
fn checked_snapshot_recorded_loader_preserves_same_host_resource_containment_and_surface_handles() {
    let mut graph = Graph::new();
    let parent = graph.add_node_with_id(
        Uuid::from_u128(1),
        "https://a.test/".into(),
        Point2D::zero(),
    );
    let child = graph.add_node_with_id(
        Uuid::from_u128(2),
        "https://a.test/child".into(),
        Point2D::zero(),
    );
    let parent_resource = ResourceNode::new("https://a.test/");
    let child_resource = ResourceNode::new("https://a.test/child");
    for resource in [&parent_resource, &child_resource] {
        assert!(graph.set_resource_record(
            resource.id(),
            Some(PersistedResourceRecord {
                canonical_iri: resource.canonical_iri().into(),
                facets: vec![],
            })
        ));
    }
    assert!(graph.set_shown_resource(Uuid::from_u128(1), Some(parent_resource.id())));
    assert!(graph.set_shown_resource(Uuid::from_u128(2), Some(child_resource.id())));
    let mut resource_relation = EdgePayload::new();
    for sub_kind in [ContainmentSubKind::Domain, ContainmentSubKind::UrlPath] {
        resource_relation.assert_relation(EdgeAssertion::Containment { sub_kind });
    }
    assert!(graph.set_resource_edges_between(
        child_resource.id(),
        parent_resource.id(),
        &[persisted_edge_for_ids(
            child_resource.id(),
            parent_resource.id(),
            &resource_relation
        ),]
    ));
    for (id, author, time) in [
        ("held-a", "https://alice.test/", 11),
        ("held-b", "https://bob.test/", 22),
    ] {
        let mut payload = EdgePayload::new();
        payload.push_persisted_semantic_statement(SemanticStatement {
            statement_id: id.into(),
            predicate: predicate_iri(SemanticSubKind::UserGrouped).into(),
            recognized_sub_kind: Some(SemanticSubKind::UserGrouped),
            label: Some(id.into()),
            graph_scope: GraphScope::User,
            provenance_iri: Some(author.into()),
            asserted_at_ms: Some(time),
            first_asserted_ms: Some(time),
            withdrawn: None,
            basis: Default::default(),
        });
        graph.inner.connect(child, parent, payload);
    }
    let snapshot = graph.to_snapshot();
    let recorded = Graph::try_from_recorded_snapshot(&snapshot).unwrap();
    assert_eq!(
        recorded.to_snapshot().resource_edges,
        snapshot.resource_edges
    );
    assert_eq!(
        recorded.to_snapshot().edges,
        snapshot.edges,
        "parallel held surface records remain exact"
    );
    assert_eq!(recorded.inner.edge_count(), 2);
    assert!(
        recorded
            .relations()
            .all(|row| !matches!(row.kind, RelationKind::Containment(_))),
        "recorded resource containment does not acquire Surface authority"
    );
    let legacy = Graph::try_from_snapshot(&snapshot).unwrap();
    for kind in [ContainmentSubKind::Domain, ContainmentSubKind::UrlPath] {
        assert!(
            legacy
                .relations()
                .any(|row| row.kind == RelationKind::Containment(kind)),
            "the existing loader still derives legacy containment"
        );
    }
    assert_eq!(legacy.to_snapshot().resource_edges, snapshot.resource_edges);
    let mut invalid = snapshot.clone();
    invalid.resource_edges[0].from_node_id = Uuid::from_u128(99).to_string();
    let before = serde_json::to_value(&invalid).unwrap();
    let legacy_error = Graph::try_from_snapshot(&invalid)
        .err()
        .expect("legacy checked boundary rejects invalid resource");
    let recorded_error = Graph::try_from_recorded_snapshot(&invalid)
        .err()
        .expect("recorded checked boundary rejects invalid resource");
    assert_eq!(legacy_error, recorded_error);
    assert!(matches!(
        recorded_error,
        ResourceSnapshotError::MissingResource { .. }
    ));
    assert_eq!(serde_json::to_value(&invalid).unwrap(), before);
    assert!(Graph::try_from_recorded_snapshot(&snapshot).is_ok());
}

#[test]
fn checked_snapshot_compatibility_wrappers_stop_before_partial_materialization() {
    let (valid, surface, from, to) = resource_snapshot_fixture();
    assert_resource_snapshot_restores(&valid, surface, from, to);
    let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&valid).unwrap();
    let graph = rkyv::from_bytes::<Graph, rkyv::rancor::Error>(&bytes).unwrap();
    assert_eq!(graph.to_snapshot().resource_edges, valid.resource_edges);

    let mut invalid = valid.clone();
    let mut conflicting = invalid.resource_edges[0].clone();
    conflicting.from_node_id = to.to_string();
    conflicting.to_node_id = from.to_string();
    invalid.resource_edges.push(conflicting);
    let error = checked_rejection(&invalid).to_string();
    let stopped = std::panic::catch_unwind(|| Graph::from_snapshot(&invalid));
    let panic = match stopped {
        Err(panic) => panic,
        Ok(_) => panic!("compatibility wrapper returned partial graph"),
    };
    let message = panic.downcast_ref::<String>().unwrap();
    assert!(message.contains(&error));
    let bytes = rkyv::to_bytes::<rkyv::rancor::Error>(&invalid).unwrap();
    assert!(
        std::panic::catch_unwind(|| { rkyv::from_bytes::<Graph, rkyv::rancor::Error>(&bytes) })
            .is_err()
    );
    assert!(Graph::try_from_snapshot(&valid).is_ok());
}

#[test]
fn checked_resource_load_accepts_identical_duplicates_and_legacy_empty_columns() {
    let (mut snapshot, surface, from, to) = resource_snapshot_fixture();
    assert!(Graph::try_from_snapshot(&snapshot).is_ok());
    snapshot.resources.push(snapshot.resources[0].clone());
    snapshot
        .shown_resources
        .push(snapshot.shown_resources[0].clone());
    let graph = Graph::try_from_snapshot(&snapshot).unwrap();
    assert_eq!(graph.resources.node_count(), 2);
    assert_eq!(graph.resources.edge_count(), 2);
    assert_eq!(graph.shown_resources.get(&surface), Some(&from));
    assert_eq!(
        graph.persisted_resource_edges_between(from, to),
        snapshot.resource_edges
    );
    snapshot.resources.clear();
    snapshot.resource_edges.clear();
    snapshot.shown_resources.clear();
    let graph = Graph::try_from_snapshot(&snapshot).unwrap();
    assert_eq!(graph.node_count(), 1);
    assert_eq!(graph.resources.node_count(), 0);
    let mut json = serde_json::to_value(&snapshot).unwrap();
    for column in ["resources", "resource_edges", "shown_resources"] {
        json.as_object_mut().unwrap().remove(column);
    }
    let legacy = serde_json::from_value::<GraphSnapshot>(json).unwrap();
    assert!(Graph::try_from_snapshot(&legacy).is_ok());
}

#[test]
fn checked_resource_load_rejects_invalid_facets_and_conflicting_records() {
    let (base, _, _, _) = resource_snapshot_fixture();
    assert!(Graph::try_from_snapshot(&base).is_ok());
    let mut invalid = base.clone();
    invalid.resources[0].facets[0].value_json = "{".into();
    assert!(matches!(
        checked_rejection(&invalid),
        ResourceSnapshotError::InvalidFacet { .. }
    ));
    let mut duplicate = base.clone();
    let facet = duplicate.resources[0].facets[0].clone();
    duplicate.resources[0].facets.push(facet);
    assert!(matches!(
        checked_rejection(&duplicate),
        ResourceSnapshotError::DuplicateFacet { .. }
    ));
    let mut conflict = base.clone();
    let mut record = conflict.resources[0].clone();
    record.facets[0].value_json = "false".into();
    conflict.resources.push(record);
    assert!(matches!(
        checked_rejection(&conflict),
        ResourceSnapshotError::ConflictingResource { .. }
    ));
}

#[test]
fn checked_resource_load_rejects_invalid_endpoints_and_unhandled_semantic_data() {
    let (base, _, from, to) = resource_snapshot_fixture();
    assert!(Graph::try_from_snapshot(&base).is_ok());
    let mut invalid = base.clone();
    invalid.resource_edges[0].from_node_id = "invalid".into();
    assert!(matches!(
        checked_rejection(&invalid),
        ResourceSnapshotError::InvalidUuid { .. }
    ));
    let mut noncanonical = base.clone();
    noncanonical.resource_edges[0].from_node_id = from.to_string().to_uppercase();
    assert_ne!(
        noncanonical.resource_edges[0].from_node_id,
        from.to_string()
    );
    assert!(matches!(
        checked_rejection(&noncanonical),
        ResourceSnapshotError::NonCanonicalUuid { .. }
    ));
    let mut unknown = base.clone();
    unknown.resource_edges[0].to_node_id = Uuid::from_u128(123).to_string();
    assert!(matches!(
        checked_rejection(&unknown),
        ResourceSnapshotError::MissingResource { .. }
    ));
    let mut aggregate = base.clone();
    aggregate.assertion_schema = 0;
    let edge = &mut aggregate.resource_edges[0];
    edge.assertions = None;
    edge.semantic = Some(PersistedSemanticEdgeData {
        sub_kinds: vec![PersistedSemanticSubKind::Cites],
        ..Default::default()
    });
    assert!(matches!(
        checked_rejection(&aggregate),
        ResourceSnapshotError::LegacySemanticAggregate { .. }
    ));
    for handle in ["", "  ", "opaque\nhandle"] {
        let mut opaque = base.clone();
        opaque.resource_edges[0].retained_assertions_mut()[0].statement_id = handle.into();
        let expected = opaque.resource_edges[0].retained_assertions()[0].clone();
        let graph = Graph::try_from_snapshot(&opaque).unwrap();
        assert_eq!(
            graph.persisted_resource_edges_between(from, to)[0].retained_assertions()[0],
            expected
        );
    }
    let mut empty_sidecar = base.clone();
    empty_sidecar.resource_edges[1].assertions = Some(vec![]);
    let graph = Graph::try_from_snapshot(&empty_sidecar).unwrap();
    assert_eq!(graph.resources.edge_count(), 2);
    assert_eq!(
        graph.persisted_resource_edges_between(from, to)[1].provenance,
        base.resource_edges[1].provenance
    );
}

#[test]
fn checked_resource_load_rejects_reused_handles_and_conflicting_shown_resources() {
    let (base, surface, from, to) = resource_snapshot_fixture();
    assert!(Graph::try_from_snapshot(&base).is_ok());
    let mut changed_metadata = base.clone();
    let mut statement = changed_metadata.resource_edges[0].retained_assertions()[0].clone();
    statement.asserted_at_ms = Some(999);
    changed_metadata.resource_edges[0]
        .assertions
        .as_mut()
        .unwrap()
        .push(statement);
    assert!(matches!(
        checked_rejection(&changed_metadata),
        ResourceSnapshotError::RepeatedAssertionHandle(_)
    ));
    let mut changed_pair = base.clone();
    let mut reverse = changed_pair.resource_edges[0].clone();
    reverse.from_node_id = to.to_string();
    reverse.to_node_id = from.to_string();
    changed_pair.resource_edges.push(reverse);
    assert!(matches!(
        checked_rejection(&changed_pair),
        ResourceSnapshotError::RepeatedAssertionHandle(_)
    ));
    let mut active_surface = base.clone();
    let mut surface_edge = active_surface.resource_edges[0].clone();
    surface_edge.from_node_id = surface.to_string();
    surface_edge.to_node_id = surface.to_string();
    active_surface.edges.push(surface_edge);
    assert!(matches!(
        checked_rejection(&active_surface),
        ResourceSnapshotError::RepeatedAssertionHandle(_)
    ));
    let mut orphan_surface = active_surface.clone();
    orphan_surface.edges[0].to_node_id = Uuid::from_u128(123).to_string();
    assert!(matches!(
        checked_rejection(&orphan_surface),
        ResourceSnapshotError::MissingSurface { .. }
    ));
    // Schema 0 retains the historical permissive orphan handling.
    orphan_surface.assertion_schema = 0;
    let restored = Graph::try_from_snapshot(&orphan_surface).unwrap();
    assert_eq!(
        restored.persisted_resource_edges_between(from, to),
        base.resource_edges
    );
    assert_eq!(restored.edge_count(), 0);
    assert_eq!(
        orphan_surface.edges[0].assertions,
        active_surface.edges[0].assertions
    );
    let mut uppercase_surface = active_surface.clone();
    uppercase_surface.edges[0].from_node_id = surface.to_string().to_uppercase();
    assert!(matches!(
        checked_rejection(&uppercase_surface),
        ResourceSnapshotError::NonCanonicalUuid { .. }
    ));
    let mut shown_conflict = base.clone();
    shown_conflict.shown_resources.push(PersistedShownResource {
        surface_id: surface.to_string(),
        resource_id: to.to_string(),
    });
    assert!(matches!(
        checked_rejection(&shown_conflict),
        ResourceSnapshotError::ConflictingShownResource { .. }
    ));
    let mut missing_surface = base.clone();
    missing_surface.shown_resources[0].surface_id = Uuid::from_u128(123).to_string();
    assert!(matches!(
        checked_rejection(&missing_surface),
        ResourceSnapshotError::MissingSurface { .. }
    ));
    let mut missing_resource = base.clone();
    missing_resource.shown_resources[0].resource_id = Uuid::from_u128(123).to_string();
    assert!(matches!(
        checked_rejection(&missing_resource),
        ResourceSnapshotError::MissingResource { .. }
    ));
    let mut invalid_mapping = base.clone();
    invalid_mapping.shown_resources[0].resource_id = "invalid".into();
    assert!(matches!(
        checked_rejection(&invalid_mapping),
        ResourceSnapshotError::InvalidUuid { .. }
    ));
}

/// Q013: the saved node address owns the current address on load; a stale
/// navigation cursor must not undo a successful `SetNodeUrl`.
#[test]
fn recorded_load_keeps_a_set_node_url_over_the_navigation_cursor() {
    use crate::graph::apply::{GraphDelta, add_node, apply_graph_delta};
    let mut graph = Graph::new();
    let id = Uuid::from_u128(1);
    let key = add_node(&mut graph, Some(id), "https://audit.test/old".into(), Default::default());
    apply_graph_delta(
        &mut graph,
        GraphDelta::NavigateNode { key, url: "https://audit.test/old".into() },
    );
    apply_graph_delta(
        &mut graph,
        GraphDelta::SetNodeUrl { key, new_url: "https://audit.test/new".into() },
    );
    let restored = Graph::try_from_recorded_snapshot(&graph.to_snapshot()).unwrap();
    let key = restored.get_node_key_by_id(id).unwrap();
    assert_eq!(restored.get_node(key).unwrap().url(), "https://audit.test/new");
    assert_eq!(restored.get_nodes_by_url("https://audit.test/new"), vec![key]);
    assert!(restored.get_nodes_by_url("https://audit.test/old").is_empty());
}

/// Q015: every index in the navigation snapshot names a row of its own column,
/// so each checked load refuses one that names none instead of panicking when
/// Stemma hydrates it.
#[test]
fn checked_loads_refuse_a_navigation_index_that_names_no_row() {
    use crate::graph::apply::add_node;
    use chartulary::stemma::{StemmaSnapshot, TransitionKind};
    type Nav = StemmaSnapshot<String, String, NodeHistoryOwner, ()>;
    const BAD: usize = 999_999;

    let (a, b) = (Uuid::from_u128(1), Uuid::from_u128(2));
    let mut nav = SharedNavigationMemory::empty();
    nav.record_visit(a, "https://audit.test/a", TransitionKind::UrlTyped, 1);
    nav.record_visit(a, "https://audit.test/b", TransitionKind::LinkClick, 2);
    nav.back(a, 3);
    nav.spawn(b, a);
    nav.record_visit(b, "https://audit.test/c", TransitionKind::UrlTyped, 4);
    let mut graph = Graph::new();
    for (id, url) in [(a, "https://audit.test/a"), (b, "https://audit.test/c")] {
        add_node(&mut graph, Some(id), url.into(), Default::default());
    }
    let mut valid = graph.to_snapshot();
    valid.navigation = nav;
    assert!(Graph::try_from_snapshot(&valid).is_ok());
    assert!(Graph::try_from_recorded_snapshot(&valid).is_ok());

    let corruptions: [(&str, fn(&mut Nav)); 10] = [
        ("visit entry", |n| n.visits[0].entry = BAD),
        ("visit parent", |n| n.visits[1].parent = Some(BAD)),
        ("visit child", |n| n.visits[0].children[0] = BAD),
        ("binding owner", |n| {
            n.visits
                .iter_mut()
                .flat_map(|v| &mut v.bindings)
                .next()
                .unwrap()
                .owner = BAD
        }),
        ("binding forward child", |n| {
            let binding = n
                .visits
                .iter_mut()
                .flat_map(|v| &mut v.bindings)
                .next()
                .unwrap();
            binding.forward_child = Some(BAD)
        }),
        ("owner origin", |n| n.owners[0].origin = Some(BAD)),
        ("owner current", |n| n.owners[0].current = Some(BAD)),
        ("owner creator", |n| n.owners[1].creator = Some(BAD)),
        ("owner pending origin parent", |n| {
            n.owners[1].pending_origin_parent = Some(BAD)
        }),
        ("owner owned visit", |n| n.owners[0].owned_visits[0] = BAD),
    ];
    for (what, corrupt) in corruptions {
        let mut snapshot = valid.clone();
        let mut nav = snapshot.navigation.snapshot().clone();
        corrupt(&mut nav);
        snapshot.navigation = SharedNavigationMemory::from_snapshot(nav);
        let loads = [
            Graph::try_from_snapshot(&snapshot),
            Graph::try_from_recorded_snapshot(&snapshot),
        ];
        for load in loads {
            assert!(
                matches!(load, Err(ResourceSnapshotError::InvalidNavigation(_))),
                "{what}"
            );
        }
    }
}