//! The business kits Cotton ships (yoga, kitchen) install into an empty //! database as data and every page they hold reads, with their reservation //! and rules engines doing what the definitions say. mod support; use serde_json::{json, Value}; use support::{ mcp::{call_tool, call_tool_raw, mint_token}, Harness, }; fn seed_serving(h: &Harness) { use rusqlite::types::Value as S; h.db.run("INSERT INTO cotton_serving_machine (id,workspace_id,epoch,device_credential_id,author_id,since,_hlc) VALUES ('synthetic-server',?,1,'synthetic-device',?,'2026-09-01T00:00:00Z','')", &[S::Text(h.workspace_id.clone()), S::Text(h.file_author())]).unwrap(); } fn create(h: &Harness, token: &str, table: &str, data: Value) -> String { call_tool(h, token, "create_record", json!({"table": table, "data": data}))["record"]["id"] .as_str() .unwrap() .to_string() } fn read(h: &Harness, token: &str, table: &str, id: &str) -> Value { call_tool(h, token, "get_record", json!({"table": table, "id": id, "today": "2026-10-19"})) } /// Every table the package lists answers its list, and each named row its page. fn every_page_reads(h: &Harness, token: &str, package: &str) { let kit = cotton_core::toolkits::shipped_kit_tools(); let tool = &kit.iter().find(|p| p.tool.id == package).unwrap().tool; for table in &tool.tables { let listed = call_tool(h, token, "list_records", json!({"table": table, "today": "2026-10-19"})); for record in listed["records"].as_array().unwrap() { read(h, token, table, record["id"].as_str().unwrap()); } } } // RULE: A shipped business package installs into an empty database with nothing but data — its tables, its reservation binding and its rules — and every page it lists reads without error; two bookings for one last place give one booking. #[test] fn yoga_installs_into_an_empty_database_and_its_pages_read() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Studio"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "yoga"}))["added"], true); every_page_reads(&h, &token, "yoga"); let trainer = create(&h, &token, "yoga_trainer", json!({"name": "Synthetic Trainer"})); let class = create(&h, &token, "yoga_class", json!({"name": "Synthetic hatha", "capacity": 1})); let session = create(&h, &token, "yoga_session", json!({"name": "Synthetic hatha, Monday", "class": class, "trainer": trainer, "on": "2026-10-19"})); let member = create(&h, &token, "yoga_member", json!({"name": "Synthetic Member", "currency": "XTS"})); let other = create(&h, &token, "yoga_member", json!({"name": "Synthetic Other", "currency": "XTS"})); create(&h, &token, "yoga_pack", json!({"name": "Ten classes", "member": member, "classes": 10, "price": 500000, "currency": "XTS", "expires": "2026-10-24"})); create(&h, &token, "yoga_payment", json!({"name": "Cash", "member": member, "amount": 200000, "currency": "XTS", "method": "cash", "taken_on": "2026-10-19"})); seed_serving(&h); let one = call_tool(&h, &token, "reserve_record", json!({"tool": "yoga", "slot": session, "data": {"member": member, "name": "Synthetic hatha, Monday", "on": "2026-10-19"}})); assert_eq!(one["ok"], true, "{one}"); let full = call_tool(&h, &token, "reserve_record", json!({"tool": "yoga", "slot": session, "data": {"member": other}})); assert_eq!(full, json!({"ok": false, "reason": "full", "remaining": 0})); let booking = one["record"]["id"].as_str().unwrap().to_string(); // The attendance row is the debit: the member's pack is worked out from it, never written down. let attended = create(&h, &token, "yoga_attendance", json!({"name": "Here", "booking": booking, "member": member, "attended_on": "2026-10-19"})); let attendance = read(&h, &token, "yoga_attendance", &attended); assert_eq!(attendance["data"]["member_left"], json!(9), "{attendance}"); let session = read(&h, &token, "yoga_session", &session); assert_eq!(session["data"]["booked"], json!(1), "{session}"); assert_eq!(session["data"]["present"], json!(1)); assert_eq!(session["data"]["places_left"], json!(0)); let member = read(&h, &token, "yoga_member", &member); assert_eq!(member["data"]["pack_left"], json!(9), "{member}"); assert_eq!(member["data"]["owed"], json!(300000)); assert_eq!(member["data"]["last_visit"], json!("2026-10-19")); every_page_reads(&h, &token, "yoga"); } // RULE: The kitchen package installs the same way: an order takes its quantity from the day's quota through the reservation engine, a line's total is the rule's arithmetic, and stock is the sum of its rows. #[test] fn kitchen_installs_into_an_empty_database_and_its_pages_read() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Kitchen"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "kitchen"}))["added"], true); every_page_reads(&h, &token, "kitchen"); let dish = create(&h, &token, "kitchen_dish", json!({"name": "Synthetic curry", "price": 15000, "currency": "XTS"})); let day = create(&h, &token, "kitchen_menu_day", json!({"name": "Synthetic curry, Monday", "dish": dish, "on": "2026-10-19", "quota": 2})); let customer = create(&h, &token, "kitchen_customer", json!({"name": "Synthetic Customer", "currency": "XTS"})); seed_serving(&h); let one = call_tool(&h, &token, "reserve_record", json!({"tool": "kitchen", "slot": day, "data": {"customer": customer, "name": "Synthetic order", "for_day": "2026-10-19", "quantity": 2, "currency": "XTS"}})); assert_eq!(one["ok"], true, "{one}"); let full = call_tool(&h, &token, "reserve_record", json!({"tool": "kitchen", "slot": day, "data": {"customer": customer}})); assert_eq!(full["reason"], "full"); let order = one["record"]["id"].as_str().unwrap().to_string(); let line = one["lines"][0]["id"].as_str().unwrap().to_string(); assert_eq!(read(&h, &token, "kitchen_order_line", &line)["data"]["line_total"], json!(30000)); create(&h, &token, "kitchen_payment", json!({"name": "UPI", "customer": customer, "order": order, "amount": 10000, "currency": "XTS", "method": "upi", "taken_on": "2026-10-19"})); create(&h, &token, "kitchen_delivery", json!({"name": "Synthetic order", "order": order, "customer": customer, "due_on": "2026-10-19", "slot": "lunch"})); let rice = create(&h, &token, "kitchen_ingredient", json!({"name": "Rice", "unit": "kg"})); create(&h, &token, "kitchen_stock", json!({"name": "Bought", "ingredient": rice, "quantity": 10, "on": "2026-10-18", "reason": "bought"})); create(&h, &token, "kitchen_stock", json!({"name": "Used", "ingredient": rice, "quantity": -3, "on": "2026-10-19", "reason": "used"})); assert_eq!(read(&h, &token, "kitchen_menu_day", &day)["data"]["left"], json!(0)); let customer = read(&h, &token, "kitchen_customer", &customer); assert_eq!(customer["data"]["owed"], json!(20000), "{customer}"); assert_eq!(read(&h, &token, "kitchen_ingredient", &rice)["data"]["in_stock"], json!(7)); every_page_reads(&h, &token, "kitchen"); } fn act(h: &Harness, token: &str, table: &str, id: &str, act: &str, picked: Option<&str>) -> Value { let mut args = json!({"table": table, "id": id, "act": act}); if let Some(picked) = picked { args["picked"] = json!(picked); } call_tool(h, token, "run_record_act", args) } // RULE: Book takes a member and a positive whole number of places; one booking consumes that many places, refuses overbooking and returns all places when cancelled. #[test] fn yoga_books_and_cancels_by_its_acts() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Studio"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "yoga"}))["added"], true); let class = create(&h, &token, "yoga_class", json!({"name": "Synthetic hatha", "capacity": 4})); let session = create(&h, &token, "yoga_session", json!({"name": "Synthetic hatha, Monday", "class": class, "on": "2026-10-19"})); let member = create(&h, &token, "yoga_member", json!({"name": "Synthetic Member", "currency": "XTS"})); seed_serving(&h); let booked = call_tool(&h, &token, "run_record_act", json!({"table":"yoga_session", "id":session, "act":"book", "picked":[member, 3]})); assert_eq!(booked["ok"], true, "{booked}"); assert_eq!(booked["record"]["data"]["member"], json!(member)); assert_eq!(booked["record"]["data"]["on"], "2026-10-19"); assert_eq!(booked["record"]["data"]["name"], "Synthetic hatha, Monday"); assert_eq!(booked["record"]["data"]["quantity"], 3); assert_eq!(booked["remaining"], 1); let page = read(&h, &token, "yoga_session", &session); assert_eq!(page["data"]["booked"], 3); assert_eq!(page["data"]["places_left"], 1); let full = call_tool(&h, &token, "run_record_act", json!({"table":"yoga_session", "id":session, "act":"book", "picked":[member, 2]})); assert_eq!(full, json!({"ok":false,"reason":"full","remaining":1})); for quantity in [json!(0), json!(-1), json!(1.5)] { assert!(call_tool_raw(&h, &token, "run_record_act", json!({"table":"yoga_session", "id":session, "act":"book", "picked":[member, quantity]})).is_error); } let cancelled = act(&h, &token, "yoga_booking", booked["record"]["id"].as_str().unwrap(), "cancel", None); assert_eq!(cancelled["remaining"], 4, "{cancelled}"); } // RULE: Take an order picks a customer and quantity, writes one order for that quantity and consumes that many plates; its delivery is written by rule with the order's slot and address, unpaid and undelivered; the chef's Delivered dates it and Paid writes the payment for the order's total; cancellation returns the plates. #[test] fn kitchen_orders_cancels_and_delivers_by_its_acts() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Kitchen"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "kitchen"}))["added"], true); let dish = create(&h, &token, "kitchen_dish", json!({"name": "Synthetic curry", "price": 15000, "currency": "XTS"})); let day = create(&h, &token, "kitchen_menu_day", json!({"name": "Synthetic curry, Monday", "dish": dish, "on": "2026-10-19", "quota": 3})); let customer = create(&h, &token, "kitchen_customer", json!({"name": "Synthetic Customer", "currency": "XTS"})); seed_serving(&h); let ordered = call_tool(&h, &token, "run_record_act", json!({"table":"kitchen_menu_day", "id":day, "act":"order", "picked":[customer, 2]})); assert_eq!(ordered["ok"], true, "{ordered}"); assert_eq!(ordered["record"]["data"]["for_day"], "2026-10-19"); assert_eq!(ordered["record"]["data"]["customer"], customer); assert_eq!(ordered["record"]["data"]["quantity"], 2); assert_eq!(ordered["remaining"], 1); assert_eq!(read(&h, &token, "kitchen_menu_day", &day)["data"]["left"], 1); let full = call_tool(&h, &token, "run_record_act", json!({"table":"kitchen_menu_day", "id":day, "act":"order", "picked":[customer, 2]})); assert_eq!(full, json!({"ok":false,"reason":"full","remaining":1})); for quantity in [json!(0), json!(-1), json!(1.5)] { assert!(call_tool_raw(&h, &token, "run_record_act", json!({"table":"kitchen_menu_day", "id":day, "act":"order", "picked":[customer, quantity]})).is_error); } let orders = call_tool(&h, &token, "list_records", json!({"table":"kitchen_order"}))["records"].clone(); assert_eq!(orders.as_array().unwrap().len(), 1, "{orders}"); assert_eq!(orders[0]["data"]["quantity"], 2); let order = ordered["record"]["id"].as_str().unwrap().to_string(); let deliveries = call_tool(&h, &token, "list_records", json!({"table": "kitchen_delivery", "today": "2026-10-19"}))["records"].clone(); assert_eq!(deliveries.as_array().unwrap().len(), 1, "{deliveries}"); assert_eq!((&deliveries[0]["data"]["paid"], &deliveries[0]["data"]["delivered"]), (&json!(false), &json!(false)), "{deliveries}"); let delivery = deliveries[0]["id"].as_str().unwrap().to_string(); // Staff's order is priced as a customer's is: one line for the day's dish. assert_eq!(ordered["quote"], json!({"quantity": 2, "price": 15000, "currency": "XTS", "total": 30000}), "{ordered}"); let line = &ordered["lines"][0]["data"]; assert_eq!((&line["order"], &line["customer"], &line["dish"], &line["line_total"]), (&json!(order), &json!(customer), &json!(dish), &json!(30000)), "{ordered}"); let delivered = act(&h, &token, "kitchen_delivery", &delivery, "delivered", None); assert_eq!(delivered["record"]["data"]["delivered"], true, "{delivered}"); let delivered = read(&h, &token, "kitchen_delivery", &delivery); assert!(is_a_day(&delivered["data"]["delivered_at"]), "{delivered}"); assert_eq!(delivered["data"]["total"], json!(30000), "the delivery says the order's total"); assert_eq!(act(&h, &token, "kitchen_delivery", &delivery, "paid", None)["record"]["data"]["paid"], true); let payments = call_tool(&h, &token, "list_records", json!({"table": "kitchen_payment"}))["records"].clone(); assert_eq!(payments.as_array().unwrap().len(), 1, "{payments}"); let paid = &payments[0]["data"]; assert_eq!((&paid["order"], &paid["customer"], &paid["amount"], &paid["taken_on"]), (&json!(order), &json!(customer), &json!(30000), &json!("2026-10-19")), "{payments}"); let cancelled = act(&h, &token, "kitchen_order", &order, "cancel", None); assert_eq!(cancelled["remaining"], 3, "{cancelled}"); let lines = call_tool(&h, &token, "list_records", json!({"table": "kitchen_order_line"}))["records"].clone(); assert!(lines.as_array().unwrap().is_empty(), "an order's lines go with it: {lines}"); } fn act_number(h: &Harness, token: &str, table: &str, id: &str, act: &str, picked: Option) -> Value { let mut args = json!({"table": table, "id": id, "act": act}); if let Some(picked) = picked { args["picked"] = json!(picked); } call_tool(h, token, "run_record_act", args) } fn is_a_day(value: &Value) -> bool { value.as_str().is_some_and(|d| chrono::NaiveDate::parse_from_str(d, "%Y-%m-%d").is_ok()) } // RULE: Here on a booking writes its attendance row by rule — the booking, its member and the session's day — so who sees the row (mine(member)) is never left to whoever marks it; Take cash on a member creates a payment from a number the person picks and Renew a membership on a plan the person picks, each dated today by its rule, with the member link and currency from the member's own row. #[test] fn yoga_here_take_cash_and_renew_are_acts_and_rules() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Studio"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "yoga"}))["added"], true); let class = create(&h, &token, "yoga_class", json!({"name": "Synthetic hatha", "capacity": 2})); let session = create(&h, &token, "yoga_session", json!({"name": "Synthetic hatha, Monday", "class": class, "on": "2026-10-19"})); let member = create(&h, &token, "yoga_member", json!({"name": "Synthetic Member", "currency": "XTS"})); seed_serving(&h); let pack = create(&h, &token, "yoga_offer", json!({"name": "Synthetic ten classes", "kind": "classes", "classes": 10, "valid_days": 60, "price": 100, "currency": "XTS"})); let renewed = call_tool(&h, &token, "run_record_act", json!({"table": "yoga_member", "id": member, "act": "renew", "picked": pack})); let renewed = call_tool(&h, &token, "get_record", json!({"table": "yoga_pack", "id": renewed["record"]["id"]})); assert_eq!(renewed["data"]["classes"], json!(10), "{renewed}"); let renewed = json!({"record": renewed}); assert_eq!(renewed["record"]["data"]["member"], json!(member)); assert_eq!(renewed["record"]["data"]["currency"], "XTS"); assert!(is_a_day(&renewed["record"]["data"]["bought_on"]), "{renewed}"); assert!(call_tool_raw(&h, &token, "run_record_act", json!({"table": "yoga_member", "id": member, "act": "renew", "picked": "no-such-plan"})).is_error); let booked = call_tool(&h, &token, "run_record_act", json!({"table":"yoga_session", "id":session, "act":"book", "picked":[member, 1]})); let booking = booked["record"]["id"].as_str().unwrap().to_string(); let here = act(&h, &token, "yoga_booking", &booking, "here", None); assert_eq!(here["record"]["data"]["here"], true, "{here}"); let attended = call_tool(&h, &token, "list_records", json!({"table": "yoga_attendance", "today": "2026-10-19"}))["records"].clone(); assert_eq!(attended.as_array().unwrap().len(), 1, "{attended}"); assert_eq!(attended[0]["data"]["booking"], json!(booking)); assert_eq!(attended[0]["data"]["member"], json!(member)); assert_eq!(attended[0]["data"]["attended_on"], "2026-10-19"); // Here is offered once: a second mark would take a second class off the pack. assert!(call_tool_raw(&h, &token, "run_record_act", json!({"table": "yoga_booking", "id": booking, "act": "here"})).is_error); let paid = act_number(&h, &token, "yoga_member", &member, "take_cash", Some(200000)); assert_eq!(paid["record"]["data"]["method"], "cash", "{paid}"); assert!(is_a_day(&paid["record"]["data"]["taken_on"]), "{paid}"); assert!(call_tool_raw(&h, &token, "run_record_act", json!({"table": "yoga_member", "id": member, "act": "take_cash"})).is_error); let member = read(&h, &token, "yoga_member", &member); assert_eq!(member["data"]["pack_left"], json!(9), "{member}"); assert_eq!(member["data"]["paid"], json!(200000)); every_page_reads(&h, &token, "yoga"); } // RULE: Take cash on a kitchen customer creates a cash payment from a picked amount, linked to the customer and dated today by its rule. #[test] fn kitchen_takes_cash_by_its_act() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Kitchen"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "kitchen"}))["added"], true); let customer = create(&h, &token, "kitchen_customer", json!({"name": "Synthetic Customer", "currency": "XTS"})); let paid = act_number(&h, &token, "kitchen_customer", &customer, "take_cash", Some(15000)); assert_eq!(paid["record"]["data"]["customer"], json!(customer), "{paid}"); assert_eq!(paid["record"]["data"]["amount"], json!(15000)); assert!(is_a_day(&paid["record"]["data"]["taken_on"]), "{paid}"); assert_eq!(read(&h, &token, "kitchen_customer", &customer)["data"]["paid"], json!(15000)); } // RULE: A pack balance is purchase classes less attendance rows; no writable counter can overwrite a debit during sync. #[test] fn yoga_pack_remaining_is_a_rollup_of_attendance_rows() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Packs"); call_tool(&h, &token, "add_tool", json!({"tool_id":"yoga"})); let member = create(&h, &token, "yoga_member", json!({"name":"Synthetic Member"})); let pack = create(&h, &token, "yoga_pack", json!({"name":"Two classes", "member":member, "classes":2, "expires":"2026-10-20"})); let class = create(&h, &token, "yoga_class", json!({"name":"Synthetic Class", "capacity":4})); let session = create(&h, &token, "yoga_session", json!({"name":"Synthetic Session", "class":class, "on":"2026-10-19"})); seed_serving(&h); for left in [1,0] { let booked = call_tool(&h, &token, "run_record_act", json!({"table":"yoga_session", "id":session, "act":"book", "picked":[member,1]})); create(&h, &token, "yoga_attendance", json!({"booking":booked["record"]["id"], "member":member, "pack":pack, "attended_on":"2026-10-19"})); assert_eq!(read(&h, &token, "yoga_pack", &pack)["data"]["classes_left"], left); assert_eq!(read(&h, &token, "yoga_member", &member)["data"]["pack_left"], left); } for table in cotton_core::toolkits::shipped_kit_tools().into_iter().find(|p| p.tool.id == "yoga").unwrap().tables { for col in table.columns { assert_ne!(col.counter, Some(true)); if ["classes_left", "classes_used", "pack_left"].contains(&col.key.as_str()) { assert!(col.computed.is_some(), "{} must never be stored", col.key); } } } } // RULE: Order on a dish's card takes a reservation on one of that dish's menu days, picked with the customer and the plates: the order is for that day and its delivery is written; a menu day of another dish is no choice, and the slot is never picked on a menu day's own Take an order. #[test] fn a_dish_is_ordered_from_its_card_for_one_of_its_days() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Kitchen"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "kitchen"}))["added"], true); let dish = create(&h, &token, "kitchen_dish", json!({"name": "Synthetic curry", "price": 15000, "currency": "XTS"})); let other = create(&h, &token, "kitchen_dish", json!({"name": "Synthetic dal", "price": 9000, "currency": "XTS"})); let tuesday = create(&h, &token, "kitchen_menu_day", json!({"name": "Synthetic curry, Tuesday", "dish": dish, "on": "2026-10-20", "quota": 4})); let dal = create(&h, &token, "kitchen_menu_day", json!({"name": "Synthetic dal, Tuesday", "dish": other, "on": "2026-10-20", "quota": 4})); let customer = create(&h, &token, "kitchen_customer", json!({"name": "Synthetic Customer", "currency": "XTS"})); seed_serving(&h); let order = |slot: &str| call_tool_raw(&h, &token, "run_record_act", json!({"table": "kitchen_dish", "id": dish, "act": "order", "picked": [slot, customer, 2]})); assert!(order(&dal).is_error, "another dish's day is no choice"); let ordered = call_tool(&h, &token, "run_record_act", json!({"table": "kitchen_dish", "id": dish, "act": "order", "picked": [tuesday, customer, 2]})); assert_eq!(ordered["ok"], true, "{ordered}"); assert_eq!((&ordered["record"]["data"]["for_day"], &ordered["record"]["data"]["menu_day"]), (&json!("2026-10-20"), &json!(tuesday)), "{ordered}"); assert_eq!(ordered["remaining"], 2); assert_eq!(ordered["quote"]["total"], json!(30000), "{ordered}"); let deliveries = call_tool(&h, &token, "list_records", json!({"table": "kitchen_delivery"}))["records"].clone(); assert_eq!(deliveries[0]["data"]["due_on"], json!("2026-10-20"), "{deliveries}"); let package = cotton_core::toolkits::shipped_kit_tools().into_iter().find(|p| p.tool.id == "kitchen").unwrap(); let mut tables = package.tables.clone(); let day = tables.iter_mut().find(|t| t.id == "kitchen_menu_day").unwrap(); let take = day.acts.as_mut().unwrap().iter_mut().find(|a| a.id == "order").unwrap(); let mut picks = take.picks.clone().unwrap(); picks.insert(0, serde_json::from_value(json!({"argument": "slot", "kind": "record", "table": "kitchen_menu_day"})).unwrap()); take.picks = Some(picks); assert!(cotton_core::definitions::acts::validate_tables(&package.tool, &tables).is_err(), "a menu day never picks its slot"); } /// A business kit's deck: the fan cards its kit.json lists after its first tool, read from the kit, never listed here. fn deck_of(kit: &str) -> Vec { let (_, json) = cotton_core::kit::APP_KITS.iter().find(|(id, _)| *id == kit).unwrap_or_else(|| panic!("no kit {kit}")); let file: serde_json::Value = serde_json::from_str(json).expect("a kit.json"); let tools: Vec = file["tools"].as_array().expect("a kit names its tools").iter().map(|tool| tool.as_str().unwrap().to_string()).collect(); assert_eq!(tools.first().map(String::as_str), Some(kit), "{kit}'s first tool is its own"); tools[1..].to_vec() } // RULE: The kitchen's deck is data. Each fan card beside Kitchen (Orders, Plans, Customers, Money, Chef, Kitchen) is a package of views over Kitchen's tables, refused before Kitchen is installed and added after it; every view reads as the phone sends it, and every kitchen table opens on a card that validates. #[test] fn kitchen_deck_cards_are_views_over_the_kitchen() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Kitchen"); assert!(call_tool_raw(&h, &token, "add_tool", json!({"tool_id": "kitchen_orders"})).is_error, "Orders reads Kitchen's tables"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "kitchen"}))["added"], true); let deck = deck_of("kitchen"); assert!(!deck.is_empty()); for tool in &deck { assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": tool}))["added"], true, "{tool}"); } let kit = cotton_core::toolkits::shipped_kit_tools(); for tool in std::iter::once("kitchen").chain(deck.iter().map(String::as_str)) { let package = kit.iter().find(|p| p.tool.id == tool).unwrap(); assert_eq!(package.tables.is_empty(), tool != "kitchen", "{tool}"); for view in &package.tool.views { let mut args = serde_json::Value::Object(view.read.clone()); args["view"] = json!(format!("{tool}/{}", view.id)); args["today"] = json!("2026-10-19"); call_tool(&h, &token, "list_records", args); } } let kitchen = kit.iter().find(|p| p.tool.id == "kitchen").unwrap(); for table in &kitchen.tables { table.card.as_ref().unwrap_or_else(|| panic!("{} opens on a card", table.id)).validate(table, "card").unwrap(); } } // RULE: A dish's rating is the mean of its reviews' stars, unrounded (a face rounds for display, d8e2b87fa), and its count theirs, never written by hand; a subscription's meals left count down from the orders taken against it, and an order on a plan with no meal left for its day, or on another customer's plan, is refused whole — no plate is taken. #[test] fn kitchen_ratings_and_meals_left_are_counted() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Kitchen"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "kitchen"}))["added"], true); let dish = create(&h, &token, "kitchen_dish", json!({"name": "Synthetic curry", "price": 15000, "currency": "XTS", "spice": "very_spicy"})); assert_eq!(read(&h, &token, "kitchen_dish", &dish)["data"]["rating"], json!(null), "no reviews, no rating"); for stars in [5, 5, 4] { create(&h, &token, "kitchen_review", json!({"name": "Synthetic R.", "dish": dish, "rating": stars, "on": "2026-10-19"})); } let rated = read(&h, &token, "kitchen_dish", &dish); assert_eq!((&rated["data"]["rating"], &rated["data"]["reviews"]), (&json!(14.0 / 3.0), &json!(3)), "{rated}"); let customer = create(&h, &token, "kitchen_customer", json!({"name": "Synthetic Customer", "currency": "XTS"})); let plan = create(&h, &token, "kitchen_plan", json!({"name": "Weekday lunch, 5 meals", "meals": 5, "price": 119900, "currency": "XTS"})); let bought = create(&h, &token, "kitchen_subscription", json!({"name": "Synthetic dinner", "customer": customer, "plan": plan, "meals": 5, "status": "active"})); assert!(is_a_day(&read(&h, &token, "kitchen_subscription", &bought)["data"]["started_on"]), "dated the day it is bought"); let held = create(&h, &token, "kitchen_subscription", json!({"name": "Synthetic lunch", "customer": customer, "plan": plan, "meals": 3, "status": "active", "started_on": "2026-10-01", "ends_on": "2026-10-31"})); let day = create(&h, &token, "kitchen_menu_day", json!({"name": "Synthetic curry, Monday", "dish": dish, "on": "2026-10-19", "quota": 5})); seed_serving(&h); for _ in 0..2 { let one = call_tool(&h, &token, "reserve_record", json!({"tool": "kitchen", "slot": day, "data": {"customer": customer, "name": "Plan meal", "for_day": "2026-10-19", "quantity": 1, "subscription": held}})); assert_eq!(one["ok"], true, "{one}"); assert_eq!(one["quote"]["total"], json!(0), "a meal on a plan is priced at nothing: {one}"); } assert_eq!(read(&h, &token, "kitchen_subscription", &held)["data"]["meals_left"], json!(1)); let plate = |who: &str, quantity: i64| call_tool_raw(&h, &token, "reserve_record", json!({"tool": "kitchen", "slot": day, "data": {"customer": who, "name": "Plan meal", "quantity": quantity, "subscription": held}})); assert!(plate(&customer, 2).is_error, "two plates on a plan with one meal left"); let other = create(&h, &token, "kitchen_customer", json!({"name": "Synthetic Other", "currency": "XTS"})); assert!(plate(&other, 1).is_error, "a meal on another customer's plan"); assert_eq!(read(&h, &token, "kitchen_menu_day", &day)["data"]["left"], json!(3), "a refused meal takes no plate"); assert!(!plate(&customer, 1).is_error, "the last meal"); assert_eq!(read(&h, &token, "kitchen_subscription", &held)["data"]["meals_left"], json!(0)); // A meal on a plan goes out paid by the plan: no payment of its own. let deliveries = call_tool(&h, &token, "list_records", json!({"table": "kitchen_delivery"}))["records"].clone(); assert!(deliveries.as_array().unwrap().iter().all(|d| d["data"]["paid"] == true && d["data"]["subscription"] == json!(held)), "{deliveries}"); assert!(call_tool(&h, &token, "list_records", json!({"table": "kitchen_payment"}))["records"].as_array().unwrap().is_empty()); } // RULE: The yoga decks carry no table of their own: each installs after the Schedule (the yoga tool) and every view it deals reads the Schedule's tables, the Clients' absent and ending-soon chips included; before the Schedule is there, a deck is refused by name. #[test] fn yoga_decks_install_over_the_schedule_and_every_view_reads() { let decks = deck_of("yoga"); assert!(decks.contains(&"yoga_classes".to_string()), "the yoga kit deals Classes, the deck this test refuses first"); let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Studio"); assert!(call_tool_raw(&h, &token, "add_tool", json!({"tool_id": "yoga_classes"})).is_error); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "yoga"}))["added"], true); let member = create(&h, &token, "yoga_member", json!({"name": "Synthetic Member", "currency": "XTS", "joined_on": "2026-10-01"})); create(&h, &token, "yoga_pack", json!({"name": "Monthly", "member": member, "bought_on": "2026-10-01", "expires": "2099-10-30", "price": 600000, "currency": "XTS"})); create(&h, &token, "yoga_class", json!({"name": "Synthetic flow", "category": "daily", "capacity": 4})); create(&h, &token, "yoga_expense", json!({"name": "Synthetic rent", "category": "rent", "amount": 100, "currency": "XTS", "spent_on": "2026-10-19"})); let kit = cotton_core::toolkits::shipped_kit_tools(); for deck in decks.iter().map(String::as_str) { assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": deck}))["added"], true, "{deck}"); let tool = &kit.iter().find(|p| p.tool.id == deck).unwrap().tool; assert!(tool.tables.is_empty(), "{deck} carries no table"); for view in &tool.views { let mut args = Value::Object(view.read.clone()); args["view"] = json!(format!("{deck}/{}", view.id)); args["today"] = json!("2026-10-19"); call_tool(&h, &token, "list_records", args); } } let ending = call_tool(&h, &token, "list_records", json!({"table": "yoga_member", "where": {"pack_expires": {"ahead_days": 14}}})); assert_eq!(ending["records"].as_array().unwrap().len(), 0, "a membership ending in 2099 is not ending soon"); let absent = call_tool(&h, &token, "list_records", json!({"table": "yoga_member", "where": {"last_visit": {"before_days": 14}}})); assert_eq!(absent["records"].as_array().unwrap().len(), 0, "a member who never came has no last visit to be absent from"); let yoga = kit.iter().find(|p| p.tool.id == "yoga").unwrap(); for table in &yoga.tables { table.card.as_ref().unwrap_or_else(|| panic!("{} opens on a card", table.id)).validate(table, "card").unwrap(); } } // RULE: A dish's rating is the unrounded average its reviews derive, and its reading writes it as a person would: three reviews of 5, 5 and 4 hold 4.666…, and the dish's line reads "★ 4.7". #[test] fn a_dish_rating_derives_unrounded_and_reads_one_decimal() { let mut h = Harness::new(); let token = mint_token(&mut h, &["read", "write"], "Kitchen"); assert_eq!(call_tool(&h, &token, "add_tool", json!({"tool_id": "kitchen"}))["added"], true); let dish = create(&h, &token, "kitchen_dish", json!({"name": "Synthetic curry", "price": 15000, "currency": "XTS"})); for rating in [5, 5, 4] { create(&h, &token, "kitchen_review", json!({"name": "Synthetic review", "dish": dish, "rating": rating})); } let read = read(&h, &token, "kitchen_dish", &dish); let stored = read["data"]["rating"].as_f64().expect("the rating is derived"); assert!((stored - 14.0 / 3.0).abs() < 1e-12, "the stored average is never rounded: {read}"); let definition = cotton_core::toolkits::shipped_kit_tools().into_iter().find(|p| p.tool.id == "kitchen").unwrap() .tables.into_iter().find(|t| t.id == "kitchen_dish").unwrap(); let line = cotton_core::records::page::slip_line(&definition, read["data"].as_object().unwrap(), None, None).unwrap_or_default(); assert!(line.contains("★ 4.7") && !line.contains("4.67"), "{line}"); }