Cotton code
62 lines · 3.4 KB · RustRaw
//! `list_groups` with no table: every group (view) of the database in one//! read. Driven end to end through the dispatcher. Every name is invented.mod support;use serde_json::{json, Value};use support::harness::Harness;use support::mcp::{call_tool, call_tool_raw, mint_token};fn harness() -> (Harness, String) {    let mut h = Harness::new();    let secret = mint_token(&mut h, &["read", "write"], "Claude");    (h, secret)}fn made(h: &Harness, secret: &str, table: &str, data: Value) -> String {    let made = call_tool(h, secret, "create_record", json!({ "table": table, "data": data }));    made["record"]["id"].as_str().unwrap_or_else(|| panic!("no {table}: {made}")).to_string()}/// RULE: list_groups with no table answers every group of the database once, whatever table it gathers — hand-picked or filter — each naming its table; a deleted group is not among them, and `record` is refused without its table.////// Any table that keeps records takes a group: a list asked table by table/// would have to know which tables those are, and would miss the one it did/// not ask.#[test]fn list_groups_without_a_table_answers_every_group_of_the_database() {    let (h, secret) = harness();    let gutters = made(&h, &secret, "todo", json!({ "title": "Clear the gutters", "status": "open" }));    made(&h, &secret, "note", json!({ "title": "Buy string" }));    let house = call_tool(&h, &secret, "create_group", json!({ "table": "todo", "name": "House", "records": [gutters] }));    let going =        call_tool(&h, &secret, "create_group", json!({ "table": "todo", "name": "Going", "where": { "status": "open" } }));    let errands = call_tool(&h, &secret, "create_group", json!({ "table": "note", "name": "Errands" }));    let gone = call_tool(&h, &secret, "create_group", json!({ "table": "note", "name": "Dropped" }));    call_tool(&h, &secret, "delete_group", json!({ "id": gone["group"]["id"] }));    let every = call_tool(&h, &secret, "list_groups", json!({}));    let groups = every["groups"].as_array().unwrap_or_else(|| panic!("no groups: {every}"));    // The harness's clock stands still, so the order made is not asserted here.    let mut said: Vec<(&str, &str)> =        groups.iter().map(|group| (group["name"].as_str().unwrap(), group["table"].as_str().unwrap())).collect();    said.sort_unstable();    assert_eq!(said, vec![("Errands", "note"), ("Going", "todo"), ("House", "todo")], "{every}");    let named = |name: &str| groups.iter().find(|group| group["name"] == name).unwrap_or_else(|| panic!("no {name}"));    assert_eq!(named("House")["id"], house["group"]["id"]);    assert_eq!(named("House")["count"], 1);    assert!(named("House").get("where").map_or(true, Value::is_null), "a hand-picked group keeps no filter");    assert_eq!(named("Going")["id"], going["group"]["id"]);    assert_eq!(named("Going")["where"], json!({ "status": "open" }));    assert_eq!(named("Going")["count"], 1, "a filter group is counted live");    assert_eq!(named("Errands")["id"], errands["group"]["id"]);    assert_eq!(named("Errands")["count"], 0);    // One table's read is unchanged.    let todos = call_tool(&h, &secret, "list_groups", json!({ "table": "todo" }));    assert_eq!(todos["groups"].as_array().unwrap().len(), 2);    let answer = call_tool_raw(&h, &secret, "list_groups", json!({ "record": gutters }));    assert!(answer.is_error, "record without its table was expected to refuse: {}", answer.text);}