use ::budget::{budget, history, persistence, products, sheet}; #[test] fn calendar_boundaries() { assert!(budget::normalize_date("2024-02-29").is_some()); for d in ["2025-02-29","1900-02-29","2026-04-31","+2026-01-01"] {assert!(budget::normalize_date(d).is_none(),"{d}");} assert!(history::Period::new("2026-02-01","2026-01-01").is_err()); let p = history::Period::new("2026-01-01","2026-01-31").unwrap(); assert!(p.contains("31/01/2026")); assert!(!p.contains("2026-02-01")); } #[test] fn snapshot_preserves_formulas_formats_and_sparse_positions() { let mut wb = budget::workbook(); wb.sheets[0].set_input((100,9),"=2+3"); let bytes = sheet::snapshot_to_bytes(&wb).unwrap(); let restored = sheet::snapshot_from_bytes(&bytes).unwrap(); assert_eq!(restored.input_at(0,(100,9)),"=2+3"); assert_eq!(restored.sheets[0].display((100,9)),"5"); assert_eq!(bytes,sheet::snapshot_to_bytes(&restored).unwrap()); } #[test] fn history_snapshots_do_not_change_with_planner() { let mut wb = budget::workbook(); let pos = budget::layout().sections[1].item_rows[0]; wb.set_input_at(0,pos,"100"); let p = history::Period::new("2026-01-01","2026-01-31").unwrap(); history::capture(&mut wb,&p).unwrap(); wb.set_input_at(0,pos,"900"); assert!(history::report(&wb,&p).contains("100.00")); assert!(history::capture(&mut wb,&p).is_err()); let i=wb.sheet_index_named(budget::PAYMENTS_SHEET).unwrap(); for (r,d) in [(1,"2026-01-31"),(2,"2026-02-01")] { for (c,t) in [(0,d),(1,"HOUSING"),(2,"Rent"),(3,"15")] {wb.set_input_at(i,(r,c),t);} } let report=history::report(&wb,&p); assert!(report.contains("Paid: 15.00")); assert!(!report.contains("2026-02-01")); } #[test] fn product_is_planned_not_paid_and_replay_is_safe() { let p=products::Product{id:"abc".into(),title:"Chair".into(),url:"https://example.com/p".into(),date:"2026-01-31".into(),currency:"ZAR".into(),unit_minor:10000,quantity:2,shipping_minor:500,section:1,item:4}; let mut wb=budget::workbook(); assert!(products::add(&mut wb,&p).unwrap()); assert!(!products::add(&mut wb,&p).unwrap()); assert_eq!(wb.display_at(0,budget::layout().sections[1].item_rows[4]),"205.00"); assert_eq!(wb.sheets[1].used_range().unwrap().1.0,0); } #[test] fn persistence_round_trip_and_corruption_protection() { let dir=std::env::temp_dir().join(format!("budget-tests-{}",std::process::id())); let _=std::fs::remove_dir_all(&dir); let (mut store,wb)=persistence::Store::open(&dir).unwrap(); assert!(wb.is_none()); store.save(&budget::workbook()).unwrap(); assert!(persistence::Store::open(&dir).unwrap().1.is_some()); std::fs::write(dir.join("budget-state-v1.json"),b"bad").unwrap(); assert!(persistence::Store::open(&dir).is_err()); assert_eq!(std::fs::read(dir.join("budget-state-v1.json")).unwrap(),b"bad"); std::fs::remove_dir_all(dir).unwrap(); } #[test] fn missing_or_partial_budgets_are_not_compared() { let mut wb=budget::workbook(); history::capture(&mut wb,&history::Period::new("2026-01-01","2026-01-31").unwrap()).unwrap(); for (start,end) in [("2026-01-01","2026-02-28"),("2026-01-15","2026-01-31")] { assert!(history::report(&wb,&history::Period::new(start,end).unwrap()).contains("comparison unavailable")); } history::capture(&mut wb,&history::Period::new("2026-02-01","2026-02-28").unwrap()).unwrap(); assert!(history::report(&wb,&history::Period::new("2026-01-01","2026-02-28").unwrap()).contains("Budget minus paid")); } #[test] fn product_rejects_currency_and_overflow() { let mut p=products::Product{id:"abc".into(),title:"Chair".into(),url:"https://example.com/p".into(),date:"2026-01-31".into(),currency:"USD".into(),unit_minor:10000,quantity:2,shipping_minor:500,section:1,item:4}; assert!(p.total().is_err()); p.currency="ZAR".into(); p.unit_minor=i64::MAX; assert!(p.total().is_err()); p.unit_minor=100; p.quantity=0; assert!(p.total().is_err()); } // A product pointed at an additional tab: section/index count into the // named rows, the Item cell is that block's slot, and the label columns // carry the block's header text. The block itself is laid out the way // the strip's "+" does it (header text, named slots, block SUM). #[test] fn product_add_targets_additional_tabs() { let mut wb = budget::workbook(); let planner = wb.sheet_index_named(budget::BUDGET_SHEET).unwrap(); let sheet = &mut wb.sheets[planner]; // Slots 0 and 2 named, slot 1 left empty: item index counts named // rows, so 1 must land on "Paint", not on the empty slot. let header_row = budget::additional_header_row(0); let slot0 = budget::additional_name_pos(0, 0); let slot2 = budget::additional_name_pos(0, 2); sheet.set_input((header_row, 0), &budget::additional_header(0)); sheet.set_input( budget::additional_subtotal_pos(0), &format!("=SUM(B{}:B{})", slot0.0 + 1, slot0.0 + budget::ADD_SLOTS), ); sheet.set_input(slot0, "Decor"); sheet.set_input(slot2, "Paint"); drop(sheet); wb.set_input_at(planner, budget::additional_amount_pos(0, 0), "450"); let section = budget::layout().sections.len(); let p = products::Product{id:"abc".into(),title:"Paint".into(),url:"https://example.com/p".into(),date:"2026-01-31".into(),currency:"ZAR".into(),unit_minor:20000,quantity:1,shipping_minor:0,section,item:1}; assert!(products::add(&mut wb, &p).unwrap()); // The named slot's cell gained the product total as a formula. assert_eq!(wb.input_at(planner, budget::additional_amount_pos(0, 2)), "=(0)+200.00"); // The Products row spells the block header, not a static group name. let i = wb.sheet_index_named("Products").unwrap(); let r = wb.sheets[i].used_range().unwrap().1.0; assert_eq!(wb.input_at(i, (r, 9)), "ADDITIONAL EXPENSES 1"); assert_eq!(wb.input_at(i, (r, 10)), "Paint"); } // The period snapshot lists every additional tab's named slots as line // items of their block, empty amounts and all (only a missing *name* // drops a row, because that is what makes a slot a line item). #[test] fn history_capture_snapshots_additional_tabs() { let mut wb = budget::workbook(); let planner = wb.sheet_index_named(budget::BUDGET_SHEET).unwrap(); let header_row = budget::additional_header_row(0); wb.sheets[planner].set_input((header_row, 0), &budget::additional_header(0)); let slot0 = budget::additional_name_pos(0, 0); let slot2 = budget::additional_name_pos(0, 2); wb.sheets[planner].set_input(slot0, "Decor"); wb.sheets[planner].set_input(slot2, "Paint"); wb.set_input_at(planner, budget::additional_amount_pos(0, 0), "450"); let p = history::Period::new("2026-01-01", "2026-01-31").unwrap(); history::capture(&mut wb, &p).unwrap(); let idx = wb.sheet_index_named("Period budgets").unwrap(); let lines: Vec = (1..=wb.sheets[idx].used_range().unwrap().1.0) .map(|r| (0..5).map(|c| wb.input_at(idx, (r, c))).collect::>().join(",")) .collect(); assert!(lines.iter().any(|l| l == "2026-01-01,2026-01-31,ADDITIONAL EXPENSES 1,Decor,450"), "{lines:?}"); assert!(lines.iter().any(|l| l == "2026-01-01,2026-01-31,ADDITIONAL EXPENSES 1,Paint,0"), "{lines:?}"); // Only named slots are line items; the empty middle slot has no row. assert!(!lines.iter().any(|l| l.contains("ADDITIONAL EXPENSES 1,,")), "{lines:?}"); }