Thing 2.0: SwarmRenamed, and a test for load_store's fallback to the entry type

Closes thing2:finding:cut-state-port.s2.schema-id-fallback-untested.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-28 03:19:29 +01:00
commit 70a20dddc2
2 changed files with 38 additions and 0 deletions

View file

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

View file

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