Files
wanderbooks/crates/ledger-gui/tests/view_model.rs
T
swaitsandClaude Fable 5.2 d4c0036599 fix(store,gui,package): every reader that derives facts reads the committed journal (P15 T1 review)
A failed call's ids are handed out again (nothing it wrote counted), so a
reader of the raw journal pins dead lines on whatever takes the id next: a
failed books.create_bill for entry 42 then a manual entry 42 showed as a
Bill. One accessor, calls::committed (lines outside a call and of calls
that closed ok, the frame left out), now feeds the viewer's books (entry
events and origin, asset trails, party events), the package feed attention
reads, and the adopt preview's settings check. The journal page and
journal_history keep every line and say in words of each line of a call
that failed, never finished or is still running that it did not count.

A call's closing line is now bounded (ANSWER_CAP 4 KiB: above it, the
answer's SHA-256 and length), a call that wrote no line leaves no line (its
key and answer live in a bounded in-memory table), and the journaled
argument digest is an HMAC under the books' own call key, so two requests
that differ only in a tax id are two requests and nothing journaled lets a
guess of one be tested.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 10:09:17 -07:00

1103 lines
41 KiB
Rust

//! The viewer's numbers are the books' numbers: the view model
//! (books::Books, books::Kpis) against ledger-core, computed here
//! independently, on random books.
use chrono::{Datelike, Days, Months, NaiveDate};
use ledger_core::reports::{balance_sheet_as_of, profit_and_loss_for_period, trial_balance_as_of};
use ledger_core::{AccountType, Currency, DocumentLine, Money, Side};
use ledger_gui::{Source, data, fmt};
use ledger_store::Store;
use proptest::prelude::*;
use rust_decimal::{Decimal, RoundingStrategy};
use std::path::PathBuf;
const CHART: [(&str, &str, AccountType); 9] = [
("1000", "Checking", AccountType::Asset),
("1010", "Savings", AccountType::Asset),
("1200", "Accounts Receivable", AccountType::Asset),
("1500", "Equipment", AccountType::Asset),
("2000", "Accounts Payable", AccountType::Liability),
("2100", "Due to owners", AccountType::Liability),
("3000", "Capital", AccountType::Equity),
("4000", "Revenue", AccountType::Revenue),
("6000", "Supplies", AccountType::Expense),
];
const CASH: [&str; 2] = ["1000", "1010"];
#[derive(Clone, Debug)]
struct Plan {
today: NaiveDate,
fy_month: u32,
/// (days before today, debit index, credit index, cents)
entries: Vec<(u64, usize, usize, i64)>,
}
fn plan() -> impl Strategy<Value = Plan> {
(
// Month ends and leap days included.
prop_oneof![
Just(NaiveDate::from_ymd_opt(2026, 3, 31).unwrap()),
Just(NaiveDate::from_ymd_opt(2028, 2, 29).unwrap()),
Just(NaiveDate::from_ymd_opt(2026, 1, 1).unwrap()),
(0u64..900).prop_map(|d| NaiveDate::from_ymd_opt(2025, 1, 1).unwrap() + Days::new(d)),
],
1u32..=12,
proptest::collection::vec((0u64..200, 0usize..9, 0usize..9, 1i64..5_000_000), 0..25),
)
.prop_map(|(today, fy_month, entries)| Plan {
today,
fy_month,
entries: entries.into_iter().filter(|e| e.1 != e.2).collect(),
})
}
/// A test's dir, removed when the test ends, pass or panic.
struct TestDir(PathBuf);
impl std::ops::Deref for TestDir {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl Drop for TestDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}
fn dir(tag: &str) -> TestDir {
let d = std::env::temp_dir().join(format!("ledger-gui-vm-{}-{tag}", std::process::id()));
let _ = std::fs::remove_dir_all(&d);
std::fs::create_dir_all(&d).unwrap();
TestDir(d)
}
/// The books, where they are (kept while the guard lives), and their ledger.
fn build(plan: &Plan, tag: &str) -> (TestDir, Source, ledger_core::Ledger) {
let d = dir(tag);
let mut s = Store::create(&d.join("books-data"), Currency::USD).unwrap();
let ids: Vec<_> = CHART
.iter()
.map(|(c, n, t)| s.add_account(c, n, *t, None, false).unwrap())
.collect();
// As replay posts them: random lines land on receivables and payables
// by hand, as books an earlier version wrote can hold them (the
// control guard, BS-arap-2, refuses them as new work), and the
// overview must still read them.
ledger_core::parties::replaying(|| {
for &(back, dr, cr, cents) in &plan.entries {
let e = s.create_entry(plan.today - Days::new(back), "x").unwrap();
let m = Money::new(Decimal::new(cents, 2), Currency::USD);
s.add_line(e, ids[dr], Side::Debit, m).unwrap();
s.add_line(e, ids[cr], Side::Credit, m).unwrap();
s.post_entry(e).unwrap();
}
});
journal_settings(
&mut s,
&format!(
"[company]\nname = \"Oracle Co\"\nfiscal_year_start = \"{:02}-01\"\n[books]\ncash_accounts = [\"1000\", \"1010\"]\nreceipt_threshold = \"25.00\"\n",
plan.fy_month
),
);
let ledger = s.ledger().clone();
let source = Source {
data_dir: d.join("books-data"),
today: Some(plan.today),
};
(d, source, ledger)
}
/// An account's normal-side balance at the end of `date`, from the trial
/// balance.
fn balance(l: &ledger_core::Ledger, code: &str, date: NaiveDate) -> Decimal {
let t = l.chart().find_by_code(code).unwrap().account_type;
let tb = trial_balance_as_of(l, date);
let net = tb
.lines
.iter()
.find(|x| x.code == code)
.map(|x| x.debit - x.credit)
.unwrap_or_default();
match t {
AccountType::Asset | AccountType::Expense => net,
_ => -net,
}
}
fn cash(l: &ledger_core::Ledger, date: NaiveDate) -> Decimal {
CASH.iter().map(|c| balance(l, c, date)).sum()
}
fn first_of(d: NaiveDate) -> NaiveDate {
NaiveDate::from_ymd_opt(d.year(), d.month(), 1).unwrap()
}
proptest! {
#![proptest_config(ProptestConfig { cases: 48, ..ProptestConfig::default() })]
/// Kpis INVARIANTS 1-7 and the period helpers, against ledger-core.
#[test]
fn the_overview_numbers_are_the_books_numbers(p in plan()) {
let (_dir, source, l) = build(&p, "kpi");
let b = data::load_once(&source).unwrap();
let k = &b.kpis;
let t = p.today;
let m0 = first_of(t);
prop_assert_eq!(b.month_start(), m0);
prop_assert_eq!(k.cash, cash(&l, t));
prop_assert_eq!(k.cash_30d_ago, cash(&l, t - Days::new(30)));
prop_assert_eq!(k.liabilities, balance_sheet_as_of(&l, t).unwrap().total_liabilities);
prop_assert_eq!(k.receivables, balance(&l, "1200", t));
let pl = |s, e| profit_and_loss_for_period(&l, s, e, None).unwrap();
let mtd = pl(m0, t);
prop_assert_eq!(k.revenue_mtd, mtd.total_revenue);
prop_assert_eq!(k.expenses_mtd, mtd.total_expenses);
let prev_start = m0 - Months::new(1);
let prev_last = m0 - Days::new(1);
let same = NaiveDate::from_ymd_opt(prev_start.year(), prev_start.month(), t.day())
.unwrap_or(prev_last);
let prev = pl(prev_start, same);
prop_assert_eq!(k.revenue_prev_month, prev.total_revenue);
prop_assert_eq!(k.expenses_prev_month, prev.total_expenses);
// Fiscal year: its first day is the last fiscal-start first-of-month
// on or before today.
let mut fy = NaiveDate::from_ymd_opt(t.year(), p.fy_month, 1).unwrap();
if fy > t {
fy = fy - Months::new(12);
}
prop_assert_eq!(b.fiscal_ytd(), (fy, t));
// One default period everywhere (BS-period-3): what a report or
// register opens on is the fiscal year to date, the server's
// resolver's own answer for no dates, and party totals count this
// calendar year.
prop_assert_eq!(b.default_period(), (fy, t));
prop_assert_eq!(b.periods().statement(None, None), Ok((fy, t)));
prop_assert_eq!(ledger_gui::pages::reports::Range::default().dates(&b), (fy, t));
prop_assert_eq!(
ledger_gui::pages::accounts::RegisterRange::default().dates(&b),
(fy, t)
);
let jan1 = NaiveDate::from_ymd_opt(t.year(), 1, 1).unwrap();
prop_assert_eq!(
b.party_span(ledger_gui::books::PartyPeriod::ThisYear).0,
jan1
);
let ytd = pl(fy, t);
prop_assert_eq!(k.net_ytd, ytd.total_revenue - ytd.total_expenses);
let first = p.entries.iter().map(|e| t - Days::new(e.0)).min();
prop_assert_eq!(k.first_entry, first);
// Burn: the three full months before m0, averaged over those the
// books cover.
let three = pl(m0 - Months::new(3), prev_last);
let loss = three.total_expenses - three.total_revenue;
let covered = match first {
None => 0,
Some(f) => (0..3)
.filter(|i| first_of(m0 - Months::new(i + 1)) >= first_of(f))
.count() as i64,
};
let burn = (loss > Decimal::ZERO && covered > 0).then(|| {
(loss / Decimal::from(covered))
.round_dp_with_strategy(2, RoundingStrategy::MidpointAwayFromZero)
});
prop_assert_eq!(k.monthly_burn, burn);
// Trailing months: n months ending with this one.
for n in 1..=12u32 {
prop_assert_eq!(b.trailing_months(n), (m0 - Months::new(n - 1), t));
}
// Cash history: each point is the cash balance that day, dates
// strictly rising, through the last day cash moved.
let h = b.cash_history();
for w in h.windows(2) {
prop_assert!(w[0].0 < w[1].0);
}
for (d, v) in &h {
prop_assert_eq!(*v, cash(&l, *d), "cash on {}", d);
}
// ... and it isn't empty when a cash account moved: its last point
// is the last day one did (a transfer between them included).
let cash_idx = [0usize, 1];
let last_move = p
.entries
.iter()
.filter(|e| cash_idx.contains(&e.1) || cash_idx.contains(&e.2))
.map(|e| t - Days::new(e.0))
.max();
prop_assert_eq!(h.last().map(|x| x.0), last_move);
// Names and labels.
for (code, name, _) in CHART {
let id = l.chart().find_by_code(code).unwrap().id;
prop_assert_eq!(b.account_label(id), format!("{code} {name}"));
prop_assert_eq!(b.account_name(id), name);
}
prop_assert_eq!(b.company_name(), "Oracle Co");
let events = std::fs::read_to_string(source.data_dir.join("journal.jsonl"))
.unwrap()
.lines()
.count() as u64;
prop_assert_eq!(b.seq(), events);
prop_assert_eq!(b.receipt_rule(), "over the $25.00 receipt threshold");
let _ = std::fs::remove_dir_all(source.data_dir.parent().unwrap());
}
}
/// Where each entry came from, and who it was with, from the journal.
#[test]
fn origins_and_parties() {
let d = dir("origin");
let today = NaiveDate::from_ymd_opt(2026, 5, 20).unwrap();
let mut s = Store::create(&d.join("books-data"), Currency::USD).unwrap();
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let sup = s
.add_account("6000", "Supplies", AccountType::Expense, None, false)
.unwrap();
let cust = s.add_customer("Acme Buyer").unwrap();
let vend = s.add_vendor("Paper Co").unwrap();
let line = |a: i64| {
vec![DocumentLine {
description: "x".into(),
amount: Decimal::new(a, 2),
class_id: None,
account: None,
quantity: None,
unit_price: None,
}]
};
let inv = s
.create_invoice(cust, today, today + Days::new(30), line(10_000))
.unwrap();
s.create_bill(vend, today, today + Days::new(30), line(5_000))
.unwrap();
let paid = s
.receive_payment(inv, today, Decimal::new(10_000, 2), cash)
.unwrap();
let e = s.create_entry(today, "Paper").unwrap();
let m = Money::new(Decimal::new(1_000, 2), Currency::USD);
s.add_line(e, sup, Side::Debit, m).unwrap();
s.add_line(e, cash, Side::Credit, m).unwrap();
s.post_entry(e).unwrap();
let (blob, _, _) = s
.attach_receipt(Some(e), "r.pdf", "application/pdf", b"%PDF receipt", None)
.unwrap();
let rev = s.reverse_entry(e, today, "undo").unwrap();
journal_settings(&mut s, "[books]\nreceipt_threshold = \"0.00\"\n");
drop(s);
let source = Source {
data_dir: d.join("books-data"),
today: Some(today),
};
let b = data::load_once(&source).unwrap();
let by_origin = |o: &str| b.entries.iter().filter(|x| x.origin == o).count();
assert_eq!(by_origin("Invoice"), 1);
assert_eq!(by_origin("Bill"), 1);
assert_eq!(by_origin("Customer payment"), 1);
assert_eq!(by_origin("Reversal"), 1);
assert_eq!(b.entry(paid).unwrap().party.as_deref(), Some("Acme Buyer"));
let bill_entry = b.entries.iter().find(|x| x.origin == "Bill").unwrap();
assert_eq!(bill_entry.party.as_deref(), Some("Paper Co"));
assert_eq!(b.entry(e).unwrap().reversed_by, Some(rev));
assert_eq!(b.entry(rev).unwrap().reversal_of, Some(e));
// Every entry's journal events name it.
for x in &b.entries {
assert!(!x.events.is_empty(), "entry {} has no events", x.id);
}
assert_eq!(b.receipt_rule(), "every entry needs a receipt");
// The receipt's bytes are where the blob root says.
let a = b.attachments.iter().find(|a| a.id == blob).unwrap();
let path = b
.blob_root()
.join(&a.blob_sha[..2])
.join(&a.blob_sha[2..4])
.join(&a.blob_sha);
assert_eq!(std::fs::read(path).unwrap(), b"%PDF receipt");
assert_eq!(b.integrity.files_ok, 1);
assert!(b.integrity.file_failures.is_empty());
}
/// fmt helpers the viewer shows: percent change and tax forms.
#[test]
fn percent_change_and_tax_forms() {
let d = |s: &str| s.parse::<Decimal>().unwrap();
assert_eq!(fmt::pct_change(d("0"), d("5")), None);
assert_eq!(fmt::pct_change(d("100"), d("150")), Some(d("50.0")));
assert_eq!(fmt::pct_change(d("200"), d("150")), Some(d("-25.0")));
// K-U9: no percent of a negative base (it misleads); Decimal, never f64.
assert_eq!(fmt::pct_change(d("-100"), d("-50")), None);
use ledger_core::TaxForm::*;
for (f, number) in [
(ScheduleC, "Schedule C"),
(Form1065, "1065"),
(Form1120S, "1120-S"),
(Form1120, "1120"),
] {
assert!(fmt::tax_form(f).contains(number), "{f:?}");
}
assert_eq!(fmt::compact(d("-1234")), "\u{2212}$1.2k");
}
fn store_at(tag: &str) -> (TestDir, Store) {
let d = dir(tag);
let s = Store::create(&d.join("books-data"), Currency::USD).unwrap();
(d, s)
}
/// The settings `toml` says, journaled into `s` as the server adopts a
/// settings file: the viewer reads settings only from the journal.
fn journal_settings(s: &mut Store, toml: &str) {
let import = ledger_config::codec::import(toml, None, s.ledger().settings(), "USD").unwrap();
s.adopt_settings(
&import.changes,
&ledger_core::settings::Source {
decided_by: "the test".into(),
decided_on: NaiveDate::from_ymd_opt(2026, 1, 1).unwrap(),
citation: None,
note: None,
},
)
.unwrap();
}
/// The books in `d`, with the settings `toml` says journaled (none when
/// it's empty: the defaults).
fn source(d: &std::path::Path, toml: &str, today: NaiveDate) -> Source {
if !toml.is_empty() {
journal_settings(&mut Store::open(&d.join("books-data")).unwrap(), toml);
}
Source {
data_dir: d.join("books-data"),
today: Some(today),
}
}
fn post(s: &mut Store, date: NaiveDate, dr: u64, cr: u64, cents: i64) -> u64 {
let e = s.create_entry(date, "x").unwrap();
let m = Money::new(Decimal::new(cents, 2), Currency::USD);
s.add_line(e, dr, Side::Debit, m).unwrap();
s.add_line(e, cr, Side::Credit, m).unwrap();
s.post_entry(e).unwrap();
e
}
/// Kpis INVARIANTS 4 and 6 on fixed dates: the prior month runs to the
/// same day; burn averages over only the months the books cover.
#[test]
fn comparisons_and_burn_on_fixed_dates() {
let (d, mut s) = store_at("burn");
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let rev = s
.add_account("4000", "Sales", AccountType::Revenue, None, false)
.unwrap();
let exp = s
.add_account("6000", "Rent", AccountType::Expense, None, false)
.unwrap();
let today = NaiveDate::from_ymd_opt(2026, 5, 10).unwrap();
// The books start in March: burn covers March and April, not February.
post(
&mut s,
NaiveDate::from_ymd_opt(2026, 3, 3).unwrap(),
exp,
cash,
30_000,
);
post(
&mut s,
NaiveDate::from_ymd_opt(2026, 4, 10).unwrap(),
exp,
cash,
10_000,
);
post(
&mut s,
NaiveDate::from_ymd_opt(2026, 4, 11).unwrap(),
cash,
rev,
7_000,
); // after the same day
post(
&mut s,
NaiveDate::from_ymd_opt(2026, 5, 2).unwrap(),
cash,
rev,
1_000,
);
drop(s);
let b = data::load_once(&source(&d, "[books]\ncash_accounts = [\"1000\"]\n", today)).unwrap();
let k = &b.kpis;
assert_eq!(k.revenue_mtd, Decimal::new(1_000, 2));
assert_eq!(k.expenses_prev_month, Decimal::new(10_000, 2), "April 1-10");
assert_eq!(
k.revenue_prev_month,
Decimal::ZERO,
"April 11 is after the same day"
);
// Feb-Apr loss: 300 + 100 - 70 = 330 over the 2 months covered.
assert_eq!(k.monthly_burn, Some(Decimal::new(16_500, 2)));
// A profitable stretch has no burn; books starting this month have none.
let (d2, mut s2) = store_at("burn-new");
let c2 = s2
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let e2 = s2
.add_account("6000", "Rent", AccountType::Expense, None, false)
.unwrap();
post(
&mut s2,
NaiveDate::from_ymd_opt(2026, 5, 1).unwrap(),
e2,
c2,
5_000,
);
drop(s2);
let b2 = data::load_once(&source(&d2, "", today)).unwrap();
assert_eq!(b2.kpis.monthly_burn, None);
// Three full months of loss: divided by 3, half away from zero.
let (d3, mut s3) = store_at("burn-three");
let c3 = s3
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let e3 = s3
.add_account("6000", "Rent", AccountType::Expense, None, false)
.unwrap();
post(
&mut s3,
NaiveDate::from_ymd_opt(2025, 12, 1).unwrap(),
e3,
c3,
1,
);
post(
&mut s3,
NaiveDate::from_ymd_opt(2026, 3, 1).unwrap(),
e3,
c3,
1_000,
);
drop(s3);
let b3 = data::load_once(&source(&d3, "", today)).unwrap();
// 10.00 / 3 = 3.333... -> 3.33
assert_eq!(b3.kpis.monthly_burn, Some(Decimal::new(333, 2)));
// Books covering only last month: one month, not two.
let (d4, mut s4) = store_at("burn-one");
let c4 = s4
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let e4 = s4
.add_account("6000", "Rent", AccountType::Expense, None, false)
.unwrap();
post(
&mut s4,
NaiveDate::from_ymd_opt(2026, 4, 20).unwrap(),
e4,
c4,
9_000,
);
drop(s4);
let b4 = data::load_once(&source(&d4, "", today)).unwrap();
assert_eq!(b4.kpis.monthly_burn, Some(Decimal::new(9_000, 2)));
// Breaking even burns nothing.
let (d5, mut s5) = store_at("burn-even");
let c5 = s5
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let r5 = s5
.add_account("4000", "Sales", AccountType::Revenue, None, false)
.unwrap();
let e5 = s5
.add_account("6000", "Rent", AccountType::Expense, None, false)
.unwrap();
post(
&mut s5,
NaiveDate::from_ymd_opt(2026, 3, 5).unwrap(),
e5,
c5,
4_000,
);
post(
&mut s5,
NaiveDate::from_ymd_opt(2026, 3, 6).unwrap(),
c5,
r5,
4_000,
);
drop(s5);
let b5 = data::load_once(&source(&d5, "", today)).unwrap();
assert_eq!(b5.kpis.monthly_burn, None);
}
/// The view model's indexes: cash accounts by name when the settings name
/// none, card accounts, each entry's accounts (debits first, once each),
/// and what's new since the last refresh.
#[test]
fn indexes_cash_cards_accounts_and_fresh() {
let (d, mut s) = store_at("index");
let checking = s
.add_account("1000", "Business Checking", AccountType::Asset, None, false)
.unwrap();
let savings = s
.add_account("1010", "Savings", AccountType::Asset, None, false)
.unwrap();
let group = s
.add_account("1090", "Cash accounts", AccountType::Asset, None, true)
.unwrap();
let equip = s
.add_account("1500", "Equipment", AccountType::Asset, None, false)
.unwrap();
let card = s
.add_account(
"2050",
"Chase Ink Card",
AccountType::Liability,
None,
false,
)
.unwrap();
let cardish = s
.add_account("1600", "Card reader", AccountType::Asset, None, false)
.unwrap();
let rent = s
.add_account("6000", "Rent", AccountType::Expense, None, false)
.unwrap();
let today = NaiveDate::from_ymd_opt(2026, 6, 1).unwrap();
// Two debits to one account, then two credits.
let e = s.create_entry(today, "split").unwrap();
let m = |c| Money::new(Decimal::new(c, 2), Currency::USD);
s.add_line(e, rent, Side::Debit, m(100)).unwrap();
s.add_line(e, rent, Side::Debit, m(200)).unwrap();
s.add_line(e, card, Side::Credit, m(250)).unwrap();
s.add_line(e, checking, Side::Credit, m(50)).unwrap();
s.post_entry(e).unwrap();
drop(s);
let src = source(&d, "", today);
let b = data::load_once(&src).unwrap();
let mut cash = b.cash_accounts.clone();
cash.sort();
assert_eq!(
cash,
vec![checking, savings],
"by name, postable assets only"
);
let _ = (group, equip, cardish);
assert_eq!(b.card_accounts, vec![card], "liabilities named card");
assert_eq!(b.entry(e).unwrap().accounts, vec![rent, card, checking]);
assert!(b.fresh.is_empty(), "a first load marks nothing new");
// A new entry after a refresh is the only fresh one.
let seq = b.seq();
let mut s = Store::open(&d.join("books-data")).unwrap();
let e2 = post(&mut s, today, rent, checking, 999);
drop(s);
let snap = ledger_store::Snapshot::load(&src.data_dir).unwrap();
let settings = ledger_config::codec::books_settings(snap.ledger().settings(), "USD");
let mut verified = std::collections::HashSet::new();
let integrity = ledger_gui::books::Integrity::check(&snap, &mut verified);
let b2 = ledger_gui::books::Books::build(&snap, settings, today, Some(seq), integrity).unwrap();
assert_eq!(b2.fresh, [e2].into_iter().collect());
}
/// Integrity counts every stored file, read once: a second check with the
/// same memory counts them again without rereading; a damaged one fails.
#[test]
fn integrity_counts_files_once_verified() {
let (d, mut s) = store_at("integrity");
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let rent = s
.add_account("6000", "Rent", AccountType::Expense, None, false)
.unwrap();
let e = post(
&mut s,
NaiveDate::from_ymd_opt(2026, 1, 2).unwrap(),
rent,
cash,
100,
);
s.attach_receipt(Some(e), "a.pdf", "application/pdf", b"one", None)
.unwrap();
s.attach_receipt(Some(e), "b.pdf", "application/pdf", b"two", None)
.unwrap();
drop(s);
let snap = ledger_store::Snapshot::load(&d.join("books-data")).unwrap();
let mut verified = std::collections::HashSet::new();
let first = ledger_gui::books::Integrity::check(&snap, &mut verified);
assert_eq!((first.files_ok, first.file_failures.len()), (2, 0));
let again = ledger_gui::books::Integrity::check(&snap, &mut verified);
assert_eq!((again.files_ok, again.file_failures.len()), (2, 0));
}
/// Origins and parties for the rest of the journal's kinds: the invoice
/// itself shows its customer, a bill payment its vendor, and a payroll
/// tax remittance is labeled as one (so the receipt rule for hand-made
/// entries doesn't flag it).
#[test]
fn more_origins_and_parties() {
let (d, mut s) = store_at("origin2");
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let today = NaiveDate::from_ymd_opt(2026, 7, 15).unwrap();
let cust = s.add_customer("Acme Buyer").unwrap();
let vend = s.add_vendor("Paper Co").unwrap();
let line = |a: i64| {
vec![DocumentLine {
description: "x".into(),
amount: Decimal::new(a, 2),
class_id: None,
account: None,
quantity: None,
unit_price: None,
}]
};
s.create_invoice(cust, today, today + Days::new(30), line(10_000))
.unwrap();
let bill = s
.create_bill(vend, today, today + Days::new(30), line(5_000))
.unwrap();
let paid = s
.pay_bill(bill, today, Decimal::new(5_000, 2), cash)
.unwrap();
s.run_payroll(
today,
vec![ledger_core::PayrollLine {
employee_name: "Pat".into(),
employee: None,
gross_pay: Decimal::new(100_000, 2),
federal_withholding: Decimal::new(10_000, 2),
state_withholding: Decimal::new(3_000, 2),
ss_employee: Decimal::new(6_200, 2),
medicare_employee: Decimal::new(1_450, 2),
ss_employer: Decimal::new(6_200, 2),
medicare_employer: Decimal::new(1_450, 2),
other_deductions: Decimal::ZERO,
}],
cash,
)
.unwrap();
let remit = s.remit_payroll_taxes(today, cash).unwrap();
drop(s);
let b = data::load_once(&source(
&d,
"[books]\nreceipt_threshold = \"0.00\"\n",
today,
))
.unwrap();
let inv = b.entries.iter().find(|x| x.origin == "Invoice").unwrap();
assert_eq!(inv.party.as_deref(), Some("Acme Buyer"));
let pay = b.entry(paid).unwrap();
assert_eq!(pay.origin, "Bill payment");
assert_eq!(pay.party.as_deref(), Some("Paper Co"));
assert_eq!(
b.entries.iter().filter(|x| x.origin == "Payroll").count(),
1
);
let r = b.entry(remit).unwrap();
assert_eq!(r.origin, "Payroll taxes");
assert!(!r.missing_receipt, "a remittance isn't a hand-made entry");
}
/// Defect 8, repros 1.4.2 and 1.4.3 (BS-ledger-11): Needs attention lists
/// every revenue and expense account without a tax category, with
/// activity or not: one never used, one used only last fiscal year; never
/// a header, an account with a category, or an asset. Info under warn,
/// Todo under refuse.
#[test]
fn needs_attention_flags_every_uncategorized_pl_account_defect_8() {
let today = NaiveDate::from_ymd_opt(2026, 10, 20).unwrap();
let d = dir("attention-tax");
let mut s = Store::create(&d.join("books-data"), Currency::USD).unwrap();
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let ops = s
.add_account("6000", "Operating", AccountType::Expense, None, true)
.unwrap();
s.add_account("6150", "Software", AccountType::Expense, Some(ops), false)
.unwrap();
let hosting = s
.add_account("6160", "Hosting", AccountType::Expense, Some(ops), false)
.unwrap();
s.add_account_with_tax(
"4000",
"Sales",
AccountType::Revenue,
None,
false,
Some(("gross_receipts", None)),
)
.unwrap();
let e = s
.create_entry(NaiveDate::from_ymd_opt(2025, 6, 1).unwrap(), "last year")
.unwrap();
let m = Money::new(Decimal::new(1_200, 2), Currency::USD);
s.add_line(e, hosting, Side::Debit, m).unwrap();
s.add_line(e, cash, Side::Credit, m).unwrap();
s.post_entry(e).unwrap();
journal_settings(
&mut s,
"[company]\nname = \"Fernhill Labs LLC\"\n[books]\ncash_accounts = [\"1000\"]\n",
);
let source = Source {
data_dir: d.join("books-data"),
today: Some(today),
};
let item = |b: &ledger_gui::books::Books| {
ledger_gui::attention::find(b)
.into_iter()
.find(|a| a.title.contains("without a tax category"))
};
let b = data::load_once(&source).unwrap();
let a = item(&b).expect("listed");
assert_eq!(a.title, "2 accounts without a tax category");
assert!(
a.detail.starts_with("6150 Software, 6160 Hosting. Each revenue and expense account maps to a line of the return"),
"{}",
a.detail
);
assert_eq!(a.severity, ledger_gui::books::Severity::Info);
assert!(
a.prompt.contains("books.tax_categories") && a.prompt.contains("books.set_tax_category")
);
// Under refuse, a to-do.
s.change_settings(
&[ledger_core::settings::Change {
key: "policy.require_tax_category".into(),
value: Some(serde_json::json!("refuse")),
}],
&ledger_core::settings::Source {
decided_by: "Tobin Harlow".into(),
decided_on: today,
citation: None,
note: None,
},
)
.unwrap();
let b = data::load_once(&source).unwrap();
assert_eq!(
item(&b).unwrap().severity,
ledger_gui::books::Severity::Todo
);
// Categorized: no item at all.
for code in ["6150", "6160"] {
let id = s.ledger().chart().find_by_code(code).unwrap().id;
s.set_account_tax(id, Some("other_deductions"), None)
.unwrap();
}
let b = data::load_once(&source).unwrap();
assert!(item(&b).is_none());
}
/// "Show all dates" is every date (P3 settled in design, BS-period-3):
/// the register's and the reports' all-dates range runs from the first
/// posted entry through the later of today and the last, so an entry
/// dated after today is never left out of it (as party totals' "All time"
/// already ends); with no such entry it ends today.
#[test]
fn all_dates_holds_an_entry_dated_after_today() {
use ledger_gui::pages::accounts::RegisterRange;
use ledger_gui::pages::reports::Range;
let today = NaiveDate::from_ymd_opt(2026, 10, 20).unwrap();
let d = dir("all-dates-future");
let mut s = Store::create(&d.join("books-data"), Currency::USD).unwrap();
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let capital = s
.add_account("3000", "Capital", AccountType::Equity, None, false)
.unwrap();
let source = Source {
data_dir: d.join("books-data"),
today: Some(today),
};
let post = |s: &mut Store, day: NaiveDate| {
let e = s.create_entry(day, "x").unwrap();
let m = Money::new(Decimal::new(500, 2), Currency::USD);
s.add_line(e, cash, Side::Debit, m).unwrap();
s.add_line(e, capital, Side::Credit, m).unwrap();
s.post_entry(e).unwrap();
};
let first = NaiveDate::from_ymd_opt(2025, 3, 15).unwrap();
post(&mut s, first);
let b = data::load_once(&source).unwrap();
assert_eq!(b.all_dates(), (first, today));
let later = NaiveDate::from_ymd_opt(2026, 12, 1).unwrap();
post(&mut s, later);
let b = data::load_once(&source).unwrap();
assert_eq!(b.kpis.last_entry, Some(later));
assert_eq!(RegisterRange::All.dates(&b), (first, later));
assert_eq!(Range::AllTime.dates(&b), (first, later));
}
/// A write-off this calendar year is one Needs-attention item, "Write-off
/// awaiting the CPA's note", through the viewer and through the server's
/// own read of the books alike (plan P4b S7; the one function in
/// ledger-package::attention).
#[test]
fn a_write_off_is_flagged_for_the_cpa() {
let d = dir("write-off");
let today = NaiveDate::from_ymd_opt(2026, 6, 30).unwrap();
let mut s = Store::create(&d.join("books-data"), Currency::USD).unwrap();
s.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let cust = s.add_customer("Ridgeway Co").unwrap();
let inv = s
.create_invoice(
cust,
NaiveDate::from_ymd_opt(2026, 2, 1).unwrap(),
NaiveDate::from_ymd_opt(2026, 3, 1).unwrap(),
vec![DocumentLine {
description: "Survey".into(),
amount: Decimal::new(10_000, 2),
class_id: None,
account: None,
quantity: None,
unit_price: None,
}],
)
.unwrap();
let (entry, _) = s
.write_off_invoice(
inv,
NaiveDate::from_ymd_opt(2026, 3, 15).unwrap(),
Decimal::new(10_000, 2),
"collection exhausted",
Some(ledger_store::WriteOffEvidence::File {
file_name: "collection-letter.pdf",
mime_type: "application/pdf",
bytes: b"%PDF-1.4\nFinal demand\n%%EOF\n",
description: Some("Collection attempts"),
}),
)
.unwrap();
// The server's list: the same function, over the Store.
let settings = s.ledger().settings();
let company = settings.company("USD");
let (cash, financing, investing) = (
settings.cash_accounts(),
settings.financing_accounts(),
settings.investing_accounts(),
);
let server = ledger_package::find(
&s,
today,
&ledger_package::Settings {
company: &company,
cash_accounts: &cash,
financing_accounts: &financing,
investing_accounts: &investing,
},
);
let want = format!("Write-off awaiting the CPA's note: Invoice {inv}");
let said = server.iter().find(|a| a.title == want).unwrap_or_else(|| {
panic!(
"the server's list has no {want:?}: {:?}",
server.iter().map(|a| &a.title).collect::<Vec<_>>()
)
});
assert_eq!(said.severity, ledger_package::Severity::Todo);
assert!(said.prompt.contains("wanderbooks"), "{}", said.prompt);
assert!(said.detail.contains("100.00"), "{}", said.detail);
drop(s);
// The viewer's list, over its snapshot, has the same item.
let books = data::load_once(&Source {
data_dir: d.join("books-data"),
today: Some(today),
})
.unwrap();
let shown = books
.attention
.iter()
.find(|a| a.title == want)
.unwrap_or_else(|| {
panic!(
"the viewer shows no {want:?}: {:?}",
books.attention.iter().map(|a| &a.title).collect::<Vec<_>>()
)
});
assert_eq!(shown.target, Some(ledger_gui::nav::Route::Documents));
// The viewer reads the same figures from the journal it replayed.
let st = books.ledger.invoice_state(inv, today).unwrap().unwrap();
assert_eq!(st.status, ledger_core::DocStatus::WrittenOff);
assert!(books.ledger.get_entry(entry).is_some());
}
/// Plan P4b S4.5: the viewer, which replays the journal itself, holds a
/// numbered invoice exactly as the store that wrote it does: its number,
/// terms, bill-to snapshot, the customer's PO and memo, and its lines'
/// quantity and unit price. What the page shows is read from those, so
/// the viewer and the server cannot disagree about them.
#[test]
fn the_viewer_holds_a_numbered_invoice_as_the_store_does() {
use ledger_core::arap::InvoiceFields;
use ledger_core::{PartySnapshot, Terms};
let d = dir("numbered-parity");
let today = NaiveDate::from_ymd_opt(2026, 3, 15).unwrap();
let mut s = Store::create(&d.join("books-data"), Currency::USD).unwrap();
s.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let cust = s.add_customer("Harbor Coffee").unwrap();
let inv = s
.create_invoice_with(
cust,
NaiveDate::from_ymd_opt(2026, 3, 1).unwrap(),
NaiveDate::from_ymd_opt(2026, 3, 31).unwrap(),
vec![DocumentLine {
description: "Beans".into(),
amount: Decimal::new(9_999, 2),
class_id: None,
account: None,
quantity: Some(Decimal::new(3, 0)),
unit_price: Some(Decimal::new(3_333, 2)),
}],
InvoiceFields {
number: Some("INV-0007".into()),
terms: Some(Terms::parse("2/10 net 30").unwrap()),
bill_to: Some(PartySnapshot {
name: "Harbor Coffee Roasters, Inc.".into(),
address: Some("455 Wharf Street".into()),
email: Some("ap@harbor.example".into()),
phone: None,
contact: Some("Ana Reyes".into()),
billing_address: None,
shipping_address: Some("Dock 4".into()),
billing_email: None,
}),
seller: None,
customer_ref: Some("PO-4471".into()),
memo: Some("Thanks".into()),
},
)
.unwrap();
let stored = s.ledger().get_invoice(inv).unwrap().clone();
drop(s);
let books = data::load_once(&Source {
data_dir: d.join("books-data"),
today: Some(today),
})
.unwrap();
let shown = books.ledger.get_invoice(inv).unwrap();
assert_eq!(shown, &stored);
assert_eq!(shown.number.as_deref(), Some("INV-0007"));
assert_eq!(shown.terms.as_ref().unwrap().describe(), "2/10 net 30");
assert_eq!(shown.lines[0].quantity, Some(Decimal::new(3, 0)));
}
/// A call that failed after writing lines, followed by a call that takes
/// the ids it handed out again (nothing it wrote counted): the viewer's
/// books, the package feed that attention reads, and the history all say
/// the dead lines are not facts, and nothing of the dead call is pinned on
/// what took its ids (WB-format-12).
#[test]
fn a_failed_calls_lines_are_not_facts_when_its_ids_are_used_again() {
use ledger_store::calls::{CallEnd, CallId, Standing};
let (d, mut s) = store_at("failed-call");
let today = NaiveDate::from_ymd_opt(2026, 5, 20).unwrap();
let cash = s
.add_account("1000", "Checking", AccountType::Asset, None, false)
.unwrap();
let cap = s
.add_account("3000", "Capital", AccountType::Equity, None, false)
.unwrap();
let id = |key: &str, tool: &str| CallId {
token: "t".into(),
tool: tool.into(),
key: key.into(),
args: "a".repeat(64),
};
let lines = |amount: i64| {
vec![DocumentLine {
description: "x".into(),
amount: Decimal::new(amount, 2),
class_id: None,
account: None,
quantity: None,
unit_price: None,
}]
};
let before = s.journal_seq();
// A failed books.create_bill: a vendor, a bill, an entry (id 1).
s.begin_call(id("key-dead-0001", "books.create_bill"))
.unwrap();
let dead_vendor = s.add_vendor("Dead Vendor").unwrap();
s.create_bill(dead_vendor, today, today + Days::new(30), lines(5_000))
.unwrap();
s.close_call(CallEnd::Failed("the call failed after its lines"))
.unwrap();
let dead_end = s.journal_seq();
assert!(dead_end > before + 3);
// A manual entry, which takes entry id 1 again, and a vendor, which
// takes party id 1 again.
let e = s.create_entry(today, "Opening deposit").unwrap();
assert_eq!(e, 1, "the failed call's entry id is handed out again");
let m = Money::new(Decimal::new(100_000, 2), Currency::USD);
s.add_line(e, cash, Side::Debit, m).unwrap();
s.add_line(e, cap, Side::Credit, m).unwrap();
s.post_entry(e).unwrap();
let live_vendor = s.add_vendor("Real Vendor").unwrap();
assert_eq!(live_vendor, dead_vendor);
drop(s);
let source = Source {
data_dir: d.join("books-data"),
today: Some(today),
};
let b = data::load_once(&source).unwrap();
// The entry is a manual one: not a "Bill", which is what the dead
// bill.created line (for entry 1) would have made of it.
let entry = b.entry(e).unwrap();
assert_eq!(entry.origin, "Journal entry");
assert!(
entry
.events
.iter()
.all(|&i| !(before + 1..=dead_end).contains(&b.journal[i].seq)),
"an entry's events include a dead line"
);
// No fact the viewer derives holds a dead line: the committed journal
// has none of them, and none of the frame.
assert!(
b.journal
.iter()
.all(|l| !(before + 1..=dead_end).contains(&l.seq) && !l.line_type.starts_with("call."))
);
// The party that took the id again has none of the dead vendor's events.
let party = &b.parties[&live_vendor];
assert!(
party
.events
.iter()
.all(|&i| !(before + 1..=dead_end).contains(&b.journal[i].seq))
);
// The package feed attention reads is the same committed journal.
let snap = ledger_store::Snapshot::load(&d.join("books-data")).unwrap();
let fed = ledger_package::PackageBooks::journal(&snap).unwrap();
assert_eq!(fed.len(), b.journal.len());
assert!(
fed.iter()
.all(|l| !(before + 1..=dead_end).contains(&l.seq))
);
let store = Store::open(&d.join("books-data")).unwrap();
let fed = ledger_package::PackageBooks::journal(&store).unwrap();
assert_eq!(fed.len(), b.journal.len());
// The viewer's seq is the journal's head, frame lines counted.
assert_eq!(b.seq(), b.history.len() as u64);
// History shows every line, and says of the dead ones in words that
// they did not count; the viewer never names a dead line by the live
// entry that took its id.
let dead: Vec<_> = b
.history
.iter()
.filter(|(_, st)| *st == Standing::Failed)
.collect();
assert!(dead.len() >= 3);
for (line, st) in dead {
let ev = ledger_gui::pages::journal::event_for(&b, line, *st);
assert!(ev.text.contains("not counted"), "{}", ev.text);
assert!(ev.route.is_none());
}
}