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")