use super::*; use serde_json::json; fn tool() -> ToolDefinition { serde_json::from_value(json!({"id":"classes","name":"Classes","tables":["class"], "instructions":"","automations":[],"author":{"device":"test","name":"Test"},"signature":null})).unwrap() } // RULE: Packages name only shipped capabilities, so an unknown engine never installs as a silently incomplete tool. #[test] fn capability_names_are_validated() { let mut t = tool(); t.engines = vec!["reserve@1".into(), "rules@1".into()]; assert!(t.validate().is_ok()); t.engines.push("unknown@1".into()); assert!(t.validate().is_err()); } // RULE: Guards and arithmetic read declared columns only; a false guard cannot produce a write and arithmetic preserves whole amount values. #[test] fn rules_guard_and_arithmetic_are_typed() { use super::automations::expressions::*; let row = json!({"places":3,"price":125,"active":true}) .as_object() .unwrap() .clone(); assert_eq!( evaluate( &json!({"*": [{"column":"places"},{"column":"price"}]}), &row ) .unwrap(), json!(375) ); assert_eq!( evaluate(&json!({">": [{"column":"places"},4]}), &row).unwrap(), json!(false) ); assert_eq!( evaluate(&json!({">": [{"column":"places"},2]}), &row).unwrap(), json!(true) ); assert!(evaluate(&json!({"/": [3,0]}), &row).is_err()); assert!(evaluate(&json!({"+": [i64::MAX,1]}), &row).is_err()); assert!(evaluate(&json!({"/": [i64::MIN,-1]}), &row).is_err()); assert_eq!( evaluate(&json!({"*": [3.0,125]}), &row).unwrap(), json!(375) ); assert_eq!( evaluate( &json!({">": [9007199254740993_i64,9007199254740992_i64]}), &row ) .unwrap(), json!(true) ); assert_eq!( evaluate(&json!({"==": [3.0,3]}), &row).unwrap(), json!(true) ); assert_eq!( evaluate(&json!({"==": [null,true]}), &row).unwrap(), json!(false) ); assert!(evaluate(&json!({"column":"missing"}), &row).is_err()); } // RULE: A mutable counter would lose debits to last-writer-wins; an automation must write movement rows and a balance must be computed. #[test] fn automations_refuse_declared_counters_and_unknown_expression_columns() { let mut table: TableDefinition = serde_json::from_str(include_str!( "../../../cotton-tools/tests/fixtures/classroom/tables/student.json" )) .unwrap(); table.columns.push( serde_json::from_value(json!({"key":"balance","type":"number","counter":true})).unwrap(), ); let mut t = tool(); t.tables = vec![table.id.clone()]; t.automations = vec![ json!({"id":"debit","name":"Debit","when":{"table":table.id,"column":"attending","to":true}, "then":[{"set":{"column":"balance","value":1}}]}), ]; assert!(automations::validate_tables(&t, &[table.clone()]) .unwrap_err() .contains("counter")); table.columns.last_mut().unwrap().counter = None; t.automations[0]["then"][0]["set"]["value"] = json!({"+": [{"column":"missing"},1]}); assert!(automations::validate_tables(&t, &[table]) .unwrap_err() .contains("missing")); } // RULE: Reservation definitions bind a numeric capacity and a real claim link at save time, so an action never guesses its storage or oversells a misspelled column. #[test] fn reservation_definition_requires_declared_capacity_and_claim_link() { let mut t = tool(); t.tables = vec!["slot".into(), "claim".into()]; t.engines = vec!["reserve@1".into()]; t.reserve=Some(serde_json::from_value(json!({"slot_table":"slot","capacity_column":"capacity","claim_table":"claim","slot_column":"slot"})).unwrap()); let tables:Vec=[json!({"id":"slot","name":"Slot","plural":"Slots","columns":[{"key":"capacity","type":"number"}]}), json!({"id":"claim","name":"Claim","plural":"Claims","columns":[{"key":"slot","type":"link","link_to":"slot"}]})] .into_iter().map(|mut v| {let base:serde_json::Value=serde_json::from_str(include_str!("../../../cotton-tools/tests/fixtures/classroom/tables/student.json")).unwrap();for(k,x)in base.as_object().unwrap(){if v.get(k).is_none(){v[k]=x.clone();}}serde_json::from_value(v).unwrap()}).collect(); assert!(reserve::validate_tables(&t, &tables).is_ok()); t.reserve.as_mut().unwrap().capacity_column = "missing".into(); assert!(reserve::validate_tables(&t, &tables).is_err()); } // RULE: Rules set typed text and calendar values from expressions without treating dates as arithmetic or accepting writes to counters. #[test] fn rules_set_text_dates_and_clock_expressions() { use super::automations::expressions::*; let mut table: TableDefinition = serde_json::from_str(include_str!( "../../../cotton-tools/tests/fixtures/classroom/tables/student.json" )) .unwrap(); for (key, kind) in [ ("marked_at", "date"), ("marked_on", "date"), ("copy", "text"), ] { table .columns .push(serde_json::from_value(json!({"key":key,"type":kind})).unwrap()); } table.columns.iter_mut().find(|c| c.key == "marked_at").unwrap().format = Some(ColumnFormat::CalendarDatetime); let mut t = tool(); t.tables = vec![table.id.clone()]; t.engines = vec!["rules@1".into()]; for (column, value, expected) in [ ( "marked_at", json!({"now":true}), json!("2026-10-19T23:59:59Z"), ), ("marked_on", json!({"today":true}), json!("2026-10-20")), ("copy", json!({"column":"name"}), json!("Synthetic")), ] { t.automations = vec![ json!({"id":"mark","name":"Mark","when":{"table":table.id,"column":"attending","to":true},"then":[{"set":{"column":column,"value":value}}]}), ]; assert!(automations::validate_tables(&t, &[table.clone()]).is_ok()); assert_eq!( evaluate_at( &value, json!({"name":"Synthetic"}).as_object().unwrap(), "2026-10-19T23:59:59Z", "2026-10-20" ) .unwrap(), expected ); } t.automations[0]["then"][0]["set"] = json!({"column":"score","value":{"now":true}}); assert!(automations::validate_tables(&t, &[table]).is_err()); assert!(evaluate_at(&json!({"now":false}), &Default::default(), "x", "y").is_err()); } // RULE: Reservation quantity is a stored numeric claim column, so readonly or misspelled quantities cannot change what a claim consumes. #[test] fn reservation_quantity_binding_is_stored_numeric() { let mut t = tool(); t.tables = vec!["slot".into(), "claim".into()]; t.engines = vec!["reserve@1".into()]; t.reserve = Some(serde_json::from_value(json!({"slot_table":"slot","capacity_column":"capacity","claim_table":"claim","slot_column":"slot","quantity_column":"quantity"})).unwrap()); let base: serde_json::Value = serde_json::from_str(include_str!("../../../cotton-tools/tests/fixtures/classroom/tables/student.json")).unwrap(); let mut tables: Vec = [ json!({"id":"slot","columns":[{"key":"capacity","type":"number"}]}), json!({"id":"claim","columns":[{"key":"slot","type":"link","link_to":"slot"},{"key":"quantity","type":"number"}]}) ].into_iter().map(|v| {let mut b=base.clone();for(k,v)in v.as_object().unwrap(){b[k]=v.clone();}serde_json::from_value(b).unwrap()}).collect(); assert!(reserve::validate_tables(&t, &tables).is_ok()); tables[1].columns[1].column_type = ColumnType::Text; assert!(reserve::validate_tables(&t, &tables).is_err()); tables[1].columns[1].column_type = ColumnType::Number; tables[1].columns[1].readonly = Some(true); assert!(reserve::validate_tables(&t, &tables).is_err()); }