test(store): golden file for draft states, discard and the evidence gate
One fixture serves both parity rows (draft states and discard; the one evidence gate). A Store fixture, because the gate's receipt-on-file half lives in the store. New fixture, built like books_for_drafts and extended. Hand-computed in plan/golden-worksheets/draft-states-and-evidence-gate.md, before serializing anything: - Requirement (B-43 inv. 2, threshold strictly > 25.00): drafts of 40.00, 60.00 and 100.00 are Required; 12.50 is Exempt (policy); the reversal (debits 100.00) is Exempt by origin (D-47, D-12); the closing entry (52.50) is Exempt by origin. Receipt counts are pinned beside them: the gate refuses the 40.00 and 60.00 drafts (no receipt) with the journal head unchanged, then lets the rest post; the reversal has no receipt. - Trial balance 2026-01-31 before and after the discard of the 60.00 draft: Cash 447.50 Dr, Equity 500.00 Cr, Software 52.50 Dr, totals 500.00; identical after the discard, next entry id 7 both times. - The close is refused while the draft is open (B-12), accepted after the discard: Dr Retained Earnings 52.50 / Cr Software 52.50 (a loss), id 7; trial balance then 447.50 Dr, 500.00 Cr, 52.50 Dr. Code and hand values agreed on the first run. Strength: four scratch edits of the golden (a balance, a decision, a discard reason, the next id) each fail the test. Adds serde::Serialize to evidence::Requirement (tagged, snake_case) so the gate's result can be pinned. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3ac3e8b1b4
commit
2593931c47
4 files changed
+670
-1
No files matched your search
@@ -137,7 +137,11 @@ pub enum Origin {
|
||||
}
|
||||
|
||||
/// Whether an entry needs evidence before it posts, and why (or why not).
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
/// Serialized as `{"decision": "required", "reason": ..}` or
|
||||
/// `{"decision": "exempt", "source": ..}` (the golden for the evidence
|
||||
/// gate pins it).
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
|
||||
#[serde(tag = "decision", rename_all = "snake_case")]
|
||||
pub enum Requirement {
|
||||
/// The receipt policy covers it: at least one receipt is needed.
|
||||
Required {
|
||||
|
||||
@@ -0,0 +1,252 @@
|
||||
{
|
||||
"drafts_open": [
|
||||
{
|
||||
"entry_id": 2,
|
||||
"date": "2026-01-10",
|
||||
"description": "Figma annual plan",
|
||||
"debits": "40.00",
|
||||
"state": "awaiting_evidence",
|
||||
"note": "invoice due from Figma",
|
||||
"receipts": 0,
|
||||
"requirement": {
|
||||
"decision": "required",
|
||||
"reason": "its debits 40.00 are strictly greater than the 25.00 threshold"
|
||||
}
|
||||
},
|
||||
{
|
||||
"entry_id": 3,
|
||||
"date": "2026-01-11",
|
||||
"description": "Duplicate hosting invoice",
|
||||
"debits": "60.00",
|
||||
"state": "building",
|
||||
"note": null,
|
||||
"receipts": 0,
|
||||
"requirement": {
|
||||
"decision": "required",
|
||||
"reason": "its debits 60.00 are strictly greater than the 25.00 threshold"
|
||||
}
|
||||
},
|
||||
{
|
||||
"entry_id": 4,
|
||||
"date": "2026-01-15",
|
||||
"description": "Office supplies",
|
||||
"debits": "12.50",
|
||||
"state": "building",
|
||||
"note": null,
|
||||
"receipts": 0,
|
||||
"requirement": {
|
||||
"decision": "exempt",
|
||||
"source": "the receipt policy (policy.evidence.*): under the threshold, or on exempt accounts"
|
||||
}
|
||||
},
|
||||
{
|
||||
"entry_id": 5,
|
||||
"date": "2026-01-20",
|
||||
"description": "Hosting",
|
||||
"debits": "100.00",
|
||||
"state": "building",
|
||||
"note": null,
|
||||
"receipts": 1,
|
||||
"requirement": {
|
||||
"decision": "required",
|
||||
"reason": "its debits 100.00 are strictly greater than the 25.00 threshold"
|
||||
}
|
||||
}
|
||||
],
|
||||
"gate_refusals": [
|
||||
{
|
||||
"entry_id": 2,
|
||||
"refused": "receipt_required",
|
||||
"journal_head_unchanged": true
|
||||
},
|
||||
{
|
||||
"entry_id": 3,
|
||||
"refused": "receipt_required",
|
||||
"journal_head_unchanged": true
|
||||
}
|
||||
],
|
||||
"posted_entries": [
|
||||
{
|
||||
"entry_id": 1,
|
||||
"description": "Seed",
|
||||
"reversal_of": null,
|
||||
"debits": "500.00",
|
||||
"receipts": 1,
|
||||
"requirement": {
|
||||
"decision": "required",
|
||||
"reason": "its debits 500.00 are strictly greater than the 25.00 threshold"
|
||||
}
|
||||
},
|
||||
{
|
||||
"entry_id": 2,
|
||||
"description": "Figma annual plan",
|
||||
"reversal_of": null,
|
||||
"debits": "40.00",
|
||||
"receipts": 1,
|
||||
"requirement": {
|
||||
"decision": "required",
|
||||
"reason": "its debits 40.00 are strictly greater than the 25.00 threshold"
|
||||
}
|
||||
},
|
||||
{
|
||||
"entry_id": 4,
|
||||
"description": "Office supplies",
|
||||
"reversal_of": null,
|
||||
"debits": "12.50",
|
||||
"receipts": 0,
|
||||
"requirement": {
|
||||
"decision": "exempt",
|
||||
"source": "the receipt policy (policy.evidence.*): under the threshold, or on exempt accounts"
|
||||
}
|
||||
},
|
||||
{
|
||||
"entry_id": 5,
|
||||
"description": "Hosting",
|
||||
"reversal_of": null,
|
||||
"debits": "100.00",
|
||||
"receipts": 1,
|
||||
"requirement": {
|
||||
"decision": "required",
|
||||
"reason": "its debits 100.00 are strictly greater than the 25.00 threshold"
|
||||
}
|
||||
},
|
||||
{
|
||||
"entry_id": 6,
|
||||
"description": "Reverse hosting (billed in error)",
|
||||
"reversal_of": 5,
|
||||
"debits": "100.00",
|
||||
"receipts": 0,
|
||||
"requirement": {
|
||||
"decision": "exempt",
|
||||
"source": "a reversal: its evidence is its original's (B-43, D-47, D-12)"
|
||||
}
|
||||
}
|
||||
],
|
||||
"close_refused_with_draft": {
|
||||
"refused": "drafts_in_period",
|
||||
"period_id": 1
|
||||
},
|
||||
"discard_of_posted_refused": {
|
||||
"entry_id": 1,
|
||||
"refused": true
|
||||
},
|
||||
"before_discard": {
|
||||
"drafts": [
|
||||
{
|
||||
"entry_id": 3,
|
||||
"date": "2026-01-11",
|
||||
"description": "Duplicate hosting invoice",
|
||||
"debits": "60.00",
|
||||
"state": "building",
|
||||
"note": null,
|
||||
"receipts": 0,
|
||||
"requirement": {
|
||||
"decision": "required",
|
||||
"reason": "its debits 60.00 are strictly greater than the 25.00 threshold"
|
||||
}
|
||||
}
|
||||
],
|
||||
"next_entry_id": 7,
|
||||
"trial_balance": {
|
||||
"lines": [
|
||||
{
|
||||
"code": "1000",
|
||||
"name": "Cash",
|
||||
"debit": "447.50",
|
||||
"credit": "0"
|
||||
},
|
||||
{
|
||||
"code": "3000",
|
||||
"name": "Owner's Equity",
|
||||
"debit": "0",
|
||||
"credit": "500.00"
|
||||
},
|
||||
{
|
||||
"code": "6100",
|
||||
"name": "Software",
|
||||
"debit": "52.50",
|
||||
"credit": "0"
|
||||
}
|
||||
],
|
||||
"total_debits": "500.00",
|
||||
"total_credits": "500.00"
|
||||
}
|
||||
},
|
||||
"draft_journal_lines": [
|
||||
{
|
||||
"type": "entry.draft_state_set",
|
||||
"id": 2,
|
||||
"state": "awaiting_evidence",
|
||||
"note": "invoice due from Figma"
|
||||
},
|
||||
{
|
||||
"type": "entry.draft_discarded",
|
||||
"id": 3,
|
||||
"reason": "entered twice"
|
||||
}
|
||||
],
|
||||
"after_discard": {
|
||||
"drafts": [],
|
||||
"next_entry_id": 7,
|
||||
"trial_balance": {
|
||||
"lines": [
|
||||
{
|
||||
"code": "1000",
|
||||
"name": "Cash",
|
||||
"debit": "447.50",
|
||||
"credit": "0"
|
||||
},
|
||||
{
|
||||
"code": "3000",
|
||||
"name": "Owner's Equity",
|
||||
"debit": "0",
|
||||
"credit": "500.00"
|
||||
},
|
||||
{
|
||||
"code": "6100",
|
||||
"name": "Software",
|
||||
"debit": "52.50",
|
||||
"credit": "0"
|
||||
}
|
||||
],
|
||||
"total_debits": "500.00",
|
||||
"total_credits": "500.00"
|
||||
},
|
||||
"trial_balance_equals_before": true
|
||||
},
|
||||
"close_after_discard": {
|
||||
"closing_entry": 7,
|
||||
"debits": "52.50",
|
||||
"receipts": 0,
|
||||
"requirement": {
|
||||
"decision": "exempt",
|
||||
"source": "a period's closing entry: derived from the books (B-43)"
|
||||
}
|
||||
},
|
||||
"after_close": {
|
||||
"trial_balance": {
|
||||
"lines": [
|
||||
{
|
||||
"code": "1000",
|
||||
"name": "Cash",
|
||||
"debit": "447.50",
|
||||
"credit": "0"
|
||||
},
|
||||
{
|
||||
"code": "3000",
|
||||
"name": "Owner's Equity",
|
||||
"debit": "0",
|
||||
"credit": "500.00"
|
||||
},
|
||||
{
|
||||
"code": "3999",
|
||||
"name": "Retained Earnings",
|
||||
"debit": "52.50",
|
||||
"credit": "0"
|
||||
}
|
||||
],
|
||||
"total_debits": "500.00",
|
||||
"total_credits": "500.00"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
//! Golden file for draft states, discard and the one evidence gate
|
||||
//! (`docs/parity.md`: "Draft states and discard" and "One evidence gate").
|
||||
//!
|
||||
//! Rules: `docs/spec/rules.md` B-43 (a draft waits on its evidence; one that
|
||||
//! will never post can be discarded), D-47 (a reversal needs no receipt of
|
||||
//! its own), B-12 (a period with an open draft cannot close).
|
||||
//!
|
||||
//! It is a `Store` fixture, not a bare `Ledger`, because the gate's second
|
||||
//! half (is a receipt on file) lives in the store. Every expected value is
|
||||
//! computed by hand in
|
||||
//! `plan/golden-worksheets/draft-states-and-evidence-gate.md`.
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use ledger_core::evidence::requirement;
|
||||
use ledger_core::reports::trial_balance_as_of;
|
||||
use ledger_core::{AccountType, Currency, DraftState, Money, Side};
|
||||
use ledger_store::{Store, StoreError};
|
||||
use rust_decimal::Decimal;
|
||||
use serde_json::{Value, json};
|
||||
use std::path::PathBuf;
|
||||
|
||||
fn jan(day: u32) -> NaiveDate {
|
||||
NaiveDate::from_ymd_opt(2026, 1, day).unwrap()
|
||||
}
|
||||
|
||||
/// Exact cents: `usd(1250)` is 12.50.
|
||||
fn usd(cents: i64) -> Money {
|
||||
Money::new(Decimal::new(cents, 2), Currency::USD)
|
||||
}
|
||||
|
||||
/// A test's dir, removed when the test ends, pass or panic.
|
||||
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-{name}-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
TestDir(dir)
|
||||
}
|
||||
|
||||
/// A draft of `cents` from `debit` to `credit`.
|
||||
fn draft(
|
||||
s: &mut Store,
|
||||
date: NaiveDate,
|
||||
what: &str,
|
||||
(debit, credit): (u64, u64),
|
||||
cents: i64,
|
||||
) -> u64 {
|
||||
let id = s.create_entry(date, what).unwrap();
|
||||
s.add_line(id, debit, Side::Debit, usd(cents)).unwrap();
|
||||
s.add_line(id, credit, Side::Credit, usd(cents)).unwrap();
|
||||
id
|
||||
}
|
||||
|
||||
/// File a receipt (distinct bytes: identical bytes are one file) on `entry`.
|
||||
fn receipt(s: &mut Store, entry: u64) {
|
||||
s.attach_receipt(
|
||||
Some(entry),
|
||||
"receipt.pdf",
|
||||
"application/pdf",
|
||||
format!("%PDF-1.4 receipt for entry {entry}").as_bytes(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
/// One draft as the viewer lists it: its state and the gate's decision.
|
||||
fn draft_record(s: &Store, id: u64) -> Value {
|
||||
let ledger = s.ledger();
|
||||
let e = ledger.get_entry(id).unwrap();
|
||||
let note = match e.draft_state() {
|
||||
DraftState::Building => Value::Null,
|
||||
DraftState::AwaitingEvidence { note } => json!(note),
|
||||
};
|
||||
json!({
|
||||
"entry_id": id,
|
||||
"date": e.date().to_string(),
|
||||
"description": e.description(),
|
||||
"debits": ledger_core::evidence::Policy::entry_debits(e).unwrap().to_string(),
|
||||
"state": e.draft_state().key(),
|
||||
"note": note,
|
||||
"receipts": s.list_attachments(Some(id)).unwrap().len(),
|
||||
"requirement": serde_json::to_value(requirement(ledger, e, ledger.settings())).unwrap(),
|
||||
})
|
||||
}
|
||||
|
||||
fn drafts(s: &Store) -> Vec<Value> {
|
||||
s.ledger()
|
||||
.drafts()
|
||||
.iter()
|
||||
.map(|e| draft_record(s, e.id()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn trial_balance(s: &Store) -> Value {
|
||||
serde_json::to_value(trial_balance_as_of(s.ledger(), jan(31))).unwrap()
|
||||
}
|
||||
|
||||
/// By-hand worksheet: `plan/golden-worksheets/draft-states-and-evidence-gate.md`.
|
||||
#[test]
|
||||
fn golden_draft_states_and_evidence_gate() {
|
||||
let dir = test_dir("golden-drafts");
|
||||
let mut s = Store::create(&dir.0, Currency::USD).unwrap();
|
||||
let cash = s
|
||||
.add_account("1000", "Cash", AccountType::Asset, None, false)
|
||||
.unwrap();
|
||||
let equity = s
|
||||
.add_account("3000", "Owner's Equity", AccountType::Equity, None, false)
|
||||
.unwrap();
|
||||
let software = s
|
||||
.add_account("6100", "Software", AccountType::Expense, None, false)
|
||||
.unwrap();
|
||||
|
||||
// 1: the seed, with its receipt, posts through the gate.
|
||||
let seed = draft(&mut s, jan(2), "Seed", (cash, equity), 50_000);
|
||||
receipt(&mut s, seed);
|
||||
s.post_entry_checked(seed).unwrap();
|
||||
|
||||
// 2..5: four drafts. 2 waits for its invoice; 5 already has its receipt.
|
||||
let figma = draft(
|
||||
&mut s,
|
||||
jan(10),
|
||||
"Figma annual plan",
|
||||
(software, cash),
|
||||
4_000,
|
||||
);
|
||||
let dup = draft(
|
||||
&mut s,
|
||||
jan(11),
|
||||
"Duplicate hosting invoice",
|
||||
(software, cash),
|
||||
6_000,
|
||||
);
|
||||
let supplies = draft(&mut s, jan(15), "Office supplies", (software, cash), 1_250);
|
||||
let hosting = draft(&mut s, jan(20), "Hosting", (software, cash), 10_000);
|
||||
assert_eq!((figma, dup, supplies, hosting), (2, 3, 4, 5));
|
||||
s.set_draft_state(
|
||||
figma,
|
||||
DraftState::AwaitingEvidence {
|
||||
note: "invoice due from Figma".into(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
receipt(&mut s, hosting);
|
||||
let drafts_open = drafts(&s);
|
||||
|
||||
// The gate refuses 2 and 3 (required, no receipt), journaling nothing.
|
||||
let mut gate_refusals = Vec::new();
|
||||
for id in [figma, dup] {
|
||||
let head = s.head();
|
||||
let refused = match s.post_entry_checked(id) {
|
||||
Err(StoreError::ReceiptRequired(info)) => {
|
||||
assert_eq!(info.entry_id, id);
|
||||
"receipt_required"
|
||||
}
|
||||
other => panic!("expected ReceiptRequired, got {other:?}"),
|
||||
};
|
||||
gate_refusals.push(json!({
|
||||
"entry_id": id,
|
||||
"refused": refused,
|
||||
"journal_head_unchanged": s.head() == head,
|
||||
}));
|
||||
}
|
||||
|
||||
// 4 (under the threshold) and 5 (receipt on file) post; 5 is reversed
|
||||
// with no receipt of its own; 2 posts once its receipt is filed.
|
||||
s.post_entry_checked(supplies).unwrap();
|
||||
s.post_entry_checked(hosting).unwrap();
|
||||
let reversal = s
|
||||
.reverse_entry(hosting, jan(25), "Reverse hosting (billed in error)")
|
||||
.unwrap();
|
||||
assert_eq!(reversal, 6);
|
||||
receipt(&mut s, figma);
|
||||
s.post_entry_checked(figma).unwrap();
|
||||
|
||||
// A period that draft 3 holds open.
|
||||
let period = s.create_period("2026-01", jan(1), jan(31)).unwrap();
|
||||
let close_refused = match s.close_period(period) {
|
||||
Err(StoreError::Ledger(ledger_core::LedgerError::DraftsInPeriod(p))) => {
|
||||
json!({"refused": "drafts_in_period", "period_id": p})
|
||||
}
|
||||
other => panic!("expected DraftsInPeriod, got {other:?}"),
|
||||
};
|
||||
// Only a draft can be discarded.
|
||||
let discard_posted = json!({
|
||||
"entry_id": seed,
|
||||
"refused": s.discard_draft(seed, "oops").is_err(),
|
||||
});
|
||||
|
||||
let before_discard = json!({
|
||||
"drafts": drafts(&s),
|
||||
"next_entry_id": s.ledger().next_entry_id(),
|
||||
"trial_balance": trial_balance(&s),
|
||||
});
|
||||
s.discard_draft(dup, "entered twice").unwrap();
|
||||
let after_tb = trial_balance(&s);
|
||||
let after_discard = json!({
|
||||
"drafts": drafts(&s),
|
||||
"next_entry_id": s.ledger().next_entry_id(),
|
||||
"trial_balance": after_tb,
|
||||
"trial_balance_equals_before": after_tb == before_discard["trial_balance"],
|
||||
});
|
||||
|
||||
// The close is accepted now.
|
||||
let closing = s
|
||||
.close_period(period)
|
||||
.unwrap()
|
||||
.expect("a loss is swept, so there is a closing entry");
|
||||
let closing_entry = s.ledger().get_entry(closing).unwrap().clone();
|
||||
let close_after_discard = json!({
|
||||
"closing_entry": closing,
|
||||
"debits": ledger_core::evidence::Policy::entry_debits(&closing_entry).unwrap().to_string(),
|
||||
"receipts": s.list_attachments(Some(closing)).unwrap().len(),
|
||||
"requirement": serde_json::to_value(
|
||||
requirement(s.ledger(), &closing_entry, s.ledger().settings())).unwrap(),
|
||||
});
|
||||
|
||||
// Every entry that posted, as the gate sees it now (not 3: discarded).
|
||||
let posted_entries: Vec<Value> = [seed, figma, supplies, hosting, reversal]
|
||||
.iter()
|
||||
.map(|&id| {
|
||||
let e = s.ledger().get_entry(id).unwrap();
|
||||
json!({
|
||||
"entry_id": id,
|
||||
"description": e.description(),
|
||||
"reversal_of": e.reversal_of(),
|
||||
"debits": ledger_core::evidence::Policy::entry_debits(e).unwrap().to_string(),
|
||||
"receipts": s.list_attachments(Some(id)).unwrap().len(),
|
||||
"requirement": serde_json::to_value(
|
||||
requirement(s.ledger(), e, s.ledger().settings())).unwrap(),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
|
||||
// The journal's draft lines: a state and a discard, with their reasons.
|
||||
let draft_journal_lines: Vec<Value> =
|
||||
std::fs::read_to_string(dir.0.join(ledger_store::JOURNAL_FILE))
|
||||
.unwrap()
|
||||
.lines()
|
||||
.map(|l| serde_json::from_str::<Value>(l).unwrap())
|
||||
.filter(|l| {
|
||||
l["type"]
|
||||
.as_str()
|
||||
.is_some_and(|t| t.starts_with("entry.draft_"))
|
||||
})
|
||||
.map(|l| {
|
||||
let p = &l["payload"];
|
||||
match l["type"].as_str().unwrap() {
|
||||
"entry.draft_state_set" => json!({
|
||||
"type": l["type"], "id": p["id"], "state": p["state"], "note": p["note"],
|
||||
}),
|
||||
_ => json!({"type": l["type"], "id": p["id"], "reason": p["reason"]}),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
s.verify().unwrap();
|
||||
let got = json!({
|
||||
"drafts_open": drafts_open,
|
||||
"gate_refusals": gate_refusals,
|
||||
"posted_entries": posted_entries,
|
||||
"close_refused_with_draft": close_refused,
|
||||
"discard_of_posted_refused": discard_posted,
|
||||
"before_discard": before_discard,
|
||||
"draft_journal_lines": draft_journal_lines,
|
||||
"after_discard": after_discard,
|
||||
"close_after_discard": close_after_discard,
|
||||
"after_close": {"trial_balance": trial_balance(&s)},
|
||||
});
|
||||
let want: Value =
|
||||
serde_json::from_str(include_str!("golden/draft_states_and_evidence_gate.json")).unwrap();
|
||||
assert_eq!(
|
||||
got,
|
||||
want,
|
||||
"got:\n{}",
|
||||
serde_json::to_string_pretty(&got).unwrap()
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user