"""
Comprehensive tests for the Semantic Scholar search engine.
Tests initialization, configuration, and API parameters.
"""


class TestSemanticScholarSearchEngineInit:
    """Tests for Semantic Scholar search engine initialization."""

    def test_init_default_parameters(self):
        """Test initialization with default parameters."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine()

        assert engine.max_results >= 10
        assert engine.is_public is True
        assert engine.is_scientific is True

    def test_init_with_api_key(self):
        """Test initialization with API key."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine(api_key="test_api_key")
        assert engine.api_key == "test_api_key"

    def test_init_custom_max_results(self):
        """Test initialization with custom max results."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine(max_results=50)
        assert engine.max_results >= 50

    def test_init_with_custom_parameters(self):
        """Test initialization with various custom parameters."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine(
            max_results=100,
            api_key="my_key",
            max_retries=5,
        )

        assert engine.max_results >= 100
        assert engine.api_key == "my_key"


class TestSemanticScholarSession:
    """Tests for Semantic Scholar session creation."""

    def test_session_created(self):
        """Test that a requests session is created."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine()
        session = engine._create_session()

        assert session is not None

    def test_session_has_retry_adapter(self):
        """Test that session has retry adapter configured."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine()
        session = engine._create_session()

        # Check that adapters are mounted
        assert "https://" in session.adapters
        assert "http://" in session.adapters


class TestSemanticScholarAPIConfiguration:
    """Tests for Semantic Scholar API configuration."""

    def test_api_key_in_headers(self):
        """Test that API key is included in headers when provided."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine(api_key="my_s2_api_key")
        assert engine.api_key == "my_s2_api_key"

    def test_base_url_configured(self):
        """Test that base URL is properly configured."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine()
        assert hasattr(engine, "base_url")
        assert "semanticscholar" in engine.base_url.lower()


class TestSemanticScholarEngineType:
    """Tests for Semantic Scholar engine type identification."""

    def test_engine_type_set(self):
        """Test that engine type is properly set."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine()
        # engine_type is derived from class name
        assert (
            "semantic" in engine.engine_type.lower()
            or "scholar" in engine.engine_type.lower()
        )

    def test_engine_is_scientific(self):
        """Test that engine is marked as scientific."""
        from local_deep_research.web_search_engines.engines.search_engine_semantic_scholar import (
            SemanticScholarSearchEngine,
        )

        engine = SemanticScholarSearchEngine()
        assert engine.is_scientific is True
        assert engine.is_generic is False
