use std::sync::Arc;
use serde_json::{Value, json};
use tokio::sync::Mutex;
use trusty_common::stdio_mcp_client::{McpTool, StdioMcpClient};
use crate::mcp::composed_name;
use crate::mcp::tool::{
MAX_INPUT_SCHEMA_BYTES, McpBridgeTool, build_schema, is_mcp_error, render_result,
};
use crate::tools::traits::ToolExecutor;
fn advertised(name: &str, input_schema: Value) -> McpTool {
McpTool {
name: name.to_string(),
description: Some("Does a thing.".to_string()),
input_schema,
}
}
#[test]
fn composed_names_follow_the_claude_code_convention() {
assert_eq!(
composed_name("github", "create_issue"),
"mcp__github__create_issue"
);
}
#[test]
fn a_real_input_schema_passes_through() {
let tool = advertised(
"echo",
json!({"type": "object", "properties": {"message": {"type": "string"}}, "required": ["message"]}),
);
let schema = build_schema("fixture", &tool).expect("schema");
assert_eq!(schema["function"]["name"], "mcp__fixture__echo");
assert_eq!(schema["function"]["description"], "[fixture] Does a thing.");
assert_eq!(
schema["function"]["parameters"],
json!({"type": "object", "properties": {"message": {"type": "string"}}, "required": ["message"]}),
);
}
#[test]
fn an_absent_input_schema_falls_back_to_an_open_object() {
let tool = advertised("bare", Value::Null);
let schema = build_schema("fixture", &tool).expect("schema");
assert_eq!(
schema["function"]["parameters"],
json!({"type": "object", "properties": {}})
);
}
#[test]
fn an_oversized_input_schema_is_rejected() {
let huge: Vec<String> = (0..20_000).map(|i| format!("value-{i}")).collect();
let tool = advertised(
"oversized",
json!({"type": "object", "properties": {"choice": {"enum": huge}}}),
);
let reason = build_schema("fixture", &tool).expect_err("must refuse");
assert!(
reason.contains(&MAX_INPUT_SCHEMA_BYTES.to_string()),
"the reason names the cap: {reason}",
);
}
#[test]
fn text_frames_are_joined() {
let result =
json!({"content": [{"type": "text", "text": "one"}, {"type": "text", "text": "two"}]});
assert_eq!(render_result(&result), "one\ntwo");
}
#[test]
fn a_frameless_result_renders_as_json() {
let result = json!({"structuredContent": {"ok": true}});
let rendered = render_result(&result);
assert!(rendered.contains("structuredContent"), "{rendered}");
}
#[test]
fn the_in_band_error_flag_is_detected() {
assert!(is_mcp_error(&json!({"isError": true, "content": []})));
assert!(!is_mcp_error(&json!({"content": []})));
assert!(!is_mcp_error(&json!({"isError": false, "content": []})));
}
#[tokio::test]
#[cfg(unix)]
async fn execute_surfaces_a_broken_server_recoverably() {
let client = StdioMcpClient::spawn("/bin/cat", &[], "trusty-code")
.await
.expect("/bin/cat spawns");
let tool = advertised("echo", json!({"type": "object"}));
let tool = McpBridgeTool::new("ghost", &tool, Arc::new(Mutex::new(client))).expect("schema");
let result = tool.execute(json!({})).await;
assert!(result.is_error());
assert!(!result.is_fatal(), "one broken server must not end the run");
assert!(
result.content().contains("ghost") && result.content().contains("echo"),
"the message identifies what failed: {}",
result.content(),
);
assert_eq!(tool.server(), "ghost");
}