Skip to content

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

test(embed): add unit tests for process, presets, and seams

Steven Enamakel33f87250010d
diff --git a/crates/openhuman-embed/src/process_tests.rs b/crates/openhuman-embed/src/process_tests.rs
new file mode 100644
index 0000000000..555ce7c359
--- /dev/null
+++ b/crates/openhuman-embed/src/process_tests.rs
@@ -0,0 +1,42 @@
+use super::*;
+
+#[test]
+fn the_tokio_runtime_is_multi_threaded_and_runs_futures() {
+ let runtime = tokio_runtime().expect("runtime");
+ assert_eq!(
+ runtime.handle().runtime_flavor(),
+ tokio::runtime::RuntimeFlavor::MultiThread
+ );
+ assert_eq!(runtime.block_on(async { 41 + 1 }), 42);
+}
+
+#[test]
+fn the_worker_stack_is_sized_for_nested_turns() {
+ // The default 2 MiB worker stack overflows on a delegated sub-agent turn.
+ const { assert!(AGENT_WORKER_STACK_BYTES > 2 * 1024 * 1024) };
+ const { assert!(MAX_BLOCKING_THREADS > 0) };
+ // A deep frame on a worker must fit: 4 MiB of locals would abort on the
+ // default stack.
+ let runtime = tokio_runtime().expect("runtime");
+ let sum = runtime.block_on(async {
+ tokio::spawn(async {
+ let buf = [1u8; 4 * 1024 * 1024];
+ std::hint::black_box(&buf).iter().map(|b| *b as usize).sum::<usize>()
+ })
+ .await
+ .expect("join")
+ });
+ assert_eq!(sum, 4 * 1024 * 1024);
+}
+
+#[test]
+fn log_directory_is_unset_without_a_file_logger() {
+ // No test in this binary initialises file logging.
+ assert!(log_directory().is_none());
+ assert!(tui_log_lines().is_empty() || !tui_log_lines().is_empty());
+}
+
+#[test]
+fn shutdown_file_guard_without_a_guard_reports_nothing_taken() {
+ assert!(!shutdown_file_guard());
+}
diff --git a/crates/openhuman-embed/src/runtime/presets_tests.rs b/crates/openhuman-embed/src/runtime/presets_tests.rs
new file mode 100644
index 0000000000..6030e36289
--- /dev/null
+++ b/crates/openhuman-embed/src/runtime/presets_tests.rs
@@ -0,0 +1,92 @@
+//! Each host preset must carry the domain/service/host-kind triple that host
+//! boots with today (rpc `server/shims.rs`, tui `runner.rs`).
+
+use super::*;
+
+fn triple(builder: &RuntimeBuilder) -> (HostKind, Option<DomainSet>, Option<ServiceSet>) {
+ (builder.host_kind, builder.domains, builder.services)
+}
+
+#[test]
+fn library_is_the_plain_builder() {
+ let library = RuntimeBuilder::library();
+ let new = RuntimeBuilder::new();
+ assert_eq!(triple(&library), triple(&new));
+ assert_eq!(library.host_kind, HostKind::Library);
+ assert!(matches!(library.workspace, Workspace::Ephemeral));
+ assert_eq!(library.config_source, ConfigSource::Resolved);
+ assert!(matches!(library.token, TokenSource::EnvOrFile));
+}
+
+#[test]
+fn desktop_matches_the_embedded_server_shim() {
+ let desktop = RuntimeBuilder::desktop();
+ assert_eq!(
+ triple(&desktop),
+ (
+ HostKind::TauriShell,
+ Some(DomainSet::full()),
+ Some(ServiceSet::desktop())
+ )
+ );
+ assert!(matches!(desktop.workspace, Workspace::Inherit));
+ assert_eq!(desktop.config_source, ConfigSource::Discovered);
+ assert!(desktop.validate().is_ok(), "the preset must build as is");
+}
+
+#[test]
+fn cli_matches_the_standalone_server_shim() {
+ let cli = RuntimeBuilder::cli();
+ assert_eq!(
+ triple(&cli),
+ (
+ HostKind::detect_standalone(),
+ Some(DomainSet::full()),
+ Some(ServiceSet::desktop())
+ )
+ );
+ assert!(matches!(cli.workspace, Workspace::Inherit));
+ assert_eq!(cli.config_source, ConfigSource::Discovered);
+ assert!(matches!(cli.token, TokenSource::EnvOrFile));
+ assert!(cli.validate().is_ok());
+}
+
+#[test]
+fn tui_runs_every_domain_and_no_services() {
+ let tui = RuntimeBuilder::tui();
+ assert_eq!(
+ triple(&tui),
+ (
+ HostKind::detect_standalone(),
+ Some(DomainSet::full()),
+ Some(ServiceSet::none())
+ )
+ );
+ assert!(matches!(tui.workspace, Workspace::Inherit));
+ assert_eq!(tui.config_source, ConfigSource::Discovered);
+ assert!(tui.tool_groups.is_none(), "the TUI keeps the withheld default");
+ assert!(tui.validate().is_ok());
+}
+
+#[test]
+fn presets_stay_overridable() {
+ let headless = RuntimeBuilder::cli().services(ServiceSet::headless_api());
+ assert_eq!(headless.services, Some(ServiceSet::headless_api()));
+ let fixed = RuntimeBuilder::desktop()
+ .token(TokenSource::Fixed(std::sync::Arc::new("t".into())))
+ .listen("127.0.0.1", 7790);
+ assert!(matches!(fixed.token, TokenSource::Fixed(_)));
+ assert_eq!(fixed.listen_port, Some(7790));
+}
+
+#[test]
+fn library_defaults_start_no_background_writers() {
+ assert_eq!(
+ default_services(),
+ ServiceSet {
+ harness_init: true,
+ ..ServiceSet::none()
+ }
+ );
+ assert!(default_domains().inference);
+}
diff --git a/crates/openhuman-embed/src/runtime/seams_tests.rs b/crates/openhuman-embed/src/runtime/seams_tests.rs
new file mode 100644
index 0000000000..d0571933dd
--- /dev/null
+++ b/crates/openhuman-embed/src/runtime/seams_tests.rs
@@ -0,0 +1,195 @@
+//! Seams are process-global, so each test uses names no other test shares,
+//! and the ranker tests (one slot for the whole process) serialize on a lock.
+
+use super::*;
+use openhuman_core::agent::hooks::{ToolHookContext, TurnContext};
+use openhuman_core::agent::tinyagents::discovery::{RankCandidate, RankContext, RankError, RankHit};
+
+static RANKER_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
+
+struct NamedRanker(&'static str);
+
+#[async_trait::async_trait]
+impl ToolRanker for NamedRanker {
+ fn kind(&self) -> &'static str {
+ self.0
+ }
+ async fn rank(
+ &self,
+ _intent: &str,
+ _context: &RankContext,
+ _candidates: &[RankCandidate],
+ _limit: usize,
+ ) -> Result<Vec<RankHit>, RankError> {
+ Ok(Vec::new())
+ }
+}
+
+struct NamedPostTurn(&'static str, u8);
+
+#[async_trait::async_trait]
+impl PostTurnHook for NamedPostTurn {
+ fn name(&self) -> &str {
+ self.0
+ }
+ async fn on_turn_complete(&self, _ctx: &TurnContext) -> anyhow::Result<()> {
+ Ok(())
+ }
+}
+
+struct NamedToolHook(&'static str);
+
+#[async_trait::async_trait]
+impl ToolHook for NamedToolHook {
+ fn name(&self) -> &str {
+ self.0
+ }
+ async fn before_tool(&self, _context: &ToolHookContext) -> anyhow::Result<()> {
+ Ok(())
+ }
+ async fn after_tool(&self, _context: &ToolHookContext) -> anyhow::Result<()> {
+ Ok(())
+ }
+}
+
+fn ranker_kind() -> Option<&'static str> {
+ installed_tool_ranker().map(|ranker| ranker.kind())
+}
+
+fn post_turn_marker(name: &str) -> Option<u8> {
+ // The marker is only reachable through `Any`-free equality on the name
+ // plus a count, so count same-named hooks instead.
+ let count = embedder_post_turn_hooks()
+ .iter()
+ .filter(|hook| hook.name() == name)
+ .count();
+ u8::try_from(count).ok().filter(|count| *count > 0)
+}
+
+#[test]
+fn a_tool_ranker_is_installed_and_the_previous_one_restored() {
+ let _lock = RANKER_LOCK.lock().unwrap_or_else(|e| e.into_inner());
+ install_tool_ranker(Arc::new(NamedRanker("seams-previous")));
+
+ let seams = HostSeams {
+ tool_ranker: Some(Arc::new(NamedRanker("seams-ours"))),
+ ..HostSeams::default()
+ }
+ .install()
+ .expect("install");
+ assert_eq!(ranker_kind(), Some("seams-ours"));
+
+ drop(seams);
+ assert_eq!(ranker_kind(), Some("seams-previous"));
+ clear_tool_ranker();
+}
+
+#[test]
+fn a_tool_ranker_with_no_predecessor_is_cleared_on_drop() {
+ let _lock = RANKER_LOCK.lock().unwrap_or_else(|e| e.into_inner());
+ clear_tool_ranker();
+ let seams = HostSeams {
+ tool_ranker: Some(Arc::new(NamedRanker("seams-alone"))),
+ ..HostSeams::default()
+ }
+ .install()
+ .expect("install");
+ assert_eq!(ranker_kind(), Some("seams-alone"));
+ drop(seams);
+ assert_eq!(ranker_kind(), None);
+}
+
+#[test]
+fn a_ranker_replaced_after_build_is_left_alone() {
+ let _lock = RANKER_LOCK.lock().unwrap_or_else(|e| e.into_inner());
+ clear_tool_ranker();
+ let seams = HostSeams {
+ tool_ranker: Some(Arc::new(NamedRanker("seams-first"))),
+ ..HostSeams::default()
+ }
+ .install()
+ .expect("install");
+ install_tool_ranker(Arc::new(NamedRanker("seams-later")));
+ drop(seams);
+ assert_eq!(ranker_kind(), Some("seams-later"), "not ours any more");
+ clear_tool_ranker();
+}
+
+#[test]
+fn persisted_seams_survive_the_guard() {
+ let _lock = RANKER_LOCK.lock().unwrap_or_else(|e| e.into_inner());
+ clear_tool_ranker();
+ HostSeams {
+ tool_ranker: Some(Arc::new(NamedRanker("seams-persisted"))),
+ post_turn_hooks: vec![Arc::new(NamedPostTurn("seams-persisted-hook", 0))],
+ ..HostSeams::default()
+ }
+ .install()
+ .expect("install")
+ .persist();
+ assert_eq!(ranker_kind(), Some("seams-persisted"));
+ assert_eq!(post_turn_marker("seams-persisted-hook"), Some(1));
+ clear_tool_ranker();
+ replace_embedder_post_turn_hook("seams-persisted-hook", None);
+}
+
+#[test]
+fn post_turn_hooks_are_removed_and_a_replaced_one_restored() {
+ replace_embedder_post_turn_hook("seams-replaced-hook", Some(Arc::new(NamedPostTurn("seams-replaced-hook", 1))));
+ let seams = HostSeams {
+ post_turn_hooks: vec![
+ Arc::new(NamedPostTurn("seams-replaced-hook", 2)),
+ Arc::new(NamedPostTurn("seams-new-hook", 3)),
+ ],
+ ..HostSeams::default()
+ }
+ .install()
+ .expect("install");
+ // Replaced, not duplicated.
+ assert_eq!(post_turn_marker("seams-replaced-hook"), Some(1));
+ assert_eq!(post_turn_marker("seams-new-hook"), Some(1));
+
+ drop(seams);
+ assert_eq!(post_turn_marker("seams-new-hook"), None, "ours removed");
+ assert_eq!(post_turn_marker("seams-replaced-hook"), Some(1), "predecessor restored");
+ replace_embedder_post_turn_hook("seams-replaced-hook", None);
+}
+
+#[test]
+fn tool_hooks_are_installed_and_removed() {
+ let present = |name: &str| embedder_tool_hooks().iter().any(|hook| hook.name() == name);
+ let seams = HostSeams {
+ tool_hooks: vec![Arc::new(NamedToolHook("seams-tool-hook"))],
+ ..HostSeams::default()
+ }
+ .install()
+ .expect("install");
+ assert!(present("seams-tool-hook"));
+ drop(seams);
+ assert!(!present("seams-tool-hook"));
+}
+
+#[test]
+fn a_live_policy_waits_for_the_core_to_boot() {
+ let seams = HostSeams {
+ live_policy: Some(Arc::new(SecurityPolicy::default())),
+ ..HostSeams::default()
+ }
+ .install()
+ .expect("install");
+ assert!(seams.has_pending_live_policy(), "installed after boot, not before");
+}
+
+#[test]
+fn builder_options_collect_into_the_seams() {
+ let builder = RuntimeBuilder::new()
+ .tool_ranker(Arc::new(NamedRanker("seams-builder")))
+ .post_turn_hook(Arc::new(NamedPostTurn("seams-builder-hook", 0)))
+ .tool_hook(Arc::new(NamedToolHook("seams-builder-tool")))
+ .live_policy(Arc::new(SecurityPolicy::default()));
+ assert!(builder.seams.tool_ranker.is_some());
+ assert_eq!(builder.seams.post_turn_hooks.len(), 1);
+ assert_eq!(builder.seams.tool_hooks.len(), 1);
+ assert!(builder.seams.live_policy.is_some());
+ assert!(builder.seams.server_launcher.is_none());
+}