124 lines · 6.4 KB · RustRaw
//! The sparkline face, drawn for a synthetic table: a definition names it,//! `summarize_records` answers its points.mod support;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};fn define(h: &Harness, definition: &Value) { let stamp = h.clock.now().to_rfc3339(); h.run( "INSERT INTO cotton_table_definitions (id, workspace_id, definition, builtin, created_at, updated_at) \ VALUES (?, ?, ?, 0, ?, ?)", &[ SqlValue::Text(definition["id"].as_str().unwrap().to_string()), SqlValue::Text(h.workspace_id.clone()), SqlValue::Text(definition.to_string()), SqlValue::Text(stamp.clone()), SqlValue::Text(stamp), ], );}fn table(id: &str, columns: Value, tile: Value) -> Value { json!({ "id": id, "name": id, "plural": id, "picture": null, "builtin": false, "engine": null, "columns": columns, "title": "name", "slip": { "title": "name" }, "tile": tile, "page": [ { "heading": null, "columns": ["name"] } ], "instructions": "A synthetic table." })}/// RULE: a definition that names the sparkline face draws one number of a/// linked table over its last days — the readings in the window, oldest/// first, each placed across it, with the last value the face writes — and a/// line naming a column that is not a number is refused where it is read.#[test]fn a_definition_naming_the_sparkline_face_draws_it_for_a_synthetic_table() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); define(&h, &table( "gauge_reading", json!([ { "key": "name", "type": "text" }, { "key": "week_pct", "type": "number" }, { "key": "sampled_at", "type": "date", "format": "ago" } ]), json!({ "face": "slip-list" }), )); define(&h, &table( "gauge", json!([ { "key": "name", "type": "text" } ]), json!({ "face": "sparkline", "sparkline": { "table": "gauge_reading", "value": "week_pct", "at": "sampled_at" } }), )); let now = h.clock.now(); for (name, days_ago, pct) in [("old", 9, 5), ("first", 6, 20), ("second", 3, 35), ("latest", 0, 41)] { let at = (now - chrono::Duration::days(days_ago)).to_rfc3339(); call_tool(&h, &secret, "create_record", json!({ "table": "gauge_reading", "data": { "name": name, "week_pct": pct, "sampled_at": at } })); } let answer = call_tool(&h, &secret, "summarize_records", json!({ "table": "gauge" })); let line = &answer["sparkline"]; assert_eq!(line["days"], json!(7), "a week when the definition names no window"); let values: Vec<f64> = line["points"].as_array().unwrap().iter().map(|p| p["value"].as_f64().unwrap()).collect(); assert_eq!(values, [20.0, 35.0, 41.0], "the reading nine days back is outside the window"); let ts: Vec<f64> = line["points"].as_array().unwrap().iter().map(|p| p["t"].as_f64().unwrap()).collect(); assert!(ts.windows(2).all(|pair| pair[0] < pair[1]) && ts[2] > 0.99, "{ts:?}"); assert_eq!((line["low"].clone(), line["high"].clone(), line["last"].clone()), (json!(20.0), json!(41.0), json!(41.0))); define(&h, &table( "bad_gauge", json!([ { "key": "name", "type": "text" } ]), json!({ "face": "sparkline", "sparkline": { "table": "gauge_reading", "value": "name", "at": "sampled_at" } }), )); let refused = call_tool_raw(&h, &secret, "summarize_records", json!({ "table": "bad_gauge" })); assert!(refused.is_error && refused.text.contains("number"), "{}", refused.text);}/// RULE: a page section that names a sparkline draws the line of ITS record —/// the linked table's rows whose link column holds the page's record, none of/// another record's — and a tile's line may link no record.#[test]fn a_page_section_sparkline_draws_its_records_readings_only() { let mut h = Harness::new(); let secret = mint_token(&mut h, &["read", "write"], "Claude"); define(&h, &table( "account_reading", json!([ { "key": "name", "type": "text" }, { "key": "account", "type": "link", "link_to": "account" }, { "key": "week_pct", "type": "number" }, { "key": "sampled_at", "type": "date", "format": "ago" } ]), json!({ "face": "slip-list" }), )); let mut account = table("account", json!([ { "key": "name", "type": "text" } ]), json!({ "face": "slip-list" })); account["page"] = json!([ { "heading": null, "columns": ["name"] }, { "heading": "This week", "sparkline": { "table": "account_reading", "value": "week_pct", "at": "sampled_at", "link": "account" } } ]); define(&h, &account); let mine = call_tool(&h, &secret, "create_record", json!({ "table": "account", "data": { "name": "mine" } }))["record"]["id"].as_str().unwrap().to_string(); let other = call_tool(&h, &secret, "create_record", json!({ "table": "account", "data": { "name": "other" } }))["record"]["id"].as_str().unwrap().to_string(); let now = h.clock.now(); for (account, days_ago, pct) in [(&mine, 2, 10), (&other, 1, 99), (&mine, 0, 30)] { let at = (now - chrono::Duration::days(days_ago)).to_rfc3339(); call_tool(&h, &secret, "create_record", json!({ "table": "account_reading", "data": { "name": "r", "account": account, "week_pct": pct, "sampled_at": at } })); } let answer = call_tool(&h, &secret, "summarize_records", json!({ "table": "account", "record": mine })); let line = &answer["sparklines"][0]; assert_eq!((line["value"].clone(), line["at"].clone()), (json!("week_pct"), json!("sampled_at"))); let values: Vec<f64> = line["points"].as_array().unwrap().iter().map(|p| p["value"].as_f64().unwrap()).collect(); assert_eq!(values, [10.0, 30.0], "another account's reading is not on this page"); assert!(answer.get("sparkline").is_none(), "the tile draws no line"); let mut linked_tile = account.clone(); linked_tile["id"] = json!("linked_tile"); linked_tile["tile"] = json!({ "face": "sparkline", "sparkline": { "table": "account_reading", "value": "week_pct", "at": "sampled_at", "link": "account" } }); let parsed: cotton_core::definitions::TableDefinition = serde_json::from_value(linked_tile).unwrap(); assert!(parsed.validate().is_err(), "a tile's line links no record");}