# tests/test_collect.py
from datetime import datetime, timezone, timedelta

import collect

def test_fresh_entries_drops_old_keeps_recent_and_undated():
    now = datetime.now(timezone.utc)
    def e(link, days_ago=None):
        pub = "" if days_ago is None else (now - timedelta(days=days_ago)).isoformat()
        return {"link": link, "published": pub, "source": "x", "title": "t"}
    entries = [e("recent", 2), e("old", 30), e("undated", None)]
    out = collect.fresh_entries(entries, now, max_age_days=7)
    links = {x["link"] for x in out}
    assert links == {"recent", "undated"}

def test_has_signals():
    assert collect.has_signals([], [], []) is False
    assert collect.has_signals([{"post_id": "x"}], [], []) is True
    assert collect.has_signals([], [], [{"link": "u"}]) is True

def test_build_digest_has_sections_and_sources():
    tg = [{"account": "rushuffle", "caption": "new mix", "metric": 25000,
           "ratio": 2.5, "permalink": "https://t.me/rushuffle/1"}]
    rd = [{"account": "indieheads", "caption": "Beach House album", "metric": 4200,
           "ratio": 3.1, "permalink": "https://www.reddit.com/r/indieheads/x"}]
    rss = [{"source": "pitchfork.com", "title": "Radiohead tour",
            "link": "https://pitchfork.com/1", "summary": "..."},
           {"source": "stereogum.com", "title": "Radiohead tour dates",
            "link": "https://stereogum.com/2", "summary": "..."}]
    md = collect.build_digest_md("2026-07-26", tg, rd, rss)
    assert "## Telegram" in md and "rushuffle" in md
    assert "## Reddit" in md and "indieheads" in md
    assert "## RSS" in md and "pitchfork.com" in md and "stereogum.com" in md
    assert "https://pitchfork.com/1" in md
