import adapt


def test_tg_to_records_uses_reactions_when_channel_has_them():
    # канал отдаёт реакции → метрика = реакции (ближе к «залетело»)
    fw = [{"post_id": "tg:a:1", "account": "a", "platform": "telegram",
           "caption": "hi", "posted_at": "2026-07-20T10:00:00+00:00",
           "likes": 42, "comments": None, "views": 1200,
           "permalink": "https://t.me/a/1"}]
    out = adapt.tg_to_records(fw)
    assert out[0]["metric"] == 42
    assert out[0]["metric_kind"] == "reactions"
    assert out[0]["platform"] == "telegram"
    assert out[0]["permalink"] == "https://t.me/a/1"
    assert "views" not in out[0]  # приведено к нашей форме


def test_tg_to_records_falls_back_to_views_without_reactions():
    # у канала реакций нет (все None) → метрика = просмотры
    fw = [{"post_id": "tg:b:1", "account": "b", "platform": "telegram",
           "caption": "hi", "posted_at": "2026-07-20T10:00:00+00:00",
           "likes": None, "comments": None, "views": 900,
           "permalink": "https://t.me/b/1"}]
    out = adapt.tg_to_records(fw)
    assert out[0]["metric"] == 900
    assert out[0]["metric_kind"] == "views"
