Files
crank/crates/crank-community-mcp/src/rmcp_negotiation_handler_runtime_tests.rs
T
bsodfather bcdf57074b
CI / UI Checks (pull_request) Successful in 12s
CI / Rust Checks (pull_request) Successful in 49m59s
CI / Rust Feature Validation (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Required Checks (pull_request) Successful in 2s
CI / Deploy (pull_request) Skipped
MCP: execute negotiation and ping through SDK handlers
2026-11-13 20:27:41 +00:00

140 lines
4.9 KiB
Rust

//! Behavioral RED for real negotiation/ping handler execution and result consumption.
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
use super::{CounterSet, observe, support::*};
use serde_json::{Value, json};
use std::{sync::Arc, time::Duration};
async fn request(
h: &Endpoint,
revision: &str,
payload: Value,
accept: &str,
session: Option<&str>,
) -> reqwest::Response {
h.request(revision, session, &h.key, accept, payload)
.header("x-request-id", "negotiation-handler-atdd")
.header(
"traceparent",
"00-11111111111111111111111111111111-2222222222222222-01",
)
.send()
.await
.unwrap()
}
async fn terminal(response: reqwest::Response, sse: bool) -> Value {
assert_eq!(response.status(), 200);
let raw = tokio::time::timeout(Duration::from_secs(6), response.text())
.await
.unwrap()
.unwrap();
if sse {
let frames: Vec<Value> = raw
.lines()
.filter_map(|line| line.strip_prefix("data:"))
.map(|line| serde_json::from_str(line.trim()).unwrap())
.collect();
assert_eq!(frames.len(), 1, "single terminal response frame");
frames.into_iter().next().unwrap()
} else {
serde_json::from_str(&raw).unwrap()
}
}
fn handler_counts(c: &CounterSet) -> [usize; 6] {
[
c.sdk_initialize_handler_completed(),
c.sdk_initialize_handler_consumed(),
c.sdk_discover_handler_completed(),
c.sdk_discover_handler_consumed(),
c.sdk_ping_handler_completed(),
c.sdk_ping_handler_consumed(),
]
}
fn delta(after: [usize; 6], before: [usize; 6]) -> [usize; 6] {
std::array::from_fn(|index| after[index] - before[index])
}
#[tokio::test]
async fn rmcp_negotiation_handler_http_executes_and_consumes_initialize_discover_and_ping() {
crank_test_support::postgres_schema_url_for_task("negotiation_handler_owner", "t_219756db")
.await;
let h = Endpoint::started().await;
let session = h.session(LEGACY_REVISION).await;
let before_all = handler_counts(&h.counters);
for (revision, method, session) in [
(LEGACY_REVISION, "initialize", None),
(MODERN_REVISION, "server/discover", None),
] {
let mut payload = if method == "initialize" {
legacy_initialize(1)
} else {
json!({"jsonrpc":"2.0","id":1,"method":method,"params":{"_meta":modern_meta()}})
};
for accept in ["application/json", "text/event-stream, application/json"] {
if method == "initialize" {
payload["id"] = json!(if accept.starts_with("text/event") {
2
} else {
1
});
}
let response = request(&h, revision, payload.clone(), accept, session).await;
let body = terminal(response, accept.starts_with("text/event")).await;
assert_eq!(body["jsonrpc"], "2.0");
}
}
for revision in [LEGACY_REVISION, MODERN_REVISION] {
for accept in ["application/json", "text/event-stream, application/json"] {
let payload = json!({"jsonrpc":"2.0","id":"ping","method":"ping","params":{"_meta":if revision == MODERN_REVISION { modern_meta() } else { json!({"future":true}) }}});
let response = request(
&h,
revision,
payload,
accept,
if revision == LEGACY_REVISION {
session.as_deref()
} else {
None
},
)
.await;
let body = terminal(response, accept.starts_with("text/event")).await;
assert_eq!(body["id"], "ping");
assert!(body.get("error").is_none());
if revision == LEGACY_REVISION {
assert_eq!(body["result"]["protocolVersion"], LEGACY_REVISION);
}
}
}
let after = handler_counts(&h.counters);
assert_eq!(
delta(after, before_all),
[2, 2, 2, 2, 4, 4],
"successful initialize/discover/ping HTTP responses must complete in and consume their SDK handler results"
);
}
#[tokio::test]
async fn rmcp_negotiation_handler_observer_positive_control() {
let c = Arc::new(CounterSet::default());
observe(c.clone(), async {
super::sdk_initialize_handler_completed();
super::sdk_initialize_handler_consumed();
super::sdk_discover_handler_completed();
super::sdk_discover_handler_consumed();
super::sdk_ping_handler_completed();
super::sdk_ping_handler_consumed();
assert_eq!(handler_counts(&c), [1; 6]);
})
.await;
assert_eq!(handler_counts(&c), [1; 6]);
super::sdk_initialize_handler_completed();
assert_eq!(
handler_counts(&c),
[1; 6],
"markers outside observer scope are inert"
);
}