coffer/tests/test_public_ledger.py
leora 27f66a1878 Public ledger + News publish on every change
All Coffer txs are public (agent/company ids only, no human PII).
Snapshot to public-ledger.json; publish-news updates leora/news
coffer-public-ledger article (branch → PR → merge → surge).
Design note locks the rule. Quarry 3005d129.
2026-10-05 23:03:50 +08:00

62 lines
2.5 KiB
Python

import tempfile
import unittest
from pathlib import Path
from coffer.news_publish import ensure_ledger_table_css, upsert_ledger_article
from coffer.product import CofferProduct
from coffer.public_ledger import render_ledger_article, tx_public
class PublicLedgerTests(unittest.TestCase):
def test_tx_public_drops_draw_detail_keeps_ids(self):
raw = {
"seq": 1,
"kind": "contract_payment",
"amount_credits": "200",
"amount_centicredits": 20000,
"debit_account": "company:coffer",
"credit_account": "agent:leora",
"reason": "build-contract",
"meta": {
"contract": "coffer-product-build",
"draws": [{"kind": "funded", "amount": 20000}],
"handler_email": "should-not-leak@example.com",
},
}
pub = tx_public(raw)
self.assertEqual(pub["debit_account"], "company:coffer")
self.assertNotIn("draws", pub["meta"])
self.assertNotIn("handler_email", pub["meta"])
self.assertEqual(pub["meta"]["contract"], "coffer-product-build")
def test_article_has_no_human_pii_markers(self):
product = None
with tempfile.TemporaryDirectory() as tmp:
product = CofferProduct(Path(tmp) / "c.db")
product.bootstrap_opening_books()
product.record_build_contract()
snap = product.public_snapshot()
html = snap["article_html"]
self.assertIn('id="coffer-public-ledger"', html)
self.assertIn("company:coffer", html)
self.assertIn("agent:leora", html)
self.assertIn("contract_payment", html)
self.assertNotIn("handler_email", html.lower())
self.assertNotIn("@", html) # no emails
self.assertNotRegex(html.lower(), r"[a-z0-9._%+-]+@[a-z0-9.-]+")
product.close()
def test_upsert_replaces_article(self):
base = "<html><style></style><main><h1>News</h1><article id=\"coffer-public-ledger\"><p>old</p></article><article><h2>Other</h2></article></main></html>"
base = ensure_ledger_table_css(base)
new = ' <article id="coffer-public-ledger">\n <p>new ledger</p>\n </article>\n'
out = upsert_ledger_article(base, new)
self.assertEqual(out.count("coffer-public-ledger"), 1)
self.assertIn("new ledger", out)
self.assertNotIn("old", out)
self.assertIn("coffer-ledger-css", out)
if __name__ == "__main__":
unittest.main()