#!/usr/bin/env python3 """Regression test: 'oc-incognito' — an OC payer whose OC profile hides its email. WHY THIS EXISTS mmoya@mmoya.org sat in MANUAL_MEMBERS because OC hides the address on their profile (incognito-5ad60182, email null since 2023), so email matching can never find them. But "manual" means hand-provisioned, and the sweep SKIPS manual members entirely — so a paying subscriber had no lapse logic, no dues tracking and no order attribution. Two different facts were sharing one word. An oc-incognito member needs no exemption from reconciliation: the partition already matches OC orders by the PAYER'S SLUG, and members.slug holds exactly that slug for these people. What they need is to stop being skipped. The invariants pinned here: * an explicit source beats MANUAL_MEMBERS membership — that is the whole point; mmoya must not read as manual just because their address is listed; * only 'manual' is exempt (lapse guard, reconciliation skip, shortfall credits). 'oc-incognito' is a payer and gets payer treatment; * an unreadable or unexpected source falls back to 'oc' — the direction that keeps reconciling someone rather than quietly exempting them; * the source column is optional (every existing row is NULL, meaning "not stated"), so this ships without a data migration. """ import os import pathlib import sys import tempfile ROOT = pathlib.Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "source-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) MMOYA = "mmoya@mmoya.org" OTHER = "someone@example.org" conn = m.get_db() # The column arrives with get_db()'s own migration — asserted, not created here, # because "the migration ran" is part of what this test is for. check_cols = {r[1] for r in conn.execute("PRAGMA table_info(members)")} assert "source" in check_cols, "get_db() must migrate in members.source" conn.execute("INSERT OR REPLACE INTO members (email, slug, active) VALUES (?,?,1)", (MMOYA, "incognito-5ad60182")) conn.execute("INSERT OR REPLACE INTO members (email, slug, active) VALUES (?,?,1)", (OTHER, "guest-abc123")) conn.commit() conn.close() m.MANUAL_MEMBERS = {MMOYA, "founder@example.org"} print("1. the fallback: a listed address with no source of its own is manual") check(m.member_source(MMOYA) == "manual", "an address in MANUAL_MEMBERS and nothing else reads manual (before the flip)") check(m.is_manual_member(MMOYA) is True, "…and is exempt as such") print("2. an explicit source BEATS the manual list — this is the mmoya case") check(m.set_member_source(MMOYA, "oc-incognito") is True, "the source is written") check(m.member_source(MMOYA) == "oc-incognito", f"mmoya reads oc-incognito even while listed in MANUAL_MEMBERS (got {m.member_source(MMOYA)!r})") check(m.is_manual_member(MMOYA) is False, "and is therefore NOT exempt: a paying member gets payer treatment") print("3. everyone else is unchanged") check(m.member_source(OTHER) == "oc", "an unlisted member is oc (the default)") check(m.member_source("founder@example.org") == "manual", "an unlisted-in-DB manual address stays manual") check(m.member_source("nobody@nowhere.test") == "oc", "an address with no row at all is oc, not manual") print("4. a bad source value never silently exempts anyone") conn = m.get_db() conn.execute("UPDATE members SET source = 'banana' WHERE lower(email) = ?", (OTHER,)) conn.commit(); conn.close() check(m.member_source(OTHER) == "oc", f"an unrecognised source falls back to oc — the direction that keeps reconciling (got {m.member_source(OTHER)!r})") try: m.set_member_source(OTHER, "banana") check(False, "set_member_source must reject an unknown source") except ValueError: check(True, "set_member_source rejects an unknown source") print("5. the three decisions that used to key on MANUAL_MEMBERS now key on the source") src = (ROOT / "app" / "main.py").read_text() recon = src[src.index("async def reconcile_allowances"):] recon = recon[:recon.index("def ", recon.index("for row in rows"))] check('member_source(email, conn) == "manual"' in recon, "the reconciliation skips only source == manual") check("in MANUAL_MEMBERS" not in recon, "and no longer keys on the raw list (an oc-incognito member must not be skipped)") check("not is_manual_member(email)" in src, "the sweep's deactivation guard uses the source too") check("is_manual_member(email) or m.get(\"org_slug\")" in src, "shortfall credits exempt manual + org-backed, nothing else") check('"source": member_source(email)' in src, "the panel payload carries the source (so it can stop saying 'manual')") print("6. the column is optional — existing rows carry NULL and still work") conn = m.get_db() conn.execute("UPDATE members SET source = NULL WHERE lower(email) = ?", (MMOYA,)) conn.commit(); conn.close() m.MANUAL_MEMBERS = {"founder@example.org"} # the address leaves the list check(m.member_source(MMOYA) == "oc", "once mmoya leaves MANUAL_MEMBERS with no source set, they read oc — which is why the " "source is set BEFORE the address is removed from the env") print() if FAILS: print(f"oc-incognito: {len(FAILS)} FAILURE(S)") sys.exit(1) print("oc-incognito: OK — an explicit source wins, only manual is exempt, bad values fail safe")