test(core): golden file for date-range profit and loss

Add a golden for the P&L over [2026-01-01, 2026-01-31] on an extended copy
of the hand-verified January fixture: the original five entries, plus a
multi-leg equipment purchase, a January close, and a February entry. (The
original fixture has no closing entry and no entry after an as-of date, so
a copy was extended; the first five entries are unchanged.)

Hand recomputation (plan/golden-worksheets/profit-and-loss-period.md):
revenue account 4000 is credited 4,000 by the Jan 10 invoice; expense
account 6000 is debited 800 by the Jan 15 rent. The Jan 31 closing entry
(Dr revenue 4,000, Cr rent 800, Cr retained earnings 3,200) lies inside
the range but is excluded because a close moves earnings rather than
earning or spending, and the Feb 5 entry after the range is excluded too.
So revenue 4,000.00, expenses 800.00, net income 3,200.00 -- exactly the
3,200 the close moved. The golden pins those numbers.

Co-Authored-By: DeepSeek V4.1 Flash <noreply@opencode.ai>
This commit is contained in:
swaitsandDeepSeek V4.1 Flash committed 2026-11-07 09:59:01 -07:00
1 parent d075235559
commit 05c4b16385
3 files changed
+241

No files matched your search

@@ -0,0 +1,20 @@
{
"start": "2026-01-01",
"end": "2026-01-31",
"revenue": [
{
"code": "4000",
"name": "Service Revenue",
"amount": "4000"
}
],
"expenses": [
{
"code": "6000",
"name": "Rent Expense",
"amount": "800"
}
],
"total_revenue": "4000",
"total_expenses": "800"
}
+155
View File
@@ -0,0 +1,155 @@
//! Golden files for the report rows in `docs/parity.md` that had none:
//! profit and loss for a date range, balance sheet as of a date, trial
//! balance as of a date, and the general ledger by account.
//!
//! The fixture is an **extended copy** of the hand-verified
//! `fixture_company()` in `invariants.rs`. The original has no closing
//! entry, no entry dated after an as-of date, and no multi-leg entry, so it
//! cannot show the exclusions and the `-Split-` case these reports are
//! about. Entries 1–5 are the original fixture unchanged; entries 6–8 are
//! the extension. Every expected number is computed by hand in
//! `plan/golden-worksheets/`; see the worksheet named in each test.
use chrono::NaiveDate;
use ledger_core::reports::profit_and_loss_for_period;
use ledger_core::{AccountId, AccountType, Currency, Ledger, Money, Side};
use rust_decimal::Decimal;
fn date(day: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(2026, 1, day).unwrap()
}
fn february(day: u32) -> NaiveDate {
NaiveDate::from_ymd_opt(2026, 2, day).unwrap()
}
fn usd(amount: u64) -> Money {
Money::new(Decimal::from(amount), Currency::USD)
}
/// Post a balanced entry in one call. Fixtures must be valid by construction.
fn post(ledger: &mut Ledger, date: NaiveDate, description: &str, lines: &[(AccountId, Side, u64)]) {
// As replay posts it: this fixture holds hand lines on the receivable
// and payable control accounts (1200, 2000), which books an earlier
// version wrote still hold and every report must read; the control
// guard (rules.md B-38) refuses them as new work.
ledger_core::parties::replaying(|| {
let id = ledger.create_entry(date, description);
for (account, side, amount) in lines {
ledger.add_line(id, *account, *side, usd(*amount)).unwrap();
}
ledger.post_entry(id).unwrap();
})
}
/// The extended January 2026 fixture: the original five entries, then a
/// multi-leg equipment purchase (cash and payable), a January close, and a
/// February entry after every as-of date used here.
fn golden_company() -> Ledger {
let mut ledger = Ledger::new(Currency::USD);
let cash = ledger
.add_account("1000", "Cash", AccountType::Asset, None, false)
.unwrap();
let ar = ledger
.add_account(
"1200",
"Accounts Receivable",
AccountType::Asset,
None,
false,
)
.unwrap();
let equipment = ledger
.add_account("1500", "Equipment", AccountType::Asset, None, false)
.unwrap();
let ap = ledger
.add_account(
"2000",
"Accounts Payable",
AccountType::Liability,
None,
false,
)
.unwrap();
let equity = ledger
.add_account("3000", "Owner's Equity", AccountType::Equity, None, false)
.unwrap();
let revenue = ledger
.add_account("4000", "Service Revenue", AccountType::Revenue, None, false)
.unwrap();
let rent = ledger
.add_account("6000", "Rent Expense", AccountType::Expense, None, false)
.unwrap();
// 1. Owner invests $10,000 cash.
post(
&mut ledger,
date(2),
"Owner investment",
&[(cash, Side::Debit, 10_000), (equity, Side::Credit, 10_000)],
);
// 2. Buy equipment for $2,500 cash.
post(
&mut ledger,
date(5),
"Buy equipment",
&[(equipment, Side::Debit, 2_500), (cash, Side::Credit, 2_500)],
);
// 3. Bill a client $4,000.
post(
&mut ledger,
date(10),
"Invoice client",
&[(ar, Side::Debit, 4_000), (revenue, Side::Credit, 4_000)],
);
// 4. Pay January rent, $800.
post(
&mut ledger,
date(15),
"January rent",
&[(rent, Side::Debit, 800), (cash, Side::Credit, 800)],
);
// 5. Collect $1,500 of receivables.
post(
&mut ledger,
date(20),
"Client payment",
&[(cash, Side::Debit, 1_500), (ar, Side::Credit, 1_500)],
);
// 6. Buy more equipment: $1,000 cash and $500 on account (multi-leg, so
// its Equipment line's split is "-Split-").
post(
&mut ledger,
date(25),
"Buy more equipment",
&[
(equipment, Side::Debit, 1_500),
(cash, Side::Credit, 1_000),
(ap, Side::Credit, 500),
],
);
// 7. Close January: revenue 4,000 less rent 800 moves 3,200 to retained
// earnings, dated at the period end (2026-01-31).
let period = ledger.create_period("2026-01", date(1), date(31)).unwrap();
ledger.close_period(period).unwrap();
// 8. February revenue after every as-of date in these tests.
post(
&mut ledger,
february(5),
"February service",
&[(ar, Side::Debit, 600), (revenue, Side::Credit, 600)],
);
ledger
}
/// By-hand worksheet: `plan/golden-worksheets/profit-and-loss-period.md`.
#[test]
fn golden_profit_and_loss_period() {
let ledger = golden_company();
let report = profit_and_loss_for_period(&ledger, date(1), date(31), None).unwrap();
let got = serde_json::to_value(&report).unwrap();
let want: serde_json::Value =
serde_json::from_str(include_str!("golden/profit_and_loss_period.json")).unwrap();
assert_eq!(got, want);
}
@@ -0,0 +1,66 @@
# Golden worksheet — profit and loss for a date range
Report: `profit_and_loss_for_period(&ledger, 2026-01-01, 2026-01-31, None)`.
Golden: `crates/ledger-core/tests/golden/profit_and_loss_period.json`.
Test: `golden_profit_and_loss_period` in
`crates/ledger-core/tests/golden_reports.rs`.
## Fixture
The fixture is an **extended copy** of `fixture_company()` in
`tests/invariants.rs` (the hand-verified January 2026 fixture), because the
original has neither a closing entry nor an entry dated after an as-of date
and no multi-leg entry (needed for the general-ledger split). The six
original entries are unchanged; entries 6–8 are the extension:
| # | Date | Description | Lines |
|---|------|-------------|-------|
| 1 | 2026-01-02 | Owner investment | Dr 1000 Cash 10,000.00; Cr 3000 Owner's Equity 10,000.00 |
| 2 | 2026-01-05 | Buy equipment | Dr 1500 Equipment 2,500.00; Cr 1000 Cash 2,500.00 |
| 3 | 2026-01-10 | Invoice client | Dr 1200 Accounts Receivable 4,000.00; Cr 4000 Service Revenue 4,000.00 |
| 4 | 2026-01-15 | January rent | Dr 6000 Rent Expense 800.00; Cr 1000 Cash 800.00 |
| 5 | 2026-01-20 | Client payment | Dr 1000 Cash 1,500.00; Cr 1200 Accounts Receivable 1,500.00 |
| 6 | 2026-01-25 | Buy more equipment | Dr 1500 Equipment 1,500.00; Cr 1000 Cash 1,000.00; Cr 2000 Accounts Payable 500.00 |
| 7 | 2026-01-31 | Close period 2026-01 (generated by `close_period`) | Dr 4000 Service Revenue 4,000.00; Cr 6000 Rent Expense 800.00; Cr 3999 Retained Earnings 3,200.00 |
| 8 | 2026-02-05 | February service | Dr 1200 Accounts Receivable 600.00; Cr 4000 Service Revenue 600.00 |
Every entry balances: 1–5 are two-line pairs; 6 is 1,500 = 1,000 + 500; 7
is 4,000 = 800 + 3,200; 8 is 600 = 600.
Accounts: 1000 Cash (Asset), 1200 Accounts Receivable (Asset), 1500
Equipment (Asset), 2000 Accounts Payable (Liability), 3000 Owner's Equity
(Equity), 4000 Service Revenue (Revenue), 6000 Rent Expense (Expense), and
3999 Retained Earnings (Equity, created by the close).
## Hand computation
The range is 2026-01-01 through 2026-01-31, both inclusive.
- **Closing entry 7 is inside the range but excluded.** A close moves
earnings into retained earnings; it neither earns revenue nor spends
money (rules.md B-14/B-11). Counting it would make the month break even.
- **Entry 8 (2026-02-05) is outside the range and excluded.**
Revenue accounts, normal side credit, amount = credits − debits:
- 4000 Service Revenue: entry 3 credits 4,000; entry 7 is the close
(excluded); entry 8 is February (excluded). 4,000 − 0 = **4,000.00**.
Expense accounts, normal side debit, amount = debits − credits:
- 6000 Rent Expense: entry 4 debits 800; entry 7 is the close (excluded).
800 − 0 = **800.00**.
Totals:
- `total_revenue` = **4,000.00**
- `total_expenses` = **800.00**
- net income = 4,000 − 800 = **3,200.00**
Cross-check against the close: entry 7 credits Retained Earnings exactly
3,200, which is January's revenue minus expenses. The P&L for the closed
month therefore reports the same earnings the close moved.
The fixture therefore demonstrates both exclusions the parity row asks
for: a closing entry inside the range is not counted, and a later-period
entry is not counted.