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.

4.9 KB
Raw Blame
"""A failed PubMed text extraction must not claim a paywall it never saw.

``PubMedDownloader._download_text`` returns ``None`` for an unparseable URL, a
Europe PMC API error, an unreachable PDF and a PDF whose text could not be
extracted alike. Reporting all of them as "may require subscription" states a
paywall as fact for failures that have nothing to do with access, and the word
"subscription" is exactly what ``FailureClassifier.classify_failure`` matches to
declare a permanent ``paywall_or_login`` failure (#6412).

The genuine paywall signal is unaffected: it comes from Europe PMC's
``isOpenAccess`` flag on the PDF path, asserted in
``test_pubmed_coverage.py::TestDownloadPdfWithResult``.
"""

from unittest.mock import MagicMock, patch

import pytest

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

PAYWALL_WORDS = (
    "subscription",
    "paywall",
    "requires login",
    "requires authentication",
)

# The wording this fix replaced, kept as a control: the assertions below are
# about the reason being classifiable, not about phrasing taste.
PREVIOUS_REASON = "Full text not available - may require subscription"


@pytest.fixture
def downloader():
    dl = PubMedDownloader(rate_limit_delay=0.0)
    dl.last_request_time = 0
    return dl


# Failures with no access evidence behind them, covering each way
# `_download_text` can give up.
NON_PAYWALL_FAILURES = [
    pytest.param(
        "https://pubmed.ncbi.nlm.nih.gov/12345678/",
        id="api-and-pdf-both-failed",
    ),
    pytest.param(
        "https://europepmc.org/article/MED/12345678", id="no-id-in-url"
    ),
    pytest.param(
        "https://ncbi.nlm.nih.gov/pmc/articles/PMC12345/",
        id="pmc-text-unavailable",
    ),
]


@pytest.mark.parametrize("url", NON_PAYWALL_FAILURES)
def test_text_failure_does_not_assert_a_paywall(downloader, url):
    with patch.object(downloader, "_download_text", return_value=None):
        result = downloader.download_with_result(url, ContentType.TEXT)

    assert result.is_success is False
    assert result.skip_reason, (
        "a skipped text download must still say something"
    )
    named = [
        word for word in PAYWALL_WORDS if word in result.skip_reason.lower()
    ]
    assert not named, f"{result.skip_reason!r} asserts {named} without evidence"


def test_text_failure_reason_is_not_classified_as_a_permanent_paywall(
    downloader,
):
    """The reason travels to the classifier as the failure `details`.

    ``RetryManager.record_attempt`` passes the skip reason straight through as
    ``details``, so the wording decides whether the resource is blacklisted.
    """
    from local_deep_research.library.download_management.failure_classifier import (
        FailureClassifier,
    )

    with patch.object(downloader, "_download_text", return_value=None):
        reason = downloader.download_with_result(
            "https://pubmed.ncbi.nlm.nih.gov/12345678/", ContentType.TEXT
        ).skip_reason

    classifier = FailureClassifier()
    failure = classifier.classify_failure(
        error_type="unknown",
        details=reason,
        url="https://pubmed.ncbi.nlm.nih.gov/12345678/",
    )
    assert not (
        isinstance(failure, PermanentFailure)
        and failure.error_type == "paywall_or_login"
    ), f"{reason!r} is still read as a paywall: {failure.error_type}"

    # Control: the previous wording is, so the change is load-bearing.
    previous = classifier.classify_failure(
        error_type="unknown",
        details=PREVIOUS_REASON,
        url="https://pubmed.ncbi.nlm.nih.gov/12345678/",
    )
    assert isinstance(previous, PermanentFailure)
    assert previous.error_type == "paywall_or_login"


def test_successful_text_extraction_is_untouched(downloader):
    with patch.object(downloader, "_download_text", return_value=b"full text"):
        result = downloader.download_with_result(
            "https://pubmed.ncbi.nlm.nih.gov/12345678/", ContentType.TEXT
        )

    assert result.is_success is True
    assert result.content == b"full text"
    assert result.skip_reason is None


def test_a_real_paywall_is_still_reported_on_the_pdf_path(downloader):
    """Europe PMC's open-access flag is evidence, and it must keep speaking."""
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {
        "resultList": {
            "result": [{"isOpenAccess": "N", "journalTitle": "Nature Medicine"}]
        }
    }

    with patch.object(downloader.session, "get", return_value=response):
        result = downloader.download_with_result(
            "https://pubmed.ncbi.nlm.nih.gov/12345678/", ContentType.PDF
        )

    assert result.is_success is False
    assert "subscription" in result.skip_reason.lower()
    assert "Nature Medicine" in result.skip_reason