From 72f7bcece96c7640fea4faea639568a3dc92b905 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Sat, 7 Nov 2026 10:24:53 -0700 Subject: [PATCH] test(core): golden file for prepaid and deferred amortization The two hand-verified amortization fixtures in schedules.rs now have one golden covering both plans, their rounding, their remaining balance and the statements they move. Recomputing them by hand from rules.md B-16 (monthly_even: each period total/months to the cent, the last period taking the remainder) before serializing: - Prepaid: 1,200.00 of insurance from 2026-03-01 over 12 months is 100.00 a month exactly. March-May are posted, so 300.00 is expensed and 900.00 remains; the balance sheet carries 900.00 of prepaid. - Deferred: 1,000.00 from 2026-01-01 over 3 months is 333.33, 333.33 and 333.34 (the last period absorbs the 0.01 remainder), summing to 1,000.00 exactly. All three are posted, so 1,000.00 is revenue and nothing remains a liability. Every period, cumulative and remaining figure agrees with the hand worksheet in plan/golden-worksheets/amortization-plans.md. No existing golden or assertion changes. Co-Authored-By: DeepSeek V4.1 Flash --- .../tests/golden/amortization_plans.json | 159 ++++++++++++++++++ crates/ledger-core/tests/schedules.rs | 91 ++++++++++ plan/golden-worksheets/amortization-plans.md | 100 +++++++++++ 3 files changed, 350 insertions(+) create mode 100644 crates/ledger-core/tests/golden/amortization_plans.json create mode 100644 plan/golden-worksheets/amortization-plans.md diff --git a/crates/ledger-core/tests/golden/amortization_plans.json b/crates/ledger-core/tests/golden/amortization_plans.json new file mode 100644 index 00000000..d359922a --- /dev/null +++ b/crates/ledger-core/tests/golden/amortization_plans.json @@ -0,0 +1,159 @@ +{ + "deferred": { + "balance_sheet": { + "as_of": "2026-03-31", + "assets": [ + { + "amount": "1000", + "code": "1000", + "name": "Cash" + } + ], + "current_earnings": "1000.00", + "equity": [], + "liabilities": [], + "total_assets": "1000", + "total_equity": "1000.00", + "total_liabilities": "0" + }, + "currency": "USD", + "description": "Quarterly plan, Acme", + "kind": "deferred_revenue", + "months": 3, + "plan": [ + { + "amount": "333.33", + "date": "2026-01-31" + }, + { + "amount": "333.33", + "date": "2026-02-28" + }, + { + "amount": "333.34", + "date": "2026-03-31" + } + ], + "posted": [ + "2026-01-31", + "2026-02-28", + "2026-03-31" + ], + "profit_and_loss": { + "end": "2026-03-31", + "expenses": [], + "revenue": [ + { + "amount": "1000.00", + "code": "4000", + "name": "Subscription Revenue" + } + ], + "start": "2026-01-01", + "total_expenses": "0", + "total_revenue": "1000.00" + }, + "remaining": "0", + "start": "2026-01-01", + "total": "1000" + }, + "prepaid": { + "balance_sheet": { + "as_of": "2026-05-31", + "assets": [ + { + "amount": "-1200", + "code": "1000", + "name": "Cash" + }, + { + "amount": "900", + "code": "1300", + "name": "Prepaid Expenses" + } + ], + "current_earnings": "-300", + "equity": [], + "liabilities": [], + "total_assets": "-300", + "total_equity": "-300", + "total_liabilities": "0" + }, + "currency": "USD", + "description": "Liability insurance", + "kind": "prepaid_expense", + "months": 12, + "plan": [ + { + "amount": "100", + "date": "2026-03-31" + }, + { + "amount": "100", + "date": "2026-04-30" + }, + { + "amount": "100", + "date": "2026-05-31" + }, + { + "amount": "100", + "date": "2026-06-30" + }, + { + "amount": "100", + "date": "2026-07-31" + }, + { + "amount": "100", + "date": "2026-08-31" + }, + { + "amount": "100", + "date": "2026-09-30" + }, + { + "amount": "100", + "date": "2026-10-31" + }, + { + "amount": "100", + "date": "2026-11-30" + }, + { + "amount": "100", + "date": "2026-12-31" + }, + { + "amount": "100", + "date": "2027-01-31" + }, + { + "amount": "100", + "date": "2027-02-28" + } + ], + "posted": [ + "2026-03-31", + "2026-04-30", + "2026-05-31" + ], + "profit_and_loss": { + "end": "2026-05-31", + "expenses": [ + { + "amount": "300", + "code": "6300", + "name": "Insurance" + } + ], + "revenue": [], + "start": "2026-03-01", + "total_expenses": "300", + "total_revenue": "0" + }, + "remaining": "900", + "start": "2026-03-01", + "total": "1200" + } +} diff --git a/crates/ledger-core/tests/schedules.rs b/crates/ledger-core/tests/schedules.rs index 44fc8480..b4cb0855 100644 --- a/crates/ledger-core/tests/schedules.rs +++ b/crates/ledger-core/tests/schedules.rs @@ -811,3 +811,94 @@ fn a_month_that_rounds_to_nothing_is_never_a_posting() { assert_eq!(s.next().unwrap(), Some(d(3, 31))); assert_eq!(s.remaining().unwrap(), dec("0.01")); } + +/// The plan as JSON: one object per period, in plan order. +fn plan_values(plan: Vec<(NaiveDate, Decimal)>) -> Vec { + plan.into_iter() + .map(|(date, amount)| serde_json::json!({"date": date, "amount": amount})) + .collect() +} + +/// Golden (oracle Layer 1): the two hand-verified amortization fixtures, +/// every period, rounding remainder and running balance recomputed in +/// plan/golden-worksheets/amortization-plans.md before this test was run. +/// A year of prepaid insurance at 100.00 a month (three months posted, +/// 900.00 left) and three months of deferred revenue at 333.33, 333.33, +/// 333.34 (the last period takes the rounding, so the three sum to +/// 1,000.00 exactly: rules.md B-16). +#[test] +fn golden_amortization_plans() { + let mut b = books(); + post(&mut b.l, d(3, 1), b.prepaid, b.cash, "1200"); + let prepaid_id = + b.l.register_schedule(NewSchedule { + description: "Liability insurance".into(), + kind: ScheduleKind::PrepaidExpense, + total: usd("1200"), + start: d(3, 1), + months: 12, + balance_account: b.prepaid, + pl_account: b.insurance, + convention: ledger_core::ScheduleConvention::MonthlyEven, + }) + .unwrap(); + for (month, _) in b.l.get_schedule(prepaid_id).unwrap().due(d(5, 31)).unwrap() { + b.l.post_schedule_month(prepaid_id, month).unwrap(); + } + let prepaid_schedule = b.l.get_schedule(prepaid_id).unwrap().clone(); + let prepaid = serde_json::json!({ + "description": prepaid_schedule.description, + "kind": prepaid_schedule.kind, + "total": prepaid_schedule.total.amount, + "currency": prepaid_schedule.total.currency.code(), + "start": prepaid_schedule.start, + "months": prepaid_schedule.months, + "posted": prepaid_schedule.posted, + "plan": plan_values(prepaid_schedule.plan().unwrap()), + "remaining": prepaid_schedule.remaining().unwrap(), + "profit_and_loss": profit_and_loss_for_period(&b.l, d(3, 1), d(5, 31), None).unwrap(), + "balance_sheet": balance_sheet_as_of(&b.l, d(5, 31)).unwrap(), + }); + + let mut b = books(); + post(&mut b.l, d(1, 1), b.cash, b.deferred, "1000"); + let deferred_id = + b.l.register_schedule(NewSchedule { + description: "Quarterly plan, Acme".into(), + kind: ScheduleKind::DeferredRevenue, + total: usd("1000"), + start: d(1, 1), + months: 3, + balance_account: b.deferred, + pl_account: b.revenue, + convention: ledger_core::ScheduleConvention::MonthlyEven, + }) + .unwrap(); + for (month, _) in + b.l.get_schedule(deferred_id) + .unwrap() + .due(d(12, 31)) + .unwrap() + { + b.l.post_schedule_month(deferred_id, month).unwrap(); + } + let deferred_schedule = b.l.get_schedule(deferred_id).unwrap().clone(); + let deferred = serde_json::json!({ + "description": deferred_schedule.description, + "kind": deferred_schedule.kind, + "total": deferred_schedule.total.amount, + "currency": deferred_schedule.total.currency.code(), + "start": deferred_schedule.start, + "months": deferred_schedule.months, + "posted": deferred_schedule.posted, + "plan": plan_values(deferred_schedule.plan().unwrap()), + "remaining": deferred_schedule.remaining().unwrap(), + "profit_and_loss": profit_and_loss_for_period(&b.l, d(1, 1), d(3, 31), None).unwrap(), + "balance_sheet": balance_sheet_as_of(&b.l, d(3, 31)).unwrap(), + }); + + let got = serde_json::json!({"prepaid": prepaid, "deferred": deferred}); + let want: serde_json::Value = + serde_json::from_str(include_str!("golden/amortization_plans.json")).unwrap(); + assert_eq!(got, want); +} diff --git a/plan/golden-worksheets/amortization-plans.md b/plan/golden-worksheets/amortization-plans.md new file mode 100644 index 00000000..59d3d2d5 --- /dev/null +++ b/plan/golden-worksheets/amortization-plans.md @@ -0,0 +1,100 @@ +# Golden worksheet: prepaid expense and deferred revenue amortization + +Golden: `crates/ledger-core/tests/golden/amortization_plans.json` +Test: `golden_amortization_plans` in `crates/ledger-core/tests/schedules.rs` +Fixture: the existing `books()` fixture and the two hand-verified setups in +`a_prepaid_expense_is_expensed_month_by_month` and +`deferred_revenue_is_earned_to_the_cent` (same accounts, same totals, same +conventions). +Rules: `docs/spec/rules.md` B-16 (prepaids recognized over the period +benefited, deferred revenue as it is earned; equal-months schedules, the +last month absorbing rounding), with B-15's rounding discipline, and D-37 +(monthly_even schedules start on the 1st). + +The schedules in this fixture ask explicitly for `monthly_even` (the +convention of every schedule registered before P3), so the plan is +`amortization_plan`: each period is `total ÷ months` rounded to the cent, +and the last period is `total − each × (months − 1)`, so the plan sums to +the total exactly. + +## Prepaid expense: a year of insurance from 2026-03-01 + +Entry: 2026-03-01, Dr 1300 Prepaid Expenses 1,200 / Cr 1000 Cash 1,200. + +Plan, 12 equal months (monthly_even; the 1st is the start): + +- Each of the first 11 months: 1,200 ÷ 12 = **100.00**. +- Last month: 1,200 − 100.00 × 11 = 1,200 − 1,100 = **100.00** (no + remainder here; the division is exact). + +| # | Period | Amount | Cumulative | Remaining | +|---|---|---|---|---| +| 1 | 2026-03-31 | 100 | 100 | 1,100 | +| 2 | 2026-04-30 | 100 | 200 | 1,000 | +| 3 | 2026-05-31 | 100 | 300 | 900 | +| 4 | 2026-06-30 | 100 | 400 | 800 | +| 5 | 2026-07-31 | 100 | 500 | 700 | +| 6 | 2026-08-31 | 100 | 600 | 600 | +| 7 | 2026-09-30 | 100 | 700 | 500 | +| 8 | 2026-10-31 | 100 | 800 | 400 | +| 9 | 2026-11-30 | 100 | 900 | 300 | +| 10 | 2026-12-31 | 100 | 1,000 | 200 | +| 11 | 2027-01-31 | 100 | 1,100 | 100 | +| 12 | 2027-02-28 | 100 | 1,200 | 0 | + +The fixture posts the first three months (March, April, May). Remaining = +1,200 − 300 = **900**. `posted` is those three month ends. + +Postings: each month Dr 6300 Insurance / Cr 1300 Prepaid Expenses, 100. + +Profit and loss, 2026-03-01 through 2026-05-31: insurance expense +100 + 100 + 100 = **300**; revenue 0. + +Balance sheet at 2026-05-31: +- Cash: 0 − 1,200 = **−1,200** (the fixture pays the premium from cash and + has no other entries). +- Prepaid Expenses: 1,200 − 300 = **900**. +- Total assets: −1,200 + 900 = **−300**. +- Liabilities: **0**. +- Current earnings: revenue 0 − expense 300 = **−300**; equity **−300**. +- −300 = 0 + (−300): balanced. + +## Deferred revenue: three months from 2026-01-01 + +Entry: 2026-01-01, Dr 1000 Cash 1,000 / Cr 2400 Deferred Revenue 1,000. + +Plan, 3 equal months: + +- Each of the first 2 months: 1,000 ÷ 3 = 333.333… → **333.33**. +- Last month: 1,000 − 333.33 × 2 = 1,000 − 666.66 = **333.34** (the + rounding remainder, 0.01, is absorbed by the last period). +- Sum: 333.33 + 333.33 + 333.34 = **1,000.00** exactly. + +| # | Period | Amount | Cumulative | Remaining | +|---|---|---|---|---| +| 1 | 2026-01-31 | 333.33 | 333.33 | 666.67 | +| 2 | 2026-02-28 | 333.33 | 666.66 | 333.34 | +| 3 | 2026-03-31 | 333.34 | 1,000.00 | 0 | + +All three months are posted, so remaining = **0**. Postings: each month +Dr 2400 Deferred Revenue / Cr 4000 Subscription Revenue. + +Profit and loss, 2026-01-01 through 2026-03-31: revenue 333.33 + 333.33 + +333.34 = **1,000.00**; expenses 0. + +Balance sheet at 2026-03-31: +- Cash **1,000**. +- Liabilities: the deferred revenue is fully earned → **0**. +- Current earnings **1,000.00**; equity **1,000.00**. +- 1,000 = 0 + 1,000.00: balanced. + +## Notes on the serialized figures + +- The decimal strings keep the arithmetic scale: the prepaid plan divides + exactly, so its amounts print as `"100"`, `"300"`, `"900"`, `"1200"`; + the deferred plan carries cents, so its amounts print as `"333.33"`, + `"333.34"`, `"1000.00"`, and its remaining as `"0"`. +- `total` is the schedule's `Money.amount`; `currency` is the code (USD), + not `Money`'s three-byte serde form, so the golden stays readable. +- `posted` is the schedule's posted month ends; `plan` is every period, + posted or not, in order.