import goodnews.feeds as feeds

RSS = b"""<?xml version="1.0"?>
<rss><channel>
  <item>
    <title>Volunteers restore creek and rescue stranded wildlife</title>
    <description>A hopeful recovery effort</description>
    <link>http://example.com/1</link>
    <pubDate>Sat, 30 May 2026 10:00:00 GMT</pubDate>
  </item>
  <item>
    <title>Quarterly tax filing deadline reminder</title>
    <description>routine notice</description>
    <link>http://example.com/2</link>
    <pubDate>Sat, 30 May 2026 09:00:00 GMT</pubDate>
  </item>
</channel></rss>"""


def test_preview_feed_scores_sample_without_network(monkeypatch):
    monkeypatch.setattr(feeds, "fetch_feed", lambda url, **kw: RSS)
    p = feeds.preview_feed("http://example.com/feed")

    assert p["sampled"] == 2
    assert p["classified"] is False
    assert 0.0 <= p["acceptance_rate"] <= 1.0
    assert p["accepted"] >= 1  # the restore/rescue story should pass the heuristic
    assert p["newest_published"] is not None
    assert isinstance(p["topic_mix"], dict) and p["topic_mix"] == {}  # empty without a model
    assert all("reason" in ex and "title" in ex for ex in p["examples_rejected"])


def test_preview_feed_respects_sample_cap(monkeypatch):
    monkeypatch.setattr(feeds, "fetch_feed", lambda url, **kw: RSS)
    p = feeds.preview_feed("http://example.com/feed", sample=1)
    assert p["sampled"] == 1
