129 lines
3.8 KiB
Rust
129 lines
3.8 KiB
Rust
use budget::{
|
|
budget as planner,
|
|
desktop_review::{money, Review, ReviewFields},
|
|
products, sheet,
|
|
};
|
|
fn fields() -> ReviewFields<'static> {
|
|
ReviewFields {
|
|
title: "Mouse",
|
|
url: "https://example.test/item",
|
|
date: "2026-10-27",
|
|
currency: "ZAR",
|
|
price: "249.99",
|
|
shipping: "30",
|
|
quantity: "2",
|
|
section: 1,
|
|
item: 0,
|
|
}
|
|
}
|
|
#[test]
|
|
fn decimal_money_is_exact_and_bounded() {
|
|
for (input, expected) in [("0", 0), ("0.01", 1), ("249.99", 24999), ("1.2", 120)] {
|
|
assert_eq!(money(input).unwrap(), expected);
|
|
}
|
|
for bad in [
|
|
"",
|
|
"NaN",
|
|
"-1",
|
|
"+1",
|
|
"1,000",
|
|
"R 10",
|
|
"1.234",
|
|
"1.2.3",
|
|
"9000000000.01",
|
|
"9999999999999999999999999999999",
|
|
] {
|
|
assert!(money(bad).is_err(), "{bad}");
|
|
}
|
|
}
|
|
#[test]
|
|
fn review_requires_valid_currency_date_category_quantity_and_title() {
|
|
let review = Review::new("Untrusted OCR: USD 12.34".into()).unwrap();
|
|
assert_eq!(review.product(fields()).unwrap().total().unwrap(), 52998);
|
|
let mut f = fields();
|
|
f.currency = "USD";
|
|
assert!(review.product(f).is_err());
|
|
let mut f = fields();
|
|
f.date = "2026-02-30";
|
|
assert!(review.product(f).is_err());
|
|
let mut f = fields();
|
|
f.section = 0;
|
|
assert!(review.product(f).is_err());
|
|
let mut f = fields();
|
|
f.item = usize::MAX;
|
|
assert!(review.product(f).is_err());
|
|
let mut f = fields();
|
|
f.quantity = "0";
|
|
assert!(review.product(f).is_err());
|
|
let mut f = fields();
|
|
f.quantity = "1.5";
|
|
assert!(review.product(f).is_err());
|
|
let mut f = fields();
|
|
f.title = " ";
|
|
assert!(review.product(f).is_err());
|
|
let mut f = fields();
|
|
f.url = "file:///private";
|
|
assert!(review.product(f).is_err());
|
|
}
|
|
#[test]
|
|
fn discard_does_not_write_and_new_captures_have_distinct_ids() {
|
|
let workbook = planner::workbook();
|
|
let before = sheet::snapshot_to_bytes(&workbook).unwrap();
|
|
let a = Review::new("Mouse".into()).unwrap();
|
|
let b = Review::new("Mouse".into()).unwrap();
|
|
assert_ne!(a.id, b.id);
|
|
drop(a);
|
|
drop(b);
|
|
assert_eq!(before, sheet::snapshot_to_bytes(&workbook).unwrap());
|
|
}
|
|
#[test]
|
|
fn failed_save_does_not_mutate_and_retry_is_durable_idempotent() {
|
|
let mut workbook = planner::workbook();
|
|
let before = sheet::snapshot_to_bytes(&workbook).unwrap();
|
|
let review = Review::new("Mouse".into()).unwrap();
|
|
let product = review.product(fields()).unwrap();
|
|
assert!(products::confirm(&mut workbook, &product, |_| Err("disk full".into())).is_err());
|
|
assert_eq!(before, sheet::snapshot_to_bytes(&workbook).unwrap());
|
|
let mut persisted = Vec::new();
|
|
assert!(products::confirm(&mut workbook, &product, |candidate| {
|
|
persisted = sheet::snapshot_to_bytes(candidate)?;
|
|
Ok(())
|
|
})
|
|
.unwrap());
|
|
assert_eq!(persisted, sheet::snapshot_to_bytes(&workbook).unwrap());
|
|
assert!(
|
|
!products::confirm(&mut workbook, &product, |_| panic!("duplicate persisted")).unwrap()
|
|
);
|
|
assert_eq!(persisted, sheet::snapshot_to_bytes(&workbook).unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn only_one_explicit_standalone_zar_quote_is_suggested() {
|
|
assert_eq!(
|
|
Review::new("Mouse\nZAR 249.99".into())
|
|
.unwrap()
|
|
.suggested_zar_price()
|
|
.as_deref(),
|
|
Some("249.99")
|
|
);
|
|
assert_eq!(
|
|
Review::new("Mouse\nR249.9".into())
|
|
.unwrap()
|
|
.suggested_zar_price()
|
|
.as_deref(),
|
|
Some("249.90")
|
|
);
|
|
for text in [
|
|
"Mouse 249.99",
|
|
"USD 249.99",
|
|
"R 299\nR 249",
|
|
"R 249.99\nR 30",
|
|
"R -1",
|
|
"R 1,000.00",
|
|
] {
|
|
assert!(Review::new(text.into())
|
|
.unwrap()
|
|
.suggested_zar_price()
|
|
.is_none());
|
|
}
|
|
}
|