Files
wanderbooks/crates/ledger-core/tests/bookspec/queries/drafts.rs
T
swaitsandClaude Sonnet 5.5 8c57046c53 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>
2026-11-09 03:44:13 -07:00

81 lines
3.7 KiB
Rust

use crate::*;
impl Books<'_> {
/// Where the entry event `q.entry` made stands (BS-entry-5), read from
/// the engine's own books: a draft's state (with its note when it awaits
/// evidence), posted (a posted entry has no state of its own), or gone
/// (a discarded draft leaves the ledger).
pub(crate) fn answer_draft_state(&self, q: &Query) -> Result<Value, Refusal> {
let n = q.entry.ok_or_else(|| self.missing("entry"))?;
let id = self.entry_or_refuse(n)?;
let mut m = JsonMap::new();
let state = match self.l().get_entry(id) {
None => "discarded",
Some(e) if e.status() == EntryStatus::Posted => "posted",
Some(e) => match e.draft_state() {
ledger_core::DraftState::Building => "building",
ledger_core::DraftState::AwaitingEvidence { note } => {
m.insert("note".to_string(), Value::String(note.clone()));
"awaiting_evidence"
}
},
};
m.insert("state".to_string(), Value::String(state.to_string()));
Ok(Value::Object(m))
}
/// The engine's one decision whether the entry event `q.entry` made
/// needs evidence (`ledger_core::evidence::requirement`, BS-entry-5): the
/// decision as the engine gives it, its reason or exemption read into
/// the examples' closed words.
pub(crate) fn answer_evidence(&self, q: &Query) -> Result<Value, Refusal> {
use ledger_core::evidence::{Requirement, requirement};
let n = q.entry.ok_or_else(|| self.missing("entry"))?;
let id = self.entry_or_refuse(n)?;
let entry = self
.l()
.get_entry(id)
.ok_or_else(|| self.missing("entry"))?;
let (decision, because) = match requirement(self.l(), entry, self.l().settings()) {
Requirement::Required { .. } => ("required", "policy"),
Requirement::Exempt { source } => (
"exempt",
// The engine words the source of an exemption in sentences;
// the examples' words are the closed list of BS-entry-5.
match source {
s if s.starts_with("the receipt policy") => "policy",
s if s.starts_with("a reversal") => "reversal",
s if s.starts_with("a period's closing entry") => "closing_entry",
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"
}
other => {
return Err(engine_refusal(
RuleId::new(Area::Entry, 5),
format!("an exemption with no word in the examples: {other}"),
));
}
},
),
};
Ok(json_figures([
("decision".to_string(), decision.to_string()),
("because".to_string(), because.to_string()),
]))
}
}
/// Check a `draft_state` expectation: the state, and the note when the draft
/// awaits evidence, and nothing else.
pub(crate) fn check_draft_state(b: &Books<'_>, x: &Expect) {
check_figures(b, x, b.answer_draft_state(&x.query()));
}
/// Check an `evidence` expectation: the decision and why, both given.
pub(crate) fn check_evidence(b: &Books<'_>, x: &Expect) {
check_figures(b, x, b.answer_evidence(&x.query()));
}