ledger-core/tests/exact_totals.rs: the engine's sum/add/sub helpers, arap's document_total, audit::check_articulation (current_earnings + closed was a raw + that panicked for a public caller's MAX figures) and the frozen close splits' try_ twins refuse at Decimal::MAX with TooLargeToCompute, and give the infallible rules' exact result for ordinary input. ledger-store/tests/exact_totals.rs: an invoice or a bill of enormous lines is refused and journals nothing. (A posted entry's lines are capped when it posts, restored or new, so report sums over posted lines cannot reach the edge; documents are the figures that can.) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
72 lines
2.6 KiB
Rust
72 lines
2.6 KiB
Rust
//! Documents whose lines' sum is past exact arithmetic are refused, never a
|
|
//! panic (kernel sweep unit K-U4; rules.md B-21, B-28).
|
|
//!
|
|
//! 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: docs/spec/rules.md B-28 (a replayed line is accepted of any
|
|
//! size), B-21 (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");
|
|
}
|