Files
wanderbooks/crates/ledger-core/tests/bookspec/common.rs
T
swaitsandClaude Sonnet 5.5 a87e2d0a7a test(core): the adapter runs a platform's payout, an asset's tag and the K-1's tax-basis note through the engine
The bookspec schema gained a `payout` event, a `tag_asset` event, an
`asset_tag` query and a note on the tax-basis property line (BS-revenue-1,
BS-asset-3, BS-partner-17). The adapter runs each through the engine and
rebuilds none of it: a payout goes through `payouts::payout_plan`, which
refuses unless gross less fee is the deposit to the cent, and its plan's lines
post as one entry; a tag goes through `Store::set_asset_tag`, which refuses a
tag of a wrong form and one another asset holds or ever held; the tax-basis
line's note is `Note::PrepaidAtBook` read off the worksheet. The engine's
InvalidPayout, InvalidAssetTag and AssetTagTaken are worded as the examples
word them.

Thirteen examples were added and every one the engine's picks reach passes:
five are for options wanderbooks did not pick (no-tags, every-prepaid-is-cash,
every-prepaid-is-property, left-open, whole-payment-to-each) and are skipped,
so the skipped count is 22 + 5 = 27 and 112 of 139 run. A prepaid contract
credited straight to capital is property (1,500.00 each) and a service paid
through Due to Members is cash (1,000.00 each); the contract enters the tax
capital at its book amount with the note; a 1,000.00 check naming Dana Studio
at 600.00 counts 600.00 toward Dana's 1099.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-09 12:05:15 -07:00

341 lines
13 KiB
Rust

use crate::*;
/// A JSON object: the shape every figure query answers in.
pub(crate) type JsonMap = serde_json::Map<String, Value>;
/// The `AccountType` an example's account type names.
pub(crate) fn account_type(a: &AccountSpec, id: &str) -> AccountType {
match a.kind.as_str() {
"asset" => AccountType::Asset,
"liability" => AccountType::Liability,
"equity" => AccountType::Equity,
"revenue" => AccountType::Revenue,
"expense" => AccountType::Expense,
other => panic!("{id}: account type {other:?}"),
}
}
/// The period `[from, to]` a period query covers, each date omitted
/// resolved by the one rule (BS-period-3) on `setup.today`; panics on
/// `as_of`, on an omitted date without `setup.today`, and on a period the
/// rule refuses.
pub(crate) fn period(ex: &Example, x: &Expect) -> (NaiveDate, NaiveDate) {
let q = x.query();
resolved_q_period(ex, &q).unwrap_or_else(|e| panic!("{}: {}: {e}", ex.id, x.query))
}
/// [`period`], or the rule's refusal.
pub(crate) fn resolved_period(
ex: &Example,
x: &Expect,
) -> Result<(NaiveDate, NaiveDate), LedgerError> {
resolved_q_period(ex, &x.query())
}
/// A query's period, or the rule's refusal.
pub(crate) fn resolved_q_period(
ex: &Example,
q: &Query,
) -> Result<(NaiveDate, NaiveDate), LedgerError> {
let id = &ex.id;
assert!(
q.as_of.is_none(),
"{id}: {} covers a period (from, to), not as_of",
q.name
);
let from = q.from.as_deref().map(|s| date(s, id));
let to = q.to.as_deref().map(|s| date(s, id));
// A `period` query asks the rule itself, both dates given or not.
if let (Some(from), Some(to)) = (from, to)
&& q.name != "period"
{
assert!(from <= to, "{id}: {} from {from} after to {to}", q.name);
return Ok((from, to));
}
defaults(ex, &q.name).statement(from, to)
}
/// The day an as-of query is made as of (omitted: `setup.today`,
/// BS-period-3); panics on `from` or `to`.
pub(crate) fn day(ex: &Example, x: &Expect) -> NaiveDate {
let q = x.query();
resolved_q_day(ex, &q).unwrap_or_else(|e| panic!("{}: {}: {e}", ex.id, x.query))
}
/// A query's day, or the rule's refusal.
pub(crate) fn resolved_q_day(ex: &Example, q: &Query) -> Result<NaiveDate, LedgerError> {
let id = &ex.id;
assert!(
q.from.is_none() && q.to.is_none(),
"{id}: {} is as of a day (as_of), not a period",
q.name
);
match q.as_of.as_deref() {
Some(s) => Ok(date(s, id)),
None => defaults(ex, &q.name).as_of(None).pipe(Ok),
}
}
/// Rust has no pipe; this keeps the day rule a single expression.
pub(crate) trait Pipe: Sized {
fn pipe<T>(self, f: impl FnOnce(Self) -> T) -> T {
f(self)
}
}
impl<T> Pipe for T {}
/// What an omitted date means for `ex` (BS-period-3): its fiscal year
/// on `setup.today`, which `query` needs.
pub(crate) fn defaults(ex: &Example, query: &str) -> PeriodDefaults {
let today = ex.setup.today.as_deref().unwrap_or_else(|| {
panic!(
"{}: {query} omits a date: setup.today says what it means (BS-period-3)",
ex.id
)
});
let company = Company {
fiscal_year_start: fiscal_year_start(ex),
..Company::default()
};
period_defaults(&company, date(today, &format!("{} today", ex.id)))
}
/// `setup.fiscal_year_start`, MM-DD, as (month, day).
pub(crate) fn fiscal_year_start(ex: &Example) -> (u32, u32) {
let s = &ex.setup.fiscal_year_start;
let (m, d) = s
.split_once('-')
.unwrap_or_else(|| panic!("{}: fiscal_year_start {s:?}", ex.id));
let md = (
m.parse()
.unwrap_or_else(|_| panic!("{}: fiscal_year_start {s:?}", ex.id)),
d.parse()
.unwrap_or_else(|_| panic!("{}: fiscal_year_start {s:?}", ex.id)),
);
assert!(
NaiveDate::from_ymd_opt(2001, md.0, md.1).is_some(),
"{}: fiscal_year_start {s:?}",
ex.id
);
md
}
/// A refusal in the examples' words (bookspec/README.md), engine-neutral:
/// what was refused, never the engine's message.
pub(crate) fn refusal(e: &LedgerError) -> String {
match e {
LedgerError::InvalidDateRange { .. } => "end_before_start",
LedgerError::StartAfterToday { .. } => "start_after_today",
LedgerError::EmptyAccountName => "blank",
LedgerError::AccountNameUnchanged { .. } => "unchanged",
LedgerError::DuplicateAccountName { .. } => "sibling_has_it",
LedgerError::AccountNameHoldsTin => "holds_a_tin",
LedgerError::InvalidAccountName(_) => "not_one_line",
LedgerError::DuplicateAccountCode(_) => "code_taken",
LedgerError::TaxCategoryRequired { .. } => "tax_category_required",
LedgerError::DepreciationPastDisposal(_) => "depreciation_past_disposal",
// A reversal dated before its original (D-73) is the examples'
// "entry_irreversible" too: bookspec's depreciation example words
// the refusal that way, whichever engine check makes it.
LedgerError::AssetEntryIrreversible(..) | LedgerError::ReversalBeforeOriginal { .. } => {
"entry_irreversible"
}
LedgerError::AssetAlreadyDisposed(_) => "already_disposed",
LedgerError::InvalidDepreciationPeriod(_) => "invalid_depreciation_period",
LedgerError::InvalidSchedule(_) => "invalid_schedule",
LedgerError::UntaggedControlLine { .. } => "untagged_control_line",
LedgerError::PaymentBeforeDocument { .. } => "payment_before_document",
LedgerError::DuplicateVendorRef { .. } => "duplicate_vendor_reference",
// An invoice with a bill, and documents of two parties, are the one
// refusal: a customer and a vendor are two parties (BS-arap-6).
LedgerError::SettlementMixedKinds(_) | LedgerError::SettlementPartiesDiffer(..) => {
"settlement_mixes_parties"
}
LedgerError::SettlementDuplicateDocument(_) => "settlement_repeats_a_document",
LedgerError::Overpayment(..) => "application_above_owed",
LedgerError::DiscountNeedsReason { .. } => "discount_needs_reason",
LedgerError::DiscountOnAssetBill { .. } => "discount_on_asset_bill",
LedgerError::FeeAboveCash { .. } => "fee_above_cash",
LedgerError::UnbalancedEntry { .. } => "unbalanced",
LedgerError::TooFewLines => "too_few_lines",
LedgerError::NegativeAmountLine => "negative_amount",
LedgerError::ZeroAmountLine => "zero_amount",
LedgerError::FinerThanMinorUnit { .. } => "finer_than_minor_unit",
LedgerError::ParentTypeMismatch { .. } => "parent_type_mismatch",
LedgerError::EntryAlreadyReversed(..) => "already_reversed",
LedgerError::ClosedPeriod(..) => "period_closed",
// A period that shares days with another while neither contains the
// other (BS-period-4, and BS-period-2 for an engine that nests).
LedgerError::PeriodsOverlap { .. } => "period_overlaps",
// A line on a header account (a placeholder takes no postings),
// BS-entry-1 and BS-entry-6.
LedgerError::PlaceholderAccount(_) => "header_account",
// A close with a draft dated in its period (BS-period-2).
LedgerError::DraftsInPeriod(_) => "drafts_in_period",
// A draft's command on an entry that is no draft: it is posted
// already (a post, a state, a discard), or was discarded and is
// gone (BS-entry-5).
LedgerError::EntryAlreadyPosted(_)
| LedgerError::PostedEntryImmutable
| LedgerError::UnknownEntry(_) => "not_drafted",
// A date outside 0001-01-01 through 9999-12-31, or an amount past
// a line's cap: refused rather than wrapped (BS-entry-4,
// BS-entry-7).
LedgerError::DateOutOfRange(_) => "date_out_of_range",
LedgerError::AmountOutOfRange => "amount_out_of_range",
// A role assigned to an account that is not postable or not of the
// role's type (BS-ledger-19).
LedgerError::ControlAccountMismatch { .. } => "role_account_mismatch",
// A 1099 threshold for a year the books do not know (BS-1099-2).
LedgerError::RuleNotKnown { .. } => "unknown_1099_year",
// A platform's payout whose deposit is not gross less fee
// (BS-revenue-1): `payout_plan` refuses it for no other reason that
// the examples name.
LedgerError::InvalidPayout(why) if why.starts_with("payout doesn't reconcile") => {
"payout_does_not_reconcile"
}
// A tag of a wrong form, and one another asset holds or ever held
// (BS-asset-3).
LedgerError::InvalidAssetTag { .. } => "asset_tag_invalid",
LedgerError::AssetTagTaken { .. } => "asset_tag_taken",
other => return format!("another refusal: {other}"),
}
.to_string()
}
/// [`refusal`] of a command the books refused: the engine's reason, or the
/// store's own (none is in the examples' words).
pub(crate) fn store_refusal(e: &StoreError) -> String {
match e {
StoreError::Ledger(e) => refusal(e),
// The evidence gate (BS-entry-5): the entry needs a document.
StoreError::ReceiptRequired(_) => "evidence_required".to_string(),
other => format!("another refusal: {other}"),
}
}
/// The text of the bookspec example file `file`, carried by the pinned
/// `bookspec-checker` crate.
pub(crate) fn example_text(file: &str) -> &'static str {
bookspec_checker::examples()
.into_iter()
.find(|(name, _)| *name == file)
.unwrap_or_else(|| panic!("no bookspec example {file}"))
.1
}
pub(crate) fn date(s: &str, what: &str) -> NaiveDate {
NaiveDate::parse_from_str(s, "%Y-%m-%d").unwrap_or_else(|e| panic!("{what}: {s:?}: {e}"))
}
/// An exact decimal string: digits, an optional sign and point; never a
/// float's exponent.
pub(crate) fn exact(s: &str, what: &str) -> Decimal {
assert!(
!s.is_empty()
&& s.chars()
.all(|c| c.is_ascii_digit() || c == '.' || c == '-')
&& s.matches('.').count() <= 1,
"{what}: {s:?} is not an exact decimal string"
);
s.parse()
.unwrap_or_else(|e| panic!("{what}: {s:?} is not a decimal: {e}"))
}
/// The refusal a store error is reported as: the examples' words when the
/// engine's reason is one of them, the engine's own otherwise.
pub(crate) fn store_refusal_obj(e: &StoreError) -> Refusal {
match e {
StoreError::Ledger(e) => Refusal::of_reason(refusal(e)),
StoreError::ReceiptRequired(_) => Refusal::of_reason("evidence_required"),
other => Refusal::of_reason(format!("another refusal: {other}")),
}
}
/// The worker's refusal for an engine error the examples' words don't
/// name.
pub(crate) fn engine_refusal(rule: RuleId, e: impl std::fmt::Display) -> Refusal {
Refusal::new(rule, format!("another refusal: {e}"))
}
/// A JSON object of named strings (every figure an exact decimal string,
/// never a number).
pub(crate) fn json_figures<I>(figures: I) -> Value
where
I: IntoIterator<Item = (String, String)>,
{
Value::Object(
figures
.into_iter()
.map(|(k, v)| (k, Value::String(v)))
.collect(),
)
}
/// The accounts the codes name.
pub(crate) fn codes(l: &Ledger, cs: &[String], id: &str) -> Vec<ledger_core::AccountId> {
cs.iter()
.map(|c| {
l.chart()
.find_by_code(c)
.unwrap_or_else(|| panic!("{id}: no account {c}"))
.id
})
.collect()
}
/// Every key is one of `keys`, and every one of `keys` is given: a figure
/// query lists everything it covers, so an example can't leave a figure
/// unchecked by leaving it out.
pub(crate) fn exactly(ex: &Example, x: &Expect, keys: &BTreeSet<String>) {
let given: BTreeSet<String> = x.figures(&ex.id).keys().cloned().collect();
assert_eq!(
&given, keys,
"{}: {} lists exactly what it covers",
ex.id, x.query
);
}
/// No dates: a query about one thing the books did, not about a day.
pub(crate) fn undated(ex: &Example, x: &Expect) {
assert!(
x.from.is_none() && x.to.is_none() && x.as_of.is_none(),
"{}: {} takes no dates",
ex.id,
x.query
);
}
/// A month key, "YYYY-MM", checked.
pub(crate) fn month_key(key: &str, what: &str) {
assert!(
key.len() == 7 && NaiveDate::parse_from_str(&format!("{key}-01"), "%Y-%m-%d").is_ok(),
"{what}: {key:?} is no month (YYYY-MM)"
);
}
/// Check expectations whose values are named figures: each key one of
/// `figures`' (an unknown one refused), its value the figure's text.
pub(crate) fn named<K: Borrow<str> + Ord>(
ex: &Example,
x: &Expect,
what: &str,
figures: &BTreeMap<K, String>,
) {
for (key, want) in x.figures(&ex.id) {
let got = figures
.get(key.as_str())
.unwrap_or_else(|| panic!("{what}: no figure {key:?}"));
assert_eq!(got, want, "{what}: {key}");
}
}
/// A decimal figure as an example writes it: two places at least, as the
/// books keep money (exact; "0.00", "-14.20").
pub(crate) fn figure(d: Decimal) -> String {
let mut d = d.normalize();
if d.scale() < 2 {
d.rescale(2);
}
d.to_string()
}