cultcache-rs and cultcache-py union parity tests: golden models carry element ids
Wrap and the Outcome arms gain id slots (unit arms become one-slot id records); the Python unit-arm codec behaviour is kept on a separate unit union. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
b6b6572abf
commit
e0747912c6
2 changed files with 101 additions and 52 deletions
|
|
@ -68,22 +68,51 @@ image = define_record_codec(
|
|||
],
|
||||
type_name=_GOLDEN + "Image",
|
||||
)
|
||||
wrap = define_record_codec("Wrap", [database_entry_field("inner", 0, codec=_node)], type_name=_GOLDEN + "Wrap")
|
||||
wrap = define_record_codec(
|
||||
"Wrap",
|
||||
[database_entry_field("inner", 0, codec=_node), database_entry_field("id", 1, codec=t.string)],
|
||||
type_name=_GOLDEN + "Wrap",
|
||||
)
|
||||
node = define_union(
|
||||
"Node",
|
||||
{0: ("Box", box), 1: ("Text", text), 2: ("Use", use), 5: ("Image", image), 6: ("Wrap", wrap)},
|
||||
type_name=_GOLDEN + "Node",
|
||||
)
|
||||
# The golden's Outcome: the arms of a list carry an element id, so Denied, Floor and Ceiling are one-slot records.
|
||||
_id_only = lambda name: define_record_codec(name, [database_entry_field("id", 0, codec=t.string)]) # noqa: E731
|
||||
outcome = define_union(
|
||||
"Outcome",
|
||||
{
|
||||
0: (
|
||||
"Pending",
|
||||
define_record_codec(
|
||||
"Pending",
|
||||
[database_entry_field("message", 0, codec=t.string), database_entry_field("id", 1, codec=t.string)],
|
||||
),
|
||||
),
|
||||
1: (
|
||||
"Completed",
|
||||
define_record_codec(
|
||||
"Completed",
|
||||
[database_entry_field("source_version", 0, codec=t.int32), database_entry_field("id", 1, codec=t.string)],
|
||||
),
|
||||
),
|
||||
2: ("Denied", _id_only("Denied")),
|
||||
-2147483648: ("Floor", _id_only("Floor")),
|
||||
2147483647: ("Ceiling", _id_only("Ceiling")),
|
||||
},
|
||||
type_name=_GOLDEN + "Outcome",
|
||||
)
|
||||
# The codec's unit arms (no value, written [key, []]), kept apart from the golden: a document list's arms carry ids.
|
||||
unit_outcome = define_union(
|
||||
"Outcome",
|
||||
{
|
||||
0: ("Pending", define_record_codec("Pending", [database_entry_field("message", 0, codec=t.string)])),
|
||||
1: ("Completed", define_record_codec("Completed", [database_entry_field("source_version", 0, codec=t.int32)])),
|
||||
2: ("Denied", None),
|
||||
-2147483648: ("Floor", None),
|
||||
2147483647: ("Ceiling", None),
|
||||
},
|
||||
type_name=_GOLDEN + "Outcome",
|
||||
type_name=_GOLDEN + "UnitOutcome",
|
||||
)
|
||||
tool = define_record_codec(
|
||||
"Tool",
|
||||
|
|
@ -121,41 +150,41 @@ def _surface_value() -> dict:
|
|||
"root": UnionArm(
|
||||
"Box",
|
||||
{
|
||||
"id": "root",
|
||||
"id": "0000000000c0",
|
||||
"children": [
|
||||
UnionArm("Text", {"id": "t1", "content": "hello"}),
|
||||
UnionArm("Text", {"id": "0000000000c1", "content": "hello"}),
|
||||
UnionArm(
|
||||
"Use",
|
||||
{
|
||||
"id": "u1",
|
||||
"id": "0000000000c2",
|
||||
"composition": {
|
||||
"name": "comp",
|
||||
"body": UnionArm(
|
||||
"Box",
|
||||
{
|
||||
"id": "inner",
|
||||
"id": "0000000000d0",
|
||||
"children": [
|
||||
UnionArm("Text", {"id": "deep", "content": "x"}),
|
||||
UnionArm("Image", {"id": "img", "uri": "uri://a", "alt": "alt"}),
|
||||
UnionArm("Text", {"id": "0000000000d1", "content": "x"}),
|
||||
UnionArm("Image", {"id": "0000000000d2", "uri": "uri://a", "alt": "alt"}),
|
||||
],
|
||||
},
|
||||
),
|
||||
},
|
||||
"slots": [
|
||||
UnionArm("Text", {"id": "s1", "content": "slot"}),
|
||||
UnionArm("Text", {"id": "0000000000e1", "content": "slot"}),
|
||||
None,
|
||||
UnionArm("Box", {"id": "b2", "children": []}),
|
||||
UnionArm("Box", {"id": "0000000000e2", "children": []}),
|
||||
],
|
||||
},
|
||||
),
|
||||
UnionArm("Image", {"id": "i2", "uri": "uri://b", "alt": ""}),
|
||||
UnionArm("Image", {"id": "0000000000c3", "uri": "uri://b", "alt": ""}),
|
||||
],
|
||||
},
|
||||
),
|
||||
"outcomes": [
|
||||
UnionArm("Pending", {"message": "wait"}),
|
||||
UnionArm("Completed", {"source_version": 7}),
|
||||
UnionArm("Denied"),
|
||||
UnionArm("Pending", {"message": "wait", "id": "0000000000a1"}),
|
||||
UnionArm("Completed", {"source_version": 7, "id": "0000000000a2"}),
|
||||
UnionArm("Denied", {"id": "0000000000a3"}),
|
||||
],
|
||||
"part": UnionArm("Tool", {"name": "hammer", "uses": 12}),
|
||||
"crafted": UnionArm("Part", {"name": "gear", "count": 4}),
|
||||
|
|
@ -256,12 +285,12 @@ class UnionCodecRuleTests(unittest.TestCase):
|
|||
)
|
||||
|
||||
def test_unit_arm_is_written_with_empty_slots_and_reads_back(self) -> None:
|
||||
self.assertEqual(outcome.encode(UnionArm("Denied")), [2, []])
|
||||
self.assertEqual(outcome.decode([2, []]), UnionArm("Denied", None))
|
||||
self.assertEqual(unit_outcome.encode(UnionArm("Denied")), [2, []])
|
||||
self.assertEqual(unit_outcome.decode([2, []]), UnionArm("Denied", None))
|
||||
|
||||
def test_unit_arm_refuses_a_value_to_carry(self) -> None:
|
||||
with self.assertRaises(ValueError) as caught:
|
||||
outcome.encode(UnionArm("Denied", {"message": self.CANARY}))
|
||||
unit_outcome.encode(UnionArm("Denied", {"message": self.CANARY}))
|
||||
self.assertEqual(str(caught.exception), "Union 'Outcome' arm 2 is a unit arm and carries no value")
|
||||
|
||||
def test_arm_keys_are_written_as_declared(self) -> None:
|
||||
|
|
@ -333,7 +362,7 @@ class UnionCodecRuleTests(unittest.TestCase):
|
|||
self.assertEqual(row.decode(encoded), {"id": "r", "cells": cells})
|
||||
|
||||
def test_a_unit_arm_with_extra_slots_is_read_past(self) -> None:
|
||||
self.assertEqual(outcome.decode([2, ["extra"]]), UnionArm("Denied"))
|
||||
self.assertEqual(unit_outcome.decode([2, ["extra"]]), UnionArm("Denied"))
|
||||
|
||||
def test_union_arms_are_immutable_hashable_values(self) -> None:
|
||||
arm = UnionArm("Denied")
|
||||
|
|
@ -478,7 +507,7 @@ CANARY = "CANARY-union-golden-4c2a"
|
|||
def _wrap_chain(wraps: int, leaf: UnionArm) -> UnionArm:
|
||||
value = leaf
|
||||
for _ in range(wraps):
|
||||
value = UnionArm("Wrap", {"inner": value})
|
||||
value = UnionArm("Wrap", {"inner": value, "id": "0000000000b1"})
|
||||
return value
|
||||
|
||||
|
||||
|
|
@ -521,9 +550,9 @@ class BoundsVectorTests(unittest.TestCase):
|
|||
self.assertEqual(counts, {"accepted": 4, "refused": 7, "read-only": 1})
|
||||
|
||||
def test_the_value_at_the_limit_writes_the_vector_and_the_value_past_it_is_refused_on_write(self) -> None:
|
||||
at_limit = {**_surface_value(), "root": _wrap_chain(126, UnionArm("Box", {"id": "leaf", "children": []}))}
|
||||
at_limit = {**_surface_value(), "root": _wrap_chain(126, UnionArm("Box", {"id": "0000000000f0", "children": []}))}
|
||||
self.assertEqual(surface_document.encode_payload(at_limit), _vector("depth-at-limit.msgpack"))
|
||||
past = {**_surface_value(), "root": _wrap_chain(127, UnionArm("Text", {"id": "leaf", "content": CANARY}))}
|
||||
past = {**_surface_value(), "root": _wrap_chain(127, UnionArm("Text", {"id": "0000000000f0", "content": CANARY}))}
|
||||
with self.assertRaises(CultCachePayloadDepthError) as caught:
|
||||
surface_document.encode_payload(past)
|
||||
message = str(caught.exception)
|
||||
|
|
@ -533,7 +562,7 @@ class BoundsVectorTests(unittest.TestCase):
|
|||
self.assertNotIn(CANARY, message)
|
||||
|
||||
def test_a_cache_put_of_the_value_past_the_limit_refuses_and_stores_nothing(self) -> None:
|
||||
past = {**_surface_value(), "root": _wrap_chain(127, UnionArm("Box", {"id": "leaf", "children": []}))}
|
||||
past = {**_surface_value(), "root": _wrap_chain(127, UnionArm("Box", {"id": "0000000000f0", "children": []}))}
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
path = Path(tmp) / "store.cc"
|
||||
cache = (
|
||||
|
|
@ -709,8 +738,8 @@ class UnionDecodeBoundsTests(unittest.TestCase):
|
|||
self.assertEqual(str(caught.exception), expected)
|
||||
|
||||
def test_the_ends_of_the_int32_key_domain_are_unit_arms_that_read_and_write(self) -> None:
|
||||
self.assertEqual(outcome.decode([-(2**31), []]), UnionArm("Floor"))
|
||||
self.assertEqual(outcome.encode(UnionArm("Ceiling")), [2**31 - 1, []])
|
||||
self.assertEqual(unit_outcome.decode([-(2**31), []]), UnionArm("Floor"))
|
||||
self.assertEqual(unit_outcome.encode(UnionArm("Ceiling")), [2**31 - 1, []])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@ enum Node {
|
|||
struct WrapArm {
|
||||
#[cultcache(key = 0)]
|
||||
inner: Option<std::boxed::Box<Node>>,
|
||||
#[cultcache(key = 1)]
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, CultSlots)]
|
||||
|
|
@ -86,23 +88,34 @@ enum Outcome {
|
|||
#[cultcache(key = 1)]
|
||||
Completed(CompletedArm),
|
||||
#[cultcache(key = 2)]
|
||||
Denied,
|
||||
Denied(IdArm),
|
||||
#[cultcache(key = -2147483648)]
|
||||
Floor,
|
||||
Floor(IdArm),
|
||||
#[cultcache(key = 2147483647)]
|
||||
Ceiling,
|
||||
Ceiling(IdArm),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, CultSlots)]
|
||||
struct PendingArm {
|
||||
#[cultcache(key = 0)]
|
||||
message: String,
|
||||
#[cultcache(key = 1)]
|
||||
id: String,
|
||||
}
|
||||
|
||||
// An arm with no members of its own: just its element id.
|
||||
#[derive(Clone, Debug, PartialEq, CultSlots)]
|
||||
struct IdArm {
|
||||
#[cultcache(key = 0)]
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, CultSlots)]
|
||||
struct CompletedArm {
|
||||
#[cultcache(key = 0)]
|
||||
source_version: i32,
|
||||
#[cultcache(key = 1)]
|
||||
id: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, CultSlots)]
|
||||
|
|
@ -178,26 +191,26 @@ fn boxed(id: &str, children: Vec<Node>) -> Node {
|
|||
}
|
||||
|
||||
fn constructed() -> UnionSurface {
|
||||
let inner = boxed("inner", vec![text("deep", "x"), image("img", "uri://a", "alt")]);
|
||||
let inner = boxed("0000000000d0", vec![text("0000000000d1", "x"), image("0000000000d2", "uri://a", "alt")]);
|
||||
UnionSurface {
|
||||
id: "surface".into(),
|
||||
version: 3,
|
||||
root: Some(boxed(
|
||||
"root",
|
||||
"0000000000c0",
|
||||
vec![
|
||||
text("t1", "hello"),
|
||||
text("0000000000c1", "hello"),
|
||||
Node::Use(UseArm {
|
||||
id: "u1".into(),
|
||||
id: "0000000000c2".into(),
|
||||
composition: Composition { name: "comp".into(), body: Some(std::boxed::Box::new(inner)) },
|
||||
slots: vec![Some(text("s1", "slot")), None, Some(boxed("b2", vec![]))],
|
||||
slots: vec![Some(text("0000000000e1", "slot")), None, Some(boxed("0000000000e2", vec![]))],
|
||||
}),
|
||||
image("i2", "uri://b", ""),
|
||||
image("0000000000c3", "uri://b", ""),
|
||||
],
|
||||
)),
|
||||
outcomes: vec![
|
||||
Outcome::Pending(PendingArm { message: "wait".into() }),
|
||||
Outcome::Completed(CompletedArm { source_version: 7 }),
|
||||
Outcome::Denied,
|
||||
Outcome::Pending(PendingArm { message: "wait".into(), id: "0000000000a1".into() }),
|
||||
Outcome::Completed(CompletedArm { source_version: 7, id: "0000000000a2".into() }),
|
||||
Outcome::Denied(id_arm("0000000000a3")),
|
||||
],
|
||||
part: Some(Item::Tool(UsesArm { name: "hammer".into(), uses: 12 })),
|
||||
crafted: Some(CraftedItem::Part(CountArm { name: "gear".into(), count: 4 })),
|
||||
|
|
@ -205,6 +218,10 @@ fn constructed() -> UnionSurface {
|
|||
}
|
||||
}
|
||||
|
||||
fn id_arm(id: &str) -> IdArm {
|
||||
IdArm { id: id.into() }
|
||||
}
|
||||
|
||||
fn vector(name: &str) -> Vec<u8> {
|
||||
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
||||
.join("../../tests/vectors/union-parity")
|
||||
|
|
@ -225,9 +242,12 @@ fn union_surface_decodes_and_reencodes() {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn a_unit_arm_is_a_key_and_an_empty_slots_array() {
|
||||
assert_eq!(rmp_serde::to_vec(&Outcome::Denied).unwrap(), [0x92, 0x02, 0x90]);
|
||||
assert_eq!(rmp_serde::from_slice::<Outcome>(&[0x92, 0x02, 0x90]).unwrap(), Outcome::Denied);
|
||||
fn an_id_only_arm_is_a_key_and_a_one_slot_array() {
|
||||
let mut bytes = vec![0x92, 0x02, 0x91, 0xac];
|
||||
bytes.extend_from_slice(b"0000000000a3");
|
||||
let denied = Outcome::Denied(id_arm("0000000000a3"));
|
||||
assert_eq!(rmp_serde::to_vec(&denied).unwrap(), bytes);
|
||||
assert_eq!(rmp_serde::from_slice::<Outcome>(&bytes).unwrap(), denied);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -246,7 +266,7 @@ fn union_refusal_vectors() {
|
|||
let key = key.expect(&file);
|
||||
assert!(message.contains(&format!("unknown arm key {key} ")), "{file}: {message}");
|
||||
}
|
||||
for canary in ["root", "hello", "uri://", "surface"] {
|
||||
for canary in ["0000000000c0", "hello", "uri://", "surface"] {
|
||||
assert!(!message.contains(canary), "{file} echoed a value: {message}");
|
||||
}
|
||||
}
|
||||
|
|
@ -612,9 +632,9 @@ fn bounds_vectors() {
|
|||
let value = result.unwrap_or_else(|message| panic!("{file}: {message}"));
|
||||
assert_eq!(rmp_serde::to_vec(&value).unwrap(), vector(detail), "{file}");
|
||||
match file {
|
||||
"arm-key-int32-min.msgpack" => assert_eq!(value.outcomes.last(), Some(&Outcome::Floor)),
|
||||
"arm-key-int32-min.msgpack" => assert_eq!(value.outcomes.last(), Some(&Outcome::Floor(id_arm("0000000000a4")))),
|
||||
"arm-key-int32-max.msgpack" | "arm-key-wide-coding.msgpack" => {
|
||||
assert_eq!(value.outcomes.last(), Some(&Outcome::Ceiling))
|
||||
assert_eq!(value.outcomes.last(), Some(&Outcome::Ceiling(id_arm("0000000000a5"))))
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
@ -648,7 +668,7 @@ fn bounds_vectors() {
|
|||
fn wrap_chain(wraps: usize, leaf: Node) -> UnionSurface {
|
||||
let mut node = leaf;
|
||||
for _ in 0..wraps {
|
||||
node = Node::Wrap(WrapArm { inner: Some(std::boxed::Box::new(node)) });
|
||||
node = Node::Wrap(WrapArm { inner: Some(std::boxed::Box::new(node)), id: "0000000000b1".into() });
|
||||
}
|
||||
UnionSurface { root: Some(node), ..constructed() }
|
||||
}
|
||||
|
|
@ -657,9 +677,9 @@ fn wrap_chain(wraps: usize, leaf: Node) -> UnionSurface {
|
|||
// children) ends at 2 * (wraps + 1) + 2 and a Text leaf at 2 * (wraps + 1) + 1.
|
||||
#[test]
|
||||
fn the_constructed_chains_write_the_vector_bytes() {
|
||||
let at_limit = wrap_chain(126, boxed("leaf", vec![]));
|
||||
let at_limit = wrap_chain(126, boxed("0000000000f0", vec![]));
|
||||
assert_eq!(rmp_serde::to_vec(&at_limit).unwrap(), vector("depth-at-limit.msgpack"));
|
||||
let past_limit = wrap_chain(127, text("leaf", CANARY));
|
||||
let past_limit = wrap_chain(127, text("0000000000f0", CANARY));
|
||||
assert_eq!(rmp_serde::to_vec(&past_limit).unwrap(), vector("depth-past-limit.msgpack"));
|
||||
}
|
||||
|
||||
|
|
@ -677,14 +697,14 @@ fn write_refused_past_limit() {
|
|||
cache.register_entry_type::<UnionSurface>().unwrap();
|
||||
cache.add_generic_backing_store(SingleFileMessagePackBackingStore::new(&path)).unwrap();
|
||||
|
||||
let at_limit = wrap_chain(126, boxed("leaf", vec![]));
|
||||
let at_limit = wrap_chain(126, boxed("0000000000f0", vec![]));
|
||||
assert_eq!(cache.put("at", &at_limit).unwrap(), at_limit);
|
||||
let stored = cache.snapshot_envelopes();
|
||||
assert_eq!(stored.len(), 1);
|
||||
assert_eq!(stored[0].payload, vector("depth-at-limit.msgpack"));
|
||||
let file_before = std::fs::read(&path).unwrap();
|
||||
|
||||
let error = cache.put("past", &wrap_chain(127, text("leaf", CANARY))).unwrap_err();
|
||||
let error = cache.put("past", &wrap_chain(127, text("0000000000f0", CANARY))).unwrap_err();
|
||||
assert_eq!(error.downcast_ref::<PayloadTooDeep>(), Some(&PayloadTooDeep { depth: 257, limit: 256 }));
|
||||
assert!(!format!("{error:#}").contains(CANARY), "{error:#}");
|
||||
assert_eq!(cache.get::<UnionSurface>("past").unwrap(), None);
|
||||
|
|
@ -741,8 +761,8 @@ fn raw(type_name: &str, key: &str, payload: Vec<u8>) -> CultCacheEnvelope {
|
|||
|
||||
#[test]
|
||||
fn the_raw_doors_hold_the_bound_for_arms_maps_and_arrays() {
|
||||
let arms_at = rmp_serde::to_vec(&wrap_chain(126, boxed("leaf", vec![]))).unwrap();
|
||||
let arms_past = rmp_serde::to_vec(&wrap_chain(127, text("leaf", CANARY))).unwrap();
|
||||
let arms_at = rmp_serde::to_vec(&wrap_chain(126, boxed("0000000000f0", vec![]))).unwrap();
|
||||
let arms_past = rmp_serde::to_vec(&wrap_chain(127, text("0000000000f0", CANARY))).unwrap();
|
||||
let maps_at = rmp_serde::to_vec_named(&map_doc(MAX_PAYLOAD_DEPTH - 1)).unwrap();
|
||||
let maps_past = rmp_serde::to_vec_named(&map_doc(MAX_PAYLOAD_DEPTH)).unwrap();
|
||||
let arrays_at = vector("depth-at-limit.msgpack");
|
||||
|
|
@ -790,13 +810,13 @@ fn stack_budget_debug_2mib() {
|
|||
.spawn(|| {
|
||||
assert_eq!(
|
||||
through_cache(&vector("depth-at-limit.msgpack")).unwrap(),
|
||||
wrap_chain(126, boxed("leaf", vec![]))
|
||||
wrap_chain(126, boxed("0000000000f0", vec![]))
|
||||
);
|
||||
let refused = through_cache(&vector("depth-past-limit.msgpack")).unwrap_err();
|
||||
assert!(refused.contains("the limit is 256"), "{refused}");
|
||||
let mut cache = CultCache::new();
|
||||
cache.register_entry_type::<UnionSurface>().unwrap();
|
||||
let value = wrap_chain(126, boxed("leaf", vec![]));
|
||||
let value = wrap_chain(126, boxed("0000000000f0", vec![]));
|
||||
assert_eq!(cache.put("at", &value).unwrap(), value);
|
||||
assert_eq!(cache.get::<UnionSurface>("at").unwrap(), Some(value));
|
||||
let mut deep = vec![0x91u8; 100_000];
|
||||
|
|
|
|||
Loading…
Reference in a new issue