Files
member-portal/tests/test_settle_held_credits.py
T
inference-admin e9d0fd259a
ci/woodpecker/push/woodpecker Pipeline was canceled
portal: settle held credits in the sweep, and report the ones nobody can be credited for
A credit pack bought while the buyer's membership was inactive is held — correctly,
we must never mint credits for a non-member. The gap was what happened next: the hold
sat until a human noticed (mmoya's $10 waited from 01:45 until they wrote in).

Every sweep now grants any hold that resolves to an ACTIVE member (by email first, then
slug), reusing grant_held_credits() so held:<id> keeps it idempotent; holds with no
active member older than HELD_ALERT_HOURS are emailed to info@. Tests cover grant/hold/
no-double-apply.
2026-11-17 04:51:40 +00:00

34 lines
1.8 KiB
Python

import asyncio, os, sys, tempfile
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "settle.db")
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import app.main as m
m.DB_PATH = os.environ["DB_PATH"]
def seed():
conn = m.get_db()
for email, slug, active, credits in (
("active@example.org", "active-slug", 1, 0.0),
("inactive@example.org", "inactive-slug", 0, 0.0),
):
conn.execute("INSERT OR REPLACE INTO members (email, slug, active, credit_balance) VALUES (?,?,?,?)",
(email, slug, active, credits))
conn.execute("INSERT INTO held_credits (slug, email, name, amount, oc_reference) VALUES (?,?,?,?,?)",
("active-slug", "active@example.org", "A", 10.0, "oc:active-slug:xyz"))
conn.execute("INSERT INTO held_credits (slug, email, name, amount, oc_reference) VALUES (?,?,?,?,?)",
("inactive-slug", "inactive@example.org", "I", 5.0, "oc:inactive-slug:xyz"))
conn.commit(); conn.close()
def credits(email):
conn = m.get_db(); v = conn.execute("SELECT credit_balance FROM members WHERE email=?", (email,)).fetchone()[0]; conn.close(); return v
seed()
r1 = asyncio.run(m.settle_held_credits(send=False))
print(" run1:", r1["held"], "held |", len(r1["granted"]), "granted |", len(r1["unresolved"]), "unresolved")
assert credits("active@example.org") == 10.0, "active member's hold must be credited"
assert credits("inactive@example.org") == 0.0, "inactive member must NOT be credited"
assert len(r1["granted"]) == 1 and len(r1["unresolved"]) == 1
r2 = asyncio.run(m.settle_held_credits(send=False))
assert credits("active@example.org") == 10.0, "re-run must not double-apply"
assert len(r2["granted"]) == 0, "second run grants nothing"
print("SETTLE HELD CREDITS OK: grants to active, holds for inactive, idempotent")