|
| 1 | +// Skip under Miri — TTL fixtures use `tempfile::tempdir()` + |
| 2 | +// `std::fs::create_dir_all/write`, both blocked by Miri's isolation. |
| 3 | +#![cfg(not(miri))] |
| 4 | + |
| 5 | +//! `OpenProjectBridge` scope-lock test — Northstar plan §3 C4. |
| 6 | +//! |
| 7 | +//! Verifies that a bridge locked to the `OpenProject` namespace |
| 8 | +//! resolves an OpenProject entity by URI, and that the same bridge |
| 9 | +//! refuses a `Healthcare` entity (cross-namespace leak refused). |
| 10 | +//! Mirrors the contract pinned by `bridge_scope_lock.rs` for the |
| 11 | +//! Woa/Medcare pair — symmetric coverage so the addition can't |
| 12 | +//! silently relax the scope-lock guarantee. |
| 13 | +
|
| 14 | +use lance_graph_ontology::bridges::{MedcareBridge, OpenProjectBridge}; |
| 15 | +use lance_graph_ontology::{NamespaceBridge, OgitUri, OntologyRegistry}; |
| 16 | +use std::fs; |
| 17 | +use std::sync::Arc; |
| 18 | + |
| 19 | +const TTL: &str = r#" |
| 20 | +@prefix ogit: <http://www.purl.org/ogit/> . |
| 21 | +@prefix ogit.OpenProject: <http://www.purl.org/ogit/OpenProject/> . |
| 22 | +@prefix ogit.Healthcare: <http://www.purl.org/ogit/Healthcare/> . |
| 23 | +@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . |
| 24 | +
|
| 25 | +ogit.OpenProject:WorkPackage |
| 26 | + a rdfs:Class; |
| 27 | + rdfs:subClassOf ogit:Entity; |
| 28 | + rdfs:label "WorkPackage"; |
| 29 | + ogit:scope "NTO"; |
| 30 | + ogit:parent ogit:Node; |
| 31 | + ogit:mandatory-attributes ( |
| 32 | + ogit:id |
| 33 | + ); |
| 34 | + ogit:optional-attributes ( ) ; |
| 35 | +. |
| 36 | +
|
| 37 | +ogit.Healthcare:Patient |
| 38 | + a rdfs:Class; |
| 39 | + rdfs:subClassOf ogit:Entity; |
| 40 | + rdfs:label "Patient"; |
| 41 | + ogit:scope "NTO"; |
| 42 | + ogit:parent ogit:Node; |
| 43 | + ogit:mandatory-attributes ( |
| 44 | + ogit:id |
| 45 | + ); |
| 46 | + ogit:optional-attributes ( ) ; |
| 47 | +. |
| 48 | +"#; |
| 49 | + |
| 50 | +fn make_registry() -> Arc<OntologyRegistry> { |
| 51 | + let tmp = tempfile::tempdir().unwrap(); |
| 52 | + fs::create_dir_all(tmp.path().join("OpenProject")).unwrap(); |
| 53 | + fs::create_dir_all(tmp.path().join("Healthcare")).unwrap(); |
| 54 | + fs::write(tmp.path().join("OpenProject").join("ents.ttl"), TTL).unwrap(); |
| 55 | + fs::write(tmp.path().join("Healthcare").join("ents.ttl"), TTL).unwrap(); |
| 56 | + let registry = Arc::new(OntologyRegistry::new_in_memory()); |
| 57 | + registry |
| 58 | + .hydrate_once_sync(tmp.path(), &["OpenProject", "Healthcare"]) |
| 59 | + .unwrap(); |
| 60 | + // Keep the tempdir alive for the duration of the test by leaking; the |
| 61 | + // test process exits shortly after. |
| 62 | + std::mem::forget(tmp); |
| 63 | + registry |
| 64 | +} |
| 65 | + |
| 66 | +#[test] |
| 67 | +fn openproject_bridge_resolves_openproject_entity_by_uri() { |
| 68 | + let registry = make_registry(); |
| 69 | + let bridge = OpenProjectBridge::new(registry).unwrap(); |
| 70 | + let uri = OgitUri::parse("ogit.OpenProject:WorkPackage").unwrap(); |
| 71 | + let entity = bridge.entity_by_uri(&uri).expect("scoped URI resolution"); |
| 72 | + assert_eq!(entity.schema_ptr.namespace_id(), bridge.g_lock()); |
| 73 | +} |
| 74 | + |
| 75 | +#[test] |
| 76 | +fn openproject_bridge_rejects_healthcare_entity_by_uri() { |
| 77 | + let registry = make_registry(); |
| 78 | + let bridge = OpenProjectBridge::new(registry).unwrap(); |
| 79 | + let uri = OgitUri::parse("ogit.Healthcare:Patient").unwrap(); |
| 80 | + let result = bridge.entity_by_uri(&uri); |
| 81 | + assert!( |
| 82 | + result.is_err(), |
| 83 | + "expected scope lock to refuse cross-namespace, got {result:?}", |
| 84 | + ); |
| 85 | + let err = result.unwrap_err(); |
| 86 | + let msg = format!("{err:?}"); |
| 87 | + assert!( |
| 88 | + msg.contains("CrossNamespaceLeak") || msg.contains("NotInScope"), |
| 89 | + "expected CrossNamespaceLeak or NotInScope, got {msg}", |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +#[test] |
| 94 | +fn medcare_bridge_rejects_openproject_entity_by_uri() { |
| 95 | + // Symmetry pin: the Healthcare bridge equally refuses an OpenProject |
| 96 | + // entity. Together with the previous test, the cross-namespace lock |
| 97 | + // is bidirectional. |
| 98 | + let registry = make_registry(); |
| 99 | + let bridge = MedcareBridge::new(registry).unwrap(); |
| 100 | + let uri = OgitUri::parse("ogit.OpenProject:WorkPackage").unwrap(); |
| 101 | + let result = bridge.entity_by_uri(&uri); |
| 102 | + assert!( |
| 103 | + result.is_err(), |
| 104 | + "expected medcare scope lock to refuse OpenProject URI, got {result:?}", |
| 105 | + ); |
| 106 | + let err = result.unwrap_err(); |
| 107 | + let msg = format!("{err:?}"); |
| 108 | + assert!( |
| 109 | + msg.contains("CrossNamespaceLeak") || msg.contains("NotInScope"), |
| 110 | + "expected CrossNamespaceLeak or NotInScope, got {msg}", |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +#[test] |
| 115 | +fn openproject_bridge_id_is_lowercase_openproject() { |
| 116 | + let registry = make_registry(); |
| 117 | + let bridge = OpenProjectBridge::new(registry).unwrap(); |
| 118 | + assert_eq!(bridge.bridge_id(), "openproject"); |
| 119 | +} |
| 120 | + |
| 121 | +#[test] |
| 122 | +fn openproject_bridge_g_lock_matches_openproject_namespace_id() { |
| 123 | + let registry = make_registry(); |
| 124 | + let expected = registry.namespace_id("OpenProject").unwrap(); |
| 125 | + let bridge = OpenProjectBridge::new(registry).unwrap(); |
| 126 | + assert_eq!(bridge.g_lock(), expected); |
| 127 | +} |
| 128 | + |
| 129 | +#[test] |
| 130 | +fn openproject_bridge_construction_fails_when_namespace_missing() { |
| 131 | + // No hydration -> no OpenProject namespace -> constructor returns |
| 132 | + // Error::UnknownNamespace. |
| 133 | + let registry = Arc::new(OntologyRegistry::new_in_memory()); |
| 134 | + let result = OpenProjectBridge::new(registry); |
| 135 | + assert!( |
| 136 | + result.is_err(), |
| 137 | + "expected UnknownNamespace error, got {result:?}", |
| 138 | + ); |
| 139 | +} |
0 commit comments