#!/usr/bin/env python3 """Regression tests for app.main._payment_verdict (allowance payment assessment). Self-contained and runnable: * from the repo root: python3 tests/test_payment_verdict.py * inside the portal app: PORTAL_CODE_DIR=/app/code python3 /tmp/test_payment_verdict.py (the container has the app's own dependencies; the host usually does not) Covers the FREQUENCY-AWARE decision: - ACTIVE YEARLY whose coverage window includes the cycle -> paid (renew) [the regression this file exists for] - ACTIVE YEARLY whose year has lapsed -> not paid - ACTIVE MONTHLY -> paid - PROCESSING -> paid (paid-pending; never a reminder) - ONETIME-only -> not paid (credit, not an allowance cycle) - an ONETIME payment does NOT mask an unpaid monthly member - ERROR MONTHLY with the last charge before the cycle -> not paid (mark-meyer's exact order, asserted to keep his current, correct outcome) - CANCELLED MONTHLY with an old charge -> not paid Does NOT cover (deliberate): the OC network fetch, the members table / anchor-day storage, email sending, the report/send modes, or deactivation. It tests the pure decision function only. The yearly case is SYNTHETIC: the collective has no yearly orders today, so this guards the branch without implying any member has one. """ import os import sys from datetime import datetime, timezone # Locate the app package: explicit env, the deployed path, or the repo root. 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 from app.main import _payment_verdict # noqa: E402 NOW = datetime(2026, 11, 4, 12, 0, tzinfo=timezone.utc) CYCLE = datetime(2026, 10, 26, 0, 0, tzinfo=timezone.utc) # mark-meyer join-day 26 def order(freq, status, last=None, next_=None): return {"frequency": freq, "status": status, "lastChargedAt": last, "nextChargeDate": next_} # (name, orders, expected_paid, evidence_must_contain) CASES = [ ("active yearly covers this cycle (last charge 2026-09-26, next 2027-09-01)", [order("YEARLY", "ACTIVE", "2026-09-26T18:00:00.000Z", "2027-09-01T00:00:00.000Z")], True, "yearly"), ("active yearly whose year has lapsed", [order("YEARLY", "ACTIVE", "2024-01-01T00:00:00.000Z", "2025-01-01T00:00:00.000Z")], False, "before this cycle"), ("active monthly", [order("MONTHLY", "ACTIVE", "2026-11-02T18:00:00.000Z", "2026-12-01T00:00:00.000Z")], True, "live recurring contribution"), ("processing monthly is paid-pending", [order("MONTHLY", "PROCESSING", "2026-09-16T19:03:08.000Z", "2026-11-01T00:00:00.000Z")], True, "live recurring contribution"), ("onetime-only is not an allowance cycle", [order("ONETIME", "PAID", "2026-11-01T00:00:00.000Z", None)], False, "no recurring"), ("onetime does not mask an unpaid monthly", [order("ONETIME", "PAID", "2026-11-02T00:00:00.000Z", None), order("MONTHLY", "ERROR", "2026-09-26T18:05:00.000Z", "2026-11-06T00:00:00.000Z")], False, "before this cycle"), ("mark-meyer's exact order: monthly ERROR, last charge 2026-09-26", [order("MONTHLY", "ERROR", "2026-09-26T18:05:00.000Z", "2026-11-06T00:00:00.000Z")], False, "last charge 2026-09-26 is before this cycle"), ("cancelled monthly with old charge", [order("MONTHLY", "CANCELLED", "2026-08-01T11:05:08.000Z", None)], False, "before this cycle"), ("monthly charged within the cycle (7-day slack)", [order("MONTHLY", "ERROR", "2026-10-28T00:00:00.000Z", None)], True, "within the current cycle"), ("no orders at all", [], False, "no recurring"), ] failures = 0 for name, orders, expect_paid, expect_text in CASES: paid, evidence = _payment_verdict(orders, CYCLE, now=NOW) ok = (paid == expect_paid) and (expect_text in evidence) if not ok: failures += 1 print(f"[{'PASS' if ok else 'FAIL'}] {name}\n" f" paid={paid} (expected {expect_paid}) evidence={evidence!r}") print(f"\n{len(CASES) - failures}/{len(CASES)} passed") sys.exit(1 if failures else 0)