"""
Behavioral tests for error_reporter module.

Tests ErrorCategory enum and ErrorReporter class for error categorization.
"""


class TestErrorCategoryEnum:
    """Tests for ErrorCategory enum."""

    def test_connection_error_exists(self):
        """CONNECTION_ERROR category exists."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        assert hasattr(ErrorCategory, "CONNECTION_ERROR")

    def test_model_error_exists(self):
        """MODEL_ERROR category exists."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        assert hasattr(ErrorCategory, "MODEL_ERROR")

    def test_search_error_exists(self):
        """SEARCH_ERROR category exists."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        assert hasattr(ErrorCategory, "SEARCH_ERROR")

    def test_synthesis_error_exists(self):
        """SYNTHESIS_ERROR category exists."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        assert hasattr(ErrorCategory, "SYNTHESIS_ERROR")

    def test_file_error_exists(self):
        """FILE_ERROR category exists."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        assert hasattr(ErrorCategory, "FILE_ERROR")

    def test_rate_limit_error_exists(self):
        """RATE_LIMIT_ERROR category exists."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        assert hasattr(ErrorCategory, "RATE_LIMIT_ERROR")

    def test_unknown_error_exists(self):
        """UNKNOWN_ERROR category exists."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        assert hasattr(ErrorCategory, "UNKNOWN_ERROR")

    def test_category_values_are_strings(self):
        """All category values are strings."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
        )

        for category in ErrorCategory:
            assert isinstance(category.value, str)


class TestErrorReporterInit:
    """Tests for ErrorReporter initialization."""

    def test_can_instantiate(self):
        """Can instantiate ErrorReporter."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        assert reporter is not None

    def test_has_error_patterns(self):
        """Has error_patterns dictionary."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        assert hasattr(reporter, "error_patterns")
        assert isinstance(reporter.error_patterns, dict)

    def test_error_patterns_has_all_categories(self):
        """Error patterns has entries for all categories except UNKNOWN."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        # All categories except UNKNOWN_ERROR should have patterns
        expected_categories = [
            ErrorCategory.CONNECTION_ERROR,
            ErrorCategory.MODEL_ERROR,
            ErrorCategory.SEARCH_ERROR,
            ErrorCategory.SYNTHESIS_ERROR,
            ErrorCategory.FILE_ERROR,
            ErrorCategory.RATE_LIMIT_ERROR,
        ]
        for category in expected_categories:
            assert category in reporter.error_patterns


class TestCategorizeConnectionError:
    """Tests for categorizing connection errors."""

    def test_categorizes_eof_error(self):
        """Categorizes POST predict EOF as connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("POST predict failed: EOF")
        assert result == ErrorCategory.CONNECTION_ERROR

    def test_categorizes_connection_refused(self):
        """Categorizes connection refused as connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Connection refused")
        assert result == ErrorCategory.CONNECTION_ERROR

    def test_categorizes_timeout(self):
        """Categorizes timeout as connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        # Pattern is "timeout" - message must contain this exact substring
        result = reporter.categorize_error("Request timeout error")
        assert result == ErrorCategory.CONNECTION_ERROR

    def test_categorizes_http_error(self):
        """Categorizes HTTP error as connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("HTTP error 503")
        assert result == ErrorCategory.CONNECTION_ERROR

    def test_categorizes_errno_111(self):
        """Categorizes Errno 111 as connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("[Errno 111] Connection refused")
        assert result == ErrorCategory.CONNECTION_ERROR

    def test_categorizes_docker_host_error(self):
        """Categorizes Docker host.docker.internal error as connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error(
            "Cannot connect to host.docker.internal"
        )
        assert result == ErrorCategory.CONNECTION_ERROR

    def test_categorizes_network_error(self):
        """Categorizes network error as connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Network error occurred")
        assert result == ErrorCategory.CONNECTION_ERROR


class TestCategorizeModelError:
    """Tests for categorizing model errors."""

    def test_categorizes_model_not_found(self):
        """Categorizes Model not found as model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Model 'llama' not found")
        assert result == ErrorCategory.MODEL_ERROR

    def test_categorizes_invalid_model(self):
        """Categorizes invalid model as model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Invalid model name")
        assert result == ErrorCategory.MODEL_ERROR

    def test_categorizes_ollama_not_available(self):
        """Categorizes Ollama not available as model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Ollama is not available")
        assert result == ErrorCategory.MODEL_ERROR

    def test_categorizes_api_key_invalid(self):
        """Categorizes API key invalid as model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("API key is invalid")
        assert result == ErrorCategory.MODEL_ERROR

    def test_categorizes_authentication_error(self):
        """Categorizes authentication error as model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Authentication error occurred")
        assert result == ErrorCategory.MODEL_ERROR

    def test_categorizes_401_api_key(self):
        """Categorizes 401 API key error as model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("401: Invalid API key")
        assert result == ErrorCategory.MODEL_ERROR

    def test_categorizes_no_auth_credentials(self):
        """Categorizes no auth credentials as model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("No auth credentials found")
        assert result == ErrorCategory.MODEL_ERROR


class TestCategorizeRateLimitError:
    """Tests for categorizing rate limit errors."""

    def test_categorizes_429_resource_exhausted(self):
        """Categorizes 429 resource exhausted as rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("429: resource exhausted")
        assert result == ErrorCategory.RATE_LIMIT_ERROR

    def test_categorizes_429_too_many_requests(self):
        """Categorizes 429 too many requests as rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("429: too many requests")
        assert result == ErrorCategory.RATE_LIMIT_ERROR

    def test_categorizes_rate_limit(self):
        """Categorizes rate limit message as rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Rate limit exceeded")
        assert result == ErrorCategory.RATE_LIMIT_ERROR

    def test_categorizes_quota_exceeded(self):
        """Categorizes quota exceeded as rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Quota exceeded for this API")
        assert result == ErrorCategory.RATE_LIMIT_ERROR

    def test_categorizes_llm_rate_limit(self):
        """Categorizes LLM rate limit as rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("LLM rate limit reached")
        assert result == ErrorCategory.RATE_LIMIT_ERROR

    def test_categorizes_api_rate_limit(self):
        """Categorizes API rate limit as rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("API rate limit exceeded")
        assert result == ErrorCategory.RATE_LIMIT_ERROR

    def test_categorizes_maximum_requests_per_minute(self):
        """Categorizes maximum requests per minute as rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error(
            "Maximum requests per minute reached"
        )
        assert result == ErrorCategory.RATE_LIMIT_ERROR


class TestCategorizeSearchError:
    """Tests for categorizing search errors."""

    def test_categorizes_search_failed(self):
        """Categorizes search failed as search error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Search failed")
        assert result == ErrorCategory.SEARCH_ERROR

    def test_categorizes_no_search_results(self):
        """Categorizes no search results as search error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("No search results found")
        assert result == ErrorCategory.SEARCH_ERROR

    def test_categorizes_search_engine_error(self):
        """Categorizes search engine error as search error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Search engine error occurred")
        assert result == ErrorCategory.SEARCH_ERROR

    def test_categorizes_search_too_long(self):
        """Categorizes search too long as search error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error(
            "The search is longer than 256 characters"
        )
        assert result == ErrorCategory.SEARCH_ERROR

    def test_categorizes_github_api_error(self):
        """Categorizes GitHub API error as search error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("GitHub API error: 403")
        assert result == ErrorCategory.SEARCH_ERROR

    def test_categorizes_database_locked(self):
        """Categorizes a SQLite lock as a file-system error (it is the local
        DB file, not a search problem)."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("database is locked")
        assert result == ErrorCategory.FILE_ERROR


class TestCategorizeSynthesisError:
    """Tests for categorizing synthesis errors."""

    def test_categorizes_error_synthesis(self):
        """Categorizes error in synthesis as synthesis error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Error during synthesis")
        assert result == ErrorCategory.SYNTHESIS_ERROR

    def test_categorizes_failed_to_generate(self):
        """Categorizes failed to generate as synthesis error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Failed to generate report")
        assert result == ErrorCategory.SYNTHESIS_ERROR

    def test_categorizes_report_taking_long(self):
        """Categorizes report taking long as synthesis error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        # "timeout" would match CONNECTION_ERROR first, use different pattern
        result = reporter.categorize_error("The report is taking too long")
        assert result == ErrorCategory.SYNTHESIS_ERROR

    def test_categorizes_detailed_report_stuck(self):
        """Categorizes detailed report stuck as synthesis error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("detailed report appears stuck")
        assert result == ErrorCategory.SYNTHESIS_ERROR


class TestCategorizeFileError:
    """Tests for categorizing file errors."""

    def test_categorizes_permission_denied(self):
        """Categorizes permission denied as file error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Permission denied")
        assert result == ErrorCategory.FILE_ERROR

    def test_categorizes_file_not_found(self):
        """Categorizes file not found as file error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("File not found")
        assert result == ErrorCategory.FILE_ERROR

    def test_categorizes_cannot_write_file(self):
        """Categorizes cannot write file as file error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Cannot write to file")
        assert result == ErrorCategory.FILE_ERROR

    def test_categorizes_disk_full(self):
        """Categorizes disk full as file error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Disk is full")
        assert result == ErrorCategory.FILE_ERROR

    def test_categorizes_readonly_database(self):
        """Categorizes readonly database as file error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Attempt to write readonly database")
        assert result == ErrorCategory.FILE_ERROR


class TestCategorizeUnknownError:
    """Tests for unknown error categorization."""

    def test_returns_unknown_for_unmatched(self):
        """Returns UNKNOWN_ERROR for unmatched error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error(
            "Some completely random error message"
        )
        assert result == ErrorCategory.UNKNOWN_ERROR

    def test_returns_unknown_for_empty(self):
        """Returns UNKNOWN_ERROR for empty message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("")
        assert result == ErrorCategory.UNKNOWN_ERROR


class TestCategorizeCaseInsensitive:
    """Tests for case-insensitive error categorization."""

    def test_matches_uppercase_message(self):
        """Matches uppercase error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("CONNECTION REFUSED")
        assert result == ErrorCategory.CONNECTION_ERROR

    def test_matches_mixed_case_message(self):
        """Matches mixed case error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.categorize_error("Connection Refused")
        assert result == ErrorCategory.CONNECTION_ERROR


class TestGetUserFriendlyTitle:
    """Tests for get_user_friendly_title method."""

    def test_returns_title_for_connection_error(self):
        """Returns friendly title for connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_user_friendly_title(
            ErrorCategory.CONNECTION_ERROR
        )
        assert result == "Connection Issue"

    def test_returns_title_for_model_error(self):
        """Returns friendly title for model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_user_friendly_title(ErrorCategory.MODEL_ERROR)
        assert result == "LLM Service Error"

    def test_returns_title_for_search_error(self):
        """Returns friendly title for search error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_user_friendly_title(ErrorCategory.SEARCH_ERROR)
        assert result == "Search Service Error"

    def test_returns_title_for_synthesis_error(self):
        """Returns friendly title for synthesis error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_user_friendly_title(ErrorCategory.SYNTHESIS_ERROR)
        assert result == "Report Generation Error"

    def test_returns_title_for_file_error(self):
        """Returns friendly title for file error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_user_friendly_title(ErrorCategory.FILE_ERROR)
        assert result == "File System Error"

    def test_returns_title_for_rate_limit_error(self):
        """Returns friendly title for rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_user_friendly_title(
            ErrorCategory.RATE_LIMIT_ERROR
        )
        assert result == "API Rate Limit Exceeded"

    def test_returns_title_for_unknown_error(self):
        """Returns friendly title for unknown error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_user_friendly_title(ErrorCategory.UNKNOWN_ERROR)
        assert result == "Unexpected Error"


class TestGetSuggestedActions:
    """Tests for get_suggested_actions method."""

    def test_returns_list_for_connection_error(self):
        """Returns suggestions list for connection error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_suggested_actions(ErrorCategory.CONNECTION_ERROR)
        assert isinstance(result, list)
        assert len(result) > 0

    def test_suggestions_include_check_service(self):
        """Connection error suggestions include checking service."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_suggested_actions(ErrorCategory.CONNECTION_ERROR)
        assert any("LLM service" in s or "Ollama" in s for s in result)

    def test_returns_suggestions_for_model_error(self):
        """Returns suggestions for model error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_suggested_actions(ErrorCategory.MODEL_ERROR)
        assert isinstance(result, list)
        assert len(result) > 0

    def test_returns_suggestions_for_rate_limit_error(self):
        """Returns suggestions for rate limit error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_suggested_actions(ErrorCategory.RATE_LIMIT_ERROR)
        assert isinstance(result, list)
        assert len(result) > 0

    def test_rate_limit_suggests_enabling_rate_limiting(self):
        """Rate limit suggestions include enabling rate limiting."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_suggested_actions(ErrorCategory.RATE_LIMIT_ERROR)
        assert any("Rate Limiting" in s for s in result)

    def test_returns_suggestions_for_unknown_error(self):
        """Returns suggestions for unknown error."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.get_suggested_actions(ErrorCategory.UNKNOWN_ERROR)
        assert isinstance(result, list)
        assert len(result) > 0


class TestDetermineSeverity:
    """Tests for _determine_severity method."""

    def test_connection_error_is_high(self):
        """Connection error has high severity."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._determine_severity(ErrorCategory.CONNECTION_ERROR)
        assert result == "high"

    def test_model_error_is_high(self):
        """Model error has high severity."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._determine_severity(ErrorCategory.MODEL_ERROR)
        assert result == "high"

    def test_synthesis_error_is_low(self):
        """Synthesis error has low severity."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._determine_severity(ErrorCategory.SYNTHESIS_ERROR)
        assert result == "low"

    def test_rate_limit_error_is_medium(self):
        """Rate limit error has medium severity."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._determine_severity(ErrorCategory.RATE_LIMIT_ERROR)
        assert result == "medium"

    def test_unknown_error_is_high(self):
        """Unknown error has high severity."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._determine_severity(ErrorCategory.UNKNOWN_ERROR)
        assert result == "high"


class TestIsRecoverable:
    """Tests for _is_recoverable method."""

    def test_connection_error_is_recoverable(self):
        """Connection error is recoverable."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._is_recoverable(ErrorCategory.CONNECTION_ERROR)
        assert result is True

    def test_model_error_is_recoverable(self):
        """Model error is recoverable."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._is_recoverable(ErrorCategory.MODEL_ERROR)
        assert result is True

    def test_rate_limit_error_is_recoverable(self):
        """Rate limit error is recoverable."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._is_recoverable(ErrorCategory.RATE_LIMIT_ERROR)
        assert result is True

    def test_unknown_error_is_not_recoverable(self):
        """Unknown error is not recoverable."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._is_recoverable(ErrorCategory.UNKNOWN_ERROR)
        assert result is False


class TestExtractServiceName:
    """Tests for _extract_service_name method."""

    def test_extracts_openai(self):
        """Extracts OpenAI from error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._extract_service_name("OpenAI API error: 429")
        assert result == "Openai"

    def test_extracts_anthropic(self):
        """Extracts Anthropic from error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._extract_service_name("Anthropic rate limit exceeded")
        assert result == "Anthropic"

    def test_extracts_google(self):
        """Extracts Google from error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._extract_service_name("Google API error")
        assert result == "Google"

    def test_extracts_ollama(self):
        """Extracts Ollama from error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._extract_service_name("Ollama connection failed")
        assert result == "Ollama"

    def test_extracts_tavily(self):
        """Extracts Tavily from error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._extract_service_name("Tavily search failed")
        assert result == "Tavily"

    def test_extracts_brave(self):
        """Extracts Brave from error message."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._extract_service_name("Brave Search API error")
        assert result == "Brave"

    def test_returns_default_for_unknown(self):
        """Returns API Service for unknown service."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter._extract_service_name("Unknown service error")
        assert result == "API Service"


class TestAnalyzeError:
    """Tests for analyze_error method."""

    def test_returns_dict(self):
        """Returns a dictionary."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.analyze_error("Connection refused")
        assert isinstance(result, dict)

    def test_includes_category(self):
        """Includes category in analysis."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorCategory,
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.analyze_error("Connection refused")
        assert "category" in result
        assert result["category"] == ErrorCategory.CONNECTION_ERROR

    def test_includes_title(self):
        """Includes user-friendly title in analysis."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.analyze_error("Connection refused")
        assert "title" in result
        assert isinstance(result["title"], str)

    def test_includes_original_error(self):
        """Includes original error message in analysis."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        error_msg = "Connection refused"
        result = reporter.analyze_error(error_msg)
        assert "original_error" in result
        assert result["original_error"] == error_msg

    def test_includes_suggestions(self):
        """Includes suggestions in analysis."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.analyze_error("Connection refused")
        assert "suggestions" in result
        assert isinstance(result["suggestions"], list)

    def test_includes_severity(self):
        """Includes severity in analysis."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.analyze_error("Connection refused")
        assert "severity" in result
        assert result["severity"] in ["low", "medium", "high"]

    def test_includes_recoverable(self):
        """Includes recoverable flag in analysis."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        result = reporter.analyze_error("Connection refused")
        assert "recoverable" in result
        assert isinstance(result["recoverable"], bool)

    def test_includes_context_if_provided(self):
        """Includes context in analysis if provided."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        context = {"key": "value"}
        result = reporter.analyze_error("Error", context=context)
        assert "context" in result
        assert result["context"] == context

    def test_has_partial_results_with_findings(self):
        """Sets has_partial_results when findings in context."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        context = {"findings": ["some finding"]}
        result = reporter.analyze_error("Error", context=context)
        assert "has_partial_results" in result
        assert result["has_partial_results"] is True

    def test_has_partial_results_with_current_knowledge(self):
        """Sets has_partial_results when current_knowledge in context."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        context = {"current_knowledge": "some knowledge"}
        result = reporter.analyze_error("Error", context=context)
        assert result["has_partial_results"] is True

    def test_has_partial_results_with_search_results(self):
        """Sets has_partial_results when search_results in context."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        context = {"search_results": ["result1"]}
        result = reporter.analyze_error("Error", context=context)
        assert result["has_partial_results"] is True

    def test_no_partial_results_with_empty_context(self):
        """No partial results with empty context."""
        from local_deep_research.error_handling.error_reporter import (
            ErrorReporter,
        )

        reporter = ErrorReporter()
        context = {}
        result = reporter.analyze_error("Error", context=context)
        assert (
            "has_partial_results" not in result
            or result.get("has_partial_results") is False
        )
