From 56461c1bb6fab6db70d32996d1748742aa45afd3 Mon Sep 17 00:00:00 2001 From: Stephen Waits Date: Mon, 9 Nov 2026 04:23:10 -0700 Subject: [PATCH] test(core): the adapter's refused hand entries go through a real Store, and payroll is its own source A hand entry the example says is refused when posted at once was made on a clone of the Ledger and the receipt-gate error hand-built from `evidence::requirement`, as `Store::post_entry_checked` builds it: the adapter, not the engine, produced the refusal. A store journals a draft the moment it is made, so the entry is now made, lined and posted on a rehearsal: the books replayed from their journal into a Store of their own (the journal and blobs copied, opened as any store is), and posted through `post_entry_checked` under a policy, `post_entry` otherwise. The refusal is the engine's own (ReceiptRequired included) and the example's books keep nothing. A payroll run or tax remittance is exempt by its own source, so the engine's "a payroll ..." sentence now reads as bookspec's new `payroll_posting`, not `document_entry`. A draft reversal, which wanderbooks has no path for, is returned as an unsupported-operation error (reported as "another refusal"), not a panic that takes the harness down with it. The claims for B-43 name the new example, entry-only-a-draft-can-be-posted-or- discarded (a posted entry or a discarded draft is no draft), and the rehearsal. Needs bookspec's x-h1c commits (payroll_posting, the new example, the 25.00 threshold example); the pin moves when they land. Co-Authored-By: Claude Sonnet 5.5 --- crates/ledger-core/tests/bookspec/books.rs | 65 +++++++++++++++++++ crates/ledger-core/tests/bookspec/events.rs | 52 +++++++-------- .../tests/bookspec/queries/drafts.rs | 7 +- docs/spec/claims/entry.md | 9 ++- 4 files changed, 100 insertions(+), 33 deletions(-) diff --git a/crates/ledger-core/tests/bookspec/books.rs b/crates/ledger-core/tests/bookspec/books.rs index 1e5f4030..29c794a5 100644 --- a/crates/ledger-core/tests/bookspec/books.rs +++ b/crates/ledger-core/tests/bookspec/books.rs @@ -216,6 +216,50 @@ pub(crate) fn new_schedule( } } +/// A store of the books as they stood (see [`Books::rehearsal`]), and the +/// directory it lives in, which goes with it. +pub(crate) struct Rehearsal { + store: Store, + _dir: tempfile::TempDir, +} + +impl std::ops::Deref for Rehearsal { + type Target = Store; + fn deref(&self) -> &Store { + &self.store + } +} + +impl std::ops::DerefMut for Rehearsal { + fn deref_mut(&mut self) -> &mut Store { + &mut self.store + } +} + +/// Copy the directory `from` to `to`, the entries `keep` names (by file +/// name) only, subdirectories whole. +fn copy_tree( + from: &std::path::Path, + to: &std::path::Path, + keep: &dyn Fn(&str) -> bool, +) -> std::io::Result<()> { + std::fs::create_dir_all(to)?; + for entry in std::fs::read_dir(from)? { + let entry = entry?; + let name = entry.file_name(); + if !keep(&name.to_string_lossy()) { + continue; + } + let target = to.join(&name); + if entry.file_type()?.is_dir() { + copy_tree(&entry.path(), &target, &|_| true)?; + } else { + std::fs::copy(entry.path(), target)?; + } + } + Ok(()) +} + impl<'a> Books<'a> { /// The books an example sets up, before any event: its shape checked, /// its accounts, fixed assets, schedules, parties, members, payees, @@ -559,6 +603,27 @@ impl<'a> Books<'a> { /// The entry event `event` (counting from 1) posted: an event applied /// already, that posted exactly one. + /// The books as they stand, replayed from their journal into a store of + /// their own (a copy of the journal and the blobs, opened as any store + /// is), for a step the books must refuse without keeping a trace: a store + /// journals a draft the moment it is made, so a refused entry is made on + /// this and dropped with it. The directory lives as long as the store. + pub(crate) fn rehearsal(&self, what: &str) -> Rehearsal { + let from = self._dir.path().join("books"); + let dir = tempfile::tempdir().unwrap(); + let to = dir.path().join("books"); + // The journal is the books; the database is derived, and the lock is + // this process's own. + copy_tree(&from, &to, &|name| { + name != ledger_store::store::LOCK_FILE + && !ledger_store::store::is_database_file(name) + && !name.contains(".db-") + }) + .unwrap_or_else(|e| panic!("{what}: copying the books for a rehearsal: {e}")); + let store = took(Store::open(&to), &format!("{what}: the rehearsal")); + Rehearsal { store, _dir: dir } + } + pub(crate) fn entry_of(&self, event: usize, what: &str) -> EntryId { assert!( event >= 1 && event <= self.posted.len(), diff --git a/crates/ledger-core/tests/bookspec/events.rs b/crates/ledger-core/tests/bookspec/events.rs index 6958fdc7..6d8fdf08 100644 --- a/crates/ledger-core/tests/bookspec/events.rs +++ b/crates/ledger-core/tests/bookspec/events.rs @@ -402,9 +402,14 @@ impl Books<'_> { } Ok(()) } - "reverses" if e.draft => panic!( - "{what}: wanderbooks has no draft reversal: it picks reversal-evidence-is-originals (BS-ledger-17), under which a reversal posts at once" - ), + // Not a panic: the example fails as an engine refusal (`another + // refusal: ...`), and the harness goes on to report it. + "reverses" if e.draft => Err(StoreError::Io(std::io::Error::new( + std::io::ErrorKind::Unsupported, + format!( + "{what}: wanderbooks has no draft reversal: it picks reversal-evidence-is-originals (BS-ledger-17), under which a reversal posts at once" + ), + ))), "post_draft" => { let p = e.post_draft.as_ref().expect("checked: a post"); let entry = self.entry_of(p.entry, what); @@ -453,10 +458,16 @@ impl Books<'_> { } "lines" if e.refused.is_some() => { // The books take an entry whole or not at all, so a - // refused one leaves no draft behind: it is tried on a - // clone, which is how the engine refuses it. - let mut probe = self.store.ledger().clone(); - let entry = probe.create_entry(day(), description); + // refused one leaves no draft behind. The store journals a + // draft the moment it is made, so the entry is made, lined + // and posted on a rehearsal: the same books, replayed from + // their journal into a store of their own, which the engine + // refuses through its own gate (`post_entry_checked` under a + // policy, the server's path for a hand entry) and which is + // then thrown away. The refusal is the engine's, never + // rebuilt here. + let mut rehearsal = self.rehearsal(what); + let entry = rehearsal.create_entry(day(), description)?; for (i, line) in e.lines.iter().enumerate() { let side = match line.side.as_str() { "debit" => Side::Debit, @@ -465,36 +476,19 @@ impl Books<'_> { }; let amount = Money::new(money(&line.amount), self.currency); let account = self.id(&line.account); - probe.add_line(entry, account, side, amount)?; + rehearsal.add_line(entry, account, side, amount)?; if let Some(p) = &line.project { let class = *self.projects.get(p).unwrap_or_else(|| { panic!("{what}: project {p:?} not in setup.projects") }); - probe.set_line_class(entry, i, Some(class))?; + rehearsal.set_line_class(entry, i, Some(class))?; } } - // The evidence gate, as the server's post runs it: a hand entry - // the policy covers has no document, and an event cannot - // attach one before the entry exists. if self.has_policy() { - let draft = probe.get_entry(entry).expect("just drafted").clone(); - let settings = probe.settings(); - if let ledger_core::evidence::Requirement::Required { reason } = - ledger_core::evidence::requirement(&probe, &draft, settings) - { - let policy = ledger_core::evidence::Policy::from_settings(settings); - return Err(StoreError::ReceiptRequired(Box::new( - ledger_store::error::ReceiptRequiredInfo { - entry_id: entry, - debits: ledger_core::evidence::Policy::entry_debits(&draft).ok(), - policy: policy.describe(), - split_group: policy.split_group(&probe, entry, &draft), - reason, - }, - ))); - } + rehearsal.post_entry_checked(entry) + } else { + rehearsal.post_entry(entry) } - Ok(probe.post_entry(entry)?) } "lines" => { let post = |this: &mut Self| -> Result<(), StoreError> { diff --git a/crates/ledger-core/tests/bookspec/queries/drafts.rs b/crates/ledger-core/tests/bookspec/queries/drafts.rs index de7f8897..c32d279b 100644 --- a/crates/ledger-core/tests/bookspec/queries/drafts.rs +++ b/crates/ledger-core/tests/bookspec/queries/drafts.rs @@ -49,9 +49,10 @@ impl Books<'_> { s if s.starts_with("a schedule month's posting") => "schedule_posting", s if s.starts_with("a depreciation run") => "depreciation_run", s if s.starts_with("an asset's disposal") => "asset_disposal", - s if s.starts_with("a document's entry") || s.starts_with("a payroll") => { - "document_entry" - } + // A payroll command's posting (a run, or the tax + // remittance) is its own source, not a document's. + s if s.starts_with("a payroll") => "payroll_posting", + s if s.starts_with("a document's entry") => "document_entry", other => { return Err(engine_refusal( RuleId::new(Area::Entry, 5), diff --git a/docs/spec/claims/entry.md b/docs/spec/claims/entry.md index 42ecf20b..c1556792 100644 --- a/docs/spec/claims/entry.md +++ b/docs/spec/claims/entry.md @@ -222,7 +222,14 @@ pinned; every tool over every old book). `entry-a-discarded-draft-leaves-the-books-unchanged`, `entry-a-drafts-state-is-a-note-and-a-posted-entry-has-none`, `period-a-draft-holds-its-period-open-until-it-is-discarded` (BS-period-2's -`LedgerError::DraftsInPeriod`) and `entry-evidence-is-decided-in-one-place`. +`LedgerError::DraftsInPeriod`), `entry-evidence-is-decided-in-one-place` and +`entry-only-a-draft-can-be-posted-or-discarded` (`LedgerError::EntryAlreadyPosted` +and `LedgerError::UnknownEntry`, through `Store::post_entry` and `Store::discard_draft`). An entry the example +says is refused when posted at once is made, lined and posted on a rehearsal: +the books replayed from their journal into a `Store` of their own (the +journal and the blobs copied, `Store::open`), so the refusal, an evidence +refusal included, is `Store::post_entry_checked`'s own and the example's books +keep no draft the engine would have journaled. ### Product rules