use super::*;
#[test]
fn test_disjoint_places_glue() {
let sheaf = Sheaf::new();
let s1 = Section::new(
OpenSet::from_places([Place::new("a")]),
Marking::new().with_token("a", "hash-a"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("b")]),
Marking::new().with_token("b", "hash-b"),
);
let global = sheaf.gluing_check(&[s1, s2], &[OpenSet::from_places([Place::new("a")]), OpenSet::from_places([Place::new("b")])]).unwrap();
assert_eq!(global.open_set.places.len(), 2);
}
#[test]
fn test_overlapping_matching_restrictions_glue() {
let sheaf = Sheaf::new();
let _shared = OpenSet::from_places([Place::new("shared")]);
let s1 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
Marking::new().with_token("shared", "hash-s").with_token("a", "hash-a"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
Marking::new().with_token("shared", "hash-s").with_token("b", "hash-b"),
);
let global = sheaf.gluing_check(&[s1, s2], &[OpenSet::from_places([Place::new("shared"), Place::new("a")]), OpenSet::from_places([Place::new("shared"), Place::new("b")])]).unwrap();
assert!(global.open_set.places.contains(&Place::new("shared")));
}
#[test]
fn test_overlapping_mismatched_restrictions_fail() {
let sheaf = Sheaf::new();
let s1 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
Marking::new().with_token("shared", "hash-s1"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
Marking::new().with_token("shared", "hash-s2"),
);
let result = sheaf.gluing_check(&[s1, s2], &[OpenSet::from_places([Place::new("shared"), Place::new("a")]), OpenSet::from_places([Place::new("shared"), Place::new("b")])]);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("COHOMOLOGICAL OBSTRUCTION"));
}
#[test]
fn test_three_section_cover_glue() {
let sheaf = Sheaf::new();
let cover = [
OpenSet::from_places([Place::new("a"), Place::new("b")]),
OpenSet::from_places([Place::new("b"), Place::new("c")]),
OpenSet::from_places([Place::new("c"), Place::new("d")]),
];
let s1 = Section::new(cover[0].clone(), Marking::new().with_token("a", "h").with_token("b", "h"));
let s2 = Section::new(cover[1].clone(), Marking::new().with_token("b", "h").with_token("c", "h"));
let s3 = Section::new(cover[2].clone(), Marking::new().with_token("c", "h").with_token("d", "h"));
let global = sheaf.gluing_check(&[s1, s2, s3], &cover).unwrap();
assert_eq!(global.open_set.places.len(), 4);
}
#[test]
fn test_three_sections_one_conflict() {
let sheaf = Sheaf::new();
let cover = [
OpenSet::from_places([Place::new("a"), Place::new("b")]),
OpenSet::from_places([Place::new("b"), Place::new("c")]),
OpenSet::from_places([Place::new("c"), Place::new("d")]),
];
let s1 = Section::new(cover[0].clone(), Marking::new().with_token("a", "h").with_token("b", "h1"));
let s2 = Section::new(cover[1].clone(), Marking::new().with_token("b", "h2").with_token("c", "h"));
let s3 = Section::new(cover[2].clone(), Marking::new().with_token("c", "h").with_token("d", "h"));
let result = sheaf.gluing_check(&[s1, s2, s3], &cover);
assert!(result.is_err());
}
#[test]
fn test_open_set_operations() {
let a = OpenSet::from_places([Place::new("x"), Place::new("y")]);
let b = OpenSet::from_places([Place::new("y"), Place::new("z")]);
assert!(!a.is_disjoint(&b));
let inter = a.intersection(&b);
assert_eq!(inter.places, [Place::new("y")].iter().cloned().collect());
assert!(OpenSet::from_places([Place::new("y")]).is_subset(&a));
}
#[test]
fn test_identical_sections_produce_identical_merkle_roots() {
let s1 = Section::new(
OpenSet::from_places([Place::new("a"), Place::new("b")]),
Marking::new().with_token("a", "h1").with_token("b", "h2"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("a"), Place::new("b")]),
Marking::new().with_token("a", "h1").with_token("b", "h2"),
);
let m1 = section_to_merkle(&s1);
let m2 = section_to_merkle(&s2);
assert_eq!(m1.hash, m2.hash);
}
#[test]
fn test_different_sections_produce_different_merkle_roots() {
let s1 = Section::new(
OpenSet::from_places([Place::new("a"), Place::new("b")]),
Marking::new().with_token("a", "h1").with_token("b", "h2"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("a"), Place::new("b")]),
Marking::new().with_token("a", "h1").with_token("b", "h3"),
);
let m1 = section_to_merkle(&s1);
let m2 = section_to_merkle(&s2);
assert_ne!(m1.hash, m2.hash);
}
#[test]
fn test_merkle_sub_root_detects_same_conflicts() {
let sheaf = Sheaf::new();
let s1 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
Marking::new().with_token("shared", "hash-s1"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
Marking::new().with_token("shared", "hash-s2"),
);
let cover = [
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
];
let result = sheaf.gluing_check(&[s1, s2], &cover);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("COHOMOLOGICAL OBSTRUCTION"));
}
#[test]
fn test_merkle_sub_root_matching_sections_pass() {
let sheaf = Sheaf::new();
let s1 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
Marking::new().with_token("shared", "hash-s").with_token("a", "hash-a"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
Marking::new().with_token("shared", "hash-s").with_token("b", "hash-b"),
);
let cover = [
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
];
let result = sheaf.gluing_check(&[s1, s2], &cover);
assert!(result.is_ok());
}
#[test]
fn test_conflict_report_from_gluing_failure() {
let sheaf = Sheaf::new();
let s1 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
Marking::new().with_token("shared", "hash-s1"),
);
let s2 = Section::new(
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
Marking::new().with_token("shared", "hash-s2"),
);
let cover = [
OpenSet::from_places([Place::new("shared"), Place::new("a")]),
OpenSet::from_places([Place::new("shared"), Place::new("b")]),
];
let err = sheaf.gluing_check(&[s1, s2], &cover).unwrap_err();
let report = CohomologyConflictReport::from_error(
&err,
vec!["did:key:agent1".to_string(), "did:key:agent2".to_string()],
);
assert!(report.is_some());
let r = report.unwrap();
assert_eq!(r.expected_marking.by_place.get("shared"), Some(&vec!["hash-s1".to_string()]));
assert_eq!(r.actual_marking.by_place.get("shared"), Some(&vec!["hash-s2".to_string()]));
assert_eq!(r.cohomology_class, "[delta_s]");
assert_eq!(r.agent_dids.len(), 2);
}
#[test]
fn test_conflict_report_yaml_serialization() {
let report = CohomologyConflictReport {
conflicting_places: (Place::new("a"), Place::new("b")),
intersection: OpenSet::from_places([Place::new("shared")]),
expected_marking: Marking::new().with_token("shared", "h1"),
actual_marking: Marking::new().with_token("shared", "h2"),
cohomology_class: "[delta_s]".to_string(),
timestamp: "2026-01-01T00:00:00Z".to_string(),
agent_dids: vec!["did:key:a1".to_string()],
};
let yaml = report.to_yaml();
assert!(yaml.contains("cohomology_class"));
assert!(yaml.contains("[delta_s]"));
assert!(yaml.contains("conflicting_places"));
let hash1 = report.content_hash();
let hash2 = report.content_hash();
assert_eq!(hash1, hash2);
}
#[test]
fn test_conflict_report_deterministic_hash() {
let make_report = || CohomologyConflictReport {
conflicting_places: (Place::new("x"), Place::new("y")),
intersection: OpenSet::from_places([Place::new("z")]),
expected_marking: Marking::new().with_token("z", "h1"),
actual_marking: Marking::new().with_token("z", "h2"),
cohomology_class: "[delta_s]".to_string(),
timestamp: "2026-01-01T00:00:00Z".to_string(),
agent_dids: vec!["did:key:a".to_string()],
};
let r1 = make_report();
let r2 = make_report();
assert_eq!(r1.content_hash(), r2.content_hash());
}
#[test]
fn test_benchmark_gluing_check_100_places_200_overlaps() {
let sheaf = Sheaf::new();
let mut sections = Vec::new();
let mut cover = Vec::new();
for i in 0..100 {
let p1 = format!("p{}", i);
let p2 = format!("p{}", (i + 1) % 100);
let os = OpenSet::from_places([Place::new(&p1), Place::new(&p2)]);
let marking = Marking::new()
.with_token(&p1, &format!("h{}", i))
.with_token(&p2, &format!("h{}", i));
sections.push(Section::new(os.clone(), marking));
cover.push(os);
}
let start = std::time::Instant::now();
let result = sheaf.gluing_check(§ions, &cover);
let elapsed = start.elapsed();
let _ = result;
assert!(
elapsed.as_millis() < 100,
"gluing_check took {}ms, expected < 100ms",
elapsed.as_millis()
);
}
#[cfg(feature = "native")]
#[test]
fn test_store_and_load_section_merkle() {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.execute(
"CREATE TABLE card (hash TEXT PRIMARY KEY, content TEXT, g_time TEXT)",
[],
)
.unwrap();
let section = Section::new(
OpenSet::from_places([Place::new("a"), Place::new("b")]),
Marking::new().with_token("a", "h1").with_token("b", "h2"),
);
let hash = store_section_merkle(§ion, &conn).unwrap();
let loaded = load_section_merkle(&hash, &conn).unwrap();
assert_eq!(loaded.open_set, section.open_set);
assert_eq!(loaded.marking, section.marking);
}
#[cfg(feature = "native")]
#[test]
fn test_store_conflict_report_in_db() {
let conn = rusqlite::Connection::open_in_memory().unwrap();
conn.execute(
"CREATE TABLE card (hash TEXT PRIMARY KEY, content TEXT, g_time TEXT)",
[],
)
.unwrap();
let report = CohomologyConflictReport {
conflicting_places: (Place::new("a"), Place::new("b")),
intersection: OpenSet::from_places([Place::new("shared")]),
expected_marking: Marking::new().with_token("shared", "h1"),
actual_marking: Marking::new().with_token("shared", "h2"),
cohomology_class: "[delta_s]".to_string(),
timestamp: "2026-01-01T00:00:00Z".to_string(),
agent_dids: vec!["did:key:a1".to_string(), "did:key:a2".to_string()],
};
let hash = store_conflict_report(&report, &conn).unwrap();
let content: String = conn
.query_row(
"SELECT content FROM card WHERE hash = ?",
[&hash],
|row| row.get(0),
)
.unwrap();
assert!(content.contains("cohomology_class"));
assert!(content.contains("[delta_s]"));
assert!(content.contains("did:key:a1"));
}