diff --git a/crates/thing-testkit/src/state.rs b/crates/thing-testkit/src/state.rs index 7b92911..59e0f89 100644 --- a/crates/thing-testkit/src/state.rs +++ b/crates/thing-testkit/src/state.rs @@ -38,6 +38,19 @@ impl DatabaseEntry for SwarmWithoutRows { const SCHEMA_NAME: &'static str = "gamecult.thing.test.swarm.v1"; } +/// `Swarm` with its load member named Burden: the record a provider that renamed a member holds +/// under the same schema id. +#[derive(Clone, Debug, PartialEq, DatabaseEntry)] +#[cultcache(type = "gamecult.thing.test.swarm.v1", schema = "gamecult.thing.test.swarm.v1")] +pub struct SwarmRenamed { + #[cultcache(key = 0)] + pub id: String, + #[cultcache(key = 1)] + pub name: String, + #[cultcache(key = 2)] + pub burden: f64, +} + #[derive(Clone, Debug, PartialEq, DatabaseEntry)] #[cultcache(type = "gamecult.thing.test.target.v1", schema = "gamecult.thing.test.target.v1")] pub struct Target { diff --git a/crates/thing-testkit/tests/state.rs b/crates/thing-testkit/tests/state.rs index 64c21d0..59f69b2 100644 --- a/crates/thing-testkit/tests/state.rs +++ b/crates/thing-testkit/tests/state.rs @@ -221,3 +221,28 @@ fn load_store_names_a_record_by_its_schema_id_when_it_has_one() { let Lookup::Refused(refusal) = found else { panic!("{found:?}") }; assert_eq!(refusal.kind, SchemaId::new("gamecult.thing.test.alias.v1")); } + +#[test] +fn load_store_falls_back_to_the_entry_type_when_an_envelope_has_no_schema_id() { + // A store written without schema ids names a record's schema by its entry type, and the + // catalog's member rows are found under that name. + let dir = std::env::temp_dir().join(format!("thing-state-noschema-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("noschema.cc"); + let bytes = thing_testkit::store::bytes(|cache| { + cache.register_entry_type::().unwrap(); + let swarm = Swarm { id: "a".into(), name: "n".into(), load: 1.0 }; + let (mut envelope, _) = cache.prepare_entry("k", &swarm).unwrap(); + envelope.stored_at = thing_testkit::store::PINNED_CLOCK.into(); + envelope.schema_id = None; + cache.put_envelope::(envelope).unwrap(); + }); + std::fs::write(&path, bytes).unwrap(); + let mut state = MemState::new(); + state.load_store(&ProviderId::new(PROVIDER), &path); + let _ = std::fs::remove_dir_all(&dir); + let found = state.read(&at(SWARM_SCHEMA, "k")); + let Lookup::Present(Entry { record: Record::State { schema, value }, .. }) = found else { panic!("{found:?}") }; + assert_eq!(schema, SchemaId::new(SWARM_SCHEMA)); + assert_eq!(value.member("Name"), Some(&Text("n".into()))); +}