//! Behavioural tests of the BUILD-internal semantic observer (Story 3.1 section 7.2). //! //! This file is the `tests` child module of `crate::app::dispatch_observer` (wired with `#[path]` //! there), so the module path — and therefore the sanctioned selector //! `cargo test -p crank-community-mcp --lib app::dispatch_observer::` — is unchanged. The fixtures //! and helpers live in the sibling `support` module of the same parent. use super::support::*; use serde_json::json; use std::sync::Arc; // --------------------------------------------------------------------------------------------- // §7.2 pre-admission rejects: zero era-adapter entries, zero Tools-service entries, zero outbound // calls — the admission marker itself is the only increment. // --------------------------------------------------------------------------------------------- #[tokio::test] async fn dispatch_observer_pre_admission_rejects_never_enter_an_era_adapter_or_the_service() { let harness = Harness::started().await; let session = harness.initialize_legacy_session().await; // F7/G6: each reject below is an individual triple — a valid control of the same era, one // one-factor mutation of the chosen field, the reject with its exact marker/store/outbound // deltas, and an immediate valid follow-up with its own scoped output. One shared follow-up at // the end of the test would say nothing about the state a single reject left behind. // 1. malformed envelope: a session-bearing valid legacy call with only the envelope version // field mutated, so a zero era/service delta is a property of that one field. run_valid_legacy_call( &harness, &session, 11, "control-envelope@example.com", "control before the malformed envelope", ) .await; let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let mut malformed_body = legacy_tool_call(12, "envelope@example.com"); malformed_body["jsonrpc"] = json!("1.0"); let malformed = harness .post(&header_refs(&legacy_headers(&session)), &malformed_body) .await; assert_eq!( malformed.status().as_u16(), 400, "a malformed envelope is a rejection" ); assert_no_result("malformed envelope", &json_body(malformed).await); assert_deltas( "malformed envelope", before, harness.snapshot(), Snapshot::new(1, 0, 0, 0), ); assert_eq!(harness.upstream_calls(), arrivals); // F7: a reject before admission must not touch the session store at all — an extra `get` here is // invisible to the markers and to the outbound counter, so it is counted explicitly. assert_session_ops( "malformed envelope", ops_before, harness.session_ops(), SessionOps { get: 0, create: 0, mark_initialized: 0, }, ); run_valid_legacy_call( &harness, &session, 13, "follow-up-envelope@example.com", "follow-up after the malformed envelope", ) .await; // 2. ambiguous session carrier: the same valid legacy call with one conflicting session header. run_valid_legacy_call( &harness, &session, 21, "control-ambiguous@example.com", "control before the ambiguous session carrier", ) .await; let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let ambiguous = harness .post( &[ ("MCP-Protocol-Version", LEGACY_REVISION), ("MCP-Session-Id", session.as_str()), ("MCP-Session-Id", "session-of-another-era"), ], &legacy_tool_call(22, "ambiguous@example.com"), ) .await; assert_eq!( ambiguous.status().as_u16(), 400, "an ambiguous carrier is a rejection" ); assert_no_result("ambiguous session carrier", &json_body(ambiguous).await); assert_deltas( "ambiguous session carrier", before, harness.snapshot(), Snapshot::new(1, 0, 0, 0), ); assert_eq!(harness.upstream_calls(), arrivals); assert_session_ops( "ambiguous session carrier", ops_before, harness.session_ops(), SessionOps { get: 0, create: 0, mark_initialized: 0, }, ); run_valid_legacy_call( &harness, &session, 23, "follow-up-ambiguous@example.com", "follow-up after the ambiguous session carrier", ) .await; // 3. unresolvable identity: the same session-bearing legacy call with a modern `_meta` added, so // the header and the body name two different revisions. run_valid_legacy_call( &harness, &session, 31, "control-identity@example.com", "control before the unresolvable identity", ) .await; let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let mut unresolvable_body = legacy_tool_call(32, "unresolvable@example.com"); unresolvable_body["params"]["_meta"] = modern_meta(); let unresolvable = harness .post(&header_refs(&legacy_headers(&session)), &unresolvable_body) .await; assert_eq!( unresolvable.status().as_u16(), 400, "an unresolvable identity is a rejection" ); assert_no_result("unresolvable identity", &json_body(unresolvable).await); assert_deltas( "unresolvable identity", before, harness.snapshot(), Snapshot::new(1, 0, 0, 0), ); assert_eq!(harness.upstream_calls(), arrivals); assert_session_ops( "unresolvable identity", ops_before, harness.session_ops(), SessionOps { get: 0, create: 0, mark_initialized: 0, }, ); run_valid_legacy_call( &harness, &session, 33, "follow-up-identity@example.com", "follow-up after the unresolvable identity", ) .await; // 4. modern carrier conflict: a valid modern call that also carries the legacy session — the same // request with one extra carrier header. run_valid_modern_call( &harness, 41, "control-conflict@example.com", "control before the modern carrier conflict", ) .await; let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let mut conflicting = modern_headers(); conflicting.push(("MCP-Session-Id", session.clone())); let conflict = harness .post( &header_refs(&conflicting), &modern_tool_call(42, "conflict@example.com"), ) .await; assert_eq!(conflict.status().as_u16(), 400); assert_no_result("modern carrier conflict", &json_body(conflict).await); assert_deltas( "modern carrier conflict", before, harness.snapshot(), Snapshot::new(1, 0, 0, 0), ); assert_eq!(harness.upstream_calls(), arrivals); assert_session_ops( "modern carrier conflict", ops_before, harness.session_ops(), SessionOps { get: 0, create: 0, mark_initialized: 0, }, ); run_valid_modern_call( &harness, 43, "follow-up-conflict@example.com", "follow-up after the modern carrier conflict", ) .await; } // --------------------------------------------------------------------------------------------- // §7.2 in-adapter rejects: exactly the own era-adapter, never the other one, no service entry. // --------------------------------------------------------------------------------------------- #[tokio::test] async fn dispatch_observer_in_adapter_rejects_enter_exactly_their_own_era_and_no_service() { let harness = Harness::started().await; let session = harness.initialize_legacy_session().await; // F7: every in-adapter reject gets its own control → one-factor negative → follow-up triple, so a // reject can never be confused with a broken endpoint and the state after the reject is proven to // serve the very next request. run_valid_legacy_call( &harness, &session, 30, "control-legacy@example.com", "control before the legacy reject", ) .await; // Legacy unknown method: a rejection inside the legacy adapter. The negative is the valid legacy // call body of this era with exactly one factor changed - the method identifier - so the same // `params`/`name`/`arguments` payload is proven not to reach the owning Tools service. let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let mut legacy_unknown_request = legacy_tool_call(31, "unknown-legacy@example.com"); legacy_unknown_request["method"] = json!("tools/not-a-method"); let legacy_unknown = harness .post( &header_refs(&legacy_headers(&session)), &legacy_unknown_request, ) .await; let legacy_unknown_body = json_body(legacy_unknown).await; assert_eq!( legacy_unknown_body["error"]["code"], -32601, "an unregistered legacy method is an unsupported-method error, got {legacy_unknown_body}" ); assert_deltas( "legacy unknown method", before, harness.snapshot(), Snapshot::new(1, 0, 1, 0), ); assert_eq!(harness.upstream_calls(), arrivals); // The legacy carrier is still resolved by the admission's revision check (one read), and the // reject neither creates nor mutates a session. assert_session_ops( "legacy unknown method", ops_before, harness.session_ops(), SessionOps { get: 1, create: 0, mark_initialized: 0, }, ); run_valid_legacy_call( &harness, &session, 31, "follow-up-legacy@example.com", "follow-up after the legacy reject", ) .await; // Modern unknown method: the control is a valid modern call of the same era, the negative is that // same valid call with only the method identifier changed - the body method and its coherent // `Mcp-Method` wire carrier, both denoting the one logical field - and the follow-up is an // independent modern success on a session store that must still be untouched. run_valid_modern_call( &harness, 33, "control-modern@example.com", "control before the modern reject", ) .await; let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let mut modern_unknown_request = modern_tool_call(34, "unknown-modern@example.com"); modern_unknown_request["method"] = json!("tools/not-a-method"); let modern_unknown = harness .post( &[ ("MCP-Protocol-Version", MODERN_REVISION), ("Mcp-Method", "tools/not-a-method"), ("Mcp-Name", TOOL_NAME), ], &modern_unknown_request, ) .await; let modern_unknown_body = json_body(modern_unknown).await; assert_eq!( modern_unknown_body["error"]["code"], -32601, "an unregistered modern method is an unsupported-method error, got {modern_unknown_body}" ); assert_deltas( "modern unknown method", before, harness.snapshot(), Snapshot::new(1, 1, 0, 0), ); assert_eq!(harness.upstream_calls(), arrivals); assert_session_ops( "modern unknown method", ops_before, harness.session_ops(), SessionOps { get: 0, create: 0, mark_initialized: 0, }, ); // The independent modern follow-up after the reject: its own era, its own scoped output, and a // session store that is still untouched. run_valid_modern_call( &harness, 35, "follow-up-modern@example.com", "follow-up after the modern reject", ) .await; } // --------------------------------------------------------------------------------------------- // G2: the registered kind of the two legacy lifecycle operations — an envelope of the wrong kind may // not be served as the other action. // --------------------------------------------------------------------------------------------- #[tokio::test] async fn dispatch_observer_lifecycle_envelope_kind_cannot_swap_the_two_legacy_actions() { let harness = Harness::started().await; let session = harness.initialize_legacy_session().await; // Form 1: `initialize` written as a notification — the valid request control with only its `id` // removed. `initialize` is registered as a request, so this form must neither initialize the live // session nor mint a new one. run_valid_legacy_initialize(&harness, 61, "control initialize request").await; let mut initialize_notification = legacy_initialize(62); initialize_notification .as_object_mut() .expect("the initialize request is an object") .remove("id"); assert!(initialize_notification.get("id").is_none()); let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let notification = harness .post( &header_refs(&legacy_headers(&session)), &initialize_notification, ) .await; assert_eq!( notification.status().as_u16(), 202, "an initialize notification is acknowledged, never served as an initialize request" ); assert_deltas( "initialize as a notification", before, harness.snapshot(), Snapshot::new(1, 0, 1, 0), ); assert_eq!(harness.upstream_calls(), arrivals); // The admission's revision check reads the carried session once; the wrong-kind form creates no // session and marks none initialized. assert_session_ops( "initialize as a notification", ops_before, harness.session_ops(), SessionOps { get: 1, create: 0, mark_initialized: 0, }, ); run_valid_legacy_call( &harness, &session, 63, "follow-up-initialize-notification@example.com", "follow-up after the initialize notification", ) .await; // Form 2: `notifications/initialized` written as a request — the valid notification control with // only an `id` added. It must not create a session: the registered kind is a notification. run_valid_initialized_notification(&harness, &session, "control initialized notification") .await; let mut initialized_request = initialized_notification(); initialized_request["id"] = json!(72); assert!(initialized_request.get("id").is_some()); let before = harness.snapshot(); let ops_before = harness.session_ops(); let arrivals = harness.upstream_calls(); let request = harness .post( &header_refs(&legacy_headers(&session)), &initialized_request, ) .await; let request_body = json_body(request).await; assert_eq!( request_body["error"]["code"], -32601, "a request-shaped initialized notification is an unsupported method, got {request_body}" ); assert_deltas( "initialized as a request", before, harness.snapshot(), Snapshot::new(1, 0, 1, 0), ); assert_eq!(harness.upstream_calls(), arrivals); assert_session_ops( "initialized as a request", ops_before, harness.session_ops(), SessionOps { get: 1, create: 0, mark_initialized: 0, }, ); run_valid_legacy_call( &harness, &session, 73, "follow-up-initialized-request@example.com", "follow-up after the initialized request", ) .await; } // --------------------------------------------------------------------------------------------- // §7.2 valid calls: the own era-adapter, the owning Tools service and exactly one outbound call per // call, with scoped outputs. // --------------------------------------------------------------------------------------------- #[tokio::test] async fn dispatch_observer_valid_calls_enter_the_own_era_and_the_owning_tools_service() { let harness = Harness::started().await; let session = harness.initialize_legacy_session().await; run_valid_legacy_call( &harness, &session, 41, "legacy@example.com", "valid legacy tools/call", ) .await; run_valid_modern_call( &harness, 42, "modern@example.com", "valid modern tools/call", ) .await; // The legacy session never served the modern call: the legacy follow-up still works, with its own // scoped output, and the modern call did not consume it. run_valid_legacy_call( &harness, &session, 43, "legacy-after-modern@example.com", "legacy follow-up after the modern call", ) .await; } // --------------------------------------------------------------------------------------------- // F5: the registration drives the *real* endpoint. One private inert method descriptor is added to a // copy of the production tables and served with this router; removing it again restores the rejection. // --------------------------------------------------------------------------------------------- #[tokio::test] async fn dispatch_observer_registration_add_remove_changes_the_real_endpoint_dispatch() { // "Removed": the inert method is not registered, so both eras answer the era-correct -32601 and // nothing enters an era handler beyond the rejection, the service or the upstream. let without = Harness::started_with_registration(Some(Arc::new(registration_without_inert_method()))) .await; let session = without.initialize_legacy_session().await; let before = without.snapshot(); let arrivals = without.upstream_calls(); let unregistered = without .post( &header_refs(&legacy_headers(&session)), &json!({ "jsonrpc": "2.0", "id": 51, "method": INERT_METHOD_NAME, "params": {} }), ) .await; let unregistered_body = json_body(unregistered).await; assert_eq!( unregistered_body["error"]["code"], -32601, "an unregistered method must stay unsupported, got {unregistered_body}" ); assert_deltas( "inert method without a registration (legacy)", before, without.snapshot(), Snapshot::new(1, 0, 1, 0), ); assert_eq!(without.upstream_calls(), arrivals); let before = without.snapshot(); let unregistered_modern = without .post( &[ ("MCP-Protocol-Version", MODERN_REVISION), ("Mcp-Method", INERT_METHOD_NAME), ("Mcp-Name", TOOL_NAME), ], &json!({ "jsonrpc": "2.0", "id": 52, "method": INERT_METHOD_NAME, "params": { "_meta": modern_meta() } }), ) .await; assert_eq!( json_body(unregistered_modern).await["error"]["code"], -32601, "an unregistered modern method must stay unsupported" ); assert_deltas( "inert method without a registration (modern)", before, without.snapshot(), Snapshot::new(1, 1, 0, 0), ); // "Added": the very same request now reaches the handler the registration maps it to — in both // eras, through the real router, with no change to the production tables. let with = Harness::started_with_registration(Some(Arc::new(registration_with_inert_method()))).await; let session = with.initialize_legacy_session().await; let before = with.snapshot(); let arrivals = with.upstream_calls(); let dispatched = with .post( &header_refs(&legacy_headers(&session)), &json!({ "jsonrpc": "2.0", "id": 53, "method": INERT_METHOD_NAME, "params": {} }), ) .await; let dispatched_body = json_body(dispatched).await; assert_eq!( dispatched_body["result"]["protocolVersion"], LEGACY_REVISION, "the registered method must reach its registered handler (legacy), got {dispatched_body}" ); assert_deltas( "inert method with a registration (legacy)", before, with.snapshot(), Snapshot::new(1, 0, 1, 0), ); assert_eq!( with.upstream_calls(), arrivals, "the inert handler is not the Tools service: no outbound call is made" ); let before = with.snapshot(); let dispatched_modern = with .post( &[ ("MCP-Protocol-Version", MODERN_REVISION), ("Mcp-Method", INERT_METHOD_NAME), ("Mcp-Name", TOOL_NAME), ], &json!({ "jsonrpc": "2.0", "id": 54, "method": INERT_METHOD_NAME, "params": { "_meta": modern_meta() } }), ) .await; let dispatched_modern_body = json_body(dispatched_modern).await; assert_eq!( dispatched_modern_body["result"]["protocolVersion"], MODERN_REVISION, "the registered method must reach its registered handler (modern), got {dispatched_modern_body}" ); assert_deltas( "inert method with a registration (modern)", before, with.snapshot(), Snapshot::new(1, 1, 0, 0), ); // The advertisement moves with the same registration snapshot: the inert capability is declared // by the observed registration and appears in the modern discovery result it serves. let discover = with .post( &[ ("MCP-Protocol-Version", MODERN_REVISION), ("Mcp-Method", "server/discover"), ], &json!({ "jsonrpc": "2.0", "id": 55, "method": "server/discover", "params": { "_meta": modern_meta() } }), ) .await; let discover_body = json_body(discover).await; assert_eq!( discover_body["result"]["capabilities"]["inert_test_capability"]["enabled"], true, "the observed registration owns the advertisement too, got {discover_body}" ); let discover_without = without .post( &[ ("MCP-Protocol-Version", MODERN_REVISION), ("Mcp-Method", "server/discover"), ], &json!({ "jsonrpc": "2.0", "id": 56, "method": "server/discover", "params": { "_meta": modern_meta() } }), ) .await; assert!( json_body(discover_without).await["result"]["capabilities"] .get("inert_test_capability") .is_none(), "a capability that is not registered in this snapshot must not be advertised" ); }