Cotton code
68 lines · 2.8 KB · RustRaw
//! The invites Home counts and the list its row opens are one set: someone//! else's events still asking for a reply (`asked_of_you::INVITE_SQL`). Home//! once said "2 invites to answer" over two of the owner's own Google events//! (11 Oct 2026); its row then opened the plain calendar. Driven through the//! dispatcher's `list_records`, as the phone's narrowed calendar reads it.//!//! Every value is invented ([nothing-real-ever-goes-in-cotton-s-public-tree]).mod support;use rusqlite::types::Value as SqlValue;use serde_json::{json, Value};use support::harness::{synthetic_uuid, Harness};use support::mcp::call_tool_as_owner;const PHONE: &str = "device:phone";fn an_event(h: &Harness, title: &str, my_response: Option<&str>, organizer_is_self: bool) -> String {    let added = call_tool_as_owner(        h,        PHONE,        "create_record",        json!({ "table": "event", "data": { "title": title, "starts_at": "2099-10-15T09:30:00.000Z" } }),    );    assert!(!added.is_error, "create_record refused: {}", added.text);    let id = added.json()["record"]["id"].as_str().expect("the event's id").to_string();    h.run(        "UPDATE cotton_events SET my_response = ?, organizer_is_self = ? WHERE id = ?",        &[            my_response.map_or(SqlValue::Null, |reply| SqlValue::Text(reply.to_string())),            SqlValue::Integer(i64::from(organizer_is_self)),            SqlValue::Text(id.clone()),        ],    );    id}// RULE: list_records over events with where {invited, my_response: needs_action} answers exactly Home's invites — someone else's unanswered events — and never the owner's own events or an answered invite.#[test]fn the_invites_list_is_the_set_home_counts() {    let h = Harness::new();    h.run(        "INSERT INTO cotton_collections (id, workspace_id, kind, name, is_default, created_at) \         VALUES (?, ?, 'calendar', 'Personal', 1, '2026-10-01T09:00:00.000Z')",        &[SqlValue::Text(synthetic_uuid("calendar")), SqlValue::Text(h.workspace_id.clone())],    );    let asking = an_event(&h, "Asking", Some("needs_action"), false);    an_event(&h, "Answered", Some("accepted"), false);    an_event(&h, "Own", None, true);    an_event(&h, "Own and asking", Some("needs_action"), true);    an_event(&h, "Subscribed", None, false);    let answer = call_tool_as_owner(        &h,        PHONE,        "list_records",        json!({ "table": "event", "where": { "invited": "invited", "my_response": "needs_action" } }),    );    assert!(!answer.is_error, "list_records refused: {}", answer.text);    let page: Value = answer.json();    let ids: Vec<&str> = page["records"]        .as_array()        .expect("records")        .iter()        .filter_map(|record| record["id"].as_str())        .collect();    assert_eq!(ids, vec![asking.as_str()], "{page}");}