//! Category plans and categories of your own: a plan carries forward until a later one replaces it, zero //! removes it, and only a real spending category can have one. Every refusal changes nothing. use fiscus_ledger::categories::MAX_PLAN; use fiscus_ledger::{Date, Event, EventBody, Haler, State, Timestamp}; fn d(s: &str) -> Date { s.parse().unwrap() } fn plan(id: &str, category: &str, amount: i64, from: &str) -> Event { Event::new(id, Timestamp(0), EventBody::CategoryPlanned { category: category.into(), amount: Haler(amount), from: d(from) }) } fn created(id: &str, category: &str, label: &str) -> Event { Event::new(id, Timestamp(0), EventBody::CategoryCreated { category: category.into(), label: label.into() }) } #[test] fn a_plan_carries_forward_until_a_later_one_replaces_it() { let s = State::replay(&[plan("p1", "groceries", 800_000, "2026-09-01"), plan("p2", "groceries", 900_000, "2026-11-01"), plan("p3", "eating-out", 200_000, "2026-10-01")]); assert!(s.rejected.is_empty(), "{:?}", s.rejected); assert_eq!(s.plan_for("groceries", d("2026-08-31")), None, "not before its month"); assert_eq!(s.plan_for("groceries", d("2026-09-15")), Some(Haler(800_000))); assert_eq!(s.plan_for("groceries", d("2026-10-31")), Some(Haler(800_000)), "carried forward"); assert_eq!(s.plan_for("groceries", d("2026-11-01")), Some(Haler(900_000))); assert_eq!(s.plan_for("groceries", d("2027-06-10")), Some(Haler(900_000))); assert_eq!(s.plan_for("eating-out", d("2026-09-30")), None); assert_eq!(s.plan_for("transport", d("2026-11-01")), None); } #[test] fn the_latest_plan_for_a_month_wins_and_zero_removes_it() { // Written later for an earlier month: the plan from November still counts in November. let s = State::replay(&[plan("p1", "groceries", 900_000, "2026-11-01"), plan("p2", "groceries", 700_000, "2026-10-01")]); assert_eq!(s.plan_for("groceries", d("2026-11-20")), Some(Haler(900_000))); assert_eq!(s.plan_for("groceries", d("2026-10-20")), Some(Haler(700_000))); // Two for the same month: the later in the log. let s = State::replay(&[plan("p1", "groceries", 900_000, "2026-11-01"), plan("p2", "groceries", 500_000, "2026-11-01")]); assert_eq!(s.plan_for("groceries", d("2026-11-20")), Some(Haler(500_000))); // Zero from December: no plan from then on. let s = State::replay(&[plan("p1", "groceries", 900_000, "2026-11-01"), plan("p2", "groceries", 0, "2026-12-01")]); assert_eq!(s.plan_for("groceries", d("2026-11-30")), Some(Haler(900_000))); assert_eq!(s.plan_for("groceries", d("2026-12-01")), None); } #[test] fn only_a_spending_category_can_have_a_plan() { for (category, amount, from, why) in [ ("income", 100, "2026-11-01", "money in"), ("transfers", 100, "2026-11-01", "money moved"), ("uncategorised", 100, "2026-11-01", "not sorted"), ("fuel", 100, "2026-11-01", "an unknown category"), ("Groceries", 100, "2026-11-01", "not an id"), ("groceries", -1, "2026-11-01", "negative"), ("groceries", MAX_PLAN.0 + 1, "2026-11-01", "over 100 million"), ("groceries", 100, "2026-11-02", "not the 1st"), ] { let s = State::replay(&[plan("p", category, amount, from)]); assert_eq!(s.rejected.len(), 1, "{why}"); assert!(s.category_plans.is_empty(), "{why}: nothing kept"); } let s = State::replay(&[plan("p", "groceries", MAX_PLAN.0, "2026-11-01")]); assert!(s.rejected.is_empty(), "exactly 100 million is a plan"); } #[test] fn categories_of_your_own() { let s = State::replay(&[created("c1", "fuel", "Fuel"), plan("p1", "fuel", 300_000, "2026-11-01")]); assert!(s.rejected.is_empty(), "{:?}", s.rejected); assert_eq!(s.category_label("fuel"), "Fuel"); assert_eq!(s.category_label("groceries"), "Groceries"); assert_eq!(s.plan_for("fuel", d("2026-11-03")), Some(Haler(300_000))); let all = s.categories(); assert_eq!(all.last().unwrap(), &("fuel".to_string(), "Fuel".to_string()), "yours after the default ones"); assert_eq!(all[0].0, "groceries"); for (event, why) in [ (created("c2", "fuel", "Petrol"), "the same id twice"), (created("c2", "groceries", "Food"), "a default category"), (created("c2", "Fuel-2", "Fuel"), "not an id"), (created("c2", "parking", ""), "an empty name"), (created("c2", "parking", " Parking"), "a name with spaces around"), (created("c2", "parking", &"x".repeat(41)), "a long name"), (created("c2", "parking", "Park\ning"), "a control character"), ] { let mut t = s.clone(); t.apply(&event); assert_eq!(t.rejected.len(), 1, "{why}"); assert_eq!(t.custom_categories.len(), 1, "{why}: nothing kept"); } } #[test] fn the_events_round_trip_and_replay_the_same() { let log = vec![created("c1", "fuel", "Fuel"), plan("p1", "fuel", 300_000, "2026-11-01"), plan("p2", "groceries", 900_000, "2026-10-01")]; let json = serde_json::to_string(&log).unwrap(); for name in ["category.planned", "category.created"] { assert!(json.contains(name), "{name}"); } let back: Vec = serde_json::from_str(&json).unwrap(); assert_eq!(back, log); assert_eq!(State::replay(&back), State::replay(&log)); }