# retoor <retoor@molodetz.nl>

from tests.conftest import BASE_URL

ROCKET = "\U0001F680"
FIRE = "\U0001F525"


def _create_post(page, content, title):
    page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
    page.locator(".feed-fab").first.wait_for(state="visible", timeout=10000)
    page.locator(".feed-fab").first.click()
    page.check("input[value='random']")
    page.fill("#post-content", content)
    page.fill("#post-title", title)
    page.locator("#create-post-modal button.btn-primary:has-text('Post')").click()
    page.wait_for_url(f"{BASE_URL}/posts/*", wait_until="domcontentloaded")


def test_title_emoji_rendered_server_side(alice):
    page, _ = alice
    _create_post(page, "Body content for the server-rendered title emoji test.", "Launch day :rocket:")
    title = page.locator(".post-detail-title").first
    title.wait_for(state="visible")
    text = title.text_content()
    assert ROCKET in text
    assert ":rocket:" not in text
    assert page.locator("dp-title").count() == 0


def test_title_inline_markdown_rendered_server_side(alice):
    page, _ = alice
    _create_post(page, "Body content for the server-rendered title markdown test.", "Ship **v2** today")
    strong = page.locator(".post-detail-title strong").first
    strong.wait_for(state="visible")
    assert strong.text_content().strip() == "v2"


def test_content_youtube_embedded_server_side(alice):
    page, _ = alice
    _create_post(page, "watch https://youtu.be/dQw4w9WgXcQ now", "Embed host post")
    iframe = page.locator(".post-detail-content .embed-youtube iframe").first
    iframe.wait_for(state="attached")
    assert "youtube.com/embed/dQw4w9WgXcQ" in (iframe.get_attribute("src") or "")


def test_content_code_block_has_copy_button(alice):
    page, _ = alice
    _create_post(page, "snippet\n\n```python\nprint('hi')\n```", "Code copy host post")
    btn = page.locator(".post-detail-content pre.code-has-copy .code-copy-btn").first
    btn.wait_for(state="visible")
    assert btn.inner_text().strip().lower() == "copy"


def test_feed_title_emoji_rendered_server_side(alice):
    page, _ = alice
    _create_post(page, "Body content for the feed server-rendered title test.", "Feed title :fire:")
    page.goto(f"{BASE_URL}/feed", wait_until="domcontentloaded")
    card_title = page.locator(".post-title").first
    card_title.wait_for(state="visible")
    assert FIRE in card_title.text_content()
    assert page.locator("dp-title").count() == 0
