mirror of
https://github.com/storytold/soundcraft.git
synced 2026-11-21 20:39:52 +00:00
mcp: add protocol transcript tests for initialize, cancel, and batch (#1673)
This commit is contained in:
@@ -290,4 +290,72 @@ mod tests {
|
||||
let v: Value = serde_json::from_str(reply(r#"{"jsonrpc":"2.0","id":1,"method":"ping"}"#).trim()).unwrap();
|
||||
assert_eq!(v["id"], 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stdio_initialize_negotiation_transcript() {
|
||||
let mut lines: Vec<Value> = craft_mcp_server::SUPPORTED_VERSIONS
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, version)| {
|
||||
json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": i + 1,
|
||||
"method": "initialize",
|
||||
"params": {"protocolVersion": version, "capabilities": {}, "clientInfo": {"name": "parity-client", "version": "1.0"}},
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
lines.push(json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": 99,
|
||||
"method": "initialize",
|
||||
"params": {"protocolVersion": "1999-01-01", "capabilities": {}, "clientInfo": {"name": "parity-client", "version": "1.0"}},
|
||||
}));
|
||||
lines.push(json!({"jsonrpc": "2.0", "method": "notifications/initialized"}));
|
||||
lines.push(json!({"jsonrpc": "2.0", "id": 100, "method": "ping", "params": {}}));
|
||||
let input: String = lines.iter().map(|v| v.to_string() + "\n").collect();
|
||||
|
||||
let mut server = Server::new(Headless::new(Engine::default()));
|
||||
let mut out = Vec::new();
|
||||
server.serve(input.as_bytes(), &mut out).unwrap();
|
||||
|
||||
let replies: Vec<Value> = std::str::from_utf8(&out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
|
||||
assert_eq!(replies.len(), craft_mcp_server::SUPPORTED_VERSIONS.len() + 2);
|
||||
for (i, version) in craft_mcp_server::SUPPORTED_VERSIONS.iter().enumerate() {
|
||||
assert_eq!(replies[i]["id"], i + 1);
|
||||
assert_eq!(replies[i]["result"]["protocolVersion"], *version);
|
||||
assert_eq!(replies[i]["result"]["serverInfo"]["name"], "soundcraft");
|
||||
}
|
||||
let fallback = &replies[craft_mcp_server::SUPPORTED_VERSIONS.len()];
|
||||
assert_eq!(fallback["id"], 99);
|
||||
assert_eq!(fallback["result"]["protocolVersion"], PROTOCOL_VERSION);
|
||||
let ping = &replies[craft_mcp_server::SUPPORTED_VERSIONS.len() + 1];
|
||||
assert_eq!(ping["id"], 100);
|
||||
assert_eq!(ping["result"], json!({}));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stdio_cancel_and_batch_transcript() {
|
||||
let input = concat!(
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"initialize\",\"params\":{\"protocolVersion\":\"2025-06-18\"}}\n",
|
||||
"{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}\n",
|
||||
"{\"jsonrpc\":\"2.0\",\"method\":\"notifications/cancelled\",\"params\":{\"requestId\":1,\"reason\":\"client\"}}\n",
|
||||
"[]\n",
|
||||
"[{\"jsonrpc\":\"2.0\",\"method\":\"notifications/cancelled\",\"params\":{\"requestId\":9}}]\n",
|
||||
"[{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"ping\"},{\"jsonrpc\":\"2.0\",\"method\":\"notifications/progress\"}]\n",
|
||||
"{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"ping\",\"params\":{}}\n"
|
||||
);
|
||||
let mut server = Server::new(Headless::new(Engine::default()));
|
||||
let mut out = Vec::new();
|
||||
server.serve(input.as_bytes(), &mut out).unwrap();
|
||||
|
||||
let replies: Vec<Value> = std::str::from_utf8(&out).unwrap().lines().map(|l| serde_json::from_str(l).unwrap()).collect();
|
||||
assert_eq!(replies.len(), 4, "{replies:?}");
|
||||
assert_eq!(replies[0]["id"], 1);
|
||||
assert_eq!(replies[0]["result"]["serverInfo"]["name"], "soundcraft");
|
||||
assert_eq!(replies[1]["id"], Value::Null);
|
||||
assert_eq!(replies[1]["error"]["code"], -32600);
|
||||
assert_eq!(replies[2], json!([{"jsonrpc": "2.0", "id": 2, "result": {}}]));
|
||||
assert_eq!(replies[3], json!({"jsonrpc": "2.0", "id": 3, "result": {}}));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user