//! Groups leave the generic store (137): a group is a `cotton_groups` row and //! each record a person put in it is its own `cotton_group_members` row, its //! id derived from the group and the record. Before 137 a member was one key //! of the group row's `data`, merged per key; this guard holds that the move //! to rows loses nothing that merge gave: two devices that each add someone //! to the same group while apart both keep theirs, on both copies. //! //! Only the store's public API is used: `mint` writes, `arrive` folds. use cotton_store::actor::{Actor, JobName}; use cotton_store::fixtures::{file_at_version, seed_workspace_row, NOW, OWNER, WORKSPACE}; use cotton_store::fold::{arrive, Arrival, FoldResult}; use cotton_store::meta::{seed_meta, Meta}; use cotton_store::mint::{first_author, mint, Act, AuthorReason, Change, Clock, Held, History, IdSource}; use cotton_store::protocol::derived_id::group_member_id; use cotton_store::protocol::types::CottonOp; use cotton_store::schema::ddl::SCHEMA_VERSION; use cotton_store::Driver; use serde_json::{json, Map, Value}; const T0: i64 = 1_760_300_000_000; const GROUP: &str = "group-family"; struct Ids(u32, &'static str); impl IdSource for Ids { fn next_op_id(&mut self, _now_ms: i64) -> cotton_store::Result { self.0 += 1; Ok(format!("{}-{:05}", self.1, self.0)) } } struct Dev { db: Driver, ids: Ids, fact: CottonOp, } fn device(tag: &'static str) -> Dev { let db = file_at_version(SCHEMA_VERSION); seed_meta( &db, &Meta { workspace_id: WORKSPACE.to_string(), owner_id: OWNER.to_string(), device_id: format!("device:{tag}"), last_server_seq: 0, last_hlc: None, }, SCHEMA_VERSION, ) .expect("seeds"); seed_workspace_row(&db); let mut ids = Ids(0, tag); let author = first_author(&db, &format!("key-{tag}"), Some(tag), AuthorReason::First, &Clock::wall(T0 - 10), &mut ids, Held::Served) .expect("the author"); Dev { db, ids, fact: author.fact } } impl Dev { fn act(&mut self, at_ms: i64, changes: Vec) -> Vec { let act = Act { actor: Actor::device_job(JobName::named("test").expect("a job name")), verb: "update_group".to_string(), history: History::None, changes, }; mint(&self.db, &act, &Clock::wall(at_ms), &mut self.ids, Held::Served).expect("mints").ops } fn take(&self, ops: &[CottonOp]) -> FoldResult { arrive(&self.db, ops, Arrival::Pulled).expect("folds") } /// The group's members, by record id. fn members(&self) -> Vec { self.db .all( "SELECT record_id FROM cotton_group_members WHERE group_id = ? ORDER BY record_id", &[rusqlite::types::Value::Text(GROUP.into())], ) .expect("reads") .iter() .filter_map(|row| row.text("record_id").map(str::to_owned)) .collect() } } fn obj(value: Value) -> Map { value.as_object().expect("an object").clone() } /// `record` put in the group at `place`: its own member row. fn member(record: &str, place: f64) -> Change { let id = group_member_id(GROUP, record); Change::create( "cotton_group_members", &id, obj(json!({ "id": id, "workspace_id": WORKSPACE, "group_id": GROUP, "record_id": record, "place": place, "created_at": NOW, "updated_at": NOW, })), ) } fn quiet(result: &FoldResult, what: &str) { assert!(result.rejected.is_empty(), "{what}: refused {:?}", result.rejected); assert!(result.waiting.is_empty(), "{what}: waiting {:?}", result.waiting); } /// RULE: Two devices that each add a different record to one group while apart both keep both once each folds the other's write — a member is its own row, never a key of the group's row that one write could overwrite (137: groups leave cotton_records). #[test] fn two_devices_adding_different_members_to_one_group_both_keep_both() { let mut mac = device("mac"); let mut phone = device("phone"); quiet(&phone.take(std::slice::from_ref(&mac.fact)), "the Mac's author"); quiet(&mac.take(std::slice::from_ref(&phone.fact)), "the phone's author"); // The group, made on the Mac and held by both. let made = mac.act( T0 + 1_000, vec![Change::create( "cotton_groups", GROUP, obj(json!({ "id": GROUP, "workspace_id": WORKSPACE, "table_id": "person", "name": "Family", "record_byline": "user", "created_at": NOW, "updated_at": NOW, })), )], ); quiet(&phone.take(&made), "the group"); // Apart: the Mac adds Asha, the phone adds Ben, each at the next place — // the same place, since neither has seen the other's. let on_mac = mac.act(T0 + 2_000, vec![member("person-asha", 1.0)]); let on_phone = phone.act(T0 + 2_100, vec![member("person-ben", 1.0)]); assert_eq!(mac.members(), ["person-asha"]); assert_eq!(phone.members(), ["person-ben"]); // Each folds the other's write. quiet(&mac.take(&on_phone), "the phone's add on the Mac"); quiet(&phone.take(&on_mac), "the Mac's add on the phone"); let both = ["person-asha", "person-ben"]; assert_eq!(mac.members(), both, "the Mac keeps both members"); assert_eq!(phone.members(), both, "the phone keeps both members"); }