From 344e09933df102daf30926d1d378710d93e56162 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 06:15:38 -0700 Subject: [PATCH] test(core): cite the rule that states the fact, and assert the fact, not the wording Review of V-D2's tests: - S-E3's vocabulary: `item k` and `INVARIANT k` become `invariant k` of WB-ledger-15, WB-ledger-9 (no numbered parts: cited whole), and the statements and invariants of BS-arap-5 and BS-arap-6 that the tests actually pin. - The split-group tests cite WB-entry-6, the only rule that says "as part of a split group", not BS-entry-5. - A refusal is pinned by its variant plus the fragment that states the fact (a figure, "positive", "strictly greater"), not its whole sentence; the `ConventionKind::ALL.len() == 7` pin is dropped. - `a_send...moves_no_figure` compared receivables with itself; it now asserts the bank unchanged and receivables at 500.00. - The disposal test says a day before the asset was acquired is refused and now asserts DisposalBeforeAcquisition. - Main's FeeAboveCash variant replaces the InvalidSettlement text the fee test matched; it now asserts the variant and both figures. Co-Authored-By: Claude Sonnet 5.5 --- .../ledger-core/tests/conventions_refusals.rs | 3 +- crates/ledger-core/tests/document_commands.rs | 30 +++++++----- crates/ledger-core/tests/evidence_gate.rs | 47 +++++++++++-------- crates/ledger-core/tests/ledger_edits.rs | 9 +++- crates/ledger-core/tests/replay_documents.rs | 7 +-- 5 files changed, 59 insertions(+), 37 deletions(-) diff --git a/crates/ledger-core/tests/conventions_refusals.rs b/crates/ledger-core/tests/conventions_refusals.rs index 3140f8b3..2f51851e 100644 --- a/crates/ledger-core/tests/conventions_refusals.rs +++ b/crates/ledger-core/tests/conventions_refusals.rs @@ -944,7 +944,7 @@ fn drafting_from_a_template_refuses_what_it_cannot_make_whole() { // ---------------------------------------------------------------- the pure helpers -/// WB-ledger-9, INVARIANT 2: a split is exact. With no rounding places, +/// WB-ledger-9: a template's split is exact. With no rounding places, /// each part is amount × share and the last absorbs any difference; with /// places, each part is truncated and the leftover goes by largest /// remainder; either way the parts sum to the amount. @@ -979,7 +979,6 @@ fn a_split_sums_to_its_amount_exactly() { /// The convention kinds' words round-trip, and an unknown word is none. #[test] fn convention_kinds_round_trip_their_words() { - assert_eq!(ConventionKind::ALL.len(), 7); for k in ConventionKind::ALL { assert_eq!(ConventionKind::parse(k.key()), Some(k)); } diff --git a/crates/ledger-core/tests/document_commands.rs b/crates/ledger-core/tests/document_commands.rs index 6fbd00fa..e33e7bbc 100644 --- a/crates/ledger-core/tests/document_commands.rs +++ b/crates/ledger-core/tests/document_commands.rs @@ -137,7 +137,9 @@ fn a_send_is_a_fact_of_the_invoices_life_and_moves_no_figure() { .map(|&x| l.doc_state(doc, x).unwrap()) .collect() }; - let (before_states, before_ar) = (states(&b.l), b.balance("1200")); + let (before_states, before_ar, before_bank) = + (states(&b.l), b.balance("1200"), b.balance("1000")); + assert_eq!(before_ar, dec("500.00")); let mut first = sent(d(3, 2), &["ap@fernhill.example"]); first.reference = Some("".into()); @@ -149,10 +151,10 @@ fn a_send_is_a_fact_of_the_invoices_life_and_moves_no_figure() { assert_eq!(b.l.get_invoice(inv).unwrap().sends, vec![first, reminder]); assert_eq!(states(&b.l), before_states, "no figure moved on any date"); - assert_eq!( - (b.balance("1200"), b.balance("1200")), - (before_ar, dec("500.00")) - ); + // Receivables still owe the invoice, and the bank is where it was. + assert_eq!(b.balance("1200"), dec("500.00")); + assert_eq!(b.balance("1200"), before_ar); + assert_eq!(b.balance("1000"), before_bank); } /// mark_sent INVARIANT 1 and SCENARIO 1: the invoice must exist and the @@ -289,7 +291,8 @@ fn settle_one(b: &Books, inv: u64, cash: &str) -> SettleParams { } } -/// BS-arap-6 INVARIANT 5: each thing a settlement can be refused for, +/// BS-arap-6 statements 1, 4, 5 and 6 (invariants 4 and 5): each thing a +/// settlement can be refused for, /// refused with its own error, the books untouched. The invoice is for /// 500.00 dated March 1; the payment is dated March 20. #[test] @@ -401,10 +404,13 @@ fn a_fee_is_an_expense_taken_from_the_payment_and_a_closed_period_refuses() { // The fee is above the cash. let mut p = settle_one(&b, inv, "100.00"); p.fee = fee("100.01", Some(b.fees)); - assert!(matches!( + assert_eq!( refused(&mut b.l, |l| l.settle(p)), - LedgerError::InvalidSettlement(m) if m.contains("fee is above the cash") - )); + LedgerError::FeeAboveCash { + fee: dec("100.01"), + cash: dec("100.00") + } + ); // The fee's account must be an expense that takes postings. let mut p = settle_one(&b, inv, "100.00"); p.fee = fee("3.20", Some(b.revenue)); @@ -479,7 +485,9 @@ fn a_bill_settlement_mirrors_an_invoices_and_describes_itself() { // ---------------------------------------------------------------- write_off -/// BS-arap-5 INVARIANTS 2 to 4: a write-off is refused on the cash basis, +/// BS-arap-5 statements 2, 4 and 6 (invariants 2, 3 and 5; the one-line +/// reason is the engine's own guard, beside statement 9's evidence of the +/// debt): a write-off is refused on the cash basis, /// without a one-line reason, for an amount that is not above zero, off the /// calendar, before the invoice, in a closed period, or more than the /// invoice owes on every day from then on; the refusals change nothing. @@ -542,7 +550,7 @@ fn a_write_off_is_refused_for_each_rule_it_breaks() { assert_eq!(b.balance("1200"), Decimal::ZERO); } -/// BS-arap-5 INVARIANT 2: on cash-basis books nothing is receivable to +/// BS-arap-5 statement 6 (invariant 5): on cash-basis books nothing is receivable to /// write off, so the command is refused whatever else is true. #[test] fn a_write_off_is_refused_on_the_cash_basis() { diff --git a/crates/ledger-core/tests/evidence_gate.rs b/crates/ledger-core/tests/evidence_gate.rs index d52140cf..3d30e330 100644 --- a/crates/ledger-core/tests/evidence_gate.rs +++ b/crates/ledger-core/tests/evidence_gate.rs @@ -139,7 +139,7 @@ fn exempt_source(r: &Requirement) -> &'static str { // ---------------------------------------------------------------- annotations -/// WB-ledger-15 item 4: an annotation's fields are a closed list; text is +/// WB-ledger-15 invariant 4: an annotation's fields are a closed list; text is /// kept trimmed and a blank one is not kept; every field is optional. #[test] fn annotation_fields_are_trimmed_and_blank_ones_dropped() { @@ -165,7 +165,7 @@ fn annotation_fields_are_trimmed_and_blank_ones_dropped() { assert!(!kept.is_empty()); } -/// WB-ledger-15 item 4: "nothing recorded" is judged field by field: any +/// WB-ledger-15 invariant 4: "nothing recorded" is judged field by field: any /// one field alone makes the record non-empty, and none makes it empty. #[test] fn an_annotation_field_alone_is_a_record() { @@ -190,27 +190,29 @@ fn an_annotation_field_alone_is_a_record() { } } -/// WB-ledger-15 item 4 and its numbering (item 5 of the receipt rule): a +/// WB-ledger-15 invariant 4 and its numbering (the receipt rule's): a /// receipt number is positive, an amount is not negative, a date is in the /// books' calendar (0001-01-01..=9999-12-31). A zero amount is a fact (a /// no-charge receipt). #[test] fn an_annotation_refuses_a_zero_number_a_negative_amount_and_a_date_off_the_calendar() { let bad = |f: AnnotationFields| f.checked().unwrap_err(); - assert_eq!( + // The variant, and the fact each refusal states; the sentence around + // it is the engine's own. + assert!(matches!( bad(AnnotationFields { receipt_number: Some(0), ..Default::default() }), - LedgerError::InvalidEvidence("a receipt number is positive".into()) - ); - assert_eq!( + LedgerError::InvalidEvidence(m) if m.contains("positive") + )); + assert!(matches!( bad(AnnotationFields { amount: Some(dec("-0.01")), ..Default::default() }), - LedgerError::InvalidEvidence("an annotated amount is not negative".into()) - ); + LedgerError::InvalidEvidence(m) if m.contains("negative") + )); let off = NaiveDate::MAX; assert_eq!( bad(AnnotationFields { @@ -229,7 +231,7 @@ fn an_annotation_refuses_a_zero_number_a_negative_amount_and_a_date_off_the_cale assert_eq!(ok.clone().checked().unwrap(), ok); } -/// WB-ledger-15 item 4: an annotation records at least one field or a +/// WB-ledger-15 invariant 4: an annotation records at least one field or a /// note; the note is trimmed, and a blank one is no note. #[test] fn an_annotation_needs_a_field_or_a_note() { @@ -296,9 +298,12 @@ fn the_policy_reads_the_books_settings() { let p = Policy::from_settings(b.l.settings()); assert_eq!(p.threshold, dec("25.00")); assert!(p.required_accounts.is_empty() && p.exempt_accounts.is_empty()); - assert_eq!( - p.describe(), - "receipts required when entry debits are strictly greater than 25.00" + // The description states the threshold and that it is strict (the + // wording is the engine's own). + let described = p.describe(); + assert!( + described.contains("strictly greater") && described.contains("25.00"), + "{described}" ); set( @@ -366,9 +371,12 @@ fn a_hand_entry_is_required_above_the_threshold_with_its_reason() { ); let r = requirement(&b.l, big); assert!(r.is_required()); - assert_eq!( - required_reason(&r), - "its debits 40.00 are strictly greater than the 25.00 threshold" + // The reason carries the figures and the strict comparison (the + // sentence is the engine's own). + let why = required_reason(&r); + assert!( + why.contains("40.00") && why.contains("25.00") && why.contains("strictly greater"), + "{why}" ); let r = requirement(&b.l, small); assert!(!r.is_required()); @@ -451,8 +459,9 @@ fn debits_past_exact_arithmetic_are_refused_by_the_sum_and_past_the_threshold_fo // ---------------------------------------------------------------- split groups -/// BS-entry-5, the anti-gaming rule: four same-day, same-description, -/// same-debit-account entries of 10.00 are one 40.00 purchase. The draft +/// WB-entry-6 (an entry "as part of a split group"), the anti-gaming +/// rule: four same-day, same-description, same-debit-account entries of +/// 10.00 are one 40.00 purchase. The draft /// that brings the group past the threshold needs a receipt and names the /// group; the ones before it, and a posted entry, do not (a posted entry's /// decision was made when it posted). @@ -481,7 +490,7 @@ fn a_split_purchase_is_one_purchase() { assert!(!requirement(&b.l, third).is_required()); } -/// BS-entry-5: what is NOT a split group. A different day, a different +/// WB-entry-6: what is NOT a split group. A different day, a different /// description, a different debit account, a group still under the /// threshold and a reversal in the group (reversals are exempt) leave the /// new draft alone. diff --git a/crates/ledger-core/tests/ledger_edits.rs b/crates/ledger-core/tests/ledger_edits.rs index 176c3f60..6a8edfea 100644 --- a/crates/ledger-core/tests/ledger_edits.rs +++ b/crates/ledger-core/tests/ledger_edits.rs @@ -298,6 +298,11 @@ fn a_disposal_is_refused_unless_the_asset_is_active_and_the_proceeds_make_sense( refused(&mut l, |l| l.dispose_asset(id, d(6, 1), usd("-1.00"))), LedgerError::NegativeProceeds(dec("-1.00")) ); + // The laptop went into service on 1/15: a day before that is refused. + assert!(matches!( + refused(&mut l, |l| l.dispose_asset(id, d(1, 14), usd("1.00"))), + LedgerError::DisposalBeforeAcquisition { .. } + )); l.dispose_asset(id, d(6, 1), usd("700.00")).unwrap(); assert_eq!( refused(&mut l, |l| l.dispose_asset(id, d(7, 1), usd("1.00"))), @@ -364,7 +369,7 @@ fn the_fiscal_year_does_not_move_under_a_closed_period() { // ---------------------------------------------------------------- evidence on the ledger -/// WB-ledger-15 item 4 and 5: annotations are checked as `Annotation:: +/// WB-ledger-15 invariants 4 and 5: annotations are checked as `Annotation:: /// checked` says before they are kept; a second annotation of the same file /// replaces the first; receipt numbers run from the highest annotated, so a /// human's duplicate is not renumbered. @@ -420,7 +425,7 @@ fn annotations_replace_by_file_and_receipt_numbers_run_from_the_max() { assert_eq!(l.next_receipt_number(), u32::MAX, "saturates, never wraps"); } -/// WB-ledger-15 item 5: `entry.settles` names entries that exist, never +/// WB-ledger-15 invariant 5: `entry.settles` names entries that exist, never /// the entry itself, never one twice; "reimbursed" is derived from it /// alone; a refusal changes nothing. #[test] diff --git a/crates/ledger-core/tests/replay_documents.rs b/crates/ledger-core/tests/replay_documents.rs index 3e45ba1c..640c490b 100644 --- a/crates/ledger-core/tests/replay_documents.rs +++ b/crates/ledger-core/tests/replay_documents.rs @@ -10,7 +10,8 @@ //! open and tie out: a document's payment is read from the line an //! event names, the control check lists what it could not match. //! SOURCE: BS-entry-4 (replay takes what was journaled); BS-arap-6 -//! INVARIANT 3 (the posting rows' line indices are stable); +//! invariant 2 (every control line a settlement posts carries its +//! document, so the posting rows' line indices are stable); //! BS-arap-2 (a document's figures are read from its tagged lines, //! and the control account ties to them plus the untagged lines). //! ``` @@ -119,7 +120,7 @@ impl Books { } } -/// BS-arap-6 INVARIANT 3: a payment event that names the line (index 1, the +/// BS-arap-6 invariant 2: a payment event that names the line (index 1, the /// receivables credit) tags exactly that line. The invoice then reads 40.00 /// paid and 460.00 owed, and the control account ties out with nothing /// untagged. @@ -183,7 +184,7 @@ fn a_payment_event_that_names_a_line_that_does_not_match_leaves_it_untagged() { assert!(!b.unattached(d(3, 31)).contains(&(e, 1))); } -/// BS-arap-6 INVARIANT 3: a payment with an early-payment discount posts a +/// BS-arap-6 statement 3 and invariant 2: a payment with an early-payment discount posts a /// payment line and a discount line against the same control account; the /// event names both. A 500.00 invoice settled with 490.00 in cash and /// 10.00 of discount reads paid 490.00, discounted 10.00, nothing owed.