//! The gates `search_records` owes that no integration test can give it: the //! two vocabularies its refusals print, checked against the generated //! catalogue — which is emitted from the TypeScript and is therefore the other //! half of both lists. use serde_json::{json, Map, Value}; use super::{validate_search, SEARCH_KINDS, SEARCH_MODES}; /// The enum inside `search_records`'s generated `inputSchema`, for a property. fn schema_enum(property: &str) -> Vec { let tool = crate::mcp::protocol::tool_by_name("search_records").expect("search_records"); let schema = tool.input_schema_json(); let node = &schema["properties"][property]; // `kinds` is an array property; its enum sits on `items`. let holder = if node.get("items").is_some() { &node["items"] } else { node }; holder["enum"] .as_array() .expect("an enum in the generated schema") .iter() .map(|value| value.as_str().unwrap_or_default().to_string()) .collect() } // RULE: Search kinds follow the shared table-id grammar in both the catalogue and validator, so imported tables can be searched by id. #[test] fn catalogue_allows_user_table_ids() { let schema = crate::mcp::protocol::tool_by_name("search_records").unwrap().input_schema_json(); assert_eq!(schema["properties"]["kinds"]["items"]["pattern"], "^[a-z][a-z0-9_]*$"); assert!(schema["properties"]["kinds"]["items"].get("enum").is_none()); let parsed = validate_search(&args(json!({"query":"x","kinds":["mixtape"]}))).unwrap(); assert_eq!(parsed.kinds, vec!["mixtape"]); assert!(SEARCH_KINDS.iter().all(|kind| cotton_core::definitions::is_valid_id(kind))); } #[test] fn catalogue_names_the_same_search_modes_in_the_same_order() { assert_eq!(schema_enum("mode"), SEARCH_MODES.to_vec()); } fn args(value: Value) -> Map { value.as_object().cloned().unwrap_or_default() } #[test] fn a_query_is_required() { let refusal = validate_search(&args(json!({}))).expect_err("no query"); assert_eq!(refusal.message(), "query is required"); } #[test] fn an_empty_kinds_array_reads_as_the_whole_file() { let parsed = validate_search(&args(json!({ "query": "x", "kinds": [] }))).expect("valid"); assert_eq!(parsed.kinds, SEARCH_KINDS.to_vec()); } #[test] fn an_invalid_table_id_is_named_with_the_vocabulary() { let refusal = validate_search(&args(json!({ "query": "x", "kinds": ["bad-id"] }))).expect_err("refused"); assert_eq!( refusal.message(), format!( "invalid kind \"bad-id\". Use a table id or one of: {}", SEARCH_KINDS.join(", ") ) ); } #[test] fn kinds_must_be_an_array() { let refusal = validate_search(&args(json!({ "query": "x", "kinds": "note" }))).expect_err("refused"); assert_eq!(refusal.message(), "kinds must be an array"); } #[test] fn an_unknown_mode_names_the_three() { let refusal = validate_search(&args(json!({ "query": "x", "mode": "semantic" }))).expect_err("refused"); assert_eq!( refusal.message(), "unknown mode \"semantic\". Allowed: text, meaning, both" ); } #[test] fn the_mode_defaults_to_text_and_the_limit_is_clamped() { let parsed = validate_search(&args(json!({ "query": "x" }))).expect("valid"); assert_eq!(parsed.mode, "text"); assert_eq!(parsed.limit, 20); let big = validate_search(&args(json!({ "query": "x", "limit": 500 }))).expect("valid"); assert_eq!(big.limit, 50); }