//! The chart face, drawn from a definition alone: a kit's table and a //! built-in name it, `summarize_records` answers the series every face //! draws. Every row is synthetic and every value invented. mod support; #[path = "support/work_fixture.rs"] mod work_fixture; use std::sync::Arc; use cotton_tools::clock::Clock; use rusqlite::types::Value as SqlValue; use serde_json::{json, Value}; use support::harness::Harness; use support::mcp::{call_tool, call_tool_raw, mint_token}; /// RULE: Work's hourly view uses the same imported day/hour rows through MCP, so a fixture upgrade can add a 48-hour chart without rewriting the recorded landings. #[test] fn work_view_charts_existing_split_hour_rows() { use base64::{engine::general_purpose::STANDARD, Engine}; let bytes = cotton_core::definitions::package::encode_unsigned_package(&work_fixture::package()).unwrap(); let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Invented coordinator"); call_tool(&h, &token, "import_tool", json!({"bytes": STANDARD.encode(bytes)})); for (day, hour, value) in [("2026-10-13", 23, 2), ("2026-10-14", 0, 3), ("2026-10-14", 1, 100)] { call_tool(&h, &token, "create_record", json!({"table":"landing", "data":{"name":format!("{day} {hour}"), "hour_at":day, "hour":hour, "landings":value}})); } let answer = call_tool(&h, &token, "summarize_records", json!({"table":"landing", "at":"2026-10-13T18:42:00Z"})); let chart = &answer["view_chart"]; assert_eq!(chart["by"], "hour"); assert_eq!(chart["buckets"].as_array().unwrap().len(), 48); assert_eq!(chart["total"], 5); assert_eq!(chart["buckets"][47]["start"], "2026-10-14T00:00:00+05:30"); } /// A shared database made from the shipped Home kitchen kit — an invented kitchen. fn kitchen() -> Harness { let h = Harness::new(); h.run("UPDATE cotton_workspaces SET kind = 'shared' WHERE id = ?", &[SqlValue::Text(h.workspace_id.clone())]); let kit = cotton_tools::toolkits::kit(None, "home-kitchen").unwrap().unwrap(); let now = h.clock.now(); h.with(|db| cotton_tools::toolkits::install(db, &kit, now, Arc::clone(&h.random), cotton_store::mint::Held::Served)).unwrap(); h } /// One payment into the kitchen's Money, from one invented customer. fn pay(h: &Harness, secret: &str, name: &str, on: &str, more: Value) { let customer = match h.one("SELECT id FROM cotton_kitchen_customer LIMIT 1", &[]) { Some(row) => row["id"].as_str().unwrap().to_string(), None => call_tool(h, secret, "create_record", json!({ "table": "kitchen_customer", "data": { "name": "Invented customer" } }))["record"]["id"].as_str().unwrap().to_string(), }; let mut data = json!({ "name": name, "taken_on": on, "customer": customer, "amount": 100, "currency": "INR", "method": "cash" }); for (key, value) in more.as_object().unwrap() { data[key] = value.clone(); } call_tool(h, secret, "create_record", json!({ "table": "kitchen_payment", "data": data })); } fn buckets(chart: &Value) -> Vec<(String, Value)> { chart["buckets"] .as_array() .unwrap_or_else(|| panic!("no buckets in {chart}")) .iter() .map(|bucket| (bucket["start"].as_str().unwrap().to_string(), bucket["value"].clone())) .collect() } /// RULE: A chart face declared in a kit tool's definition draws with no code /// for that tool — the definition names the face, `summarize_records` answers /// the series, the same one the tile and an assistant read. /// Kitchen payments counted by week taken, UPI ones left out. #[test] fn a_chart_face_on_a_kit_table_draws_from_its_definition_alone() { let mut h = kitchen(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); let set = call_tool_raw(&h, &secret, "update_definition", json!({ "table": "kitchen_payment", "tile": { "face": "chart", "chart": { "kind": "bar", "x": "taken_on", "y": "count", "by": "week", "periods": 3, "where": { "method": ["cash"] } } } })); assert!(!set.is_error, "{}", set.text); // 12 Oct 2026 is a Monday. for (name, on, method) in [ ("Invented one", "2026-09-29", "cash"), ("Invented two", "2026-10-04", "cash"), ("Invented three", "2026-10-12", "cash"), ("Invented four", "2026-10-15", "cash"), ("Invented five", "2026-10-14", "upi"), ("Long ago", "2026-08-01", "cash"), ] { pay(&h, &secret, name, on, json!({ "method": method })); } let answer = call_tool(&h, &secret, "summarize_records", json!({ "table": "kitchen_payment", "today": "2026-10-12" })); let chart = &answer["chart"]; assert_eq!((chart["kind"].clone(), chart["by"].clone()), (json!("bar"), json!("week"))); assert_eq!( buckets(chart), [("2026-09-28".into(), json!(2)), ("2026-10-05".into(), json!(0)), ("2026-10-12".into(), json!(2))], "Monday weeks; the UPI payment and August's are not counted" ); assert!(chart.get("currency").is_none(), "a count has no currency"); // A kit table whose face names a column that is not a date is refused // where the definition is written, never drawn. let refused = call_tool_raw(&h, &secret, "update_definition", json!({ "table": "kitchen_payment", "tile": { "face": "chart", "chart": { "kind": "bar", "x": "name", "y": "count", "by": "week" } } })); assert!(refused.is_error && refused.text.contains("date"), "{}", refused.text); } /// RULE: A chart over an amount column sums integer minor units and carries /// the currency; a payment in another currency is never added in — it is named /// in `left_out`. #[test] fn a_chart_over_an_amount_sums_minor_units_in_one_currency() { let mut h = kitchen(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); call_tool(&h, &secret, "update_definition", json!({ "table": "kitchen_payment", "tile": { "face": "chart", "chart": { "kind": "line", "x": "taken_on", "y": "amount", "by": "month", "periods": 2 } } })); for (name, on, amount, currency) in [ ("a", "2026-09-30", 120_050, "INR"), ("b", "2026-10-01", 99_99, "INR"), ("c", "2026-10-11", 1, "INR"), ("d", "2026-10-11", 4_500, "USD"), ] { pay(&h, &secret, name, on, json!({ "amount": amount, "currency": currency })); } let answer = call_tool(&h, &secret, "summarize_records", json!({ "table": "kitchen_payment", "today": "2026-10-12" })); let chart = &answer["chart"]; assert_eq!(buckets(chart), [("2026-09-01".into(), json!(120_050)), ("2026-10-01".into(), json!(10_000))]); assert_eq!(chart["currency"], json!("INR")); assert_eq!(chart["total"], json!(130_050)); assert_eq!(chart["left_out"], json!([{ "currency": "USD", "rows": 1 }])); } /// RULE: A chart a kit's table ships as a view is offered to Home as that /// table's own (`chart/