fix(assertions): send Balance directives in Fava's JSON shape (libra-#39)

format_balance returned a Beancount source string, but fava.add_entry
feeds PUT /add_entries whose deserialiser expects
{"t": "Balance", "amount": {"number", "currency"}, ...} — every
assertion create 500'd. Returns the dict shape now.

The seven strict-xfail reconciliation tests tracking this flip to
regular passing tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Padreug 2026-07-12 15:42:44 +02:00
commit c0c8acbe30
2 changed files with 15 additions and 30 deletions

View file

@ -115,13 +115,18 @@ def format_balance(
account: str,
amount: int,
currency: str = "SATS"
) -> str:
) -> Dict[str, Any]:
"""
Format a balance assertion directive for Beancount.
Format a balance assertion directive for Fava's JSON API.
Balance assertions verify that an account has an expected balance on a specific date.
They are checked automatically by Beancount when the file is loaded.
Fava's `deserialise` (fava/serialisation.py) expects
`{"t": "Balance", "amount": {"number", "currency"}, ...}` the
previous source-string return 500'd on every assertion create
(libra-#39).
Args:
date_val: Date of the balance assertion
account: Account name (e.g., "Assets:Bitcoin:Lightning")
@ -129,15 +134,15 @@ def format_balance(
currency: Currency code (default: "SATS")
Returns:
Beancount balance directive as a string
Example:
>>> format_balance(date(2025, 11, 10), "Assets:Bitcoin:Lightning", 1500000, "SATS")
'2025-11-10 balance Assets:Bitcoin:Lightning 1500000 SATS'
Fava API Balance entry dict ready for `fava.add_entry`.
"""
date_str = date_val.strftime('%Y-%m-%d')
# Two spaces between account and amount (Beancount convention)
return f"{date_str} balance {account} {amount} {currency}"
return {
"t": "Balance",
"date": date_val.strftime('%Y-%m-%d'),
"account": account,
"amount": {"number": str(amount), "currency": currency},
"meta": {},
}
def format_posting_with_cost(