import pytest
from datetime import datetime, timezone

from goodnews.db import connect, init_db
from goodnews.sources import review_sources


@pytest.fixture
def conn():
    c = connect(":memory:"); init_db(c)
    c.execute("INSERT INTO sources (id,name,feed_url,trust_score) VALUES (1,'Pay','http://p/f',5)")
    yield c; c.close()


def test_paywall_heavy_flagged(conn):
    now = datetime.now(timezone.utc).isoformat()
    for i in range(20):
        url = f"https://www.newscientist.com/{i}" if i < 15 else f"https://phys.org/{i}"
        conn.execute("INSERT INTO articles (id,source_id,canonical_url,title,published_at,url_hash) "
                     "VALUES (?,1,?,?,?,?)", (i, url, f"t{i}", now, f"h{i}"))
        conn.execute("INSERT INTO article_scores (article_id,cortisol_score,ragebait_score,accepted) "
                     "VALUES (?,1,0,1)", (i,))
    conn.commit()
    flagged = review_sources(conn)
    assert flagged and "paywall-heavy" in flagged[0]["reason"]
