#![cfg(not(target_family = "wasm"))]
use std::sync::Arc;
use std::time::Duration;
use a2a_client::agent_card::AgentCardResolver;
use a2a_client::auth::AuthInterceptor;
use a2a_client::{A2AClient, A2AClientFactory, Transport};
use anyhow::{Context, ensure};
use futures::StreamExt;
use rig_a2a::A2aServer;
use rig_a2a::a2a::{
SendMessageConfiguration, StreamResponse, SubscribeToTaskRequest, TRANSPORT_PROTOCOL_JSONRPC,
TaskPushNotificationConfig, TaskState, error_code,
};
use rig_a2a::axum::body::Bytes;
use rig_a2a::axum::extract::State;
use rig_a2a::axum::http::StatusCode;
use rig_a2a::axum::routing::post;
use rig_agent::{Agent, AgentBuilder};
use rig_core::test_utils::{MockCompletionModel, MockStreamEvent};
use tokio::sync::{Notify, mpsc};
use super::serve::{MockControlledTool, connect, reply, request, serve};
const TOKEN: &str = "push-token";
async fn serve_with(
agent: Agent,
configure: impl FnOnce(A2aServer) -> A2aServer,
) -> anyhow::Result<String> {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let url = format!("http://{}", listener.local_addr()?);
let app = configure(A2aServer::new(agent, &url)).into_router();
tokio::spawn(async move { rig_a2a::axum::serve(listener, app).await });
Ok(url)
}
async fn connect_bearer(url: &str) -> anyhow::Result<A2AClient<Box<dyn Transport>>> {
let card = AgentCardResolver::new(None).resolve(url).await?;
let factory = A2AClientFactory::builder()
.preferred_bindings(vec![TRANSPORT_PROTOCOL_JSONRPC.to_owned()])
.with_interceptor(Arc::new(AuthInterceptor::bearer(TOKEN)))
.build();
Ok(factory.create_from_card(&card).await?)
}
async fn webhook() -> anyhow::Result<(String, mpsc::UnboundedReceiver<StreamResponse>)> {
async fn capture(
State(sender): State<mpsc::UnboundedSender<StreamResponse>>,
body: Bytes,
) -> StatusCode {
if let Ok(event) = serde_json::from_slice::<StreamResponse>(&body) {
let _ = sender.send(event);
}
StatusCode::OK
}
let (sender, receiver) = mpsc::unbounded_channel();
let app = rig_a2a::axum::Router::new()
.route("/hook", post(capture))
.with_state(sender);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await?;
let url = format!("http://{}/hook", listener.local_addr()?);
tokio::spawn(async move { rig_a2a::axum::serve(listener, app).await });
Ok((url, receiver))
}
fn config(task_id: &str, url: &str) -> TaskPushNotificationConfig {
TaskPushNotificationConfig {
url: url.to_owned(),
id: None,
task_id: task_id.to_owned(),
token: None,
authentication: None,
tenant: None,
}
}
fn no_turns() -> MockCompletionModel {
MockCompletionModel::from_stream_turns(Vec::<Vec<MockStreamEvent>>::new())
}
#[tokio::test]
async fn the_card_declares_push_only_when_it_is_on() -> anyhow::Result<()> {
let off = serve(AgentBuilder::new(no_turns()).name("echo").build()).await?;
let card = AgentCardResolver::new(None).resolve(&off).await?;
ensure!(
card.capabilities.push_notifications == Some(false),
"{card:?}"
);
let on = serve_with(AgentBuilder::new(no_turns()).name("echo").build(), |s| {
s.bearer_token(TOKEN).push_notifications(true)
})
.await?;
let card = AgentCardResolver::new(None).resolve(&on).await?;
ensure!(
card.capabilities.push_notifications == Some(true),
"{card:?}"
);
Ok(())
}
#[tokio::test]
async fn push_without_a_bearer_token_stays_off() -> anyhow::Result<()> {
let url = serve_with(AgentBuilder::new(no_turns()).name("echo").build(), |s| {
s.push_notifications(true)
})
.await?;
let card = AgentCardResolver::new(None).resolve(&url).await?;
ensure!(
card.capabilities.push_notifications == Some(false),
"{card:?}"
);
let client = connect(&url, TRANSPORT_PROTOCOL_JSONRPC).await?;
let error = client
.create_push_config(&config("task-1", "https://hooks.example.com/a"))
.await
.err()
.context("set-config is refused without a bearer token")?;
ensure!(
error.code == error_code::PUSH_NOTIFICATION_NOT_SUPPORTED,
"{error:?}"
);
Ok(())
}
#[tokio::test]
async fn push_configs_deliver_task_updates_to_a_webhook() -> anyhow::Result<()> {
let (hook, mut deliveries) = webhook().await?;
let agent = AgentBuilder::new(MockCompletionModel::from_stream_turns([reply(&["Done"])]))
.name("echo")
.build();
let url = serve_with(agent, |s| {
s.bearer_token(TOKEN)
.push_notifications(true)
.allow_unguarded_push_urls()
})
.await?;
let client = connect_bearer(&url).await?;
let mut send = request("Hi", None);
send.configuration = Some(SendMessageConfiguration {
accepted_output_modes: None,
task_push_notification_config: Some(config("", &hook)),
history_length: None,
return_immediately: None,
});
let mut stream = client.send_streaming_message(&send).await?;
let mut task_id = None;
while let Some(event) = stream.next().await {
if let StreamResponse::Task(task) = event? {
task_id = Some(task.id);
}
}
let task_id = task_id.context("the stream starts with the task")?;
loop {
let event = tokio::time::timeout(Duration::from_secs(10), deliveries.recv())
.await
.context("the webhook got the final update in time")?
.context("the webhook channel closed")?;
if matches!(
&event,
StreamResponse::StatusUpdate(update)
if update.task_id == task_id && update.status.state == TaskState::Completed
) {
break;
}
}
Ok(())
}
#[tokio::test]
async fn set_config_is_refused_when_push_is_off() -> anyhow::Result<()> {
let url = serve(AgentBuilder::new(no_turns()).name("echo").build()).await?;
let client = connect(&url, TRANSPORT_PROTOCOL_JSONRPC).await?;
let error = client
.create_push_config(&config("task-1", "https://hooks.example.com/a"))
.await
.err()
.context("set-config is refused while push is off")?;
ensure!(
error.code == error_code::PUSH_NOTIFICATION_NOT_SUPPORTED,
"{error:?}"
);
Ok(())
}
#[tokio::test]
async fn webhook_urls_are_checked_when_saved() -> anyhow::Result<()> {
let url = serve_with(AgentBuilder::new(no_turns()).name("echo").build(), |s| {
s.bearer_token(TOKEN)
.push_notifications(true)
.push_allowed_hosts(["hooks.example.com"])
})
.await?;
let client = connect_bearer(&url).await?;
for refused in [
"http://hooks.example.com/a",
"ftp://hooks.example.com/a",
"https://evil.example.net/a",
"https://100.64.0.1/a",
"https://[64:ff9b::808:808]/a",
] {
let error = client
.create_push_config(&config("task-1", refused))
.await
.err()
.with_context(|| format!("{refused} is refused"))?;
ensure!(
error.code == error_code::INVALID_PARAMS,
"{refused}: {error:?}"
);
}
client
.create_push_config(&config("task-1", "https://Hooks.Example.com/a"))
.await?;
Ok(())
}
#[tokio::test]
async fn each_task_holds_at_most_ten_configs() -> anyhow::Result<()> {
let url = serve_with(AgentBuilder::new(no_turns()).name("echo").build(), |s| {
s.bearer_token(TOKEN).push_notifications(true)
})
.await?;
let client = connect_bearer(&url).await?;
for n in 0..10 {
client
.create_push_config(&config("task-1", &format!("https://hooks.example.com/{n}")))
.await?;
}
let error = client
.create_push_config(&config("task-1", "https://hooks.example.com/11"))
.await
.err()
.context("an eleventh config on one task is refused")?;
ensure!(error.code == error_code::INVALID_PARAMS, "{error:?}");
client
.create_push_config(&config("task-2", "https://hooks.example.com/a"))
.await?;
Ok(())
}
#[tokio::test]
async fn resubscribe_gets_the_rest_of_a_running_task() -> anyhow::Result<()> {
let started = Arc::new(Notify::new());
let release = Arc::new(Notify::new());
let tool = MockControlledTool::new(Arc::clone(&started), Arc::clone(&release));
let model = MockCompletionModel::from_stream_turns([
vec![
MockStreamEvent::tool_call("call-1", "controlled", serde_json::json!({})),
MockStreamEvent::final_response_with_default_usage(),
],
reply(&["Done"]),
]);
let agent = AgentBuilder::new(model)
.name("echo")
.tool(tool)
.default_max_turns(3)
.build();
let client = connect(&serve(agent).await?, TRANSPORT_PROTOCOL_JSONRPC).await?;
let mut original = client
.send_streaming_message(&request("Wait", None))
.await?;
let Some(Ok(StreamResponse::Task(task))) = original.next().await else {
anyhow::bail!("the stream starts with the task");
};
started.notified().await;
let mut resubscribed = client
.subscribe_to_task(&SubscribeToTaskRequest {
id: task.id.clone(),
tenant: None,
})
.await?;
release.notify_one();
let drained = tokio::time::timeout(Duration::from_secs(10), async {
let mut last = None;
while let Some(event) = resubscribed.next().await {
last = Some(event?);
}
Ok::<_, rig_a2a::a2a::A2AError>(last)
})
.await
.context("the resubscribed stream ends")??;
let Some(StreamResponse::StatusUpdate(done)) = drained else {
anyhow::bail!("the resubscribed stream ends with a status update");
};
ensure!(done.status.state == TaskState::Completed, "{done:?}");
let finished = tokio::time::timeout(Duration::from_secs(10), async {
while original.next().await.is_some() {}
})
.await;
ensure!(finished.is_ok(), "the original stream ends too");
Ok(())
}