From 0f4ab2d0d95b3beca52c4c38858f1ce0aeb80edb Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 27 Nov 2026 20:08:02 +0100 Subject: [PATCH] brokkr-ctl on MessagePack payloads: reads through cultcache_rs::decode_payload, one MessagePack value per write, tests off JSON Co-Authored-By: Claude Sonnet 5.5 --- Cargo.lock | 12 ++++++++++++ brokkr-ctl/Cargo.toml | 1 + brokkr-ctl/src/blender.rs | 6 +++--- brokkr-ctl/tests/cli.rs | 17 ++++++++++++++--- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dabf833..f01e20a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -47,6 +47,18 @@ dependencies = [ "generic-array", ] +[[package]] +name = "brokkr-ctl" +version = "0.1.0" +dependencies = [ + "anyhow", + "cultcache-rs", + "rmp-serde", + "serde_json", + "tempfile", + "uuid", +] + [[package]] name = "brokkr-daemon" version = "0.1.0" diff --git a/brokkr-ctl/Cargo.toml b/brokkr-ctl/Cargo.toml index ce74ab3..4470cd6 100644 --- a/brokkr-ctl/Cargo.toml +++ b/brokkr-ctl/Cargo.toml @@ -7,6 +7,7 @@ publish = false [dependencies] anyhow = "1" cultcache-rs = { workspace = true } +rmp-serde = "1.3" serde_json = "1" uuid = { version = "1", features = ["v7"] } diff --git a/brokkr-ctl/src/blender.rs b/brokkr-ctl/src/blender.rs index c35148b..c342e91 100644 --- a/brokkr-ctl/src/blender.rs +++ b/brokkr-ctl/src/blender.rs @@ -217,7 +217,7 @@ fn send(options: &Options) -> Result { store.push(&CultCacheEnvelope { key: format!("{INTENT_PREFIX}{command_id}"), r#type: INTENT_SCHEMA.to_string(), - payload: serde_json::to_vec(&intent)?, + payload: rmp_serde::to_vec(&intent)?, stored_at: iso_now(), schema_id: Some(INTENT_SCHEMA.to_string()), })?; @@ -343,8 +343,8 @@ fn status(store: &SingleFileMessagePackBackingStore) -> Result { } fn decode(envelope: &CultCacheEnvelope) -> Result { - serde_json::from_slice(&envelope.payload) - .with_context(|| format!("{} is not a JSON map payload", envelope.key)) + cultcache_rs::decode_payload::(&envelope.payload) + .with_context(|| format!("{} is not one MessagePack value", envelope.key)) } fn print_fields(value: &Value) { diff --git a/brokkr-ctl/tests/cli.rs b/brokkr-ctl/tests/cli.rs index a6e2ced..072ae2a 100644 --- a/brokkr-ctl/tests/cli.rs +++ b/brokkr-ctl/tests/cli.rs @@ -12,7 +12,7 @@ fn envelope(schema: &str, key: &str, payload: &Value) -> CultCacheEnvelope { CultCacheEnvelope { key: key.to_string(), r#type: schema.to_string(), - payload: serde_json::to_vec(payload).unwrap(), + payload: rmp_serde::to_vec(payload).unwrap(), stored_at: "2026-11-27T00:00:00.000000+00:00".to_string(), schema_id: Some(schema.to_string()), } @@ -71,7 +71,7 @@ fn send_writes_a_pullable_intent() { .expect("intent key"); assert_eq!(intent.r#type, INTENT); assert_eq!(intent.schema_id.as_deref(), Some(INTENT)); - let payload: Value = serde_json::from_slice(&intent.payload).unwrap(); + let payload: Value = cultcache_rs::decode_payload(&intent.payload).unwrap(); assert_eq!( payload, json!({"schema": INTENT, "commandId": id, "action": "createObject", "requestedBy": "nibu/hands", @@ -202,7 +202,7 @@ fn help_words_as_option_values_do_not_swallow_the_command() { let id = stdout(&output).trim().strip_prefix("commandId: ").unwrap().to_string(); let found = SingleFileMessagePackBackingStore::new(&path).pull_all().unwrap(); let intent = found.iter().find(|e| e.key == format!("blender/commands/{id}")).unwrap(); - let payload: Value = serde_json::from_slice(&intent.payload).unwrap(); + let payload: Value = cultcache_rs::decode_payload(&intent.payload).unwrap(); assert_eq!(payload["note"], "-h"); assert_eq!(payload["requestedBy"], "nibu/--help"); // a real help flag still prints help and sends nothing @@ -320,3 +320,14 @@ fn help_names_every_dispatched_action_and_only_fields_the_addon_mentions() { assert!(source.contains(&format!("\"{field}\"")), "add-on never mentions {field}"); } } + +#[test] +fn a_json_payload_is_refused_not_guessed() { + let dir = tempfile::tempdir().unwrap(); + let path = store_with_receipts(dir.path(), &[]); + let mut json_receipt = envelope(RECEIPT, "blender/receipts/j", &json!({"commandId": "j", "status": "accepted"})); + json_receipt.payload = serde_json::to_vec(&json!({"commandId": "j", "status": "accepted"})).unwrap(); + push(&path, &[json_receipt]); + let output = ctl(&path, &["wait", "j", "--timeout", "1"], None); + assert_eq!(output.status.code(), Some(1)); +}