check / check (push) Canceled after 0s
ledger-config: parse gets a sourced spec. A fiscal year starts on the first of a month (IRS Pub. 538); tax_classification is read by whole words (Treas. Reg. 301.7701-3, Schedule C instructions; "business corp" no longer reads as an S corp, "corporation or S corporation" is refused); duplicate or blank members and shared capital accounts are refused; a negative or non-grammatical receipt threshold is refused; codes trimmed. tests/spec_config.rs (65 spec-blind tests, 14 properties), fuzz target fuzz_config_parse (weekly). Mutation: 55 of 55 viable caught (from 25/38). The receipt policy moves to ledger-config, with one loader (effective_receipt_policy) and one decimal grammar, so the server and the viewer enforce and show the same rule (the viewer had its own: only expense-like debits, a silent $25 default, and no config.json). ledger-mcp: a module spec (MCP 2025-06-18 tools, JSON-RPC 2.0, the operating contract). Amounts in are exactly -?digits[.digits]; a number that is one account's code and another's id (vendors, classes alike) is refused, not guessed; the threshold is validated at startup. Fixed: add_line with a bad class journaled the line before refusing; profit_and_loss_by_class ignored `class`; aging buckets said "0". A wire in front of rmcp answers malformed JSON with -32700 and invalid requests with -32600 and their id (rmcp dropped the first, omitted the id). tests/spec_mcp.rs (26 spec-blind tests: every tool's result against its own outputSchema, a random-call state machine checking the journal is untouched by every refusal, amount grammar, previews, the receipt policy). ledger-gui: money, accounting, and dollars never round (they rounded to cents, banker's style); export file names avoid Windows device names, trailing dots and spaces, empty names, and 255-byte overflow; specs on attention, history, fuzzy search, and axis ticks. ledger-core: the lifetime profit_and_loss is renamed unclosed_earnings (docs/testing.md #3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GWVo7x3UHw5kwCUaWxiejX
388 lines
16 KiB
Rust
388 lines
16 KiB
Rust
//! Property tests for the ledger engine: invariants that must hold for
|
|
//! *every* input, not just the hand-picked examples in `invariants.rs`.
|
|
//!
|
|
//! These are the properties mutation testing will try to break: if a
|
|
//! mutant survives the example tests but breaks a property, the property
|
|
//! catches it.
|
|
|
|
use chrono::NaiveDate;
|
|
use ledger_core::{
|
|
AccountType, Currency, Ledger, Money, NewAsset, Side, accumulated_depreciation, book_value,
|
|
depreciation_schedule,
|
|
reports::{
|
|
balance_sheet, balance_sheet_as_of, profit_and_loss_for_period, trial_balance,
|
|
unclosed_earnings,
|
|
},
|
|
};
|
|
use proptest::prelude::*;
|
|
use rust_decimal::Decimal;
|
|
|
|
fn arb_account_type() -> impl Strategy<Value = AccountType> {
|
|
prop_oneof![
|
|
Just(AccountType::Asset),
|
|
Just(AccountType::Liability),
|
|
Just(AccountType::Equity),
|
|
Just(AccountType::Revenue),
|
|
Just(AccountType::Expense),
|
|
]
|
|
}
|
|
|
|
/// A random chart: 2..=6 accounts with unique codes, no placeholders.
|
|
fn arb_chart() -> impl Strategy<Value = Vec<(String, String, AccountType)>> {
|
|
prop::collection::vec((1u32..9999u32, arb_account_type()), 2..=6).prop_map(|v| {
|
|
v.into_iter()
|
|
.enumerate()
|
|
.map(|(i, (_, t))| (format!("{:04}", 1000 + i), format!("Account {i}"), t))
|
|
.collect()
|
|
})
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
struct GenEntry {
|
|
lines: Vec<(u64, Side, Decimal)>,
|
|
date: NaiveDate,
|
|
}
|
|
|
|
/// A random *balanced* entry over `num_accounts` (1-based ids): 2..=4
|
|
/// random lines plus a plug line for the imbalance. Amounts are whole
|
|
/// cents, so the plug is always exact.
|
|
fn arb_entry(num_accounts: usize) -> impl Strategy<Value = GenEntry> {
|
|
(
|
|
prop::collection::vec(
|
|
(
|
|
1u64..=(num_accounts as u64),
|
|
prop_oneof![Just(Side::Debit), Just(Side::Credit)],
|
|
1u32..1_000_000u32,
|
|
),
|
|
2..=4,
|
|
),
|
|
1u32..=12u32,
|
|
1u32..=28u32,
|
|
)
|
|
.prop_map(|(raw, month, day)| {
|
|
let mut lines: Vec<(u64, Side, Decimal)> = raw
|
|
.into_iter()
|
|
.map(|(a, s, cents)| (a, s, Decimal::new(cents as i64, 2)))
|
|
.collect();
|
|
let net: Decimal = lines
|
|
.iter()
|
|
.map(|(_, s, amt)| match s {
|
|
Side::Debit => *amt,
|
|
Side::Credit => -*amt,
|
|
})
|
|
.sum();
|
|
if net != Decimal::ZERO {
|
|
let (side, amt) = if net > Decimal::ZERO {
|
|
(Side::Credit, net)
|
|
} else {
|
|
(Side::Debit, -net)
|
|
};
|
|
lines.push((1, side, amt));
|
|
}
|
|
GenEntry {
|
|
lines,
|
|
date: NaiveDate::from_ymd_opt(2026, month, day).unwrap(),
|
|
}
|
|
})
|
|
}
|
|
|
|
fn arb_ops() -> impl Strategy<Value = (Vec<(String, String, AccountType)>, Vec<GenEntry>)> {
|
|
arb_chart().prop_flat_map(|chart| {
|
|
let n = chart.len();
|
|
(Just(chart), prop::collection::vec(arb_entry(n), 0..=10))
|
|
})
|
|
}
|
|
|
|
fn build_ledger(
|
|
chart: &[(String, String, AccountType)],
|
|
entries: &[GenEntry],
|
|
) -> (Ledger, Vec<u64>) {
|
|
let mut ledger = Ledger::new(Currency::USD);
|
|
for (code, name, typ) in chart {
|
|
ledger.add_account(code, name, *typ, None, false).unwrap();
|
|
}
|
|
let mut ids = Vec::new();
|
|
for e in entries {
|
|
let id = ledger.create_entry(e.date, "prop");
|
|
for (a, s, amt) in &e.lines {
|
|
ledger
|
|
.add_line(id, *a, *s, Money::new(*amt, Currency::USD))
|
|
.unwrap();
|
|
}
|
|
ledger.post_entry(id).unwrap();
|
|
ids.push(id);
|
|
}
|
|
(ledger, ids)
|
|
}
|
|
|
|
proptest! {
|
|
#![proptest_config(ProptestConfig::with_cases(256))]
|
|
|
|
/// The trial balance always balances, no matter the operations.
|
|
#[test]
|
|
fn prop_trial_balance_balances((chart, entries) in arb_ops()) {
|
|
let (ledger, _) = build_ledger(&chart, &entries);
|
|
let tb = trial_balance(&ledger);
|
|
prop_assert_eq!(tb.total_debits, tb.total_credits);
|
|
let debit_sum: Decimal = tb.lines.iter().map(|l| l.debit).sum();
|
|
let credit_sum: Decimal = tb.lines.iter().map(|l| l.credit).sum();
|
|
prop_assert_eq!(debit_sum, tb.total_debits);
|
|
prop_assert_eq!(credit_sum, tb.total_credits);
|
|
}
|
|
|
|
/// The accounting equation holds for every ledger state:
|
|
/// assets == liabilities + equity (with current earnings folded in).
|
|
#[test]
|
|
fn prop_balance_sheet_balances((chart, entries) in arb_ops()) {
|
|
let (ledger, _) = build_ledger(&chart, &entries);
|
|
let bs = balance_sheet(&ledger).unwrap();
|
|
prop_assert_eq!(bs.assets, bs.liabilities + bs.equity);
|
|
}
|
|
|
|
/// Period P&Ls partition the year: split anywhere, the two halves sum
|
|
/// to the whole, and the whole is the life-of-ledger P&L.
|
|
#[test]
|
|
fn prop_period_pnl_partitions((chart, entries) in arb_ops(), split in 0u32..365) {
|
|
let (ledger, _) = build_ledger(&chart, &entries);
|
|
let jan1 = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
|
|
let dec31 = NaiveDate::from_ymd_opt(2026, 12, 31).unwrap();
|
|
let cut = jan1 + chrono::Days::new(split as u64);
|
|
let first = profit_and_loss_for_period(&ledger, jan1, cut, None).unwrap();
|
|
let rest = if cut < dec31 {
|
|
Some(profit_and_loss_for_period(&ledger, cut.succ_opt().unwrap(), dec31, None).unwrap())
|
|
} else {
|
|
None
|
|
};
|
|
let whole = profit_and_loss_for_period(&ledger, jan1, dec31, None).unwrap();
|
|
let (r2, e2) = rest.map_or((Decimal::ZERO, Decimal::ZERO), |p| (p.total_revenue, p.total_expenses));
|
|
prop_assert_eq!(first.total_revenue + r2, whole.total_revenue);
|
|
prop_assert_eq!(first.total_expenses + e2, whole.total_expenses);
|
|
let all = unclosed_earnings(&ledger).unwrap();
|
|
prop_assert_eq!((whole.total_revenue, whole.total_expenses), (all.revenue, all.expenses));
|
|
}
|
|
|
|
/// The accounting equation holds as of every date, and a period's net
|
|
/// income is exactly the change in current earnings across it.
|
|
#[test]
|
|
fn prop_balance_sheet_as_of_balances((chart, entries) in arb_ops(), a in 0u32..365, b in 0u32..365) {
|
|
let (ledger, _) = build_ledger(&chart, &entries);
|
|
let jan1 = NaiveDate::from_ymd_opt(2026, 1, 1).unwrap();
|
|
let (lo, hi) = (a.min(b), a.max(b));
|
|
let start = jan1 + chrono::Days::new(lo as u64);
|
|
let end = jan1 + chrono::Days::new(hi as u64);
|
|
let before = balance_sheet_as_of(&ledger, start.pred_opt().unwrap()).unwrap();
|
|
let after = balance_sheet_as_of(&ledger, end).unwrap();
|
|
prop_assert!(before.balanced());
|
|
prop_assert!(after.balanced());
|
|
let pnl = profit_and_loss_for_period(&ledger, start, end, None).unwrap();
|
|
prop_assert_eq!(after.current_earnings - before.current_earnings, pnl.net_income());
|
|
// Past the last entry, as-of agrees with the life-of-ledger sheet.
|
|
let all = balance_sheet(&ledger).unwrap();
|
|
let last = balance_sheet_as_of(&ledger, NaiveDate::from_ymd_opt(2026, 12, 31).unwrap()).unwrap();
|
|
prop_assert_eq!(
|
|
(last.total_assets, last.total_liabilities, last.total_equity),
|
|
(all.assets, all.liabilities, all.equity)
|
|
);
|
|
}
|
|
|
|
/// Every posted entry balances on its own, exactly.
|
|
#[test]
|
|
fn prop_posted_entries_balance((chart, entries) in arb_ops()) {
|
|
let (ledger, ids) = build_ledger(&chart, &entries);
|
|
for id in ids {
|
|
let e = ledger.get_entry(id).unwrap();
|
|
let debits: Decimal = e.lines().iter()
|
|
.filter(|l| l.side == Side::Debit)
|
|
.map(|l| l.amount.amount).sum();
|
|
let credits: Decimal = e.lines().iter()
|
|
.filter(|l| l.side == Side::Credit)
|
|
.map(|l| l.amount.amount).sum();
|
|
prop_assert_eq!(debits, credits);
|
|
}
|
|
}
|
|
|
|
/// Reversing every entry returns the trial balance to zero:
|
|
/// reversals are exact inverses.
|
|
#[test]
|
|
fn prop_reversals_restore_zero((chart, entries) in arb_ops()) {
|
|
let (mut ledger, ids) = build_ledger(&chart, &entries);
|
|
for id in &ids {
|
|
ledger.reverse_entry(*id, NaiveDate::from_ymd_opt(2026, 12, 31).unwrap(), "reversal").unwrap();
|
|
}
|
|
let tb = trial_balance(&ledger);
|
|
prop_assert_eq!(tb.total_debits, Decimal::ZERO);
|
|
prop_assert_eq!(tb.total_credits, Decimal::ZERO);
|
|
}
|
|
|
|
/// Posted entries are immutable: serializing them before and after
|
|
/// further operations yields identical bytes.
|
|
#[test]
|
|
fn prop_posted_entries_immutable((chart, entries) in arb_ops(), extra in prop::collection::vec(arb_entry(2), 0..=5)) {
|
|
prop_assume!(chart.len() >= 2);
|
|
let (mut ledger, ids) = build_ledger(&chart, &entries);
|
|
let before: Vec<String> = ids.iter().map(|id| {
|
|
serde_json::to_string(&serde_json::to_value(ledger.get_entry(*id).unwrap()).unwrap()).unwrap()
|
|
}).collect();
|
|
// More operations: entries sized for a 2-account chart would break
|
|
// on bigger charts, so rebuild them against this chart.
|
|
let n = chart.len();
|
|
let extra_entries: Vec<GenEntry> = extra.into_iter().map(|mut e| {
|
|
for (a, _, _) in e.lines.iter_mut() {
|
|
*a = (*a % n as u64) + 1;
|
|
}
|
|
// Rebalance after remapping accounts (net may have changed).
|
|
let net: Decimal = e.lines.iter().map(|(_, s, amt)| match s {
|
|
Side::Debit => *amt, Side::Credit => -*amt,
|
|
}).sum();
|
|
if net != Decimal::ZERO {
|
|
let (side, amt) = if net > Decimal::ZERO { (Side::Credit, net) } else { (Side::Debit, -net) };
|
|
e.lines.push((1, side, amt));
|
|
}
|
|
e
|
|
}).collect();
|
|
for e in &extra_entries {
|
|
let id = ledger.create_entry(e.date, "extra");
|
|
for (a, s, amt) in &e.lines {
|
|
ledger.add_line(id, *a, *s, Money::new(*amt, Currency::USD)).unwrap();
|
|
}
|
|
ledger.post_entry(id).unwrap();
|
|
}
|
|
for id in &ids {
|
|
ledger.reverse_entry(*id, NaiveDate::from_ymd_opt(2026, 12, 31).unwrap(), "rev").unwrap();
|
|
}
|
|
let after: Vec<String> = ids.iter().map(|id| {
|
|
serde_json::to_string(&serde_json::to_value(ledger.get_entry(*id).unwrap()).unwrap()).unwrap()
|
|
}).collect();
|
|
prop_assert_eq!(before, after);
|
|
}
|
|
|
|
/// A journal entry survives a JSON round-trip unchanged.
|
|
#[test]
|
|
fn prop_entry_json_roundtrip((chart, entries) in arb_ops()) {
|
|
let (ledger, ids) = build_ledger(&chart, &entries);
|
|
for id in ids {
|
|
let e = ledger.get_entry(id).unwrap();
|
|
let v = serde_json::to_value(e).unwrap();
|
|
let s = serde_json::to_string(&v).unwrap();
|
|
let v2: serde_json::Value = serde_json::from_str(&s).unwrap();
|
|
prop_assert_eq!(v, v2);
|
|
}
|
|
}
|
|
|
|
/// Depreciation schedules are exact: the lifetime total is precisely
|
|
/// cost minus salvage, book value plus accumulated always equals cost,
|
|
/// and amounts are never negative.
|
|
#[test]
|
|
fn prop_depreciation_schedule_exact(
|
|
cost_cents in 100u64..10_000_000u64,
|
|
salvage_cents in 0u64..5_000_000u64,
|
|
life in 1u32..360u32,
|
|
y in 2000i32..2027i32,
|
|
m in 1u32..=12u32,
|
|
d in 1u32..=28u32,
|
|
) {
|
|
let cost_cents = cost_cents.max(salvage_cents + 1);
|
|
let cost = Decimal::new(cost_cents as i64, 2);
|
|
let salvage = Decimal::new(salvage_cents as i64, 2);
|
|
let mut ledger = Ledger::new(Currency::USD);
|
|
let ca = ledger.add_account("1500", "Equipment", AccountType::Asset, None, false).unwrap();
|
|
let aa = ledger.add_account("1501", "Accum", AccountType::Asset, None, false).unwrap();
|
|
let ex = ledger.add_account("6100", "Depr", AccountType::Expense, None, false).unwrap();
|
|
let id = ledger.register_asset(NewAsset {
|
|
description: "prop".to_string(),
|
|
purchase_date: NaiveDate::from_ymd_opt(y, m, d).unwrap(),
|
|
cost: Money::new(cost, Currency::USD),
|
|
salvage_value: Money::new(salvage, Currency::USD),
|
|
useful_life_months: life,
|
|
cost_account: ca,
|
|
accum_depr_account: aa,
|
|
expense_account: ex,
|
|
}).unwrap();
|
|
let asset = ledger.get_asset(id).unwrap();
|
|
let far = asset.purchase_date.checked_add_months(chrono::Months::new(life + 24)).unwrap();
|
|
let sched = depreciation_schedule(asset, far);
|
|
// The lifetime total is exactly cost minus salvage: never a penny off.
|
|
let total: Decimal = sched.iter().map(|l| l.amount).sum();
|
|
prop_assert_eq!(total, cost - salvage);
|
|
for l in &sched {
|
|
prop_assert!(l.amount >= Decimal::ZERO);
|
|
prop_assert_eq!(l.book_value + l.accumulated, cost);
|
|
}
|
|
// Accumulated is monotone non-decreasing.
|
|
for w in sched.windows(2) {
|
|
prop_assert!(w[1].accumulated >= w[0].accumulated);
|
|
}
|
|
// Book value at any date matches the schedule.
|
|
if let Some(last) = sched.last() {
|
|
prop_assert_eq!(book_value(asset, far), last.book_value);
|
|
prop_assert_eq!(accumulated_depreciation(asset, far), last.accumulated);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Member splits (periods::allocate): conservation, a cent of each exact
|
|
// share, fairness, and sign symmetry. Rank 2: laws, not recomputation.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/// 1 to 5 positive shares summing to exactly 1 (the last takes the rest).
|
|
fn arb_shares() -> impl Strategy<Value = Vec<Decimal>> {
|
|
prop::collection::vec(1u32..1000, 1..=5).prop_map(|weights| {
|
|
let total: u32 = weights.iter().sum();
|
|
let mut shares: Vec<Decimal> = weights
|
|
.iter()
|
|
.map(|w| Decimal::from(*w) / Decimal::from(total))
|
|
.collect();
|
|
let rest: Decimal = shares[..shares.len() - 1].iter().sum();
|
|
*shares.last_mut().unwrap() = Decimal::ONE - rest;
|
|
shares
|
|
})
|
|
}
|
|
|
|
proptest! {
|
|
#![proptest_config(ProptestConfig::with_cases(2000))]
|
|
|
|
#[test]
|
|
fn prop_split_parts_sum_to_the_whole_within_a_cent_of_each_share(
|
|
cents in -10_000_000_000i64..10_000_000_000, shares in arb_shares(),
|
|
) {
|
|
let amount = Decimal::new(cents, 2);
|
|
let parts = ledger_core::periods::allocate(amount, &shares);
|
|
prop_assert_eq!(parts.len(), shares.len());
|
|
prop_assert_eq!(parts.iter().copied().sum::<Decimal>(), amount);
|
|
for (p, s) in parts.iter().zip(&shares) {
|
|
prop_assert!((*p - amount * s).abs() < Decimal::new(1, 2), "{p} vs share {s}");
|
|
prop_assert!(p.scale() <= 2 || p.normalize().scale() <= 2, "{p} is not whole cents");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn prop_split_never_rounds_up_a_smaller_remainder_over_a_larger(
|
|
cents in 0i64..10_000_000_000, shares in arb_shares(),
|
|
) {
|
|
let amount = Decimal::new(cents, 2);
|
|
let parts = ledger_core::periods::allocate(amount, &shares);
|
|
let exact: Vec<Decimal> = shares.iter().map(|s| amount * s).collect();
|
|
let floor = |e: &Decimal| e.round_dp_with_strategy(2, rust_decimal::RoundingStrategy::ToZero);
|
|
for i in 0..parts.len() {
|
|
for j in 0..parts.len() {
|
|
let (ri, rj) = (exact[i] - floor(&exact[i]), exact[j] - floor(&exact[j]));
|
|
let (up_i, up_j) = (parts[i] > floor(&exact[i]), parts[j] > floor(&exact[j]));
|
|
if ri > rj {
|
|
prop_assert!(up_i || !up_j, "member {j} rounded up over member {i}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn prop_split_of_a_loss_is_the_negated_split_of_the_gain(
|
|
cents in 0i64..10_000_000_000, shares in arb_shares(),
|
|
) {
|
|
let amount = Decimal::new(cents, 2);
|
|
let gain = ledger_core::periods::allocate(amount, &shares);
|
|
let loss = ledger_core::periods::allocate(-amount, &shares);
|
|
prop_assert_eq!(loss, gain.iter().map(|p| -*p).collect::<Vec<_>>());
|
|
}
|
|
}
|