"""
Behavioral tests for web/utils/formatters module.

Tests convert_debug_to_markdown function with various input formats.
"""


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

    def test_returns_string(self):
        """Returns a string."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        result = convert_debug_to_markdown("some text", "test query")
        assert isinstance(result, str)

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

        result = convert_debug_to_markdown(None, "test query")
        assert "test query" in result

    def test_empty_string_returns_message(self):
        """Empty string returns informative message."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        result = convert_debug_to_markdown("", "test query")
        assert "test query" in result

    def test_plain_text_returned_as_is(self):
        """Plain text without special markers is returned stripped."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        result = convert_debug_to_markdown("Hello World", "query")
        assert result == "Hello World"

    def test_extracts_content_after_detailed_findings(self):
        """Extracts content after DETAILED FINDINGS: marker."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "SOME HEADER\nDETAILED FINDINGS:\nActual content here"
        result = convert_debug_to_markdown(raw, "query")
        assert "Actual content here" in result
        assert "SOME HEADER" not in result

    def test_removes_divider_lines(self):
        """Removes lines starting with ===."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "Line 1\n=== Divider ===\nLine 2"
        result = convert_debug_to_markdown(raw, "query")
        assert "===" not in result
        assert "Line 1" in result
        assert "Line 2" in result

    def test_removes_80_char_divider(self):
        """Removes 80-character = divider lines."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "Line 1\n" + "=" * 80 + "\nLine 2"
        result = convert_debug_to_markdown(raw, "query")
        assert "=" * 80 not in result
        assert "Line 1" in result
        assert "Line 2" in result

    def test_strips_whitespace(self):
        """Result is stripped of leading/trailing whitespace."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "  \n  content  \n  "
        result = convert_debug_to_markdown(raw, "query")
        assert result == "content"

    def test_preserves_markdown_headers(self):
        """Preserves markdown headers in content."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "# Header\n## Subheader\nParagraph text"
        result = convert_debug_to_markdown(raw, "query")
        assert "# Header" in result
        assert "## Subheader" in result

    def test_preserves_bullet_points(self):
        """Preserves bullet points in content."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "- Item 1\n- Item 2\n- Item 3"
        result = convert_debug_to_markdown(raw, "query")
        assert "- Item 1" in result
        assert "- Item 2" in result
        assert "- Item 3" in result

    def test_detailed_findings_with_dividers(self):
        """Handles DETAILED FINDINGS with divider lines."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "SUMMARY\nDETAILED FINDINGS:\n=== Section ===\nContent"
        result = convert_debug_to_markdown(raw, "query")
        assert "===" not in result
        assert "Content" in result
        assert "SUMMARY" not in result

    def test_multiline_content_preserved(self):
        """Multiple lines of content are preserved."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw = "Line 1\nLine 2\nLine 3\nLine 4"
        result = convert_debug_to_markdown(raw, "query")
        assert "Line 1" in result
        assert "Line 4" in result

    def test_query_in_none_fallback(self):
        """Query appears in fallback message for None input."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        result = convert_debug_to_markdown(None, "quantum computing")
        assert "quantum computing" in result
