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.

17.9 KB
Raw Blame
"""Tests for OpenAccessDOIDownloader (DOI -> open-access PDF resolution)."""

from unittest.mock import Mock, patch

import pytest
import requests
from loguru import logger

from local_deep_research.research_library.downloaders.base import BaseDownloader
from local_deep_research.research_library.downloaders.open_access_doi import (
    OpenAccessDOIDownloader,
)


def _oa_work(pdf_url=None, landing=None, locations=None, is_oa=True):
    """Build an OpenAlex /works response body for a single work.

    ``locations`` mirrors OpenAlex's real schema: a list of location objects,
    each with an ``is_oa`` flag and an optional ``pdf_url`` (OpenAlex returns
    ``locations``, not ``oa_locations``).
    """
    return {
        "results": [
            {
                "id": "W1234567890",
                "open_access": {
                    "is_oa": is_oa,
                    "oa_status": "gold" if is_oa else "closed",
                },
                "best_oa_location": (
                    {"pdf_url": pdf_url, "landing_page_url": landing}
                    if (pdf_url is not None or landing is not None)
                    else None
                ),
                "locations": locations or [],
            }
        ]
    }


def _api_response(body, status_code=200):
    r = Mock()
    r.status_code = status_code
    r.json.return_value = body
    return r


def _pdf_response(pdf_content):
    r = Mock()
    r.status_code = 200
    r.content = pdf_content
    r.headers = {"Content-Type": "application/pdf"}
    return r


# Canonical legacy SICI DOI. Percent-encoded and literal doi.org URLs
# must both normalize to this string, and a query or fragment must not.
_SICI_DOI = "10.1002/(SICI)1099-1409(199908/10)3:6/7<672::AID-JPP192>3.0.CO;2-8"
_SICI_PATHS = (
    "10.1002/(SICI)1099-1409(199908/10)3:6/7%3C672::AID-JPP192%3E3.0.CO;2-8",
    "10.1002/(SICI)1099-1409(199908/10)3:6/7<672::AID-JPP192>3.0.CO;2-8",
    "10.1002/(SICI)1099-1409(199908/10)3:6/7%3C672::AID-JPP192%3E3.0.CO%3B2-8",
)


class TestOpenAccessDOIDownloaderInit:
    def test_preserves_download_service_context(self):
        snapshot = {"research_library.auto_persist_research_sources": False}
        downloader = OpenAccessDOIDownloader(
            username="test_user",
            password="test_password",
            settings_snapshot=snapshot,
            enable_fetch_persistence=False,
        )
        try:
            assert downloader.username == "test_user"
            assert downloader.settings_snapshot is snapshot
            assert downloader.enable_fetch_persistence is False
        finally:
            downloader.close()

    def test_initializes_with_default_timeout(self):
        assert OpenAccessDOIDownloader().timeout == 30

    def test_initializes_with_custom_timeout(self):
        assert OpenAccessDOIDownloader(timeout=60).timeout == 60

    def test_creates_session(self):
        assert OpenAccessDOIDownloader().session is not None


class TestOpenAccessDOIDownloaderCanHandle:
    def test_handles_doi_org(self):
        assert OpenAccessDOIDownloader().can_handle(
            "https://doi.org/10.3390/ma15196591"
        )

    def test_handles_dx_doi_org(self):
        assert OpenAccessDOIDownloader().can_handle(
            "https://dx.doi.org/10.3390/ma15196591"
        )

    def test_rejects_non_doi(self):
        d = OpenAccessDOIDownloader()
        assert not d.can_handle("https://www.nature.com/articles/x")
        assert not d.can_handle("https://arxiv.org/abs/1234.5678")
        assert not d.can_handle("https://openalex.org/W123")

    def test_rejects_empty(self):
        assert not OpenAccessDOIDownloader().can_handle("")


class TestOpenAccessDOIDownloaderExtractDoi:
    def test_extracts_from_doi_org(self):
        assert (
            OpenAccessDOIDownloader()._extract_doi(
                "https://doi.org/10.3390/ma15196591"
            )
            == "10.3390/ma15196591"
        )

    def test_extracts_from_dx_doi_org(self):
        assert (
            OpenAccessDOIDownloader()._extract_doi(
                "https://dx.doi.org/10.1038/s41586-021-04301-9"
            )
            == "10.1038/s41586-021-04301-9"
        )

    def test_returns_none_for_non_doi(self):
        assert (
            OpenAccessDOIDownloader()._extract_doi("https://example.com/x")
            is None
        )

    def test_returns_none_for_empty_path(self):
        assert (
            OpenAccessDOIDownloader()._extract_doi("https://doi.org/") is None
        )

    @pytest.mark.parametrize("host", ["doi.org", "dx.doi.org"])
    @pytest.mark.parametrize("path", _SICI_PATHS)
    @pytest.mark.parametrize("suffix", ["", "?query=1#frag"])
    def test_preserves_semicolon_suffix(self, host, path, suffix):
        """SICI suffixes survive parsing and percent-decoding.

        Literal ``<``/``>``/``;`` and ``%3C``/``%3E``/``%3B`` normalize to
        the same DOI. ``?query=1#frag`` is removed with the split, before
        decoding, so it is not part of the identifier.
        """
        doi = OpenAccessDOIDownloader()._extract_doi(
            f"https://{host}/{path}{suffix}"
        )
        assert doi == _SICI_DOI


class TestOpenAccessDOIDownloaderResolve:
    def test_best_location_pdf(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            return_value=_api_response(
                _oa_work(pdf_url="https://example.com/paper.pdf")
            ),
        ):
            url, status = d._resolve_oa_pdf("10.3390/ma15196591")
        assert status == "oa"
        assert url == "https://example.com/paper.pdf"

    def test_falls_back_to_oa_locations(self):
        """best_oa_location.pdf_url is None but an OA location has a PDF.

        Verifies the documented ``locations`` schema (not the non-existent
        ``oa_locations``): the resolver scans ``locations`` and picks the one
        with ``is_oa=True`` and a ``pdf_url``, skipping non-OA locations.
        """
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            return_value=_api_response(
                _oa_work(
                    pdf_url=None,
                    landing="https://example.com/landing",
                    locations=[
                        {
                            "is_oa": False,
                            "pdf_url": "https://example.com/paywalled.pdf",
                        },
                        {
                            "is_oa": True,
                            "pdf_url": "https://example.com/pmc.pdf",
                        },
                    ],
                )
            ),
        ):
            url, status = d._resolve_oa_pdf("10.3390/ma15196591")
        assert status == "oa"
        assert url == "https://example.com/pmc.pdf"

    def test_skips_non_oa_locations(self):
        """A non-OA location with a PDF must not be used (paywall risk).

        With no ``best_oa_location`` PDF, no OA location PDF, and no landing
        page, the resolver returns ``unknown`` (retryable) — not the paywalled
        PDF.
        """
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            return_value=_api_response(
                _oa_work(
                    pdf_url=None,
                    locations=[
                        {
                            "is_oa": False,
                            "pdf_url": "https://example.com/paywalled.pdf",
                        },
                    ],
                )
            ),
        ):
            url, status = d._resolve_oa_pdf("10.3390/ma15196591")
        assert status != "oa"
        assert url is None

    def test_not_oa(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            return_value=_api_response(_oa_work(pdf_url=None, is_oa=False)),
        ):
            url, status = d._resolve_oa_pdf("10.1016/j.tice.2019.05.008")
        assert status == "not_oa"
        assert url is None

    def test_api_error_is_unknown(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session, "get", return_value=_api_response({}, status_code=500)
        ):
            url, status = d._resolve_oa_pdf("10.3390/ma15196591")
        assert status == "unknown"
        assert url is None

    def test_not_found_is_unknown(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session, "get", return_value=_api_response({"results": []})
        ):
            url, status = d._resolve_oa_pdf("10.9999/does-not-exist")
        assert status == "unknown"
        assert url is None

    def test_exception_is_unknown(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            side_effect=requests.exceptions.ConnectionError("network down"),
        ):
            url, status = d._resolve_oa_pdf("10.3390/ma15196591")
        assert status == "unknown"
        assert url is None

    def test_landing_page_pdf_check(self):
        """No direct PDF URL, but the landing page serves application/pdf."""
        d = OpenAccessDOIDownloader()
        head = Mock()
        head.headers = {"Content-Type": "application/pdf"}
        with patch.object(
            d.session,
            "get",
            return_value=_api_response(
                _oa_work(
                    pdf_url=None,
                    landing="https://example.com/paper.pdf",
                )
            ),
        ):
            with patch.object(d.session, "head", return_value=head):
                url, status = d._resolve_oa_pdf("10.3390/ma15196591")
        assert status == "oa"
        assert url == "https://example.com/paper.pdf"

    def test_oa_without_any_pdf_is_unknown_not_paywall(self):
        """OA work with no retrievable PDF must be retryable, not a paywall."""
        d = OpenAccessDOIDownloader()
        head = Mock()
        head.headers = {"Content-Type": "text/html"}
        with patch.object(
            d.session,
            "get",
            return_value=_api_response(
                _oa_work(pdf_url=None, landing="https://example.com/landing")
            ),
        ):
            with patch.object(d.session, "head", return_value=head):
                url, status = d._resolve_oa_pdf("10.3390/ma15196591")
        assert status == "unknown"
        assert url is None


class TestOpenAccessDOIDownloaderDownloadWithResult:
    def test_success(self, mock_pdf_content):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            side_effect=[
                _api_response(
                    _oa_work(pdf_url="https://example.com/paper.pdf")
                ),
                _pdf_response(mock_pdf_content),
            ],
        ):
            result = d.download_with_result(
                "https://doi.org/10.3390/ma15196591"
            )
        assert result.is_success is True
        assert result.content == mock_pdf_content

    def test_not_oa_is_permanent_paywall(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            return_value=_api_response(_oa_work(pdf_url=None, is_oa=False)),
        ):
            result = d.download_with_result(
                "https://doi.org/10.1016/j.tice.2019.05.008"
            )
        assert result.is_success is False
        assert "paywall" in result.skip_reason.lower()

    def test_api_error_is_retryable_not_paywall(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session, "get", return_value=_api_response({}, status_code=500)
        ):
            result = d.download_with_result(
                "https://doi.org/10.3390/ma15196591"
            )
        assert result.is_success is False
        assert "paywall" not in result.skip_reason.lower()

    def test_pdf_download_failure_is_retryable(self):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            side_effect=[
                _api_response(
                    _oa_work(pdf_url="https://example.com/paper.pdf")
                ),
                Mock(status_code=403, content=b"", headers={}),
            ],
        ):
            result = d.download_with_result(
                "https://doi.org/10.3390/ma15196591"
            )
        assert result.is_success is False
        assert "paywall" not in result.skip_reason.lower()

    def test_text_success(self, mock_pdf_content):
        d = OpenAccessDOIDownloader()
        with patch.object(
            d.session,
            "get",
            side_effect=[
                _api_response(
                    _oa_work(pdf_url="https://example.com/paper.pdf")
                ),
                _pdf_response(mock_pdf_content),
            ],
        ):
            with patch.object(
                OpenAccessDOIDownloader,
                "extract_text_from_pdf",
                return_value="extracted text",
            ):
                result = d.download_with_result(
                    "https://doi.org/10.3390/ma15196591",
                    content_type="text",
                )
        assert result.is_success is True
        assert result.content == b"extracted text"

    def test_cannot_extract_doi(self):
        d = OpenAccessDOIDownloader()
        result = d.download_with_result("https://doi.org/")
        assert result.is_success is False
        assert result.skip_reason is not None

    @pytest.mark.parametrize("host", ["doi.org", "dx.doi.org"])
    def test_sici_suffix_is_sent_in_openalex_filter(
        self, host, mock_pdf_content
    ):
        """The OpenAlex filter receives the full normalized SICI DOI."""
        d = OpenAccessDOIDownloader()
        url = (
            f"https://{host}/10.1002/(SICI)1099-1409(199908/10)3:6/7"
            "%3C672::AID-JPP192%3E3.0.CO;2-8"
        )
        with patch.object(
            d.session,
            "get",
            side_effect=[
                _api_response(
                    _oa_work(pdf_url="https://example.com/paper.pdf")
                ),
                _pdf_response(mock_pdf_content),
            ],
        ) as session_get:
            result = d.download_with_result(url)

        assert result.is_success is True
        assert result.content == mock_pdf_content
        filters = [
            call.kwargs["params"]["filter"]
            for call in session_get.call_args_list
            if isinstance(call.kwargs.get("params"), dict)
            and "filter" in call.kwargs["params"]
        ]
        assert filters == [f"doi:{_SICI_DOI}"]


@pytest.fixture
def doi_logs():
    messages = []
    logger.enable("local_deep_research")
    sink = logger.add(
        messages.append, level="DEBUG", format="{message}\n{exception}"
    )
    try:
        yield messages
    finally:
        logger.remove(sink)
        logger.disable("local_deep_research")


def test_pdf_download_logs_origin_only(doi_logs):
    downloader = OpenAccessDOIDownloader()
    downloader._resolve_oa_pdf = Mock(
        return_value=(
            "https://publisher.example/document-marker.pdf?token=private-marker",
            "oa",
        )
    )
    try:
        with patch.object(
            BaseDownloader, "_download_pdf", return_value=b"%PDF"
        ):
            assert downloader.download_with_result(
                "https://doi.org/10.1234/document-marker"
            ).is_success
    finally:
        downloader.close()

    rendered = "".join(doi_logs)
    assert "https://publisher.example" in rendered
    assert "document-marker" not in rendered
    assert "private-marker" not in rendered
    assert all(message.record["exception"] is None for message in doi_logs)


@pytest.mark.parametrize(
    "body",
    [
        {"results": []},
        _oa_work(is_oa=False),
        _oa_work(landing="https://publisher.example/document-marker"),
    ],
)
def test_lookup_diagnostics_omit_document_details(body, doi_logs):
    downloader = OpenAccessDOIDownloader()
    response = _api_response(body)
    head = Mock(headers={"Content-Type": "text/html"})
    try:
        with (
            patch.object(downloader.session, "get", return_value=response),
            patch.object(downloader.session, "head", return_value=head),
        ):
            downloader._resolve_oa_pdf("10.1234/document-marker")
        response.close.assert_called_once()
        if body.get("results") and body["results"][0].get("best_oa_location"):
            head.close.assert_called_once()
    finally:
        downloader.close()

    assert "document-marker" not in "".join(doi_logs)
    assert all(message.record["exception"] is None for message in doi_logs)


@pytest.mark.parametrize("failure_point", ["get", "json", "head"])
def test_lookup_failures_emit_controlled_diagnostics(failure_point, doi_logs):
    downloader = OpenAccessDOIDownloader()
    response = _api_response(
        _oa_work(landing="https://publisher.example/document-marker")
    )
    error = requests.RequestException("document-marker private-marker")
    if failure_point == "json":
        response.json.side_effect = ValueError("document-marker private-marker")
    try:
        with (
            patch.object(
                downloader.session,
                "get",
                side_effect=error if failure_point == "get" else None,
                return_value=response,
            ),
            patch.object(downloader.session, "head", side_effect=error),
        ):
            assert downloader._resolve_oa_pdf("10.1234/document-marker") == (
                None,
                "unknown",
            )
        if failure_point != "get":
            response.close.assert_called_once()
    finally:
        downloader.close()

    rendered = "".join(doi_logs)
    assert "ValueError" in rendered or "RequestException" in rendered
    assert "document-marker" not in rendered
    assert "private-marker" not in rendered
    assert all(message.record["exception"] is None for message in doi_logs)