import asyncio, os, sys, tempfile os.environ["DB_PATH"] = os.path.join(tempfile.mkdtemp(), "sources.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"] conn = m.get_db() conn.execute("INSERT OR REPLACE INTO members (email, slug, active, source) VALUES (?,?,?,?)", ("a@example.org", "slug-a", 1, "oc-incognito")) conn.execute("INSERT OR REPLACE INTO members (email, slug, active, source) VALUES (?,?,?,?)", ("b@example.org", "slug-b", 1, "oc-incognito")) conn.commit(); conn.close() m.fetch_member_index = lambda: [("a@example.org", "slug-a"), ("other@example.org", "slug-z")] r = asyncio.run(m.derive_member_sources()) conn = m.get_db() a = conn.execute("SELECT source, oc_email FROM members WHERE email='a@example.org'").fetchone() b = conn.execute("SELECT source, oc_email FROM members WHERE email='b@example.org'").fetchone() conn.close() print(" flipped:", r["flipped"], "| a:", tuple(a), "| b:", tuple(b)) assert tuple(a) == ("oc", "a@example.org"), "visible email must flip the source and record it" assert tuple(b) == ("oc-incognito", None), "a profile still hiding its email stays incognito" print("DERIVE SOURCES OK: flips only where the profile now shows an email")