"""Tests for conftest.py LLM test fixtures and helpers.

Verifies all helpers produce valid typed objects with correct defaults.
"""

from tests.conftest import (
    make_analysis_result,
    make_briefing_result,
    make_category_response,
    make_mission_item,
    sample_config,
)


class TestMakeAnalysisResult:
    """Tests for make_analysis_result helper."""

    def test_returns_analysis_result_with_defaults(self):
        """Returns AnalysisResult with all default values."""
        result = make_analysis_result()

        assert result.files == []
        assert result.parsed_files == {}
        assert result.modules == []
        assert result.scored_files == []
        assert result.entry_points == []
        assert result.languages == {}
        assert result.repo_path == "/tmp/test-repo"

    def test_overrides_single_field(self):
        """Allows overriding single field while keeping others default."""
        result = make_analysis_result(repo_path="/custom/path")

        assert result.repo_path == "/custom/path"
        assert result.files == []

    def test_overrides_multiple_fields(self):
        """Allows overriding multiple fields."""
        from unfamiliar.analysis.models import FileInfo

        files = [
            FileInfo(
                path="test.py", language="python", size_bytes=100, content_hash="abc"
            )
        ]
        result = make_analysis_result(files=files, languages={"python": 1})

        assert len(result.files) == 1
        assert result.files[0].path == "test.py"
        assert result.languages == {"python": 1}

    def test_import_graph_is_empty_by_default(self):
        """ImportGraph is initialized empty."""
        result = make_analysis_result()

        assert result.import_graph.edges == {}
        assert result.import_graph.files == {}
        assert result.import_graph.in_degree == {}

    def test_test_info_is_empty_by_default(self):
        """TestInfo is initialized empty."""
        result = make_analysis_result()

        assert result.test_info.test_files == set()
        assert result.test_info.source_to_tests == {}
        assert result.test_info.untested_files == set()
        assert result.test_info.tier == "basic"
        assert result.test_info.coverage_summary == {}

    def test_security_is_empty_by_default(self):
        """SecurityResult is initialized empty."""
        result = make_analysis_result()

        assert result.security.secrets == []
        assert result.security.vulnerabilities == []
        assert result.security.manifests_scanned == []
        assert result.security.scan_errors == []


class TestMakeBriefingResult:
    """Tests for make_briefing_result helper."""

    def test_returns_briefing_result_with_defaults(self):
        """Returns BriefingResult with valid BriefingResponse."""
        result = make_briefing_result()

        assert result.briefing.summary == "Test codebase summary."
        assert result.briefing.tech_stack == ["Python"]
        assert result.briefing.security_posture == "No significant security concerns."
        assert result.briefing.modules == []
        assert result.modules == []
        assert result.context_tokens_estimate == 100

    def test_overrides_briefing_field(self):
        """Allows overriding briefing."""
        from unfamiliar.llm.briefing.schemas import BriefingResponse

        custom_briefing = BriefingResponse(
            summary="Custom summary",
            tech_stack=["TypeScript", "Node"],
            security_posture="Has vulnerabilities",
            modules=[],
        )
        result = make_briefing_result(briefing=custom_briefing)

        assert result.briefing.summary == "Custom summary"
        assert result.briefing.tech_stack == ["TypeScript", "Node"]

    def test_overrides_modules_list(self):
        """Allows overriding modules list."""
        from unfamiliar.llm.briefing.schemas import ModuleDescription

        modules = [
            ModuleDescription(
                name="Auth", description="Auth module", security_relevant=True
            )
        ]
        result = make_briefing_result(modules=modules)

        assert len(result.modules) == 1
        assert result.modules[0].name == "Auth"
        assert result.modules[0].security_relevant is True

    def test_overrides_context_tokens_estimate(self):
        """Allows overriding token estimate."""
        result = make_briefing_result(context_tokens_estimate=5000)

        assert result.context_tokens_estimate == 5000


class TestMakeMissionItem:
    """Tests for make_mission_item helper."""

    def test_returns_mission_item_with_defaults(self):
        """Returns MissionItemResponse with all default values."""
        result = make_mission_item()

        assert result.title == "Test Mission"
        assert result.title_plain == "Test Mission"
        assert result.description == "A test mission description."
        assert result.difficulty == "quick"
        assert result.completion_condition == "You understand the test mission."
        assert result.security_tag is False
        assert result.standard_tag is None
        assert result.narrative_intro == "The test case begins..."
        assert result.narrative_outro == "Another test completed."
        assert result.file_refs == []
        assert result.module_index == 0
        assert result.prerequisite_titles == []

    def test_overrides_title_fields(self):
        """Allows overriding title and title_plain."""
        result = make_mission_item(
            title="The Shadow Protocol",
            title_plain="Understand Authentication",
        )

        assert result.title == "The Shadow Protocol"
        assert result.title_plain == "Understand Authentication"

    def test_overrides_difficulty(self):
        """Allows overriding difficulty."""
        result = make_mission_item(difficulty="deep_dive")

        assert result.difficulty == "deep_dive"

    def test_overrides_prerequisite_titles(self):
        """Allows overriding prerequisite_titles."""
        result = make_mission_item(
            prerequisite_titles=["First Mission", "Second Mission"]
        )

        assert result.prerequisite_titles == ["First Mission", "Second Mission"]

    def test_overrides_security_tag(self):
        """Allows overriding security_tag."""
        result = make_mission_item(security_tag=True)

        assert result.security_tag is True

    def test_overrides_file_refs(self):
        """Allows overriding file_refs."""
        from unfamiliar.llm.missions.schemas import MissionFileRefResponse

        file_refs = [
            MissionFileRefResponse(
                file_path="src/auth.py",
                line_start=10,
                line_end=25,
                description="Login handler",
            )
        ]
        result = make_mission_item(file_refs=file_refs)

        assert len(result.file_refs) == 1
        assert result.file_refs[0].file_path == "src/auth.py"
        assert result.file_refs[0].line_start == 10
        assert result.file_refs[0].line_end == 25


class TestMakeCategoryResponse:
    """Tests for make_category_response helper."""

    def test_returns_response_with_default_mission(self):
        """Returns CategoryMissionsResponse with single default mission."""
        result = make_category_response()

        assert len(result.missions) == 1
        assert result.missions[0].title == "Test Mission"
        assert result.chains == []

    def test_accepts_custom_missions_list(self):
        """Accepts custom missions list."""
        mission1 = make_mission_item(title="Mission A", title_plain="Mission A")
        mission2 = make_mission_item(title="Mission B", title_plain="Mission B")
        result = make_category_response(missions=[mission1, mission2])

        assert len(result.missions) == 2
        assert result.missions[0].title == "Mission A"
        assert result.missions[1].title == "Mission B"

    def test_accepts_empty_missions_list(self):
        """Accepts empty missions list."""
        result = make_category_response(missions=[])

        assert result.missions == []

    def test_accepts_custom_chains(self):
        """Accepts custom chains list."""
        from unfamiliar.llm.missions.schemas import ChainResponse

        chains = [
            ChainResponse(
                name="The Auth Trilogy",
                name_plain="Authentication Flow",
                description="Complete auth flow",
                mission_titles=["Login", "Session", "Logout"],
            )
        ]
        result = make_category_response(chains=chains)

        assert len(result.chains) == 1
        assert result.chains[0].name == "The Auth Trilogy"


class TestSampleConfig:
    """Tests for sample_config helper."""

    def test_returns_unfamiliar_config(self):
        """Returns UnfamiliarConfig instance."""
        from unfamiliar.config import UnfamiliarConfig

        result = sample_config()

        assert isinstance(result, UnfamiliarConfig)

    def test_has_anthropic_provider(self):
        """Has anthropic as default provider."""
        result = sample_config()

        assert result.provider == "anthropic"

    def test_has_token_budget(self):
        """Has token_budget set."""
        result = sample_config()

        assert result.token_budget == 10000

    def test_has_theme(self):
        """Has theme set."""
        result = sample_config()

        assert result.theme == "film_noir"
