"""
Comprehensive tests for the GitHub search engine.
Tests initialization, search functionality, and API configuration.
"""

import pytest


class TestGitHubSearchEngineInit:
    """Tests for GitHub search engine initialization."""

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

        engine = GitHubSearchEngine()

        assert engine.max_results >= 10
        # Default search type is repositories
        assert engine.search_type == "repositories"
        assert engine.api_key is None

    def test_init_with_api_key(self):
        """Test initialization with GitHub API key."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine(api_key="ghp_test_token_123")
        assert engine.api_key == "ghp_test_token_123"

    def test_init_with_search_type(self):
        """Test initialization with specific search type."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine(search_type="code")
        assert engine.search_type == "code"

    def test_init_with_custom_max_results(self):
        """Test initialization with custom max results."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

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


class TestGitHubSearchExecution:
    """Tests for GitHub search execution."""

    @pytest.fixture
    def github_engine(self):
        """Create a GitHub engine instance."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        return GitHubSearchEngine(max_results=10)

    def test_engine_initialization(self, github_engine):
        """Test that engine is properly initialized."""
        assert github_engine is not None
        assert github_engine.max_results >= 10

    def test_engine_has_api_base(self, github_engine):
        """Test that engine has API base URL configured."""
        assert hasattr(github_engine, "api_base")
        assert "api.github.com" in github_engine.api_base


class TestGitHubAPIConfiguration:
    """Tests for GitHub 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_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine(api_key="ghp_my_token")
        assert engine.api_key == "ghp_my_token"
        # Check that Authorization header is set
        assert "Authorization" in engine.headers
        assert "ghp_my_token" in engine.headers["Authorization"]

    def test_no_api_key_anonymous_access(self):
        """Test that engine works without API key (anonymous access)."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine()
        assert engine.api_key is None
        # Authorization header should not be present without API key
        assert "Authorization" not in engine.headers


class TestGitHubSearchTypes:
    """Tests for different GitHub search types."""

    def test_repository_search_type(self):
        """Test repository search type configuration."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine(search_type="repositories")
        assert engine.search_type == "repositories"

    def test_code_search_type(self):
        """Test code search type configuration."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine(search_type="code")
        assert engine.search_type == "code"

    def test_issues_search_type(self):
        """Test issues search type configuration."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine(search_type="issues")
        assert engine.search_type == "issues"

    def test_users_search_type(self):
        """Test users search type configuration."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine(search_type="users")
        assert engine.search_type == "users"


class TestGitHubEngineType:
    """Tests for GitHub 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_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine()
        assert "github" in engine.engine_type.lower()


class TestGitHubHeaders:
    """Tests for GitHub request headers."""

    def test_accept_header_set(self):
        """Test that Accept header is set for API compatibility."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine()
        assert "Accept" in engine.headers
        assert "application/vnd.github" in engine.headers["Accept"]

    def test_user_agent_header_set(self):
        """Test that User-Agent header is set."""
        from local_deep_research.web_search_engines.engines.search_engine_github import (
            GitHubSearchEngine,
        )

        engine = GitHubSearchEngine()
        assert "User-Agent" in engine.headers
