//! Receipts read from an OCR's lines (docs/07_receipts.md): the fields every Czech receipt shows, found by their //! labels; what to pay beats a sum before a discount; VAT lines are never the total; an IČO needs its check digit; //! unsure words are marked; hostile text is bounded and never panics. Synthetic text here: real receipts come as //! redacted OCR output in `fixtures/receipts/` (none yet). use fiscus_capture::receipt::{read, BBox, Line, ReceiptError, Word, MAX_LINES}; use fiscus_ledger::{Date, Haler}; use proptest::prelude::*; /// A receipt as text, one line per `\n`; a word ending in `?` was read with low confidence (and loses the `?`). fn page(text: &str) -> Vec { text.lines() .enumerate() .map(|(y, l)| Line { words: l .split_whitespace() .enumerate() .map(|(x, w)| { let (t, c) = match w.strip_suffix('?') { Some(t) if !t.is_empty() => (t, 420), _ => (w, 960), }; Word { text: t.into(), confidence: c, bbox: BBox { x: x as u32 * 50, y: y as u32 * 20, w: 40, h: 16 } } }) .collect(), }) .collect() } fn d(s: &str) -> Date { s.parse().unwrap() } const ALBERT: &str = "ALBERT HYPERMARKET Praha 5 IČ: 25672541 DIČ: CZ25672541 Rohlík tukový 4×3,90 15,60 Mléko polotučné 1 l 24,90 Káva zrnková 1 kg 389,00 CELKEM Kč 1 284,90 Platba kartou 1 284,90 DPH 12 % 137,67 16.11.2026 14:32 Pokl. 07 Děkujeme za nákup"; #[test] fn a_supermarket_receipt_gives_every_field() { let r = read(&page(ALBERT)).unwrap(); let total = r.total.unwrap(); assert_eq!((total.value, total.sure), (Haler(128_490), true)); assert_eq!(total.boxes.len(), 4, "the label and the amount, over two words each"); assert_eq!(r.date.unwrap().value, d("2026-11-16")); assert_eq!(r.time.unwrap().value, (14, 32)); assert_eq!(r.ico.unwrap().value, "25672541"); assert_eq!(r.dic.unwrap().value, "CZ25672541"); let shop = r.shop.unwrap(); assert_eq!((shop.value.as_str(), shop.sure), ("Albert Hypermarket", false), "the shop's name is always to check"); } #[test] fn what_is_to_pay_beats_a_sum_before_a_discount_and_vat_lines_are_never_the_total() { let r = read(&page("OBI\nIČO 25672541\nCELKEM 1 400,00\nSleva -115,10\nZáklad DPH 21 % 1 061,90\nK ÚHRADĚ 1 284,90\n2. 11. 2026")).unwrap(); assert_eq!(r.total.unwrap().value, Haler(128_490)); let r = read(&page("DPH celkem 223,00\nZaokrouhlení 0,10\nSUMA 89,90")).unwrap(); assert_eq!(r.total.unwrap().value, Haler(8_990)); } #[test] fn an_amount_alone_on_the_line_after_its_label_counts() { let r = read(&page("CELKEM\n389,00\n16.11.2026")).unwrap(); assert_eq!(r.total.unwrap().value, Haler(38_900)); let r = read(&page("CELKEM\n2 ks 389,00")).unwrap(); assert!(r.total.is_none(), "a next line that is more than an amount is not the total"); } #[test] fn a_word_read_without_confidence_marks_its_field() { let r = read(&page("CELKEM 1 284,90?\n16.11.2026? 14:32")).unwrap(); assert!(!r.total.unwrap().sure); assert!(!r.date.unwrap().sure); assert!(r.time.unwrap().sure); } #[test] fn dates_in_the_ways_receipts_print_them() { for (text, want) in [("16. 11. 2026", "2026-11-16"), ("2026-11-16", "2026-11-16"), ("16.11.26", "2026-11-16"), ("1/2/2027", "2027-02-01"), ("Datum: 05.01.2027 08:15", "2027-01-05")] { assert_eq!(read(&page(text)).unwrap().date.map(|f| f.value), Some(d(want)), "{text}"); } for text in ["31.02.2026", "16.11.1999", "16.13.2026", "1.2.3.4"] { assert!(read(&page(text)).unwrap().date.is_none(), "{text}"); } assert_eq!(read(&page("Čas 25:61\n09:05")).unwrap().time.map(|f| f.value), Some((9, 5))); } #[test] fn an_ico_needs_its_check_digit_and_a_company_dic_gives_one() { assert!(read(&page("IČ: 25672542")).unwrap().ico.is_none(), "the check digit does not fit"); assert_eq!(read(&page("IČO 2567254 1")).unwrap().ico, None, "split digits are not an IČO"); let r = read(&page("DIČ: CZ 25672541")).unwrap(); assert_eq!((r.dic.as_ref().unwrap().value.as_str(), r.ico.unwrap().value.as_str()), ("CZ25672541", "25672541")); let r = read(&page("DIČ: CZ7811241234")).unwrap(); assert!(r.ico.is_none(), "a person's DIČ is a birth number, not an IČO"); } #[test] fn the_shop_is_the_first_line_with_a_name_above_the_ico() { let r = read(&page("www.example.cz\n123 456\nLékárna U Anděla\nIČ 25672541\nCELKEM 189,00")).unwrap(); assert_eq!(r.shop.unwrap().value, "Lékárna U Anděla"); let r = read(&page("IČ 25672541\nZboží 189,00")).unwrap(); assert!(r.shop.is_none(), "nothing above the IČO"); } #[test] fn nothing_to_go_on_is_an_empty_receipt_not_an_error() { assert_eq!(read(&[]).unwrap(), Default::default()); let r = read(&page("Děkujeme za nákup\nNa shledanou")).unwrap(); assert!(r.total.is_none() && r.date.is_none() && r.ico.is_none()); } #[test] fn hostile_text_is_bounded_and_control_characters_are_dropped() { let many: Vec = (0..=MAX_LINES).map(|_| Line::default()).collect(); assert_eq!(read(&many), Err(ReceiptError::TooLarge)); let long = vec![Line { words: vec![Word { text: "x".repeat(81), confidence: 900, bbox: BBox::default() }] }]; assert_eq!(read(&long), Err(ReceiptError::TooLarge)); let r = read(&page("CELKEM\u{7} 1\u{0} 284,90\nALB\u{1b}ERT")).unwrap(); assert_eq!(r.total.unwrap().value, Haler(128_490)); let r = read(&page("CELKEM 99 999 999,00")).unwrap(); assert!(r.total.is_none(), "over the largest payment Fiscus believes"); } proptest! { #[test] fn any_text_reads_without_panicking_and_a_total_is_a_believable_payment(words in prop::collection::vec(prop::collection::vec("[ -~ÁČĎÉĚÍŇÓŘŠŤÚŮÝŽáčďéěíňóřšťúůýž×−]{0,12}", 0..12), 0..40)) { let lines: Vec = words.iter().map(|l| Line { words: l.iter().map(|t| Word { text: t.clone(), confidence: 900, bbox: BBox::default() }).collect() }).collect(); if let Ok(r) = read(&lines) { if let Some(t) = r.total { prop_assert!(t.value.0 > 0 && t.value <= fiscus_capture::amount::MAX); } } } }