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
chore(tests): add support files for test environment and RPC authentication
Steven Enamakelc51fd8b25468
diff --git a/tests/support/env_guard.rs b/tests/support/env_guard.rsnew file mode 100644index 0000000000..6791f0b79f--- /dev/null+++ b/tests/support/env_guard.rs@@ -0,0 +1,81 @@+//! Process-env guard and lock shared by the integration targets.+//!+//! [`EnvVarGuard`] sets (or removes) one variable and restores the previous+//! value on drop. It does **not** serialize anything by itself: process env is+//! global, so a test that mutates it must hold [`env_lock`] (or, inside+//! `raw_coverage_all`, the aggregate's `SHARED_ENV_LOCK`) for as long as the+//! guards live, in the order lock first, guards second.+//!+//! Include with `#[path = "support/env_guard.rs"] mod env_guard;` from a+//! standalone `tests/*.rs` target, or declare it once at the root of an+//! aggregated target and `use crate::env_guard::...` from the suites.++#![allow(dead_code)]++use std::ffi::{OsStr, OsString};+use std::path::Path;+use std::sync::{Mutex, MutexGuard, OnceLock};++/// Sets or unsets one env var for the guard's lifetime and restores the value+/// that was there before when dropped.+pub struct EnvVarGuard {+ key: &'static str,+ old: Option<OsString>,+}++impl EnvVarGuard {+ /// Set `key` to `value`.+ pub fn set(key: &'static str, value: impl AsRef<OsStr>) -> Self {+ let old = std::env::var_os(key);+ std::env::set_var(key, value.as_ref());+ Self { key, old }+ }++ /// Set `key` to a filesystem path.+ pub fn set_to_path(key: &'static str, path: &Path) -> Self {+ Self::set(key, path.as_os_str())+ }++ /// Alias of [`EnvVarGuard::set_to_path`].+ pub fn set_path(key: &'static str, path: &Path) -> Self {+ Self::set_to_path(key, path)+ }++ /// Remove `key`.+ pub fn unset(key: &'static str) -> Self {+ let old = std::env::var_os(key);+ std::env::remove_var(key);+ Self { key, old }+ }+}++impl Drop for EnvVarGuard {+ fn drop(&mut self) {+ match &self.old {+ Some(value) => std::env::set_var(self.key, value),+ None => std::env::remove_var(self.key),+ }+ }+}++static ENV_LOCK: OnceLock<Mutex<()>> = OnceLock::new();+static FILE_KEYRING_INIT: OnceLock<()> = OnceLock::new();++/// The process-wide env lock for this target. Poison is recovered so one+/// panicking test cannot wedge the rest of the binary.+pub fn env_lock() -> MutexGuard<'static, ()> {+ ENV_LOCK+ .get_or_init(|| Mutex::new(()))+ .lock()+ .unwrap_or_else(|poisoned| poisoned.into_inner())+}++/// [`env_lock`] after pinning `OPENHUMAN_KEYRING_BACKEND=file` once per+/// process (the suites that read or write secrets need the file keyring so+/// they never touch the host's real one).+pub fn env_lock_with_file_keyring() -> MutexGuard<'static, ()> {+ FILE_KEYRING_INIT.get_or_init(|| {+ std::env::set_var("OPENHUMAN_KEYRING_BACKEND", "file");+ });+ env_lock()+}diff --git a/tests/support/rpc_auth.rs b/tests/support/rpc_auth.rsnew file mode 100644index 0000000000..65a062ea47--- /dev/null+++ b/tests/support/rpc_auth.rs@@ -0,0 +1,34 @@+//! The RPC bearer for suites that serve the core router in-process.+//!+//! `core::auth::RPC_TOKEN` is a process-global `OnceLock` and `init_rpc_token`+//! is first-writer-wins, so a suite that hard-codes its own token gets a 401+//! whenever another suite sharing the process initialised first. Suites ask+//! for whatever token is actually active instead of choosing one: with no+//! `OPENHUMAN_CORE_TOKEN` in play, `init_rpc_token` mints a fresh token into a+//! per-process directory, so this adds no env mutation of its own.+//!+//! Include with `#[path = "support/rpc_auth.rs"] mod rpc_auth;` or declare it at+//! the root of an aggregated target.++#![allow(dead_code)]++use std::sync::OnceLock;++static TOKEN: OnceLock<String> = OnceLock::new();++/// Initialise the core's RPC token subsystem once and return the live token.+pub fn rpc_token() -> &'static str {+ TOKEN.get_or_init(|| {+ let dir = std::env::temp_dir().join(format!("openhuman-e2e-rpc-auth-{}", std::process::id()));+ std::fs::create_dir_all(&dir).expect("create the rpc token dir");+ openhuman_core::core::auth::init_rpc_token(&dir).expect("init the core rpc auth token");+ openhuman_core::core::auth::get_rpc_token()+ .expect("init_rpc_token leaves a token in place")+ .to_string()+ })+}++/// Make sure the token exists before a router is built or served.+pub fn ensure_rpc_auth() {+ let _ = rpc_token();+}