//! The OWNER scope, both directions, and the two tools that carry it. //! //! `McpScope::Owner` is the third scope and it is not grantable: a token can be //! minted with `read` and `write` and with nothing else, so an owner tool is //! absent from every `tools/list` and refused at every dispatch that is not the //! owner's own app. Both halves are asserted here, per tool, because a rule //! that is only tested from the allowed side is a rule that has never actually //! been tried. //! //! Nothing here reaches into a handler. A tool is called the way each caller //! really calls one — `dispatch::call_tool` over a `Ctx::new` built from a //! token, or over the `Ctx::phone` a device build uses — so what is under test //! is the answer the assistant and the app actually receive. mod support; use cotton_store::protocol::derived_id::{link_id_for, LinkRow}; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::{synthetic_uuid, Harness}; use support::mcp::{call_tool_as_owner, call_tool_raw, mint_token}; use cotton_tools::mcp::protocol::{tool_by_name, tool_catalogue, McpScope}; /// The device id the owner's app writes under. Synthetic, like every fixture in /// this tree ([nothing-real-ever-goes-in-cotton-s-public-tree]). const DEVICE: &str = "device:00000000000000000000000000"; fn writer_harness() -> (Harness, String) { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); (h, secret) } fn run(h: &Harness, sql: &str, params: Vec) { h.with(|db| db.run(sql, ¶ms)) .expect("the statement runs"); } fn count_of(h: &Harness, sql: &str, params: Vec) -> i64 { h.with(|db| db.get(sql, ¶ms)) .expect("the read runs") .and_then(|row| row.int("n")) .unwrap_or_default() } fn text_of(h: &Harness, sql: &str, params: Vec, column: &str) -> Option { h.with(|db| db.get(sql, ¶ms)) .expect("the read runs") .and_then(|row| row.text(column).map(str::to_string)) } /// Every tool this lane put on the owner scope. The list is asked of the /// CATALOGUE rather than written twice, so a third one added later is covered /// by the two rules below without anybody remembering to add it here. fn owner_tools() -> Vec<&'static str> { cotton_tools::mcp::protocol::TOOLS .iter() .filter(|tool| tool.scope == McpScope::Owner) .map(|tool| tool.name) .collect() } // ── the scope itself ──────────────────────────────────────────────────────── #[test] fn an_owner_tool_is_in_no_token_s_tools_list() { // Not "filtered out for a read-only token" — absent for EVERY token there // can be, because `owner` is not a word a token carries. The widest grant // that exists is read + write, and it is what this asks with. let listed: Vec = tool_catalogue(&["read".to_string(), "write".to_string()], false) .iter() .filter_map(|tool| tool["name"].as_str().map(str::to_string)) .collect(); assert!(!owner_tools().is_empty(), "this lane declared owner tools"); for name in owner_tools() { assert!( !listed.contains(&name.to_string()), "{name} is an owner tool and an assistant was shown it" ); } } #[test] fn an_assistant_calling_an_owner_tool_is_refused_with_the_scope_named() { let (h, secret) = writer_harness(); for name in owner_tools() { let answer = call_tool_raw(&h, &secret, name, json!({ "id": synthetic_uuid("x") })); assert!(answer.is_error, "{name} answered an assistant"); // The sentence has to NAME the scope and say who may. "Reconnect with // owner enabled" would send the person hunting for a checkbox that does // not exist, which is why this refusal is not the write one. assert!( answer.text.contains("owner-scope tool") && answer.text.contains("an assistant may not"), "the refusal for {name} does not name the scope: {}", answer.text ); assert!( !answer.text.contains("reconnect"), "the refusal for {name} offers a fix nobody can follow: {}", answer.text ); } } #[test] fn every_owner_tool_is_implemented_and_writes() { // A catalogued owner tool with no handler would refuse the OWNER too, and // the refusal would read as a scope problem rather than a missing port. for name in owner_tools() { assert!( cotton_tools::mcp::dispatch::handler_for(name).is_some(), "{name} is on the owner scope and no group registers it" ); let def = tool_by_name(name).expect("the catalogue has it"); // The owner scope is for tools that CHANGE something the person was // asked about. A read that needed it would be a read nobody could make. assert!( !def.annotations.read_only_hint, "{name} is owner-scope and marked read-only" ); } } // ── create_link into another database ─────────────────────────────────────── /// Another database the owner holds — only ever named, never opened here. const OTHER_DATABASE: &str = "33333333-3333-4333-8333-333333333333"; fn link_to(h: &Harness, note: &str, document: &str, to_database: Option<&str>) -> Value { let mut args = json!({ "fromKind": "note", "fromId": note, "toKind": "document", "toId": document, }); if let Some(database) = to_database { args["toDatabase"] = Value::from(database); } let answer = call_tool_as_owner(h, DEVICE, "create_link", args); assert!(!answer.is_error, "{}", answer.text); answer.json() } /// RULE: The database a link points into is one of its ends: one id linked in this database and in another is two rows, each with the id link_id_for derives, and naming this database explicitly is the same edge as naming none. #[test] fn create_link_to_one_id_in_mine_and_in_another_database_makes_two_rows() { let (h, _secret) = writer_harness(); let note = synthetic_uuid("note-two-databases"); let document = synthetic_uuid("document-two-databases"); let here = link_to(&h, ¬e, &document, None); let there = link_to(&h, ¬e, &document, Some(OTHER_DATABASE)); assert_ne!(here["id"], there["id"]); assert_eq!(there["toDatabase"], OTHER_DATABASE); let ends = |to_database| LinkRow { from_kind: "note", from_id: ¬e, to_kind: "document", to_id: &document, to_database, }; assert_eq!(here["id"], link_id_for(&ends(None), h.file.id())); assert_eq!(there["id"], link_id_for(&ends(Some(OTHER_DATABASE)), h.file.id())); // Saying either again hands the same row back; naming this database is // the link with none. assert_eq!(link_to(&h, ¬e, &document, Some(OTHER_DATABASE))["id"], there["id"]); let own = h.workspace_id.clone(); assert_eq!(link_to(&h, ¬e, &document, Some(&own))["id"], here["id"]); assert_eq!(count_of(&h, "SELECT COUNT(*) AS n FROM cotton_links", vec![]), 2); assert_eq!( text_of( &h, "SELECT to_database FROM cotton_links WHERE id = ?", vec![SqlValue::Text(there["id"].as_str().expect("an id").to_string())], "to_database", ) .as_deref(), Some(OTHER_DATABASE) ); } /// RULE: Mine's two links to one id — in Mine and in another database — are independent rows: deleting one by its id leaves the other. #[test] fn mine_links_to_one_id_in_mine_and_in_another_database_are_two_rows() { let (h, _secret) = writer_harness(); let note = synthetic_uuid("note-delete-one"); let document = synthetic_uuid("document-delete-one"); let here = link_to(&h, ¬e, &document, None); let there = link_to(&h, ¬e, &document, Some(OTHER_DATABASE)); run( &h, "DELETE FROM cotton_links WHERE id = ?", vec![SqlValue::Text(here["id"].as_str().expect("an id").to_string())], ); let left = |id: &Value| { count_of( &h, "SELECT COUNT(*) AS n FROM cotton_links WHERE id = ?", vec![SqlValue::Text(id.as_str().expect("an id").to_string())], ) }; assert_eq!(left(&here["id"]), 0); assert_eq!(left(&there["id"]), 1, "the link into the other database stands"); } /// RULE: Only the personal database links into another database: create_link with toDatabase in a shared database is refused and writes nothing, while a link inside it lands. #[test] fn create_link_naming_another_database_is_refused_in_a_shared_database() { let (h, _secret) = writer_harness(); run( &h, "UPDATE cotton_workspaces SET kind = 'shared' WHERE id = ?", vec![SqlValue::Text(h.workspace_id.clone())], ); let note = synthetic_uuid("note-shared"); let document = synthetic_uuid("document-shared"); let refused = call_tool_as_owner( &h, DEVICE, "create_link", json!({ "fromKind": "note", "fromId": note, "toKind": "document", "toId": document, "toDatabase": OTHER_DATABASE, }), ); assert!(refused.is_error, "{}", refused.text); assert_eq!(count_of(&h, "SELECT COUNT(*) AS n FROM cotton_links", vec![]), 0); link_to(&h, ¬e, &document, None); assert_eq!(count_of(&h, "SELECT COUNT(*) AS n FROM cotton_links", vec![]), 1); } // ── restore_version ───────────────────────────────────────────────────────── #[test] fn restore_version_refuses_a_word_that_keeps_no_versions_and_a_version_of_another_record() { let h = Harness::new(); let refused = call_tool_as_owner( &h, DEVICE, "restore_version", json!({ "kind": "mood", "id": synthetic_uuid("restore-mood"), "version": synthetic_uuid("restore-v") }), ); assert!(refused.is_error && refused.text.contains("not a kind that keeps versions"), "{}", refused.text); let missing = call_tool_as_owner( &h, DEVICE, "restore_version", json!({ "kind": "todo", "id": synthetic_uuid("restore-todo"), "version": synthetic_uuid("restore-v") }), ); assert!(missing.is_error && missing.text.contains("no such version"), "{}", missing.text); }