"""Edge-case tests for search_utilities — format_findings phase parsing and graceful handling."""


class TestFormatFindingsMalformedPhaseNonNumeric:
    """Verify ValueError from int() on non-numeric phase is caught gracefully."""

    def test_format_findings_malformed_phase_non_numeric(self):
        """'Follow-up Iteration abc.1' → int('abc') raises ValueError, caught gracefully."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Follow-up Iteration abc.1",
                "content": "Some content here.",
                "search_results": [],
                "question": "Fallback question text",
            }
        ]
        questions = {1: ["Question one"]}

        result = format_findings(findings, "Synthesized content", questions)

        # Should not raise — ValueError is caught
        assert "Some content here." in result
        # Since parsing failed, the finding's own question should be displayed as fallback
        assert "Fallback question text" in result


class TestFormatFindingsOutOfBoundsIteration:
    """Verify out-of-bounds iteration index is handled gracefully."""

    def test_format_findings_out_of_bounds_iteration(self):
        """Phase 'Follow-up Iteration 999.1' with missing iteration key → warning, no crash."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Follow-up Iteration 999.1",
                "content": "Out of bounds content.",
                "search_results": [],
                "question": "OOB fallback question",
            }
        ]
        # Only iteration 1 exists, not 999
        questions = {1: ["Only question in iteration 1"]}

        result = format_findings(findings, "Summary", questions)

        assert "Out of bounds content." in result
        # Iteration 999 doesn't exist, so fallback to finding's own question
        assert "OOB fallback question" in result


class TestFormatFindingsNoneSearchResults:
    """Verify None search_results (instead of list) is handled gracefully."""

    def test_format_findings_none_search_results(self):
        """finding['search_results'] = None should not crash extract_links_from_search_results."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Initial Search",
                "content": "Content with None results.",
                "search_results": None,
            }
        ]

        # Should not raise TypeError
        result = format_findings(findings, "Summary", {})
        assert "Content with None results." in result


class TestFormatFindingsEmptyFindingsList:
    """Verify empty findings_list produces valid output structure."""

    def test_format_findings_empty_findings_list(self):
        """Empty findings_list should produce output with synthesized content but no DETAILED FINDINGS."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        result = format_findings([], "My synthesized summary", {})

        assert "My synthesized summary" in result
        # With empty findings, the DETAILED FINDINGS header should NOT appear
        assert "DETAILED FINDINGS" not in result


class TestFormatFindingsSubqueryPhaseParsing:
    """Verify 'Sub-query X' phase correctly parsed for IterDRAG-style results."""

    def test_format_findings_subquery_phase_parsing(self):
        """'Sub-query 2' should look up questions_by_iteration[0][1] (index 1)."""
        from local_deep_research.utilities.search_utilities import (
            format_findings,
        )

        findings = [
            {
                "phase": "Sub-query 2",
                "content": "Sub-query content.",
                "search_results": [],
            }
        ]
        # IterDRAG stores sub-queries in iteration 0
        questions = {0: ["First sub-query?", "Second sub-query?"]}

        result = format_findings(findings, "Summary", questions)

        assert "Sub-query content." in result
        # "Sub-query 2" → index 1 → "Second sub-query?"
        assert "Second sub-query?" in result
