use super::*;
fn client(name: &str) -> Client {
Client::parse(name).expect("a usable identity")
}
#[test]
fn a_fresh_map_holds_nobody() {
let presence = Presence::default();
assert!(presence.live().is_empty());
assert!(!presence.is_live("phone"));
}
#[test]
fn a_connection_is_present_while_its_guard_lives() {
let presence = Presence::default();
{
let _live = presence.enter(&client("phone"), 18);
assert!(presence.is_live("phone"));
assert_eq!(presence.live(), BTreeSet::from(["phone".to_owned()]));
}
assert!(!presence.is_live("phone"), "the guard's drop released it");
assert!(presence.live().is_empty());
}
#[test]
fn a_second_connection_from_one_client_outlives_the_first() {
let presence = Presence::default();
let phone = client("phone");
let first = presence.enter(&phone, 18);
let second = presence.enter(&phone, 18);
drop(first);
assert!(presence.is_live("phone"), "one connection still stands");
drop(second);
assert!(!presence.is_live("phone"));
}
#[test]
fn a_cloned_handle_sees_the_same_connections() {
let presence = Presence::default();
let reader = presence.clone();
let _live = presence.enter(&client("laptop"), 18);
assert!(reader.is_live("laptop"));
assert_eq!(reader.live(), BTreeSet::from(["laptop".to_owned()]));
}
#[test]
fn identities_are_counted_apart() {
let presence = Presence::default();
let a = presence.enter(&client("phone"), 18);
let _b = presence.enter(&client("laptop"), 18);
assert_eq!(
presence.live(),
BTreeSet::from(["laptop".to_owned(), "phone".to_owned()])
);
drop(a);
assert_eq!(presence.live(), BTreeSet::from(["laptop".to_owned()]));
}
#[test]
fn a_client_with_no_connection_states_no_edition() {
assert!(Presence::default().editions("phone").is_empty());
}
#[test]
fn two_seats_of_one_client_state_their_own_editions() {
let presence = Presence::default();
let phone = client("phone");
let old = presence.enter(&phone, 18);
let _new = presence.enter(&phone, 21);
assert_eq!(presence.editions("phone"), vec![18, 21]);
drop(old);
assert_eq!(presence.editions("phone"), vec![21]);
assert!(presence.is_live("phone"));
}