"""
Tests for web/utils/formatters.py

Tests cover:
- convert_debug_to_markdown function
- Edge cases: None, empty, no findings section
- Standard conversion with divider removal
"""


class TestConvertDebugToMarkdown:
    """Tests for the convert_debug_to_markdown function."""

    def test_empty_input_returns_message(self):
        """Test that empty input returns informative message."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        result = convert_debug_to_markdown("", "test query")
        assert "No detailed findings available" in result
        assert "test query" in result

    def test_none_input_returns_message(self):
        """Test that None input returns informative message."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        result = convert_debug_to_markdown(None, "my research")
        assert "No detailed findings available" in result
        assert "my research" in result

    def test_extracts_detailed_findings_section(self):
        """Test extraction of content after DETAILED FINDINGS marker."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """Some header content
DETAILED FINDINGS:
This is the actual content
that should be extracted."""

        result = convert_debug_to_markdown(raw_text, "query")

        assert "This is the actual content" in result
        assert "that should be extracted" in result
        assert "Some header content" not in result

    def test_removes_divider_lines_with_equals(self):
        """Test that === divider lines are removed."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """DETAILED FINDINGS:
=== Section 1 ===
Actual content here
================================================================================
More content"""

        result = convert_debug_to_markdown(raw_text, "query")

        assert "===" not in result
        assert "Actual content here" in result
        assert "More content" in result

    def test_no_detailed_findings_uses_full_text(self):
        """Test that full text is used when no DETAILED FINDINGS marker."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """This is research content
with multiple lines
and no special markers."""

        result = convert_debug_to_markdown(raw_text, "query")

        assert "This is research content" in result
        assert "with multiple lines" in result

    def test_returns_stripped_result(self):
        """Test that result is properly stripped of whitespace."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """DETAILED FINDINGS:

   Content with whitespace

"""
        result = convert_debug_to_markdown(raw_text, "query")

        assert not result.startswith(" ")
        assert not result.endswith(" ")
        assert "Content with whitespace" in result

    def test_handles_only_divider_lines(self):
        """Test handling of text that contains only dividers."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """DETAILED FINDINGS:
================================================================================
=== Only Dividers ===
================================================================================"""

        result = convert_debug_to_markdown(raw_text, "query")

        # Should return empty or minimal content after removing dividers
        assert "===" not in result

    def test_preserves_markdown_formatting(self):
        """Test that markdown formatting is preserved."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """DETAILED FINDINGS:
# Header

- Bullet point
- Another bullet

**Bold text** and *italic*"""

        result = convert_debug_to_markdown(raw_text, "query")

        assert "# Header" in result
        assert "- Bullet point" in result
        assert "**Bold text**" in result
        assert "*italic*" in result

    def test_handles_unicode_content(self):
        """Test proper handling of unicode characters."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """DETAILED FINDINGS:
Émojis 🌍 and spëcial çharacters
日本語テキスト"""

        result = convert_debug_to_markdown(raw_text, "日本語クエリ")

        assert "🌍" in result
        assert "日本語テキスト" in result
