from mars_bot.alert import format_subscribers, render_card
from mars_bot.format import md_to_html


def test_format_subscribers_small():
    assert format_subscribers(0) == "0"
    assert format_subscribers(45) == "45"
    assert format_subscribers(987) == "987"


def test_format_subscribers_thousands():
    assert format_subscribers(1000) == "1.0k"
    assert format_subscribers(3400) == "3.4k"
    assert format_subscribers(9499) == "9.5k"
    assert format_subscribers(9999) == "10k"  # boundary cleanly switches to "Nk" form
    assert format_subscribers(10000) == "10k"
    assert format_subscribers(12345) == "12k"
    assert format_subscribers(513774) == "514k"


def test_format_subscribers_none():
    assert format_subscribers(None) == "—"


def test_render_card_basic():
    md = render_card(
        mars_channel="marsingru",
        source_username="mosptichka",
        source_title="Московская птичка",
        source_subscribers=513774,
        posted_at=1780992317,
        post_link="https://t.me/mosptichka/18151",
    )
    # Sanity assertions — конкретный формат проверяем golden-тестом ниже.
    assert "#mention" in md
    assert md.startswith("#mention")
    assert "@marsingru" in md
    assert "Московская птичка" in md
    assert "514k" in md
    assert "https://t.me/mosptichka/18151" in md
    assert "https://t.me/mosptichka" in md
    # Никаких угловых скобок вокруг URL.
    assert "(<" not in md
    assert ">)" not in md


def test_render_card_missing_subscribers():
    md = render_card(
        mars_channel="marsingru",
        source_username="x",
        source_title="X",
        source_subscribers=None,
        posted_at=1780000000,
        post_link="https://t.me/x/1",
    )
    assert "—" in md


def test_render_card_passes_through_md_to_html():
    """Карточка должна успешно конвертиться в HTML — без сломанных тегов."""
    md = render_card(
        mars_channel="marsingru",
        source_username="x",
        source_title="X Channel",
        source_subscribers=100,
        posted_at=1780000000,
        post_link="https://t.me/x/1",
    )
    html = md_to_html(md)
    # Базовые проверки: ссылки стали <a>, bold стал <b>, hashtag сохранился.
    assert "<b>" in html
    assert "<a href=" in html
    assert "#mention" in html
    # Нет угловых скобок в href.
    assert 'href="<' not in html
    # No broken anchor markup.
    assert '<a href="">' not in html


def test_render_card_uses_moscow_time():
    """Опубликовано должно быть в MSK (UTC+3), не в UTC."""
    md = render_card(
        mars_channel="marsingru",
        source_username="x",
        source_title="X",
        source_subscribers=100,
        posted_at=1780992317,  # 09.06.2026 11:05 MSK / 08:05 UTC
        post_link="https://t.me/x/1",
    )
    assert "09.06.2026 11:05" in md


def test_render_card_with_brackets_in_title_keeps_link():
    """Bracketed title не должен ломать рендер ссылки в HTML."""
    md = render_card(
        mars_channel="marsingru",
        source_username="x",
        source_title="[бета] Канал",
        source_subscribers=100,
        posted_at=1780000000,
        post_link="https://t.me/x/1",
    )
    html = md_to_html(md)
    # Ссылка на источник должна стать <a href="https://t.me/x">...</a>
    assert '<a href="https://t.me/x">' in html
    # Текст внутри ссылки должен содержать Unicode-скобки, не ASCII.
    assert "［бета］" in html
