test(core): the adapter runs drafts and evidence

bookspec's examples for BS-entry-5, BS-entry-6 and BS-ledger-17 are run through
the real Store, none reimplemented: a draft is made by `Store::create_entry` and
`add_line` (tried on a clone first, so a line the engine refuses on a header
account leaves no half-made draft in the journal), posted by `post_entry_checked`
when the example states an evidence policy (the server's gate; the primitive
otherwise), given its state by `set_draft_state`, its document by
`attach_receipt` and thrown away by `discard_draft`. A close a draft holds back
is tried on a clone too. The two queries are read off the engine: a draft's
state from the entry itself, and the evidence decision from
`evidence::requirement`, its sentence of exemption read into the examples'
closed words. The refusals are read off the engine's errors: a placeholder
account is `header_account`, the receipt gate `evidence_required`, a draft's
command on an entry that is no draft `not_drafted`, a close with a draft in its
period `drafts_in_period`.

Wanderbooks passes every example for its picks (check-when-line-added,
reversal-evidence-is-originals); the two examples for the other options are
skipped, so the pinned skip count goes from 11 to 13.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-09 03:44:13 -07:00
1 parent 609e4e6b26
commit 8c57046c53
10 files changed
+378 -32

No files matched your search

+194 -27
View File
@@ -20,11 +20,15 @@ impl Books<'_> {
("dispose_asset", e.dispose_asset.is_some()),
("run_schedules", e.run_schedules.is_some()),
("register_schedule", e.register_schedule.is_some()),
("post_draft", e.post_draft.is_some()),
("set_draft_state", e.set_draft_state.is_some()),
("attach_receipt", e.attach_receipt.is_some()),
("discard_draft", e.discard_draft.is_some()),
];
match kinds.iter().filter(|(_, given)| *given).collect::<Vec<_>>()[..] {
[(kind, _)] => kind,
_ => panic!(
"{} event {n}: an event is one of lines, reverses, close, bill, invoice, write_off, payment, settlement, recovery, run_depreciation, dispose_asset, run_schedules or register_schedule",
"{} event {n}: an event is one of lines, reverses, close, bill, invoice, write_off, payment, settlement, recovery, run_depreciation, dispose_asset, run_schedules, register_schedule, post_draft, set_draft_state, attach_receipt or discard_draft",
self.ex.id
),
}
@@ -80,8 +84,20 @@ impl Books<'_> {
"{ex} event {n}: {kind} is tagged with no parties or payment method"
);
assert!(
e.refused.is_none() || !matches!(kind, "close" | "invoice"),
"{ex} event {n}: refused is for a reversal, an entry, a bill or a command, which the books take whole or not at all"
e.refused.is_none() || kind != "invoice",
"{ex} event {n}: refused is for a reversal, an entry, a bill, a command or a close, which the books take whole or not at all"
);
assert!(
!e.draft || matches!(kind, "lines" | "reverses"),
"{ex} event {n}: draft is an entry's lines or a reversal, prepared and not yet posted"
);
assert!(
!e.draft
|| (e.parties.is_empty()
&& e.instrument.is_none()
&& e.payee.is_none()
&& !e.older),
"{ex} event {n}: a draft carries no payee, parties or payment method"
);
assert!(
!(e.older && e.refused.is_some()),
@@ -204,11 +220,19 @@ impl Books<'_> {
&self.posted[n - 1],
"{what}: what the event posted is kept"
);
// What a draft event made stands as a draft (until a `post_draft`
// posts it); what any other event made is posted.
let made = if e.draft {
EntryStatus::Draft
} else {
EntryStatus::Posted
};
for &id in &posted {
assert_eq!(
self.l().get_entry(id).map(|x| x.status()),
Some(EntryStatus::Posted),
"{what}: entry {id}, which it made, is posted"
Some(made),
"{what}: entry {id}, which it made, is {}",
if e.draft { "a draft" } else { "posted" }
);
}
if (own || matches!(kind, "write_off" | "payment" | "settlement")) && e.refused.is_none() {
@@ -333,6 +357,100 @@ impl Books<'_> {
};
let day = || when.expect("checked: dated");
match kind {
// A draft (BS-entry-5, BS-entry-6): the entry prepared, and posted
// by nothing here. The engine refuses a line on a header account
// as it is added (it picks check-when-line-added), and the books
// take an entry whole or not at all, so the draft is tried on a
// clone first: a refusal leaves no half-made draft in the journal.
"lines" if e.draft => {
let lines: Vec<_> = e
.lines
.iter()
.map(|line| {
let side = match line.side.as_str() {
"debit" => Side::Debit,
"credit" => Side::Credit,
other => panic!("{what}: side {other:?}"),
};
let class = line.project.as_ref().map(|p| {
*self.projects.get(p).unwrap_or_else(|| {
panic!("{what}: project {p:?} not in setup.projects")
})
});
(
self.id(&line.account),
side,
Money::new(money(&line.amount), self.currency),
class,
)
})
.collect();
let mut probe = self.store.ledger().clone();
let entry = probe.try_create_entry(day(), description)?;
for (i, &(account, side, amount, class)) in lines.iter().enumerate() {
probe.add_line(entry, account, side, amount)?;
if class.is_some() {
probe.set_line_class(entry, i, class)?;
}
}
let entry = self.store.create_entry(day(), description)?;
for (i, &(account, side, amount, class)) in lines.iter().enumerate() {
self.store.add_line(entry, account, side, amount)?;
if class.is_some() {
self.store.set_line_class(entry, i, class)?;
}
}
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"
),
"post_draft" => {
let p = e.post_draft.as_ref().expect("checked: a post");
let entry = self.entry_of(p.entry, what);
// With an evidence policy the post is the gate's: the
// server's path for a hand-made draft; without one, the
// primitive.
if self.has_policy() {
self.store.post_entry_checked(entry)
} else {
self.store.post_entry(entry)
}
}
"set_draft_state" => {
let s = e.set_draft_state.as_ref().expect("checked: a state");
let entry = self.entry_of(s.entry, what);
let state = match (s.state.as_str(), &s.note) {
("building", None) => ledger_core::DraftState::Building,
("awaiting_evidence", Some(note)) => {
ledger_core::DraftState::AwaitingEvidence { note: note.clone() }
}
other => panic!("{what}: draft state {other:?}"),
};
self.store.set_draft_state(entry, state)
}
"attach_receipt" => {
let r = e.attach_receipt.as_ref().expect("checked: a receipt");
let entry = self.entry_of(r.entry, what);
// The document, by its reference: its own bytes, so that two
// references are two documents (identical bytes are one file,
// plan P6 step 4).
let bytes = format!("%PDF-1.4\n{}\n%%EOF\n", r.receipt);
self.store
.attach_receipt(
Some(entry),
&format!("{}.pdf", r.receipt.replace([' ', ',', '/'], "-")),
"application/pdf",
bytes.as_bytes(),
Some(&r.receipt),
)
.map(drop)
}
"discard_draft" => {
let d = e.discard_draft.as_ref().expect("checked: a discard");
let entry = self.entry_of(d.entry, what);
self.store.discard_draft(entry, &d.reason)
}
"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
@@ -355,6 +473,27 @@ impl Books<'_> {
probe.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,
},
)));
}
}
Ok(probe.post_entry(entry)?)
}
"lines" => {
@@ -377,7 +516,11 @@ impl Books<'_> {
this.store.set_line_class(entry, i, Some(class))?;
}
}
this.store.post_entry(entry)
if this.has_policy() {
this.store.post_entry_checked(entry)
} else {
this.store.post_entry(entry)
}
};
if e.older {
ledger_core::parties::replaying(|| post(self))
@@ -399,30 +542,20 @@ impl Books<'_> {
"close" => {
let c = e.close.as_ref().expect("checked: a close");
let start = date(&c.start, what);
if e.refused.is_some() {
// A close the books refuse (a draft stands in the period,
// BS-period-2) opens no period and posts nothing: it is
// tried on a clone, with the same close below.
let mut probe = self.store.ledger().clone();
let p = probe.create_period(&format!("event {n}"), start, day())?;
return Ok(probe
.close_period_to(p, &self.close_to(c, what))
.map(drop)?);
}
let p = self
.store
.create_period(&format!("event {n}"), start, day())?;
let members: Vec<MemberClose> = self
.members
.iter()
.map(|m| MemberClose {
capital: m.capital_account,
share: m.share,
distributions: None,
})
.collect();
if c.to == "members" {
assert!(
!members.is_empty() && members.iter().all(|m| m.share > Decimal::ZERO),
"{what}: a close to the members needs their shares"
);
}
let to = match (c.to.as_str(), c.rounding.as_deref()) {
("members", None) => CloseTo::Members(members),
("members", Some("per-close")) => CloseTo::MembersPerClose(members),
("retained_earnings", None) => CloseTo::RetainedEarnings { sweep: vec![] },
other => panic!("{what}: close {other:?}"),
};
let to = self.close_to(c, what);
let closed = self.store.close_period_to(p, &to)?;
assert!(closed.is_some(), "{what}: the close has nothing to close");
Ok(())
@@ -694,6 +827,40 @@ impl Books<'_> {
}
}
}
impl Books<'_> {
/// Whether the example states an evidence policy (`setup.evidence_over`):
/// the books' own setting, which the server's post gate reads.
pub(crate) fn has_policy(&self) -> bool {
self.ex.setup.evidence_over.is_some()
}
/// Where a close sends the period's income: to retained earnings, or to
/// the members by their shares.
pub(crate) fn close_to(&self, c: &bookspec_checker::CloseSpec, what: &str) -> CloseTo {
let members: Vec<MemberClose> = self
.members
.iter()
.map(|m| MemberClose {
capital: m.capital_account,
share: m.share,
distributions: None,
})
.collect();
if c.to == "members" {
assert!(
!members.is_empty() && members.iter().all(|m| m.share > Decimal::ZERO),
"{what}: a close to the members needs their shares"
);
}
match (c.to.as_str(), c.rounding.as_deref()) {
("members", None) => CloseTo::Members(members),
("members", Some("per-close")) => CloseTo::MembersPerClose(members),
("retained_earnings", None) => CloseTo::RetainedEarnings { sweep: vec![] },
other => panic!("{what}: close {other:?}"),
}
}
}
impl Books<'_> {
/// Make a change that posts no entry (after event `after`).
/// Make a change that posts no entry (after event `after`), or say why