Files
crank/crates/crank-community-mcp/src/rmcp_notification_handler_runtime_tests.rs
T
bsodfather fd76aaf676
CI / UI Checks (pull_request) Successful in 9s
CI / Rust Checks (pull_request) Successful in 40m6s
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 1s
CI / Deploy (pull_request) Skipped
MCP: execute legacy notifications through SDK handlers
2026-11-14 00:00:27 +00:00

121 lines
4.2 KiB
Rust

//! #107 real HTTP contract for awaited legacy SDK notification handlers.
use super::rmcp_progress_cancel_runtime_tests::fixtures::Endpoint;
use super::support::*;
use serde_json::{Value, json};
use std::time::Duration;
async fn legacy_session(h: &Endpoint) -> String {
let response = h
.request(
LEGACY_REVISION,
None,
&h.key,
"application/json",
legacy_initialize(801),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 200);
let session = response.headers()["MCP-Session-Id"]
.to_str()
.unwrap()
.to_owned();
let body: Value = json_body(response).await;
assert_eq!(body["result"]["protocolVersion"], LEGACY_REVISION);
session
}
#[tokio::test]
async fn initialized_handler_executes_and_is_consumed_by_real_http_caller() {
crank_test_support::postgres_schema_url_for_task("notification_handler", "t_2a52960d").await;
let h = Endpoint::started().await;
let session = legacy_session(&h).await;
let response = h
.request(
LEGACY_REVISION,
Some(&session),
&h.key,
"application/json",
json!({"jsonrpc":"2.0","method":"notifications/initialized"}),
)
.send()
.await
.unwrap();
assert_eq!(response.status(), 202, "legacy HTTP wire positive control");
assert!(response.bytes().await.unwrap().is_empty());
assert!(h.sessions.retained(&session).await.unwrap().initialized);
assert_eq!(
h.counters.sdk_initialized_handler_completed(),
1,
"real awaited on_initialized must complete its durable operation"
);
assert_eq!(
h.counters.sdk_initialized_handler_consumed(),
1,
"HTTP caller must consume the outcome of that same SDK handler"
);
}
#[tokio::test]
async fn cancelled_handler_executes_and_is_consumed_for_owned_active_call() {
crank_test_support::postgres_schema_url_for_task("notification_handler", "t_2a52960d").await;
let h = Endpoint::started().await;
let session = h.session(LEGACY_REVISION).await.unwrap();
let call = h.call(
LEGACY_REVISION,
Some(&session),
json!(802),
Some(json!("cancel")),
"text/event-stream",
);
let active = tokio::spawn(async move { call.send().await.unwrap().text().await.unwrap() });
h.wait_started(1).await;
h.cancel(&h.key, &session, json!({"requestId":802})).await;
tokio::time::timeout(Duration::from_secs(2), active)
.await
.unwrap()
.unwrap();
assert_eq!(
h.counters.sdk_cancelled_handler_completed(),
1,
"real awaited on_cancelled must cancel the owned call"
);
assert_eq!(
h.counters.sdk_cancelled_handler_consumed(),
1,
"HTTP caller must consume the outcome of that same SDK handler"
);
}
#[tokio::test]
async fn modern_notifications_remain_ack_only_without_legacy_handlers() {
crank_test_support::postgres_schema_url_for_task("notification_handler", "t_2a52960d").await;
let h = Endpoint::started().await;
let before = h.sessions.snapshot();
let response = h.request(MODERN_REVISION, None, &h.key, "application/json",
json!({"jsonrpc":"2.0","method":"notifications/initialized","params":{"_meta":modern_meta()}}))
.send().await.unwrap();
assert_eq!(response.status(), 202);
assert!(response.bytes().await.unwrap().is_empty());
let response = h.request(MODERN_REVISION, None, &h.key, "application/json",
json!({"jsonrpc":"2.0","method":"notifications/cancelled","params":{"requestId":802,"_meta":modern_meta()}}))
.send().await.unwrap();
assert_eq!(response.status(), 202);
assert!(response.bytes().await.unwrap().is_empty());
assert_session_ops(
"modern notification ACK",
before,
h.sessions.snapshot(),
SessionOps {
get: 0,
create: 0,
mark_initialized: 0,
},
);
assert_eq!(h.counters.sdk_initialized_handler_completed(), 0);
assert_eq!(h.counters.sdk_initialized_handler_consumed(), 0);
assert_eq!(h.counters.sdk_cancelled_handler_completed(), 0);
assert_eq!(h.counters.sdk_cancelled_handler_consumed(), 0);
}