111 lines
4.3 KiB
Rust
111 lines
4.3 KiB
Rust
//! Day 2: what a shared budget's log holds. `payment.shared` carries the effective payment and nothing of
|
|
//! its origin's details; the budget's members see it and can split it; unsharing takes it out again.
|
|
|
|
use fiscus_ledger::share::shared_payment;
|
|
use fiscus_ledger::split::Share;
|
|
use fiscus_ledger::{Date, Event, EventBody, Glyph, Haler, State, Timestamp};
|
|
|
|
fn d(s: &str) -> Date {
|
|
s.parse().unwrap()
|
|
}
|
|
|
|
fn ev(id: &str, body: EventBody) -> Event {
|
|
Event::new(id, Timestamp(0), body)
|
|
}
|
|
|
|
fn personal() -> Vec<Event> {
|
|
vec![
|
|
ev("e1", EventBody::BudgetCreated { budget: "b-home".into(), name: "Home".into(), limit: Haler(1_000_000) }),
|
|
ev("e2", EventBody::BudgetCreated { budget: "b-trip".into(), name: "Trip".into(), limit: Haler(500_000) }),
|
|
ev(
|
|
"e3",
|
|
EventBody::PaymentCaptured {
|
|
payment: "p1".into(),
|
|
amount: Haler(-45_000),
|
|
date: d("2026-11-02"),
|
|
merchant: "Pension Lipno".into(),
|
|
card: Some("Visa 1234".into()),
|
|
},
|
|
),
|
|
ev("e4", EventBody::PaymentCategorised { payment: "p1".into(), category: "travel".into() }),
|
|
ev("e5", EventBody::PaymentAssigned { payment: "p1".into(), budget: "b-trip".into() }),
|
|
ev(
|
|
"e6",
|
|
EventBody::PaymentAdded {
|
|
payment: "p2".into(),
|
|
amount: Haler(-12_000),
|
|
date: d("2026-11-02"),
|
|
merchant: "Lekarna".into(),
|
|
category: None,
|
|
note: Some("private".into()),
|
|
},
|
|
),
|
|
]
|
|
}
|
|
|
|
#[test]
|
|
fn shared_payment_is_made_only_for_a_payment_in_that_budget() {
|
|
let s = State::replay(&personal());
|
|
let body = shared_payment(&s, "p1", "b-trip").expect("p1 is in the trip budget");
|
|
assert_eq!(
|
|
body,
|
|
EventBody::PaymentShared {
|
|
payment: "p1".into(),
|
|
budget: "b-trip".into(),
|
|
amount: Haler(-45_000),
|
|
date: d("2026-11-02"),
|
|
merchant: "Pension Lipno".into(),
|
|
category: Some("travel".into()),
|
|
glyph: Glyph::Captured,
|
|
}
|
|
);
|
|
let json = serde_json::to_string(&body).unwrap();
|
|
assert!(!json.contains("Visa") && !json.contains("1234"), "the card stays home: {json}");
|
|
assert!(shared_payment(&s, "p2", "b-trip").is_none(), "p2 is in the default budget, not the trip");
|
|
assert!(shared_payment(&s, "p1", "b-home").is_none());
|
|
assert!(shared_payment(&s, "nope", "b-trip").is_none());
|
|
}
|
|
|
|
fn shared_log() -> Vec<Event> {
|
|
let s = State::replay(&personal());
|
|
vec![
|
|
ev("g1", EventBody::BudgetCreated { budget: "b-trip".into(), name: "Trip".into(), limit: Haler(500_000) }),
|
|
ev("g2", shared_payment(&s, "p1", "b-trip").unwrap()),
|
|
]
|
|
}
|
|
|
|
#[test]
|
|
fn members_see_the_shared_payment_in_the_budget() {
|
|
let s = State::replay(&shared_log());
|
|
assert!(s.rejected.is_empty(), "{:?}", s.rejected);
|
|
let p = s.effective_payment("p1").unwrap();
|
|
assert_eq!((p.amount, p.glyph, p.budget.as_deref(), p.category.as_deref()), (Haler(-45_000), Glyph::Captured, Some("b-trip"), Some("travel")));
|
|
assert_eq!(p.card, None);
|
|
}
|
|
|
|
#[test]
|
|
fn a_shared_payment_can_be_split_and_unshared_until_it_is() {
|
|
let mut log = shared_log();
|
|
log.push(ev("g3", EventBody::PaymentUnshared { payment: "p1".into(), budget: "b-trip".into() }));
|
|
let s = State::replay(&log);
|
|
assert!(s.rejected.is_empty() && s.effective_payment("p1").is_none(), "unshared: gone from the budget");
|
|
|
|
let mut log = shared_log();
|
|
let shares = vec![
|
|
Share { person: "me".into(), name: "Me".into(), amount: Haler(-22_500) },
|
|
Share { person: "anna".into(), name: "Anna".into(), amount: Haler(-22_500) },
|
|
];
|
|
log.push(ev("g3", EventBody::SplitCreated { split: "s1".into(), payment: "p1".into(), payer: "me".into(), shares }));
|
|
log.push(ev("g4", EventBody::PaymentUnshared { payment: "p1".into(), budget: "b-trip".into() }));
|
|
let s = State::replay(&log);
|
|
assert_eq!(s.rejected.len(), 1, "a split payment stays shared");
|
|
assert!(s.effective_payment("p1").is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn sharing_into_a_budget_the_log_does_not_have_is_rejected() {
|
|
let s = State::replay(&personal());
|
|
let s2 = State::replay(&[ev("g2", shared_payment(&s, "p1", "b-trip").unwrap())]);
|
|
assert_eq!(s2.rejected.len(), 1);
|
|
}
|