Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(tests): deduplicate test helpers into shared rpc_harness module

Steven Enamakelb508afcd7588
diff --git a/tests/in_process/agent_approval_memory_coverage_e2e.rs b/tests/in_process/agent_approval_memory_coverage_e2e.rs
index 370bd5c815..a18a55c0b7 100644
--- a/tests/in_process/agent_approval_memory_coverage_e2e.rs
+++ b/tests/in_process/agent_approval_memory_coverage_e2e.rs
@@ -20,6 +20,7 @@
//! JSON-RPC router (`build_core_http_router`) and asserted on, so the number the
//! gate reports and the coverage that exists are the same thing.
+use crate::rpc_harness::{error_message, ok, payload};
use crate::env_guard::EnvVarGuard;
use crate::env_guard::env_lock;
use crate::rpc_auth::{ensure_rpc_auth, rpc_token};
@@ -143,32 +144,6 @@ async fn rpc(rpc_base: &str, id: i64, method: &str, params: Value) -> Value {
.unwrap_or_else(|err| panic!("json for {method}: {err}"))
}
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
-/// Peel the conditional `Outcome` envelope. A handler that emits no log
-/// lines returns the bare value; one that emits any returns
-/// `{ result, logs }`. Both shapes are valid for the same method, so every
-/// consumer has to tolerate both — see `crates/openhuman-rpc/src/mod.rs`.
-fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
- let result = ok(value, context);
- result.get("result").unwrap_or(result)
-}
-
-fn error_message<'a>(value: &'a Value, context: &str) -> &'a str {
- value
- .get("error")
- .and_then(|error| error.get("message"))
- .and_then(Value::as_str)
- .unwrap_or_else(|| panic!("{context}: error missing message: {value}"))
-}
-
// ---------------------------------------------------------------------------
// approval.get_gate_state
// ---------------------------------------------------------------------------
diff --git a/tests/in_process/channels_default_channel_e2e.rs b/tests/in_process/channels_default_channel_e2e.rs
index f5bc193e31..f03efec479 100644
--- a/tests/in_process/channels_default_channel_e2e.rs
+++ b/tests/in_process/channels_default_channel_e2e.rs
@@ -49,6 +49,7 @@
//!
//! Run with: `cargo test -p openhuman-cli --test in_process_all`
+use crate::rpc_harness::{rpc};
use crate::env_guard::EnvVarGuard;
use crate::env_guard::env_lock;
use crate::rpc_auth::{ensure_rpc_auth, rpc_token};
@@ -166,35 +167,6 @@ async fn setup() -> Harness {
}
}
-async fn rpc(base: &str, id: i64, method: &str, params: Value) -> Value {
- let client = reqwest::Client::builder()
- .timeout(Duration::from_secs(30))
- .build()
- .expect("client");
- let url = format!("{}/rpc", base.trim_end_matches('/'));
- let response = client
- .post(&url)
- .header(AUTHORIZATION, format!("Bearer {}", rpc_token()))
- .json(&json!({
- "jsonrpc": "2.0",
- "id": id,
- "method": method,
- "params": params,
- }))
- .send()
- .await
- .unwrap_or_else(|err| panic!("POST {url} {method}: {err}"));
- assert_eq!(
- response.status(),
- StatusCode::OK,
- "HTTP transport should accept {method}"
- );
- response
- .json::<Value>()
- .await
- .unwrap_or_else(|err| panic!("json for {method}: {err}"))
-}
-
async fn set_default(harness: &Harness, id: i64, channel: &str) -> Value {
rpc(
&harness.rpc_base,
diff --git a/tests/in_process/config_auth_app_state_connectivity_e2e.rs b/tests/in_process/config_auth_app_state_connectivity_e2e.rs
index 10a2c00d9d..09e1441537 100644
--- a/tests/in_process/config_auth_app_state_connectivity_e2e.rs
+++ b/tests/in_process/config_auth_app_state_connectivity_e2e.rs
@@ -1,6 +1,7 @@
//! Focused JSON-RPC E2E coverage for config, auth/credentials, app_state,
//! and connectivity controller surfaces.
+use crate::rpc_harness::{ok, payload, rpc, schema};
use crate::env_guard::{env_lock, EnvVarGuard};
use crate::rpc_auth::{ensure_rpc_auth, rpc_token};
use std::net::SocketAddr;
@@ -300,65 +301,12 @@ async fn setup() -> TestHarness {
}
}
-async fn schema(rpc_base: &str) -> Value {
- let url = format!("{}/schema", rpc_base.trim_end_matches('/'));
- reqwest::get(&url)
- .await
- .unwrap_or_else(|err| panic!("GET {url}: {err}"))
- .json::<Value>()
- .await
- .expect("schema json")
-}
-
-async fn rpc(rpc_base: &str, id: i64, method: &str, params: Value) -> Value {
- let client = reqwest::Client::builder()
- .timeout(Duration::from_secs(30))
- .build()
- .expect("client");
- let url = format!("{}/rpc", rpc_base.trim_end_matches('/'));
- let response = client
- .post(&url)
- .header(AUTHORIZATION, format!("Bearer {}", rpc_token()))
- .json(&json!({
- "jsonrpc": "2.0",
- "id": id,
- "method": method,
- "params": params,
- }))
- .send()
- .await
- .unwrap_or_else(|err| panic!("POST {url} {method}: {err}"));
- assert_eq!(
- response.status(),
- StatusCode::OK,
- "HTTP transport should accept {method}"
- );
- response
- .json::<Value>()
- .await
- .unwrap_or_else(|err| panic!("json for {method}: {err}"))
-}
-
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
fn err<'a>(value: &'a Value, context: &str) -> &'a Value {
value
.get("error")
.unwrap_or_else(|| panic!("{context}: expected JSON-RPC error, got: {value}"))
}
-fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
- let result = ok(value, context);
- result.get("result").unwrap_or(result)
-}
-
fn assert_error_contains(value: &Value, context: &str, needle: &str) {
let message = err(value, context)
.get("message")
diff --git a/tests/in_process/domain_modules_e2e.rs b/tests/in_process/domain_modules_e2e.rs
index 36dec45ee0..b0ffb48ac5 100644
--- a/tests/in_process/domain_modules_e2e.rs
+++ b/tests/in_process/domain_modules_e2e.rs
@@ -5,6 +5,7 @@
//! exercises cheap read/status handlers through HTTP. Mutating or networked
//! domain behavior remains covered by the focused `*_e2e.rs` suites.
+use crate::rpc_harness::{ok, payload, rpc, schema, write_min_config};
use crate::env_guard::EnvVarGuard;
use crate::env_guard::env_lock;
use crate::memory_module;
@@ -34,32 +35,6 @@ async fn serve_rpc() -> (
(addr, join)
}
-fn write_min_config(openhuman_dir: &Path) {
- std::fs::create_dir_all(openhuman_dir).expect("create .openhuman");
- let cfg = r#"api_url = "http://127.0.0.1:9"
-default_model = "e2e-model"
-default_temperature = 0.2
-
-[secrets]
-encrypt = false
-
-[local_ai]
-enabled = false
-
-[memory]
-provider = "none"
-embedding_provider = "none"
-embedding_model = "none"
-embedding_dimensions = 0
-
-[memory_tree]
-embedding_strict = false
-"#;
- std::fs::write(openhuman_dir.join("config.toml"), cfg).expect("write config.toml");
- let _: openhuman_core::config::Config =
- toml::from_str(cfg).expect("test config must match schema");
-}
-
struct TestHarness {
_tmp: TempDir,
_guards: Vec<EnvVarGuard>,
@@ -110,54 +85,6 @@ async fn setup() -> TestHarness {
}
}
-async fn schema(rpc_base: &str) -> Value {
- let url = format!("{}/schema", rpc_base.trim_end_matches('/'));
- reqwest::get(&url)
- .await
- .unwrap_or_else(|err| panic!("GET {url}: {err}"))
- .json::<Value>()
- .await
- .expect("schema json")
-}
-
-async fn rpc(rpc_base: &str, id: i64, method: &str, params: Value) -> Value {
- let client = reqwest::Client::builder()
- .timeout(Duration::from_secs(30))
- .build()
- .expect("client");
- let url = format!("{}/rpc", rpc_base.trim_end_matches('/'));
- let response = client
- .post(&url)
- .header(AUTHORIZATION, format!("Bearer {}", rpc_token()))
- .json(&json!({
- "jsonrpc": "2.0",
- "id": id,
- "method": method,
- "params": params,
- }))
- .send()
- .await
- .unwrap_or_else(|err| panic!("POST {url} {method}: {err}"));
- assert_eq!(
- response.status(),
- StatusCode::OK,
- "HTTP transport should accept {method}"
- );
- response
- .json::<Value>()
- .await
- .unwrap_or_else(|err| panic!("json for {method}: {err}"))
-}
-
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
fn err<'a>(value: &'a Value, context: &str) -> &'a Value {
value
.get("error")
@@ -170,11 +97,6 @@ fn data<'a>(value: &'a Value, context: &str) -> &'a Value {
.unwrap_or_else(|| panic!("{context}: missing data envelope: {value}"))
}
-fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
- let result = ok(value, context);
- result.get("result").unwrap_or(result)
-}
-
fn schema_methods(value: &Value) -> Vec<(String, String, String)> {
value
.get("methods")
diff --git a/tests/in_process/memory_graph_roundtrip_e2e.rs b/tests/in_process/memory_graph_roundtrip_e2e.rs
index 1946f49ada..4eeec44408 100644
--- a/tests/in_process/memory_graph_roundtrip_e2e.rs
+++ b/tests/in_process/memory_graph_roundtrip_e2e.rs
@@ -73,6 +73,7 @@
//! A large stack is also needed — `RUST_MIN_STACK=67108864` — because
//! publishing the policy touches deeply nested config types.
+use crate::rpc_harness::{ok, rpc};
use crate::env_guard::EnvVarGuard;
use crate::env_guard::env_lock;
use crate::rpc_auth::{ensure_rpc_auth, rpc_token};
@@ -209,46 +210,6 @@ async fn setup() -> Harness {
}
}
-async fn rpc(base: &str, id: i64, method: &str, params: Value) -> Value {
- let client = reqwest::Client::builder()
- .timeout(Duration::from_secs(30))
- .build()
- .expect("client");
- let url = format!("{}/rpc", base.trim_end_matches('/'));
- let response = client
- .post(&url)
- .header(AUTHORIZATION, format!("Bearer {}", rpc_token()))
- .json(&json!({
- "jsonrpc": "2.0",
- "id": id,
- "method": method,
- "params": params,
- }))
- .send()
- .await
- .unwrap_or_else(|err| panic!("POST {url} {method}: {err}"));
- assert_eq!(
- response.status(),
- StatusCode::OK,
- "HTTP transport should accept {method}"
- );
- response
- .json::<Value>()
- .await
- .unwrap_or_else(|err| panic!("json for {method}: {err}"))
-}
-
-/// Unwrap a JSON-RPC result, failing with the error body rather than a bare
-/// `None` so a driver-level refusal names itself.
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
/// Controllers wrap their payload in `{data|result}`; unwrap one level if present.
fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
let outer = ok(value, context);
diff --git a/tests/in_process/worker_b_domain_e2e.rs b/tests/in_process/worker_b_domain_e2e.rs
index f6fe20fa92..302fc92fc5 100644
--- a/tests/in_process/worker_b_domain_e2e.rs
+++ b/tests/in_process/worker_b_domain_e2e.rs
@@ -5,6 +5,7 @@
//! deterministic controller paths. External-service paths are asserted at
//! validation/config boundaries so the suite stays hermetic.
+use crate::rpc_harness::{ok, payload, rpc, schema, write_min_config};
use crate::env_guard::EnvVarGuard;
use crate::env_guard::env_lock;
use crate::rpc_auth::{ensure_rpc_auth, rpc_token};
@@ -33,32 +34,6 @@ async fn serve_rpc() -> (
(addr, join)
}
-fn write_min_config(openhuman_dir: &Path) {
- std::fs::create_dir_all(openhuman_dir).expect("create .openhuman");
- let cfg = r#"api_url = "http://127.0.0.1:9"
-default_model = "e2e-model"
-default_temperature = 0.2
-
-[secrets]
-encrypt = false
-
-[local_ai]
-enabled = false
-
-[memory]
-provider = "none"
-embedding_provider = "none"
-embedding_model = "none"
-embedding_dimensions = 0
-
-[memory_tree]
-embedding_strict = false
-"#;
- std::fs::write(openhuman_dir.join("config.toml"), cfg).expect("write config.toml");
- let _: openhuman_core::config::Config =
- toml::from_str(cfg).expect("test config must match schema");
-}
-
struct TestHarness {
_tmp: TempDir,
_guards: Vec<EnvVarGuard>,
@@ -97,65 +72,12 @@ async fn setup() -> TestHarness {
}
}
-async fn schema(rpc_base: &str) -> Value {
- let url = format!("{}/schema", rpc_base.trim_end_matches('/'));
- reqwest::get(&url)
- .await
- .unwrap_or_else(|err| panic!("GET {url}: {err}"))
- .json::<Value>()
- .await
- .expect("schema json")
-}
-
-async fn rpc(rpc_base: &str, id: i64, method: &str, params: Value) -> Value {
- let client = reqwest::Client::builder()
- .timeout(Duration::from_secs(30))
- .build()
- .expect("client");
- let url = format!("{}/rpc", rpc_base.trim_end_matches('/'));
- let response = client
- .post(&url)
- .header(AUTHORIZATION, format!("Bearer {}", rpc_token()))
- .json(&json!({
- "jsonrpc": "2.0",
- "id": id,
- "method": method,
- "params": params,
- }))
- .send()
- .await
- .unwrap_or_else(|err| panic!("POST {url} {method}: {err}"));
- assert_eq!(
- response.status(),
- StatusCode::OK,
- "HTTP transport should accept {method}"
- );
- response
- .json::<Value>()
- .await
- .unwrap_or_else(|err| panic!("json for {method}: {err}"))
-}
-
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
fn err<'a>(value: &'a Value, context: &str) -> &'a Value {
value
.get("error")
.unwrap_or_else(|| panic!("{context}: expected JSON-RPC error, got: {value}"))
}
-fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
- let result = ok(value, context);
- result.get("result").unwrap_or(result)
-}
-
fn error_message<'a>(value: &'a Value, context: &str) -> &'a str {
err(value, context)
.get("message")
diff --git a/tests/in_process/worker_c_modules_e2e.rs b/tests/in_process/worker_c_modules_e2e.rs
index 8f15423f37..977fa031e6 100644
--- a/tests/in_process/worker_c_modules_e2e.rs
+++ b/tests/in_process/worker_c_modules_e2e.rs
@@ -4,6 +4,7 @@
//! composio / threads slice and drives the real HTTP JSON-RPC router against
//! an isolated workspace. It avoids live network calls.
+use crate::rpc_harness::{ok, rpc};
use crate::env_guard::EnvVarGuard;
use crate::env_guard::env_lock;
use crate::rpc_auth::{ensure_rpc_auth, rpc_token};
@@ -97,44 +98,6 @@ async fn setup() -> Harness {
}
}
-async fn rpc(base: &str, id: i64, method: &str, params: Value) -> Value {
- let client = reqwest::Client::builder()
- .timeout(Duration::from_secs(30))
- .build()
- .expect("client");
- let url = format!("{}/rpc", base.trim_end_matches('/'));
- let response = client
- .post(&url)
- .header(AUTHORIZATION, format!("Bearer {}", rpc_token()))
- .json(&json!({
- "jsonrpc": "2.0",
- "id": id,
- "method": method,
- "params": params,
- }))
- .send()
- .await
- .unwrap_or_else(|err| panic!("POST {url} {method}: {err}"));
- assert_eq!(
- response.status(),
- StatusCode::OK,
- "HTTP transport should accept {method}"
- );
- response
- .json::<Value>()
- .await
- .unwrap_or_else(|err| panic!("json for {method}: {err}"))
-}
-
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
let outer = ok(value, context);
outer
diff --git a/tests/raw_coverage/inference_provider_auth_e2e.rs b/tests/raw_coverage/inference_provider_auth_e2e.rs
index b5b58edb5e..78b26a2350 100644
--- a/tests/raw_coverage/inference_provider_auth_e2e.rs
+++ b/tests/raw_coverage/inference_provider_auth_e2e.rs
@@ -27,6 +27,7 @@
//! Env is process-global and every aggregated suite shares one process, so
//! each case takes the **crate-wide** [`env_lock`] for its whole body.
+use crate::rpc_harness::{ok, payload};
use crate::env_guard::EnvVarGuard;
use std::net::SocketAddr;
use std::path::Path;
@@ -206,15 +207,6 @@ async fn setup(extra: &str) -> TestHarness {
}
}
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
fn err_message(value: &Value, context: &str) -> String {
let error = value
.get("error")
@@ -226,11 +218,6 @@ fn err_message(value: &Value, context: &str) -> String {
.to_string()
}
-fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
- let result = ok(value, context);
- result.get("result").unwrap_or(result)
-}
-
/// Write an executable stub `claude` and return its path. `body` is a POSIX
/// shell script; the caller decides what `--version` prints and what the exit
/// status is.
diff --git a/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs b/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs
index 95a8eeb000..c02d6426e7 100644
--- a/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/tool_registry_approval_raw_coverage_e2e.rs
@@ -5,6 +5,7 @@
//! direct public API calls cover persistence/redaction/provider branches that
//! are otherwise only indirectly reachable from the controllers.
+use crate::rpc_harness::{error_message, ok, payload};
use crate::env_guard::EnvVarGuard;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
@@ -189,28 +190,6 @@ async fn rpc(rpc_base: &str, id: i64, method: &str, params: Value) -> Value {
.unwrap_or_else(|err| panic!("json for {method}: {err}"))
}
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
-fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
- let result = ok(value, context);
- result.get("result").unwrap_or(result)
-}
-
-fn error_message<'a>(value: &'a Value, context: &str) -> &'a str {
- value
- .get("error")
- .and_then(|error| error.get("message"))
- .and_then(Value::as_str)
- .unwrap_or_else(|| panic!("{context}: error missing message: {value}"))
-}
-
fn test_mcp_server() -> InstalledServer {
InstalledServer {
server_id: format!("tool-registry-test-{}", uuid::Uuid::new_v4()),
diff --git a/tests/raw_coverage/worker_b_raw_coverage_e2e.rs b/tests/raw_coverage/worker_b_raw_coverage_e2e.rs
index 3e3780e8a9..2817ed37f1 100644
--- a/tests/raw_coverage/worker_b_raw_coverage_e2e.rs
+++ b/tests/raw_coverage/worker_b_raw_coverage_e2e.rs
@@ -5,6 +5,7 @@
//! to drive implementation branches that the controller reachability tests only
//! touch at validation boundaries.
+use crate::rpc_harness::{error_message, ok, payload};
use crate::env_guard::EnvVarGuard;
use std::collections::HashMap;
use std::net::SocketAddr;
@@ -311,28 +312,6 @@ async fn rpc(rpc_base: &str, id: i64, method: &str, params: Value) -> Value {
.unwrap_or_else(|err| panic!("json for {method}: {err}"))
}
-fn ok<'a>(value: &'a Value, context: &str) -> &'a Value {
- if let Some(error) = value.get("error") {
- panic!("{context}: unexpected JSON-RPC error: {error}");
- }
- value
- .get("result")
- .unwrap_or_else(|| panic!("{context}: missing result: {value}"))
-}
-
-fn payload<'a>(value: &'a Value, context: &str) -> &'a Value {
- let result = ok(value, context);
- result.get("result").unwrap_or(result)
-}
-
-fn error_message<'a>(value: &'a Value, context: &str) -> &'a str {
- value
- .get("error")
- .and_then(|error| error.get("message"))
- .and_then(Value::as_str)
- .unwrap_or_else(|| panic!("{context}: error missing message: {value}"))
-}
-
async fn configure_mock_provider(rpc_base: &str, mock_base: &str) {
let update = rpc(
rpc_base,