//! A tool's card figures worked out from its definition alone //! (`tool.figures[].summary` / `formula`): this month's money in and out and //! a net, over a made table of invented rows. mod support; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool, mint_token}; const NOW: &str = "2026-10-17T00:00:00.000Z"; fn ledger(figures: Value) -> Value { json!({ "id": "entry", "name": "Entry", "plural": "Entries", "picture": null, "builtin": false, "engine": null, "columns": [ { "key": "title", "type": "text", "required": true }, { "key": "on", "type": "date" }, { "key": "amount", "type": "amount", "currency": "currency" }, { "key": "currency", "type": "text" }, { "key": "direction", "type": "choice", "choices": ["in", "out"] } ], "title": "title", "date": "on", "project": null, "order": null, "slip": { "title": "title", "subtitle": null, "trailing": null, "photo": null, "toggle": null }, "tile": { "face": "slip-list" }, "page": [{ "heading": null, "columns": ["title"] }], "tool": { "name": "Ledger", "figures": figures }, "instructions": "An invented ledger." }) } fn define(h: &Harness, definition: &Value) { h.run( "INSERT INTO cotton_table_definitions (id, workspace_id, definition, builtin, created_at, updated_at) \ VALUES (?, ?, ?, 0, ?, ?)", &[ SqlValue::Text("entry".into()), SqlValue::Text(h.workspace_id.clone()), SqlValue::Text(definition.to_string()), SqlValue::Text(NOW.into()), SqlValue::Text(NOW.into()), ], ); } fn figures(h: &Harness, today: &str) -> serde_json::Map { let workspace = h.workspace_id.clone(); let today = chrono::NaiveDate::parse_from_str(today, "%Y-%m-%d").unwrap(); h.with(|db| cotton_tools::tool_figures::worked_figures(db, &workspace, today)).expect("figures") } /// RULE: A tool's worked figures read the table its definition names — this month's rows in and out by the person's own day, a net as a formula over them, a term said by no card — so Money's card is data and never a count written in code. #[test] fn a_tools_card_says_what_its_definition_works_out() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Invented assistant"); let month = |direction: &str| json!({ "table": "entry", "op": "sum", "column": "amount", "where": { "direction": direction }, "period": "this_month" }); let all = |direction: &str| json!({ "table": "entry", "op": "sum", "column": "amount", "where": { "direction": direction } }); define(&h, &ledger(json!([ { "read": "in_month", "many": "In {n}", "summary": month("in") }, { "read": "out_month", "many": "Out {n}", "summary": month("out") }, { "read": "in_ever", "many": "{n}", "summary": all("in"), "term": true }, { "read": "out_ever", "many": "{n}", "summary": all("out"), "term": true }, { "read": "net", "many": "Net {n}", "formula": { "plus": ["in_ever"], "minus": ["out_ever"] } }, { "read": "entries", "one": "One entry", "many": "{n} entries", "summary": { "table": "entry", "op": "count" } } ]))); for (title, on, amount, direction) in [ ("Invoice paid", "2026-10-03", 9_000_000, "in"), ("Rent", "2026-10-01", 3_000_000, "out"), ("Last month's fee", "2026-09-28", 1_000_000, "in"), ("Next month's rent", "2026-11-01", 3_000_000, "out"), ] { call_tool(&h, &token, "create_record", json!({ "table": "entry", "data": { "title": title, "on": on, "amount": amount, "currency": "INR", "direction": direction } })); } let said = figures(&h, "2026-10-17"); assert_eq!(said["in_month"], "In \u{20B9}90,000"); assert_eq!(said["out_month"], "Out \u{20B9}30,000"); assert_eq!(said["net"], "Net \u{20B9}40,000", "every in less every out"); assert_eq!(said["entries"], "4 entries"); assert!(!said.contains_key("in_ever") && !said.contains_key("out_ever"), "a term is said by no card"); let november = figures(&h, "2026-11-05"); assert_eq!(november["out_month"], "Out \u{20B9}30,000"); assert!(!november.contains_key("in_month"), "nothing came in: silence, never \u{20B9}0"); } /// RULE: A worked figure whose where its table cannot take is left out of the card, never Home's failure, and the other figures still say theirs. #[test] fn a_figure_its_table_cannot_answer_is_left_out() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Invented assistant"); define(&h, &ledger(json!([ { "read": "broken", "many": "{n}", "summary": { "table": "entry", "op": "sum", "column": "amount", "where": { "colour": "red" } } }, { "read": "entries", "many": "{n} entries", "summary": { "table": "entry", "op": "count" } } ]))); call_tool(&h, &token, "create_record", json!({ "table": "entry", "data": { "title": "One", "on": "2026-10-01", "amount": 100, "currency": "INR", "direction": "in" } })); call_tool(&h, &token, "create_record", json!({ "table": "entry", "data": { "title": "Two", "on": "2026-10-02", "amount": 100, "currency": "INR", "direction": "in" } })); let said = figures(&h, "2026-10-17"); assert!(!said.contains_key("broken")); assert_eq!(said["entries"], "2 entries"); } /// RULE: A lowest or highest figure over a made table reads its one row by number, never as words, and says that row's day — the earliest, on a tie — and in its own currency. #[test] fn a_lowest_and_highest_say_their_rows_day() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Invented assistant"); let extreme = |op: &str| json!({ "table": "entry", "op": op, "column": "amount" }); define(&h, &ledger(json!([ { "read": "smallest", "many": "Smallest {n} on {date}", "summary": extreme("min") }, { "read": "largest", "many": "Largest {n} on {date}", "summary": extreme("max") } ]))); for (title, on, amount) in [ ("Tea", "2026-10-05", 9_000), ("Rent", "2026-10-01", 3_000_000), ("Coffee", "2026-10-03", 9_000), ("Bike", "2026-10-09", 10_000_000), ] { call_tool(&h, &token, "create_record", json!({ "table": "entry", "data": { "title": title, "on": on, "amount": amount, "currency": "INR", "direction": "out" } })); } let said = figures(&h, "2026-10-17"); assert_eq!(said["smallest"], "Smallest \u{20B9}90 on 3 Oct", "a tie says the earlier day"); assert_eq!(said["largest"], "Largest \u{20B9}1,00,000 on 9 Oct", "100000 is larger than 30000, though not as words"); }