"""Additional edge case tests for benchmarks/graders.py.

Focuses on extract_answer_from_response edge cases and citation
stripping that are not covered by the existing test suite.
"""

from local_deep_research.benchmarks.graders import extract_answer_from_response


class TestCitationStripping:
    """Tests for citation removal from responses."""

    def test_adjacent_citations_all_removed(self):
        result = extract_answer_from_response(
            "Answer[1][2][3] here.", "simpleqa"
        )
        assert "[1]" not in result["extracted_answer"]
        assert "[2]" not in result["extracted_answer"]
        assert "[3]" not in result["extracted_answer"]

    def test_high_numbered_citation_removed(self):
        result = extract_answer_from_response(
            "Fact [99] and [100].", "simpleqa"
        )
        assert "[99]" not in result["extracted_answer"]
        assert "[100]" not in result["extracted_answer"]

    def test_non_numeric_brackets_preserved(self):
        result = extract_answer_from_response(
            "Use [option A] for best results.", "simpleqa"
        )
        assert "[option A]" in result["extracted_answer"]

    def test_citations_removed_before_browsecomp_extraction(self):
        response = "Exact Answer: The result [1] is Paris [2]\nConfidence: 90%"
        result = extract_answer_from_response(response, "browsecomp")
        assert "[1]" not in result["extracted_answer"]
        assert "[2]" not in result["extracted_answer"]
        assert "Paris" in result["extracted_answer"]


class TestBrowseCompEdgeCases:
    """Edge cases for BrowseComp extraction."""

    def test_exact_answer_with_colon_in_value(self):
        response = "Exact Answer: Time: 12:30 PM\nConfidence: 85%"
        result = extract_answer_from_response(response, "browsecomp")
        assert "12:30" in result["extracted_answer"]

    def test_exact_answer_with_numbers(self):
        response = "Exact Answer: 3.14159\nConfidence: 100%"
        result = extract_answer_from_response(response, "browsecomp")
        assert result["extracted_answer"] == "3.14159"

    def test_confidence_with_leading_zeros(self):
        response = "Exact Answer: test\nConfidence: 05%"
        result = extract_answer_from_response(response, "browsecomp")
        assert result["confidence"] == "05"

    def test_exact_answer_whitespace_stripped(self):
        response = "Exact Answer:   Paris   \nConfidence: 80%"
        result = extract_answer_from_response(response, "browsecomp")
        assert result["extracted_answer"] == "Paris"

    def test_multiple_exact_answer_lines_first_wins(self):
        response = "Exact Answer: First\nExact Answer: Second\nConfidence: 70%"
        result = extract_answer_from_response(response, "browsecomp")
        assert result["extracted_answer"] == "First"

    def test_answer_with_parentheses(self):
        response = "Exact Answer: Albert Einstein (1879-1955)\nConfidence: 95%"
        result = extract_answer_from_response(response, "browsecomp")
        assert "Albert Einstein" in result["extracted_answer"]
        assert "(1879-1955)" in result["extracted_answer"]

    def test_answer_with_comma(self):
        response = "Exact Answer: New York, United States\nConfidence: 90%"
        result = extract_answer_from_response(response, "browsecomp")
        assert "New York, United States" == result["extracted_answer"]

    def test_empty_exact_answer_captures_next_line(self):
        # When answer is empty, greedy \s* in regex consumes the newline,
        # so (.*?) ends up matching the next line content.
        response = "Exact Answer: \nConfidence: 50%"
        result = extract_answer_from_response(response, "browsecomp")
        # This is the actual regex behavior — not ideal but matches implementation
        assert result["extracted_answer"] == "Confidence: 50%"

    def test_only_newline_after_answer(self):
        response = "Exact Answer: Tokyo"
        result = extract_answer_from_response(response, "browsecomp")
        assert result["extracted_answer"] == "Tokyo"


class TestSimpleQAEdgeCases:
    """Edge cases for SimpleQA extraction."""

    def test_multiline_response_preserved(self):
        response = "Line 1\nLine 2\nLine 3"
        result = extract_answer_from_response(response, "simpleqa")
        assert "Line 1" in result["extracted_answer"]
        assert "Line 3" in result["extracted_answer"]

    def test_unicode_response_preserved(self):
        response = "La réponse est: café"
        result = extract_answer_from_response(response, "simpleqa")
        assert "café" in result["extracted_answer"]

    def test_response_with_only_citations(self):
        result = extract_answer_from_response("[1][2][3]", "simpleqa")
        # All citations removed, should be empty-ish
        assert "[" not in result["extracted_answer"]
