Cotton code
92 lines · 4.3 KB · RustRaw
//! B3 (owner ruling): Cotton never notifies on a new WhatsApp message or//! status — not through a code path, and not through an assistant that read//! the chats and then reached for `notify_owner`. Every fixture is synthetic.mod support;use cotton_tools::mcp::protocol::{tool_by_name, McpScope, TOOLS};use rusqlite::types::Value as SqlValue;use serde_json::{json, Value};use support::harness::{synthetic_uuid, Harness, HARNESS_NOW};use support::mcp::{call_tool_raw, mint_token};fn t(value: &str) -> SqlValue {    SqlValue::Text(value.to_string())}/// A WhatsApp chat with one line in it; answers the thread's id.fn whatsapp_chat(h: &Harness) -> String {    let ws = h.workspace_id.clone();    let thread = synthetic_uuid("thread-whatsapp");    h.run(        "INSERT INTO cotton_msg_threads (id, workspace_id, kind, title, external_key, last_message_at, created_at, updated_at) \         VALUES (?, ?, 'dm', 'Meera', 'whatsapp:15550001111@s.whatsapp.net', ?, ?, ?)",        &[t(&thread), t(&ws), t(HARNESS_NOW), t(HARNESS_NOW), t(HARNESS_NOW)],    );    h.run(        "INSERT INTO cotton_msg_messages (id, thread_id, workspace_id, direction, channel, origin, body, sent_at, created_at) \         VALUES (?, ?, ?, 'in', 'whatsapp', 'live', 'Running late, see you at the station', ?, ?)",        &[t(&synthetic_uuid("message-whatsapp")), t(&thread), t(&ws), t(HARNESS_NOW), t(HARNESS_NOW)],    );    thread}fn notify(h: &Harness, secret: &str) -> support::mcp::ToolAnswer {    call_tool_raw(h, secret, "notify_owner", json!({ "title": "Done", "body": "Filed the statements" }))}// RULE: a read tool never rings the owner's phone: every Read-scope tool, called over a database holding a WhatsApp chat, sends no notification.#[test]fn no_read_tool_sends_a_notification() {    let mut h = Harness::new();    let thread = whatsapp_chat(&h);    let secret = mint_token(&mut h, &["read", "write"], "Reader");    for tool in TOOLS.iter().filter(|tool| tool.scope == McpScope::Read) {        for args in [json!({}), json!({ "threadId": thread, "query": "station", "id": thread, "table": "message" })] {            let _ = call_tool_raw(&h, &secret, tool.name, args);        }    }    assert_eq!(h.services.notify.sent(), Vec::<Value>::new(), "a read tool sent a notification");}// RULE: a connection that read WhatsApp messages is refused notify_owner, and nothing reaches the phone; a connection that read none still notifies.#[test]fn an_assistant_that_read_whatsapp_messages_cannot_notify_the_owner() {    let mut h = Harness::new();    let thread = whatsapp_chat(&h);    let reader = mint_token(&mut h, &["read", "write"], "Reader");    let other = mint_token(&mut h, &["read", "write"], "Harvester");    assert!(!notify(&h, &reader).is_error, "before any chat read the push goes");    assert_eq!(h.services.notify.sent().len(), 1);    let read = call_tool_raw(&h, &reader, "read_message_thread", json!({ "threadId": thread }));    assert!(!read.is_error, "{}", read.text);    let refused = notify(&h, &reader);    assert!(refused.is_error, "a push after reading chats went: {}", refused.text);    assert_eq!(refused.text, cotton_tools::mcp::chat_reads::REFUSAL);    assert_eq!(h.services.notify.sent().len(), 1, "nothing more reached the phone");    assert!(!notify(&h, &other).is_error, "a connection that read no chats still notifies");    assert_eq!(h.services.notify.sent().len(), 2);}// RULE: every way a connection reads chat content (the message tools, a search hit of kind message, a generic read naming a chat table) holds its pushes.#[test]fn every_chat_read_holds_the_push() {    let reads: [(&str, fn(&str) -> Value); 4] = [        ("list_message_threads", |_| json!({})),        ("search_messages", |_| json!({ "query": "station" })),        ("search_records", |_| json!({ "query": "station" })),        ("list_records", |_| json!({ "table": "message" })),    ];    for (tool, args) in reads {        assert!(tool_by_name(tool).is_some(), "{tool} is a real tool");        let mut h = Harness::new();        let thread = whatsapp_chat(&h);        let secret = mint_token(&mut h, &["read", "write"], "Reader");        let _ = call_tool_raw(&h, &secret, tool, args(&thread));        assert!(notify(&h, &secret).is_error, "{tool} read chats and the push still went");        assert!(h.services.notify.sent().is_empty(), "{tool}");    }}