panel state: a reminder is history, not a state — renewals clear grace, grace expires
ci/woodpecker/push/woodpecker Pipeline was canceled

mark@ofosos.org showed as in grace while his contribution was live. The sweep's
own trail says it plainly: reminded 2026-11-04 for the cycle that looked unpaid,
grace elapsed 2026-11-11, and `renewed` logged at 19:02 on 2026-11-13 (last
charge 2026-11-11, next reset 2026-11-26). The panel said grace anyway, because
member_membership_state() asked only "is there a row in payment_reminders?".

That table is a HISTORY — kept so the sweep does not re-remind inside a cycle. It
is not a state, and treating it as one broke two things: a reminder never
expired (grace is PAYMENT_GRACE_DAYS long), and a renewal never cleared it.

Now the LATEST reconciliation decision decides:
  renewed / would_renew            -> active (they are paying)
  reminder inside the window       -> in_grace
  reminder past the window         -> awaiting_contribution, saying grace elapsed
  would_deactivate / deactivated   -> awaiting_contribution
Ordered by decided_at, not id: the same member had id 2296 dated 2026-11-11 next
to id 2286 dated 2026-11-13, so "latest by id" reads the wrong decision.

Reads still fail safe: an unreadable DB reports active, never a payment problem.

tests/test_membership_state.py pins all of it, including the ordering trap.
This commit is contained in:
inference-admin committed 2026-11-13 21:53:27 +00:00
1 parent b1f5a30272
commit 241ac800c6
2 files changed
+161 -13

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
#!/usr/bin/env python3
"""Regression test: the panel's membership state must not call a paying member "grace".
WHY THIS EXISTS
On 2026-11-13 mark@ofosos.org showed as in grace. He was current: the reminder
went out 2026-11-04 for the cycle that looked unpaid, his grace window elapsed
2026-11-11, and the reconciliation logged `renewed` for him at 19:02 that
afternoon — his next charge is 2026-11-26, which had not arrived and was not
supposed to.
The state function asked "is there a row in payment_reminders?" and stopped
there. That table is a HISTORY, kept so the sweep does not re-remind inside a
cycle; it is not a state. Two bugs followed: a reminder never expired (grace is
PAYMENT_GRACE_DAYS long), and a renewal never cleared it.
Ordering matters too: the same member had allowance_log id 2296 dated 2026-11-11
next to id 2286 dated 2026-11-13, so "latest by id" reads the WRONG decision.
"""
import os
import sys
import tempfile
import pathlib
from datetime import datetime, timedelta, timezone
ROOT = pathlib.Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "state-fixture.db")
import app.main as m # noqa: E402
m.DB_PATH = os.environ["DB_PATH"]
FAILS = []
def check(cond, msg):
print((" ok " if cond else " FAIL ") + msg)
if not cond:
FAILS.append(msg)
NOW = datetime.now(timezone.utc)
def stamp(dt):
return dt.strftime("%Y-%m-%d %H:%M:%S")
conn = m.get_db()
m._ensure_allowance_tables(conn)
conn.execute("DELETE FROM payment_reminders")
conn.execute("DELETE FROM allowance_log")
EMAIL = "mark@ofosos.org"
conn.execute("INSERT OR REPLACE INTO members (email, active) VALUES (?, 1)", (EMAIL,))
print("1. a RENEWAL after a reminder means active — the actual bug")
conn.execute("INSERT INTO payment_reminders (email, cycle, sent_at) VALUES (?,?,?)",
(EMAIL, "2026-10-26", stamp(NOW - timedelta(days=9)))) # reminded 9d ago
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
(EMAIL, "2026-10-26", "in_grace", stamp(NOW - timedelta(days=9))))
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
(EMAIL, "2026-10-26", "renewed", stamp(NOW - timedelta(hours=2))))
conn.commit()
state, why = m.member_membership_state(EMAIL, True)
check(state == "active", f"paid member reads active, not grace (got {state!r}: {why})")
print("2. the LATEST decision wins, even when an older row has a higher id")
conn.execute("DELETE FROM allowance_log")
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
(EMAIL, "2026-10-26", "renewed", stamp(NOW - timedelta(hours=2)))) # id 1
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
(EMAIL, "2026-10-26", "in_grace", stamp(NOW - timedelta(days=9)))) # id 2, OLDER date
conn.commit()
state, why = m.member_membership_state(EMAIL, True)
check(state == "active",
f"an older-dated row inserted later does not override the renewal (got {state!r}: {why})")
print("3. a reminder INSIDE the grace window still reads in_grace")
conn.execute("DELETE FROM allowance_log")
conn.execute("UPDATE payment_reminders SET sent_at = ? WHERE email = ?",
(stamp(NOW - timedelta(days=2)), EMAIL))
conn.commit()
state, why = m.member_membership_state(EMAIL, True)
check(state == "in_grace", f"two days into a {int(m.PAYMENT_GRACE_DAYS)}-day grace reads in_grace (got {state!r})")
print("4. grace EXPIRES — no reminder row may mean grace forever")
conn.execute("UPDATE payment_reminders SET sent_at = ? WHERE email = ?",
(stamp(NOW - timedelta(days=int(m.PAYMENT_GRACE_DAYS) + 3)), EMAIL))
conn.commit()
state, why = m.member_membership_state(EMAIL, True)
check(state == "awaiting_contribution", f"past the window reads awaiting, not grace (got {state!r})")
check("elapsed" in why, f"and says the grace elapsed (got {why!r})")
print("5. a previewed deactivation is not grace either")
conn.execute("INSERT INTO allowance_log (email, cycle, decision, decided_at) VALUES (?,?,?,?)",
(EMAIL, "2026-10-26", "would_deactivate", stamp(NOW - timedelta(hours=1))))
conn.commit()
state, why = m.member_membership_state(EMAIL, True)
check(state == "awaiting_contribution", f"would_deactivate reads awaiting (got {state!r})")
print("6. nobody flagged is simply active")
conn.execute("DELETE FROM payment_reminders")
conn.execute("DELETE FROM allowance_log")
conn.commit()
state, why = m.member_membership_state(EMAIL, True)
check(state == "active" and why == "", f"clean member reads active with no reason (got {state!r}, {why!r})")
print("7. inactive is still inactive")
check(m.member_membership_state(EMAIL, False)[0] == "inactive", "active=0 reads inactive")
print("8. an unreadable database fails SAFE, not into a grace badge")
real = m.get_db
try:
def boom():
raise RuntimeError("db locked")
m.get_db = boom
state, why = m.member_membership_state(EMAIL, True)
check(state == "active", f"a failed read does not invent a payment problem (got {state!r})")
finally:
m.get_db = real
conn.close()
print()
if FAILS:
print(f"membership state: {len(FAILS)} FAILURE(S)")
sys.exit(1)
print("membership state: OK — renewals clear grace, grace expires, reads fail safe")