//! The protocol layer's own tests — the `describe("protocol")` half of//! `packages/server/__tests__/mcp.test.ts` asked without a socket, plus the//! shapes the TypeScript exercises through it.usesuper::*;use serde_json::json;fn scopes(values: &[&str]) -> Vec<String> { values.iter().map(|value| (*value).to_string()).collect()}#[test]fn shapes_a_tool_result_and_a_tool_failure() { assert_eq!( tool_text("{\"ok\":true}", false), json!({ "content": [{ "type": "text", "text": "{\"ok\":true}" }], "isError": false }) ); assert_eq!(tool_text("no such todo", true)["isError"], json!(true));}#[test]fn tells_raw_content_from_a_plain_object() {let raw = tool_content(vec![json!({ "type": "image", "data": "…" })]); assert!(is_raw_tool_content(&raw)); assert!(!is_raw_tool_content(&json!({ "records": [] })));// `rawContent` without an array is not the shape and must not pass. assert!(!is_raw_tool_content(&json!({ "rawContent": true }))); assert!(!is_raw_tool_content( &json!({ "rawContent": false, "content": [] }) ));}// ── the catalogue ───────────────────────────────────────────────────────────// RULE: Unlaunched Cotton accepts current MCP names only; retired aliases must be unknown.#[test]fn retired_names_are_not_catalogued_or_dispatched() {for name in ["get_briefing","list_memories","memory_propose","list_memory_projects","memory_project_update", ] { assert!(tool_by_name(name).is_none(), "{name}"); assert!(crate::mcp::dispatch::handler_for(name).is_none(), "{name}"); } assert_eq!(tool_by_name("brief").map(|t| t.name), Some("brief")); assert!(crate::mcp::dispatch::handler_for("brief").is_some());}/// RULE: A read/write token sees every non-owner, non-developer tool exactly once, because a missing entry makes a valid assistant capability unreachable and either excluded kind would expose a tool the server did not grant.#[test]fn lists_every_tool_for_a_read_and_write_token() {let listed = tool_catalogue(&scopes(&["read", "write"]), false);let expected: std::collections::HashSet<&str> = TOOLS .iter() .filter(|tool| tool.scope != McpScope::Owner && !is_dev_tool(tool.name)) .map(|tool| tool.name) .collect();letmut names = std::collections::HashSet::new();for entry in &listed {let name = entry["name"].as_str().expect("a name"); assert!(names.insert(name), "{name} is advertised more than once"); } assert_eq!(names, expected); assert!(names.contains(&"log_session")); assert!(names.contains(&"search_records")); assert!(names.contains(&"file_document")); assert!(names.contains(&"remember"));// A retired name is never advertised. assert!(!names.contains(&"memory_propose")); assert!(!names.contains(&"list_by_tag"));}#[test]fn hides_write_tools_from_a_read_only_token() {let names: Vec<String> = tool_catalogue(&scopes(&["read"]), false) .iter() .map(|entry| entry["name"].as_str().expect("a name").to_string()) .collect(); assert!(names.iter().any(|name| name == "search_records")); assert!(!names.iter().any(|name| name == "create_todo")); assert!(!names.is_empty());}#[test]fn every_input_schema_parses_and_is_an_object_schema() {for tool in TOOLS {let schema = tool.input_schema_json(); assert_eq!(schema["type"], "object", "{}", tool.name); assert!(schema.get("properties").is_some(), "{}", tool.name); }}// RULE: Database lifecycle may wait for file handles and backup children; running it under a single-file write lock would deadlock archive and restore.#[test]fn names_itself_the_way_initialize_answers() { assert_eq!(PROTOCOL_VERSION, "2025-06-18"); assert_eq!( server_info(), json!({ "name": "cotton", "title": "Cotton", "version": "1.0.0" }) );}// ── the provider keys (Cotton note "OAuth and the provider keys — what the server holds") ───────────────────────────/// **No tool reaches a provider key, and the Keys page is not one.**////// The keys that pay for a run live in the vault, and the three methods that/// set and check them (`keys.list`, `keys.set`, `keys.test`) are the Mac's ffi/// methods — deliberately not MCP verbs. A model that could set the key paying/// for its own turn, or read one back, is exactly what the vault's/// no-`get_secret` rule exists to refuse, and a `keys.*` tool would be that/// rule undone under a new name.#[test]fn the_keys_page_is_not_a_tool_and_neither_is_any_reader_of_a_key() {for tool in TOOLS { assert!( !tool.name.starts_with("keys"),"{} would put the Keys page on the model's surface", tool.name ); assert_ne!(tool.name, "get_secret", "there is no get_secret"); } assert!(tool_by_name("keys.list").is_none()); assert!(tool_by_name("keys.set").is_none()); assert!(tool_by_name("keys.test").is_none());}/// Every declared provider key is written and named THROUGH the secret tools —/// which is what makes the assistant run's deny list cover all four at once/// rather than three of them plus whichever one somebody added later./// RULE: Assistants create, list, update and bin passwords but never read one: there is no tool that reveals a password.#[test]fn a_provider_key_is_reached_only_through_the_four_secret_tools() {let reaching: Vec<&str> = TOOLS .iter() .filter(|tool| tool.name.ends_with("_secret") || tool.name == "list_secrets") .map(|tool| tool.name) .collect();// FIVE NOW, and the fifth is the exception the owner wrote down: version 34// added `dev_reveal_secret`, which is off in the public build, absent from// every `tools/list` while `COTTON_DEV_TOOLS` is off, and never offered to a// Cotton assistant run at all (`assistant::deny`'s `DEV_TOOLS_IN_A_RUN`). The// rule it does not touch: there is still no `get_secret`, no reveal route,// and nothing an ordinary connection can call that answers a value. assert_eq!( reaching, vec!["create_secret","list_secrets","delete_secret","update_secret","dev_reveal_secret", ],"a sixth way to a secret needs a line on the assistant run's deny list" );// And the one that is new IS a developer tool, which is what keeps the// sentence above true: a reader of a value exists only where a server said// so out loud. assert!( is_dev_tool("dev_reveal_secret"),"the fifth reader of a secret must be a developer tool" );for key in cotton_core::vault::PROVIDER_KEYS { assert!( !key.name().is_empty() && !key.service().is_empty(),"every declared key is addressable" ); }}// RULE: Managed notes accept only current keys; retired notebooks cannot be silently ignored.#[test]fn managed_notes_reject_retired_collection_key() {let args = json!({"externalId":"fixture", "managedBy":"test", "body":"text", "collection":"old"}); assert!(crate::mcp::handlers::records_1::validate::validate_upsert_managed_note(args.as_object().unwrap()).is_err());}// ── the machines scope ──────────────────────────────────────────────────────/// RULE: `machines` is a scope word of its own, and holding `read` and `write` does not reach a `machines` tool — starting work on the owner's Macs is a grant only a personal token may carry, so the ordinary pair every token is minted with today must never stand in for it./// A `machines` tool also needs `write`: a read-only connection that somehow carried the word starts nothing.#[test]fn machines_is_a_scope_word_no_token_holds_by_default() { assert_eq!(McpScope::Machines.as_str(), "machines");letmut tool = *tool_by_name("create_session").expect("create_session is catalogued"); tool.scope = McpScope::Machines; assert!(!listed_to(&scopes(&["read", "write"]), &tool, false)); assert!(!listed_to(&scopes(&["machines"]), &tool, false)); assert!(!listed_to(&scopes(&["read", "machines"]), &tool, false)); assert!(listed_to( &scopes(&["read", "write", "machines"]), &tool,false ));}// RULE: an unavailable Personal is a typed MCP answer, so callers never mistake it for a request to create one.#[test]fn genesis_none_is_a_typed_tool_refusal() {let result = tool_text(cotton_core::genesis::NO_PERSONAL_MESSAGE, true); assert_eq!(result["isError"], true); assert_eq!(result["structuredContent"]["error_code"], "no_personal_database_in_flavour");}