use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
use super::{mount_card, mount_raw_card, source_for};
#[tokio::test]
async fn a_catalog_entry_describes_the_agent_from_its_card() {
let server = MockServer::start().await;
mount_card(&server).await;
let source = source_for(vec![("recipe", server.uri())]);
let catalog = source.catalog().await;
let entry = catalog.tool_entry("recipe").expect("entry present");
assert_eq!(entry.description, "Helps with recipes and cooking.");
assert!(
catalog.error_for("recipe").is_none(),
"a fetched card must not also be recorded as an error"
);
}
#[tokio::test]
async fn an_unreachable_agent_yields_no_entry_rather_than_failing_the_catalog() {
let source = source_for(vec![("dead", "http://127.0.0.1:1/".to_string())]);
let catalog = source.catalog().await;
assert!(catalog.tool_entry("dead").is_none());
assert!(
catalog.error_for("dead").is_some(),
"a dispatch error later has to name the real cause"
);
}
#[tokio::test]
async fn one_dead_agent_does_not_hide_a_live_one() {
let server = MockServer::start().await;
mount_card(&server).await;
let source = source_for(vec![
("live", server.uri()),
("dead", "http://127.0.0.1:1/".to_string()),
]);
let catalog = source.catalog().await;
assert!(
catalog.tool_entry("live").is_some(),
"the live agent's entry must survive alongside the dead one"
);
assert!(catalog.tool_entry("dead").is_none());
assert!(catalog.error_for("dead").is_some());
}
#[tokio::test]
async fn an_agent_serving_the_spec_shaped_security_requirements_still_yields_a_tool() {
let server = MockServer::start().await;
let card = r#"{
"name": "Recipe Agent",
"description": "Helps with recipes and cooking.",
"version": "1.0.0",
"supportedInterfaces": [
{ "url": "https://api.example.com/a2a", "protocolBinding": "JSONRPC", "protocolVersion": "1.0" }
],
"capabilities": { "streaming": false },
"defaultInputModes": ["text/plain"],
"defaultOutputModes": ["text/plain"],
"securitySchemes": {
"main": { "httpAuthSecurityScheme": { "scheme": "bearer" } }
},
"securityRequirements": [{ "schemes": { "main": { "list": ["read"] } } }],
"skills": [
{ "id": "suggest", "name": "Suggest a recipe", "description": "Suggests a dish.", "tags": ["cooking"] }
]
}"#;
mount_raw_card(&server, card.to_string()).await;
let source = source_for(vec![("recipe", server.uri())]);
let catalog = source.catalog().await;
let entry = catalog
.tool_entry("recipe")
.expect("a spec-shaped card must still produce a tool");
assert_eq!(entry.description, "Helps with recipes and cooking.");
assert!(catalog.error_for("recipe").is_none());
}
fn card_declaring_extensions(server: &MockServer, extensions: &str) -> String {
super::CARD
.replace("PLACEHOLDER", &format!("{}/a2a", server.uri()))
.replace(
r#""capabilities": { "streaming": false, "pushNotifications": false }"#,
&format!(r#""capabilities": {{ "streaming": false, "extensions": {extensions} }}"#),
)
}
#[tokio::test]
async fn an_agent_requiring_an_extension_we_do_not_implement_yields_no_tools() {
let server = MockServer::start().await;
let card = card_declaring_extensions(
&server,
r#"[{ "uri": "https://example.com/ext/signing", "required": true }]"#,
);
mount_raw_card(&server, card).await;
let source = source_for(vec![("recipe", server.uri())]);
let catalog = source.catalog().await;
assert!(
catalog.tool_entry("recipe").is_none(),
"a required extension we cannot honour must not produce a bindable tool"
);
let reason = catalog
.error_for("recipe")
.expect("the refusal must be reported like any other unusable agent");
assert!(
reason.contains("https://example.com/ext/signing"),
"the reason must name the extension so an operator can act on it, got {reason:?}"
);
}
#[tokio::test]
async fn calling_an_agent_that_requires_an_unimplemented_extension_sends_nothing() {
let server = MockServer::start().await;
let card = card_declaring_extensions(
&server,
r#"[{ "uri": "https://example.com/ext/signing", "required": true }]"#,
);
mount_raw_card(&server, card).await;
let source = source_for(vec![("recipe", server.uri())]);
let catalog = source.catalog().await;
let result = catalog
.dispatch("recipe", &serde_json::json!("hello"))
.await;
let error = result["error"].as_str().expect("an error value");
assert!(
error.contains("https://example.com/ext/signing"),
"the refusal must name the extension, got {error:?}"
);
}
#[tokio::test]
async fn an_optional_extension_changes_nothing() {
let server = MockServer::start().await;
let card = card_declaring_extensions(
&server,
r#"[
{ "uri": "https://example.com/ext/one", "required": false },
{ "uri": "https://example.com/ext/two", "description": "no required key" }
]"#,
);
mount_raw_card(&server, card).await;
let source = source_for(vec![("recipe", server.uri())]);
let catalog = source.catalog().await;
let entry = catalog
.tool_entry("recipe")
.expect("an optional extension must behave exactly as no extension does");
assert_eq!(entry.description, "Helps with recipes and cooking.");
assert!(catalog.error_for("recipe").is_none());
}
#[tokio::test]
async fn a_card_that_is_not_valid_json_is_recorded_as_an_error_not_a_panic() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path("/.well-known/agent-card.json"))
.respond_with(ResponseTemplate::new(200).set_body_string("not json"))
.mount(&server)
.await;
let source = source_for(vec![("bad", server.uri())]);
let catalog = source.catalog().await;
assert!(catalog.tool_entry("bad").is_none());
assert!(catalog.error_for("bad").is_some());
}