//! In-crate tests of `protocol::completion` (module path `protocol::completion::tests`). //! //! These cover the pure halves of the shared service: the bounded input contract of AC2 and the //! suggestion derivation of AC1. The real endpoint behaviour (both eras, real registry) is covered by //! `apps/mcp-server/tests/integration/mcp_completion.rs`. use super::*; use serde_json::json; /// Any supported revision: the suggestion derivation is era-neutral. const MODERN: &str = crate::jsonrpc::MODERN_PROTOCOL_VERSION; fn candidates(values: &[&str]) -> Vec { values.iter().map(|value| (*value).to_owned()).collect() } fn values_of(outcome: &CompletionOutcome) -> (&[String], Option, bool) { match outcome { CompletionOutcome::Completed { values, total, has_more, } => (values, *total, *has_more), _ => panic!("expected a completed outcome"), } } fn command(params: Value) -> Result { serde_json::from_value(params) } #[test] fn matching_is_a_case_insensitive_prefix_in_schema_order() { let outcome = completed( MODERN, "prompt", "W", candidates(&["warm", "dry", "wild", "warmest"]), ); let (values, total, has_more) = values_of(&outcome); assert_eq!(values, ["warm", "wild", "warmest"]); assert_eq!(total, Some(3)); assert!(!has_more); } #[test] fn an_empty_token_lists_the_bounded_enumeration() { let outcome = completed(MODERN, "prompt", "", candidates(&["a", "b", "c"])); let (values, total, has_more) = values_of(&outcome); assert_eq!(values, ["a", "b", "c"]); assert_eq!(total, Some(3)); assert!(!has_more); } #[test] fn a_token_without_a_match_returns_no_suggestions() { let outcome = completed(MODERN, "resource", "zzz", candidates(&["eu", "us"])); let (values, total, has_more) = values_of(&outcome); assert!(values.is_empty()); assert_eq!(total, Some(0)); assert!(!has_more); } #[test] fn the_response_is_capped_at_the_pinned_maximum_and_reports_more() { let all: Vec = (0..150).map(|index| format!("value-{index:03}")).collect(); let outcome = completed(MODERN, "prompt", "value-", all); let (values, total, has_more) = values_of(&outcome); assert_eq!(values.len(), MAX_COMPLETION_VALUES); assert_eq!(values.first().map(String::as_str), Some("value-000")); assert_eq!(total, Some(150), "the exact option count is still reported"); assert!(has_more); } #[test] fn duplicate_enumeration_entries_are_collapsed_before_counting() { let outcome = completed(MODERN, "prompt", "", candidates(&["eu", "eu", "us"])); let (values, total, has_more) = values_of(&outcome); assert_eq!(values, ["eu", "us"]); assert_eq!(total, Some(2)); assert!(!has_more); } #[test] fn a_truncated_schema_scan_never_claims_an_exact_total() { let all: Vec = (0..=MAX_SCHEMA_VALUES) .map(|index| format!("item{index}")) .collect(); let outcome = completed(MODERN, "resource", "item", all); let (values, total, has_more) = values_of(&outcome); assert_eq!(values.len(), MAX_COMPLETION_VALUES); assert_eq!(total, None, "a partial scan cannot name the option count"); assert!(has_more); } #[test] fn only_scalar_enumeration_entries_become_suggestions() { assert_eq!(value_text(&json!("eu")), Some("eu".to_owned())); assert_eq!(value_text(&json!(7)), Some("7".to_owned())); assert_eq!(value_text(&json!(true)), Some("true".to_owned())); assert_eq!(value_text(&json!(null)), None); assert_eq!(value_text(&json!(["eu"])), None); assert_eq!(value_text(&json!({"value": "eu"})), None); } #[test] fn a_non_ascii_token_matches_exactly_and_never_slices_mid_character() { assert!(prefix_match("привет", "пр")); // A two-byte Cyrillic letter is one character, so it is a plain prefix. assert!(prefix_match("привет", "п")); assert!(prefix_match("Ärger", "Är")); // Two ASCII bytes end inside the multi-byte `é`: no prefix is claimed for a mid-character cut. assert!(!prefix_match("aé", "ab")); assert!(!prefix_match("warm", "warmest")); } #[test] fn a_refusal_discloses_neither_reason_nor_identifiers() { assert!(matches!( not_available(MODERN, "prompt"), CompletionOutcome::NotAvailable )); assert!(matches!( refused(MODERN, "resource", "schema_unavailable"), CompletionOutcome::CatalogUnavailable )); } #[test] fn both_typed_references_parse_and_a_foreign_ref_type_is_rejected() { let prompt = command(json!({ "ref": {"type": "ref/prompt", "name": "greeting"}, "argument": {"name": "tone", "value": "w"} })) .expect("the prompt reference is the pinned shape"); assert_eq!(prompt.reference_label(), "prompt"); assert!(prompt.context.is_none()); let resource = command(json!({ "ref": {"type": "ref/resource", "uri": "crank://orders/{order_id}"}, "argument": {"name": "order_id", "value": "0"}, "context": {"arguments": {"region": "eu"}} })) .expect("the resource reference with context is the pinned shape"); assert_eq!(resource.reference_label(), "resource"); assert!(resource.validate().is_none()); assert!( command(json!({ "ref": {"type": "ref/tool", "name": "x"}, "argument": {"name": "a", "value": ""} })) .is_err(), "an unknown reference type is not a completion reference" ); } #[test] fn oversized_and_malformed_requests_are_refused_before_any_catalog_read() { let long_value = "v".repeat(MAX_ARGUMENT_VALUE_BYTES + 1); let oversized = command(json!({ "ref": {"type": "ref/prompt", "name": "greeting"}, "argument": {"name": "tone", "value": long_value} })) .expect("the request shape parses; only its bound is exceeded"); assert_eq!(oversized.validate(), Some(INVALID_PARAMS)); let long_name = "n".repeat(MAX_ARGUMENT_NAME_BYTES + 1); let oversized_name = command(json!({ "ref": {"type": "ref/prompt", "name": "greeting"}, "argument": {"name": long_name, "value": ""} })) .expect("the request shape parses"); assert_eq!(oversized_name.validate(), Some(INVALID_PARAMS)); let empty_argument = command(json!({ "ref": {"type": "ref/resource", "uri": "crank://orders/{order_id}"}, "argument": {"name": "", "value": ""} })) .expect("the request shape parses"); assert_eq!(empty_argument.validate(), Some(INVALID_PARAMS)); let empty_reference = command(json!({ "ref": {"type": "ref/prompt", "name": ""}, "argument": {"name": "tone", "value": ""} })) .expect("the request shape parses"); assert_eq!(empty_reference.validate(), Some(INVALID_PARAMS)); let long_uri = "u".repeat(MAX_REFERENCE_URI_BYTES + 1); let oversized_uri = command(json!({ "ref": {"type": "ref/resource", "uri": long_uri}, "argument": {"name": "order_id", "value": ""} })) .expect("the request shape parses"); assert_eq!(oversized_uri.validate(), Some(INVALID_PARAMS)); } #[test] fn the_optional_context_is_validated_as_the_pinned_string_map() { let non_string = command(json!({ "ref": {"type": "ref/prompt", "name": "greeting"}, "argument": {"name": "tone", "value": ""}, "context": {"arguments": {"region": 7}} })) .expect("the request shape parses"); assert_eq!(non_string.validate(), Some(INVALID_PARAMS)); let mut arguments = Map::new(); for index in 0..=MAX_CONTEXT_ARGUMENTS { arguments.insert(format!("argument{index}"), json!("value")); } let too_many = command(json!({ "ref": {"type": "ref/prompt", "name": "greeting"}, "argument": {"name": "tone", "value": ""}, "context": {"arguments": arguments} })) .expect("the request shape parses"); assert_eq!(too_many.validate(), Some(INVALID_PARAMS)); }