Files
FiscusPublic/crates/fiscus-ledger/tests/household.rs
T

87 lines
4.3 KiB
Rust

//! v1 close: the name a person shows to the members of their shared budgets, the opt-in to share
//! subscription names with a shared budget, and what a shared budget's log may hold of either.
use fiscus_ledger::{Event, EventBody, Haler, State, Timestamp};
fn ev(id: &str, body: EventBody) -> Event {
Event::new(id, Timestamp(0), body)
}
fn budgets() -> Vec<Event> {
vec![
ev("e1", EventBody::BudgetCreated { budget: "b-mine".into(), name: "Mine".into(), limit: Haler(1_000_000) }),
ev("e2", EventBody::BudgetShared { budget: "b-home".into(), name: "Home".into(), limit: Haler(2_000_000) }),
]
}
fn rejected(log: &[Event]) -> Vec<String> {
State::replay(log).rejected.iter().map(|r| r.reason.clone()).collect()
}
#[test]
fn the_profile_name_is_the_latest_trimmed_and_bounded() {
let mut log = budgets();
log.push(ev("n1", EventBody::ProfileNamed { name: " Mark ".into() }));
log.push(ev("n2", EventBody::ProfileNamed { name: "Marek".into() }));
assert_eq!(State::replay(&log).profile_name.as_deref(), Some("Marek"));
for bad in ["", " ", "a\u{7}b", &"x".repeat(41)] {
let mut log = budgets();
log.push(ev("n", EventBody::ProfileNamed { name: bad.into() }));
assert_eq!(rejected(&log).len(), 1, "{bad:?} is not a name");
assert_eq!(State::replay(&log).profile_name, None);
}
}
#[test]
fn subscription_sharing_is_an_opt_in_per_shared_budget() {
let mut log = budgets();
log.push(ev("h1", EventBody::HouseholdSharingSet { budget: "b-home".into(), on: true }));
assert!(State::replay(&log).household_sharing.contains("b-home"));
log.push(ev("h2", EventBody::HouseholdSharingSet { budget: "b-home".into(), on: false }));
assert!(State::replay(&log).household_sharing.is_empty(), "opting out takes it back");
let mut log = budgets();
log.push(ev("h1", EventBody::HouseholdSharingSet { budget: "b-mine".into(), on: true }));
log.push(ev("h2", EventBody::HouseholdSharingSet { budget: "b-none".into(), on: true }));
assert_eq!(rejected(&log).len(), 2, "never the default budget, never an unknown one");
assert!(State::replay(&log).household_sharing.is_empty());
}
#[test]
fn a_member_name_and_shared_subscription_names_need_the_budget_and_are_bounded() {
let group = vec![ev("g1", EventBody::BudgetCreated { budget: "b-home".into(), name: "Home".into(), limit: Haler(0) })];
let ok = |body: EventBody| {
let mut log = group.clone();
log.push(ev("x", body));
rejected(&log).is_empty()
};
let names = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::<Vec<_>>();
assert!(ok(EventBody::MemberNamed { budget: "b-home".into(), name: "Jana".into() }));
assert!(!ok(EventBody::MemberNamed { budget: "b-other".into(), name: "Jana".into() }));
assert!(!ok(EventBody::MemberNamed { budget: "b-home".into(), name: " ".into() }));
assert!(ok(EventBody::SubscriptionsShared { budget: "b-home".into(), names: names(&["spotify", "netflix"]) }));
assert!(ok(EventBody::SubscriptionsShared { budget: "b-home".into(), names: vec![] }), "an empty list withdraws them");
assert!(!ok(EventBody::SubscriptionsShared { budget: "b-other".into(), names: names(&["spotify"]) }));
assert!(!ok(EventBody::SubscriptionsShared { budget: "b-home".into(), names: names(&["spotify", "spotify"]) }));
assert!(!ok(EventBody::SubscriptionsShared { budget: "b-home".into(), names: names(&[""]) }));
assert!(!ok(EventBody::SubscriptionsShared { budget: "b-home".into(), names: vec!["x".repeat(41)] }));
assert!(!ok(EventBody::SubscriptionsShared { budget: "b-home".into(), names: (0..101).map(|i| format!("s{i}")).collect() }));
}
#[test]
fn the_new_events_round_trip_as_json_with_their_type_tags() {
for (body, tag) in [
(EventBody::ProfileNamed { name: "Mark".into() }, "profile.named"),
(EventBody::HouseholdSharingSet { budget: "b".into(), on: true }, "household.sharing_set"),
(EventBody::MemberNamed { budget: "b".into(), name: "Mark".into() }, "member.named"),
(EventBody::SubscriptionsShared { budget: "b".into(), names: vec!["spotify".into()] }, "subscriptions.shared"),
] {
let json = serde_json::to_value(&body).unwrap();
assert_eq!(json["type"], tag);
assert_eq!(serde_json::from_value::<EventBody>(json).unwrap(), body);
}
}