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.

5.3 KB
Raw Blame
"""PubMed: a transient Europe PMC lookup failure stays retryable (#8019).

When the Europe PMC search answered 429 or a 5xx and the NCBI PMC fallback
found nothing, the downloader used to end with "No free full-text available
- article may be paywalled" and no status code. The word "paywalled" is what
``FailureClassifier`` matches to record a permanent ``paywall_or_login``
failure, so a rate-limited or briefly unavailable lookup was never retried.
"""

from unittest.mock import MagicMock, patch

import pytest

from local_deep_research.library.download_management.failure_classifier import (
    FailureClassifier,
)
from local_deep_research.research_library.downloaders.base import (
    ContentType,
)
from local_deep_research.research_library.downloaders.pubmed import (
    PubMedDownloader,
)

PUBMED_URL = "https://pubmed.ncbi.nlm.nih.gov/12345678/"


@pytest.fixture
def downloader():
    return PubMedDownloader(timeout=30, rate_limit_delay=0)


def _response(status_code):
    response = MagicMock()
    response.status_code = status_code
    response.text = ""
    return response


def _classify(result):
    return FailureClassifier().classify_failure(
        "download_failed",
        status_code=result.status_code,
        url=PUBMED_URL,
        details=result.skip_reason,
    )


def test_rate_limited_lookup_is_reported_as_429(downloader):
    """Every HTTP call answers 429: the Europe PMC search, the NCBI elink
    lookup and the PubMed page fallback, so no PMC ID is found.

    Revert-catcher: without the carried status the result is the status-less
    "may be paywalled" reason, classified as permanent ``paywall_or_login``.
    """
    with patch.object(
        downloader.session, "get", return_value=_response(429)
    ) as mock_get:
        result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)

    assert mock_get.call_count >= 1
    assert result.is_success is False
    assert result.status_code == 429
    assert "429" in result.skip_reason
    assert "paywall" not in result.skip_reason.lower()

    failure = _classify(result)
    assert failure.error_type == "rate_limited"
    assert not failure.is_permanent()


@pytest.mark.parametrize("status_code", [500, 502, 503, 504])
def test_server_error_lookup_carries_its_status(downloader, status_code):
    """Revert-catcher: a 5xx from the Europe PMC search used to be dropped
    and the attempt recorded as a permanent paywall."""
    with patch.object(
        downloader.session, "get", return_value=_response(status_code)
    ):
        result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)

    assert result.is_success is False
    assert result.status_code == status_code
    assert f"HTTP {status_code}" in result.skip_reason
    assert "paywall" not in result.skip_reason.lower()

    failure = _classify(result)
    assert failure.error_type != "paywall_or_login"
    assert not failure.is_permanent()


def test_rate_limited_lookup_still_tries_the_pmc_fallback(downloader):
    """Guards existing behaviour; not a revert-catcher for #8019 (it passes
    with or without the fix). A failed lookup must not short-circuit: the
    NCBI PMC ID fallback can still deliver the PDF."""
    with (
        patch.object(downloader.session, "get", return_value=_response(429)),
        patch.object(
            downloader, "_get_pmc_id_from_pmid", return_value="PMC4242"
        ),
        patch.object(
            downloader, "_download_via_europe_pmc", return_value=b"%PDF-ok"
        ) as mock_europe_pmc,
    ):
        result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)

    assert result.is_success is True
    assert result.content == b"%PDF-ok"
    mock_europe_pmc.assert_called_once_with("PMC4242")


def test_pmc_fallback_failure_keeps_its_own_reason(downloader):
    """Guards existing behaviour; not a revert-catcher for #8019 (it passes
    with or without the fix). When the fallback finds a PMC ID but cannot
    fetch the PDF, that more specific reason is kept and the lookup status
    is not reported."""
    with (
        patch.object(downloader.session, "get", return_value=_response(503)),
        patch.object(
            downloader, "_get_pmc_id_from_pmid", return_value="PMC4242"
        ),
        patch.object(downloader, "_download_via_europe_pmc", return_value=None),
        patch.object(downloader, "_download_via_ncbi_pmc", return_value=None),
    ):
        result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)

    assert result.is_success is False
    assert "PMC4242" in result.skip_reason


def test_not_open_access_answer_is_still_a_paywall(downloader):
    """Guards existing behaviour; not a revert-catcher for #8019 (it passes
    with or without the fix). A successful lookup that says the article is
    not open access remains the permanent subscription classification."""
    response = _response(200)
    response.json.return_value = {
        "resultList": {
            "result": [{"isOpenAccess": "N", "journalTitle": "Some Journal"}]
        }
    }
    with patch.object(downloader.session, "get", return_value=response):
        result = downloader.download_with_result(PUBMED_URL, ContentType.PDF)

    assert result.status_code is None
    failure = _classify(result)
    assert failure.error_type == "paywall_or_login"
    assert failure.is_permanent()