#![cfg(feature = "client")]
use ag_ui::client::transport::{Transport, TransportFuture, TransportStream};
use ag_ui::client::{RunEnd, Thread, Update};
use ag_ui::{Event, RunAgentInput, TextMessageRole};
use futures_util::StreamExt;
const MANIFEST: &str = include_str!("../Cargo.toml");
fn reqwest_lines() -> Vec<(String, String)> {
let mut section = String::new();
let mut found = Vec::new();
for line in MANIFEST.lines() {
let line = line.trim();
if line.starts_with('#') {
continue;
}
if line.starts_with('[') && line.ends_with(']') {
section = line.to_owned();
continue;
}
if line.contains("reqwest") {
found.push((section.clone(), line.to_owned()));
}
}
found
}
#[test]
fn reqwest_is_optional_and_only_the_http_feature_turns_it_on() {
let mentions = reqwest_lines();
assert!(
!mentions.is_empty(),
"the http transport should still depend on reqwest"
);
for (section, line) in &mentions {
match section.as_str() {
"[dependencies]" => assert!(
line.contains("optional = true"),
"reqwest must stay optional, found: {line}"
),
"[features]" => assert!(
line.starts_with("http ="),
"only the `http` feature may enable reqwest, found: {line}"
),
other => panic!("reqwest must not appear in {other}: {line}"),
}
}
assert!(
mentions
.iter()
.any(|(section, line)| section == "[features]" && line.contains("dep:reqwest")),
"the http feature should enable reqwest through `dep:reqwest`, so that \
the feature and the dependency cannot drift apart"
);
}
#[cfg(feature = "http")]
#[test]
fn the_http_agent_builder_configures_a_transport() {
let agent = ag_ui::client::HttpAgent::builder("https://example.com/agent")
.header("authorization", "Bearer token")
.timeout(std::time::Duration::from_secs(30))
.build()
.expect("a valid URL and header");
assert_eq!(
agent.transport().url().as_str(),
"https://example.com/agent"
);
assert!(agent.transport().headers().contains_key("authorization"));
assert_eq!(agent.transport().headers()["accept"], "text/event-stream");
}
#[cfg(feature = "http")]
#[test]
fn a_bad_url_or_header_is_a_configuration_error_not_a_panic() {
let error = ag_ui::client::HttpAgent::builder("not a url")
.build()
.expect_err("that is not a URL");
assert!(matches!(error, ag_ui::client::Error::Config(_)));
let error = ag_ui::client::HttpAgent::builder("https://example.com")
.header("not a header name", "v")
.build()
.expect_err("that is not a header name");
assert!(matches!(error, ag_ui::client::Error::Config(_)));
}
#[derive(Clone, Debug)]
struct StaticTransport {
events: Vec<Event>,
}
impl Transport for StaticTransport {
fn run(&self, _input: RunAgentInput) -> TransportFuture {
let events = self.events.clone();
Box::pin(async move {
let stream = futures_util::stream::iter(events.into_iter().map(Ok));
Ok(TransportStream::typed(stream))
})
}
}
#[tokio::test]
async fn a_custom_transport_substitutes_for_the_built_in_one() {
let transport = StaticTransport {
events: vec![
Event::run_started("thread-1", "run-1"),
Event::text_message_start("msg-1", TextMessageRole::Assistant),
Event::text_message_content("msg-1", "From somewhere else entirely."),
Event::text_message_end("msg-1"),
Event::run_finished_success("thread-1", "run-1"),
],
};
let mut thread = Thread::<_>::new(transport, "thread-1");
thread.set_next_run_id("run-1");
let updates: Vec<_> = thread.send("hello").unwrap().collect().await;
assert!(matches!(
updates.last(),
Some(Update::Done(RunEnd::Success { .. }))
));
assert_eq!(
thread.applier().text_of("msg-1"),
Some("From somewhere else entirely.")
);
}
#[tokio::test]
async fn a_boxed_transport_is_a_transport() {
let transport: Box<dyn Transport> = Box::new(StaticTransport {
events: vec![
Event::run_started("thread-1", "run-1"),
Event::run_finished_success("thread-1", "run-1"),
],
});
let mut thread = Thread::<_>::new(transport, "thread-1");
thread.set_next_run_id("run-1");
let updates: Vec<_> = thread.send("hello").unwrap().collect().await;
assert!(matches!(
updates.last(),
Some(Update::Done(RunEnd::Success { .. }))
));
}