"""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 returning text — regex parses positive ints only,
        # so '-1' yields just 1; we add 0 and 1 explicitly to assert they
        # both come through.
        mock_llm = Mock()
        mock_llm.invoke.return_value = "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 = "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")
        # Duplicate indices are deduplicated while preserving order:
        # [0, 0, 1] → results for indices 0 and 1 once each.
        assert len(results) == 2
        assert results[0]["title"] == "Result 0"
        assert results[1]["title"] == "Result 1"
