diff --git a/crates/thing-core/src/port.rs b/crates/thing-core/src/port.rs index 40a77c2..c897d5e 100644 --- a/crates/thing-core/src/port.rs +++ b/crates/thing-core/src/port.rs @@ -100,3 +100,37 @@ pub trait StatePort { /// each once. fn drain(&mut self) -> Vec; } + +#[cfg(test)] +mod tests { + use super::*; + + fn address(provider: &str, schema: &str, key: &str) -> Address { + Address { provider: ProviderId::new(provider), schema: SchemaId::new(schema), key: RecordKey::new(key) } + } + + fn prefix(provider: &str, schema: &str, key_prefix: &str) -> Prefix { + Prefix { provider: ProviderId::new(provider), schema: SchemaId::new(schema), key_prefix: key_prefix.into() } + } + + #[test] + fn a_prefix_matches_provider_schema_and_key_prefix_together() { + let under = prefix("p", "s", "row-"); + assert!(under.matches(&address("p", "s", "row-1"))); + assert!(under.matches(&address("p", "s", "row-"))); + assert!(!under.matches(&address("q", "s", "row-1"))); + assert!(!under.matches(&address("p", "t", "row-1"))); + assert!(!under.matches(&address("p", "s", "col-1"))); + assert!(!under.matches(&address("p", "s", "ro"))); + } + + #[test] + fn a_record_interest_matches_only_its_address() { + let interest = Interest::Record(address("p", "s", "k")); + assert!(interest.matches(&address("p", "s", "k"))); + assert!(!interest.matches(&address("p", "s", "k2"))); + assert!(!interest.matches(&address("q", "s", "k"))); + assert!(Interest::Prefix(prefix("p", "s", "k")).matches(&address("p", "s", "k2"))); + assert!(!Interest::Prefix(prefix("p", "s", "k")).matches(&address("p", "s", "j"))); + } +} diff --git a/crates/thing-testkit/src/fixture.rs b/crates/thing-testkit/src/fixture.rs index 8cf2585..ff83ad9 100644 --- a/crates/thing-testkit/src/fixture.rs +++ b/crates/thing-testkit/src/fixture.rs @@ -61,8 +61,9 @@ impl Fixture { } } - /// Encode the built documents into a store and bless or compare the committed file. - pub fn write(self) { + /// Encode the built documents into a store and bless or compare the committed file; returns + /// the committed bytes. + pub fn write(self) -> Vec { let scratch = std::env::temp_dir().join(format!("thing-testkit-{}-{}", std::process::id(), self.file_name())); let mut cache = store::create(&scratch); let set = self.build(); @@ -77,7 +78,7 @@ impl Fixture { } let bytes = std::fs::read(&scratch).unwrap(); let _ = std::fs::remove_file(&scratch); - golden::check(&Self::dir(), self.file_name(), &bytes); + golden::check(&Self::dir(), self.file_name(), &bytes) } /// The committed store, decoded through the contract. diff --git a/crates/thing-testkit/tests/fixtures.rs b/crates/thing-testkit/tests/fixtures.rs index cd60559..c559c50 100644 --- a/crates/thing-testkit/tests/fixtures.rs +++ b/crates/thing-testkit/tests/fixtures.rs @@ -23,7 +23,9 @@ fn every_fixture_round_trips_through_the_contract() { on_big_stack(|| { for fixture in Fixture::ALL { // The builders reproduce the committed file byte for byte (or bless it). - fixture.write(); + let committed = fixture.write(); + assert_eq!(committed, std::fs::read(Fixture::dir().join(fixture.file_name())).unwrap()); + assert!(!committed.is_empty()); let built = fixture.build(); let loaded = fixture.load(); assert_eq!(loaded, built, "{}: the store decodes to the built documents", fixture.file_name()); diff --git a/crates/thing-testkit/tests/mem_state.rs b/crates/thing-testkit/tests/mem_state.rs index 512160e..1a0c5d9 100644 --- a/crates/thing-testkit/tests/mem_state.rs +++ b/crates/thing-testkit/tests/mem_state.rs @@ -243,3 +243,25 @@ fn every_call_is_logged_in_order() { let second = state.subscribe(Interest::Record(at)); assert_ne!(second, id, "a subscription id is never reused"); } + +#[test] +fn unsubscribing_ends_only_that_interest() { + let provider_a = provider("provider.a"); + let panel = composition("provider.a", "panel"); + let one = surface("provider.a", "one"); + let panel_at = address("provider.a", COMPOSITION, &panel.record_key()); + let mut state = MemState::new(); + let by_record = state.subscribe(Interest::Record(panel_at.clone())); + let by_prefix = state.subscribe(Interest::Prefix(prefix("provider.a", SURFACE, "thing:"))); + + state.unsubscribe(by_prefix); + state.put(&provider_a, &one); + state.put(&provider_a, &panel); + let drained = state.drain(); + assert_eq!(drained.len(), 1, "only the composition interest is live: {drained:?}"); + assert!(matches!(&drained[0], Change::Upsert(entry) if entry.address == panel_at)); + + state.unsubscribe(by_record); + state.put(&provider_a, &panel); + assert!(state.drain().is_empty()); +}