507eb62216
fiscus_capture::receipt::read takes the OCR's lines (words with boxes and confidence in thousandths) and finds the total, date and time, IČO (check digit), DIČ and the shop's name by their labels, never a shop's layout; unsure words mark their field, the shop is always to check; bounded, control characters dropped, a proptest. The phone's post takes a receipt (source receipt, ico, dic). The ledger gains payment.receipt_attached (the payment keeps its merchant and key), known_shop and receipt_match (exactly one candidate or none). docs/07_receipts.md; fixtures/receipts/ waits for redacted samples. v0.9.0.
115 lines
5.5 KiB
Rust
115 lines
5.5 KiB
Rust
//! 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());
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_receipt_read_on_the_phone_carries_its_shop_ico_and_dic_and_nothing_else_does() {
|
|
let body = |extra: &str| format!(r#"{{"amount":"1 284,90","merchant":" Albert Hypermarket ","card":"","time":"2026-11-16T14:32:00+01:00"{extra}}}"#);
|
|
let c = parse(body(r#","source":"receipt","ico":"25672541","dic":"CZ25672541""#).as_bytes()).unwrap();
|
|
assert_eq!(c.amount, Haler(-128_490));
|
|
assert_eq!(c.card, None);
|
|
let r = c.receipt.unwrap();
|
|
assert_eq!((r.shop.as_str(), r.ico.as_deref(), r.dic.as_deref()), ("Albert Hypermarket", Some("25672541"), Some("CZ25672541")));
|
|
let c = parse(body(r#","source":"receipt""#).as_bytes()).unwrap();
|
|
assert_eq!(c.receipt.unwrap().ico, None, "a receipt without an IČO is fine");
|
|
assert_eq!(parse(post("129.5").as_bytes()).unwrap().receipt, None, "a card capture has no receipt");
|
|
for (extra, why) in [
|
|
(r#","ico":"25672541""#, CaptureError::Receipt),
|
|
(r#","source":"receipt","ico":"25672542""#, CaptureError::Receipt),
|
|
(r#","source":"receipt","dic":"SK2020123456""#, CaptureError::Receipt),
|
|
(r#","source":"card""#, CaptureError::Source),
|
|
(r#","source":"receipt","items":[]"#, CaptureError::Malformed),
|
|
] {
|
|
assert_eq!(parse(body(extra).as_bytes()), Err(why), "{extra}");
|
|
}
|
|
}
|