Skip to content

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

test(channels): scope telegram runtime tests to core context

Steven Enamakel0164bd04e94e
diff --git a/crates/openhuman-core/src/channels/routes_tests_telegram.rs b/crates/openhuman-core/src/channels/routes_tests_telegram.rs
index c8fc5a98bc..52a676ed93 100644
--- a/crates/openhuman-core/src/channels/routes_tests_telegram.rs
+++ b/crates/openhuman-core/src/channels/routes_tests_telegram.rs
@@ -5,97 +5,110 @@ 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 channel_state = Arc::new(TestChannel::new("recording"));
- let channel = Arc::new(ChannelHandle::Test(Arc::clone(&channel_state)));
- let sender_key = "telegram_alice_chat-remote";
+ let core =
+ crate::channels::runtime::test_support::bind_test_core_context(&mut ctx, linked.config());
+ crate::core::runtime::CoreContext::scope(core, async {
+ 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";
- let new_msg = ChannelMessage {
- reply_to_id: None,
- id: "1".into(),
- sender: "alice".into(),
- reply_target: "chat-remote".into(),
- content: "/new".into(),
- channel: "telegram".into(),
- timestamp: 0,
- thread_ts: None,
- topic_id: Some("42".into()),
- sender_name: None,
- };
- assert!(handle_linked_runtime_command(&mut ctx, &new_msg, Some(&channel), &linked, None).await);
- set_route_selection(
- &ctx,
- sender_key,
- ChannelRouteSelection {
- provider: "anthropic".into(),
- model: "claude-3".into(),
- },
- );
-
- let subscriber = ChannelTurnStateSubscriber::new(workspace.clone());
- subscriber
- .handle(&DomainEvent::ChannelMessageReceived {
- channel: "telegram".into(),
- message_id: "2".into(),
+ let new_msg = ChannelMessage {
+ reply_to_id: None,
+ id: "1".into(),
sender: "alice".into(),
reply_target: "chat-remote".into(),
- content: "work".into(),
+ content: "/new".into(),
+ channel: "telegram".into(),
+ timestamp: 0,
thread_ts: None,
- inbound_envelope: None,
- workspace_dir: workspace.clone(),
- })
- .await;
+ topic_id: Some("42".into()),
+ sender_name: None,
+ };
+ assert!(
+ handle_linked_runtime_command(&mut ctx, &new_msg, Some(&channel), &linked, None).await
+ );
+ set_route_selection(
+ &ctx,
+ sender_key,
+ ChannelRouteSelection {
+ provider: "anthropic".into(),
+ model: "claude-3".into(),
+ },
+ );
- let status_msg = ChannelMessage {
- reply_to_id: None,
- id: "3".into(),
- sender: "alice".into(),
- reply_target: "chat-remote".into(),
- content: "/status".into(),
- channel: "telegram".into(),
- timestamp: 0,
- thread_ts: None,
- topic_id: Some("42".into()),
- sender_name: None,
- };
- assert!(
- handle_linked_runtime_command(&mut ctx, &status_msg, Some(&channel), &linked, None).await
- );
+ let subscriber = ChannelTurnStateSubscriber::new(workspace.clone());
+ subscriber
+ .handle(&DomainEvent::ChannelMessageReceived {
+ channel: "telegram".into(),
+ message_id: "2".into(),
+ sender: "alice".into(),
+ reply_target: "chat-remote".into(),
+ content: "work".into(),
+ thread_ts: None,
+ inbound_envelope: None,
+ workspace_dir: workspace.clone(),
+ })
+ .await;
- let sessions_msg = ChannelMessage {
- reply_to_id: None,
- id: "4".into(),
- sender: "alice".into(),
- reply_target: "chat-remote".into(),
- content: "/sessions".into(),
- channel: "telegram".into(),
- timestamp: 0,
- thread_ts: None,
- topic_id: Some("42".into()),
- sender_name: None,
- };
- assert!(
- handle_linked_runtime_command(&mut ctx, &sessions_msg, Some(&channel), &linked, None).await
- );
+ let status_msg = ChannelMessage {
+ reply_to_id: None,
+ id: "3".into(),
+ sender: "alice".into(),
+ reply_target: "chat-remote".into(),
+ content: "/status".into(),
+ channel: "telegram".into(),
+ timestamp: 0,
+ thread_ts: None,
+ topic_id: Some("42".into()),
+ sender_name: None,
+ };
+ assert!(
+ handle_linked_runtime_command(&mut ctx, &status_msg, Some(&channel), &linked, None)
+ .await
+ );
- let sent = channel_state.sent.lock().unwrap();
- assert_eq!(sent.len(), 3);
- assert!(sent[0].content.contains("Started new session"));
- assert!(sent[0]
- .content
- .contains("Durable conversation history reset for this chat."));
+ let sessions_msg = ChannelMessage {
+ reply_to_id: None,
+ id: "4".into(),
+ sender: "alice".into(),
+ reply_target: "chat-remote".into(),
+ content: "/sessions".into(),
+ channel: "telegram".into(),
+ timestamp: 0,
+ thread_ts: None,
+ topic_id: Some("42".into()),
+ sender_name: None,
+ };
+ assert!(
+ handle_linked_runtime_command(&mut ctx, &sessions_msg, Some(&channel), &linked, None)
+ .await
+ );
- assert!(sent[1].content.contains("**Status**"));
- assert!(sent[1].content.contains("Thread: `Telegram"));
- assert!(sent[1].content.contains("Provider: `anthropic`"));
- assert!(sent[1].content.contains("Model: `claude-3`"));
- assert!(sent[1].content.contains("Session turns: 0"));
- assert!(sent[1].content.contains("Turn: in progress"));
+ let sent = channel_state.sent.lock().unwrap();
+ assert_eq!(sent.len(), 3);
+ assert!(sent[0].content.contains("Started new session"));
+ assert!(sent[0]
+ .content
+ .contains("Durable conversation history reset for this chat."));
- assert!(sent[2].content.contains("**Recent sessions**"));
- assert!(sent[2].content.contains("→ `Telegram"));
- assert!(sent
- .iter()
- .all(|message| message.topic_id.as_deref() == Some("42")));
+ assert!(sent[1].content.contains("**Status**"));
+ assert!(sent[1].content.contains("Thread: `Telegram"));
+ assert!(sent[1].content.contains("Provider: `anthropic`"));
+ assert!(sent[1].content.contains("Model: `claude-3`"));
+ assert!(sent[1].content.contains("Session turns: 0"));
+ assert!(
+ sent[1].content.contains("Turn: in progress"),
+ "status should observe the received-event state in its host scope: {}",
+ sent[1].content
+ );
+
+ assert!(sent[2].content.contains("**Recent sessions**"));
+ assert!(sent[2].content.contains("→ `Telegram"));
+ assert!(sent
+ .iter()
+ .all(|message| message.topic_id.as_deref() == Some("42")));
+ })
+ .await;
}
#[tokio::test]
diff --git a/crates/openhuman-core/src/channels/runtime/test_support.rs b/crates/openhuman-core/src/channels/runtime/test_support.rs
index 8bb01aac51..17decb78d3 100644
--- a/crates/openhuman-core/src/channels/runtime/test_support.rs
+++ b/crates/openhuman-core/src/channels/runtime/test_support.rs
@@ -286,14 +286,17 @@ pub(crate) fn bind_test_core_context(
config.workspace_dir = workspace.clone();
config.action_dir = workspace.clone();
config.config_path = workspace.join("config.toml");
- config.cloud_providers = vec![crate::config::schema::CloudProviderCreds {
- id: "p_openai_channel_test".into(),
- slug: "openai".into(),
- label: "Channel test provider".into(),
- endpoint: "http://127.0.0.1:9/v1".into(),
- default_model: Some("channel-test-model".into()),
- ..Default::default()
- }];
+ config.cloud_providers = ["openai", "test-provider"]
+ .into_iter()
+ .map(|slug| crate::config::schema::CloudProviderCreds {
+ id: format!("p_{slug}_channel_test"),
+ slug: slug.into(),
+ label: "Channel test provider".into(),
+ endpoint: "http://127.0.0.1:9/v1".into(),
+ default_model: Some("channel-test-model".into()),
+ ..Default::default()
+ })
+ .collect();
context.config = Some(Arc::new(config.clone()));
let core = CoreContext::for_test(DomainSet::none(), Some(workspace)).derive_with(
ContextOverlay::new(config, DomainSet::full(), 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 74b02545e2..da88cfec81 100644
--- a/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
+++ b/crates/openhuman-core/src/channels/tests/runtime_dispatch.rs
@@ -3,7 +3,8 @@ use super::super::runtime::test_support::{
linked_telegram_runtime, run_dispatch_harness, DispatchHarnessOptions,
};
use super::super::runtime::{
- process_channel_message, run_message_dispatch_loop, RuntimeChannelMessage,
+ process_channel_message, process_channel_runtime_message, run_message_dispatch_loop,
+ RuntimeChannelMessage,
};
use super::common::{use_real_agent_handler, RecordingChannel, SlowModel};
use crate::agent::bus::{mock_agent_run_turn, AgentTurnRequest, AgentTurnResponse};
@@ -129,18 +130,11 @@ async fn message_dispatch_processes_messages_in_parallel() {
async fn process_channel_message_sends_typing_before_the_provider_reply() {
let _bus_guard = use_real_agent_handler().await;
let linked = linked_telegram_runtime().await;
- let channel = linked
- .channels()
- .get("telegram")
- .expect("linked TinyChannels fixture should expose Telegram")
- .clone();
-
- let mut channels_by_name = HashMap::new();
- channels_by_name.insert("telegram".to_string(), channel);
+ let channels_by_name = linked.channels();
let mut runtime_ctx = ChannelRuntimeContext {
relay_thread: None,
- channels_by_name: Arc::new(channels_by_name),
+ channels_by_name,
module_objects: linked.module_objects(),
turn_model_source: Some(crate::agent::tinyagents::TurnModelSource::from_model(
Arc::new(SlowModel {
@@ -171,20 +165,72 @@ async fn process_channel_message_sends_typing_before_the_provider_reply() {
linked.config(),
);
let runtime_ctx = Arc::new(runtime_ctx);
-
- process_channel_message(
+ let configured_account = runtime_ctx
+ .channels_by_name
+ .keys()
+ .find(|name| tinychannels_bus::provider_id(name) == "telegram")
+ .cloned()
+ .expect("linked Telegram should have a configured account identity");
+ let message = traits::ChannelMessage {
+ reply_to_id: None,
+ topic_id: None,
+ id: "typing-msg".to_string(),
+ sender: "alice".to_string(),
+ reply_target: "42".to_string(),
+ content: "hello".to_string(),
+ channel: "telegram".to_string(),
+ timestamp: 1,
+ thread_ts: None,
+ sender_name: None,
+ };
+ let envelope = tinychannels_bus::ChannelInboundEnvelope {
+ channel: tinychannels_bus::ChannelRef {
+ id: "telegram".into(),
+ account_id: Some(configured_account),
+ },
+ message_id: message.id.clone(),
+ conversation: tinychannels_bus::ConversationRef {
+ kind: tinychannels_bus::ConversationKind::Dm,
+ id: message.reply_target.clone(),
+ ..Default::default()
+ },
+ sender: tinychannels_bus::SenderRef {
+ id: message.sender.clone(),
+ name: message.sender_name.clone(),
+ ..Default::default()
+ },
+ timestamp_unix_ms: Some(message.timestamp * 1000),
+ text: message.content.clone(),
+ parts: vec![tinychannels_bus::ContentPart::Text {
+ text: message.content.clone(),
+ markdown_dialect: None,
+ }],
+ ..Default::default()
+ };
+ let prepared = crate::modules::channel_controller::prepare_inbound(
+ &crate::channels::context::module_client(&runtime_ctx).await,
+ tinychannels_bus::module_inbound::PrepareChannelInboundRequest {
+ source: tinychannels_bus::module_inbound::ChannelInboundSource::Envelope {
+ envelope,
+ timestamp: message.timestamp,
+ },
+ namespace: "main".into(),
+ session_policy: Default::default(),
+ preparation_mode: Default::default(),
+ },
+ )
+ .await
+ .expect("linked module should prepare the trusted configured channel input");
+ process_channel_runtime_message(
runtime_ctx,
- traits::ChannelMessage {
- reply_to_id: None,
- topic_id: None,
- id: "typing-msg".to_string(),
- sender: "alice".to_string(),
- reply_target: "42".to_string(),
- content: "hello".to_string(),
- channel: "telegram".to_string(),
- timestamp: 1,
- thread_ts: None,
- sender_name: None,
+ RuntimeChannelMessage {
+ message: prepared.message.clone(),
+ inbound_envelope: Some(prepared.envelope.clone()),
+ authenticated_relay: false,
+ prepared_inbound: Some(prepared),
+ imported_history: Vec::new(),
+ legacy_history: None,
+ progressive_target: None,
},
)
.await;