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.

16.6 KB
Raw Blame
"""Contract tests for the private policy arXiv client context manager.

Issue #4783: every ``export.arxiv.org`` API request must traverse one
process-wide monotonic 3-second single-flight gate and ``SafeSession``.

The utility ``local_deep_research.utilities.arxiv_api`` is the sole policy
owner and keeps ``_policy_arxiv_client(*, page_size: int | None = None)``
that constructs ``arxiv.Client`` through the ``arxiv`` module attribute
with ``delay_seconds=0``, closes the discarded default session, pins a
gated ``SafeSession`` whose wire call forces ``timeout=10`` and
``allow_redirects=False``, yields the client, and closes the adapted
session in ``finally``. Session installation goes through
``_install_gated_session``, which raises ``ImportError`` unless the
client exposes a real ``requests.Session`` at its private ``_session``
attribute, so an ``arxiv`` release changing that internals shape fails
loudly instead of silently bypassing the gate. Deterministic seams
mirror the downloader gate naming so existing test patches retarget
cleanly: ``_arxiv_api_request_lock``, ``_last_request_started_at``,
``_monotonic``, ``_sleep``.
"""

from collections.abc import Iterator
from dataclasses import dataclass
from types import SimpleNamespace
from typing import Protocol
from unittest.mock import MagicMock

import arxiv
import pytest
import requests
from pytest_mock import MockerFixture

from local_deep_research.security import SafeSession
from local_deep_research.utilities import arxiv_api as arxiv_api_module


class ResultsIterationError(RuntimeError):
    """Typed error raised when iterating arxiv.Client results fails.

    Subclass of ``RuntimeError`` so the existing
    ``pytest.raises(RuntimeError)`` assertion keeps catching it; the
    narrow type lets the audit rule for typed exceptions pass.
    """


@dataclass(frozen=True, slots=True)
class StubClientConstruction:
    """Recorded arxiv.Client construction arguments."""

    page_size: int
    delay_seconds: float
    num_retries: int


class StubArxivClient(Protocol):
    """Structural shape of the recording arxiv.Client double."""

    page_size: int
    delay_seconds: float
    num_retries: int


@dataclass(frozen=True, slots=True)
class StubbedClientFactory:
    """Recording doubles installed in place of ``arxiv.Client``."""

    constructed: list[StubClientConstruction]
    instances: list[StubArxivClient]
    default_sessions: list[requests.Session]


@pytest.fixture(autouse=True)
def fresh_arxiv_api_gate() -> Iterator[None]:
    """Reset the process-wide gate state around each test."""
    with arxiv_api_module._arxiv_api_request_lock:
        arxiv_api_module._last_request_started_at = None
    yield
    with arxiv_api_module._arxiv_api_request_lock:
        arxiv_api_module._last_request_started_at = None


@pytest.fixture
def stub_arxiv_client(mocker: MockerFixture) -> Iterator[StubbedClientFactory]:
    """Replace the ``arxiv.Client`` attribute with a recording double."""
    constructed: list[StubClientConstruction] = []
    instances: list[StubArxivClient] = []
    default_sessions: list[MagicMock] = []

    class StubClient:
        def __init__(
            self,
            page_size: int = 100,
            delay_seconds: float = 3.0,
            num_retries: int = 3,
        ) -> None:
            self.page_size: int = page_size
            self.delay_seconds: float = delay_seconds
            self.num_retries: int = num_retries
            self._last_request_dt: float | None = None
            # The policy adapter guards on _session being a real
            # requests.Session (see _install_gated_session), so the
            # double must pin one; its close is spied so tests can
            # still assert the discarded session was closed.
            default_session = requests.Session()
            default_session.close = MagicMock(wraps=default_session.close)
            self._session: requests.Session = default_session
            constructed.append(
                StubClientConstruction(
                    page_size=page_size,
                    delay_seconds=delay_seconds,
                    num_retries=num_retries,
                )
            )
            default_sessions.append(default_session)
            instances.append(self)

    mocker.patch.object(arxiv, "Client", StubClient)
    yield StubbedClientFactory(
        constructed=constructed,
        instances=instances,
        default_sessions=default_sessions,
    )


class TestPolicyArxivClient:
    """Tests for the private policy client context manager."""

    def test_client_constructed_with_zero_delay(
        self, stub_arxiv_client: StubbedClientFactory
    ) -> None:
        # Given the arxiv.Client attribute replaced by a recording double

        # When a policy client is opened
        with arxiv_api_module._policy_arxiv_client() as client:
            # Then construction went through arxiv.Client with zero delay
            assert len(stub_arxiv_client.constructed) == 1
            assert stub_arxiv_client.constructed[0].delay_seconds == 0
            assert client is stub_arxiv_client.instances[0]
            assert client.delay_seconds == 0

    def test_page_size_is_passed_through(
        self, stub_arxiv_client: StubbedClientFactory
    ) -> None:
        # Given a caller requesting a specific page size

        # When a policy client is opened with that page size
        with arxiv_api_module._policy_arxiv_client(page_size=25) as client:
            assert client.page_size == 25

        # Then construction received the page size with zero delay
        assert stub_arxiv_client.constructed[0].page_size == 25
        assert stub_arxiv_client.constructed[0].delay_seconds == 0

    def test_adapter_pins_safe_session(
        self, stub_arxiv_client: StubbedClientFactory
    ) -> None:
        # Given a policy-adapted client

        # When the adapter installs the request session

        # Then the pinned session is a SafeSession
        with arxiv_api_module._policy_arxiv_client() as client:
            assert isinstance(client._session, SafeSession)

    def test_discarded_default_session_is_closed(
        self, stub_arxiv_client: StubbedClientFactory
    ) -> None:
        # Given the default session recorded when the client is constructed

        # When the policy scope runs to completion
        with arxiv_api_module._policy_arxiv_client():
            pass

        # Then the discarded default session is closed exactly once
        assert len(stub_arxiv_client.default_sessions) == 1
        discarded = stub_arxiv_client.default_sessions[0]
        discarded.close.assert_called_once()

    def test_adapted_session_closed_on_success(
        self,
        mocker: MockerFixture,
        stub_arxiv_client: StubbedClientFactory,
    ) -> None:
        # Given a policy-adapted session whose cleanup is observed
        adapted_close: MagicMock | None = None
        with arxiv_api_module._policy_arxiv_client() as client:
            adapted = client._session
            adapted_close = mocker.patch.object(adapted, "close")

        # Then the adapted session is closed exactly once on normal exit
        assert adapted_close is not None
        adapted_close.assert_called_once()

    def test_adapted_session_closed_on_exception(
        self,
        mocker: MockerFixture,
        stub_arxiv_client: StubbedClientFactory,
    ) -> None:
        # Given a policy-adapted scope whose body raises
        adapted_close: MagicMock | None = None
        with pytest.raises(ResultsIterationError):
            with arxiv_api_module._policy_arxiv_client() as client:
                adapted = client._session
                adapted_close = mocker.patch.object(adapted, "close")
                raise ResultsIterationError("results iteration failed")

        # Then the adapted session is still closed exactly once
        assert adapted_close is not None
        adapted_close.assert_called_once()

    def test_adapted_close_failure_preserves_iteration_exception(
        self,
        mocker: MockerFixture,
        stub_arxiv_client: StubbedClientFactory,
    ) -> None:
        # Given an iteration failure followed by a failing session close
        iteration_error = ResultsIterationError("results iteration failed")
        adapted_close: MagicMock | None = None

        # When the policy scope unwinds
        with pytest.raises(ResultsIterationError) as raised:
            with arxiv_api_module._policy_arxiv_client() as client:
                adapted_close = mocker.patch.object(
                    client._session,
                    "close",
                    side_effect=ResultsIterationError("session close failed"),
                )
                raise iteration_error

        # Then cleanup runs once without replacing the iteration failure
        assert raised.value is iteration_error
        assert adapted_close is not None
        adapted_close.assert_called_once()

    def test_wire_call_forces_timeout_and_disables_redirects(
        self,
        mocker: MockerFixture,
        stub_arxiv_client: StubbedClientFactory,
    ) -> None:
        # Given the SafeSession request funnel observed and a frozen clock
        request = mocker.patch.object(SafeSession, "request")
        mocker.patch.object(arxiv_api_module, "_monotonic", return_value=10.0)
        mocker.patch.object(arxiv_api_module, "_sleep")

        # When one API query travels the adapted session
        with arxiv_api_module._policy_arxiv_client() as client:
            client._session.get(
                "https://export.arxiv.org/api/query",
                params={"id_list": "2301.12345"},
            )

        # Then the wire call forces the policy request arguments
        request.assert_called_once()
        call = request.call_args
        assert call is not None
        assert call.kwargs.get("timeout") == 10
        assert call.kwargs.get("allow_redirects") is False
        assert call.kwargs.get("params") == {"id_list": "2301.12345"}
        assert "https://export.arxiv.org/api/query" in str(call)

    def test_wire_calls_share_the_process_wide_gate(
        self,
        mocker: MockerFixture,
        stub_arxiv_client: StubbedClientFactory,
    ) -> None:
        # Given a clock advanced only by policy sleep
        now = 100.0
        starts: list[float] = []
        sleeps: list[float] = []

        def monotonic() -> float:
            return now

        def sleep(duration: float) -> None:
            nonlocal now
            sleeps.append(duration)
            now += duration

        def request(
            _method: str,
            _url: str,
            *,
            params: dict[str, str] | None = None,
            timeout: int | float | None = None,
            allow_redirects: bool = False,
        ) -> None:
            starts.append(now)

        mocker.patch.object(
            arxiv_api_module, "_monotonic", side_effect=monotonic
        )
        mocker.patch.object(arxiv_api_module, "_sleep", side_effect=sleep)
        mocker.patch.object(SafeSession, "request", side_effect=request)

        # When two queries travel one adapted client back to back
        with arxiv_api_module._policy_arxiv_client() as client:
            client._session.get("https://export.arxiv.org/api/query")
            client._session.get("https://export.arxiv.org/api/query")

        # Then their wire starts are spaced by the shared gate
        assert starts == [100.0, 103.0]
        assert sleeps == [3.0]

    def test_adapted_client_requests_use_shared_gate(
        self,
        mocker: MockerFixture,
        stub_arxiv_client: StubbedClientFactory,
    ) -> None:
        # Given the module gate replaced and the SafeSession funnel stubbed
        gate = mocker.patch.object(arxiv_api_module, "arxiv_api_request_gate")
        request = mocker.patch.object(SafeSession, "request")

        # When one request travels the adapted client session
        with arxiv_api_module._policy_arxiv_client() as client:
            client._session.get(
                "https://export.arxiv.org/api/query",
                headers={"User-Agent": "LDR-test"},
            )

        # Then the wire call traversed the shared gate and the
        # underlying SafeSession call carried the policy arguments
        gate.assert_called_once_with()
        request.assert_called_once()
        call = request.call_args
        assert call is not None
        assert call.kwargs.get("timeout") == 10
        assert call.kwargs.get("headers") == {"User-Agent": "LDR-test"}


class TestInstallGatedSession:
    """Guard tests for the private ``client._session`` mutation."""

    def test_raises_import_error_when_session_attribute_missing(self) -> None:
        # Given a client double without the private _session attribute
        stub = SimpleNamespace()

        # When the gated session would be installed
        with pytest.raises(ImportError, match="rate-limit gate"):
            arxiv_api_module._install_gated_session(stub)

    def test_raises_import_error_for_non_requests_session(self) -> None:
        # Given a client whose _session is not a requests.Session
        stub = SimpleNamespace(_session=object())

        # When the gated session would be installed
        with pytest.raises(ImportError, match="requests.Session at _session"):
            arxiv_api_module._install_gated_session(stub)

    def test_non_session_attribute_is_not_closed(self) -> None:
        # Given an unusable _session that records close calls
        not_a_session = MagicMock(name="not_a_session")
        stub = SimpleNamespace(_session=not_a_session)

        # When installation is refused
        with pytest.raises(ImportError):
            arxiv_api_module._install_gated_session(stub)

        # Then the rejected value was never closed
        not_a_session.close.assert_not_called()


_ATOM_FEED = b"""<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <link href="http://arxiv.org/api/query?search_query%3Did_list:2101.12345" rel="self" type="application/atom+xml"/>
  <title type="html">ArXiv Query: search_query=id_list:2101.12345</title>
  <id>http://arxiv.org/api/query</id>
  <updated>2021-01-01T00:00:00-00:00</updated>
  <opensearch:totalResults xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">1</opensearch:totalResults>
  <opensearch:startIndex xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">0</opensearch:startIndex>
  <opensearch:itemsPerPage xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">1</opensearch:itemsPerPage>
  <entry>
    <id>http://arxiv.org/abs/2101.12345v1</id>
    <updated>2021-01-02T00:00:00-00:00</updated>
    <published>2021-01-01T00:00:00-00:00</published>
    <title>Paper Title Here</title>
    <summary>The summary text.</summary>
    <author><name>A. Author</name></author>
    <link title="pdf" href="http://arxiv.org/pdf/2101.12345v1" rel="related" type="application/pdf"/>
    <arxiv:primary_category xmlns:arxiv="http://arxiv.org/schemas/atom" term="cs.AI"/>
    <category term="cs.AI"/>
  </entry>
</feed>"""


def _atom_response() -> requests.Response:
    """A 200 Atom-feed response the real arxiv client can parse."""
    response = requests.Response()
    response.status_code = 200
    response._content = _ATOM_FEED
    response.headers["content-type"] = "application/atom+xml; charset=utf-8"
    response.url = "https://export.arxiv.org/api/query"
    return response


class TestRealClientGating:
    """A real ``arxiv.Client`` (no stubs) receives the gated session."""

    def test_gated_session_installed_on_real_client(self) -> None:
        with arxiv_api_module._policy_arxiv_client() as client:
            assert isinstance(
                client._session, arxiv_api_module._GatedArxivSession
            )
            assert isinstance(client._session, SafeSession)

    def test_real_client_wire_call_travels_gated_session(
        self, mocker: MockerFixture
    ) -> None:
        # Given the transport funnel mocked with a parseable Atom feed
        request = mocker.patch.object(
            SafeSession, "request", return_value=_atom_response()
        )

        # When a real (unstubbed) client fetches one paper by id
        papers = arxiv_api_module.fetch_arxiv_results(
            arxiv_api_module.ArxivIdRequest(arxiv_id="2101.12345")
        )

        # Then the wire call went through _GatedArxivSession.get's
        # policy funnel: forced timeout, disabled redirects, and the
        # export.arxiv.org query URL the client built
        assert len(papers) == 1
        assert papers[0].entry_id == "http://arxiv.org/abs/2101.12345v1"
        request.assert_called_once()
        call = request.call_args
        assert call is not None
        assert call.args[0] == "GET"
        assert "export.arxiv.org/api/query" in call.args[1]
        assert call.kwargs.get("timeout") == 10
        assert call.kwargs.get("allow_redirects") is False