169 lines · 8.2 KB · RustRaw
//! A rollup may read another tool's table (owner, 11 Oct 2026: "a roll up//! across tools should be possible"): Customers counts the orders the Orders//! tool keeps, each tool showing only its own table. The package reader//! leaves a table it does not carry to the install, and the install checks//! the rollup against the database's own tables.//!//! Every name is invented.mod support;use base64::{engine::general_purpose::STANDARD, Engine};use cotton_core::definitions::{package::ToolPackage, TableDefinition, ToolDefinition};use serde_json::{json, Value};use support::mcp::{call_tool, call_tool_raw, mint_token};use support::Harness;fn table(id: &str, title: &str, columns: Value) -> TableDefinition { serde_json::from_value(json!({ "id": id, "name": id, "plural": format!("{id}s"), "picture": null, "builtin": false, "engine": null, "columns": columns, "title": title, "date": null, "project": null, "order": null, "slip": { "title": title, "subtitle": null, "trailing": null, "photo": null, "toggle": null }, "tile": { "face": "slip-list" }, "page": [{ "heading": null, "columns": [title] }], "instructions": "An invented table of the cross-tool rollup tests." })) .expect("a table")}fn package(tool: &str, table: TableDefinition) -> ToolPackage { let tool: ToolDefinition = serde_json::from_value(json!({ "id": tool, "name": tool, "picture": null, "tables": [table.id], "instructions": "An invented tool of the cross-tool rollup tests.", "automations": [], "author": { "device": "synthetic-rollup-fixture", "name": "Invented cook" }, "signature": null })) .expect("a tool"); ToolPackage { board: None, tool, tables: vec![table], pictures: Default::default(), samples: Default::default() }}/// The Orders tool: an order names its customer, in a table the Customers/// tool does not carry.fn orders() -> ToolPackage { package( "invented_orders", table( "invented_order", "dish", json!([ { "key": "dish", "name": "Dish", "type": "text" }, { "key": "customer", "name": "Customer", "type": "link", "link_to": "invented_customer" }, { "key": "total", "name": "Total", "type": "amount" } ]), ), )}/// The Customers tool: its customers count and add up the Orders tool's orders.fn customers() -> ToolPackage { package( "invented_customers", table( "invented_customer", "name", json!([ { "key": "name", "name": "Name", "type": "text" }, { "key": "orders", "name": "Orders", "type": "number", "computed": { "rollup": { "table": "invented_order", "link": "customer", "op": "count" } } }, { "key": "spent", "name": "Spent", "type": "amount", "computed": { "rollup": { "table": "invented_order", "link": "customer", "op": "sum", "column": "total" } } } ]), ), )}fn bytes(package: &ToolPackage) -> Value { json!({ "bytes": STANDARD.encode(cotton_core::definitions::package::encode_unsigned_package(package).expect("encodes")) })}fn customer(h: &Harness, token: &str, id: &str) -> Value { call_tool(h, token, "get_record_by_table", json!({ "table": "invented_customer", "id": id }))}/// The customer's two computed values, wherever the answer carries them.fn rolled(answer: &Value) -> (Value, Value) { let record = answer.get("record").unwrap_or(answer); let data = record.get("data").unwrap_or(record); (data["orders"].clone(), data["spent"].clone())}/// RULE: A rollup may count and sum another installed tool's records without carrying its table; the package reader defers absent tables to install so each tool shows only its own tables.#[test]fn a_rollup_over_an_installed_tools_table_imports_and_counts() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Cook"); cotton_core::definitions::package::decode_package( &cotton_core::definitions::package::encode_unsigned_package(&customers()).unwrap(), ) .expect("the reader takes a rollup over a table the package does not carry"); call_tool(&h, &token, "import_tool", bytes(&orders())); call_tool(&h, &token, "import_tool", bytes(&customers())); let made = call_tool(&h, &token, "create_record", json!({ "table": "invented_customer", "data": { "name": "Invented Meera" } })); let id = made["record"]["id"].as_str().unwrap_or_else(|| panic!("no customer: {made}")).to_string(); for (dish, total) in [("Invented thali", 25000), ("Invented dosa", 12000)] { call_tool( &h, &token, "create_record", json!({ "table": "invented_order", "data": { "dish": dish, "customer": id, "total": total } }), ); } let answer = customer(&h, &token, &id); assert_eq!(rolled(&answer), (json!(2), json!(37000)), "{answer}"); let listed = call_tool(&h, &token, "list_tools", json!({})); let tables = |tool: &str| { listed["tools"] .as_array() .unwrap() .iter() .find(|row| row["id"] == tool || row["definition"]["id"] == tool) .map(|row| row.get("definition").unwrap_or(row)["tables"].clone()) }; assert_eq!(tables("invented_customers"), Some(json!(["invented_customer"])), "Customers shows only its own table: {listed}");}/// RULE: a package whose rollup reads a table this database does not have is refused at install, naming the table, and writes nothing — the reader defers the check, the install never does.#[test]fn a_rollup_over_a_table_not_installed_is_refused_naming_it() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Cook"); let refused = call_tool_raw(&h, &token, "import_tool", bytes(&customers())); assert!(refused.is_error, "{}", refused.text); let name = cotton_core::definitions::key_as_words("invented_order"); assert!(refused.text.contains(&name), "the refusal names the table: {}", refused.text); let listed = call_tool(&h, &token, "list_tools", json!({})); let text = listed.to_string(); assert!(!text.contains("invented_customer"), "nothing of the refused package was written: {listed}");}/// RULE: Removing an empty source tool never refuses because another tool rolls it up; its count and sum stay readable as zero, and each such column carries its source table.#[test]fn removing_the_source_tool_keeps_rollups_readable_and_identifies_unavailable_columns() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Cook"); call_tool(&h, &token, "import_tool", bytes(&orders())); call_tool(&h, &token, "import_tool", bytes(&customers())); let made = call_tool(&h, &token, "create_record", json!({ "table": "invented_customer", "data": { "name": "Invented cook" } })); let id = made["record"]["id"].as_str().unwrap(); let before = customer(&h, &token, id); assert_eq!(rolled(&before), (json!(0), json!(0)), "{before}"); assert!(before.get("computed_unavailable").is_none(), "{before}"); call_tool(&h, &token, "remove_tool", json!({ "tool_id": "invented_orders" })); let after = customer(&h, &token, id); assert_eq!(rolled(&after), (json!(0), json!(0)), "{after}"); // Each column names its source table. The words came from the `everything` // definition, which no kit tool carries now, so they are not asserted here. for column in ["orders", "spent"] { assert_eq!(after["computed_unavailable"][column]["table"], "invented_order", "{after}"); } call_tool(&h, &token, "import_tool", bytes(&orders())); let restored = customer(&h, &token, id); assert!(restored.get("computed_unavailable").is_none(), "{restored}"); // A table another live tool still lists is available after one owner leaves. let mut shared = orders(); shared.tool.id = "invented_shared_orders".into(); call_tool(&h, &token, "import_tool", bytes(&shared)); call_tool(&h, &token, "remove_tool", json!({ "tool_id": "invented_orders" })); let shared = customer(&h, &token, id); assert!(shared.get("computed_unavailable").is_none(), "{shared}");}