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.

61.1 KB
Raw Blame
"""
Tests for the OpenAlexSearchEngine class.

Tests cover:
- Initialization and configuration
- Email identification and optional API-key authentication
- Sort and filter options
- Preview generation
- Abstract reconstruction
- Full content retrieval
- Rate limiting
"""

import requests

from unittest.mock import MagicMock, Mock, patch
import pytest


def _messages(mock_logger, level):
    """Return the message strings logged at *level* on a mocked logger.

    The engine modules log through ``security.secure_logging.logger``, a
    ``__slots__`` proxy: ``patch("....logger.error")`` cannot bind an
    attribute on it, so the whole logger object is replaced instead.
    """
    return [
        str(call.args[0]).format(*call.args[1:])
        if len(call.args) > 1
        else str(call.args[0])
        if call.args
        else ""
        for call in getattr(mock_logger, level).call_args_list
    ]


# Mock JournalReputationFilter for all OpenAlex tests
@pytest.fixture(autouse=True)
def mock_journal_filter():
    """Mock JournalReputationFilter to avoid LLM initialization."""
    with patch(
        "local_deep_research.advanced_search_system.filters.journal_reputation_filter.JournalReputationFilter.create_default",
        return_value=None,
    ):
        yield


class TestOpenAlexSearchEngineInit:
    """Tests for OpenAlexSearchEngine initialization."""

    def test_init_with_defaults(self):
        """Initialize with default values."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        assert engine.max_results == 25
        assert engine.sort_by == "relevance"
        assert engine.filter_open_access is False
        assert engine.min_citations == 0
        assert engine.from_publication_date is None
        assert engine.email is None
        assert engine.api_key is None
        assert engine.openalex_api_key is None
        assert "Authorization" not in engine.headers
        assert engine.api_base == "https://api.openalex.org"

    def test_init_with_custom_max_results(self):
        """Initialize with custom max_results."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(max_results=50)

        assert engine.max_results == 50

    def test_init_with_email(self):
        """Initialize with email for User-Agent identification."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(email="[email protected]")

        assert engine.email == "[email protected]"
        assert "[email protected]" in engine.headers["User-Agent"]

    def test_init_with_api_key(self):
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="  openalex-test-key  ")

        assert engine.api_key == "openalex-test-key"
        assert engine.openalex_api_key == "openalex-test-key"
        assert engine.headers["Authorization"] == "Bearer openalex-test-key"

    def test_init_with_false_email_string(self):
        """Initialize with 'False' string email."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(email="False")

        assert engine.email is None

    def test_init_with_false_api_key_string(self):
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="False")

        assert engine.api_key is None
        assert engine.openalex_api_key is None
        assert "Authorization" not in engine.headers

    # Every row below used to be sent verbatim as
    # ``Authorization: Bearer <value>``, which OpenAlex answers with
    # 401/403 — turning a working keyless engine into one that returns
    # nothing. They must all resolve to keyless mode instead.
    @pytest.mark.parametrize(
        "configured_key",
        [
            "${OPENALEX_API_KEY}",
            "${openalex_api_key}",
            "your-api-key-here",
            "YOUR_API_KEY_HERE",
            "<your_api_key>",
            "your_api_key",
            "none",
            "placeholder",
            " False ",
            "false",
            "False",
            "FALSE",
        ],
    )
    def test_init_placeholder_key_falls_back_to_keyless(self, configured_key):
        """Placeholder/sentinel keys are never sent as a Bearer token."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key=configured_key)

        assert engine.api_key is None
        assert engine.openalex_api_key is None
        assert "Authorization" not in engine.headers

    @pytest.mark.parametrize("configured_key", ["   ", "\t\n", ""])
    def test_init_whitespace_only_key_is_none_not_empty_string(
        self, configured_key
    ):
        """A blank key normalizes to None, not "" (an empty credential)."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key=configured_key)

        assert engine.api_key is None
        assert engine.openalex_api_key is None
        assert "Authorization" not in engine.headers

    @pytest.mark.parametrize(
        "configured_key", [True, 1, 123, 3.5, ["k"], {"k": 1}]
    )
    def test_init_non_string_key_is_none_not_attribute_error(
        self, configured_key
    ):
        """A truthy non-str setting used to raise AttributeError on .strip()."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key=configured_key)

        assert engine.api_key is None
        assert engine.openalex_api_key is None
        assert "Authorization" not in engine.headers

    def test_init_real_key_still_authenticates(self):
        """The placeholder filter must not reject a genuine key."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="  oa-live-abc123  ")

        assert engine.api_key == "oa-live-abc123"
        assert engine.headers["Authorization"] == "Bearer oa-live-abc123"

    def test_secret_attrs_extends_the_base_default(self):
        """The base default plus the slot holding a dropped key."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )
        from local_deep_research.web_search_engines.search_engine_base import (
            BaseSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="oa-live-abc123")

        assert set(BaseSearchEngine._secret_attrs) <= set(
            OpenAlexSearchEngine._secret_attrs
        )
        assert "_rejected_api_key" in OpenAlexSearchEngine._secret_attrs
        assert "oa-live-abc123" not in engine._scrub_error(
            RuntimeError("boom oa-live-abc123")
        )

    def test_dropped_key_stays_in_the_redaction_set(self):
        """Dropping the key must not stop it being scrubbed from logs.

        ``_scrub_error`` can only redact a literal it is handed. Clearing
        ``self.api_key`` on rejection would otherwise silently remove the
        key from the redaction set for the rest of the engine's life.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
        engine._drop_rejected_api_key()

        assert engine.api_key is None
        assert "Authorization" not in engine.headers
        assert "oa-live-abc123" not in engine._scrub_error(
            RuntimeError("boom oa-live-abc123")
        )

    def test_init_with_sort_by(self):
        """Initialize with custom sort_by."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(sort_by="cited_by_count")

        assert engine.sort_by == "cited_by_count"

    def test_init_with_open_access_filter(self):
        """Initialize with open access filter."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(filter_open_access=True)

        assert engine.filter_open_access is True

    def test_init_with_min_citations(self):
        """Initialize with minimum citations filter."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(min_citations=100)

        assert engine.min_citations == 100

    def test_init_with_from_publication_date(self):
        """Initialize with publication date filter."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(from_publication_date="2023-01-01")

        assert engine.from_publication_date == "2023-01-01"

    def test_init_with_false_publication_date(self):
        """Initialize with 'False' string publication date."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(from_publication_date="False")

        assert engine.from_publication_date is None

    def test_init_with_llm(self):
        """Initialize with LLM."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        mock_llm = Mock()
        engine = OpenAlexSearchEngine(llm=mock_llm)

        assert engine.llm is mock_llm


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

    def test_get_previews_returns_results(self):
        """Get previews returns formatted results."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.headers = {}
        mock_response.json.return_value = {
            "meta": {"count": 1},
            "results": [
                {
                    "id": "https://openalex.org/W123",
                    "display_name": "Test Paper",
                    "publication_year": 2023,
                    "publication_date": "2023-05-15",
                    "doi": "https://doi.org/10.1234/test",
                    "cited_by_count": 50,
                    "authorships": [
                        {"author": {"display_name": "John Doe"}},
                        {"author": {"display_name": "Jane Smith"}},
                    ],
                    "primary_location": {"source": {"display_name": "Nature"}},
                    "open_access": {"is_oa": True},
                    "best_oa_location": {
                        "pdf_url": "https://example.com/paper.pdf"
                    },
                    "abstract_inverted_index": {"Test": [0], "abstract": [1]},
                }
            ],
        }

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=mock_response,
        ):
            engine = OpenAlexSearchEngine()
            previews = engine._search_previews("machine learning")

            assert len(previews) == 1
            assert previews[0]["title"] == "Test Paper"
            assert previews[0]["year"] == 2023
            assert previews[0]["citations"] == 50
            assert previews[0]["is_open_access"] is True
            assert "John Doe" in previews[0]["authors"]

    def test_get_previews_with_filters(self):
        """Get previews includes filters in request."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.headers = {}
        mock_response.json.return_value = {"meta": {"count": 0}, "results": []}

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=mock_response,
        ) as mock_get:
            engine = OpenAlexSearchEngine(
                filter_open_access=True,
                min_citations=100,
                from_publication_date="2023-01-01",
            )
            engine._search_previews("test query")

            call_kwargs = mock_get.call_args[1]
            params = call_kwargs["params"]
            assert "filter" in params
            assert "is_oa:true" in params["filter"]
            assert "cited_by_count:>100" in params["filter"]
            assert "from_publication_date:2023-01-01" in params["filter"]

    def test_get_previews_with_email(self):
        """Get previews uses email only for User-Agent identification."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.headers = {}
        mock_response.json.return_value = {"meta": {"count": 0}, "results": []}

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=mock_response,
        ) as mock_get:
            engine = OpenAlexSearchEngine(email="[email protected]")
            engine._search_previews("test query")

            call_kwargs = mock_get.call_args[1]
            params = call_kwargs["params"]
            headers = call_kwargs["headers"]
            assert "mailto" not in params
            assert "[email protected]" in headers["User-Agent"]
            assert "Authorization" not in headers

    def test_get_previews_with_api_key_uses_authorization_header(self):
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.headers = {}
        mock_response.json.return_value = {"meta": {"count": 0}, "results": []}

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=mock_response,
        ) as mock_get:
            engine = OpenAlexSearchEngine(
                email="[email protected]", api_key="openalex-test-key"
            )
            engine._search_previews("test query")

            call_kwargs = mock_get.call_args[1]
            params = call_kwargs["params"]
            headers = call_kwargs["headers"]
            assert headers["Authorization"] == "Bearer openalex-test-key"
            assert "mailto" not in params
            assert "api_key" not in params
            assert "openalex-test-key" not in params.values()

    @pytest.mark.parametrize("auth_status", [401, 403])
    def test_get_previews_retries_once_without_rejected_key(self, auth_status):
        """A rejected key must not cost the user their results."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = auth_status
        rejected.headers = {}
        rejected.text = "invalid api key"

        ok = Mock()
        ok.status_code = 200
        ok.headers = {}
        ok.json.return_value = {
            "meta": {"count": 1},
            "results": [
                {"id": "https://openalex.org/W1", "display_name": "Paper"}
            ],
        }

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            side_effect=[rejected, ok],
        ) as mock_get:
            engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
            previews = engine._search_previews("test query")

        assert len(previews) == 1
        assert mock_get.call_count == 2
        first_headers = mock_get.call_args_list[0].kwargs["headers"]
        retry_headers = mock_get.call_args_list[1].kwargs["headers"]
        assert first_headers["Authorization"] == "Bearer oa-live-abc123"
        assert "Authorization" not in retry_headers
        # And the rejected key is gone for good: the header is cleared,
        # not just skipped for this one retry.
        assert "Authorization" not in engine.headers
        assert engine.api_key is None
        assert engine.openalex_api_key is None

    def test_rejected_key_is_never_sent_twice(self):
        """No later query pays another round-trip for a rejected key.

        The general property BL2 is about: once OpenAlex has refused the
        credential, *no* path on this object sends it again.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "invalid api key"

        def _ok():
            response = Mock()
            response.status_code = 200
            response.headers = {}
            response.json.return_value = {
                "meta": {"count": 1},
                "results": [
                    {"id": "https://openalex.org/W1", "display_name": "Paper"}
                ],
            }
            return response

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            side_effect=[rejected, _ok(), _ok(), _ok()],
        ) as mock_get:
            engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
            engine._search_previews("first query")
            engine._search_previews("second query")
            engine._search_previews("third query")

        # 1 keyed + 1 keyless retry + 1 each for the next two queries.
        assert mock_get.call_count == 4
        keyed = [
            call
            for call in mock_get.call_args_list
            if "Authorization" in call.kwargs["headers"]
        ]
        assert len(keyed) == 1
        assert not any(
            "oa-live-abc123" in str(call.kwargs["headers"])
            for call in mock_get.call_args_list[1:]
        )

    def test_keyless_retry_is_rate_limited_too(self):
        """The retry is a real second request, so it gets its own slot."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "invalid api key"

        ok = Mock()
        ok.status_code = 200
        ok.headers = {}
        ok.json.return_value = {"meta": {"count": 0}, "results": []}

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            side_effect=[rejected, ok],
        ) as mock_get:
            engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
            with patch.object(
                engine.rate_tracker, "apply_rate_limit", return_value=0.0
            ) as apply_rate_limit:
                engine._search_previews("test query")

        assert mock_get.call_count == 2
        assert apply_rate_limit.call_count == 2

    def test_get_previews_auth_failure_after_retry_raises(self):
        """A persistent auth failure surfaces instead of a silent []."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexAuthError,
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "invalid api key"

        still_rejected = Mock()
        still_rejected.status_code = 403
        still_rejected.raise_for_status.side_effect = requests.HTTPError(
            response=still_rejected
        )
        still_rejected.headers = {}
        still_rejected.text = "forbidden"

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            side_effect=[rejected, still_rejected],
        ) as mock_get:
            engine = OpenAlexSearchEngine(api_key="oa-live-abc123")

            with pytest.raises(OpenAlexAuthError):
                engine._get_previews("test query")

        assert mock_get.call_count == 2

    @pytest.mark.parametrize("auth_status", [401, 403])
    def test_keyless_auth_failure_behaves_exactly_as_before(self, auth_status):
        """No key configured → nothing to retry, and no key to blame.

        A keyless install behind a corporate proxy or Cloudflare can see
        a 401/403 that has nothing to do with a credential. It keeps the
        pre-existing behaviour: one request, an "API error" log, ``[]``
        — not an ``OpenAlexAuthError`` telling the operator to check a
        key they never configured.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = auth_status
        rejected.headers = {}
        rejected.text = "unauthorized"

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=rejected,
        ) as mock_get:
            engine = OpenAlexSearchEngine()
            with patch(
                "local_deep_research.web_search_engines.engines.search_engine_openalex.logger",
                new=MagicMock(),
            ) as engine_logger:
                assert engine._search_previews("test query") == []

        logged = _messages(engine_logger, "error")
        warned = _messages(engine_logger, "warning")
        assert mock_get.call_count == 1
        assert not any("API key" in message for message in logged + warned)
        assert not any(
            "openalex.org/settings/api" in message for message in warned
        )
        assert any(
            f"OpenAlex API error: {auth_status}" in message
            for message in logged
        )

    def test_run_reports_auth_failure_instead_of_empty_success(self):
        """run() takes the failure path, not the "no results" path.

        Before the fix ``_get_previews`` returned ``[]`` on 401, which
        ``run`` logs as an ordinary empty search — indistinguishable from
        "no papers matched".
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "unauthorized"

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=rejected,
        ):
            # A key must be configured: the failure is only reported as
            # an authentication failure when a key was actually sent and
            # the automatic keyless retry failed too.
            engine = OpenAlexSearchEngine(
                programmatic_mode=True, api_key="oa-live-abc123"
            )
            with (
                patch.object(
                    engine.rate_tracker, "apply_rate_limit", return_value=0.0
                ),
                patch(
                    "local_deep_research.web_search_engines.search_engine_base.logger",
                    new=MagicMock(),
                ) as base_logger,
            ):
                assert engine.run("test query") == []

        warnings = _messages(base_logger, "warning")
        assert any(
            "OpenAlexSearchEngine preview request failed" in message
            and "denied access" in message
            for message in warnings
        ), warnings

    def test_get_previews_error_body_is_sanitized(self):
        """An echoed key never reaches the log sink, at any offset.

        The body is longer than the 200-character log cap and the key is
        placed so that it straddles that boundary, with no ``Bearer``/
        ``Authorization:`` prefix in front of it. Truncating before
        scrubbing (``self._scrub_error(response.text[:200])``) leaves a
        key *prefix* that neither scrub pass can match: the literal pass
        looks for the whole key, and the shape regexes are anchored on a
        credential prefix that is not there. Scrubbing first and cutting
        afterwards is the only order that holds.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        api_key = "oa9fK3mQz7XrVnT4sJhW6yCg1EuA"
        # Land the key across index 200 with no credential prefix.
        prefix = "upstream echoed the request: " + "-" * (
            200 - len("upstream echoed the request: ") - (len(api_key) // 2)
        )
        body = prefix + api_key + "." * 120
        assert len(body) > 200
        # The key genuinely straddles the cut.
        assert len(prefix) < 200 < len(prefix) + len(api_key)
        assert "Bearer" not in body

        broken = Mock()
        broken.status_code = 500
        broken.raise_for_status.side_effect = requests.HTTPError(
            response=broken
        )
        broken.headers = {}
        broken.text = body

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=broken,
        ):
            engine = OpenAlexSearchEngine(api_key=api_key)
            with patch(
                "local_deep_research.web_search_engines.engines.search_engine_openalex.logger",
                new=MagicMock(),
            ) as engine_logger:
                assert engine._search_previews("test query") == []

        logged = _messages(engine_logger, "error")
        assert logged, "the 500 branch must log an error"
        joined = " ".join(logged)
        assert api_key not in joined
        # No *fragment* of the key survives either: check every substring
        # of length 8 or more that starts at the key's start, which is
        # exactly what a truncating-first implementation leaks.
        for cut in range(8, len(api_key) + 1):
            assert api_key[:cut] not in joined, api_key[:cut]

    def test_get_previews_rate_limit_error(self):
        """Get previews raises RateLimitError on 429."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )
        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.headers = {}
        mock_response.text = "Rate limit exceeded"

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=mock_response,
        ):
            engine = OpenAlexSearchEngine()

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

    def test_get_previews_empty_results(self):
        """Get previews handles empty results."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        mock_response = Mock()
        mock_response.status_code = 200
        mock_response.headers = {}
        mock_response.json.return_value = {"meta": {"count": 0}, "results": []}

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=mock_response,
        ):
            engine = OpenAlexSearchEngine()
            previews = engine._search_previews("test query")

            assert previews == []

    def test_get_previews_api_error(self):
        """Get previews handles API errors gracefully."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        mock_response = Mock()
        mock_response.status_code = 500
        mock_response.raise_for_status.side_effect = requests.HTTPError(
            response=mock_response
        )
        mock_response.headers = {}
        mock_response.text = "Internal server error"

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=mock_response,
        ):
            engine = OpenAlexSearchEngine()
            previews = engine._search_previews("test query")

            assert previews == []

    def test_get_previews_exception(self):
        """Get previews handles exceptions gracefully."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            side_effect=Exception("Connection error"),
        ):
            engine = OpenAlexSearchEngine()
            previews = engine._search_previews("test query")

            assert previews == []


class TestFormatWorkPreview:
    """Tests for _format_work_preview method."""

    def test_format_work_preview_full_data(self):
        """Format work preview with full data."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        work = {
            "id": "https://openalex.org/W123",
            "display_name": "Test Paper Title",
            "publication_year": 2023,
            "publication_date": "2023-05-15",
            "doi": "https://doi.org/10.1234/test",
            "cited_by_count": 100,
            "authorships": [
                {"author": {"display_name": "Author One"}},
                {"author": {"display_name": "Author Two"}},
            ],
            "primary_location": {"source": {"display_name": "Test Journal"}},
            "open_access": {"is_oa": True},
            "best_oa_location": {"pdf_url": "https://example.com/paper.pdf"},
            "abstract_inverted_index": {
                "This": [0],
                "is": [1],
                "abstract": [2],
            },
        }

        preview = engine._format_work_preview(work)

        assert preview["id"] == "https://openalex.org/W123"
        assert preview["title"] == "Test Paper Title"
        assert preview["year"] == 2023
        assert preview["citations"] == 100
        assert preview["journal"] == "Test Journal"
        assert preview["is_open_access"] is True
        assert "Author One" in preview["authors"]
        assert "Author Two" in preview["authors"]

    def test_format_work_preview_with_doi(self):
        """Format work preview converts DOI to URL."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        work = {
            "id": "https://openalex.org/W123",
            "display_name": "Test",
            "doi": "10.1234/test",
        }

        preview = engine._format_work_preview(work)

        assert preview["link"] == "https://doi.org/10.1234/test"

    def test_format_work_preview_many_authors(self):
        """Format work preview truncates many authors."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        work = {
            "id": "https://openalex.org/W123",
            "display_name": "Test",
            "authorships": [
                {"author": {"display_name": f"Author {i}"}} for i in range(10)
            ],
        }

        preview = engine._format_work_preview(work)

        assert "et al." in preview["authors"]

    def test_format_work_preview_missing_data(self):
        """Format work preview handles missing data."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        work = {"id": "https://openalex.org/W123"}

        preview = engine._format_work_preview(work)

        assert preview["title"] == "No title"
        assert preview["year"] == "unknown"
        assert preview["journal"] is None


class TestReconstructAbstract:
    """Tests for _reconstruct_abstract method."""

    def test_reconstruct_abstract_basic(self):
        """Reconstruct abstract from inverted index."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        inverted_index = {
            "This": [0],
            "is": [1],
            "a": [2],
            "test": [3],
            "abstract": [4],
        }

        abstract = engine._reconstruct_abstract(inverted_index)

        assert abstract == "This is a test abstract"

    def test_reconstruct_abstract_with_repeated_words(self):
        """Reconstruct abstract with repeated words."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        inverted_index = {
            "The": [0, 4],
            "cat": [1],
            "sat": [2],
            "on": [3],
            "mat": [5],
        }

        abstract = engine._reconstruct_abstract(inverted_index)

        assert abstract == "The cat sat on The mat"

    def test_reconstruct_abstract_empty(self):
        """Reconstruct abstract handles empty index."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        abstract = engine._reconstruct_abstract({})

        assert abstract == ""


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

    def test_get_full_content_returns_items(self):
        """Get full content returns formatted items."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

        items = [
            {
                "title": "Test Paper",
                "link": "https://example.com",
                "snippet": "Test snippet",
                "abstract": "Full abstract text",
                "authors": "John Doe",
                "year": 2023,
                "journal": "Nature",
                "citations": 100,
                "is_open_access": True,
                "oa_url": "https://example.com/oa",
            }
        ]

        results = engine._get_full_content(items)

        assert len(results) == 1
        assert results[0]["title"] == "Test Paper"
        assert results[0]["content"] == "Full abstract text"
        assert results[0]["metadata"]["authors"] == "John Doe"
        assert results[0]["metadata"]["citations"] == 100

    def test_get_full_content_uses_snippet_if_no_abstract(self):
        """Get full content uses snippet if no abstract."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine()

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

        results = engine._get_full_content(items)

        assert results[0]["content"] == "Test snippet"


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

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

        assert OpenAlexSearchEngine.is_public is True

    def test_is_scientific(self):
        """OpenAlexSearchEngine is marked as scientific."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        assert OpenAlexSearchEngine.is_scientific is True


class TestRejectedKeyReachesEnrichment:
    """The refused key must not come back out of the settings snapshot.

    ``__init__`` reads ``search.engine.web.openalex.api_key`` from the
    settings snapshot, and so does
    ``BaseSearchEngine._resolve_openalex_enrichment_key``. Clearing the
    engine's own attributes therefore is not enough: without a latch, the
    DOI enrichment pass of the *same* ``run()`` resolves the identical key
    straight back out of the snapshot and sends it again.
    """

    SNAPSHOT = {"search.engine.web.openalex.api_key": "oa-live-abc123"}

    @staticmethod
    def _work_response(count=1):
        response = Mock()
        response.status_code = 200
        response.headers = {}
        response.json.return_value = {
            "meta": {"count": count},
            "results": [
                {
                    "id": "https://openalex.org/W1",
                    "display_name": "Paper",
                    "doi": "https://doi.org/10.1234/example",
                }
            ],
        }
        return response

    def test_resolver_returns_none_after_the_engine_dropped_the_key(self):
        """(a) The snapshot still carries the key; the resolver must not."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(
            programmatic_mode=True, settings_snapshot=dict(self.SNAPSHOT)
        )
        assert engine.api_key == "oa-live-abc123"
        # Sanity: without the drop the resolver hands the key over.
        assert engine._resolve_openalex_enrichment_key() == "oa-live-abc123"

        engine._drop_rejected_api_key()

        assert (
            engine.settings_snapshot["search.engine.web.openalex.api_key"]
            == "oa-live-abc123"
        ), "the snapshot is untouched — that is the point"
        assert engine._resolve_openalex_enrichment_key() is None
        # And the literal is still redactable after the drop.
        assert "oa-live-abc123" not in engine._scrub_error(
            RuntimeError("boom oa-live-abc123")
        )

    def test_run_never_sends_the_refused_key_to_enrichment(self):
        """(b) One run(): search 401 → keyless 200 → keyless enrichment.

        Asserted on the sequence of ``Authorization`` values actually put
        on the wire across both call sites, which is the property the
        docstrings claim.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        authorizations = []

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "invalid api key"

        def _record(**kwargs):
            authorizations.append(kwargs["headers"].get("Authorization"))

        def _engine_get(*args, **kwargs):
            _record(**kwargs)
            if kwargs["headers"].get("Authorization"):
                return rejected
            return self._work_response()

        enrichment_ok = MagicMock()
        enrichment_ok.status_code = 200
        enrichment_ok.json.return_value = {"results": []}

        def _enrichment_get(*args, **kwargs):
            _record(**kwargs)
            return enrichment_ok

        engine = OpenAlexSearchEngine(
            programmatic_mode=True,
            search_snippets_only=True,
            settings_snapshot=dict(self.SNAPSHOT),
        )
        engine._preview_filters = []

        with (
            patch(
                "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
                side_effect=_engine_get,
            ) as engine_get,
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                side_effect=_enrichment_get,
            ) as enrichment_get,
            patch.object(
                engine.rate_tracker, "apply_rate_limit", return_value=0.0
            ),
        ):
            results = engine.run("test query")

        assert results, "the keyless retry must still return the results"
        assert engine_get.call_count == 2
        assert enrichment_get.call_count == 1, (
            "enrichment must go out once, keyless — not keyed, refused and "
            "retried"
        )
        assert authorizations == ["Bearer oa-live-abc123", None, None]

    def test_second_run_enriches_keylessly_from_the_first_request(self):
        """(c) The latch outlives the run that set it."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        authorizations = []

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "invalid api key"

        def _engine_get(*args, **kwargs):
            authorizations.append(
                ("search", kwargs["headers"].get("Authorization"))
            )
            if kwargs["headers"].get("Authorization"):
                return rejected
            return self._work_response()

        enrichment_ok = MagicMock()
        enrichment_ok.status_code = 200
        enrichment_ok.json.return_value = {"results": []}

        def _enrichment_get(*args, **kwargs):
            authorizations.append(
                ("enrich", kwargs["headers"].get("Authorization"))
            )
            return enrichment_ok

        engine = OpenAlexSearchEngine(
            programmatic_mode=True,
            search_snippets_only=True,
            settings_snapshot=dict(self.SNAPSHOT),
        )
        engine._preview_filters = []

        with (
            patch(
                "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
                side_effect=_engine_get,
            ),
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                side_effect=_enrichment_get,
            ),
            patch.object(
                engine.rate_tracker, "apply_rate_limit", return_value=0.0
            ),
        ):
            engine.run("first query")
            engine.run("second query")

        assert authorizations == [
            ("search", "Bearer oa-live-abc123"),
            ("search", None),
            ("enrich", None),
            ("search", None),
            ("enrich", None),
        ]

    def test_enrichment_rejection_latches_on_the_engine(self):
        """The reverse direction: enrichment refuses, search stays keyless.

        The engine's own search key is unset here (keyless install with an
        enrichment key in the snapshot), so the only rejection comes from
        the enrichment pass. Its drop is call-local, so without the
        callback the next run resolves the same key again.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(
            programmatic_mode=True, settings_snapshot=dict(self.SNAPSHOT)
        )
        # Simulate a keyless *search* path with a snapshot key still live
        # for enrichment: the resolver reads the snapshot, not self.api_key.
        engine.api_key = None
        engine.openalex_api_key = None
        assert engine._resolve_openalex_enrichment_key() == "oa-live-abc123"

        engine._note_openalex_key_rejected("oa-live-abc123")

        assert engine._resolve_openalex_enrichment_key() is None
        assert "oa-live-abc123" not in engine._scrub_error(
            RuntimeError("boom oa-live-abc123")
        )

    def test_a_second_drop_keeps_the_literal_in_the_redaction_set(self):
        """Idempotency: the second call must not store None over the key."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
        engine._drop_rejected_api_key()
        engine._drop_rejected_api_key()  # self.api_key is now None

        assert engine._rejected_api_key == "oa-live-abc123"
        assert "oa-live-abc123" not in engine._scrub_error(
            RuntimeError("boom oa-live-abc123")
        )

    def test_the_key_is_dropped_before_the_keyless_retry_goes_out(self):
        """Ordering, observed from inside the retry itself.

        If ``on_key_rejected`` moved after ``send_request(False)``, every
        response-list-driven test would still pass; this one would not.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "invalid api key"

        observed = {}

        def _get(*args, **kwargs):
            if kwargs["headers"].get("Authorization"):
                return rejected
            observed["api_key"] = engine.api_key
            observed["header"] = "Authorization" in engine.headers
            observed["latched"] = engine._openalex_key_rejected
            return self._work_response()

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            side_effect=_get,
        ):
            engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
            engine._search_previews("test query")

        assert observed == {
            "api_key": None,
            "header": False,
            "latched": True,
        }, observed

    def test_key_was_sent_is_read_once_not_twice(self):
        """One read of shared state, used for both decisions.

        ``self.api_key`` is mutable and another thread on the same engine
        can drop it. Reading it separately for the retry decision and for
        the ``OpenAlexAuthError`` gate lets the two disagree: the request
        goes out keyless (so there is no key to blame and no keyless
        retry to make) while the gate still reports an authentication
        failure. Simulated here by a key that is gone after the first
        read.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexAuthError,
            OpenAlexSearchEngine,
        )

        rejected = Mock()
        rejected.status_code = 401
        rejected.raise_for_status.side_effect = requests.HTTPError(
            response=rejected
        )
        rejected.headers = {}
        rejected.text = "invalid api key"

        engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
        reads = []

        class RacingEngine(type(engine)):
            """The key is dropped by "another thread" after one read."""

            @property
            def api_key(self):
                reads.append(len(reads))
                return "oa-live-abc123" if len(reads) == 1 else None

            @api_key.setter
            def api_key(self, value):
                pass

        del engine.__dict__["api_key"]
        engine.__class__ = RacingEngine

        with (
            patch(
                "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
                return_value=rejected,
            ) as mock_get,
            patch.object(
                engine.rate_tracker, "apply_rate_limit", return_value=0.0
            ),
            pytest.raises(OpenAlexAuthError),
        ):
            engine._get_previews("test query")

        # Two requests: the keyed one and its keyless retry. A second read
        # would hand the helper ``None``, skipping the retry entirely while
        # still raising OpenAlexAuthError for a request that carried no key.
        assert mock_get.call_count == 2


class TestEnrichmentLearnedRejectionDropsTheEnginesOwnKey:
    """The latch alone is not enough when *enrichment* is the refused path.

    ``BaseSearchEngine._note_openalex_key_rejected`` only latches, which
    is all a non-OpenAlex scientific engine needs. This engine also holds
    the OpenAlex credential in ``api_key``/``openalex_api_key`` and in the
    cached ``Authorization`` header, so when the DOI enrichment pass is
    the path that learns the key is bad — a search that returned 200
    followed by an enrichment 401, i.e. a key revoked mid-run — the next
    *search* on the same instance would otherwise re-send it.
    """

    SNAPSHOT = {"search.engine.web.openalex.api_key": "oa-live-abc123"}

    @staticmethod
    def _work_response():
        response = Mock()
        response.status_code = 200
        response.headers = {}
        response.json.return_value = {
            "meta": {"count": 1},
            "results": [
                {
                    "id": "https://openalex.org/W1",
                    "display_name": "Paper",
                    "doi": "https://doi.org/10.1234/example",
                }
            ],
        }
        return response

    def test_enrichment_rejection_stops_the_next_search_sending_the_key(self):
        """search 200 → enrichment 401 → run #2's search carries no key."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        authorizations = []

        def _engine_get(*args, **kwargs):
            authorizations.append(
                ("search", kwargs["headers"].get("Authorization"))
            )
            # The search endpoint is happy throughout: only enrichment
            # ever sees the rejection.
            return self._work_response()

        enrichment_rejected = MagicMock()
        enrichment_rejected.status_code = 401
        enrichment_rejected.raise_for_status.side_effect = requests.HTTPError(
            response=enrichment_rejected
        )
        enrichment_rejected.json.return_value = {}
        enrichment_ok = MagicMock()
        enrichment_ok.status_code = 200
        enrichment_ok.json.return_value = {"results": []}

        def _enrichment_get(*args, **kwargs):
            authorization = kwargs["headers"].get("Authorization")
            authorizations.append(("enrich", authorization))
            return enrichment_rejected if authorization else enrichment_ok

        engine = OpenAlexSearchEngine(
            programmatic_mode=True,
            search_snippets_only=True,
            settings_snapshot=dict(self.SNAPSHOT),
        )
        engine._preview_filters = []
        assert engine.headers["Authorization"] == "Bearer oa-live-abc123"

        with (
            patch(
                "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
                side_effect=_engine_get,
            ),
            patch(
                "local_deep_research.utilities.openalex_enrichment.safe_get",
                side_effect=_enrichment_get,
            ),
            patch.object(
                engine.rate_tracker, "apply_rate_limit", return_value=0.0
            ),
        ):
            engine.run("first query")
            engine.run("second query")

        assert authorizations == [
            ("search", "Bearer oa-live-abc123"),
            ("enrich", "Bearer oa-live-abc123"),
            ("enrich", None),
            # Without the engine's own drop this one is
            # "Bearer oa-live-abc123" again.
            ("search", None),
            ("enrich", None),
        ]
        # The credential is gone from every slot the engine sends from…
        assert engine.api_key is None
        assert engine.openalex_api_key is None
        assert "Authorization" not in engine.headers
        # …and still redactable.
        assert engine._rejected_api_key == "oa-live-abc123"
        assert "oa-live-abc123" not in engine._scrub_error(
            RuntimeError("boom oa-live-abc123")
        )

    def test_the_engine_drop_is_idempotent_and_keeps_the_literal(self):
        """Two rejections (search then enrichment, or a thread race)."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
        engine._note_openalex_key_rejected("oa-live-abc123")
        # Second call: ``self.api_key`` is already None and the header is
        # already gone, so nothing may raise and nothing may be lost.
        engine._note_openalex_key_rejected(None)

        assert engine.api_key is None
        assert engine.openalex_api_key is None
        assert "Authorization" not in engine.headers
        assert engine._openalex_key_rejected is True
        assert engine._rejected_api_key == "oa-live-abc123"

    def test_a_bare_call_still_drops_the_engines_live_key(self):
        """``on_key_rejected`` may be invoked with no argument at all.

        ``BaseSearchEngine.run`` hands this method to enrichment, which
        calls it with the refused key; other callers may call it bare.
        Either way the engine's own credential has to go.
        """
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        engine = OpenAlexSearchEngine(api_key="oa-live-abc123")
        engine._note_openalex_key_rejected()

        assert engine.api_key is None
        assert "Authorization" not in engine.headers
        # The live key was picked up as the literal to keep redacting.
        assert engine._rejected_api_key == "oa-live-abc123"


class TestBoundedErrorBody:
    """The pre-scrub bound must not create a partial credential.

    ``_get_previews`` bounds the regex work before scrubbing an upstream
    error body. Cutting at a fixed offset is unsafe *at any offset*,
    because scrubbing shrinks the text — a token-shaped run collapses to
    ``Bearer [REDACTED]`` (17 characters) — which pulls content from far
    beyond the 200-character log cap into the characters that get logged,
    including a key fragment the cut itself created.
    """

    API_KEY = "oa9fK3mQz7XrVnT4sJhW6yCg1EuA"

    @staticmethod
    def _logged_errors(body, api_key):
        """Drive the 500 branch with *body*; return the logged error lines."""
        from local_deep_research.web_search_engines.engines.search_engine_openalex import (
            OpenAlexSearchEngine,
        )

        broken = Mock()
        broken.status_code = 500
        broken.raise_for_status.side_effect = requests.HTTPError(
            response=broken
        )
        broken.headers = {}
        broken.text = body

        with patch(
            "local_deep_research.web_search_engines.engines.search_engine_openalex.safe_get",
            return_value=broken,
        ):
            engine = OpenAlexSearchEngine(api_key=api_key)
            with (
                patch.object(
                    engine.rate_tracker, "apply_rate_limit", return_value=0.0
                ),
                patch(
                    "local_deep_research.web_search_engines.engines.search_engine_openalex.logger",
                    new=MagicMock(),
                ) as engine_logger,
            ):
                assert engine._search_previews("test query") == []
        return _messages(engine_logger, "error")

    @staticmethod
    def _key_fragments(key, text, length=4):
        """Every contiguous *length*-run of *key* that appears in *text*."""
        return sorted(
            {
                key[index : index + length]
                for index in range(len(key) - length + 1)
                if key[index : index + length] in text
            }
        )

    @pytest.mark.parametrize("key_offset", [8172, 8178, 8184])
    def test_a_key_straddling_the_pre_scrub_cut_never_reaches_the_log(
        self, key_offset
    ):
        """The shape a fixed ``response.text[:8192]`` leaks.

        A collapsing ``Bearer`` run at the front of the body shrinks to 17
        characters under the scrub, so the material around the 8192-byte
        boundary lands inside the 200-character log cap. With the key
        straddling that boundary, a fixed cut hands the log a key
        *prefix*: too short for the literal pass to match and with no
        credential prefix for the shape regexes to anchor on.
        """
        prefix_gap = 171
        collapsing = key_offset - len("Bearer ") - prefix_gap
        body = (
            "Bearer "
            + "A" * collapsing
            + "!" * prefix_gap
            + self.API_KEY
            + "." * 400
        )
        # The key genuinely straddles 8192 — that is the whole point.
        assert len(body) > 8192
        start = body.index(self.API_KEY)
        assert start < 8192 < start + len(self.API_KEY)

        joined = " ".join(self._logged_errors(body, self.API_KEY))
        assert joined, "the 500 branch must log an error"
        assert self.API_KEY not in joined
        assert self._key_fragments(self.API_KEY, joined) == []

    def test_a_body_with_no_whitespace_at_all_yields_an_empty_detail(self):
        """Nothing in the window can be shown to be credential-free.

        The whole 8192-character prefix is one unbroken run, so it is
        dropped entirely rather than cut mid-token. The status code still
        reaches the log — the operator does not lose the diagnosis.
        """
        body = "x" * 9000
        assert not any(character.isspace() for character in body)

        logged = self._logged_errors(body, self.API_KEY)
        assert logged, "the 500 branch must log an error"
        assert any(
            message.rstrip() == "OpenAlex API error: 500 -"
            for message in logged
        ), logged

    def test_a_key_fully_inside_the_window_is_still_redacted(self):
        """The cut must not become "drop everything" either."""
        marker = "upstream echoed the request:"
        body = f"{marker} {self.API_KEY} " + "." * 9000
        assert len(body) > 8192
        assert body.index(self.API_KEY) + len(self.API_KEY) < 8192

        joined = " ".join(self._logged_errors(body, self.API_KEY))
        assert marker in joined, "the surviving prefix must still be logged"
        assert self.API_KEY not in joined
        assert self._key_fragments(self.API_KEY, joined) == []

    def test_no_key_position_survives_the_bounded_scrub_pipeline(self):
        """200 random placements of the key around the boundary.

        Each body is mixed whitespace and token characters behind a
        collapsing ``Bearer`` run, so the surviving 200 characters are
        drawn from the region around the cut rather than from the start of
        the body. Under a fixed ``response.text[:8192]`` a large minority
        of these placements leak.
        """
        import random

        from local_deep_research.security.log_sanitizer import scrub_error
        from local_deep_research.utilities.openalex_enrichment import (
            bounded_error_body,
        )

        alphabet = (
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
            "0123456789-._~+/:{}\"',=?&"
        )
        whitespace = " \t\n\r\f\v"

        def _mixed(length, rng):
            return "".join(
                rng.choice(whitespace)
                if rng.random() < 0.08
                else rng.choice(alphabet)
                for _ in range(length)
            )

        for trial in range(200):
            rng = random.Random(6289 + trial)
            key_offset = rng.randrange(8100, 8230)
            gap = rng.randrange(80, 190)
            collapsing = key_offset - gap - len("Bearer ")
            body = (
                "Bearer "
                + "A" * collapsing
                + _mixed(gap, rng)
                + self.API_KEY
                + _mixed(12000, rng)
            )
            assert len(body) > 8192
            detail = scrub_error(bounded_error_body(body), self.API_KEY)[:200]
            assert self._key_fragments(self.API_KEY, detail) == [], (
                f"trial {trial}, key at {key_offset}: {detail!r}"
            )