mod common;
use common::{DRIFT, counts_in, pair, send, text, texts};
use nibkeep_base::{EntityId, SpaceId, StatementId};
use nibkeep_crypto::RecoveryCode;
use nibkeep_crypto::{DecryptionKey, EncryptionKey, SigningKey};
use nibkeep_governance::{
Anchor, GroupRef, Joining, RecoveryListing, RecoveryStatement, list_by_recovery_code,
replica_record, state_recovery_code,
};
use nibkeep_log::{ChangeId, Log, LogError, Parcel, SpaceKeys};
use nibkeep_model::storage::Infallibly;
use nibkeep_model::{FieldName, Operation, Statement, Value, fields};
use nibkeep_testkit::{TestRng, millis_at};
use nibkeep_time::Clock;
const CAPTION: &str = "org.example.caption";
const ADMIN: &str = "admin";
const EDITOR: &str = "editor";
const VIEWER: &str = "viewer";
const NONE: &str = "none";
const MOTHER: usize = 0;
const SON: usize = 1;
const GRANDMA: usize = 2;
const FATHER: usize = 3;
const COUSIN: usize = 4;
const PEOPLE: usize = 5;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Which {
Space,
Group,
}
struct Device {
key: SigningKey,
encryption: EncryptionKey,
person: usize,
space: Log,
group: Log,
}
impl Device {
fn log(&self, which: Which) -> &Log {
match which {
Which::Space => &self.space,
Which::Group => &self.group,
}
}
fn log_mut(&mut self, which: Which) -> &mut Log {
match which {
Which::Space => &mut self.space,
Which::Group => &mut self.group,
}
}
}
struct World {
id: SpaceId,
keys: SpaceKeys,
root: Option<ChangeId>,
members: Vec<EntityId>,
}
struct Families {
rng: TestRng,
now: u64,
space: World,
group: World,
devices: Vec<Device>,
photo: EntityId,
agreements: EntityId,
addition: Option<(EntityId, StatementId)>,
}
impl Families {
fn new(seed: &str, people: &[usize]) -> Self {
assert_eq!(people[0], MOTHER);
let mut rng = TestRng::new(seed);
let world = |rng: &mut TestRng| World {
id: rng.space_id(),
keys: SpaceKeys::new(0, rng.key()),
root: None,
members: (0..PEOPLE).map(|_| rng.entity_id()).collect(),
};
let space = world(&mut rng);
let group = world(&mut rng);
let devices = people
.iter()
.map(|&person| {
let mut space_log = Log::new(space.id, Clock::new(DRIFT));
space_log.expect_governed();
let mut group_log = Log::new(group.id, Clock::new(DRIFT));
group_log.expect_governed();
Device {
key: rng.signing_key(),
encryption: DecryptionKey::generate(&mut rng).encryption_key(),
person,
space: space_log,
group: group_log,
}
})
.collect();
let photo = rng.entity_id();
let agreements = rng.entity_id();
let mut families = Self {
rng,
now: millis_at("2026-11-07", 9, 0),
space,
group,
devices,
photo,
agreements,
addition: None,
};
for which in [Which::Space, Which::Group] {
let mut founding = families.making_member(which, MOTHER, ADMIN);
founding.extend(families.listing(which, 0));
let parcel = families.commit(which, 0, founding);
families.world_mut(which).root = Some(parcel.id());
}
families
}
fn world(&self, which: Which) -> &World {
match which {
Which::Space => &self.space,
Which::Group => &self.group,
}
}
fn world_mut(&mut self, which: Which) -> &mut World {
match which {
Which::Space => &mut self.space,
Which::Group => &mut self.group,
}
}
fn tick(&mut self) -> u64 {
self.now += 1_000;
self.now
}
fn assert(&mut self, entity: EntityId, field: FieldName, value: Value) -> Operation {
common::assert(&mut self.rng, entity, field, value)
}
fn making_member(&mut self, which: Which, person: usize, role: &str) -> Vec<Operation> {
let record = self.world(which).members[person];
common::making_member(&mut self.rng, record, role)
}
fn signed_listing(&mut self, which: Which, device: usize, ai_agent: bool) -> Vec<Operation> {
let (space, members) = (self.world(which).id, &self.world(which).members);
let device = &self.devices[device];
let member = (members[device.person], ai_agent);
common::listing(
&mut self.rng,
space,
(&device.key, &device.encryption),
member,
)
}
fn listing(&mut self, which: Which, device: usize) -> Vec<Operation> {
self.signed_listing(which, device, false)
}
fn commit(&mut self, which: Which, device: usize, operations: Vec<Operation>) -> Parcel {
let wall = self.tick();
let keys = self.world(which).keys.clone();
let device = &mut self.devices[device];
let key = device.key.clone();
device
.log_mut(which)
.commit(&key, &keys, operations, wall, &mut self.rng)
.expect("commits")
}
fn invite(&mut self, which: Which, by: usize, device: usize, role: &str) -> Parcel {
let mut operations = self.making_member(which, self.devices[device].person, role);
operations.extend(self.listing(which, device));
let parcel = self.commit(which, by, operations);
self.sync(which, by, device);
parcel
}
fn list(&mut self, which: Which, by: usize, device: usize) -> Parcel {
let operations = self.listing(which, device);
self.commit(which, by, operations)
}
fn give_role(&mut self, which: Which, by: usize, person: usize, role: &str) -> Parcel {
let record = self.world(which).members[person];
self.give_role_to(which, by, record, role)
}
fn give_role_to(&mut self, which: Which, by: usize, record: EntityId, role: &str) -> Parcel {
let operation = self.assert(record, fields::ROLE, Value::Text(role.to_owned()));
self.commit(which, by, vec![operation])
}
fn revoke(&mut self, which: Which, by: usize, device: usize) -> Parcel {
let key = self.devices[device].key.verifying_key();
let record = replica_record(self.world(which).id, &key);
let operation = self.assert(record, fields::REVOKED, Value::Text(key.to_text()));
self.commit(which, by, vec![operation])
}
fn add_group(&mut self, by: usize, role: &str) -> Parcel {
let root = self.group.root.expect("founded");
self.add_group_rooted(by, role, root)
}
fn add_group_rooted(&mut self, by: usize, role: &str, root: ChangeId) -> Parcel {
let record = self.rng.entity_id();
let group = GroupRef {
group: self.group.id,
root,
key: nibkeep_crypto::key_hash(self.group.keys.space_keys().next().expect("a key").1),
};
let addition = self.rng.statement_id();
let operations = vec![
Operation::assert(Statement::new(
addition,
record,
fields::GROUP,
Value::Text(group.to_text()),
)),
self.assert(record, fields::ROLE, Value::Text(role.to_owned())),
];
self.addition = Some((record, addition));
self.commit(Which::Space, by, operations)
}
fn group_record(&self) -> EntityId {
self.addition.expect("added").0
}
fn joining(&self) -> Joining {
Joining {
space: self.space.id,
root: self.space.root.expect("founded"),
addition: self.addition.expect("added").1,
}
}
fn agree_to(&mut self, by: usize, joining: Joining) -> Parcel {
let entity = self.agreements;
let operation = self.assert(entity, fields::JOINED, Value::Text(joining.to_text()));
self.commit(Which::Group, by, vec![operation])
}
fn agree(&mut self, by: usize) -> Parcel {
let joining = self.joining();
self.agree_to(by, joining)
}
fn withdraw(&mut self, by: usize) -> Parcel {
let (entity, joining) = (self.agreements, self.joining());
let operation = self.assert(entity, fields::WITHDRAWN, Value::Text(joining.to_text()));
self.commit(Which::Group, by, vec![operation])
}
fn group_heads(&self, of: usize) -> Vec<ChangeId> {
self.devices[of]
.group
.followed(&self.group.keys)
.expect("reads the group")
.heads()
}
fn anchor(&mut self, by: usize) -> Parcel {
self.follow(by);
let changes = self.group_heads(by);
self.anchor_naming(by, changes)
}
fn anchor_naming(&mut self, by: usize, changes: Vec<ChangeId>) -> Parcel {
let anchor = Anchor {
group: self.group.id,
changes,
};
let entity = self.group_record();
let operation = self.assert(entity, fields::ANCHOR, Value::Text(anchor.to_text()));
self.commit(Which::Space, by, vec![operation])
}
fn caption(&mut self, by: usize, caption: &str) -> Parcel {
let operation = Operation::assert(Statement::new(
self.rng.statement_id(),
self.photo,
FieldName::from_static(CAPTION),
text(caption),
));
self.commit(Which::Space, by, vec![operation])
}
fn purge(&mut self, by: usize, statement: StatementId) -> Parcel {
let (wall, keys) = (self.tick(), self.space.keys.clone());
let device = &mut self.devices[by];
let key = device.key.clone();
device
.space
.purge(&key, &keys, [statement], wall, &mut self.rng)
.expect("makes the purge")
}
fn name(&mut self, by: usize, entity: EntityId, name: &str) -> Parcel {
let operation = self.assert(entity, fields::NAME, text(name));
self.commit(Which::Space, by, vec![operation])
}
fn record_in_space(&self, device: usize) -> EntityId {
replica_record(self.space.id, &self.devices[device].key.verifying_key())
}
fn follow(&mut self, of: usize) {
let (group_keys, space_keys) = (self.group.keys.clone(), self.space.keys.clone());
let device = &mut self.devices[of];
let followed = device.group.followed(&group_keys).expect("reads the group");
device
.space
.follow(vec![followed], &space_keys)
.expect("follows the group");
}
fn sync(&mut self, which: Which, from: usize, to: usize) {
if which == Which::Space {
self.sync(Which::Group, from, to);
}
let wall = self.tick();
let keys = self.world(which).keys.clone();
let receiver = to;
let (from, to) = pair(&mut self.devices, from, to);
send(from.log(which), to.log_mut(which), &keys, wall);
if which == Which::Group {
self.follow(receiver);
}
}
fn sync_all(&mut self) {
let count = self.devices.len();
for _ in 0..2 {
for from in 0..count {
for to in 0..count {
if from != to {
self.sync(Which::Space, from, to);
}
}
}
}
}
fn counts(&self, change: &Parcel) -> bool {
counts_in(self.devices.iter().map(|device| &device.space), change)
}
fn captions(&self, of: usize) -> Vec<String> {
let mut captions = texts(&self.devices[of].space, self.photo, CAPTION);
captions.sort();
captions
}
}
fn household_in_family(seed: &str, people: &[usize], role: &str) -> Families {
let mut families = Families::new(seed, people);
families.invite(Which::Group, 0, 1, EDITOR);
if people.get(2) == Some(&GRANDMA) {
families.invite(Which::Space, 0, 2, VIEWER);
}
families.add_group(0, role);
families.agree(0);
families.anchor(0);
families.sync_all();
families
}
#[test]
fn a_group_counts_in_a_space_once_both_sides_agree_and_an_anchor_names_the_agreement() {
let mut families = Families::new("agreeing", &[MOTHER, SON, GRANDMA]);
families.invite(Which::Group, 0, 1, EDITOR);
families.invite(Which::Space, 0, 2, VIEWER);
let adding = families.add_group(0, EDITOR);
families.sync_all();
let early = families.caption(1, "before the agreement");
families.sync_all();
assert!(families.counts(&adding));
assert!(!families.counts(&early));
families.agree(0);
families.sync_all();
let unanchored = families.caption(1, "agreed, not anchored");
families.sync_all();
assert!(!families.counts(&unanchored));
let anchoring = families.anchor(0);
families.sync_all();
let late = families.caption(1, "after the anchor");
families.sync_all();
assert!(families.counts(&anchoring));
assert!(families.counts(&late));
assert!(!families.counts(&early) && !families.counts(&unanchored));
assert_eq!(families.captions(2), ["after the anchor"]);
}
#[test]
fn through_a_group_a_person_has_the_lower_role_and_over_every_path_the_highest() {
let mut families = Families::new("paths", &[MOTHER, SON, GRANDMA, MOTHER]);
families.list(Which::Group, 0, 3);
families.invite(Which::Group, 0, 1, EDITOR);
families.invite(Which::Space, 0, 2, VIEWER);
families.add_group(0, VIEWER);
families.agree(0);
families.anchor(0);
families.sync_all();
let caption = families.caption(1, "a viewer's");
let phone = families.record_in_space(1);
let naming = families.name(1, phone, "The son's phone");
families.sync_all();
assert!(!families.counts(&caption));
assert!(families.counts(&naming));
let promoting = families.give_role(Which::Space, 3, GRANDMA, EDITOR);
families.sync_all();
assert!(families.counts(&promoting));
let record = families.group_record();
families.give_role_to(Which::Space, 0, record, EDITOR);
families.sync_all();
let editing = families.caption(1, "an editor's");
families.sync_all();
assert!(families.counts(&editing));
families.give_role(Which::Group, 0, SON, VIEWER);
families.sync_all();
families.anchor(0);
families.sync_all();
let viewing = families.caption(1, "a viewer's again");
families.sync_all();
assert!(!families.counts(&viewing));
assert_eq!(families.captions(0), ["an editor's"]);
}
#[test]
fn a_group_is_never_an_admin_of_a_space() {
let mut families = household_in_family("never an admin", &[MOTHER, SON, GRANDMA], EDITOR);
let record = families.group_record();
let promoting = families.give_role_to(Which::Space, 0, record, ADMIN);
families.sync_all();
assert!(!families.counts(&promoting));
let caption = families.caption(1, "an editor's");
let giving = families.give_role(Which::Space, 1, GRANDMA, EDITOR);
families.sync_all();
assert!(families.counts(&caption));
assert!(!families.counts(&giving));
let mut families = Families::new("added as an admin", &[MOTHER, SON]);
families.invite(Which::Group, 0, 1, EDITOR);
let adding = families.add_group(0, ADMIN);
families.agree(0);
families.anchor(0);
families.sync_all();
let caption = families.caption(1, "never counts");
families.sync_all();
assert!(!families.counts(&adding));
assert!(!families.counts(&caption));
}
#[test]
fn an_agreement_counts_only_for_the_addition_and_the_first_change_it_names() {
let mut families = Families::new("agreements", &[MOTHER, SON]);
families.invite(Which::Group, 0, 1, EDITOR);
families.add_group(0, EDITOR);
let joining = families.joining();
let elsewhere = Joining {
addition: families.rng.statement_id(),
..joining
};
families.agree_to(0, elsewhere);
let another_space = Joining {
root: families.group.root.expect("founded"),
..joining
};
families.agree_to(0, another_space);
families.anchor(0);
families.sync_all();
let caption = families.caption(1, "not agreed to");
families.sync_all();
assert!(!families.counts(&caption));
families.agree(0);
families.anchor(0);
families.sync_all();
let caption = families.caption(1, "agreed to");
families.sync_all();
assert!(families.counts(&caption));
}
#[test]
fn only_a_spaces_admin_adds_a_group_and_only_the_groups_admin_agrees() {
let mut families = Families::new("admins of both", &[MOTHER, SON, GRANDMA]);
families.invite(Which::Group, 0, 1, EDITOR);
families.invite(Which::Space, 0, 2, VIEWER);
families.sync_all();
let by_grandma = families.add_group(2, EDITOR);
families.sync_all();
assert!(!families.counts(&by_grandma));
families.add_group(0, EDITOR);
families.sync_all();
let by_son = families.agree(1);
families.sync_all();
families.anchor(0);
families.sync_all();
let caption = families.caption(1, "not agreed to by an admin");
families.sync_all();
assert!(!families.counts(&caption));
families.agree(0);
families.anchor(0);
families.sync_all();
let caption = families.caption(1, "agreed to by an admin");
families.sync_all();
assert!(families.counts(&caption));
assert!(
families.devices[0]
.group
.is_set_aside(by_son.id())
.infallible()
);
}
#[test]
fn a_key_listed_through_a_group_counts_once_an_anchor_names_its_listing() {
let mut families = household_in_family("listings", &[MOTHER, SON, GRANDMA, SON], EDITOR);
families.list(Which::Group, 1, 3);
families.sync_all();
let early = families.caption(3, "before an anchor names the tablet");
families.sync_all();
assert!(!families.counts(&early));
families.anchor(2);
families.sync_all();
let late = families.caption(3, "once one does");
families.sync_all();
assert!(families.counts(&late));
assert!(!families.counts(&early));
}
#[test]
fn a_device_that_a_group_lists_by_a_code_counts_in_the_space_by_its_own_anchor() {
let mut families = household_in_family(
"by a code through a group",
&[MOTHER, SON, GRANDMA, SON],
EDITOR,
);
let code = RecoveryCode::generate(&mut families.rng);
let (group, son) = (families.group.id, families.group.members[SON]);
let stated = RecoveryStatement::sign(&code, son);
let naming = state_recovery_code(group, &stated, &mut families.rng);
families.commit(Which::Group, 1, vec![naming]);
families.sync_all();
let tablet = &families.devices[3];
let key = tablet.key.verifying_key();
let signed = RecoveryListing::sign(
&code,
(group, replica_record(group, &key), son),
&key,
&tablet.encryption,
);
let mut listing = families.listing(Which::Group, 3);
listing.push(list_by_recovery_code(&signed, &mut families.rng));
families.commit(Which::Group, 3, listing);
let anchor = families.anchor(3);
let caption = families.caption(3, "from the new tablet");
families.sync_all();
assert!(families.counts(&anchor));
assert!(families.counts(&caption));
}
#[test]
fn a_removal_from_a_group_takes_effect_in_a_space_at_its_first_anchors() {
let mut families = household_in_family("first anchors", &[MOTHER, SON, GRANDMA], EDITOR);
families.give_role(Which::Group, 0, SON, NONE);
let first = families.caption(1, "seen by the first anchor");
families.sync(Which::Space, 1, 2);
let second = families.caption(1, "not seen by it");
assert!(
families.counts(&second),
"a removal that no anchor names changes nothing yet"
);
families.sync(Which::Group, 0, 2);
let first_anchor = families.anchor(2);
families.sync_all();
assert!(families.counts(&first_anchor));
assert!(families.counts(&first), "the first anchor builds on it");
assert!(!families.counts(&second));
families.anchor(0);
families.sync_all();
assert!(!families.counts(&second));
let after = families.caption(1, "after");
families.sync_all();
assert!(!families.counts(&after));
assert_eq!(families.captions(0), ["seen by the first anchor"]);
}
#[test]
fn a_removed_members_own_anchor_keeps_nothing() {
let mut families = household_in_family("own anchors", &[MOTHER, SON, GRANDMA], EDITOR);
families.give_role(Which::Group, 0, SON, NONE);
families.sync(Which::Group, 0, 2);
families.anchor(2);
families.sync(Which::Group, 0, 1);
let caption = families.caption(1, "on purpose");
let own = families.anchor(1);
families.sync_all();
assert!(!families.counts(&own));
assert!(!families.counts(&caption));
}
#[test]
fn what_a_group_sets_aside_once_the_space_knows_why_gave_nothing() {
let mut families = Families::new("gave nothing", &[MOTHER, SON, GRANDMA, FATHER, COUSIN]);
families.invite(Which::Group, 0, 3, ADMIN);
families.invite(Which::Space, 0, 2, VIEWER);
families.add_group(0, EDITOR);
families.agree(0);
families.anchor(0);
families.sync_all();
let adding_cousin = families.invite(Which::Group, 3, 4, EDITOR);
let removing_father = families.give_role(Which::Group, 0, FATHER, NONE);
families.sync(Which::Group, 3, 2);
families.anchor(2);
families.sync(Which::Space, 2, 4);
let caption = families.caption(4, "the cousin's");
families.sync(Which::Space, 4, 2);
assert!(families.counts(&caption), "while the space knows no reason");
families.sync(Which::Group, 0, 2);
families.anchor(2);
families.sync_all();
assert!(!families.counts(&caption));
for device in &families.devices {
assert!(device.group.is_set_aside(adding_cousin.id()).infallible());
assert!(!device.group.is_set_aside(removing_father.id()).infallible());
}
}
#[test]
fn a_removal_that_a_group_sets_aside_took_nothing() {
let mut families = Families::new("took nothing", &[MOTHER, SON, GRANDMA, FATHER]);
families.invite(Which::Group, 0, 3, ADMIN);
families.invite(Which::Group, 0, 1, EDITOR);
families.invite(Which::Space, 0, 2, VIEWER);
families.add_group(0, EDITOR);
families.agree(0);
families.anchor(0);
families.sync_all();
families.give_role(Which::Group, 3, SON, NONE);
families.give_role(Which::Group, 0, FATHER, NONE);
let caption = families.caption(1, "the son's");
families.sync(Which::Group, 3, 2);
families.anchor(2);
families.sync(Which::Space, 1, 2);
families.sync(Which::Space, 2, 1);
assert!(!families.counts(&caption));
families.sync(Which::Group, 0, 2);
families.anchor(2);
families.sync_all();
assert!(families.counts(&caption));
}
#[test]
fn withdrawing_an_agreement_takes_a_group_out_of_a_space_for_good() {
let mut families = household_in_family("withdrawals", &[MOTHER, SON], EDITOR);
let before = families.caption(1, "before the withdrawal");
families.sync_all();
assert!(families.counts(&before));
families.withdraw(0);
families.anchor(0);
families.sync_all();
let withdrawn = families.caption(1, "withdrawn");
families.sync_all();
assert!(!families.counts(&withdrawn));
assert!(
families.counts(&before),
"the withdrawal's first anchor builds on it"
);
families.agree(0);
families.anchor(0);
families.sync_all();
let again = families.caption(1, "agreed again");
families.sync_all();
assert!(!families.counts(&again));
families.add_group(0, EDITOR);
families.agree(0);
families.anchor(0);
families.sync_all();
let added = families.caption(1, "added anew");
families.sync_all();
assert!(families.counts(&added));
assert!(families.counts(&before));
}
#[test]
fn a_key_revoked_in_a_group_is_ended_in_the_space_on_every_path() {
let mut families = Families::new("revoked keys", &[MOTHER, SON, MOTHER]);
families.list(Which::Space, 0, 2);
families.list(Which::Group, 0, 2);
families.invite(Which::Group, 0, 1, EDITOR);
families.add_group(0, EDITOR);
families.agree(0);
families.anchor(0);
families.sync_all();
let before = families.caption(2, "before");
families.sync_all();
assert!(families.counts(&before));
families.revoke(Which::Group, 0, 2);
let unknowing = families.caption(2, "not knowing");
families.anchor(0);
families.sync_all();
assert!(!families.counts(&unknowing));
let knowing = families.caption(2, "knowing");
families.sync_all();
assert!(!families.counts(&knowing));
assert!(families.counts(&before));
}
#[test]
fn an_ai_agents_key_counts_only_where_it_is_listed_itself() {
let mut families = household_in_family("AI agents", &[MOTHER, SON, SON], EDITOR);
for which in [Which::Space, Which::Group] {
let entity = families.agreements;
let allowing = families.assert(entity, fields::AI_AGENTS, Value::Boolean(true));
families.commit(which, 0, vec![allowing]);
}
families.sync_all();
let operations = families.signed_listing(Which::Group, 2, true);
let listed = families.commit(Which::Group, 1, operations);
families.sync_all();
families.anchor(0);
families.sync_all();
assert!(
!families.devices[0]
.group
.is_set_aside(listed.id())
.infallible()
);
let caption = families.caption(2, "an AI agent's");
families.sync_all();
assert!(!families.counts(&caption));
}
#[test]
fn a_spaces_admin_takes_a_group_out_with_the_role_none() {
let mut families = household_in_family("taken out", &[MOTHER, SON], EDITOR);
let before = families.caption(1, "before");
families.sync_all();
let record = families.group_record();
families.give_role_to(Which::Space, 0, record, NONE);
let unknowing = families.caption(1, "not knowing");
families.sync_all();
assert!(!families.counts(&unknowing));
let knowing = families.caption(1, "knowing");
families.sync_all();
assert!(!families.counts(&knowing));
assert!(
families.counts(&before),
"taking the group out builds on it"
);
}
#[test]
fn the_last_direct_admin_does_not_leave_while_a_group_remains() {
let mut families = household_in_family("the last admin", &[MOTHER, SON], EDITOR);
let leaving = families.give_role(Which::Space, 0, MOTHER, NONE);
families.sync_all();
assert!(!families.counts(&leaving));
}
#[test]
fn every_device_decides_alike_whatever_order_the_logs_arrive_in() {
let mut families = household_in_family("any order", &[MOTHER, SON, GRANDMA, GRANDMA], EDITOR);
families.list(Which::Space, 2, 3);
families.give_role(Which::Group, 0, SON, NONE);
let first = families.caption(1, "kept");
families.sync(Which::Space, 1, 2);
let second = families.caption(1, "set aside");
families.sync(Which::Group, 0, 2);
families.anchor(2);
for device in 0..3 {
for other in 0..3 {
if device != other {
families.sync(Which::Space, device, other);
}
}
}
let wall = families.tick();
let keys = families.space.keys.clone();
let parcels = families.devices[0].space.parcels().infallible();
for parcel in parcels.into_iter().rev() {
match families.devices[3].space.receive(parcel, &keys, wall) {
Ok(_) | Err(LogError::UnknownAuthor) => {}
Err(error) => panic!("takes the change: {error}"),
}
}
families.sync_all();
assert!(families.counts(&first));
assert!(!families.counts(&second));
assert_eq!(families.captions(3), families.captions(0));
}
#[test]
fn every_device_decides_alike_after_random_changes_and_syncs() {
for seed in 0..6 {
let mut families = Families::new(
&format!("random {seed}"),
&[MOTHER, SON, GRANDMA, FATHER, SON],
);
families.invite(Which::Group, 0, 3, ADMIN);
families.invite(Which::Group, 0, 1, EDITOR);
families.list(Which::Group, 1, 4);
families.invite(Which::Space, 0, 2, VIEWER);
families.add_group(0, EDITOR);
families.agree(0);
families.anchor(0);
families.sync_all();
let mut captions = Vec::new();
for step in 0..24 {
let pick = |families: &mut Families, count: usize| {
usize::from(families.rng.space_id().as_bytes()[0]) % count
};
let device = pick(&mut families, 5);
match pick(&mut families, 7) {
0 | 1 => captions.push(families.caption(device, &format!("step {step}"))),
6 => {
let kept = [1, 3][pick(&mut families, 2)];
let keeps = pick(&mut families, 2) == 0;
families.keep_out(0, kept, keeps);
}
2 => {
let by = [0, 3][pick(&mut families, 2)];
let person = [SON, FATHER][pick(&mut families, 2)];
let role = [NONE, VIEWER, EDITOR][pick(&mut families, 3)];
families.give_role(Which::Group, by, person, role);
}
3 => {
families.anchor(device);
}
_ => {
let other = (device + 1 + pick(&mut families, 4)) % 5;
let which = [Which::Space, Which::Group][pick(&mut families, 2)];
families.sync(which, device, other);
}
}
}
families.sync_all();
for caption in &captions {
families.counts(caption);
}
let shown = families.captions(0);
for device in 1..5 {
assert_eq!(families.captions(device), shown, "seed {seed}");
}
}
}
#[test]
fn a_revocation_in_a_space_that_follows_a_group_counts() {
let mut families = household_in_family(
"revoked in the space",
&[MOTHER, SON, GRANDMA, MOTHER],
EDITOR,
);
families.list(Which::Space, 0, 3);
families.sync_all();
let revoking = families.revoke(Which::Space, 0, 3);
families.sync_all();
let laptops = families.caption(3, "the laptop's");
let sons = families.caption(1, "the son's");
let promoting = families.give_role(Which::Space, 0, GRANDMA, EDITOR);
families.sync_all();
assert!(families.counts(&revoking));
assert!(!families.counts(&laptops));
assert!(families.counts(&sons));
assert!(families.counts(&promoting));
}
#[test]
fn a_record_that_names_another_first_change_brings_no_group_in() {
let mut families = Families::new("another first change", &[MOTHER, SON]);
families.invite(Which::Group, 0, 1, EDITOR);
let other = families.space.root.expect("founded");
families.add_group_rooted(0, EDITOR, other);
families.agree(0);
families.anchor(0);
families.sync_all();
let caption = families.caption(1, "through another log");
families.sync_all();
assert!(!families.counts(&caption));
}
#[test]
fn a_member_through_a_group_purges_only_what_they_signed() {
let mut families = household_in_family("purges", &[MOTHER, SON], EDITOR);
let sons = families.caption(1, "the son's");
let mothers = families.caption(0, "the mother's");
families.sync_all();
let statement = |parcel: &Parcel, families: &Families| {
let payload = families.devices[0]
.space
.payload(parcel.id(), &families.space.keys)
.expect("reads")
.expect("holds it");
let Operation::Assert { statement, .. } = &payload.operations[0] else {
unreachable!("an assertion");
};
statement.id
};
let (own, others) = (statement(&sons, &families), statement(&mothers, &families));
let purging_own = families.purge(1, own);
let purging_others = families.purge(1, others);
families.sync_all();
assert!(families.counts(&purging_own));
assert!(!families.counts(&purging_others));
assert_eq!(families.captions(0), ["the mother's"]);
}
impl Families {
fn make_for(&mut self, by: usize, device: usize, role: &str) -> (EntityId, Parcel) {
let record = self.rng.entity_id();
let user = self.rng.entity_id().to_string();
let key = self.devices[device].key.verifying_key().to_text();
let operations = vec![
self.assert(record, fields::USER_ID, Value::Text(user)),
self.assert(record, fields::ROLE, Value::Text(role.to_owned())),
self.assert(record, fields::MADE_FOR, Value::Text(key)),
];
let parcel = self.commit(Which::Space, by, operations);
(record, parcel)
}
fn list_on(&mut self, device: usize, record: EntityId) -> Parcel {
let key = (&self.devices[device].key, &self.devices[device].encryption);
let operations = common::listing(&mut self.rng, self.space.id, key, (record, false));
self.commit(Which::Space, device, operations)
}
fn keep_out(&mut self, by: usize, device: usize, keeps: bool) -> Parcel {
let record = self.record_in_space(device);
let operation = self.assert(record, fields::KEPT_OUT, Value::Boolean(keeps));
self.commit(Which::Space, by, vec![operation])
}
fn is_removed(&self, device: usize) -> bool {
self.devices[0]
.space
.is_removed(&self.devices[device].key.verifying_key(), &self.space.keys)
.expect("reads the space")
}
}
#[test]
fn a_member_through_a_group_made_direct_lists_their_replicas_and_takes_the_role() {
let mut families = household_in_family("made direct", &[MOTHER, SON, GRANDMA, SON], EDITOR);
families.list(Which::Group, 1, 3);
families.sync_all();
families.anchor(0);
families.sync_all();
let early = families.give_role(Which::Space, 1, GRANDMA, EDITOR);
families.sync_all();
assert!(!families.counts(&early));
let (_, by_grandma) = families.make_for(2, 1, ADMIN);
let (record, making) = families.make_for(0, 1, ADMIN);
families.sync_all();
assert!(!families.counts(&by_grandma));
assert!(families.counts(&making));
let grandmas = families.list_on(2, record);
let phones = families.list_on(1, record);
families.sync_all();
let tablets = families.list_on(3, record);
families.sync_all();
assert!(!families.counts(&grandmas));
assert!(families.counts(&phones));
assert!(families.counts(&tablets));
let promoting = families.give_role(Which::Space, 3, GRANDMA, EDITOR);
families.sync_all();
assert!(families.counts(&promoting));
}
#[test]
fn a_member_kept_out_of_a_space_stays_in_the_group_until_an_admin_lets_them_back() {
let mut families = household_in_family("kept out", &[MOTHER, SON, GRANDMA, SON], EDITOR);
families.list(Which::Group, 1, 3);
families.sync_all();
families.anchor(0);
families.sync_all();
let before = families.caption(1, "before");
families.sync_all();
let by_grandma = families.keep_out(2, 1, true);
let keeping = families.keep_out(0, 1, true);
let unknowing = families.caption(3, "not knowing");
families.sync_all();
assert!(!families.counts(&by_grandma));
assert!(families.counts(&keeping));
assert!(families.counts(&before));
assert!(!families.counts(&unknowing));
let knowing = families.caption(3, "knowing");
families.sync_all();
assert!(!families.counts(&knowing));
assert!(families.is_removed(1) && families.is_removed(3));
let removals = families.devices[0]
.space
.removals(&families.space.keys)
.expect("reads");
assert!(removals.contains(&keeping.id()));
let roles = families.devices[0]
.group
.member_roles(&families.group.keys)
.expect("reads");
assert_eq!(roles[&families.group.members[SON]].as_text(), EDITOR);
let by_himself = families.keep_out(1, 1, false);
families.sync_all();
assert!(!families.counts(&by_himself));
families.keep_out(0, 1, false);
families.sync_all();
let back = families.caption(3, "back");
families.sync_all();
assert!(families.counts(&back));
assert!(!families.is_removed(1));
}
#[test]
fn a_member_through_a_group_leaves_one_space_and_stays_in_the_group() {
let mut families = household_in_family("leaving one space", &[MOTHER, SON], EDITOR);
let leaving = families.keep_out(1, 1, true);
families.sync_all();
let after = families.caption(1, "after leaving");
families.sync_all();
assert!(families.counts(&leaving));
assert!(!families.counts(&after));
assert!(families.is_removed(1));
}
#[test]
fn an_admin_appointed_by_a_member_made_direct_is_junior_to_the_admins_before() {
let mut families = household_in_family("appointed after", &[MOTHER, SON, GRANDMA], EDITOR);
let (record, _) = families.make_for(0, 1, ADMIN);
families.sync_all();
families.list_on(1, record);
families.sync_all();
families.give_role(Which::Space, 1, GRANDMA, ADMIN);
families.sync_all();
let by_mother = families.give_role(Which::Space, 0, GRANDMA, EDITOR);
let by_grandma = families.give_role(Which::Space, 2, MOTHER, EDITOR);
families.sync_all();
assert!(families.counts(&by_mother));
assert!(!families.counts(&by_grandma));
}
#[test]
fn keeping_oneself_out_needs_no_new_key_while_one_stays_a_member() {
let mut families = household_in_family("no new key", &[MOTHER, SON, GRANDMA], EDITOR);
let pointless = families.keep_out(2, 2, true);
families.sync_all();
assert!(families.counts(&pointless));
assert!(!families.is_removed(2));
let removals = families.devices[0]
.space
.removals(&families.space.keys)
.expect("reads");
assert!(!removals.contains(&pointless.id()));
}