[tests] Day 2: splits, share links, sharing boundary, QR Platba, Shortcut posts, bank notifications, household duplicates

This commit is contained in:
2026-11-03 23:27:26 +01:00
parent 8e085bafc3
commit b4474f288e
6 changed files with 676 additions and 0 deletions
+133
View File
@@ -0,0 +1,133 @@
//! 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/<bank>/*.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<Expected>,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Expected {
amount: i64,
merchant: String,
card: Option<String>,
}
fn samples() -> Vec<PathBuf> {
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()),
}
}
}
+92
View File
@@ -0,0 +1,92 @@
//! Day-2 mandatory test (the inbox's half in the core): the iPhone Shortcut post is hostile input. A malformed,
//! oversized or ambiguous body is an error with a reason, never a partial payment.
use fiscus_capture::shortcut::{parse, CaptureError, MAX_BODY};
use fiscus_ledger::{Date, Haler, Timestamp};
use proptest::prelude::*;
/// 2026-11-03T13:05:00Z in milliseconds.
const AT: i64 = 1_793_711_100_000;
fn post(amount: &str) -> String {
format!(r#"{{"amount":{amount},"merchant":"ALBERT HYPERMARKET","card":"Apple Card","time":"2026-11-03T14:05:00+01:00"}}"#)
}
#[test]
fn a_good_post_becomes_one_outflow() {
let c = parse(post("129.5").as_bytes()).unwrap();
assert_eq!(c.amount, Haler(-12_950));
assert_eq!(c.merchant, "ALBERT HYPERMARKET");
assert_eq!(c.card.as_deref(), Some("Apple Card"));
assert_eq!(c.date, "2026-11-03".parse::<Date>().unwrap());
// 14:05 at +01:00 is 13:05 UTC.
assert_eq!(c.at, Timestamp(AT));
}
#[test]
fn the_amount_forms_shortcuts_writes_are_read_from_text_never_as_a_float() {
for (text, want) in [
("129", 12_900),
("129.00", 12_900),
("0.1", 10),
(r#""129,50""#, 12_950),
(r#""129,50 Kč""#, 12_950),
(r#""Kč 129,50""#, 12_950),
(r#""CZK 1 234,50""#, 123_450),
("\"1\u{a0}234,50\u{a0}Kč\"", 123_450),
("\"1\u{202f}234,50 CZK\"", 123_450),
] {
assert_eq!(parse(post(text).as_bytes()).map(|c| c.amount), Ok(Haler(-want)), "{text}");
}
for text in ["0", "-129", "1e3", "129.555", "\"\"", "\"abc\"", "\"$129\"", "\"129 EUR\"", "null", "true", "[129]", "99999999999"] {
assert_eq!(parse(post(text).as_bytes()).map(|c| c.amount), Err(CaptureError::Amount), "{text}");
}
}
#[test]
fn every_field_is_checked() {
let ok = r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z"}"#;
assert!(parse(ok.as_bytes()).is_ok());
let cases: &[(&str, CaptureError)] = &[
(r#"{"merchant":"M","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Malformed),
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z","extra":1}"#, CaptureError::Malformed),
(r#"{"amount":1,"amount":2,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Malformed),
(r#"{"amount":1,"merchant":"","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Merchant),
(r#"{"amount":1,"merchant":"a\u0000b","card":"","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Merchant),
(r#"{"amount":1,"merchant":"M","card":"a\nb","time":"2026-11-03T14:05:00Z"}"#, CaptureError::Card),
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03 14:05"}"#, CaptureError::Time),
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-02-30T14:05:00Z"}"#, CaptureError::Time),
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T24:05:00Z"}"#, CaptureError::Time),
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00"}"#, CaptureError::Time),
(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z","currency":"EUR"}"#, CaptureError::Currency),
(r#"[1,2]"#, CaptureError::Malformed),
("", CaptureError::Malformed),
];
for (body, want) in cases {
assert_eq!(parse(body.as_bytes()).err().as_ref(), Some(want), "{body}");
}
let long = format!(r#"{{"amount":1,"merchant":"{}","card":"","time":"2026-11-03T14:05:00Z"}}"#, "m".repeat(201));
assert_eq!(parse(long.as_bytes()).err(), Some(CaptureError::Merchant));
assert!(parse(r#"{"amount":1,"merchant":"M","card":"","time":"2026-11-03T14:05:00Z","currency":"CZK"}"#.as_bytes()).is_ok());
}
#[test]
fn an_oversized_body_is_refused_before_parsing() {
let mut big = post("1").into_bytes();
big.resize(MAX_BODY + 1, b' ');
assert_eq!(parse(&big).err(), Some(CaptureError::TooLarge));
}
proptest! {
#[test]
fn random_bytes_never_panic_and_never_half_parse(bytes in proptest::collection::vec(any::<u8>(), 0..600)) {
let _ = parse(&bytes);
}
#[test]
fn every_truncation_of_a_good_post_is_an_error(cut in 0usize..90) {
let full = post("\"129,50 Kč\"");
let cut = cut.min(full.len() - 1);
prop_assert!(parse(&full.as_bytes()[..cut]).is_err());
}
}