thing2 cut 2: core tests for interest matching, write returns the committed bytes, unsubscribe is per interest

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 20:46:51 +01:00
commit 963aec6f83
4 changed files with 63 additions and 4 deletions

View file

@ -100,3 +100,37 @@ pub trait StatePort {
/// each once.
fn drain(&mut self) -> Vec<Change>;
}
#[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")));
}
}

View file

@ -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<u8> {
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.

View file

@ -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());

View file

@ -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());
}