use std::collections::BTreeMap;use serde_json::json;usesuper::*;usecrate::seal::keys::DataKey;usecrate::seal::share::{key_text, sharing_public, sharing_secret};const LIST: &str = "list-weekend";/// A synthetic person: their sharing key's public half and secret, from their own DK.fn person(byte: u8) -> (String, [u8; 32]) {let dk = DataKey::from_bytes([byte; 32]);let db = format!("{byte:02}000000-0000-4000-8000-000000000000"); (key_text(&sharing_public(&dk, &db).unwrap()), *sharing_secret(&dk, &db).unwrap())}fn members(ids: &[(&str, &str)]) -> BTreeMap<String, String> { ids.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect()}/// RULE (R5): both people see the list and both edit it; each edit is sealed to the list's key, and the object's/// copy holds no word of it.#[test]fn both_members_see_and_edit_the_list() {let (a_pub, a) = person(1);let (b_pub, b) = person(2);let (mut list, _) = SharedList::found(LIST, &members(&[("a", &a_pub), ("b", &b_pub)])).unwrap(); list.put("a", &a, "i1", &json!({ "text": "Oat milk", "done": false })).unwrap();let seen = list.read("b", &b); assert_eq!(seen.items["i1"]["text"], "Oat milk"); list.put("b", &b, "i1", &json!({ "text": "Oat milk", "done": true })).unwrap(); list.put("b", &b, "i2", &json!({ "text": "Rye bread", "done": false })).unwrap();let seen = list.read("a", &a); assert_eq!((seen.items["i1"]["done"].clone(), seen.items["i2"]["text"].clone()), (json!(true), json!("Rye bread"))); assert!(seen.unopened.is_empty());let stored = serde_json::to_string(&list).unwrap();// Whole phrases: base64 has no space, so a sealed value cannot hold one by chance (a three-letter word turns up in it now and then). assert!(!stored.contains("Oat milk") && !stored.contains("Rye bread"), "the object holds no word of the list: {stored}"); assert!(list.items.values().all(|v| v.starts_with("g1:1:")));}/// RULE (R5): adding a member wraps the current key for them, and they see the list as it stands.#[test]fn an_added_member_sees_the_whole_list() {let (a_pub, a) = person(1);let (c_pub, c) = person(3);let (mut list, _) = SharedList::found(LIST, &members(&[("a", &a_pub)])).unwrap(); list.put("a", &a, "i1", &json!({ "text": "Tent pegs" })).unwrap(); assert_eq!(list.read("c", &c).unopened, ["i1"], "not yet a member"); list.add_member("a", &a, "c", &c_pub).unwrap(); assert_eq!(list.read("c", &c).items["i1"]["text"], "Tent pegs"); list.put("c", &c, "i2", &json!({ "text": "Matches" })).unwrap(); assert_eq!(list.read("a", &a).items.len(), 2);}/// RULE (R5): removing a member makes a new key; every item is sealed again under it, so nothing the object holds/// after opens for the one removed, and they can neither write nor add anyone.#[test]fn a_removed_member_opens_nothing_the_list_holds_after() {let (a_pub, a) = person(1);let (b_pub, b) = person(2);let (c_pub, _) = person(3);let (mut list, _) = SharedList::found(LIST, &members(&[("a", &a_pub), ("b", &b_pub)])).unwrap(); list.put("b", &b, "i1", &json!({ "text": "Sun cream" })).unwrap(); list.remove_member("a", &a, "b").unwrap(); assert_eq!(list.membership.epoch, 2); assert!(list.items.values().all(|v| v.starts_with("g1:2:")), "every item sealed again under the new key"); list.put("a", &a, "i2", &json!({ "text": "Hat" })).unwrap();let theirs = list.read("b", &b); assert!(theirs.items.is_empty(), "the removed member opens nothing: {theirs:?}"); assert_eq!(theirs.unopened.len(), 2); assert_eq!(list.read("a", &a).items.len(), 2, "the one who stays reads it all"); assert!(list.put("b", &b, "i3", &json!({ "text": "Sneaked" })).is_err(), "a removed member writes nothing"); assert!(list.add_member("b", &b, "c", &c_pub).is_err(), "nor adds anyone"); assert!(!list.items.contains_key("i3"));}/// RULE (R5): someone who was never a member writes nothing and opens nothing; a wrap or an item moved onto/// another list opens nothing.#[test]fn a_stranger_and_a_moved_item_open_nothing() {let (a_pub, a) = person(1);let (_, z) = person(9);let (mut list, _) = SharedList::found(LIST, &members(&[("a", &a_pub)])).unwrap(); list.put("a", &a, "i1", &json!({ "text": "Map" })).unwrap(); assert!(list.put("z", &z, "i2", &json!({ "text": "x" })).is_err()); assert_eq!(list.read("z", &z).unopened, ["i1"]);let (mut other, _) = SharedList::found("list-other", &members(&[("a", &a_pub)])).unwrap(); other.items.insert("i1".into(), list.items["i1"].clone()); assert_eq!(other.read("a", &a).unopened, ["i1"], "an item is bound to its list");// And to its id: moved onto another item it opens nothing.let moved = list.items["i1"].clone(); list.items.insert("i9".into(), moved); assert_eq!(list.read("a", &a).unopened, ["i9"]);}