Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

19.1 KB
Raw Blame
"""
Tests for the SofyaSearchEngine class.

Tests cover:
- Initialization and configuration
- API key handling
- Request payload construction
- Preview generation
- Full content retrieval
- Rate limit error handling
- Run method
- Class attributes
"""

import requests

from unittest.mock import Mock, patch

import pytest

MODULE = "local_deep_research.web_search_engines.engines.search_engine_sofya"


def _make_engine(**kwargs):
    from local_deep_research.web_search_engines.engines.search_engine_sofya import (
        SofyaSearchEngine,
    )

    kwargs.setdefault("api_key", "test-api-key")
    return SofyaSearchEngine(**kwargs)


class TestSofyaSearchEngineInit:
    """Tests for SofyaSearchEngine initialization."""

    def test_init_with_api_key(self):
        """Initialize with API key and defaults."""
        engine = _make_engine()

        assert engine.api_key == "test-api-key"
        assert engine.max_results == 10
        assert engine.topic == "general"
        assert engine.include_full_content is True

    def test_init_with_custom_max_results(self):
        """Initialize with custom max_results."""
        engine = _make_engine(max_results=25)

        assert engine.max_results == 25

    def test_init_invalid_topic_falls_back_to_general(self):
        """An unsupported topic falls back to 'general'."""
        engine = _make_engine(topic="sports")

        assert engine.topic == "general"

    def test_init_with_domain_filters(self):
        """Initialize with domain filters."""
        engine = _make_engine(
            include_domains=["example.com", "test.com"],
            exclude_domains=["spam.com"],
        )

        assert engine.include_domains == ["example.com", "test.com"]
        assert engine.exclude_domains == ["spam.com"]

    def test_domain_filters_capped_at_max(self):
        """Domain lists are capped at MAX_DOMAINS entries."""
        from local_deep_research.web_search_engines.engines.search_engine_sofya import (
            SofyaSearchEngine,
        )

        too_many = [f"domain{i}.com" for i in range(15)]
        engine = _make_engine(include_domains=too_many)

        assert len(engine.include_domains) == SofyaSearchEngine.MAX_DOMAINS

    def test_init_without_api_key_raises(self):
        """Initialize without API key raises ValueError."""
        from local_deep_research.web_search_engines.engines.search_engine_sofya import (
            SofyaSearchEngine,
        )

        with pytest.raises(
            ValueError, match="No valid API key found for Sofya"
        ):
            SofyaSearchEngine()

    def test_init_with_api_key_from_settings(self):
        """Initialize with API key from settings snapshot."""
        from local_deep_research.web_search_engines.engines.search_engine_sofya import (
            SofyaSearchEngine,
        )

        engine = SofyaSearchEngine(
            settings_snapshot={
                "search.engine.web.sofya.api_key": "settings-api-key"
            }
        )

        assert engine.api_key == "settings-api-key"

    def test_init_with_llm(self):
        """Initialize with LLM."""
        mock_llm = Mock()
        engine = _make_engine(llm=mock_llm)

        assert engine.llm is mock_llm

    def test_init_derives_snippets_only_from_include_full_content(self):
        """Like TinyFish: search_snippets_only defaults to the inverse of
        include_full_content, so the full-content path actually runs when
        content is requested."""
        engine = _make_engine()
        assert engine.search_snippets_only is False

        engine = _make_engine(include_full_content=False)
        assert engine.search_snippets_only is True

    def test_init_explicit_snippets_only_wins_over_derivation(self):
        """An explicit search_snippets_only (e.g. forwarded from the
        search.snippets_only setting by the factory) beats the derived value."""
        engine = _make_engine(
            include_full_content=True, search_snippets_only=True
        )

        assert engine.search_snippets_only is True


class TestBuildPayload:
    """Tests for _build_payload."""

    def test_payload_basics(self):
        """Payload includes query, depth, max_results, and topic."""
        engine = _make_engine(max_results=5)

        payload = engine._build_payload("hello world")

        assert payload["query"] == "hello world"
        # include_full_content defaults to True (and search_snippets_only
        # derives to False), so the content will be consumed -> basic depth.
        assert payload["search_depth"] == "basic"
        assert payload["max_results"] == 5
        assert payload["topic"] == "general"
        # Optional fields are omitted when unset.
        assert "freshness" not in payload
        assert "include_domains" not in payload
        assert "exclude_domains" not in payload

    def test_payload_snippets_depth_when_no_full_content(self):
        """include_full_content=False maps to the cheaper snippets depth."""
        engine = _make_engine(include_full_content=False)

        payload = engine._build_payload("q")

        assert payload["search_depth"] == "snippets"

    def test_payload_snippets_depth_when_snippets_only(self):
        """search_snippets_only=True uses the snippets depth even when
        include_full_content=True: run() would discard the content, so the
        3x-priced basic depth must not be paid for."""
        engine = _make_engine(
            include_full_content=True, search_snippets_only=True
        )

        payload = engine._build_payload("q")

        assert payload["search_depth"] == "snippets"

    def test_payload_caps_max_results(self):
        """max_results is capped at MAX_RESULTS_CAP in the payload."""
        engine = _make_engine(max_results=100)

        payload = engine._build_payload("q")

        assert payload["max_results"] == engine.MAX_RESULTS_CAP

    def test_payload_includes_optional_fields(self):
        """Freshness and domain filters are included when set."""
        engine = _make_engine(
            freshness="week",
            include_domains=["example.com"],
            exclude_domains=["spam.com"],
        )

        payload = engine._build_payload("q")

        assert payload["freshness"] == "week"
        assert payload["include_domains"] == ["example.com"]
        assert payload["exclude_domains"] == ["spam.com"]


class TestGetPreviews:
    """Tests for _get_previews method."""

    def test_get_previews_returns_results(self):
        """Get previews returns formatted results."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.json.return_value = {
            "results": [
                {
                    "title": "Result 1",
                    "url": "https://example1.com",
                    "description": "Snippet 1",
                    "content": "Full content 1",
                },
                {
                    "title": "Result 2",
                    "url": "https://example2.com",
                    "description": "Snippet 2",
                    "content": "Full content 2",
                },
            ]
        }

        with patch(f"{MODULE}.safe_post", return_value=mock_response):
            engine = _make_engine()
            previews = engine._search_previews("test query")

            assert len(previews) == 2
            assert previews[0]["title"] == "Result 1"
            assert previews[0]["snippet"] == "Snippet 1"
            assert previews[0]["link"] == "https://example1.com"
            assert previews[0]["_full_result"]["content"] == "Full content 1"
            # The extracted content must stay in the _full_result stash until
            # _get_full_content promotes it: previews are returned as-is in
            # snippets-only mode, so full_content here would leak full page
            # text into the synthesis prompt.
            assert "full_content" not in previews[0]

    def test_get_previews_snippet_falls_back_to_content(self):
        """When description is missing, snippet falls back to content slice."""
        long_content = "x" * 1000
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.json.return_value = {
            "results": [
                {
                    "title": "No description",
                    "url": "https://example.com",
                    "content": long_content,
                }
            ]
        }

        with patch(f"{MODULE}.safe_post", return_value=mock_response):
            engine = _make_engine()
            previews = engine._search_previews("test query")

            assert len(previews[0]["snippet"]) == engine.SNIPPET_PREVIEW_CHARS

    def test_get_previews_sends_auth_header(self):
        """Get previews sends the bearer auth header."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.json.return_value = {"results": []}

        with patch(
            f"{MODULE}.safe_post", return_value=mock_response
        ) as mock_post:
            engine = _make_engine()
            engine._search_previews("test query")

            headers = mock_post.call_args[1]["headers"]
            assert headers["Authorization"] == "Bearer test-api-key"

    def test_get_previews_rate_limit_error(self):
        """Get previews raises RateLimitError on 429."""
        from local_deep_research.web_search_engines.rate_limiting import (
            RateLimitError,
        )

        mock_response = Mock()
        mock_response.status_code = 429
        mock_response.raise_for_status.side_effect = requests.HTTPError(
            response=mock_response
        )
        mock_response.text = "Rate limit exceeded"

        with patch(f"{MODULE}.safe_post", return_value=mock_response):
            engine = _make_engine()

            with pytest.raises(RateLimitError):
                engine._search_previews("test query")

    def test_get_previews_empty_results(self):
        """Get previews handles empty results."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.json.return_value = {"results": []}

        with patch(f"{MODULE}.safe_post", return_value=mock_response):
            engine = _make_engine()
            previews = engine._search_previews("test query")

            assert previews == []

    def test_get_previews_request_exception(self):
        """Get previews handles request exceptions gracefully."""
        import requests

        with patch(
            f"{MODULE}.safe_post",
            side_effect=requests.exceptions.RequestException(
                "Connection error"
            ),
        ):
            engine = _make_engine()
            previews = engine._search_previews("test query")

            assert previews == []

    def test_get_previews_request_exception_with_rate_limit_status(self):
        """A RequestException carrying a 429 response raises RateLimitError."""
        import requests

        from local_deep_research.web_search_engines.rate_limiting import (
            RateLimitError,
        )

        err = requests.exceptions.RequestException("boom")
        err.response = Mock(status_code=429)

        with patch(f"{MODULE}.safe_post", side_effect=err):
            engine = _make_engine()

            with pytest.raises(RateLimitError):
                engine._search_previews("test query")

    def test_get_previews_unexpected_error(self):
        """Get previews handles unexpected errors."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.json.side_effect = Exception("Parse error")
        mock_response.raise_for_status = Mock()

        with patch(f"{MODULE}.safe_post", return_value=mock_response):
            engine = _make_engine()
            previews = engine._search_previews("test query")

            assert previews == []


class TestGetFullContent:
    """Tests for _get_full_content method."""

    def test_get_full_content_promotes_stashed_content(self):
        """The extracted content is promoted from the _full_result stash to
        full_content, the citation link survives, and the internal
        _full_result marker is dropped."""
        engine = _make_engine(include_full_content=True)

        items = [
            {
                "title": "Result",
                "link": "https://example.com",
                "snippet": "Short snippet",
                "_full_result": {
                    "url": "https://example.com",
                    "content": "Full extracted markdown content",
                },
            }
        ]

        results = engine._get_full_content(items)

        assert len(results) == 1
        assert results[0]["full_content"] == "Full extracted markdown content"
        assert results[0]["link"] == "https://example.com"
        assert "_full_result" not in results[0]

    def test_get_full_content_falls_back_to_snippet(self):
        """When the stash carries no extracted content (e.g. snippets depth),
        fall back to the snippet."""
        engine = _make_engine(include_full_content=True)

        items = [
            {
                "title": "Result",
                "link": "https://example.com",
                "snippet": "Only a snippet",
                "_full_result": {"url": "https://example.com"},
            }
        ]

        results = engine._get_full_content(items)

        assert results[0]["full_content"] == "Only a snippet"

    def test_get_full_content_without_full_result(self):
        """Get full content handles items without _full_result."""
        engine = _make_engine(include_full_content=False)

        items = [{"title": "Result", "link": "https://example.com"}]

        results = engine._get_full_content(items)

        assert len(results) == 1
        assert results[0]["title"] == "Result"


class TestRun:
    """Tests for run method."""

    def test_run_returns_results(self):
        """Run promotes the stashed content to full_content."""
        engine = _make_engine()

        with patch.object(
            engine,
            "_get_previews",
            return_value=[
                {
                    "id": "https://example.com",
                    "title": "Result",
                    "snippet": "Snippet",
                    "link": "https://example.com",
                    "_full_result": {
                        "url": "https://example.com",
                        "content": "Full page text",
                    },
                }
            ],
        ):
            results = engine.run("test query")

        assert len(results) == 1
        # full_content is the field the report's citation handler reads
        # before falling back to the snippet.
        assert results[0]["full_content"] == "Full page text"
        assert "_full_result" not in results[0]

    def test_run_surfaces_full_content_end_to_end(self):
        """End-to-end (real _get_previews): the inline extracted page content
        reaches run() output under full_content, while the snippet stays the
        short SERP description. Guards against the depth/credits being paid for
        content that is then discarded."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.raise_for_status = Mock()
        mock_response.json.return_value = {
            "results": [
                {
                    "title": "Result",
                    "url": "https://example.com",
                    "description": "Short SERP snippet",
                    "content": "Full extracted page markdown",
                }
            ]
        }

        with patch(f"{MODULE}.safe_post", return_value=mock_response):
            engine = _make_engine(include_full_content=True)
            results = engine.run("test query")

        assert len(results) == 1
        assert results[0]["full_content"] == "Full extracted page markdown"
        assert results[0]["snippet"] == "Short SERP snippet"
        assert results[0]["link"] == "https://example.com"

    def test_run_snippets_only_carries_no_full_content(self):
        """Regression: in snippets-only mode run() returns the previews
        directly (no _get_full_content call), so they must not carry
        full_content - otherwise full page text would leak into the
        synthesis prompt despite the setting."""
        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.raise_for_status = Mock()
        mock_response.json.return_value = {
            "results": [
                {
                    "title": "Result",
                    "url": "https://example.com",
                    "description": "Short SERP snippet",
                    "content": "Full extracted page markdown",
                }
            ]
        }

        with patch(f"{MODULE}.safe_post", return_value=mock_response):
            engine = _make_engine(search_snippets_only=True)
            results = engine.run("test query")

        assert len(results) == 1
        assert "full_content" not in results[0]
        assert results[0]["snippet"] == "Short SERP snippet"

    def test_run_handles_empty_results(self):
        """Run handles empty results."""
        engine = _make_engine()

        with patch.object(engine, "_get_previews", return_value=[]):
            results = engine.run("test query")

            assert results == []


class TestClassAttributes:
    """Tests for class attributes."""

    def test_is_public(self):
        """SofyaSearchEngine is marked as public."""
        from local_deep_research.web_search_engines.engines.search_engine_sofya import (
            SofyaSearchEngine,
        )

        assert SofyaSearchEngine.is_public is True

    def test_is_generic(self):
        """SofyaSearchEngine is marked as generic."""
        from local_deep_research.web_search_engines.engines.search_engine_sofya import (
            SofyaSearchEngine,
        )

        assert SofyaSearchEngine.is_generic is True

    def test_egress_labels_match_public_engine(self):
        """A public engine must declare NON_SENSITIVE/EXPOSING egress labels
        (enforced by test_registered_engines_declare_consistent_labels)."""
        from local_deep_research.security.egress.classification import (
            Exposure,
            Sensitivity,
        )
        from local_deep_research.web_search_engines.engines.search_engine_sofya import (
            SofyaSearchEngine,
        )

        # Compare by .value, not by `is` or `==`. Under the Docker test
        # setup the package is simultaneously available from /app/src
        # (PYTHONPATH) and the installed wheel at /install/.venv, so
        # SofyaSearchEngine's class attribute and the test's `Sensitivity`
        # can resolve to distinct class objects with the same members.
        # Python Enum semantics: `is` AND `==` both return False across
        # distinct class objects; only `.value` (or `.name`) is robust.
        # The sister test test_registered_engines_declare_consistent_labels
        # uses `is` without flaking because it resolves the engine class
        # through the registry's lazy import, which pins one module object.
        assert (
            SofyaSearchEngine.egress_sensitivity.value
            == Sensitivity.NON_SENSITIVE.value
        )
        assert (
            SofyaSearchEngine.egress_exposure.value == Exposure.EXPOSING.value
        )