Skip to content

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

test(channels): add linked telegram runtime fixture for policy tests

Steven Enamakel696c078eb292
diff --git a/crates/openhuman-core/src/channels/runtime/startup.rs b/crates/openhuman-core/src/channels/runtime/startup.rs
index 73866529f5..9b30919022 100644
--- a/crates/openhuman-core/src/channels/runtime/startup.rs
+++ b/crates/openhuman-core/src/channels/runtime/startup.rs
@@ -155,6 +155,39 @@ pub use start_channels::start_channels;
pub(crate) use start_channels::start_channels_with_session;
pub(crate) use turn_parts::{build_channel_turn_parts, runtime_context, PromptToolDescs};
+/// Owns a real module-backed channel runtime for channel policy tests.
+///
+/// The module remains leased until the guard is dropped, so calls made through
+/// its channel handles exercise the same TinyBus adapter used by production.
+#[cfg(test)]
+pub(crate) struct ModuleRuntimeTestGuard(module_providers::ModuleChannelRuntime);
+
+#[cfg(test)]
+impl ModuleRuntimeTestGuard {
+ pub(crate) fn channels(
+ &self,
+ ) -> std::sync::Arc<std::collections::HashMap<String, std::sync::Arc<ChannelHandle>>> {
+ self.0.channels()
+ }
+}
+
+/// Prepare a linked TinyChannels runtime for a host policy test.
+#[cfg(test)]
+pub(crate) async fn prepare_module_runtime_for_test(
+ config: std::sync::Arc<Config>,
+ dispatch: tokio::sync::mpsc::Sender<crate::channels::runtime::dispatch::RuntimeChannelMessage>,
+) -> Result<ModuleRuntimeTestGuard> {
+ let context = std::sync::Arc::new(std::sync::OnceLock::new());
+ let runtime = module_providers::ModuleChannelRuntime::prepare(
+ &crate::core::runtime::DispatchCtx::host_default(),
+ config,
+ context,
+ dispatch,
+ )
+ .await?;
+ Ok(ModuleRuntimeTestGuard(runtime))
+}
+
// Re-exported at module scope (rather than left as private `pub(super)`
// items reached only through their owning submodule) so the `#[path]` test
// modules below — which share this module's scope via `super::*` — can see
diff --git a/crates/openhuman-core/src/channels/runtime/test_support.rs b/crates/openhuman-core/src/channels/runtime/test_support.rs
index bfd31ef8a0..db4ed00a1f 100644
--- a/crates/openhuman-core/src/channels/runtime/test_support.rs
+++ b/crates/openhuman-core/src/channels/runtime/test_support.rs
@@ -24,6 +24,8 @@ use tinychannels_bus::{ChannelMessage, SendMessage};
use tinychannels_runtime::Channel;
use tinyinference_llm::model::{ChatModel, ModelRequest, ModelResponse};
use tinytools::{Tool, ToolResult};
+use wiremock::matchers::method;
+use wiremock::{Mock, MockServer, ResponseTemplate};
#[derive(Debug, Clone)]
pub struct DispatchHarnessOptions {
@@ -132,6 +134,85 @@ pub struct DispatchHarnessObservation {
pub handler_had_progress: bool,
}
+/// A real linked TinyChannels runtime backed by a loopback provider endpoint.
+///
+/// Tests that need to exercise host policy together with channel operations
+/// should use this fixture rather than a synthetic `Channel` implementation.
+pub(crate) struct LinkedTelegramRuntime {
+ _env: crate::config::test_env::EnvVarGuard,
+ _storage: crate::channels::module_fixture_tests::MemoryStorageGuard,
+ _workspace: tempfile::TempDir,
+ server: MockServer,
+ runtime: crate::channels::runtime::startup::ModuleRuntimeTestGuard,
+}
+
+impl LinkedTelegramRuntime {
+ pub(crate) fn channels(
+ &self,
+ ) -> Arc<HashMap<String, Arc<crate::channels::runtime::ChannelHandle>>> {
+ self.runtime.channels()
+ }
+
+ pub(crate) async fn provider_request_paths(&self) -> Vec<String> {
+ self.server
+ .received_requests()
+ .await
+ .expect("mock provider should expose received requests")
+ .into_iter()
+ .map(|request| request.url.path().to_owned())
+ .collect()
+ }
+}
+
+/// Prepare an actual linked TinyChannels module with mocked Telegram HTTP.
+pub(crate) async fn linked_telegram_runtime(
+) -> LinkedTelegramRuntime {
+ let server = MockServer::start().await;
+ Mock::given(method("POST"))
+ .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
+ "ok": true,
+ "result": true
+ })))
+ .mount(&server)
+ .await;
+ let env = crate::config::test_env::EnvVarGuard::locked_async()
+ .await
+ .with("OPENHUMAN_TELEGRAM_BOT_API_BASE", server.uri());
+ let storage = crate::channels::module_fixture_tests::install_memory_storage();
+ crate::channels::module_fixture_tests::client().await;
+
+ let workspace = tempfile::tempdir().expect("temporary module workspace");
+ let mut config = crate::config::Config::default();
+ config.workspace_dir = workspace.path().to_path_buf();
+ config.action_dir = workspace.path().to_path_buf();
+ config.modules.allow_download = false;
+ config.modules.source = crate::config::schema::ModuleSourceMode::Linked;
+ config.channels_config.telegram = Some(tinychannels_bus::config::TelegramConfig {
+ bot_token: "token".into(),
+ chat_id: None,
+ allowed_users: vec!["42".into()],
+ stream_mode: Default::default(),
+ draft_update_interval_ms: 1,
+ silent_streaming: true,
+ mention_only: false,
+ });
+ let (dispatch, _receiver) = tokio::sync::mpsc::channel(4);
+ let runtime = crate::channels::runtime::startup::prepare_module_runtime_for_test(
+ Arc::new(config),
+ dispatch,
+ )
+ .await
+ .expect("linked TinyChannels runtime should prepare");
+
+ LinkedTelegramRuntime {
+ _env: env,
+ _storage: storage,
+ _workspace: workspace,
+ server,
+ runtime,
+ }
+}
+
#[derive(Default)]
struct HarnessState {
sends: tokio::sync::Mutex<Vec<ObservedSend>>,