The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(tests): extract shared test helpers into a dedicated module
Steven Enamakel4fef2ad9ce3f
diff --git a/tests/agent_harness_e2e.rs b/tests/agent_harness_e2e.rsindex eec5ca55f7..c734efdea2 100644--- a/tests/agent_harness_e2e.rs+++ b/tests/agent_harness_e2e.rs@@ -13,6 +13,9 @@#[path = "support/env_guard.rs"]mod env_guard;+#[path = "support/scripted_stack.rs"]+mod scripted_stack;+use scripted_stack::{assert_no_jsonrpc_error, current_user, lock_or_recover, text_completion, tool_calls_completion};use env_guard::EnvVarGuard;use std::net::SocketAddr;use std::path::Path;@@ -101,10 +104,6 @@ fn reset_script(responses: Vec<Value>) {with_captured(|c| c.clear());}-fn text_completion(content: &str) -> Value {- json!({ "content": content })-}-fn tool_call_completion(name: &str, arguments: Value) -> Value {json!({ "content": "", "toolCalls": [{"id": format!("call_{name}"),@@ -113,21 +112,6 @@ fn tool_call_completion(name: &str, arguments: Value) -> Value {}]})}-/// A completion carrying several tool calls in ONE assistant message.-///-/// Fan-out is now several `spawn_async_subagent` calls "issued together"-/// (orchestrator `prompt.md`), which on the wire is one message with several-/// entries in `toolCalls` — not several messages. [`tool_call_completion`]-/// cannot express that, and scripting them as separate completions would test-/// the serial shape the fan-out guidance exists to prevent.-fn tool_calls_completion(calls: &[(&str, Value)]) -> Value {- json!({ "content": "", "toolCalls": calls.iter().map(|(name, arguments)| json!({- "id": format!("call_{name}_{}", arguments.to_string().len()),- "name": name,- "arguments": arguments.to_string(),- })).collect::<Vec<_>>() })-}-fn error_completion(status: u16, message: &str) -> Value {json!({ "status": status, "error": message })}@@ -231,13 +215,6 @@ fn canary_in_flight() -> &'static Mutex<std::collections::HashSet<String>> {CANARY_IN_FLIGHT.get_or_init(|| Mutex::new(std::collections::HashSet::new()))}-fn lock_or_recover<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {- match m.lock() {- Ok(g) => g,- Err(p) => p.into_inner(),- }-}-/// Arms the barrier for `canaries` and clears any previous state.fn arm_canary_barrier(canaries: &[&str]) {*lock_or_recover(canary_barrier()) = canaries.iter().map(|c| (*c).to_string()).collect();@@ -431,10 +408,6 @@ fn completion_response(streaming: bool, message: Value) -> axum::response::Respo.into_response()}-async fn current_user(_headers: HeaderMap) -> Json<Value> {- Json(json!({ "success": true, "data": { "_id": "e2e-user-1", "username": "e2e" } }))-}-fn scripted_upstream_router() -> Router {Router::new().route("/settings", get(current_user))@@ -496,14 +469,6 @@ async fn post_json_rpc(rpc_base: &str, id: i64, method: &str, params: Value) ->.unwrap_or_else(|e| panic!("json for {method}: {e}"))}-fn assert_no_jsonrpc_error<'a>(v: &'a Value, context: &str) -> &'a Value {- if let Some(err) = v.get("error") {- panic!("{context}: JSON-RPC error: {err}");- }- v.get("result")- .unwrap_or_else(|| panic!("{context}: missing result: {v}"))-}-/// `extra_config` is appended verbatim (whole TOML tables, e.g. `[autonomy]`).fn write_min_config(openhuman_dir: &Path, api_origin: &str, extra_config: &str) {let cfg = format!(diff --git a/tests/in_process/agent_prompt_comprehension_e2e.rs b/tests/in_process/agent_prompt_comprehension_e2e.rsindex 3567784a24..4299838b2f 100644--- a/tests/in_process/agent_prompt_comprehension_e2e.rs+++ b/tests/in_process/agent_prompt_comprehension_e2e.rs@@ -16,6 +16,7 @@//! `env_lock()` across `.await` on purpose, as there.#![allow(clippy::await_holding_lock)]+use crate::scripted_stack::{assert_no_jsonrpc_error, current_user, lock_or_recover, text_completion, tool_calls_completion};use crate::env_guard::EnvVarGuard;use crate::rpc_auth::{ensure_rpc_auth, rpc_token};use crate::env_guard::env_lock_with_file_keyring as env_lock;@@ -44,13 +45,6 @@ static AGENT_DEF_REGISTRY_INIT: OnceLock<()> = OnceLock::new();static SCRIPTED: OnceLock<Mutex<std::collections::VecDeque<Value>>> = OnceLock::new();static CAPTURED: OnceLock<Mutex<Vec<Value>>> = OnceLock::new();-fn lock_or_recover<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {- match m.lock() {- Ok(g) => g,- Err(p) => p.into_inner(),- }-}-fn scripted() -> std::sync::MutexGuard<'static, std::collections::VecDeque<Value>> {lock_or_recover(SCRIPTED.get_or_init(Default::default))}@@ -66,20 +60,6 @@ fn reset_script(responses: Vec<Value>) {captured().clear();}-fn text_completion(content: &str) -> Value {- json!({ "content": content })-}--/// One tool call, id'd `call_<name>_<arglen>` so repeats of the same tool with-/// different arguments stay distinguishable (from `agent_harness_e2e.rs`).-fn tool_calls_completion(calls: &[(&str, Value)]) -> Value {- json!({ "content": "", "toolCalls": calls.iter().map(|(name, arguments)| json!({- "id": format!("call_{name}_{}", arguments.to_string().len()),- "name": name,- "arguments": arguments.to_string(),- })).collect::<Vec<_>>() })-}-fn call(name: &str, arguments: Value) -> Value {tool_calls_completion(&[(name, arguments)])}@@ -268,10 +248,6 @@ fn completion_response(streaming: bool, message: Value) -> axum::response::Respo.into_response()}-async fn current_user(_headers: HeaderMap) -> Json<Value> {- Json(json!({ "success": true, "data": { "_id": "e2e-user-1", "username": "e2e" } }))-}-/// One connected Gmail toolkit, so the orchestrator gets its actions as a/// searchable catalogue and the integrations agent has a toolkit to bind to./// Shapes from `tools_approval_channels_raw_coverage_e2e.rs`.@@ -359,14 +335,6 @@ async fn post_json_rpc(rpc_base: &str, id: i64, method: &str, params: Value) ->.unwrap_or_else(|e| panic!("json for {method}: {e}"))}-fn assert_no_jsonrpc_error<'a>(v: &'a Value, context: &str) -> &'a Value {- if let Some(err) = v.get("error") {- panic!("{context}: JSON-RPC error: {err}");- }- v.get("result")- .unwrap_or_else(|| panic!("{context}: missing result: {v}"))-}-/// `extra` is appended verbatim, for per-case `[context]` knobs.fn write_min_config(openhuman_dir: &Path, api_origin: &str, extra: &str) {// `compaction_enabled = false`: the builder's `propose_workflow` result isdiff --git a/tests/in_process_all.rs b/tests/in_process_all.rsindex baf4285558..b01896350c 100644--- a/tests/in_process_all.rs+++ b/tests/in_process_all.rs@@ -35,6 +35,8 @@ pub mod noop_memory;pub mod rpc_auth;#[path = "support/rpc_harness.rs"]pub mod rpc_harness;+#[path = "support/scripted_stack.rs"]+pub mod scripted_stack;#[path = "support/tinyhumans_boot.rs"]pub mod tinyhumans_boot;diff --git a/tests/json_rpc_e2e.rs b/tests/json_rpc_e2e.rsindex 640fa92715..a065171877 100644--- a/tests/json_rpc_e2e.rs+++ b/tests/json_rpc_e2e.rs@@ -10,6 +10,9 @@ mod tinyhumans_boot;#[path = "support/env_guard.rs"]mod env_guard;+#[path = "support/scripted_stack.rs"]+mod scripted_stack;+use scripted_stack::{assert_no_jsonrpc_error};use env_guard::EnvVarGuard;use std::collections::VecDeque;use std::net::SocketAddr;@@ -1091,14 +1094,6 @@ async fn encrypt_test_mnemonic() -> String {.value}-fn assert_no_jsonrpc_error<'a>(v: &'a Value, context: &str) -> &'a Value {- if let Some(err) = v.get("error") {- panic!("{context}: JSON-RPC error: {err}");- }- v.get("result")- .unwrap_or_else(|| panic!("{context}: missing result: {v}"))-}-fn assert_jsonrpc_error<'a>(v: &'a Value, context: &str) -> &'a Value {v.get("error").unwrap_or_else(|| panic!("{context}: expected JSON-RPC error, got: {v}"))diff --git a/tests/live_flows_demo_e2e.rs b/tests/live_flows_demo_e2e.rsindex 50170d8ddb..f12ff4b89c 100644--- a/tests/live_flows_demo_e2e.rs+++ b/tests/live_flows_demo_e2e.rs@@ -21,6 +21,9 @@#[path = "support/env_guard.rs"]mod env_guard;+#[path = "support/scripted_stack.rs"]+mod scripted_stack;+use scripted_stack::{assert_no_jsonrpc_error};use env_guard::EnvVarGuard;use std::path::Path;use std::sync::{Mutex, OnceLock};@@ -98,14 +101,6 @@ async fn post_json_rpc(rpc_base: &str, id: i64, method: &str, params: Value) ->resp.json::<Value>().await.expect("rpc json body")}-fn assert_no_jsonrpc_error<'a>(v: &'a Value, context: &str) -> &'a Value {- if let Some(err) = v.get("error") {- panic!("{context}: JSON-RPC error: {err}");- }- v.get("result")- .unwrap_or_else(|| panic!("{context}: missing result: {v}"))-}-/// Peel the `{ "result": inner, "logs": [...] }` envelope that flows ops add.fn peel_logs_envelope(v: &Value) -> &Value {if v.get("logs").is_some() {diff --git a/tests/live_routing_e2e.rs b/tests/live_routing_e2e.rsindex 667f0c8a4b..b2cb351681 100644--- a/tests/live_routing_e2e.rs+++ b/tests/live_routing_e2e.rs@@ -13,6 +13,9 @@#[path = "support/env_guard.rs"]mod env_guard;+#[path = "support/scripted_stack.rs"]+mod scripted_stack;+use scripted_stack::{assert_no_jsonrpc_error};use env_guard::EnvVarGuard;use std::path::Path;use std::sync::{Mutex, OnceLock};@@ -137,14 +140,6 @@ async fn read_sse_event_by_types(events_url: &str, target_events: &[&str]) -> Vapanic!("SSE stream ended before receiving any target event: {target_events:?}");}-fn assert_no_jsonrpc_error<'a>(v: &'a Value, context: &str) -> &'a Value {- if let Some(err) = v.get("error") {- panic!("{context}: JSON-RPC error: {err}");- }- v.get("result")- .unwrap_or_else(|| panic!("{context}: missing result: {v}"))-}-async fn serve_rpc() -> (std::net::SocketAddr, tokio::task::JoinHandle<()>) {ensure_test_rpc_auth();let app = build_core_http_router(false);diff --git a/tests/support/scripted_stack.rs b/tests/support/scripted_stack.rsnew file mode 100644index 0000000000..d5126d5470--- /dev/null+++ b/tests/support/scripted_stack.rs@@ -0,0 +1,54 @@+//! Small pieces of the scripted-upstream harness shared by the agent e2e+//! targets (`agent_harness_e2e`, `in_process/agent_prompt_comprehension_e2e`):+//! the mock backend's completion shapes, the `/auth/me` stub, and the+//! JSON-RPC result unwrap they all use.+//!+//! The scripted router itself (queue, capture, SSE collector, boot stack) stays+//! in each suite: the two differ in how they script and capture requests.+//!+//! Include with `#[path = "support/scripted_stack.rs"] mod scripted_stack;` or+//! declare it at the root of an aggregated target.++#![allow(dead_code)]++use std::sync::{Mutex, MutexGuard};++use axum::http::HeaderMap;+use axum::Json;+use serde_json::{json, Value};++/// Lock `m`, recovering the data when a panicking test poisoned it.+pub fn lock_or_recover<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {+ match m.lock() {+ Ok(g) => g,+ Err(p) => p.into_inner(),+ }+}++/// A plain-text assistant completion for the scripted upstream.+pub fn text_completion(content: &str) -> Value {+ json!({ "content": content })+}++/// One completion carrying several parallel tool calls in `toolCalls`.+pub fn tool_calls_completion(calls: &[(&str, Value)]) -> Value {+ json!({ "content": "", "toolCalls": calls.iter().map(|(name, arguments)| json!({+ "id": format!("call_{name}_{}", arguments.to_string().len()),+ "name": name,+ "arguments": arguments.to_string(),+ })).collect::<Vec<_>>() })+}++/// The `/auth/me` stub the mock backend serves.+pub async fn current_user(_headers: HeaderMap) -> Json<Value> {+ Json(json!({ "success": true, "data": { "_id": "e2e-user-1", "username": "e2e" } }))+}++/// The envelope's `result`; panics with `context` on a JSON-RPC error.+pub fn assert_no_jsonrpc_error<'a>(v: &'a Value, context: &str) -> &'a Value {+ if let Some(err) = v.get("error") {+ panic!("{context}: JSON-RPC error: {err}");+ }+ v.get("result")+ .unwrap_or_else(|| panic!("{context}: missing result: {v}"))+}