"""FC-3e: HTML-strip + word-boundary truncator unit tests."""

from backend.app.utils.text import html_to_plain, truncate_at_word


def test_html_to_plain_strips_tags():
    assert html_to_plain("<p>hi</p>") == "hi"


def test_html_to_plain_converts_br_to_newline():
    assert html_to_plain("a<br>b") == "a\nb"


def test_html_to_plain_converts_p_close_to_newline():
    assert html_to_plain("<p>one</p><p>two</p>") == "one\ntwo"


def test_html_to_plain_collapses_runs_of_whitespace():
    assert html_to_plain("a   b") == "a b"


def test_html_to_plain_preserves_intentional_newlines_between_blocks():
    assert html_to_plain("alpha<br><br>beta") == "alpha\n\nbeta"


def test_html_to_plain_idempotent_on_plain_text():
    assert html_to_plain("plain text") == "plain text"


def test_html_to_plain_none_returns_empty():
    assert html_to_plain(None) == ""


def test_html_to_plain_decodes_entities():
    assert html_to_plain("a &amp; b") == "a & b"


def test_truncate_at_word_under_limit_no_change():
    assert truncate_at_word("short", 100) == ("short", False)


def test_truncate_at_word_breaks_at_whitespace():
    assert truncate_at_word("alpha beta gamma", 8) == ("alpha…", True)


def test_truncate_at_word_no_whitespace_hard_cuts():
    # No space to break on within the limit — hard-cut at the limit.
    assert truncate_at_word("abcdefghij", 5) == ("abcde…", True)


def test_truncate_at_word_handles_none():
    assert truncate_at_word(None, 10) == ("", False)


def test_truncate_at_word_handles_empty_string():
    assert truncate_at_word("", 10) == ("", False)
