Cotton code
142 lines · 9.4 KB · RustRaw
//! A synthetic person's own address, written through the same MCP path as FFI.mod support;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 datasets() {    support::published::served()}/// RULE: A pick, all its companion facts, and its Undo are one atomic record change; invalid ids never leave partial facts.#[test]fn place_pick_is_atomic_undoable_and_preserves_unmatched_text() {    datasets();    let mut h = Harness::new();    let token = mint_token(&mut h, &["read", "write"], "Synthetic places test");    let definition = json!({        "id":"address","name":"Address","plural":"Addresses","builtin":false,        "title":"city","columns":[            {"key":"city","type":"place","place":{"id":"city_place","country":"country","zone":"timezone"}},            {"key":"city_place","type":"text","readonly":true},            {"key":"country","type":"text","readonly":true},            {"key":"timezone","type":"text","readonly":true}        ],        "slip":{"title":"city"},"tile":{"face":"slip-list"},"page":[],"instructions":"An invented address."    });    h.run("INSERT INTO cotton_table_definitions (id,workspace_id,definition,builtin,created_at,updated_at) VALUES ('address',?,?,0,?,?)",        &[SqlValue::Text(h.workspace_id.clone()),SqlValue::Text(definition.to_string()),          SqlValue::Text("2026-10-14T00:00:00Z".into()),SqlValue::Text("2026-10-14T00:00:00Z".into())]);    let call = |name: &str, args: Value| call_tool(&h, &token, name, args);    let made = call("create_record",json!({"table":"address","data":{"city":"An unlisted town"}}));    let id = &made["record"]["id"];    let picked = call("update_record",json!({"table":"address","id":id,"patch":{"city":{"place":"geonames:1269843"}}}));    assert_eq!(picked["record"]["data"]["city"], "Hyderabad");    assert_eq!(picked["record"]["data"]["city_place"], "geonames:1269843");    assert_eq!(picked["record"]["data"]["country"], "IN");    assert_eq!(picked["record"]["data"]["timezone"], "Asia/Kolkata");    assert_eq!(picked["change"]["after"]["city_place"], "geonames:1269843");    let refused = call_tool_raw(&h,&token,"update_record",json!({"table":"address","id":id,"patch":{"city":{"place":"geonames:999999"}}}));    assert!(refused.is_error);    let still = call("get_record_by_table",json!({"table":"address","id":id}));    assert_eq!(still["data"]["city"], "Hyderabad");    let undone = call("undo",json!({"change_id":picked["change"]["id"]}));    assert_eq!(undone["record"]["data"]["city"], "An unlisted town");    assert!(undone["record"]["data"].get("city_place").is_none());    let refused = call_tool_raw(&h,&token,"update_record",json!({"table":"address","id":id,"patch":{"country":"GB"}}));    assert!(refused.is_error, "country is derived only by a pick");    // A saved place remains this person's record; only its nearest dataset id links it.    let own_location = "22222222-2222-4222-8222-222222222222";    h.run("INSERT INTO cotton_locations (id,workspace_id,name,near,created_at,updated_at) VALUES (?,?,?,?,'2026-10-14','2026-10-14')",        &[own_location.to_string().into(),h.workspace_id.clone().into(),"Invented courtyard".to_string().into(),"geonames:1269843".to_string().into()]);    let own_id = format!("location:{own_location}");    let picked = call("update_record",json!({"table":"address","id":id,"patch":{"city":{"place":own_id}}}));    assert_eq!(picked["record"]["data"]["city"], "Invented courtyard");    assert_eq!(picked["record"]["data"]["city_place"], own_id);    assert_eq!(picked["record"]["data"]["country"], "IN");    assert_eq!(picked["record"]["data"]["timezone"], "Asia/Kolkata");    let raw = call("update_record",json!({"table":"address","id":id,"patch":{"city":"An unmatched village"}}));    assert!(raw["record"]["data"].get("country").is_none());    let restored = call("undo",json!({"change_id":raw["change"]["id"]}));    assert_eq!(restored["record"]["data"]["city_place"], own_id);    assert_eq!(restored["record"]["data"]["country"], "IN");    h.run("UPDATE cotton_locations SET trashed_at='2026-10-14' WHERE id=?", &[own_location.to_string().into()]);    let refused = call_tool_raw(&h,&token,"update_record",json!({"table":"address","id":id,"patch":{"city":{"place":own_id}}}));    assert!(refused.is_error, "a saved location in the Bin is no longer selectable");}/// RULE: Built-in person picks, raw imported city edits, and Undo keep the stable link and all derived facts consistent in one recorded change.#[test]fn builtin_person_place_facts_and_raw_import_invalidation() {    datasets();    let mut h = Harness::new();    let token = mint_token(&mut h, &["read","write"], "Synthetic built-in places");    let call = |name: &str,args: Value| call_tool(&h,&token,name,args);    let made = call("create_record",json!({"table":"person","data":{        "full_name":"Invented traveller","city":{"place":"geonames:1269843"}    }}));    let id = &made["record"]["id"];    assert_eq!(made["record"]["data"]["city"],"Hyderabad");    assert_eq!(made["record"]["data"]["city_place"],"geonames:1269843");    assert_eq!(made["record"]["data"]["country"],"IN");    assert_eq!(made["record"]["data"]["timezone"],"Asia/Kolkata");    assert_eq!(made["change"]["after"]["city_place"],"geonames:1269843");    let changed = call("update_record",json!({"table":"person","id":id,"patch":{"city":"An unlisted village"}}));    assert!(changed["record"]["data"].get("city_place").is_none());    assert!(changed["record"]["data"].get("country").is_none());    let undone = call("undo",json!({"change_id":changed["change"]["id"]}));    assert_eq!(undone["record"]["data"]["city_place"],"geonames:1269843");    assert_eq!(undone["record"]["data"]["timezone"],"Asia/Kolkata");    call("update_person",json!({"id":id,"city":"Another unlisted town"}));    let imported = call("get_record_by_table",json!({"table":"person","id":id}));    assert_eq!(imported["data"]["city"],"Another unlisted town");    assert!(imported["data"].get("city_place").is_none());    assert!(imported["data"].get("country").is_none());    assert!(imported["data"].get("timezone").is_none());}/// RULE: An event place pick keeps calendar edit semantics: a Google-linked event's place is Google's (Days, 20 Nov 2026), so the pick is refused in the sentence a phone shows and the row is left as it was.#[test]fn builtin_event_place_pick_on_a_google_event_is_refused() {    datasets();    let mut h = Harness::new();    h.run("INSERT INTO cotton_collections (id,workspace_id,kind,name,is_default,created_at) VALUES ('synthetic-calendar',?,'calendar','Invented calendar',1,'2026-10-14')", &[h.workspace_id.clone().into()]);    let token = mint_token(&mut h,&["read","write"],"Synthetic calendar place");    let made = call_tool(&h,&token,"create_record",json!({"table":"event","data":{        "title":"Invented appointment","starts_at":"2026-11-01T09:00:00.000Z","location":"Old town"    }}));    let id = &made["record"]["id"];    h.run("UPDATE cotton_events SET gcal_id='synthetic-calendar-event',sync_state='synced',on_google=1 WHERE id=?",        &[id.as_str().unwrap().to_string().into()]);    let refused = call_tool_raw(&h,&token,"update_record",json!({"table":"event","id":id,"patch":{"location":{"place":"geonames:1269843"}}}));    assert!(refused.is_error, "{}", refused.text);    assert!(refused.text.contains(cotton_tools::mcp::handlers::records_1::events::GOOGLE_READ_ONLY), "{}", refused.text);    let stored = h.db.get("SELECT location, sync_state FROM cotton_events WHERE id=?", &[id.as_str().unwrap().to_string().into()]).unwrap().unwrap();    assert_eq!(stored.text("location"),Some("Old town"));    assert_eq!(stored.text("sync_state"),Some("synced"));}/// RULE (14 Nov 2026): a pick is copied whole into the person's own record — its coordinates and the published version beside its id, country and zone — wherever the file keeps those columns (a copy gains them from the object's definitions); writing unpicked text clears them with the id.#[test]fn a_pick_keeps_its_coordinates_and_version_where_the_file_stores_them() {    datasets();    let mut h = Harness::new();    // The copy gains the columns as the object's definitions serve them.    h.with(|db| {        use cotton_store::schema::package_shape::{add_served, served_column};        let columns = [served_column("city_lat", "number", false).unwrap(), served_column("city_lng", "number", false).unwrap(), served_column("city_version", "text", false).unwrap()];        add_served(db, "cotton_people", &columns).unwrap();    });    let token = mint_token(&mut h, &["read","write"], "Synthetic coordinates");    let call = |name: &str,args: Value| call_tool(&h,&token,name,args);    let made = call("create_record",json!({"table":"person","data":{"full_name":"Invented traveller","city":{"place":"geonames:1269843"}}}));    let data = &made["record"]["data"];    assert_eq!((data["city_place"].as_str(), data["city_lat"].as_f64(), data["city_lng"].as_f64()), (Some("geonames:1269843"), Some(17.38), Some(78.48)));    assert_eq!(data["city_version"], support::published::VERSION, "the version the server answered at: {data}");    let unpicked = call("update_record",json!({"table":"person","id":made["record"]["id"],"patch":{"city":"An unlisted village"}}));    assert!(unpicked["record"]["data"].get("city_lat").is_none());    assert!(unpicked["record"]["data"].get("city_version").is_none());}