"""Unit tests for card fragment parser."""
from __future__ import annotations

import pytest

from blog.bp.blog.services.posts_data import _parse_card_fragments


class TestParseCardFragments:
    def test_empty_string(self):
        assert _parse_card_fragments("") == {}

    def test_single_block(self):
        html = '<!-- card-widget:42 --><div>card</div><!-- /card-widget:42 -->'
        result = _parse_card_fragments(html)
        assert result == {"42": "<div>card</div>"}

    def test_multiple_blocks(self):
        html = (
            '<!-- card-widget:1 -->A<!-- /card-widget:1 -->'
            '<!-- card-widget:2 -->B<!-- /card-widget:2 -->'
        )
        result = _parse_card_fragments(html)
        assert result == {"1": "A", "2": "B"}

    def test_empty_inner_skipped(self):
        html = '<!-- card-widget:99 -->   <!-- /card-widget:99 -->'
        result = _parse_card_fragments(html)
        assert result == {}

    def test_multiline_content(self):
        html = '<!-- card-widget:5 -->\n<p>line1</p>\n<p>line2</p>\n<!-- /card-widget:5 -->'
        result = _parse_card_fragments(html)
        assert "5" in result
        assert "<p>line1</p>" in result["5"]

    def test_mismatched_ids_not_captured(self):
        html = '<!-- card-widget:1 -->content<!-- /card-widget:2 -->'
        result = _parse_card_fragments(html)
        assert result == {}

    def test_no_markers(self):
        html = '<div>no markers here</div>'
        assert _parse_card_fragments(html) == {}
