cultcache-rs: unit tests for owned-id rows, describe order and the type-name grammar

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-27 22:56:49 +01:00
commit bfcc6c6b1f

View file

@ -8347,6 +8347,132 @@ mod tests {
Ok(())
}
// Member rows: a registered type owns its id and describes it from the type at every write.
fn note_rows() -> Vec<SchemaCatalogMember> {
let row = |slot: u32, name: &str| SchemaCatalogMember {
slot,
member_name: name.into(),
type_name: "System.String".into(),
is_reference: false,
is_many: false,
target_schema_name: None,
is_name: false,
index_alias: None,
};
vec![row(0, "Title"), row(1, "Body")]
}
fn written_catalog(path: &Path) -> Result<Vec<PersistedSchemaCatalogEntry>> {
Ok(decode_store_snapshot(&std::fs::read(path)?)?.1)
}
#[test]
fn the_derive_writes_a_row_for_every_member() {
assert_eq!(Note::schema_members(), note_rows());
}
#[test]
fn an_owned_id_is_described_from_its_registered_type_over_what_the_file_holds() -> Result<()> {
let stale_alias = SchemaCatalogMember { index_alias: Some("stale".into()), ..note_rows().remove(0) };
for held_members in [Vec::new(), vec![stale_alias]] {
let temp = tempfile::tempdir()?;
let path = temp.path().join("store.cc");
let held = PersistedSchemaCatalogEntry(
"note".into(), "note".into(), "note.v1".into(), "note".into(), "{}".into(), vec!["note".into()], held_members,
);
let record = PersistedRecord("carried".into(), "note".into(), "2026-09-30T00:00:00Z".into(), rmp_serde::to_vec(&note_one())?);
std::fs::write(&path, rmp_serde::to_vec(&PersistedStoreSnapshot("cultcache.store.v1".into(), vec![held], vec![record]))?)?;
let mut cache = CultCache::new();
cache.register_entry_type::<Note>()?;
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path))?;
cache.pull_all_backing_stores()?;
cache.put("another", &note_one())?;
let catalog = written_catalog(&path)?;
assert_eq!(catalog.len(), 1);
assert_eq!(catalog[0].6, note_rows());
assert_eq!(SchemaCatalogEntry::from(&catalog[0]).members, note_rows());
assert!(catalog[0].4.contains("\"members\":[{\"slot\":0,\"name\":\"Title\",\"type\":\"System.String\""), "{}", catalog[0].4);
}
Ok(())
}
#[test]
fn a_type_no_registration_describes_gets_members_empty() -> Result<()> {
let temp = tempfile::tempdir()?;
let path = temp.path().join("store.cc");
let mut store = SingleFileMessagePackBackingStore::new(&path);
store.describe("note", &note_rows());
store.push(&CultCacheEnvelope {
key: "a".into(),
r#type: "tests.other".into(),
payload: b"x".to_vec(),
stored_at: "2026-09-30T00:00:00Z".into(),
schema_id: Some("tests.other".into()),
})?;
let catalog = written_catalog(&path)?;
assert_eq!(catalog.len(), 1);
assert!(catalog[0].6.is_empty());
assert!(catalog[0].4.ends_with("\"members\":[]}"), "{}", catalog[0].4);
Ok(())
}
#[test]
fn describe_reaches_the_store_whichever_of_the_type_and_the_store_comes_first() -> Result<()> {
for type_first in [true, false] {
let temp = tempfile::tempdir()?;
let path = temp.path().join("store.cc");
let mut cache = CultCache::new();
if type_first {
cache.register_entry_type::<Note>()?;
}
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path))?;
if !type_first {
cache.register_entry_type::<Note>()?;
}
cache.put("a", &note_one())?;
assert_eq!(written_catalog(&path)?[0].6, note_rows(), "type first: {type_first}");
}
Ok(())
}
#[test]
fn described_canonical_json_members_are_the_ones_csharp_writes() -> Result<()> {
let bytes = std::fs::read(concat!(env!("CARGO_MANIFEST_DIR"), "/../../tests/vectors/catalog-members/csharp-members-store.cc"))?;
let csharp = decode_store_snapshot(&bytes)?.1.remove(0);
assert_eq!(csharp.6.len(), 29);
let ours = described("tests.catalog_members", "tests.catalog_members".into(), &csharp.6);
let members_of = |json: &str| json[json.find("\"members\":").expect("members")..].to_string();
assert_eq!(members_of(&ours.4), members_of(&csharp.4));
assert_eq!(ours.6, csharp.6);
Ok(())
}
#[test]
fn type_names_follow_the_csharp_grammar() {
assert_eq!(<Option<i32>>::cult_type_name(), "System.Nullable<System.Int32>");
assert_eq!(<Option<String>>::cult_type_name(), "System.String");
assert_eq!(<Option<Option<i32>>>::cult_type_name(), "System.Nullable<System.Int32>");
assert_eq!(<Vec<Vec<u8>>>::cult_type_name(), "System.Collections.Generic.List<System.Collections.Generic.List<System.Byte>>");
assert_eq!(<Box<u64>>::cult_type_name(), "System.UInt64");
assert_eq!(<Option<Box<bool>>>::cult_type_name(), "System.Nullable<System.Boolean>");
assert_eq!(
<std::collections::HashMap<String, Vec<f32>>>::cult_type_name(),
"System.Collections.Generic.Dictionary<System.String, System.Collections.Generic.List<System.Single>>"
);
for (name, expected) in [
(char::cult_type_name(), "System.Char"), (i8::cult_type_name(), "System.SByte"), (u8::cult_type_name(), "System.Byte"),
(i16::cult_type_name(), "System.Int16"), (u16::cult_type_name(), "System.UInt16"), (u32::cult_type_name(), "System.UInt32"),
(i64::cult_type_name(), "System.Int64"), (f64::cult_type_name(), "System.Double"),
(serde_bytes::ByteBuf::cult_type_name(), "System.Byte[]"),
] {
assert_eq!(name, expected);
}
use crate::{__NamedType as _, __UnnamedType as _};
struct Nameless;
assert_eq!((&&__TypeNameProbe::<Vec<Nameless>>::new()).__cult_type_name(), "System.Object");
assert_eq!((&&__TypeNameProbe::<Vec<i16>>::new()).__cult_type_name(), "System.Collections.Generic.List<System.Int16>");
}
// Store read and write faults: what a consumer can tell apart, and what a failed
// write leaves on disk.