"""High-value tests for utilities/search_utilities.py pure logic."""

import unittest

from local_deep_research.utilities.search_utilities import (
    LANGUAGE_CODE_MAP,
    format_findings,
)


class TestLanguageCodeMap(unittest.TestCase):
    def test_english_maps_to_en(self):
        assert LANGUAGE_CODE_MAP["english"] == "en"

    def test_contains_major_languages(self):
        for lang in ("french", "german", "spanish", "japanese", "chinese"):
            assert lang in LANGUAGE_CODE_MAP


class TestFormatFindings(unittest.TestCase):
    def test_followup_phase_parsing(self):
        findings = [
            {
                "phase": "Follow-up Iteration 1.2",
                "content": "Content.",
                "search_results": [],
            }
        ]
        questions = {1: ["First Q", "Second Q"]}
        result = format_findings(findings, "Sum.", questions)
        # "Second Q" must appear in the DETAILED FINDINGS section (phase-parsed),
        # not just in the questions-by-iteration listing
        detailed_section = result.split("DETAILED FINDINGS")[-1]
        assert "Second Q" in detailed_section

    def test_subquery_phase_parsing(self):
        findings = [
            {
                "phase": "Sub-query 1",
                "content": "Content.",
                "search_results": [],
            }
        ]
        questions = {0: ["Sub Q1", "Sub Q2"]}
        result = format_findings(findings, "Sum.", questions)
        # "Sub Q1" must appear in the DETAILED FINDINGS section (phase-parsed),
        # not just in the questions-by-iteration listing
        detailed_section = result.split("DETAILED FINDINGS")[-1]
        assert "Sub Q1" in detailed_section

    def test_question_in_finding_dict(self):
        findings = [
            {
                "phase": "Custom Phase",
                "content": "Content.",
                "question": "My question?",
                "search_results": [],
            }
        ]
        result = format_findings(findings, "Sum.", {})
        assert "My question?" in result

    def test_detailed_findings_section(self):
        findings = [
            {"phase": "Phase 1", "content": "Details.", "search_results": []}
        ]
        result = format_findings(findings, "Sum.", {})
        assert "DETAILED FINDINGS" in result
        assert "Phase 1" in result


if __name__ == "__main__":
    unittest.main()
