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


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

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

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

    def test_empty_string_returns_no_findings_message(self):
        """Test that empty string returns appropriate message."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

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

    def test_extracts_content_after_detailed_findings_header(self):
        """Test that content after DETAILED FINDINGS: is extracted."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = "Some preamble\nDETAILED FINDINGS:\nThis is the actual content\nMore content"
        result = convert_debug_to_markdown(raw_text, "query")

        assert "This is the actual content" in result
        assert "More content" in result
        assert "Some preamble" not in result
        assert "DETAILED FINDINGS:" not in result

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

        raw_text = "Just some regular content\nWith multiple lines"
        result = convert_debug_to_markdown(raw_text, "query")

        assert "Just some regular content" in result
        assert "With multiple lines" in result

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

        raw_text = (
            "Content line\n=== DIVIDER ===\nMore content\n================"
        )
        result = convert_debug_to_markdown(raw_text, "query")

        assert "Content line" in result
        assert "More content" in result
        assert "===" not in result
        assert "DIVIDER" not in result

    def test_removes_80_char_equals_divider(self):
        """Test that 80-char equals dividers are removed."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        divider = "=" * 80
        raw_text = f"Content\n{divider}\nMore content"
        result = convert_debug_to_markdown(raw_text, "query")

        assert "Content" in result
        assert "More content" in result
        assert divider not in result

    def test_preserves_equals_in_regular_content(self):
        """Test that equals signs in regular content are preserved."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = "a = b + c\nx == y"
        result = convert_debug_to_markdown(raw_text, "query")

        assert "a = b + c" in result
        assert "x == y" in result

    def test_strips_whitespace_from_result(self):
        """Test that result is stripped of leading/trailing whitespace."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = "  \n  Content  \n  "
        result = convert_debug_to_markdown(raw_text, "query")

        assert result == "Content"

    def test_handles_exception_gracefully(self):
        """Test that exceptions are handled and fallback message returned."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        # Create an object that will raise when converted
        class BadInput:
            def __bool__(self):
                return True

            def __contains__(self, item):
                raise RuntimeError("Test error")

        result = convert_debug_to_markdown(BadInput(), "my query")
        assert "Research on my query" in result
        assert "error formatting" in result

    def test_complex_input_with_multiple_sections(self):
        """Test complex input with headers, dividers, and content."""
        from local_deep_research.web.utils.formatters import (
            convert_debug_to_markdown,
        )

        raw_text = """
SUMMARY:
Some summary text

================================================================================
DETAILED FINDINGS:
=== Section 1 ===
First finding details
More details here

=== Section 2 ===
Second finding details
"""
        result = convert_debug_to_markdown(raw_text, "complex query")

        # Should extract after DETAILED FINDINGS
        assert "First finding details" in result
        assert "Second finding details" in result
        # Should not include content before DETAILED FINDINGS
        assert "Some summary text" not in result
        # Should remove divider lines
        assert "===" not in result
        assert "=" * 80 not in result
