#!/usr/bin/env python3 """Regression test: credit-pack reconciliation (issue #7). WHY THIS EXISTS Credits are granted on exactly ONE path — the OC `order.processed` webhook — and that path can fail AFTER the money has moved. On 2026-11-13 two purchases did: a $50 pack whose delivery 500'd (httpx's invisible 5-second default), and a $10 pack with no delivery in the retained log at all. Neither produced a ledger row, and nothing on a schedule noticed. The reconcile pass reads OC's OWN order list and compares it with our books. The thing that makes this dangerous rather than merely absent is the matching: our own stored references are NOT uniform, so the obvious `oc::` test reported 21 already-credited purchases as missing on its first run. A pass that "fixes" those would double-credit members. This test pins down, case by case, what counts as accounted for: * newer ledger rows carry the v2 id `oc:alice:or_…` * older ledger rows carry a HYPHENATED id `oc:bob:8k03reyd-…` * legacy rows carry an EMPTY suffix `oc:erin:` * non-member purchases live in `held_credits`, not the ledger, and are accounted for — they took the hold path on purpose and that a purchase with NO such record is reported (dry run) or granted ONCE (apply), using the same reference the webhook writes, so the two paths are idempotent against each other in both directions. Network is STUBBED; assertions are on our own recorded state. Runnable: python3 tests/test_credit_pack_reconcile.py (from the repo root) """ import os import sys import asyncio import tempfile from datetime import datetime, timedelta, timezone import httpx os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "fixture.db") sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import app.main as m # noqa: E402 m.DB_PATH = os.environ["DB_PATH"] m.ADMIN_TOKEN = "test-admin-token" m.ADMIN_TOKEN_LEGACY = "" m.CREDIT_RECONCILE = "report" # the shipped default m.CREDIT_RECONCILE_DAYS = 30 NOW = datetime.now(timezone.utc) def iso(days_ago: float) -> str: return (NOW - timedelta(days=days_ago)).strftime("%Y-%m-%dT%H:%M:%SZ") def stamp(days_ago: float) -> str: return (NOW - timedelta(days=days_ago)).strftime("%Y-%m-%d %H:%M:%S") def order(oid, slug, amount, days_ago=1.0, tier="Credit pack", status="PAID", frequency="ONETIME"): return {"id": oid, "status": status, "createdAt": iso(days_ago), "frequency": frequency, "amount": {"value": amount, "currency": "USD"}, "platformTipAmount": {"value": 0}, "totalAmount": {"value": amount}, "tier": {"name": tier, "slug": (tier or "").lower().replace(" ", "-")}, "fromAccount": {"slug": slug, "name": slug.title(), "type": "INDIVIDUAL"}} ORDERS = [ # accounted by the EXACT v2 reference (and the ledger amount deliberately # disagrees, to prove the exact match does not need the fuzzy second signal) order("or_AAA", "alice", 50, 2.0), # accounted only by the SECOND signal: older rows carry a hyphenated id order("or_BBB", "bob", 50, 3.0), # accounted only by the SECOND signal: a legacy EMPTY-suffix reference order("or_EEE", "erin", 5, 4.0), # accounted as HELD — a non-member purchase recorded on the hold path order("or_HHH", "holdguy", 10, 5.0), # GENUINELY UNACCOUNTED, and by an active member → the one real candidate order("or_CCC", "carol", 10, 0.5), # unaccounted, but the buyer is not an active member → hold path, not a grant order("or_DDD", "dave", 10, 0.7), # unaccounted and active, but outside the window → not this pass's business order("or_FFF", "frank", 10, 100.0), # not a credit pack / not paid / not one-time → never examined as a pack order("or_G1", "carol", 15, 1.0, tier="Membership"), order("or_G2", "carol", 10, 1.0, status="PENDING"), order("or_G3", "carol", 10, 1.0, status="REFUNDED"), order("or_G4", "carol", 10, 1.0, frequency="MONTHLY"), ] class Boom(Exception): pass async def fake_orders(per_page=100, max_pages=3): if FAIL_ORDERS: raise Boom("OC said 500") return list(ORDERS) async def fake_index(): if FAIL_INDEX: raise Boom("member index unavailable") return {e: {"slug": e.split("@")[0]} for e in ("alice@x.org", "bob@x.org", "carol@x.org", "dave@x.org", "erin@x.org", "frank@x.org", "gina@x.org")} BUDGET_PUSHES = [] SENT = [] def fake_send(to, subject, body, *a, **k): SENT.append({"to": to, "subject": subject, "body": body}) async def fake_budget(email): BUDGET_PUSHES.append(email) return 25.0 FAIL_ORDERS = False FAIL_INDEX = False results = [] def check(name, ok): results.append((name, bool(ok))) def ledger_rows(): conn = m.get_db() conn.row_factory = __import__("sqlite3").Row rows = [dict(r) for r in conn.execute( "SELECT email, delta, reference, created_at FROM credit_ledger ORDER BY id")] conn.close() return rows def balance(email): return m.get_credit_balance(email) def seed(): conn = m.get_db() conn.execute("DELETE FROM members") conn.execute("DELETE FROM credit_ledger") conn.execute("DELETE FROM held_credits") # A fresh world: the report markers are part of the state under test, so a # leftover fingerprint from an earlier section would silence a real notice. conn.execute("DELETE FROM portal_state") for email, slug, active in (("alice@x.org", "alice", 1), ("bob@x.org", "bob", 1), ("carol@x.org", "carol", 1), ("dave@x.org", "dave", 0), ("erin@x.org", "erin", 1), ("frank@x.org", "frank", 1)): conn.execute( "INSERT INTO members (email, slug, active, account_type, credit_balance) " "VALUES (?, ?, ?, 'member', 0)", (email, slug, active)) # alice: the v2 form, with an amount that DISAGREES with the order ($48 ≠ $50) conn.execute("INSERT INTO credit_ledger (email, delta, reason, reference, created_at) " "VALUES ('alice@x.org', 48.0, 'credit pack', ?, ?)", ("oc:alice:or_AAA", stamp(2.0))) # bob: the hyphenated legacy id for the SAME order (or_BBB) conn.execute("INSERT INTO credit_ledger (email, delta, reason, reference, created_at) " "VALUES ('bob@x.org', 50.0, 'credit pack', ?, ?)", ("oc:bob:8k03reyd-5agmq5x4-4nkplbwo-z7j4nxv9", stamp(3.0))) # erin: the empty-suffix legacy form conn.execute("INSERT INTO credit_ledger (email, delta, reason, reference, created_at) " "VALUES ('erin@x.org', 5.0, 'credit pack', 'oc:erin:', ?)", (stamp(4.0),)) # holdguy: a non-member purchase held, never a ledger row conn.execute("INSERT INTO held_credits (slug, email, name, amount, oc_reference, " "status, created_at) VALUES ('holdguy', 'hold@x.org', 'Hold Guy', 10.0, " "'oc:holdguy:', 'pending', ?)", (stamp(5.0),)) conn.commit() conn.close() def names(res): return sorted(c["slug"] for c in res["candidates"]) async def main(): global FAIL_ORDERS, FAIL_INDEX m.fetch_collective_orders = fake_orders m.fetch_member_index = fake_index m.sync_team_budget_from_balance = fake_budget m.send_email = fake_send # ---------- dry run: report only, change nothing -------------------------- seed() before = ledger_rows() res = await m.reconcile_credit_packs(apply=False) check(f"dry run reports ok (got {res.get('status')})", res.get("status") == "ok") check("dry run is marked dry_run", res.get("dry_run") is True) check(f"packs in window counted (got {res.get('packs_in_window')})", res.get("packs_in_window") == 6) check(f"only the unaccounted active-member pack is a candidate (got {names(res)})", names(res) == ["carol"]) check(f"the inactive member's pack is a hold, not a grant (got {res['needs_hold']})", [c["slug"] for c in res["needs_hold"]] == ["dave"]) check("dry run applied nothing", res.get("applied") == []) check("dry run wrote no ledger row", ledger_rows() == before) check(f"dry run left carol's balance alone (got {balance('carol@x.org')})", balance("carol@x.org") == 0.0) check("dry run pushed no budget", BUDGET_PUSHES == []) # the three accounting shapes that a v2-only match would have called missing for oid, slug, why in (("or_AAA", "alice", "exact v2 reference"), ("or_BBB", "bob", "hyphenated legacy id"), ("or_EEE", "erin", "empty-suffix legacy reference"), ("or_HHH", "holdguy", "held (non-member) purchase")): o = next(x for x in ORDERS if x["id"] == oid) ev = m.credit_pack_accounted(o, ledger_rows(), [ dict(r) for r in conn_rows("SELECT id, slug, email, amount, oc_reference, status, created_at FROM held_credits") ]) check(f"accounted: {why} ({oid}) -> {ev}", ev is not None) check("a genuinely unaccounted order stays unaccounted", m.credit_pack_accounted(next(x for x in ORDERS if x["id"] == "or_CCC"), ledger_rows(), []) is None) # A manual grant with NO reference links to its member by EMAIL, and the slug # and email local part routinely differ ("henry-smith" vs "h.smith@x.org"). # Matching only on the slug in the reference drops that row — which is how a # hand-credited purchase comes to look like a gap. henry = order("or_HH2", "henry-smith", 12, 1.0) henry_rows = [{"email": "h.smith@x.org", "delta": 12.0, "reference": None, "created_at": stamp(1.0), "reason": "manual application"}] check("a reference-less manual grant is matched via the MEMBER EMAIL", m.credit_pack_accounted(henry, henry_rows, [], member_email="h.smith@x.org") is not None) check("...and is NOT matched without it (the slug alone finds nothing)", m.credit_pack_accounted(henry, henry_rows, []) is None) # ---------- a read that fails is UNKNOWN, never 'nothing missing' --------- FAIL_ORDERS = True res = await m.reconcile_credit_packs(apply=False) check(f"order fetch failure is skipped/order_fetch_failed (got {res})", res.get("status") == "skipped" and res.get("reason") == "order_fetch_failed") FAIL_ORDERS = False FAIL_INDEX = True res = await m.reconcile_credit_packs(apply=False) check(f"member index failure is skipped/member_index_failed (got {res})", res.get("status") == "skipped" and res.get("reason") == "member_index_failed") FAIL_INDEX = False # ---------- apply=1 while the policy is still 'report' -------------------- seed() res = await m.reconcile_credit_packs(apply=True) check("apply is IGNORED while CREDIT_RECONCILE != 'on'", res.get("dry_run") is True and res.get("applied") == []) check(f"and still nothing was credited (got {balance('carol@x.org')})", balance("carol@x.org") == 0.0) # ---------- CREDIT_RECONCILE=on: grant exactly once ---------------------- seed() SENT.clear() m.CREDIT_RECONCILE = "on" res = await m.reconcile_credit_packs(apply=True) check(f"apply grants the one candidate (got {res.get('applied')})", [c["slug"] for c in res.get("applied", [])] == ["carol"]) check(f"carol credited $10 (got {balance('carol@x.org')})", balance("carol@x.org") == 10.0) ref = f"oc:carol:or_CCC" check(f"the grant used the webhook's own reference ({ref})", any(r["reference"] == ref for r in ledger_rows())) check(f"exactly one ledger row was added (got {len(ledger_rows())})", len(ledger_rows()) == 4) check("the budget was pushed once", BUDGET_PUSHES == ["carol@x.org"]) check("the inactive member was NOT credited", balance("dave@x.org") == 0.0) check("the out-of-window member was NOT credited", balance("frank@x.org") == 0.0) # The money notice: someone is told when a purchase had to be recovered — # and NOT told again about a set they have already heard about. check(f"the grant mails a notice naming the order (got {len(SENT)})", len(SENT) == 1 and "or_CCC" in SENT[0]["body"] and "carol" in SENT[0]["body"] and SENT[0]["to"] == "info@inference.coop") # the webhook and this pass must dedupe each other, whichever runs first check("the webhook would now answer already_credited for this order", m.credit_reference_exists(m.credit_pack_reference("carol", {"idV2": "or_CCC"}))) res = await m.reconcile_credit_packs(apply=True) check(f"a second apply changes nothing (got {res.get('applied')})", res.get("applied") == [] and res.get("candidates") == []) check(f"carol's balance unchanged by the re-run (got {balance('carol@x.org')})", balance("carol@x.org") == 10.0) check(f"still one row for the order (got {len(ledger_rows())})", len(ledger_rows()) == 4) check(f"a re-run with nothing to report mails nothing (got {len(SENT)})", len(SENT) == 1) # ---------- a candidate must SHOW the nearby rows a human needs ---------- # A manual grant can be real and carry no per-order reference: ntnsndr's three # launch-day $5 packs were credited as +20.00 then corrected by -10.75, netting # exactly 3 x $5.00, with no reference on any row. The matcher must NOT read # that as accounted for (that would hide real gaps), so the candidate has to # carry enough context for a reviewer to tell the two apart. seed() conn = m.get_db() conn.execute("INSERT INTO members (email, slug, active, account_type, credit_balance) " "VALUES ('gina@x.org', 'gina', 1, 'member', 0)") conn.execute("INSERT INTO credit_ledger (email, delta, reason, reference, created_at) " "VALUES ('gina@x.org', 7.5, 'manual application', NULL, ?)", (stamp(0.5),)) conn.commit() conn.close() async def only_gina(per_page=100, max_pages=3): return [order("or_GGG", "gina", 10, 0.5)] saved_orders = m.fetch_collective_orders m.fetch_collective_orders = only_gina res = await m.reconcile_credit_packs(apply=False) cand = (res.get("candidates") or [{}])[0] check(f"an unreferenced nearby grant is SURFACED on the candidate " f"(got {cand.get('nearby_book_rows')})", any(abs(float(n["delta"]) - 7.5) < 0.01 for n in cand.get("nearby_book_rows") or [])) check("a nearby unreferenced grant does NOT count as accounted for", len(res.get("candidates") or []) == 1) check(f"the pass still leaves the money alone (got {balance('gina@x.org')})", balance("gina@x.org") == 0.0) m.fetch_collective_orders = saved_orders # ---------- the endpoint ------------------------------------------------- # Re-seed: the direct apply above already credited carol, so the endpoint # would have no candidate and the check would pass for the wrong reason. seed() m.CREDIT_RECONCILE = "report" async def endpoint_checks(): transport = httpx.ASGITransport(app=m.app) async with httpx.AsyncClient(transport=transport, base_url="http://t") as c: r = await c.post("/admin/credit-pack-reconcile") check(f"endpoint needs the admin token (got {r.status_code})", r.status_code in (401, 403)) r = await c.post("/admin/credit-pack-reconcile", headers={"X-Admin-Token": "test-admin-token"}) body = r.json() check(f"endpoint defaults to a DRY RUN (got {body})", r.status_code == 200 and body.get("dry_run") is True) r = await c.post("/admin/credit-pack-reconcile?apply=1", headers={"X-Admin-Token": "test-admin-token"}) body = r.json() check("endpoint ?apply=1 is still a dry run while the policy is 'report'", body.get("dry_run") is True and body.get("applied") == []) m.CREDIT_RECONCILE = "on" r = await c.post("/admin/credit-pack-reconcile?apply=1", headers={"X-Admin-Token": "test-admin-token"}) body = r.json() check(f"endpoint ?apply=1 grants once the policy is 'on' (got {body})", body.get("dry_run") is False and [x["slug"] for x in body.get("applied", [])] == ["carol"]) m.CREDIT_RECONCILE = "report" await endpoint_checks() def conn_rows(sql): conn = m.get_db() conn.row_factory = __import__("sqlite3").Row rows = conn.execute(sql).fetchall() conn.close() return rows asyncio.run(main()) fails = sum(not ok for _, ok in results) for name, ok in results: print(f" [{'PASS' if ok else 'FAIL'}] {name}") print(f"\n{len(results) - fails}/{len(results)} passed") sys.exit(1 if fails else 0)