//! Story 3.17 acceptance tests: bounded completion of the arguments of bound Published Prompts and of //! the parameters of bound Published Resource templates through MCP (AT-3.17-01..03). //! //! Oracles: `docs/acceptance-test-plan.md` AT-3.17-01..03, `epics.md` Story 3.17 and the pinned //! `completion/complete` schemas of both supported revisions. Every request travels through the real //! HTTP endpoint built by `build_test_app` against a real PostgreSQL registry; the bound versions are //! produced through the production registry path. Each case runs on both supported revisions, because //! AC3 requires per-revision positive/negative/authorization coverage. //! //! No real external MCP client is exercised here: these are local harness results against the pinned //! wire shapes. That is stated as NOT_RUN in the story handoff instead of being claimed as a client //! qualification. mod fixtures; mod response_metrics; use serde_json::{Value, json}; use fixtures::*; fn completion(body: &Value) -> &Value { body.pointer("/result/completion") .unwrap_or_else(|| panic!("the completion result is missing: {body}")) } fn values(body: &Value) -> Vec { completion(body)["values"] .as_array() .unwrap_or_else(|| panic!("`completion.values` must be an array: {body}")) .iter() .map(|value| { value .as_str() .unwrap_or_else(|| panic!("every completion value is a string: {body}")) .to_owned() }) .collect() } fn error(body: &Value) -> (i64, String) { let code = body .pointer("/error/code") .and_then(Value::as_i64) .unwrap_or_else(|| panic!("expected a JSON-RPC error code: {body}")); let message = body .pointer("/error/message") .and_then(Value::as_str) .unwrap_or_else(|| panic!("expected a JSON-RPC error message: {body}")) .to_owned(); (code, message) } fn prompt_ref(name: &str) -> Value { json!({"type": "ref/prompt", "name": name}) } fn resource_ref(uri: &str) -> Value { json!({"type": "ref/resource", "uri": uri}) } fn request(reference: Value, argument: &str, value: &str) -> Value { json!({ "ref": reference, "argument": {"name": argument, "value": value} }) } /// AT-3.17-01/03: the advertised capability equals the implemented method on both revisions. #[tokio::test] async fn advertisement_declares_completions_on_both_revisions() { let endpoint = completion_endpoint().await; let discover = endpoint.modern_method("server/discover", json!({})).await; assert_eq!( discover["result"]["capabilities"]["completions"], json!({}), "the modern advertisement must declare the completions capability: {discover}" ); let initialize = endpoint.legacy_initialize().await; assert_eq!( initialize["result"]["capabilities"]["completions"], json!({}), "the legacy advertisement must declare the completions capability: {initialize}" ); } /// AT-3.17-01: bounded, authorized suggestions from the typed schema of the bound Prompt argument. #[tokio::test] async fn prompt_argument_completion_is_bounded_and_typed_on_every_revision() { let endpoint = completion_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { let all = endpoint .on_revision(revision, request(prompt_ref("greeting"), "tone", "")) .await; assert_eq!( values(&all), ["warm", "dry"], "the whole typed enumeration is suggested on {revision}: {all}" ); assert_eq!( completion(&all)["total"], json!(2), "the exact option count is reported on {revision}: {all}" ); assert!( completion(&all).get("hasMore").is_none(), "a complete enumeration carries no `hasMore` on {revision}: {all}" ); let filtered = endpoint .on_revision(revision, request(prompt_ref("greeting"), "tone", "w")) .await; assert_eq!( values(&filtered), ["warm"], "the partial token filters the enumeration on {revision}: {filtered}" ); assert_eq!(completion(&filtered)["total"], json!(1)); // Prefix matching is ASCII-case-insensitive, so a client that upper-cases a token still gets // the published option; the value itself is served exactly as published. let caseless = endpoint .on_revision(revision, request(prompt_ref("greeting"), "tone", "D")) .await; assert_eq!( values(&caseless), ["dry"], "case-insensitive prefix matching on {revision}: {caseless}" ); } } /// AT-3.17-01: a typed schema without an enumeration suggests nothing rather than inventing values. #[tokio::test] async fn an_argument_without_an_enumeration_gets_no_invented_suggestions() { let endpoint = completion_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { let body = endpoint .on_revision(revision, request(prompt_ref("greeting"), "name", "A")) .await; assert_eq!( values(&body), Vec::::new(), "a plain string argument has no enumerated suggestions on {revision}: {body}" ); assert_eq!(completion(&body)["total"], json!(0)); assert!(completion(&body).get("hasMore").is_none()); } } /// AT-3.17-01/03: a Draft, unbound, foreign or unknown Prompt (and an unknown argument) is one /// indistinguishable refusal that discloses no identifier. #[tokio::test] async fn a_draft_unbound_or_foreign_prompt_is_indistinguishable() { let endpoint = completion_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { let mut observed = Vec::new(); for (case, request_value) in [ ( "foreign prompt", request(prompt_ref("other-agent-prompt"), "tone", ""), ), ( "draft prompt", request(prompt_ref("draft-prompt"), "tone", ""), ), ( "unbound prompt", request(prompt_ref("unbound-prompt"), "tone", ""), ), ( "unknown prompt", request(prompt_ref("no-such-prompt"), "tone", ""), ), ( "unknown argument", request(prompt_ref("greeting"), "no-such-argument", ""), ), ] { let body = endpoint.on_revision(revision, request_value).await; let (code, message) = error(&body); assert_eq!(code, -32602, "{case} on {revision}: {body}"); assert_eq!( message, NOT_AVAILABLE, "{case} on {revision} must use the single safe refusal: {body}" ); assert!( body.get("result").is_none(), "{case} on {revision} must not carry a result: {body}" ); let serialized = body.to_string(); for leaked in [ "other-agent-prompt", "draft-prompt", "unbound-prompt", "no-such-prompt", "no-such-argument", "Other agent secret.", "Draft content.", "Unbound content.", ] { assert!( !serialized.contains(leaked), "{case} on {revision} must not disclose {leaked:?}: {serialized}" ); } observed.push((code, message)); } assert!( observed.windows(2).all(|pair| pair[0] == pair[1]), "every unavailable case must answer identically on {revision}: {observed:?}" ); } } /// AT-3.17-01: the second supported reference form — a bound Resource template parameter. #[tokio::test] async fn resource_template_parameter_completion_uses_the_bound_schema() { let endpoint = completion_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { let all = endpoint .on_revision( revision, request(resource_ref(BOUND_TEMPLATE_URI), "region", ""), ) .await; assert_eq!( values(&all), ["eu", "us", "apac"], "the bound template's enumerated parameter is suggested on {revision}: {all}" ); assert_eq!(completion(&all)["total"], json!(3)); let filtered = endpoint .on_revision( revision, request(resource_ref(BOUND_TEMPLATE_URI), "region", "a"), ) .await; assert_eq!( values(&filtered), ["apac"], "the partial token filters the template parameter on {revision}: {filtered}" ); } } /// AT-3.17-01/03: a foreign or unknown URI template, and an unknown parameter, share one refusal. #[tokio::test] async fn a_foreign_or_unknown_template_uri_is_indistinguishable() { let endpoint = completion_endpoint().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { let mut observed = Vec::new(); for request_value in [ request(resource_ref(FOREIGN_TEMPLATE_URI), "region", ""), request(resource_ref("crank://no-such/{region}"), "region", ""), request(resource_ref(BOUND_TEMPLATE_URI), "no-such-parameter", ""), ] { let body = endpoint.on_revision(revision, request_value).await; let (code, message) = error(&body); assert_eq!(code, -32602, "{body}"); assert_eq!(message, NOT_AVAILABLE, "{body}"); let serialized = body.to_string(); for leaked in [ FOREIGN_TEMPLATE_URI, "crank://no-such/{region}", "no-such-parameter", ] { assert!( !serialized.contains(leaked), "an unavailable template must not disclose {leaked:?}: {serialized}" ); } observed.push((code, message)); } assert!( observed.windows(2).all(|pair| pair[0] == pair[1]), "every unavailable template case must answer identically on {revision}: {observed:?}" ); } } /// AT-3.17-02: malformed or oversized requests are safe errors and never echo the input. #[tokio::test] async fn malformed_requests_are_safe_errors_without_echoing_input() { let endpoint = completion_endpoint().await; let oversized_token = "s".repeat(2_000); for revision in [MODERN_REVISION, LEGACY_REVISION] { for (case, params) in [ ( "missing ref", json!({"argument": {"name": "tone", "value": ""}}), ), ( "unknown ref type", request(json!({"type": "ref/tool", "name": "x"}), "tone", ""), ), ( "argument is not an object", json!({"ref": prompt_ref("greeting"), "argument": "tone"}), ), ( "oversized token", request(prompt_ref("greeting"), "tone", &oversized_token), ), ( "oversized argument name", request(prompt_ref("greeting"), &"n".repeat(200), ""), ), ( "context with a non-string value", json!({ "ref": prompt_ref("greeting"), "argument": {"name": "tone", "value": ""}, "context": {"arguments": {"region": 7}} }), ), ] { let body = endpoint.on_revision(revision, params).await; let (code, message) = error(&body); assert_eq!(code, -32602, "{case} on {revision}: {body}"); assert!( message == INVALID_PARAMS || message == NOT_AVAILABLE, "{case} on {revision} must be answered with one safe detail: {body}" ); let serialized = body.to_string(); assert!( !serialized.contains(&oversized_token), "{case} on {revision} must not echo the client token: {serialized}" ); assert!( !serialized.contains(&"n".repeat(200)), "{case} on {revision} must not echo the argument name: {serialized}" ); } } } /// AT-3.17-03: the authorization boundary is the Agent route and its bound snapshot — the other /// Agent of the same workspace cannot address this Agent's Prompt or template, and this Agent cannot /// address the other Agent's. #[tokio::test] async fn the_other_agents_snapshot_is_not_addressable() { let endpoint = completion_endpoint().await; let other_key = endpoint.other_agent_key().await; for revision in [MODERN_REVISION, LEGACY_REVISION] { // The other Agent's credential and route complete its own Prompt argument… let own = match revision { MODERN_REVISION => { endpoint .modern_for( OTHER_AGENT_SLUG, &other_key, request(prompt_ref("other-agent-prompt"), "tone", ""), ) .await } _ => { endpoint .legacy_for( OTHER_AGENT_SLUG, &other_key, request(prompt_ref("other-agent-prompt"), "tone", ""), ) .await } }; let (code, message) = error(&own); assert_eq!( (code, message.as_str()), (-32602, NOT_AVAILABLE), "the other Agent's Prompt has no argument of that name, so nothing is suggested on {revision}: {own}" ); // …and cannot address this Agent's bound Prompt or template. for request_value in [ request(prompt_ref("greeting"), "tone", ""), request(resource_ref(BOUND_TEMPLATE_URI), "region", ""), ] { let body = match revision { MODERN_REVISION => { endpoint .modern_for(OTHER_AGENT_SLUG, &other_key, request_value) .await } _ => { endpoint .legacy_for(OTHER_AGENT_SLUG, &other_key, request_value) .await } }; let (code, message) = error(&body); assert_eq!( (code, message.as_str()), (-32602, NOT_AVAILABLE), "a foreign snapshot is not addressable on {revision}: {body}" ); } } }