223 lines
8.6 KiB
Rust
223 lines
8.6 KiB
Rust
//! Mandatory test 3: matching captures to statement lines is one to one, a statement imported twice adds
|
|
//! nothing, and everything left over is reported on both sides.
|
|
|
|
use fiscus_ledger::import::{plan_import, StatementLine};
|
|
use fiscus_ledger::matching::find_matches;
|
|
use fiscus_ledger::{Date, Event, EventBody, Haler, State, Timestamp};
|
|
use proptest::prelude::*;
|
|
|
|
fn d(s: &str) -> Date {
|
|
s.parse().unwrap()
|
|
}
|
|
|
|
struct Ids(u64);
|
|
impl Ids {
|
|
fn next(&mut self) -> String {
|
|
self.0 += 1;
|
|
format!("id{:04}", self.0)
|
|
}
|
|
}
|
|
|
|
fn captured(ids: &mut Ids, amount: i64, date: &str, merchant: &str) -> Event {
|
|
Event::new(
|
|
ids.next(),
|
|
Timestamp(0),
|
|
EventBody::PaymentCaptured {
|
|
payment: ids.next(),
|
|
amount: Haler(amount),
|
|
date: d(date),
|
|
merchant: merchant.into(),
|
|
card: None,
|
|
},
|
|
)
|
|
}
|
|
|
|
fn line(amount: i64, date: &str, merchant: &str) -> StatementLine {
|
|
StatementLine {
|
|
bank: "test".into(),
|
|
account: "123/0000".into(),
|
|
booked: d(date),
|
|
amount: Haler(amount),
|
|
merchant: merchant.into(),
|
|
counterparty: None,
|
|
variable_symbol: None,
|
|
message: None,
|
|
bank_ref: None,
|
|
}
|
|
}
|
|
|
|
fn payment_of(e: &Event) -> String {
|
|
match &e.body {
|
|
EventBody::PaymentCaptured { payment, .. } | EventBody::PaymentImported { payment, .. } => payment.clone(),
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
|
|
fn import(state: &mut State, ids: &mut Ids, lines: &[StatementLine]) -> fiscus_ledger::import::ImportPlan {
|
|
let plan = plan_import(state, lines, &mut || ids.next(), Timestamp(1));
|
|
for e in plan.events.iter().chain(&plan.confirmations) {
|
|
state.apply(e);
|
|
}
|
|
plan
|
|
}
|
|
|
|
#[test]
|
|
fn a_capture_and_its_line_match() {
|
|
let mut ids = Ids(0);
|
|
let mut state = State::default();
|
|
let c = captured(&mut ids, -12_900, "2026-10-02", "ALBERT 1234 PRAHA");
|
|
state.apply(&c);
|
|
let plan = import(&mut state, &mut ids, &[line(-12_900, "2026-10-03", "Albert Praha")]);
|
|
assert_eq!(plan.events.len(), 1);
|
|
assert_eq!(plan.report.pairs.len(), 1);
|
|
assert_eq!(plan.report.pairs[0].0, payment_of(&c));
|
|
assert!(plan.report.unmatched_captured.is_empty() && plan.report.unmatched_lines.is_empty());
|
|
// Once confirmed, they are one payment and nothing is left to match.
|
|
assert_eq!(state.effective_payments().len(), 1);
|
|
let after = find_matches(&state);
|
|
assert!(after.pairs.is_empty() && after.unmatched_captured.is_empty() && after.unmatched_lines.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn one_capture_never_matches_two_lines() {
|
|
let mut ids = Ids(0);
|
|
let mut state = State::default();
|
|
let c = captured(&mut ids, -5_000, "2026-10-02", "Kavarna Dobra");
|
|
state.apply(&c);
|
|
let twins = [line(-5_000, "2026-10-03", "KAVARNA DOBRA"), line(-5_000, "2026-10-03", "KAVARNA DOBRA")];
|
|
let plan = import(&mut state, &mut ids, &twins);
|
|
// Two identical lines are two lines (two coffees), and the capture cannot pick one by itself.
|
|
assert_eq!(plan.events.len(), 2);
|
|
assert!(plan.report.pairs.is_empty());
|
|
assert_eq!(plan.report.ambiguous.len(), 1);
|
|
assert_eq!(plan.report.ambiguous[0].candidates.len(), 2);
|
|
assert_eq!(plan.report.unmatched_captured, vec![payment_of(&c)]);
|
|
assert_eq!(plan.report.unmatched_lines.len(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn a_second_confirmation_of_the_same_capture_is_rejected() {
|
|
let mut ids = Ids(0);
|
|
let mut state = State::default();
|
|
let c = captured(&mut ids, -5_000, "2026-10-02", "Kavarna");
|
|
state.apply(&c);
|
|
let plan = import(&mut state, &mut ids, &[line(-5_000, "2026-10-02", "Kavarna"), line(-5_000, "2026-10-04", "Kavarna")]);
|
|
assert_eq!(plan.report.pairs.len(), 1, "the line on the same day wins over the one two days later");
|
|
let second_line = payment_of(&plan.events[1]);
|
|
let again = Event::new(ids.next(), Timestamp(2), EventBody::PaymentConfirmed { payment: payment_of(&c), line: second_line });
|
|
let before = serde_json::to_string(&state.effective_payments()).unwrap();
|
|
state.apply(&again);
|
|
assert_eq!(serde_json::to_string(&state.effective_payments()).unwrap(), before);
|
|
assert_eq!(state.rejected.len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn the_same_statement_imported_twice_adds_nothing() {
|
|
let mut ids = Ids(0);
|
|
let mut state = State::default();
|
|
let lines = [
|
|
line(-12_900, "2026-10-03", "Albert"),
|
|
line(-12_900, "2026-10-03", "Albert"),
|
|
line(-49_900, "2026-10-05", "Spotify"),
|
|
line(3_500_000, "2026-10-10", "Salary"),
|
|
];
|
|
let first = import(&mut state, &mut ids, &lines);
|
|
assert_eq!(first.events.len(), 4);
|
|
let snapshot = serde_json::to_vec(&state).unwrap();
|
|
let second = import(&mut state, &mut ids, &lines);
|
|
assert!(second.events.is_empty() && second.confirmations.is_empty());
|
|
assert_eq!(second.duplicates, 4);
|
|
assert_eq!(serde_json::to_vec(&state).unwrap(), snapshot);
|
|
// An overlapping statement adds only its new lines.
|
|
let mut overlap = lines.to_vec();
|
|
overlap.push(line(-9_900, "2026-10-12", "Netflix"));
|
|
let third = import(&mut state, &mut ids, &overlap);
|
|
assert_eq!((third.events.len(), third.duplicates), (1, 4));
|
|
}
|
|
|
|
#[test]
|
|
fn unmatched_items_are_reported_on_both_sides() {
|
|
let mut ids = Ids(0);
|
|
let mut state = State::default();
|
|
let c1 = captured(&mut ids, -12_900, "2026-10-02", "Albert");
|
|
let c2 = captured(&mut ids, -7_000, "2026-10-02", "Rohlik"); // never on the statement
|
|
state.apply(&c1);
|
|
state.apply(&c2);
|
|
let plan = import(
|
|
&mut state,
|
|
&mut ids,
|
|
&[
|
|
line(-12_900, "2026-10-03", "Albert"),
|
|
line(-7_001, "2026-10-03", "Rohlik"), // one haléř off: not the same payment
|
|
line(-7_000, "2026-10-20", "Rohlik"), // too late
|
|
line(-3_000, "2026-10-03", "Benzina"), // nothing captured
|
|
],
|
|
);
|
|
assert_eq!(plan.report.pairs, vec![(payment_of(&c1), payment_of(&plan.events[0]))]);
|
|
assert_eq!(plan.report.unmatched_captured, vec![payment_of(&c2)]);
|
|
assert_eq!(plan.report.unmatched_lines.len(), 3);
|
|
}
|
|
|
|
#[test]
|
|
fn a_hand_added_payment_without_merchant_is_only_proposed() {
|
|
let mut ids = Ids(0);
|
|
let mut state = State::default();
|
|
let added = Event::new(
|
|
ids.next(),
|
|
Timestamp(0),
|
|
EventBody::PaymentAdded {
|
|
payment: ids.next(),
|
|
amount: Haler(-25_000),
|
|
date: d("2026-10-02"),
|
|
merchant: String::new(),
|
|
category: Some("groceries".into()),
|
|
note: None,
|
|
},
|
|
);
|
|
state.apply(&added);
|
|
let plan = import(&mut state, &mut ids, &[line(-25_000, "2026-10-02", "Lidl")]);
|
|
assert!(plan.report.pairs.is_empty());
|
|
assert_eq!(plan.report.ambiguous.len(), 1);
|
|
}
|
|
|
|
proptest! {
|
|
#[test]
|
|
fn matching_is_always_one_to_one_and_complete(
|
|
caps in prop::collection::vec((0i64..4, 0i32..10, 0usize..3), 0..12),
|
|
lines in prop::collection::vec((0i64..4, 0i32..10, 0usize..3), 0..12),
|
|
) {
|
|
let merchants = ["Albert", "Lidl", "Spotify"];
|
|
let base = d("2026-10-01");
|
|
let mut ids = Ids(0);
|
|
let mut state = State::default();
|
|
for (a, day, m) in &caps {
|
|
let e = Event::new(ids.next(), Timestamp(0), EventBody::PaymentCaptured {
|
|
payment: ids.next(), amount: Haler(-(a + 1) * 1000), date: base.add_days(*day),
|
|
merchant: merchants[*m].into(), card: None,
|
|
});
|
|
state.apply(&e);
|
|
}
|
|
let lines: Vec<_> = lines.iter().map(|(a, day, m)| StatementLine {
|
|
booked: base.add_days(*day), amount: Haler(-(a + 1) * 1000), merchant: merchants[*m].into(),
|
|
..line(0, "2026-10-01", "")
|
|
}).collect();
|
|
let plan = import(&mut state, &mut ids, &lines);
|
|
let r = &plan.report;
|
|
let mut seen_c = std::collections::BTreeSet::new();
|
|
let mut seen_l = std::collections::BTreeSet::new();
|
|
for (c, l) in &r.pairs {
|
|
prop_assert!(seen_c.insert(c.clone()), "capture matched twice");
|
|
prop_assert!(seen_l.insert(l.clone()), "line matched twice");
|
|
}
|
|
prop_assert_eq!(seen_c.len() + r.unmatched_captured.len(), caps.len());
|
|
prop_assert_eq!(seen_l.len() + r.unmatched_lines.len(), lines.len());
|
|
prop_assert!(state.rejected.is_empty());
|
|
// Applying the plan leaves every capture in at most one pair and the totals unchanged.
|
|
let total_lines: i64 = lines.iter().map(|l| l.amount.0).sum();
|
|
let total_unmatched_caps: i64 = state.effective_payments().iter()
|
|
.filter(|p| p.line.is_none()).map(|p| p.amount.0).sum();
|
|
let total: i64 = state.effective_payments().iter().map(|p| p.amount.0).sum();
|
|
prop_assert_eq!(total, total_lines + total_unmatched_caps);
|
|
}
|
|
}
|