use serde_json::json;
use super::StdioMcpClient;
#[cfg(unix)]
const READ_ID: &str = r#"id=$(printf '%s' "$l" | sed -n 's/.*"id":\([0-9][0-9]*\).*/\1/p')"#;
#[tokio::test]
#[cfg(unix)]
async fn server_request_with_matching_id_is_not_returned_as_result() {
let script = r#"read -r l
printf '{"jsonrpc":"2.0","id":1,"result":{"serverInfo":{"name":"mock","version":"1"}}}\n'
read -r l
read -r l
printf '{"jsonrpc":"2.0","id":2,"method":"roots/list"}\n'
printf '{"jsonrpc":"2.0","id":2,"result":{"answer":"tool"}}\n'
sleep 5"#;
let mut client = StdioMcpClient::spawn("sh", &["-c", script], "test-client")
.await
.expect("spawn mock");
client.initialize().await.expect("initialize");
let result = client
.call_tool("t", json!({}))
.await
.expect("the response, not the server request, is the result");
assert_eq!(result, json!({ "answer": "tool" }));
}
#[tokio::test]
#[cfg(unix)]
async fn stale_frame_after_respawn_is_not_returned_as_result() {
let dir = tempfile::tempdir().expect("tempdir");
let marker = dir.path().join("generation-1-ran");
let marker = marker.to_str().expect("utf-8 temp path");
let script = format!(
r#"if [ -e "$1" ]; then
read -r l; {READ_ID}
printf '{{"jsonrpc":"2.0","id":%s,"result":{{"serverInfo":{{"name":"mock","version":"2"}}}}}}\n' "$id"
read -r l
printf '{{"jsonrpc":"2.0","id":2,"result":{{"stale":true}}}}\n'
while read -r l; do {READ_ID}
printf '{{"jsonrpc":"2.0","id":%s,"result":{{"fresh":true}}}}\n' "$id"
done
else
: > "$1"
read -r l
printf '{{"jsonrpc":"2.0","id":1,"result":{{"serverInfo":{{"name":"mock","version":"1"}}}}}}\n'
read -r l
read -r l
printf '{{"jsonrpc":"2.0","id":2,"result":{{"first":true}}}}\n'
fi"#
);
let mut client = StdioMcpClient::spawn("sh", &["-c", &script, "sh", marker], "test-client")
.await
.expect("spawn mock");
client.initialize().await.expect("initialize");
let first = client.call_tool("t", json!({})).await.expect("first call");
assert_eq!(first, json!({ "first": true }));
for _ in 0..100 {
if !client.is_alive() {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(20)).await;
}
assert!(!client.is_alive(), "generation 1 should have exited");
let second = client
.call_tool("t", json!({}))
.await
.expect("call after respawn");
assert_eq!(second, json!({ "fresh": true }), "stale frame was misread");
}
#[test]
fn is_response_to_accepts_only_responses_with_the_id() {
use super::client::is_response_to;
let id = json!(2);
let cases = [
(json!({"jsonrpc":"2.0","id":2,"result":{}}), true),
(json!({"jsonrpc":"2.0","id":2,"error":{"code":-1}}), true),
(json!({"jsonrpc":"2.0","id":2,"method":"roots/list"}), false),
(
json!({"jsonrpc":"2.0","id":2,"method":"x","result":{}}),
false,
),
(json!({"jsonrpc":"2.0","id":2}), false),
(json!({"jsonrpc":"2.0","id":3,"result":{}}), false),
(
json!({"jsonrpc":"2.0","method":"notifications/progress"}),
false,
),
];
for (frame, want) in cases {
assert_eq!(is_response_to(&frame, &id), want, "{frame}");
}
}
#[tokio::test]
#[cfg(unix)]
async fn timeout_mid_line_does_not_corrupt_the_next_call() {
let script = format!(
r#"read -r l
printf '{{"jsonrpc":"2.0","id":1,"result":{{"serverInfo":{{"name":"mock","version":"1"}}}}}}\n'
read -r l
read -r l
printf '{{"jsonrpc":"2.0","id":2,"result":'
sleep 1
printf '{{"late":true}}}}\n'
read -r l; {READ_ID}
printf '{{"jsonrpc":"2.0","id":%s,"result":{{"fresh":true}}}}\n' "$id"
sleep 5"#
);
let mut client = StdioMcpClient::spawn("sh", &["-c", &script], "test-client")
.await
.expect("spawn mock");
client.initialize().await.expect("initialize");
let bound = std::time::Duration::from_millis(300);
let first = client.call_tool_with_timeout("t", json!({}), bound).await;
assert!(first.is_err(), "the first call must time out: {first:?}");
let second = client
.call_tool("t", json!({}))
.await
.expect("the call after a mid-line timeout");
assert_eq!(second, json!({ "fresh": true }));
}
#[tokio::test]
#[cfg(unix)]
async fn late_reply_to_timed_out_call_is_skipped() {
let script = format!(
r#"read -r l
printf '{{"jsonrpc":"2.0","id":1,"result":{{"serverInfo":{{"name":"mock","version":"1"}}}}}}\n'
read -r l
read -r l
sleep 1
printf '{{"jsonrpc":"2.0","id":2,"result":{{"late":true}}}}\n'
read -r l; {READ_ID}
printf '{{"jsonrpc":"2.0","id":%s,"result":{{"fresh":true}}}}\n' "$id"
sleep 5"#
);
let mut client = StdioMcpClient::spawn("sh", &["-c", &script], "test-client")
.await
.expect("spawn mock");
client.initialize().await.expect("initialize");
let bound = std::time::Duration::from_millis(300);
let first = client.call_tool_with_timeout("t", json!({}), bound).await;
assert!(first.is_err(), "the first call must time out: {first:?}");
let second = client.call_tool("t", json!({})).await.expect("call N+1");
assert_eq!(second, json!({ "fresh": true }), "late reply was misread");
}
#[tokio::test]
#[cfg(unix)]
async fn error_frame_with_method_is_not_returned_as_result() {
let script = r#"read -r l
printf '{"jsonrpc":"2.0","id":1,"result":{"serverInfo":{"name":"mock","version":"1"}}}\n'
read -r l
read -r l
printf '{"jsonrpc":"2.0","id":2,"method":"x","error":{"code":-1,"message":"m"}}\n'
printf '{"jsonrpc":"2.0","id":2,"result":{"answer":"tool"}}\n'
sleep 5"#;
let mut client = StdioMcpClient::spawn("sh", &["-c", script], "test-client")
.await
.expect("spawn mock");
client.initialize().await.expect("initialize");
let result = client.call_tool("t", json!({})).await.expect("real result");
assert_eq!(result, json!({ "answer": "tool" }));
}
#[tokio::test]
#[cfg(unix)]
async fn oversized_line_errors_and_next_call_skips_its_tail() {
let script = format!(
r#"read -r l
printf '{{"jsonrpc":"2.0","id":1,"result":{{"serverInfo":{{"name":"mock","version":"1"}}}}}}\n'
read -r l
read -r l
head -c 17000000 /dev/zero | tr '\0' '{{'
sleep 1
head -c 1000 /dev/zero | tr '\0' '{{'
printf '\n'
read -r l; {READ_ID}
printf '{{"jsonrpc":"2.0","id":%s,"result":{{"fresh":true}}}}\n' "$id"
sleep 5"#
);
let mut client = StdioMcpClient::spawn("sh", &["-c", &script], "test-client")
.await
.expect("spawn mock");
client.initialize().await.expect("initialize");
let first = client.call_tool("t", json!({})).await;
let err = format!(
"{:#}",
first.expect_err("an oversized line must fail the call")
);
assert!(err.contains("16777216"), "error must name the cap: {err}");
assert!(
client.line_buf.len() <= super::MAX_LINE_BYTES,
"buffer must stay bounded: {}",
client.line_buf.len()
);
let second = client
.call_tool("t", json!({}))
.await
.expect("the call after an oversized line");
assert_eq!(second, json!({ "fresh": true }));
}
#[test]
fn log_field_truncates_long_values() {
let long = "m".repeat(10_000);
let shown = super::client::log_field(&long);
assert_eq!(shown, format!("{}…(+9936)", "m".repeat(64)));
assert_eq!(super::client::log_field("ping"), "ping");
}