merge: every report and book total goes through the kernel's checked sum (K-U4; branch t3/kernel-checked-totals, bc81b849; reviewed)

periods.rs conflicted with K-U2: the frozen replay rules delegate to
K-U4's checked twins, whose rounding now goes through K-U2's
round_places (so every rounding stays in the kernel); no figure changes.
The K-U4 note gains the review's list of bounded raw sums left, and the
caveat that the twin test checks delegation (the review's independent
20,000-case comparison found the twins identical).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Opus 5.5 committed 2026-11-08 17:09:27 -07:00
commit 57d86b1920
25 files changed
+1184 -229

No files matched your search

+71
View File
@@ -0,0 +1,71 @@
//! 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");
}