Files
member-portal/tests/test_credit_eligibility.py
T
inference-bot ca336a3115
ci/woodpecker/push/woodpecker Pipeline was canceled
credits: only charge spend incurred while credits were available (#12 follow-up)
close_credit_cycles drew max(0, cycle_spend - allowance) capped by the
pack's current balance, so a pack bought mid-cycle was charged for spend
that predated it. Split the cycle at the first grant: allowance is consumed
first, then credits cover only the spend after they became available.
apply_cycle_draw takes the eligible figure; the ledger reason records it.
Correction rows for the six over-drawn clawback cycles are separate.
2026-12-02 15:32:34 +00:00

119 lines
4.6 KiB
Python

#!/usr/bin/env python3
"""Regression tests: a cycle draw may only charge spend credits could cover.
A pack bought mid-cycle must not be charged for spend incurred before it
existed. Reproduces the consensus_polo100 case: $27.81 spent before the $10
pack arrived, then $0.62 after it; the eligible draw is $0.62, not $10.
"""
import os
import sys
import tempfile
from datetime import datetime, timezone
_TEST_DB = os.path.join(tempfile.mkdtemp(prefix="creditelig-test-"), "members.db")
os.environ["DB_PATH"] = _TEST_DB # must exist BEFORE app.main is imported
for _cand in (os.environ.get("PORTAL_CODE_DIR"), "/app/code",
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))):
if _cand and os.path.isfile(os.path.join(_cand, "app", "main.py")):
sys.path.insert(0, _cand)
break
import app.main as m # noqa: E402
failures = 0
def check(name, got, want):
global failures
ok = got == want
if not ok:
failures += 1
print(f"[{'PASS' if ok else 'FAIL'}] {name}")
if not ok:
print(f" got {got!r}\n want {want!r}")
def check_near(name, got, want):
global failures
ok = round(float(got), 6) == round(float(want), 6)
if not ok:
failures += 1
print(f"[{'PASS' if ok else 'FAIL'}] {name}")
if not ok:
print(f" got {got!r}\n want {want!r}")
conn = m.get_db()
m._ensure_allowance_tables(conn) # adds anchor_day
for email, purchased, anchor in [
("midcycle@example.com", 10.0, 15),
("carried@example.com", 50.0, 15),
("none@example.com", 0.0, 15),
("partial@example.com", 20.0, 15)]:
conn.execute(
"INSERT INTO members (email, key_token, active, account_type, balance, "
"credit_balance, credits_drawn, anchor_day) VALUES (?, '', 1, 'member', 15.0, ?, 0, ?)",
(email, purchased, anchor))
# Pack bought inside the cycle, after the allowance had already been overspent.
conn.execute(
"INSERT INTO credit_ledger (email, delta, reason, reference, created_at) "
"VALUES ('midcycle@example.com', 10.0, 'pack', 'pack-mid', '2026-11-12 13:05:26')")
# Pack held from before the cycle.
conn.execute(
"INSERT INTO credit_ledger (email, delta, reason, reference, created_at) "
"VALUES ('carried@example.com', 50.0, 'pack', 'pack-carry', '2026-09-20 10:00:00')")
# Pack arrives when only part of the allowance has been used.
conn.execute(
"INSERT INTO credit_ledger (email, delta, reason, reference, created_at) "
"VALUES ('partial@example.com', 20.0, 'pack', 'pack-part', '2026-10-20 12:00:00')")
conn.commit()
conn.close()
CS = datetime(2026, 10, 15, tzinfo=timezone.utc)
CE = datetime(2026, 11, 15, tzinfo=timezone.utc)
# Spend windows for the mid-cycle member: 27.81376 before the pack, 0.624698 after.
def fake_spend(team_id, start, end):
if team_id == "t-mid":
if end.day == 12 and end.month == 11:
return 27.81376
if start == CS and end == CE:
return 28.438458
if team_id == "t-partial" and start == CS and end == datetime(2026, 10, 20, 12, tzinfo=timezone.utc):
return 5.0
return None
m.cycle_spend_from_ledger = fake_spend
# mid-cycle pack: only spend after the pack is eligible
got = m.credit_eligible_overage("midcycle@example.com", "t-mid", CS, CE, 15.0, 28.438458)
check_near("mid-cycle pack is not charged for earlier spend", got, 0.624698)
# pack carried in: whole overage is eligible
got = m.credit_eligible_overage("carried@example.com", "t-carry", CS, CE, 15.0, 28.438458)
check_near("credits at cycle start cover the whole overage", got, 13.438458)
# no credits: nothing eligible
got = m.credit_eligible_overage("none@example.com", "t-none", CS, CE, 15.0, 28.438458)
check_near("no credits means no eligible draw", got, 0.0)
# partial allowance left: overage above what remains is eligible
got = m.credit_eligible_overage("partial@example.com", "t-partial", CS, CE, 15.0, 20.0)
check_near("allowance is consumed before credits", got, 5.0)
# the eligible figure drives the draw, and the replay still draws nothing
drawn = m.apply_cycle_draw("midcycle@example.com", "2026-10-15", 28.438458, 15.0, eligible=0.624698)
check_near("draw uses the credit-eligible overage", drawn, 0.624698)
check_near("credits_drawn records only that amount", m.get_credits_drawn("midcycle@example.com"), 0.624698)
check_near("purchased total is unchanged", m.get_credit_balance("midcycle@example.com"), 10.0)
check_near("replay draws nothing",
m.apply_cycle_draw("midcycle@example.com", "2026-10-15", 28.438458, 15.0, eligible=0.624698), 0.0)
print()
if failures:
print(f"{failures} FAILURE(S)")
sys.exit(1)
print("all checks passed")