Skip to content

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

feat(web_chat): add tests for channel events and surface handling

Steven Enamakel06d6ebb5f7cb
diff --git a/crates/openhuman-core/src/web_chat/channel_event_tests.rs b/crates/openhuman-core/src/web_chat/channel_event_tests.rs
index 8b13789179..5564e7d290 100644
--- a/crates/openhuman-core/src/web_chat/channel_event_tests.rs
+++ b/crates/openhuman-core/src/web_chat/channel_event_tests.rs
@@ -1 +1,107 @@
+use super::*;
+use tinychannels_bus::channel::{ContentPart, OutboundContentPart};
+fn context() -> bus_web::WebEventContext {
+ bus_web::WebEventContext {
+ client_id: "c1".into(),
+ thread_id: "t1".into(),
+ request_id: "r1".into(),
+ seq: Some(4),
+ ts: Some(99),
+ ..Default::default()
+ }
+}
+
+#[test]
+fn a_typed_event_becomes_the_flat_form_with_the_same_wire_fields() {
+ let flat = WebChannelEvent::from(bus_web::WebChannelEvent::ChatDone {
+ context: context(),
+ full_response: Some("hello".into()),
+ citations: Some(serde_json::json!([{ "id": 1 }])),
+ usage: Some(TurnUsagePayload {
+ input_tokens: 3,
+ ..Default::default()
+ }),
+ timing: Some(TurnTimingPayload {
+ total_ms: Some(10),
+ ..Default::default()
+ }),
+ parts: None,
+ });
+ assert_eq!(flat.event, "chat_done");
+ assert_eq!(
+ (flat.client_id.as_str(), flat.thread_id.as_str(), flat.request_id.as_str()),
+ ("c1", "t1", "r1")
+ );
+ assert_eq!((flat.seq, flat.ts), (Some(4), Some(99)));
+ assert_eq!(flat.full_response.as_deref(), Some("hello"));
+ assert_eq!(flat.usage.expect("usage").input_tokens, 3);
+ assert_eq!(flat.timing.expect("timing").total_ms, Some(10));
+ assert!(flat.citations.is_some());
+ // Tenant stamps are the host's, never the wire's.
+ assert!(flat.agent.is_none() && flat.profile.is_none());
+}
+
+#[test]
+fn a_null_argument_payload_and_an_unnamed_tool_are_absent_not_empty() {
+ let call = WebChannelEvent::from(bus_web::WebChannelEvent::ToolCall {
+ context: context(),
+ tool_name: Some("shell".into()),
+ skill_id: None,
+ args: Some(serde_json::Value::Null),
+ round: Some(1),
+ tool_call_id: Some("call".into()),
+ tool_display_label: None,
+ tool_display_detail: None,
+ });
+ assert_eq!(call.event, "tool_call");
+ assert!(call.args.is_none());
+ assert_eq!(call.tool_name.as_deref(), Some("shell"));
+
+ let delta = WebChannelEvent::from(bus_web::WebChannelEvent::ToolArgsDelta {
+ context: context(),
+ delta: Some("{".into()),
+ delta_kind: Some("tool_args".into()),
+ tool_call_id: Some("call".into()),
+ tool_name: Some(String::new()),
+ skill_id: None,
+ round: Some(1),
+ });
+ assert_eq!(delta.event, "tool_args_delta");
+ assert!(delta.tool_name.is_none());
+ assert_eq!(delta.tool_call_id.as_deref(), Some("call"));
+}
+
+#[test]
+fn typed_content_parts_ride_the_flat_form_to_the_wire() {
+ let flat = WebChannelEvent::from(bus_web::WebChannelEvent::ChatMessage {
+ context: context(),
+ message: None,
+ full_response: Some("look".into()),
+ parts: Some(vec![OutboundContentPart::Content {
+ part: ContentPart::Text {
+ text: "look".into(),
+ markdown_dialect: None,
+ },
+ }]),
+ });
+ assert_eq!(flat.event, "chat_message");
+ let wire = serde_json::to_value(&flat).expect("serializes");
+ assert_eq!(wire["parts"][0]["part"]["text"], "look");
+}
+
+#[test]
+fn tenant_stamps_route_but_never_serialize() {
+ let mut event = WebChannelEvent {
+ event: "chat_done".into(),
+ ..Default::default()
+ };
+ event.stamp_tenant(Some(crate::core::runtime::Tenant {
+ profile: Some("p".into()),
+ agent: Some("a".into()),
+ }));
+ assert!(event.belongs_to("a") && event.belongs_to_profile("p"));
+ assert!(!event.belongs_to("other"));
+ let wire = serde_json::to_value(&event).expect("serializes");
+ assert!(wire.get("agent").is_none() && wire.get("profile").is_none());
+}
diff --git a/crates/openhuman-core/src/web_chat/surface_tests.rs b/crates/openhuman-core/src/web_chat/surface_tests.rs
index 8b13789179..62060c29c2 100644
--- a/crates/openhuman-core/src/web_chat/surface_tests.rs
+++ b/crates/openhuman-core/src/web_chat/surface_tests.rs
@@ -1 +1,88 @@
+use super::*;
+use crate::core::runtime::DispatchCtx;
+fn ids(request_id: &str) -> TurnIds {
+ TurnIds {
+ client_id: "surface-client".into(),
+ thread_id: format!("surface-thread-{request_id}"),
+ request_id: request_id.into(),
+ }
+}
+
+async fn next_for(
+ rx: &mut tokio::sync::broadcast::Receiver<WebChannelEvent>,
+ request_id: &str,
+) -> WebChannelEvent {
+ tokio::time::timeout(std::time::Duration::from_secs(5), async {
+ loop {
+ match rx.recv().await {
+ Ok(event) if event.request_id == request_id => return event,
+ Ok(_) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue,
+ Err(error) => panic!("bus closed: {error}"),
+ }
+ }
+ })
+ .await
+ .expect("event within timeout")
+}
+
+#[tokio::test]
+async fn output_events_keep_the_turns_ids_and_one_increasing_seq() {
+ let mut rx = crate::web_chat::subscribe_web_channel_events();
+ let surface = Surface::new(&DispatchCtx::ambient(), ids("surface-seq"));
+ for text in ["a", "b"] {
+ surface
+ .output(ChannelOutputEvent::TextDelta {
+ text: text.into(),
+ iteration: Some(2),
+ })
+ .await;
+ }
+ let first = next_for(&mut rx, "surface-seq").await;
+ let second = next_for(&mut rx, "surface-seq").await;
+ assert_eq!(first.event, "text_delta");
+ assert_eq!(first.client_id, "surface-client");
+ assert_eq!(first.thread_id, "surface-thread-surface-seq");
+ assert_eq!((first.delta.as_deref(), first.round), (Some("a"), Some(2)));
+ assert_eq!((first.seq, second.seq), (Some(1), Some(2)));
+ assert!(first.ts.is_some(), "every event carries a wall-clock stamp");
+}
+
+#[tokio::test]
+async fn an_interim_bubble_carries_the_round_the_canonical_output_lacks() {
+ let mut rx = crate::web_chat::subscribe_web_channel_events();
+ let surface = Surface::new(&DispatchCtx::ambient(), ids("surface-interim"));
+ surface.interim(3, "Let me check.".into()).await;
+ let event = next_for(&mut rx, "surface-interim").await;
+ assert_eq!(event.event, "chat_interim");
+ assert_eq!(event.full_response.as_deref(), Some("Let me check."));
+ assert_eq!(event.round, Some(3));
+}
+
+#[tokio::test]
+async fn host_authored_events_pass_through_without_a_seq() {
+ let mut rx = crate::web_chat::subscribe_web_channel_events();
+ let surface = Surface::new(&DispatchCtx::ambient(), ids("surface-terminal"));
+ surface
+ .event(bus_web::WebChannelEvent::ChatCancelled {
+ context: surface.context(),
+ cancel_reason: Some("user_stop".into()),
+ superseded_by: None,
+ })
+ .await;
+ let event = next_for(&mut rx, "surface-terminal").await;
+ assert_eq!(event.event, "chat_cancelled");
+ assert_eq!(event.cancel_reason.as_deref(), Some("user_stop"));
+ assert_eq!(event.seq, None);
+}
+
+#[tokio::test]
+async fn the_addressing_context_names_the_turn() {
+ let surface = Surface::new(&DispatchCtx::ambient(), ids("surface-context"));
+ let context = surface.context();
+ assert_eq!(
+ (context.client_id.as_str(), context.request_id.as_str()),
+ ("surface-client", "surface-context")
+ );
+ assert!(context.seq.is_none() && context.ts.is_none());
+}