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

70 lines
3.0 KiB
Rust

//! Day 2: household duplicates ("you both pay Spotify"), only from names members chose to share.
use fiscus_insight::household::{household_duplicates, Duplicate};
fn names(v: &[&str]) -> Vec<String> {
v.iter().map(|s| s.to_string()).collect()
}
#[test]
fn two_members_paying_the_same_service_is_a_duplicate() {
let shared = vec![
("mark".to_string(), names(&["PAYPAL *SPOTIFY", "Netflix.com", "O2 Czech"])),
("jana".to_string(), names(&["Spotify AB", "Netflix"])),
("kid".to_string(), names(&["Spotify"])),
];
let d = household_duplicates(&shared);
assert_eq!(
d,
vec![
Duplicate { key: "netflix".into(), members: vec!["jana".into(), "mark".into()] },
Duplicate { key: "spotify".into(), members: vec!["jana".into(), "kid".into(), "mark".into()] },
]
);
}
#[test]
fn what_is_shared_is_the_service_key_of_each_subscription_still_paid() {
use fiscus_insight::household::shared_names;
use fiscus_ledger::{Date, Decision, Event, EventBody, Haler, State, Timestamp};
let d = |s: &str| s.parse::<Date>().unwrap();
let mut log = Vec::new();
for (i, m) in [1u32, 2, 3, 4].iter().enumerate() {
for (j, merchant) in ["PAYPAL *SPOTIFY 4029357733", "NETFLIX.COM", "WEDOS INTERNET A.S.", "TV"].iter().enumerate() {
log.push(Event::new(
format!("p{i}{j}"),
Timestamp(0),
EventBody::PaymentAdded {
payment: format!("p{i}{j}"),
amount: Haler(-19_900),
date: d(&format!("2026-{:02}-0{}", m + 6, j + 1)),
merchant: merchant.to_string(),
category: None,
note: None,
},
));
}
}
let today = d("2026-10-20");
let state = State::replay(&log);
// Only the first word of the merchant key, so a reference number never leaves; "TV" is too short to say anything.
assert_eq!(shared_names(&state, today), names(&["netflix", "spotify", "wedos"]));
log.push(Event::new(
"cancel",
Timestamp(0),
EventBody::SubscriptionDecided { subscription: "netflix com".into(), decision: Decision::Cancel, amount: Haler(19_900), on: d("2026-10-05") },
));
let state = State::replay(&log);
let key = fiscus_insight::subscriptions::analyse(&state, today).subscriptions.iter().find(|s| s.merchant.starts_with("NETFLIX")).map(|s| s.key.clone());
assert_eq!(key.as_deref(), Some("netflix com"), "the decision above names Netflix's subscription key");
assert_eq!(shared_names(&state, today), names(&["spotify", "wedos"]), "a cancelled one is not paid any more");
}
#[test]
fn one_member_alone_or_nobody_sharing_is_nothing() {
assert!(household_duplicates(&[]).is_empty());
let one = vec![("mark".to_string(), names(&["Spotify", "spotify", "PAYPAL *SPOTIFY"]))];
assert!(household_duplicates(&one).is_empty(), "the same member twice is not a duplicate");
}