Files
wanderbooks/crates/ledger-store/tests/exact_totals.rs
T
swaitsandMuse Wonder 1.0 489360f598 refactor: rule references cite bookspec ids
Every B-n citation in crates/, docs/, deploy/, scripts/ and .github/ now names its bookspec id BS-<area>-<n> (D-68), via scripts/rewrite-rule-refs.py: the map is parsed from docs/spec/rules.md's index, rules.md-prefixed and bare forms are rewritten, sub-references (rule 11, item 7, INVARIANT 2) survive, and plan/, docs/spec/rules.md and docs/spec/claims/ are untouched. Program-output string literals that cited a rule were reverted by hand so user-visible output and its goldens are unchanged; DatedRule.catalog, metadata no output prints, now holds BS-1099-1. One SOURCE line that cited only 'R-?' now also cites BS-statement-5.

Co-Authored-By: Muse Wonder 1.0
2026-11-08 19:40:22 -07:00

72 lines
2.6 KiB
Rust

//! Documents whose lines' sum is past exact arithmetic are refused, never a
//! panic (kernel sweep unit K-U4; BS-entry-3, BS-entry-4).
//!
//! A posted entry's lines are bounded by the line cap at the moment it
//! posts, restored or new (`entry.posted` validates), so a report's sums
//! over posted lines cannot reach the edge; a document's lines are the
//! figures that can be restored "of any size", and are what this guards.
//!
//! ```text
//! GOAL: a document whose lines sum past exact arithmetic is refused with
//! a ledger error and journals nothing.
//! SOURCE: BS-entry-4 (a replayed line is accepted of any
//! size), BS-entry-3 (amounts are exact); bookspec BS-entry-3,
//! BS-entry-4.
//! ```
use chrono::NaiveDate;
use ledger_core::Currency;
use ledger_core::arap::DocumentLine;
use ledger_store::{Store, StoreError};
use rust_decimal::Decimal;
use std::path::PathBuf;
struct TestDir(PathBuf);
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn test_dir(name: &str) -> TestDir {
let dir = std::env::temp_dir().join(format!(
"wanderbooks-store-exact-totals-{name}-{}",
std::process::id()
));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
TestDir(dir)
}
fn d(y: i32, m: u32, day: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(y, m, day).unwrap()
}
/// Store::create_invoice / create_bill: lines whose sum is past exact
/// arithmetic are refused (the raw .sum() ran before the checked
/// document_total), and the books are untouched.
#[test]
fn a_document_of_enormous_lines_is_refused_not_a_panic() {
let dir = test_dir("documents");
let mut s = Store::create(&dir.0, Currency::USD).unwrap();
let customer = s.add_customer("Cedar").unwrap();
let vendor = s.add_vendor("Pine").unwrap();
let line = |amount: Decimal| DocumentLine {
description: "x".into(),
amount,
class_id: None,
account: None,
quantity: None,
unit_price: None,
};
let lines = || vec![line(Decimal::MAX), line(Decimal::MAX)];
let before = std::fs::read_to_string(dir.0.join(ledger_store::JOURNAL_FILE)).unwrap();
let invoice = s.create_invoice(customer, d(2026, 1, 2), d(2026, 2, 1), lines());
assert!(matches!(invoice, Err(StoreError::Ledger(_))), "{invoice:?}");
let bill = s.create_bill(vendor, d(2026, 1, 2), d(2026, 2, 1), lines());
assert!(matches!(bill, Err(StoreError::Ledger(_))), "{bill:?}");
let after = std::fs::read_to_string(dir.0.join(ledger_store::JOURNAL_FILE)).unwrap();
assert_eq!(before, after, "a refused document journals nothing");
}