"""Edge-case tests for web_search_engines/search_engine_base.py — API key validation and relevance filtering."""


class TestApiKeyValidation:
    """Tests for _check_api_key_availability."""

    def test_api_key_whitespace_only_rejected(self):
        """'   '.strip() becomes empty, correctly rejected."""
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        config = {"requires_api_key": True, "api_key": "   "}
        assert (
            BaseSearchEngine._check_api_key_availability("test_engine", config)
            is False
        )

    def test_api_key_placeholder_suffix_detection(self):
        """Keys ending in _API_KEY treated as placeholders."""
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        config = {"requires_api_key": True, "api_key": "BRAVE_API_KEY"}
        assert (
            BaseSearchEngine._check_api_key_availability("brave", config)
            is False
        )

    def test_real_api_keys_not_false_positive(self):
        """Real-looking API keys are NOT rejected by placeholder check."""
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        for key in ["sk-abc123xyz", "AIzaSyD-example-key-123"]:
            config = {"requires_api_key": True, "api_key": key}
            assert (
                BaseSearchEngine._check_api_key_availability("engine", config)
                is True
            )

    def test_api_key_not_required_always_passes(self):
        """Engine that doesn't require API key always passes."""
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        config = {"requires_api_key": False}
        assert (
            BaseSearchEngine._check_api_key_availability("test", config) is True
        )


class TestFilterRelevanceEdgeCases:
    """Tests for _filter_for_relevance index validation."""

    def test_filter_relevance_negative_indices_skipped(self):
        """LLM returns [-1, 0] — negative index guard `0 <= idx` works."""
        from unittest.mock import Mock
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        # Create a concrete subclass to test the base method
        class TestEngine(BaseSearchEngine):
            def _get_previews(self, query):
                return []

            def _get_full_content(self, relevant_items):
                return relevant_items

        # Mock LLM that returns negative indices
        mock_llm = Mock()
        mock_llm.invoke.return_value = Mock(content="[-1, 0, 1]")

        engine = TestEngine(
            llm=mock_llm,
            programmatic_mode=True,
            settings_snapshot={"rate_limiter.enabled": False},
        )

        previews = [
            {
                "title": "Result 0",
                "snippet": "First result",
                "url": "https://a.com",
            },
            {
                "title": "Result 1",
                "snippet": "Second result",
                "url": "https://b.com",
            },
        ]

        results = engine._filter_for_relevance(previews, "test query")
        # -1 should be skipped, 0 and 1 should be kept
        assert len(results) == 2
        assert results[0]["title"] == "Result 0"
        assert results[1]["title"] == "Result 1"

    def test_filter_relevance_duplicate_indices(self):
        """LLM returning [0, 0, 1] produces duplicates (no dedup)."""
        from unittest.mock import Mock
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        class TestEngine(BaseSearchEngine):
            def _get_previews(self, query):
                return []

            def _get_full_content(self, relevant_items):
                return relevant_items

        mock_llm = Mock()
        mock_llm.invoke.return_value = Mock(content="[0, 0, 1]")

        engine = TestEngine(
            llm=mock_llm,
            programmatic_mode=True,
            settings_snapshot={"rate_limiter.enabled": False},
        )

        previews = [
            {"title": "Result 0", "snippet": "First", "url": "https://a.com"},
            {"title": "Result 1", "snippet": "Second", "url": "https://b.com"},
        ]

        results = engine._filter_for_relevance(previews, "test query")
        # Documents that duplicates are NOT deduped — [0, 0, 1] produces 3 results
        assert len(results) == 3
        assert results[0]["title"] == "Result 0"
        assert results[1]["title"] == "Result 0"  # duplicate
        assert results[2]["title"] == "Result 1"
