//! Day-2 mandatory test: Android bank notifications. Every real fixture parses; a notification from an app //! the person did not select is never read; an unknown format is ignored, never guessed. //! //! The registry has no templates until real samples exist, so the engine is tested here with SYNTHETIC //! templates that live only in this file. They are not any bank's format. use fiscus_capture::notification::{recognise, recognise_with, Recognised, Template, TEMPLATES}; use fiscus_ledger::{Date, Haler, Timestamp}; use serde::Deserialize; use std::path::{Path, PathBuf}; const APP: &str = "test.synthetic.bank"; const SYNTHETIC: &[Template] = &[ Template { app: APP, bank: "synthetic", title: Some("Card payment"), text: "Paid {amount} at {merchant} with card {card}." }, Template { app: APP, bank: "synthetic", title: None, text: "{_}: {amount} - {merchant}" }, ]; fn day() -> Date { "2026-11-03".parse().unwrap() } fn at() -> Timestamp { Timestamp(1_793_711_100_000) } fn run(app: &str, title: &str, text: &str, selected: &[&str]) -> Recognised { recognise_with(SYNTHETIC, app, title, text, day(), at(), selected) } #[test] fn a_notification_matching_a_template_is_a_payment() { match run(APP, "Card payment", "Paid 129,50 Kč at Albert Praha with card *1234.", &[APP]) { Recognised::Payment(c) => { assert_eq!(c.amount, Haler(-12_950)); assert_eq!(c.merchant, "Albert Praha"); assert_eq!(c.card.as_deref(), Some("*1234")); assert_eq!((c.date, c.at), (day(), at())); } other => panic!("{other:?}"), } assert!(matches!(run(APP, "anything", "Debit: 1 234,00 CZK - Shell", &[APP]), Recognised::Payment(c) if c.amount == Haler(-123_400) && c.merchant == "Shell")); } #[test] fn an_app_that_was_not_selected_is_never_read() { // Even text that a template would match: the app decides first. assert_eq!(run(APP, "Card payment", "Paid 129,50 Kč at Albert with card *1234.", &[]), Recognised::NotSelected); assert_eq!(run(APP, "Card payment", "Paid 129,50 Kč at Albert with card *1234.", &["other.bank"]), Recognised::NotSelected); assert_eq!(run("com.chat.app", "Card payment", "Paid 129,50 Kč at Albert with card *1234.", &[APP]), Recognised::NotSelected); } #[test] fn an_unknown_format_is_ignored_never_guessed() { for (title, text) in [ ("Card payment", "Paid 129,50 Kč at Albert with card *1234"), // the final full stop is missing ("Card payment", "Paid around 129,50 Kč at Albert with card *1234."), ("Card payment", "Paid 129,50 EUR at Albert with card *1234."), ("Card payment", "Paid 0 Kč at Albert with card *1234."), ("Balance", "Your balance is 12 000 Kč"), ("Card payment", ""), ] { assert_eq!(run(APP, title, text, &[APP]), Recognised::Unknown, "{text}"); } let long = format!("Debit: {} - Shell", "1".repeat(5000)); assert_eq!(run(APP, "x", &long, &[APP]), Recognised::Unknown); } #[test] fn an_ambiguous_match_is_unknown() { // `{_}: {amount} - {merchant}` matches "A: 1 - B: 2 - C" two ways with different amounts. assert_eq!(run(APP, "x", "A: 1 - B: 2 - C", &[APP]), Recognised::Unknown); } #[test] fn the_real_registry_is_used_by_recognise() { // With no templates for an app, a selected app's notification is unknown, never a payment. for t in TEMPLATES { assert!(!t.app.is_empty() && !t.bank.is_empty() && !t.text.is_empty()); } assert_eq!(recognise(APP, "Card payment", "Paid 129,50 Kč at Albert with card *1234.", day(), at(), &[APP]), Recognised::Unknown); } /// A real, redacted notification and what it must become (`fixtures/notifications//*.json`). #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct Sample { app: String, title: String, text: String, /// `null` for a notification that is not a payment (a balance, a login) and must stay unknown. expected: Option, } #[derive(Deserialize)] #[serde(deny_unknown_fields)] struct Expected { amount: i64, merchant: String, card: Option, } fn samples() -> Vec { let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures/notifications"); let mut out = Vec::new(); let Ok(banks) = std::fs::read_dir(&root) else { return out }; for bank in banks.flatten().filter(|e| e.path().is_dir()) { for f in std::fs::read_dir(bank.path()).unwrap().flatten() { if f.path().extension().is_some_and(|e| e == "json") { out.push(f.path()); } } } out.sort(); out } #[test] fn every_real_fixture_parses_to_what_it_must() { let all = samples(); eprintln!("notification fixtures: {}", all.len()); for path in all { let s: Sample = serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap(); let got = recognise(&s.app, &s.title, &s.text, day(), at(), &[s.app.as_str()]); match (&s.expected, got) { (None, Recognised::Unknown) => {} (Some(e), Recognised::Payment(c)) => { assert_eq!((c.amount, c.merchant.as_str(), c.card.as_deref()), (Haler(e.amount), e.merchant.as_str(), e.card.as_deref()), "{}", path.display()); } (_, got) => panic!("{}: {got:?}", path.display()), } } }