mod common;
use std::collections::BTreeMap;
use common::{DRIFT, counts_in, pair, send, text, texts};
use nibkeep_base::{EntityId, SpaceId, StatementId};
use nibkeep_crypto::{DecryptionKey, EncryptionKey, SigningKey};
use nibkeep_governance::{Anchor, GroupRef, Joining};
use nibkeep_log::{ChangeId, Log, Parcel, SpaceKeys};
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 EDITOR: &str = "editor";
const VIEWER: &str = "viewer";
const NONE: &str = "none";
const FAMILY: usize = 0;
const HOUSEHOLD: usize = 1;
const KIDS: usize = 2;
const LOGS: usize = 3;
const MOTHER: usize = 0;
const SON: usize = 1;
const GRANDMA: usize = 2;
const FATHER: usize = 3;
const PEOPLE: usize = 4;
struct Device {
key: SigningKey,
encryption: EncryptionKey,
person: usize,
logs: Vec<Log>,
}
struct World {
id: SpaceId,
keys: SpaceKeys,
root: Option<ChangeId>,
members: Vec<EntityId>,
entity: EntityId,
}
struct Nested {
rng: TestRng,
now: u64,
worlds: Vec<World>,
devices: Vec<Device>,
photo: EntityId,
additions: BTreeMap<(usize, usize), (EntityId, StatementId)>,
following: [Vec<usize>; LOGS],
}
impl Nested {
fn new(seed: &str, people: &[usize]) -> Self {
assert_eq!(people[0], MOTHER);
let mut rng = TestRng::new(seed);
let worlds: Vec<World> = (0..LOGS)
.map(|_| World {
id: rng.space_id(),
keys: SpaceKeys::new(0, rng.key()),
root: None,
members: (0..PEOPLE).map(|_| rng.entity_id()).collect(),
entity: rng.entity_id(),
})
.collect();
let devices = people
.iter()
.map(|&person| Device {
key: rng.signing_key(),
encryption: DecryptionKey::generate(&mut rng).encryption_key(),
person,
logs: worlds
.iter()
.map(|world| {
let mut log = Log::new(world.id, Clock::new(DRIFT));
log.expect_governed();
log
})
.collect(),
})
.collect();
let photo = rng.entity_id();
let mut nested = Self {
rng,
now: millis_at("2026-11-07", 9, 0),
worlds,
devices,
photo,
additions: BTreeMap::new(),
following: [vec![HOUSEHOLD, KIDS], vec![KIDS], Vec::new()],
};
for log in 0..LOGS {
let mut founding = nested.making_member(log, MOTHER, "admin");
founding.extend(nested.listing(log, 0, false));
let parcel = nested.commit(log, 0, founding);
nested.worlds[log].root = Some(parcel.id());
}
nested
}
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, log: usize, person: usize, role: &str) -> Vec<Operation> {
common::making_member(&mut self.rng, self.worlds[log].members[person], role)
}
fn listing(&mut self, log: usize, device: usize, ai_agent: bool) -> Vec<Operation> {
let (world, device) = (&self.worlds[log], &self.devices[device]);
let member = (world.members[device.person], ai_agent);
common::listing(
&mut self.rng,
world.id,
(&device.key, &device.encryption),
member,
)
}
fn commit(&mut self, log: usize, device: usize, operations: Vec<Operation>) -> Parcel {
let wall = self.tick();
let keys = self.worlds[log].keys.clone();
let device = &mut self.devices[device];
let key = device.key.clone();
device.logs[log]
.commit(&key, &keys, operations, wall, &mut self.rng)
.expect("commits")
}
fn invite(&mut self, log: usize, by: usize, device: usize, role: &str) -> Parcel {
let mut operations = self.making_member(log, self.devices[device].person, role);
operations.extend(self.listing(log, device, false));
let parcel = self.commit(log, by, operations);
self.sync(log, by, device);
parcel
}
fn give_role(&mut self, log: usize, by: usize, person: usize, role: &str) -> Parcel {
let record = self.worlds[log].members[person];
self.give_role_to(log, by, record, role)
}
fn give_role_to(&mut self, log: usize, by: usize, record: EntityId, role: &str) -> Parcel {
let operation = self.assert(record, fields::ROLE, Value::Text(role.to_owned()));
self.commit(log, by, vec![operation])
}
fn add_group(&mut self, log: usize, group: usize, by: usize, role: &str) -> Parcel {
let record = self.rng.entity_id();
let world = &self.worlds[group];
let reference = GroupRef {
group: world.id,
root: world.root.expect("founded"),
key: nibkeep_crypto::key_hash(world.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(reference.to_text()),
)),
self.assert(record, fields::ROLE, Value::Text(role.to_owned())),
];
self.additions.insert((log, group), (record, addition));
self.commit(log, by, operations)
}
fn record(&self, log: usize, group: usize) -> EntityId {
self.additions[&(log, group)].0
}
fn joining(&self, log: usize, group: usize) -> Joining {
Joining {
space: self.worlds[log].id,
root: self.worlds[log].root.expect("founded"),
addition: self.additions[&(log, group)].1,
}
}
fn agree(&mut self, group: usize, log: usize, by: usize) -> Parcel {
let (entity, joining) = (self.worlds[group].entity, self.joining(log, group));
let operation = self.assert(entity, fields::JOINED, Value::Text(joining.to_text()));
self.commit(group, by, vec![operation])
}
fn withdraw(&mut self, group: usize, log: usize, by: usize) -> Parcel {
let (entity, joining) = (self.worlds[group].entity, self.joining(log, group));
let operation = self.assert(entity, fields::WITHDRAWN, Value::Text(joining.to_text()));
self.commit(group, by, vec![operation])
}
fn anchor(&mut self, log: usize, group: usize, by: usize) -> Parcel {
self.follow(by);
let changes = self.devices[by].logs[group]
.followed(&self.worlds[group].keys)
.expect("reads the group")
.heads();
let anchor = Anchor {
group: self.worlds[group].id,
changes,
};
let entity = self.worlds[log].entity;
let operation = self.assert(entity, fields::ANCHOR, Value::Text(anchor.to_text()));
self.commit(log, 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(FAMILY, by, vec![operation])
}
fn follow(&mut self, of: usize) {
for log in 0..LOGS {
let keys = self.worlds[log].keys.clone();
let device = &self.devices[of];
let followed = self.following[log]
.iter()
.map(|&group| {
device.logs[group]
.followed(&self.worlds[group].keys)
.expect("reads the group")
})
.collect();
self.devices[of].logs[log]
.follow(followed, &keys)
.expect("follows the groups");
}
}
fn sync_one(&mut self, log: usize, from: usize, to: usize) {
let wall = self.tick();
let (sender, receiver) = pair(&mut self.devices, from, to);
send(
&sender.logs[log],
&mut receiver.logs[log],
&self.worlds[log].keys,
wall,
);
}
fn sync(&mut self, log: usize, from: usize, to: usize) {
for group in self.following[log].clone() {
self.sync_one(group, from, to);
}
self.follow(to);
self.sync_one(log, from, to);
self.follow(to);
}
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 {
for log in [KIDS, HOUSEHOLD, FAMILY] {
self.sync(log, from, to);
}
}
}
}
}
}
fn counts_in(&self, log: usize, change: &Parcel) -> bool {
counts_in(self.devices.iter().map(|device| &device.logs[log]), change)
}
fn counts(&self, change: &Parcel) -> bool {
self.counts_in(FAMILY, change)
}
fn captions(&self, of: usize) -> Vec<String> {
let mut captions = texts(&self.devices[of].logs[FAMILY], self.photo, CAPTION);
captions.sort();
captions
}
fn is_removed(&self, on: usize, device: usize) -> bool {
self.devices[on].logs[FAMILY]
.is_removed(
&self.devices[device].key.verifying_key(),
&self.worlds[FAMILY].keys,
)
.expect("reads the space")
}
fn removals(&self, on: usize) -> Vec<ChangeId> {
self.devices[on].logs[FAMILY]
.removals(&self.worlds[FAMILY].keys)
.expect("reads the space")
}
}
fn kids_in_household_in_family(
seed: &str,
people: &[usize],
kids_role: &str,
household_role: &str,
) -> Nested {
let mut nested = Nested::new(seed, people);
nested.invite(KIDS, 0, 1, EDITOR);
if let Some(grandma) = people.iter().position(|&person| person == GRANDMA) {
nested.invite(FAMILY, 0, grandma, VIEWER);
}
nested.add_group(FAMILY, HOUSEHOLD, 0, household_role);
nested.agree(HOUSEHOLD, FAMILY, 0);
nested.add_group(HOUSEHOLD, KIDS, 0, kids_role);
nested.agree(KIDS, HOUSEHOLD, 0);
nested.anchor(HOUSEHOLD, KIDS, 0);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.anchor(FAMILY, KIDS, 0);
nested.sync_all();
nested
}
#[test]
fn a_group_inside_a_group_gives_the_space_once_every_level_agrees_and_its_anchors_name_them() {
let mut nested = Nested::new("every level", &[MOTHER, SON, GRANDMA]);
nested.invite(KIDS, 0, 1, EDITOR);
nested.invite(FAMILY, 0, 2, VIEWER);
nested.add_group(FAMILY, HOUSEHOLD, 0, EDITOR);
nested.agree(HOUSEHOLD, FAMILY, 0);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.sync_all();
let alone = nested.caption(1, "the kids in nothing");
nested.add_group(HOUSEHOLD, KIDS, 0, EDITOR);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.sync_all();
let unagreed = nested.caption(1, "before the kids agree");
nested.agree(KIDS, HOUSEHOLD, 0);
nested.anchor(HOUSEHOLD, KIDS, 0);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.sync_all();
let unanchored = nested.caption(1, "before the family anchors the kids");
nested.sync_all();
let anchoring = nested.anchor(FAMILY, KIDS, 2);
nested.sync_all();
let late = nested.caption(1, "once the family's anchors name every level");
nested.sync_all();
assert!(nested.counts(&anchoring));
assert!(nested.counts(&late));
for early in [&alone, &unagreed, &unanchored] {
assert!(!nested.counts(early));
}
assert_eq!(
nested.captions(2),
["once the family's anchors name every level"]
);
let listed = nested.devices[2].logs[FAMILY]
.listed_through_groups(&nested.worlds[FAMILY].keys)
.expect("reads the space");
assert!(listed.contains(&nested.devices[1].key.verifying_key()));
}
#[test]
fn through_groups_inside_groups_a_person_has_the_lowest_role_on_the_path() {
let mut nested = kids_in_household_in_family("lowest role", &[MOTHER, SON], VIEWER, EDITOR);
let as_viewers = nested.caption(1, "the kids as viewers of the household");
nested.sync_all();
assert!(!nested.counts(&as_viewers));
let kids = nested.record(HOUSEHOLD, KIDS);
nested.give_role_to(HOUSEHOLD, 0, kids, EDITOR);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.sync_all();
let as_editors = nested.caption(1, "the kids as editors");
nested.sync_all();
assert!(nested.counts(&as_editors));
let as_admins = nested.give_role_to(HOUSEHOLD, 0, kids, "admin");
nested.sync_all();
assert!(!nested.counts_in(HOUSEHOLD, &as_admins));
let household = nested.record(FAMILY, HOUSEHOLD);
nested.give_role_to(FAMILY, 0, household, VIEWER);
nested.sync_all();
let household_viewing = nested.caption(1, "the household as viewers of the family");
nested.sync_all();
assert!(!nested.counts(&household_viewing));
}
#[test]
fn a_removal_from_a_group_inside_a_group_takes_effect_in_the_space_at_its_first_anchors() {
let mut nested = kids_in_household_in_family(
"removed from the kids",
&[MOTHER, SON, GRANDMA],
EDITOR,
EDITOR,
);
nested.give_role(KIDS, 0, SON, NONE);
let first = nested.caption(1, "seen by the first anchor");
nested.sync(FAMILY, 1, 2);
let second = nested.caption(1, "not seen by it");
assert!(
nested.counts(&second),
"a removal that no anchor names changes nothing yet"
);
assert!(!nested.is_removed(0, 1));
nested.sync(KIDS, 0, 2);
let first_anchor = nested.anchor(FAMILY, KIDS, 2);
nested.sync_all();
assert!(nested.counts(&first_anchor));
assert!(nested.counts(&first), "the first anchor builds on it");
assert!(!nested.counts(&second));
let after = nested.caption(1, "after");
nested.sync_all();
assert!(!nested.counts(&after));
assert!(nested.is_removed(0, 1));
assert!(nested.removals(0).contains(&first_anchor.id()));
assert_eq!(nested.captions(2), ["seen by the first anchor"]);
}
#[test]
fn taking_a_group_out_of_a_group_takes_it_out_of_the_space_at_its_first_anchors() {
let mut nested =
kids_in_household_in_family("kids taken out", &[MOTHER, SON, GRANDMA], EDITOR, EDITOR);
let kids = nested.record(HOUSEHOLD, KIDS);
nested.give_role_to(HOUSEHOLD, 0, kids, NONE);
let first = nested.caption(1, "seen by the first anchor");
nested.sync(FAMILY, 1, 2);
let second = nested.caption(1, "not seen by it");
nested.sync(HOUSEHOLD, 0, 2);
let first_anchor = nested.anchor(FAMILY, HOUSEHOLD, 2);
nested.sync_all();
assert!(nested.counts(&first_anchor));
assert!(nested.counts(&first));
assert!(!nested.counts(&second));
assert!(nested.is_removed(0, 1));
assert!(nested.removals(0).contains(&first_anchor.id()));
}
#[test]
fn a_group_that_withdraws_from_a_group_leaves_every_space_the_group_is_in_for_good() {
let mut nested = kids_in_household_in_family("kids withdraw", &[MOTHER, SON], EDITOR, EDITOR);
let before = nested.caption(1, "before the withdrawal");
nested.sync_all();
nested.withdraw(KIDS, HOUSEHOLD, 0);
let anchoring = nested.anchor(FAMILY, KIDS, 0);
nested.sync_all();
let after = nested.caption(1, "after the withdrawal");
nested.sync_all();
assert!(nested.counts(&before));
assert!(!nested.counts(&after));
assert!(nested.is_removed(0, 1));
assert!(nested.removals(0).contains(&anchoring.id()));
nested.agree(KIDS, HOUSEHOLD, 0);
nested.anchor(FAMILY, KIDS, 0);
nested.sync_all();
let again = nested.caption(1, "agreed again");
nested.sync_all();
assert!(!nested.counts(&again));
}
#[test]
fn groups_that_hold_each_other_give_nothing_through_the_cycle() {
let mut nested = kids_in_household_in_family("a cycle", &[MOTHER, SON, FATHER], EDITOR, EDITOR);
nested.invite(HOUSEHOLD, 0, 2, EDITOR);
nested.following = [vec![HOUSEHOLD, KIDS], vec![KIDS], vec![HOUSEHOLD]];
nested.add_group(KIDS, HOUSEHOLD, 0, EDITOR);
nested.agree(HOUSEHOLD, KIDS, 0);
nested.anchor(KIDS, HOUSEHOLD, 0);
nested.anchor(HOUSEHOLD, KIDS, 0);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.anchor(FAMILY, KIDS, 0);
nested.sync_all();
let sons = nested.caption(1, "the son's");
let fathers = nested.caption(2, "the father's");
nested.sync_all();
assert!(nested.counts(&sons));
assert!(nested.counts(&fathers));
let kids = nested.record(HOUSEHOLD, KIDS);
nested.give_role_to(HOUSEHOLD, 0, kids, NONE);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.sync_all();
let sons = nested.caption(1, "the son's, out of the household");
let fathers = nested.caption(2, "the father's, still in it");
nested.sync_all();
assert!(!nested.counts(&sons));
assert!(nested.counts(&fathers));
}
#[test]
fn every_device_decides_alike_after_random_changes_at_every_level_and_syncs() {
for seed in 0..6 {
let mut nested = kids_in_household_in_family(
&format!("random {seed}"),
&[MOTHER, SON, GRANDMA, FATHER],
EDITOR,
EDITOR,
);
nested.invite(HOUSEHOLD, 0, 3, EDITOR);
nested.sync_all();
let mut captions = Vec::new();
for step in 0..24 {
let pick = |nested: &mut Nested, count: usize| {
usize::from(nested.rng.space_id().as_bytes()[0]) % count
};
let device = pick(&mut nested, 4);
match pick(&mut nested, 7) {
0 | 1 => captions.push(nested.caption(device, &format!("step {step}"))),
2 => {
let role = [NONE, VIEWER, EDITOR][pick(&mut nested, 3)];
if pick(&mut nested, 2) == 0 {
nested.give_role(KIDS, 0, SON, role);
} else {
nested.give_role(HOUSEHOLD, 0, FATHER, role);
}
}
3 => {
let role = [NONE, VIEWER, EDITOR][pick(&mut nested, 3)];
let kids = nested.record(HOUSEHOLD, KIDS);
nested.give_role_to(HOUSEHOLD, 0, kids, role);
}
4 => {
let (log, group) = [(FAMILY, HOUSEHOLD), (FAMILY, KIDS), (HOUSEHOLD, KIDS)]
[pick(&mut nested, 3)];
nested.anchor(log, group, device);
}
_ => {
let other = (device + 1 + pick(&mut nested, 3)) % 4;
let log = [FAMILY, HOUSEHOLD, KIDS][pick(&mut nested, 3)];
nested.sync(log, device, other);
}
}
}
nested.sync_all();
for caption in &captions {
nested.counts(caption);
}
let shown = nested.captions(0);
for device in 1..4 {
assert_eq!(nested.captions(device), shown, "seed {seed}");
}
}
}
#[test]
fn a_group_inside_a_group_whose_record_has_no_role_there_gives_nothing() {
let mut nested = Nested::new("no role inside", &[MOTHER, SON, FATHER]);
nested.invite(KIDS, 0, 1, EDITOR);
nested.invite(KIDS, 0, 2, "admin");
nested.invite(HOUSEHOLD, 0, 2, EDITOR);
nested.add_group(FAMILY, HOUSEHOLD, 0, EDITOR);
nested.agree(HOUSEHOLD, FAMILY, 0);
nested.anchor(FAMILY, HOUSEHOLD, 0);
nested.sync_all();
let adding = nested.add_group(HOUSEHOLD, KIDS, 2, EDITOR);
nested.agree(KIDS, HOUSEHOLD, 2);
nested.anchor(HOUSEHOLD, KIDS, 2);
nested.anchor(FAMILY, HOUSEHOLD, 2);
nested.anchor(FAMILY, KIDS, 2);
nested.sync_all();
assert!(!nested.counts_in(HOUSEHOLD, &adding));
let caption = nested.caption(1, "through a record with no role");
nested.sync_all();
assert!(!nested.counts(&caption));
let listed = nested.devices[0].logs[FAMILY]
.listed_through_groups(&nested.worlds[FAMILY].keys)
.expect("reads the space");
assert!(!listed.contains(&nested.devices[1].key.verifying_key()));
assert!(listed.contains(&nested.devices[2].key.verifying_key()));
}