From 7e7ec731cd6d756ac487245643449b19fb836bd3 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Thu, 19 Nov 2026 12:13:00 -0800 Subject: [PATCH] test(cli): keep the raw refusal exit test independent of BUZZ_* env (#9733) ## Summary Follow-up to #9653. Its new test `cli_exits_nonzero_when_the_relay_refuses_a_raw_publish` went through `run_from_args`, which resolves credentials from the caller's environment. With `BUZZ_AUTH_TAG` set (as it is for agents run by the ACP harness), the test client's key doesn't match the auth tag and the test exits 3 instead of 2. The check moves to an integration test, [`tests/raw_publish_exit.rs`](https://github.com/block/buzz/blob/c77555c348b230baa4327b9c2c15aa9a8a8d4013/crates/buzz-cli/tests/raw_publish_exit.rs), that runs the built `buzz` binary against a fake relay answering `accepted:false`, with every `BUZZ_*` variable removed from the child only. It asserts exit 2, empty stdout, a `relay_error` and one submission, so it covers the binary's real route and exit mapping. ## Regressions None: test-only change. ### Validation Ran the `buzz-cli` suite with a junk `BUZZ_AUTH_TAG` and `BUZZ_FEDERATED_IDENTITY_TOKEN` set. Replacing the `Cmd::Raw` dispatch with `Ok(())` makes the new test fail. --------- Signed-off-by: Tom Brow --- crates/buzz-cli/src/commands/raw.rs | 22 ------ crates/buzz-cli/tests/raw_publish_exit.rs | 85 +++++++++++++++++++++++ 2 files changed, 85 insertions(+), 22 deletions(-) create mode 100644 crates/buzz-cli/tests/raw_publish_exit.rs diff --git a/crates/buzz-cli/src/commands/raw.rs b/crates/buzz-cli/src/commands/raw.rs index 063d31d6a1..a2011d8d16 100644 --- a/crates/buzz-cli/src/commands/raw.rs +++ b/crates/buzz-cli/src/commands/raw.rs @@ -622,26 +622,4 @@ mod tests { assert!(out.is_empty(), "a refusal prints nothing to stdout"); assert_eq!(crate::error::exit_code(&err), 2); } - - #[tokio::test] - async fn cli_exits_nonzero_when_the_relay_refuses_a_raw_publish() { - let (url, captured) = fake_relay_acking("[]", false).await; - let key = Keys::generate().secret_key().to_secret_hex(); - let code = crate::run_from_args([ - "buzz", - "--relay", - url.as_str(), - "--private-key", - key.as_str(), - "raw", - "publish", - "--kind", - "7", - "--content", - "+", - ]) - .await; - assert_eq!(code, 2, "a refused publish is a relay error"); - assert_eq!(captured.events.lock().unwrap().len(), 1); - } } diff --git a/crates/buzz-cli/tests/raw_publish_exit.rs b/crates/buzz-cli/tests/raw_publish_exit.rs new file mode 100644 index 0000000000..878a37b970 --- /dev/null +++ b/crates/buzz-cli/tests/raw_publish_exit.rs @@ -0,0 +1,85 @@ +//! Runs the real `buzz` binary against a local fake relay to check the +//! process exit code of `raw publish` when the relay refuses the event. +//! +//! The child gets none of the caller's `BUZZ_*` variables, so an agent's +//! inherited auth tag or federated identity cannot change the outcome. + +use std::process::Command; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::sync::Arc; + +use axum::http::StatusCode; +use axum::routing::post; +use axum::Router; +use tokio::net::TcpListener; + +/// A relay bridge whose `/events` answers HTTP 200 with `accepted:false`, +/// as the relay does for a duplicate reaction. Returns its URL and a count +/// of submitted events. +async fn refusing_relay() -> (String, Arc) { + let submitted = Arc::new(AtomicUsize::new(0)); + let counter = submitted.clone(); + let app = Router::new().route( + "/events", + post(move |body: String| { + let counter = counter.clone(); + async move { + counter.fetch_add(1, Ordering::SeqCst); + let event: serde_json::Value = serde_json::from_str(&body).unwrap(); + let id = event["id"].as_str().unwrap().to_string(); + ( + StatusCode::OK, + [("content-type", "application/json")], + format!( + r#"{{"event_id":"{id}","accepted":false,"message":"duplicate: reaction already exists"}}"# + ), + ) + } + }), + ); + let listener = TcpListener::bind("127.0.0.1:0").await.unwrap(); + let addr = listener.local_addr().unwrap(); + tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); + (format!("http://{addr}"), submitted) +} + +#[tokio::test(flavor = "multi_thread")] +async fn raw_publish_exits_2_when_the_relay_refuses() { + let (url, submitted) = refusing_relay().await; + let key = nostr::Keys::generate().secret_key().to_secret_hex(); + + let mut cmd = Command::new(env!("CARGO_BIN_EXE_buzz")); + for (name, _) in std::env::vars_os() { + if name.to_string_lossy().starts_with("BUZZ_") { + cmd.env_remove(name); + } + } + cmd.args([ + "--relay", + url.as_str(), + "--private-key", + key.as_str(), + "raw", + "publish", + "--kind", + "7", + "--content", + "+", + ]); + let output = tokio::task::spawn_blocking(move || cmd.output().unwrap()) + .await + .unwrap(); + + let stderr = String::from_utf8_lossy(&output.stderr); + assert_eq!( + output.status.code(), + Some(2), + "a refused publish is a relay error; stderr: {stderr}" + ); + assert!( + output.stdout.is_empty(), + "a refusal prints nothing to stdout" + ); + assert!(stderr.contains("relay_error"), "stderr: {stderr}"); + assert_eq!(submitted.load(Ordering::SeqCst), 1); +}