130 lines
5.8 KiB
Rust
130 lines
5.8 KiB
Rust
//! Mandatory test 4: derived state is a pure function of the log. Delete it, rebuild it from the log, and
|
|
//! the bytes are identical, however it was built.
|
|
|
|
use fiscus_ledger::import::{plan_import, StatementLine};
|
|
use fiscus_ledger::{Date, Decision, Event, EventBody, Haler, State, Timestamp};
|
|
|
|
fn d(s: &str) -> Date {
|
|
s.parse().unwrap()
|
|
}
|
|
|
|
/// A month of everything: captures, a statement, hand-added cash, fixes, rules, budgets, a trial, decisions,
|
|
/// and a few events that must be rejected.
|
|
fn sample_log() -> Vec<Event> {
|
|
let mut n = 0u64;
|
|
let mut id = || {
|
|
n += 1;
|
|
format!("e{n:04}")
|
|
};
|
|
let mut log = Vec::new();
|
|
let push = |log: &mut Vec<Event>, id: String, body: EventBody| {
|
|
let at = Timestamp(1_790_000_000_000 + log.len() as i64 * 1000);
|
|
log.push(Event::new(id, at, body));
|
|
};
|
|
push(&mut log, id(), EventBody::BudgetCreated { budget: "b-home".into(), name: "Home".into(), limit: Haler(2_000_000) });
|
|
push(&mut log, id(), EventBody::BudgetCreated { budget: "b-fun".into(), name: "Fun".into(), limit: Haler(300_000) });
|
|
push(&mut log, id(), EventBody::BudgetLimitSet { budget: "b-fun".into(), limit: Haler(350_000) });
|
|
push(
|
|
&mut log,
|
|
id(),
|
|
EventBody::PaymentCaptured { payment: "p-cap1".into(), amount: Haler(-12_900), date: d("2026-10-02"), merchant: "ALBERT 1234".into(), card: Some("Visa".into()) },
|
|
);
|
|
push(
|
|
&mut log,
|
|
id(),
|
|
EventBody::PaymentAdded { payment: "p-cash".into(), amount: Haler(-8_000), date: d("2026-10-04"), merchant: "Trh".into(), category: Some("groceries".into()), note: Some("market".into()) },
|
|
);
|
|
let lines: Vec<StatementLine> = [
|
|
(-12_900, "2026-10-03", "Albert"),
|
|
(-49_900, "2026-10-05", "PAYPAL *SPOTIFY"),
|
|
(-100, "2026-10-06", "NETFLIX.COM"),
|
|
(3_500_000, "2026-10-10", "Mzda"),
|
|
]
|
|
.iter()
|
|
.map(|(a, date, m)| StatementLine {
|
|
bank: "test".into(),
|
|
account: "1/0000".into(),
|
|
booked: d(date),
|
|
amount: Haler(*a),
|
|
merchant: (*m).into(),
|
|
counterparty: None,
|
|
variable_symbol: None,
|
|
message: None,
|
|
bank_ref: None,
|
|
})
|
|
.collect();
|
|
let mut state = State::replay(&log);
|
|
let mut k = 0u64;
|
|
let plan = plan_import(&state, &lines, &mut || {
|
|
k += 1;
|
|
format!("imp{k:03}")
|
|
}, Timestamp(1_790_000_100_000));
|
|
for e in plan.events.iter().chain(&plan.confirmations) {
|
|
state.apply(e);
|
|
log.push(e.clone());
|
|
}
|
|
push(&mut log, id(), EventBody::RuleCreated { rule: "r1".into(), merchant_key: "spotify".into(), category: "subscriptions".into() });
|
|
push(&mut log, id(), EventBody::PaymentCategorised { payment: "p-cap1".into(), category: "groceries".into() });
|
|
push(&mut log, id(), EventBody::PaymentAssigned { payment: "p-cash".into(), budget: "b-fun".into() });
|
|
push(&mut log, id(), EventBody::PaymentTrialMarked { payment: "imp003".into(), ends: d("2026-11-06") });
|
|
push(&mut log, id(), EventBody::SubscriptionDecided { subscription: "spotify".into(), decision: Decision::Keep, amount: Haler(49_900), on: d("2026-10-20") });
|
|
// To be rejected, deterministically.
|
|
push(&mut log, id(), EventBody::PaymentAssigned { payment: "p-nope".into(), budget: "b-fun".into() });
|
|
push(&mut log, id(), EventBody::BudgetCreated { budget: "b-fun".into(), name: "Again".into(), limit: Haler(1) });
|
|
push(&mut log, id(), EventBody::PaymentAdded { payment: "p-zero".into(), amount: Haler(0), date: d("2026-10-04"), merchant: "x".into(), category: None, note: None });
|
|
let dup = log[3].clone();
|
|
log.push(dup); // the same event twice (a sync replay)
|
|
log
|
|
}
|
|
|
|
#[test]
|
|
fn rebuild_from_the_log_is_byte_identical() {
|
|
let log = sample_log();
|
|
|
|
// 1. Built incrementally, saved, "deleted".
|
|
let mut live = State::default();
|
|
for e in &log {
|
|
live.apply(e);
|
|
}
|
|
let saved = serde_json::to_vec(&live).unwrap();
|
|
drop(live);
|
|
|
|
// 2. Rebuilt in one go from the log.
|
|
let rebuilt = State::replay(&log);
|
|
assert_eq!(serde_json::to_vec(&rebuilt).unwrap(), saved);
|
|
|
|
// 3. Rebuilt from the log after it went through its serialised form (what the app stores).
|
|
let stored = serde_json::to_vec(&log).unwrap();
|
|
let reloaded: Vec<Event> = serde_json::from_slice(&stored).unwrap();
|
|
assert_eq!(serde_json::to_vec(&State::replay(&reloaded)).unwrap(), saved);
|
|
|
|
// 4. The derived views are byte-identical too.
|
|
assert_eq!(
|
|
serde_json::to_vec(&State::replay(&log).effective_payments()).unwrap(),
|
|
serde_json::to_vec(&rebuilt.effective_payments()).unwrap()
|
|
);
|
|
|
|
// The bad events were rejected, and the right ones.
|
|
assert_eq!(rebuilt.rejected.len(), 4, "{:#?}", rebuilt.rejected);
|
|
}
|
|
|
|
#[test]
|
|
fn the_state_says_what_the_log_says() {
|
|
let s = State::replay(&sample_log());
|
|
let pays = s.effective_payments();
|
|
// cap1 + its line are one; cash; spotify; netflix check; salary.
|
|
assert_eq!(pays.len(), 5);
|
|
let albert = pays.iter().find(|p| p.merchant_key == "albert").unwrap();
|
|
assert_eq!(albert.category.as_deref(), Some("groceries"));
|
|
assert!(albert.line.is_some() && albert.captured.is_some());
|
|
let spotify = pays.iter().find(|p| p.merchant_key == "spotify").unwrap();
|
|
assert_eq!(spotify.category.as_deref(), Some("subscriptions"), "the rule applies to earlier payments");
|
|
let cash = pays.iter().find(|p| p.id == "p-cash").unwrap();
|
|
assert_eq!(cash.budget.as_deref(), Some("b-fun"));
|
|
assert_eq!(spotify.budget.as_deref(), Some("b-home"), "the first budget is the default");
|
|
assert_eq!(s.budgets["b-fun"].limit, Haler(350_000));
|
|
// Money in = money on the statement + hand-added cash.
|
|
let total = Haler::sum(pays.iter().map(|p| p.amount)).unwrap();
|
|
assert_eq!(total, Haler(-12_900 - 49_900 - 100 + 3_500_000 - 8_000));
|
|
}
|