Skip to content

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

test(channels): share core context across linked runtime tests

Steven Enamakele072e2e91b8d
diff --git a/crates/openhuman-core/src/channels/routes_tests.rs b/crates/openhuman-core/src/channels/routes_tests.rs
index 025cdc59b2..08d7df6431 100644
--- a/crates/openhuman-core/src/channels/routes_tests.rs
+++ b/crates/openhuman-core/src/channels/routes_tests.rs
@@ -121,7 +121,13 @@ async fn handle_linked_runtime_command(
ctx.channels_by_name = linked.channels();
ctx.module_objects = linked.module_objects();
- let core = crate::channels::runtime::test_support::bind_test_core_context(ctx, linked.config());
+ let core = ctx
+ .core
+ .as_ref()
+ .and_then(std::sync::Weak::upgrade)
+ .unwrap_or_else(|| {
+ crate::channels::runtime::test_support::bind_test_core_context(ctx, linked.config())
+ });
crate::core::runtime::CoreContext::scope(core, async {
let account_id = linked
.channels()
diff --git a/crates/openhuman-core/src/channels/routes_tests_telegram.rs b/crates/openhuman-core/src/channels/routes_tests_telegram.rs
index 76eb9ed36a..0e2809ffec 100644
--- a/crates/openhuman-core/src/channels/routes_tests_telegram.rs
+++ b/crates/openhuman-core/src/channels/routes_tests_telegram.rs
@@ -5,6 +5,8 @@ async fn handle_runtime_command_telegram_new_status_and_sessions_round_trip() {
let linked = crate::channels::runtime::test_support::linked_telegram_runtime().await;
let workspace = linked.config().workspace_dir.clone();
let mut ctx = runtime_context(workspace.clone());
+ let _core =
+ crate::channels::runtime::test_support::bind_test_core_context(&mut ctx, linked.config());
let channel_state = Arc::new(TestChannel::new("recording"));
let channel = Arc::new(ChannelHandle::Test(Arc::clone(&channel_state)));
let sender_key = "telegram_alice_chat-remote";
@@ -32,8 +34,11 @@ async fn handle_runtime_command_telegram_new_status_and_sessions_round_trip() {
);
let subscriber = ChannelTurnStateSubscriber::with_config(Arc::new(linked.config().clone()));
- let event_core =
- crate::channels::runtime::test_support::bind_test_core_context(&mut ctx, linked.config());
+ let event_core = ctx
+ .core
+ .as_ref()
+ .and_then(std::sync::Weak::upgrade)
+ .expect("the linked route commands and event must share a core scope");
crate::core::runtime::CoreContext::scope(event_core, async {
subscriber
.handle(&DomainEvent::ChannelMessageReceived {
diff --git a/crates/openhuman-core/src/channels/runtime/test_support.rs b/crates/openhuman-core/src/channels/runtime/test_support.rs
index 62ecc2d9ee..a5cf997160 100644
--- a/crates/openhuman-core/src/channels/runtime/test_support.rs
+++ b/crates/openhuman-core/src/channels/runtime/test_support.rs
@@ -234,6 +234,20 @@ pub(crate) async fn linked_telegram_runtime() -> LinkedTelegramRuntime {
})))
.mount(&server)
.await;
+ Mock::given(method("POST"))
+ .and(path("/v1/chat/completions"))
+ .respond_with(ResponseTemplate::new(200).set_body_raw(
+ concat!(
+ "data: {\"choices\":[{\"index\":0,\"delta\":{\"content\":\"typing fixture reply\"}}]}\n\n",
+ "data: {\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n",
+ "data: [DONE]\n\n"
+ )
+ .as_bytes()
+ .to_vec(),
+ "text/event-stream",
+ ))
+ .mount(&server)
+ .await;
let env = crate::config::test_env::EnvVarGuard::locked_async()
.await
.with("OPENHUMAN_TELEGRAM_BOT_API_BASE", server.uri());
@@ -282,6 +296,14 @@ pub(crate) async fn linked_telegram_runtime() -> LinkedTelegramRuntime {
pub(crate) fn bind_test_core_context(
context: &mut crate::channels::context::ChannelRuntimeContext,
base_config: &crate::config::Config,
+) -> Arc<crate::core::runtime::CoreContext> {
+ bind_test_core_context_with_provider_endpoint(context, base_config, "http://127.0.0.1:9/v1")
+}
+
+pub(crate) fn bind_test_core_context_with_provider_endpoint(
+ context: &mut crate::channels::context::ChannelRuntimeContext,
+ base_config: &crate::config::Config,
+ provider_endpoint: &str,
) -> Arc<crate::core::runtime::CoreContext> {
use crate::core::runtime::{ContextOverlay, CoreContext, DomainSet};
@@ -296,7 +318,7 @@ pub(crate) fn bind_test_core_context(
id: format!("p_{slug}_channel_test"),
slug: slug.into(),
label: "Channel test provider".into(),
- endpoint: "http://127.0.0.1:9/v1".into(),
+ endpoint: provider_endpoint.into(),
default_model: Some("channel-test-model".into()),
..Default::default()
})
diff --git a/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs b/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
index 7ec8922d85..7d030521a9 100644
--- a/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
+++ b/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
@@ -136,11 +136,7 @@ async fn process_channel_message_sends_typing_before_the_provider_reply() {
relay_thread: None,
channels_by_name,
module_objects: linked.module_objects(),
- turn_model_source: Some(crate::agent::tinyagents::TurnModelSource::from_model(
- Arc::new(SlowModel {
- delay: Duration::from_millis(20),
- }),
- )),
+ turn_model_source: None,
default_provider: Arc::new("test-provider".to_string()),
tools_registry: Arc::new(vec![]),
system_prompt: crate::channels::ChannelSystemPrompt::fixed("test-system-prompt"),
@@ -160,13 +156,12 @@ async fn process_channel_message_sends_typing_before_the_provider_reply() {
config: None,
core: None,
};
- let _core = crate::channels::runtime::test_support::bind_test_core_context(
- &mut runtime_ctx,
- linked.config(),
- );
- // Keep the bound core for host policy/config while selecting the injected
- // model source below, as bespoke test hosts do.
- runtime_ctx.config = None;
+ let _core =
+ crate::channels::runtime::test_support::bind_test_core_context_with_provider_endpoint(
+ &mut runtime_ctx,
+ linked.config(),
+ &linked.provider_endpoint(),
+ );
let runtime_ctx = Arc::new(runtime_ctx);
let configured_account = runtime_ctx
.channels_by_name
@@ -255,6 +250,23 @@ async fn process_channel_message_sends_typing_before_the_provider_reply() {
1,
"the module-backed provider should receive the completed reply; got {requests:?}"
);
+ let typing_index = requests
+ .iter()
+ .position(|path| path.ends_with("/sendChatAction"))
+ .expect("typing action should precede the model reply");
+ let reply_index = requests
+ .iter()
+ .position(|path| path.ends_with("/sendMessage"))
+ .expect("provider should receive the model reply");
+ assert!(
+ typing_index < reply_index,
+ "typing should precede reply: {requests:?}"
+ );
+ let sent = linked
+ .provider_request_bodies("/bottoken/sendMessage")
+ .await;
+ assert_eq!(sent.len(), 1);
+ assert_eq!(sent[0]["text"], "typing fixture reply");
}
/// Integration test that proves channel dispatch actually routes through