use agent_client_protocol::v1::schema::{
SessionUpdate, ToolCall, ToolCallStatus, ToolCallUpdate, ToolCallUpdateFields,
};
use pretty_assertions::assert_eq;
use serde_json::{json, Map, Value};
use super::Announced;
fn arguments(value: Value) -> Map<String, Value> {
let Value::Object(arguments) = value else {
unreachable!("arguments are an object");
};
arguments
}
#[tokio::test(start_paused = true)]
async fn an_mcp_call_claims_the_announcement_of_its_arguments() {
type Heard = (SessionUpdate, Option<&'static str>);
type Claim = (&'static str, Value);
type Case = (
&'static str,
Vec<Heard>,
Vec<Claim>,
Vec<Option<&'static str>>,
);
let ask = |color: &str| Some(json!({"color": color, "toolSummary": "Ask"}));
let announced = |tool: &'static str, id: &'static str, status, input: Option<Value>| {
let call = ToolCall::new(id, format!("mcp_rho_{tool}"))
.status(status)
.raw_input(input);
(SessionUpdate::ToolCall(call), Some(tool))
};
let running = |tool, id, input| announced(tool, id, ToolCallStatus::InProgress, input);
let updated = |id: &'static str, status| {
let update = ToolCallUpdate::new(id, ToolCallUpdateFields::new().status(status));
(SessionUpdate::ToolCallUpdate(update), None::<&'static str>)
};
let red = || json!({"color": "red"});
let cases: [Case; 9] = [
(
"arguments pick among the tool's calls",
vec![
running("ask", "a", ask("red")),
running("ask", "b", ask("blue")),
],
vec![("ask", json!({"color": "blue"})), ("ask", red())],
vec![Some("b"), Some("a")],
),
(
"none carries them: oldest first",
vec![running("ask", "a", None), running("ask", "b", ask("blue"))],
vec![
("ask", json!({"color": "green"})),
("ask", json!({"color": "green"})),
],
vec![Some("a"), Some("b")],
),
(
"no arguments: oldest first",
vec![
running("ask", "a", ask("red")),
running("ask", "b", Some(json!({}))),
],
vec![("ask", json!({}))],
vec![Some("a")],
),
(
"another tool's call",
vec![running("other", "a", ask("red"))],
vec![("ask", red())],
vec![None],
),
(
"announced pending",
vec![announced("ask", "a", ToolCallStatus::Pending, ask("red"))],
vec![("ask", red())],
vec![Some("a")],
),
(
"announced as it ended",
vec![announced("ask", "a", ToolCallStatus::Completed, ask("red"))],
vec![("ask", red())],
vec![None],
),
(
"a call the agent completed",
vec![
running("ask", "a", ask("red")),
running("ask", "b", ask("red")),
updated("a", ToolCallStatus::Completed),
],
vec![("ask", red()), ("ask", red())],
vec![Some("b"), None],
),
(
"a call the agent failed",
vec![
running("ask", "a", ask("red")),
updated("a", ToolCallStatus::Failed),
],
vec![("ask", red())],
vec![None],
),
(
"an update that ends nothing",
vec![
running("ask", "a", ask("red")),
updated("a", ToolCallStatus::InProgress),
],
vec![("ask", red())],
vec![Some("a")],
),
];
for (name, heard, claims, expected) in cases {
let calls = Announced::default();
for (update, tool) in &heard {
calls.hear(update, *tool);
}
let mut claimed = Vec::new();
for (tool, call) in claims {
let id = calls.claim(tool, &arguments(call)).await;
claimed.push(id.map(|id| id.to_string()));
}
let expected: Vec<Option<String>> = expected
.into_iter()
.map(|id| id.map(str::to_owned))
.collect();
assert_eq!(claimed, expected, "{name}");
}
}