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.

56.9 KB
Raw Blame
import pytest

from local_deep_research.research_library.downloaders.arxiv import (
    MAX_MATH_ELEMENTS,
    ArxivDownloader,
    ArxivTextResult,
    ArxivTextSource,
)
from local_deep_research.research_library.downloaders.base import ContentType
from local_deep_research.utilities import arxiv_api


@pytest.fixture
def downloader():
    return ArxivDownloader(timeout=30)


def test_text_uses_official_html_and_preserves_tex(downloader, mocker):
    # Given an ar5iv URL whose official arXiv HTML contains annotated MathML.
    # The prose is repeated to a realistic length on purpose: a real LaTeXML
    # rendition carries a title, authors, abstract and body and runs to tens
    # of thousands of characters, so a few hundred would be indistinguishable
    # from the "no HTML rendition" stub that
    # MIN_STANDALONE_HTML_TEXT_LENGTH exists to reject. Repeating the body
    # does not weaken what this test asserts, which is that the TeX
    # annotation survives extraction.
    body_prose = (
        "This paper develops a detailed mathematical argument with enough "
        "explanatory prose for the shared extraction pipeline to retain the "
        "article body. The derivation is repeated across several examples so "
        "readers can follow every step and understand the surrounding context. "
    ) * 8
    html = (
        "<html><head><title>Versioned Paper</title></head>"
        "<body><article><p>"
        + body_prose
        + "The central ratio is <math><semantics><mfrac><mi>a</mi><mi>b</mi>"
        '</mfrac><annotation encoding="application/x-tex">\\frac{a}{b}'
        "</annotation></semantics></math> and the remaining discussion explains "
        "why this expression is useful in practice. Additional conclusions "
        "describe the assumptions, the resulting bounds, and the implications "
        "for future experiments.</p></article></body></html>"
    )
    fetch_html = mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(html, "https://arxiv.org/html/2501.12345v2"),
    )
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")
    hard_clock = mocker.patch.object(
        arxiv_api,
        "_monotonic",
        side_effect=AssertionError("HTML path touched API policy clock"),
    )
    hard_sleep = mocker.patch.object(
        arxiv_api,
        "_sleep",
        side_effect=AssertionError("HTML path touched API policy sleep"),
    )

    # When text is downloaded from an ar5iv input
    result = downloader.download_text_with_source(
        "https://ar5iv.org/2501.12345v2"
    )

    # Then only official versioned HTML is fetched and normalized text is returned
    assert result is not None
    assert result.source is ArxivTextSource.ARXIV_HTML
    text = result.text
    assert r"\frac{a}{b}" in text
    assert "Source: https://arxiv.org/abs/2501.12345v2" in text
    assert "<math" not in text
    assert "application/x-tex" not in text
    fetch_html.assert_called_once_with(
        "https://arxiv.org/html/2501.12345v2", max_attempts=1
    )
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()
    hard_clock.assert_not_called()
    hard_sleep.assert_not_called()


def test_pdf_path_does_not_touch_api_gate(downloader, mocker):
    # Given a direct PDF download with tripwires on the API-only policy gate
    hard_clock = mocker.patch.object(
        arxiv_api,
        "_monotonic",
        side_effect=AssertionError("PDF path touched API policy clock"),
    )
    hard_sleep = mocker.patch.object(
        arxiv_api,
        "_sleep",
        side_effect=AssertionError("PDF path touched API policy sleep"),
    )
    pdf_download = mocker.patch(
        "local_deep_research.research_library.downloaders"
        ".base.BaseDownloader._download_pdf",
        return_value=b"%PDF-test",
    )

    # When the PDF path is used
    content = downloader.download(
        "https://arxiv.org/abs/2301.12345", ContentType.PDF
    )

    # Then no hard API gate state is consulted
    assert content == b"%PDF-test"
    pdf_download.assert_called_once()
    hard_clock.assert_not_called()
    hard_sleep.assert_not_called()


@pytest.mark.parametrize(
    "unusable_math",
    [
        "<math><mi>x</mi></math>",
        (
            "<math><semantics><mi>x</mi>"
            '<annotation encoding="application/x-tex">   </annotation>'
            "</semantics></math>"
        ),
    ],
    ids=["missing-tex-annotation", "empty-tex-annotation"],
)
def test_text_preserves_document_when_any_math_lacks_usable_tex(
    downloader, mocker, unusable_math
):
    # Given HTML whose prose is long enough to stand alone and one formula
    # without a usable TeX annotation -- the partially annotated renditions
    # at the heart of issue #4783. The prose length matters because
    # text-first callers have no PDF text to compare against, so
    # MIN_STANDALONE_HTML_TEXT_LENGTH decides whether HTML can stand alone.
    body_prose = (
        "This paper develops a detailed mathematical argument with enough "
        "explanatory prose for the shared extraction pipeline to retain the "
        "article body. The derivation is repeated across several examples so "
        "readers can follow every step and understand the surrounding context. "
    ) * 8
    html = (
        "<html><head><title>Partially Annotated Paper</title></head>"
        "<body><article><p>"
        + body_prose
        + "A fully annotated formula <math><semantics><mi>y</mi>"
        + '<annotation encoding="application/x-tex">'
        + "y^2</annotation></semantics></math> sits beside an unusable one "
        + f"{unusable_math}. The surrounding discussion continues with "
        "assumptions, results, limitations, and conclusions."
        "</p></article></body></html>"
    )
    fetch_html = mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(html, "https://arxiv.org/html/math.AG/0601001v3"),
    )
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When text is requested for a versioned legacy identifier
    result = downloader.download_with_result(
        "https://arxiv.org/html/math.AG/0601001v3?download=1#section",
        ContentType.TEXT,
    )

    # Then the document survives: the annotated formula keeps its TeX and
    # the unusable one degrades to visible MathML text instead of rejecting
    # the whole HTML rendition
    assert result.is_success is True
    content = result.content.decode("utf-8")
    assert "y^2" in content
    assert "<math" not in content
    assert "Source: https://arxiv.org/abs/math.AG/0601001v3" in content
    fetch_html.assert_called_once_with(
        "https://arxiv.org/html/math.AG/0601001v3", max_attempts=1
    )
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()


def test_math_nodes_convert_through_every_fallback_tier(downloader, mocker):
    # Given a LaTeXML-style rendition exercising every conversion tier: a
    # whitespace-first TeX annotation pair, a TeX annotation that exists
    # only as x-tex+html, an alttext attribute over visible MathML, and
    # bare visible MathML. A genuinely empty node is deliberately absent
    # here: it still takes the whole-page PDF exit by design, which
    # test_neither_source_still_falls_back_to_the_pdf pins.
    body_prose = (
        "This paper develops a detailed mathematical argument with enough "
        "explanatory prose for the shared extraction pipeline to retain the "
        "article body. The derivation is repeated across several examples so "
        "readers can follow every step and understand the surrounding context. "
    ) * 8
    html = (
        "<html><head><title>Partly Annotated LaTeXML Paper</title></head>"
        "<body><article><p>"
        + body_prose
        + "Equations arrive in every state of annotation: "
        + "<math><semantics><mfrac><mi>a</mi><mi>b</mi></mfrac>"
        + '<annotation encoding="application/x-tex">   </annotation>'
        + '<annotation encoding="application/x-tex">'
        + "\\frac{a}{b}</annotation></semantics></math>, "
        + "<math><semantics><mi>z</mi>"
        + '<annotation encoding="application/x-tex"> </annotation>'
        + '<annotation encoding="application/x-tex+html">'
        + "\\sqrt{z}</annotation></semantics></math>, "
        + '<math alttext="\\alpha"><mi>α</mi></math>, '
        + "<math><mrow><mi>p</mi><mo>+</mo><mi>q</mi></mrow></math>. "
        + "The discussion continues with assumptions, results, limitations, "
        + "and conclusions so nothing is lost."
        "</p></article></body></html>"
    )
    fetch_html = mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(html, "https://arxiv.org/html/2501.12345v2"),
    )
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When text is produced without PDF bytes in hand
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2501.12345v2"
    )

    # Then each node converted by its best available tier and none of them
    # rejected the document
    assert result is not None
    assert result.source is ArxivTextSource.ARXIV_HTML
    text = result.text
    assert "\\frac{a}{b}" in text
    assert "\\sqrt{z}" in text
    assert "\\alpha" in text
    assert "p + q" in text
    assert "α" not in text
    assert "<math" not in text
    assert "application/x-tex" not in text
    fetch_html.assert_called_once_with(
        "https://arxiv.org/html/2501.12345v2", max_attempts=1
    )
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()


def _math_page(count: int) -> str:
    """A rendition whose <math> elements all share one parent."""
    formula = (
        "<math><semantics><mi>x</mi>"
        '<annotation encoding="application/x-tex">x^2</annotation>'
        "</semantics></math> "
    )
    prose = (
        "This article carries a very large number of typeset equations and "
        "enough surrounding prose that the shared extraction pipeline keeps "
        "the body. Each equation is followed by discussion of the bounds it "
        "implies and the assumptions behind them. "
    ) * 8
    return (
        "<html><head><title>Equation Heavy</title></head>"
        "<body><article><p>" + prose + formula * count + "</p></article>"
        "</body></html>"
    )


def test_text_falls_back_to_pdf_above_the_math_element_bound(
    downloader, mocker
):
    # Given a rendition carrying one more equation than the bound allows.
    # arXiv renders this HTML from author-submitted LaTeX and it is reached
    # on the search path, so the work one page can demand has to be capped.
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(
            _math_page(MAX_MATH_ELEMENTS + 1),
            "https://arxiv.org/html/2501.12345v2",
        ),
    )
    mocker.patch.object(downloader, "_download_pdf", return_value=b"%PDF-test")
    mocker.patch.object(
        downloader, "extract_text_from_pdf", return_value="PDF fallback text"
    )
    mocker.patch.object(downloader, "_fetch_from_arxiv_api", return_value=None)

    # When text is requested
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2501.12345v2"
    )

    # Then the page is not rewritten and the PDF path wins
    assert result is not None
    assert result.source is ArxivTextSource.LOCAL_PDF
    assert "x^2" not in result.text


def test_text_still_rewrites_a_page_at_the_math_element_bound(
    downloader, mocker
):
    # Given a rendition exactly at the bound -- the guard caps the work an
    # untrusted page can demand, it is not what makes the rewrite correct,
    # and no page at or below it may be skipped
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(
            _math_page(MAX_MATH_ELEMENTS),
            "https://arxiv.org/html/2501.12345v2",
        ),
    )
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When text is requested
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2501.12345v2"
    )

    # Then the TeX is preserved from the HTML and no PDF is fetched
    assert result is not None
    assert result.source is ArxivTextSource.ARXIV_HTML
    assert "x^2" in result.text
    assert "<math" not in result.text
    assert "application/x-tex" not in result.text
    pdf_download.assert_not_called()


def test_small_page_with_shared_parent_still_rewrites_every_formula(
    downloader,
):
    # Given a handful of equations under one parent -- the shape the old
    # replace_with loop made quadratic
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(_math_page(5), "lxml")

    # When the rewrite runs
    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    # Then every formula is replaced by its TeX, in document order
    assert rewritten is True
    assert soup.find_all("math") == []
    assert soup.get_text().count("x^2") == 5


def test_math_element_bound_is_five_thousand():
    # The two tests above build their pages from MAX_MATH_ELEMENTS, so they
    # pass for any value of it, including one small enough to reject every
    # real paper. Pin the number itself: changing it is a deliberate act
    # that has to be made here as well.
    assert MAX_MATH_ELEMENTS == 5000


def test_rewrite_renames_in_place_and_never_calls_replace_with(
    downloader, mocker
):
    # Given equations sharing one parent. Tag.replace_with locates the
    # element by scanning that parent's contents, so using it here is what
    # made the loop O(n^2); the bound above caps the damage but does not
    # prevent the regression, and the rewrite's output is identical either
    # way, so nothing else in this file can tell the two apart.
    from bs4 import BeautifulSoup
    from bs4.element import PageElement

    soup = BeautifulSoup(_math_page(5), "lxml")
    replace_with = mocker.spy(PageElement, "replace_with")

    # When the rewrite runs
    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    # Then it mutates each element where it stands -- renamed to a <span>
    # holding only the TeX -- and never reaches for replace_with
    assert rewritten is True
    replace_with.assert_not_called()
    spans = [
        tag
        for tag in soup.find_all("span")
        if tag.get_text() == "x^2" and not tag.attrs
    ]
    assert len(spans) == 5


def test_namespace_prefixed_mathml_is_rewritten_too(downloader, mocker):
    # Given a rendition whose MathML is namespace-prefixed. lxml's HTML
    # parser keeps the prefix in the tag name, so matching the literal tag
    # "math" finds nothing here and the page is accepted with its MathML
    # left in place. On this page newspaper4k's text then wins the
    # extractor tiebreak and keeps the <m:mi> symbol glued to the TeX
    # ("x\\frac{1}{2}"); on other pages the equation is dropped instead. The
    # glued form is what this fixture produces without the rewrite, so the
    # sentence arriving clean is what distinguishes the rewrite from a
    # revert.
    prose = (
        "This article states a bound and then discusses the assumptions "
        "behind it at length, so the shared extraction pipeline keeps the "
        "article body rather than discarding it as boilerplate. "
    ) * 40
    page = (
        "<html><head><title>Prefixed MathML</title></head>"
        "<body><article><p>" + prose + "The bound is "
        "<m:math><m:semantics><m:mi>x</m:mi>"
        '<m:annotation encoding="application/x-tex">\\frac{1}{2}'
        "</m:annotation></m:semantics></m:math> throughout."
        "</p></article></body></html>"
    )
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(page, "https://arxiv.org/html/2501.12345v2"),
    )
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When text is requested
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2501.12345v2"
    )

    # Then the TeX is stored clean, without the <m:mi> symbol glued to it,
    # and no PDF is fetched
    assert result is not None
    assert result.source is ArxivTextSource.ARXIV_HTML
    assert "The bound is \\frac{1}{2} throughout." in result.text
    pdf_download.assert_not_called()


def test_text_falls_back_when_html_extracts_no_content(downloader, mocker):
    # Given available HTML that has no meaningful article content
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(
            "<html><body><p>Short.</p></body></html>",
            "https://arxiv.org/html/2301.12345",
        ),
    )
    mocker.patch.object(downloader, "_download_pdf", return_value=b"%PDF-test")
    mocker.patch.object(
        downloader, "extract_text_from_pdf", return_value="Recovered PDF text"
    )
    mocker.patch.object(downloader, "_fetch_from_arxiv_api", return_value=None)

    # When text is requested
    result = downloader.download_with_result(
        "https://arxiv.org/abs/2301.12345", ContentType.TEXT
    )

    # Then empty HTML extraction falls back to the existing PDF path
    assert result.is_success is True
    assert result.content == (
        b"Recovered PDF text\n\nSource: https://arxiv.org/abs/2301.12345"
    )


def test_text_falls_back_when_html_pipeline_cannot_be_used(downloader, mocker):
    # Given HTML whose extraction pipeline unexpectedly cannot process it
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(
            "<html><body><article>content</article></body></html>",
            "https://arxiv.org/html/2301.12345",
        ),
    )
    mocker.patch.object(
        downloader,
        "_extract_content",
        side_effect=RuntimeError("malformed HTML"),
    )
    mocker.patch.object(downloader, "_download_pdf", return_value=b"%PDF-test")
    mocker.patch.object(
        downloader,
        "extract_text_from_pdf",
        return_value="PDF after malformed HTML",
    )
    mocker.patch.object(downloader, "_fetch_from_arxiv_api", return_value=None)

    # When text is requested
    result = downloader.download_with_result(
        "https://arxiv.org/abs/2301.12345", ContentType.TEXT
    )

    # Then the failure is contained and the existing PDF path is used
    assert result.is_success is True
    assert result.content == (
        b"PDF after malformed HTML\n\nSource: https://arxiv.org/abs/2301.12345"
    )


def test_text_entrypoints_share_the_same_download_seam(downloader, mocker):
    # Given a successful text behavior seam
    text_download = mocker.patch.object(
        downloader,
        "download_text_with_source",
        return_value=ArxivTextResult("shared text", ArxivTextSource.ARXIV_HTML),
    )
    mocker.patch.object(
        downloader,
        "_download_pdf",
        side_effect=AssertionError("text entrypoint bypassed shared seam"),
    )
    url = "https://arxiv.org/abs/2301.12345v4"

    # When both public text entrypoints are used
    content = downloader.download(url, ContentType.TEXT)
    result = downloader.download_with_result(url, ContentType.TEXT)

    # Then both expose the same bytes through that one seam
    assert content == b"shared text"
    assert result.is_success is True
    assert result.content == b"shared text"
    assert text_download.call_count == 2


def test_download_text_reuses_supplied_pdf_bytes(downloader, mocker):
    # Given unavailable HTML and PDF bytes already downloaded by the caller
    pdf_content = b"%PDF-already-downloaded"
    mocker.patch.object(downloader, "_download_html_text", return_value=None)
    pdf_download = mocker.patch.object(
        downloader,
        "_download_pdf",
        side_effect=AssertionError("supplied PDF bytes were downloaded again"),
    )
    extract_text = mocker.patch.object(
        downloader,
        "extract_text_from_pdf",
        return_value="Text from supplied PDF",
    )
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When the public text seam receives those bytes
    result = downloader.download_text(
        "https://arxiv.org/abs/2301.12345",
        pdf_content=pdf_content,
    )

    # Then it extracts those exact bytes without another PDF or API request
    assert result == (
        "Text from supplied PDF\n\nSource: https://arxiv.org/abs/2301.12345"
    )
    extract_text.assert_called_once_with(pdf_content)
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()


def test_download_text_treats_empty_pdf_bytes_as_supplied(downloader, mocker):
    # Given unavailable HTML and explicitly supplied empty PDF bytes
    mocker.patch.object(downloader, "_download_html_text", return_value=None)
    pdf_download = mocker.patch.object(
        downloader,
        "_download_pdf",
        side_effect=AssertionError("empty supplied bytes triggered a download"),
    )
    extract_text = mocker.patch.object(
        downloader,
        "extract_text_from_pdf",
        return_value=None,
    )
    api_fetch = mocker.patch.object(
        downloader,
        "_fetch_from_arxiv_api",
        return_value="API metadata and abstract",
    )

    # When text production receives the empty bytes
    result = downloader.download_text(
        "https://ar5iv.org/2301.12345v2",
        pdf_content=b"",
    )

    # Then no PDF is redownloaded and the final API fallback remains available
    assert result == (
        "API metadata and abstract\n\n"
        "Source: https://arxiv.org/abs/2301.12345v2"
    )
    extract_text.assert_called_once_with(b"")
    pdf_download.assert_not_called()
    api_fetch.assert_called_once_with("2301.12345v2")


def test_download_text_with_source_reports_local_pdf(downloader, mocker):
    # Given unavailable HTML and exact PDF bytes supplied by the caller
    pdf_content = b"%PDF-reused-exactly"
    mocker.patch.object(downloader, "_download_html_text", return_value=None)
    extract_text = mocker.patch.object(
        downloader,
        "extract_text_from_pdf",
        return_value="PDF body with trailing whitespace\n\n",
    )
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When typed text production succeeds from those bytes
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2301.12345v4",
        pdf_content=pdf_content,
    )

    # Then the source is typed and canonical attribution has one blank line
    assert result == ArxivTextResult(
        text=(
            "PDF body with trailing whitespace\n\n"
            "Source: https://arxiv.org/abs/2301.12345v4"
        ),
        source=ArxivTextSource.LOCAL_PDF,
    )
    extract_text.assert_called_once_with(pdf_content)
    api_fetch.assert_not_called()


def test_download_text_with_source_reports_arxiv_api(downloader, mocker):
    # Given unavailable HTML and PDF text with usable API metadata
    mocker.patch.object(downloader, "_download_html_text", return_value=None)
    mocker.patch.object(downloader, "_download_pdf", return_value=None)
    mocker.patch.object(
        downloader,
        "_fetch_from_arxiv_api",
        return_value="Title: API fallback",
    )

    # When typed text production reaches its final source
    result = downloader.download_text_with_source(
        "https://ar5iv.org/math.AG/0601001v3"
    )

    # Then the API source and version-preserving attribution are retained
    assert result == ArxivTextResult(
        text=(
            "Title: API fallback\n\n"
            "Source: https://arxiv.org/abs/math.AG/0601001v3"
        ),
        source=ArxivTextSource.ARXIV_API,
    )


def test_download_text_delegates_to_typed_result(downloader, mocker):
    # Given a typed HTML result
    typed_result = ArxivTextResult(
        text="typed text",
        source=ArxivTextSource.ARXIV_HTML,
    )
    typed_download = mocker.patch.object(
        downloader,
        "download_text_with_source",
        return_value=typed_result,
    )

    # When the compatibility method is called
    result = downloader.download_text(
        "https://arxiv.org/abs/2301.12345", pdf_content=b"existing"
    )

    # Then it exposes only text while preserving the typed seam internally
    assert result == "typed text"
    typed_download.assert_called_once_with(
        "https://arxiv.org/abs/2301.12345",
        pdf_content=b"existing",
    )


def test_download_text_rejects_invalid_url_without_requests(downloader, mocker):
    # Given tripwires on every text source
    html_download = mocker.patch.object(downloader, "_download_html_text")
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When an invalid identifier reaches the public text seam
    result = downloader.download_text(
        "https://example.com/not-arxiv",
        pdf_content=b"ignored",
    )

    # Then it is rejected before any source is queried
    assert result is None
    html_download.assert_not_called()
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()


# arXiv answers /html/{id} with a stub page for papers that have no HTML
# rendition. It is short, but long enough to clear the extraction pipeline's
# minimum content length, so only a comparison against the PDF rejects it.
DEGENERATE_ARXIV_HTML_STUB = (
    "<html><head><title>No HTML for 2301.12345</title></head>"
    "<body><article><p>"
    "No HTML rendition is available for this submission. The authors did not "
    "provide a LaTeX source that arXiv could convert, so please consult the "
    "PDF version of the paper instead. This notice is generated automatically "
    "for every submission without an HTML rendition."
    "</p></article></body></html>"
)

FULL_PDF_TEXT = (
    "This is the complete body of the paper as extracted from the PDF. "
) * 40


def test_text_keeps_pdf_when_html_is_a_degenerate_stub(downloader, mocker):
    # Given a stub HTML page that extracts cleanly but carries no paper, and
    # complete PDF text already in hand from the same call
    fetch_html = mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(
            DEGENERATE_ARXIV_HTML_STUB,
            "https://arxiv.org/html/2301.12345",
        ),
    )
    mocker.patch.object(
        downloader,
        "extract_text_from_pdf",
        return_value=FULL_PDF_TEXT,
    )
    pdf_download = mocker.patch.object(
        downloader,
        "_download_pdf",
        side_effect=AssertionError("PDF already in hand was redownloaded"),
    )
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When text is produced with those PDF bytes supplied
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2301.12345",
        pdf_content=b"%PDF-complete",
    )

    # Then the stub loses to the PDF text it would otherwise have replaced
    assert result is not None
    assert result.source is ArxivTextSource.LOCAL_PDF
    assert result.text == (
        f"{FULL_PDF_TEXT.rstrip()}\n\nSource: https://arxiv.org/abs/2301.12345"
    )
    fetch_html.assert_called_once_with(
        "https://arxiv.org/html/2301.12345", max_attempts=1
    )
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()


def test_text_falls_back_to_pdf_when_a_stub_arrives_with_no_pdf_in_hand(
    downloader, mocker
):
    """The ratio guard cannot fire when there is no PDF text to compare to.

    This is the path every text-first caller takes -- ContentFetcher.fetch,
    pipeline.fetch_and_extract, and download_service's first-ever text
    extraction all call in with ``pdf_content=None``. Before the standalone
    floor, ``_html_text_beats_pdf_text`` returned True immediately for those,
    so a stub was returned as ARXIV_HTML and persisted as high quality while
    the PDF was never fetched at all.
    """
    # Given the same stub, but with no PDF bytes supplied by the caller
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(
            DEGENERATE_ARXIV_HTML_STUB,
            "https://arxiv.org/html/2301.12345",
        ),
    )
    pdf_download = mocker.patch.object(
        downloader, "_download_pdf", return_value=b"%PDF-complete"
    )
    mocker.patch.object(
        downloader, "extract_text_from_pdf", return_value=FULL_PDF_TEXT
    )
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When text is produced without PDF bytes
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2301.12345"
    )

    # Then the stub is refused and the PDF is fetched instead -- the
    # behaviour these paths had before HTML-first existed.
    assert result is not None
    assert result.source is ArxivTextSource.LOCAL_PDF
    assert result.text == (
        f"{FULL_PDF_TEXT.rstrip()}\n\nSource: https://arxiv.org/abs/2301.12345"
    )
    pdf_download.assert_called_once()
    api_fetch.assert_not_called()


def test_text_rejects_html_served_from_a_redirected_url(downloader, mocker):
    # Given a full-length HTML body that was served from the abstract page
    # rather than from the requested HTML rendition
    redirected_html = (
        "<html><body><article><p>"
        + (
            "The abstract landing page repeats enough prose that content "
            "extraction succeeds and returns a long document. "
        )
        * 20
        + "</p></article></body></html>"
    )
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(redirected_html, "https://arxiv.org/abs/2301.12345"),
    )
    mocker.patch.object(
        downloader, "extract_text_from_pdf", return_value="Authoritative PDF"
    )
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    # When text is produced
    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2301.12345",
        pdf_content=b"%PDF-complete",
    )

    # Then the redirected body is refused regardless of its length
    assert result is not None
    assert result.source is ArxivTextSource.LOCAL_PDF
    assert result.text == (
        "Authoritative PDF\n\nSource: https://arxiv.org/abs/2301.12345"
    )
    api_fetch.assert_not_called()


@pytest.mark.parametrize(
    "final_url",
    [
        "https://arxiv.org/html/9999.99999v2",
        "https://arxiv.org/html/2301.12345v3",
        "https://arxiv.org/html/2301.12345",
        "https://ar5iv.org/html/2301.12345v2",
        "https://arxiv.org/html/not-an-id",
        "https://arxiv.org.evil.example/html/2301.12345v2",
        "http://arxiv.org/html/2301.12345v2",
        "https://arxiv.org:8443/html/2301.12345v2",
        "https://[email protected]/html/2301.12345v2",
    ],
)
def test_html_identity_failure_preserves_supplied_pdf(
    downloader, mocker, final_url
):
    """A different source cannot replace the PDF under its citation."""
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=("<html>Unrelated paper</html>", final_url),
    )
    extract_html = mocker.patch.object(downloader, "_extract_content")
    mocker.patch.object(
        downloader, "extract_text_from_pdf", return_value="Requested PDF text"
    )
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    result = downloader.download_text_with_source(
        "https://arxiv.org/abs/2301.12345v2", pdf_content=b"%PDF-existing"
    )
    assert result is not None
    assert result.source is ArxivTextSource.LOCAL_PDF
    assert result.text == (
        "Requested PDF text\n\nSource: https://arxiv.org/abs/2301.12345v2"
    )
    extract_html.assert_not_called()
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()


@pytest.mark.parametrize(
    ("final_url", "accepted"),
    [
        ("https://ar5iv.labs.arxiv.org/html/2301.12345v2", True),
        ("https://ar5iv.org/html/2301.12345v2", False),
    ],
)
def test_ar5iv_acceptance_follows_the_arxiv_org_trust_boundary(
    downloader, mocker, final_url, accepted
):
    # Given a rendition served from an ar5iv host. The boundary the identity
    # check enforces is the ``.arxiv.org`` suffix, so the host the real ar5iv
    # service runs on is inside it while the bare ar5iv.org domain -- an
    # input identifier only -- is not.
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=("<html>Paper</html>", final_url),
    )
    extract = mocker.patch.object(
        downloader,
        "_extract_content",
        return_value={"content": "rendition text"},
    )

    # When the HTML rendition is requested
    result = downloader._download_html_text("2301.12345v2")

    # Then only the in-boundary host is treated as authoritative
    assert result == ("rendition text" if accepted else None)
    assert extract.called is accepted


@pytest.mark.parametrize(
    ("requested_id", "returned_id"),
    [
        ("2301.12345", "2301.12345v3"),
        ("2301.12345v2", "2301.12345v2"),
        ("math.AG/0601001", "math.AG/0601001v3"),
        ("math.AG/0601001v3", "math.AG/0601001v3"),
    ],
)
def test_html_accepts_matching_rendition(
    downloader, mocker, requested_id, returned_id
):
    """Keep valid canonical redirects, including legacy paper identifiers."""
    mocker.patch.object(
        downloader,
        "_fetch_html_with_final_url",
        return_value=(
            "<html>Paper</html>",
            f"https://arxiv.org/html/{returned_id}",
        ),
    )
    extract = mocker.patch.object(
        downloader,
        "_extract_content",
        return_value={"content": "matching paper"},
    )
    assert downloader._download_html_text(requested_id) == "matching paper"
    extract.assert_called_once_with(
        mocker.ANY, f"https://arxiv.org/abs/{requested_id}"
    )


# ---------------------------------------------------------------------------
# TeX sources on a <math> element (#6414) and the anchored matchers (#6418)
# ---------------------------------------------------------------------------


def _page_with(formula: str) -> str:
    """One rendition carrying `formula`, with enough prose to survive the
    shared extraction pipeline."""
    prose = (
        "This article carries typeset equations and enough surrounding prose "
        "that the shared extraction pipeline keeps the body. Each equation is "
        "followed by discussion of the bounds it implies. "
    ) * 8
    return (
        "<html><head><title>Equation</title></head>"
        "<body><article><p>" + prose + formula + "</p></article></body></html>"
    )


def test_alttext_is_read_when_the_annotation_child_is_absent(downloader):
    # Given a rendition whose TeX lives only in alttext. LaTeXML always writes
    # it there; the parallel <annotation> markup depends on how arXiv invokes
    # LaTeXML, so this shape used to take the PDF exit with its TeX in hand.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with('<math alttext="x^2"><mi>x</mi></math>'), "lxml"
    )

    # When the rewrite runs
    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    # Then the page is kept and the TeX is in the text
    assert rewritten is True
    assert soup.find_all("math") == []
    assert "x^2" in soup.get_text()


def test_alttext_is_read_on_a_namespace_prefixed_element(downloader):
    # The shape #5617 widened the matcher for, carrying only annotation-xml --
    # a different encoding, and not a TeX source.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            '<m:math alttext="\\alpha"><m:semantics><m:mi>a</m:mi>'
            '<m:annotation-xml encoding="MathML-Content"><m:ci>a</m:ci>'
            "</m:annotation-xml></m:semantics></m:math>"
        ),
        "lxml",
    )

    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    assert rewritten is True
    assert "\\alpha" in soup.get_text()


def test_an_empty_annotation_falls_through_to_alttext(downloader):
    # An element can carry an empty annotation and a usable attribute, and
    # there is no reason to prefer the empty one.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            '<math alttext="x^2"><semantics>'
            '<annotation encoding="application/x-tex"></annotation>'
            "</semantics></math>"
        ),
        "lxml",
    )

    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    assert rewritten is True
    assert "x^2" in soup.get_text()


def test_the_annotation_still_wins_over_alttext(downloader):
    # Order matters and must be observable: the annotation is the explicit
    # source, so a disagreement resolves to it.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            '<math alttext="FROM_ALTTEXT"><semantics>'
            '<annotation encoding="application/x-tex">FROM_ANNOTATION'
            "</annotation></semantics></math>"
        ),
        "lxml",
    )

    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    assert rewritten is True
    text = soup.get_text()
    assert "FROM_ANNOTATION" in text
    assert "FROM_ALTTEXT" not in text


def test_neither_source_still_falls_back_to_the_pdf(downloader):
    # The exit is narrower now, not gone: a <math> with no annotation, no
    # alttext, and no visible MathML text carries nothing at all, and the
    # MathML must not be left in the text. A node with rendered characters
    # no longer takes this exit -- it converts through the visible-text
    # tier -- which test_math_nodes_convert_through_every_fallback_tier
    # pins from the other side.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(_page_with("<math></math>"), "lxml")

    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    assert rewritten is False
    # The comment above promises the MathML is not left behind; assert it,
    # rather than trusting that returning False is enough.
    assert "<mi>" not in soup.get_text()
    assert "<math" not in soup.get_text()


def test_an_empty_annotation_falls_through_to_a_later_one(downloader):
    """``find`` returns the FIRST matching descendant, so an element carrying
    an empty x-tex annotation ahead of a usable one used to take the PDF exit
    with its TeX sitting right there.
    """
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            "<math><semantics>"
            '<annotation encoding="application/x-tex"></annotation>'
            '<annotation encoding="application/x-tex">x^2</annotation>'
            "</semantics></math>"
        ),
        "lxml",
    )

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    assert "x^2" in soup.get_text()


def test_an_empty_alttext_is_not_a_tex_source(downloader):
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with('<math alttext="   "><mi>QVAR</mi></math>'), "lxml"
    )

    # The whitespace alttext is skipped and the visible MathML text is used
    # instead, so the element converts rather than taking the PDF exit --
    # but through "QVAR", never through the empty attribute. The token is
    # absent from the page prose, so the check can fail.
    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    assert "QVAR" in soup.get_text()


def test_the_local_name_matchers_are_anchored(downloader):
    """#6418: the matchers are `(?:^|:)math$` and `(?:^|:)annotation$` so that
    `<m:math>` is rewritten alongside `<math>`. Nothing pinned the anchors --
    replacing both with unanchored `math` / `annotation` left every test green,
    although the unanchored form would rewrite `<mathjax>` and would read
    `<annotation-xml>` as a TeX annotation.
    """
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            "<mathjax>NOT_MATH</mathjax>"
            '<math alttext="real"><semantics>'
            '<annotation-xml encoding="MathML-Content">NOT_TEX</annotation-xml>'
            "</semantics></math>"
        ),
        "lxml",
    )

    rewritten = downloader._rewrite_math_to_tex(soup, "2501.12345v2")

    assert rewritten is True
    # `<mathjax>` is not a <math> element: it survives untouched.
    assert soup.find("mathjax") is not None
    assert "NOT_MATH" in soup.get_text()
    # `<annotation-xml>` is not a TeX annotation: alttext supplied the TeX.
    assert "real" in soup.get_text()
    assert "NOT_TEX" not in soup.get_text()


def test_visible_text_tier_excludes_hidden_annotation_text(downloader):
    # A node with rendered MathML beside an annotation-xml: no TeX
    # annotation of either encoding and no alttext, so the visible-text
    # tier runs. It must yield only the rendered characters -- the hidden
    # Content MathML inside <annotation-xml> (and inside a plain
    # <annotation> of another encoding) must never surface in the
    # equation. Red under reverting the decompose: the hidden text is
    # glued onto the visible symbol ("XVAR HIDDEN_CI").
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            "<math><semantics><mrow><mi>XVAR</mi></mrow>"
            '<annotation-xml encoding="MathML-Content"><ci>HIDDEN_CI'
            "</ci></annotation-xml></semantics></math>"
            "<m:math><m:semantics><m:mi>YVAR</m:mi>"
            '<m:annotation-xml encoding="MathML-Content"><m:ci>PREFIXED_HIDDEN'
            "</m:annotation-xml></m:semantics></m:math>"
            "<math><semantics><mrow><mi>ZVAR</mi></mrow>"
            '<annotation encoding="application/x-asy">ASY_HIDDEN'
            "</annotation></semantics></math>"
        ),
        "lxml",
    )

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    text = soup.get_text()
    # Tokens absent from the page prose, so each check can fail.
    assert "XVAR" in text and "YVAR" in text and "ZVAR" in text
    assert "HIDDEN_CI" not in text
    assert "PREFIXED_HIDDEN" not in text
    assert "ASY_HIDDEN" not in text


def test_hidden_annotation_only_text_takes_the_pdf_exit(downloader):
    # A node whose ONLY text lives inside annotation-xml carries no
    # rendered characters. The visible-text tier must read it as empty
    # (the annotation is decomposed out of the copy first) so the node is
    # genuinely empty and the rewrite takes the PDF exit, instead of
    # emitting the hidden Content MathML text as the equation and keeping
    # the page. Red under reverting the decompose: the hidden text
    # converts the element and stays in the page.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            "<math><semantics>"
            '<annotation-xml encoding="MathML-Content"><ci>ONLY_HIDDEN'
            "</ci></annotation-xml></semantics></math>"
        ),
        "lxml",
    )

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is False


def test_nested_math_is_rewritten_once_per_outermost_element(
    downloader, mocker
):
    # Given a chain of nested <math> elements with no TeX anywhere, so every
    # level would fall to the visible-text tier. Visiting each level re-reads
    # the whole chain below it, which is quadratic in the depth; only the
    # outermost element is rewritten, and its text covers the chain. Red
    # under iterating every find_all match: the ladder runs once per level.
    from bs4 import BeautifulSoup

    depth = 50
    soup = BeautifulSoup(
        _page_with(
            "<math><mrow><mi>NVAR</mi>" * depth + "</mrow></math>" * depth
        ),
        "lxml",
    )
    ladder = mocker.spy(ArxivDownloader, "_tex_for_math_element")

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    assert ladder.call_count == 1
    assert soup.find_all("math") == []
    assert soup.get_text().count("NVAR") == depth


def test_nested_math_without_tex_is_covered_by_its_ancestor(downloader):
    # An inner <math> carrying nothing sits inside an outer one whose
    # alttext supplies the TeX: the outer element's TeX stands for the
    # whole subtree, so the page is kept rather than sent to the PDF.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            '<math alttext="OUTER_TEX"><mrow><math></math></mrow></math>'
        ),
        "lxml",
    )

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    assert "OUTER_TEX" in soup.get_text()
    assert soup.find_all("math") == []


@pytest.mark.parametrize(
    "encoding", ("application/x-tex", "application/x-tex+html")
)
def test_nested_tex_annotations_are_read_once_per_outermost_match(
    downloader, mocker, encoding
):
    # Given a chain of nested, empty same-encoding annotations: reading the
    # text of every level re-reads the whole chain below it, quadratic in
    # the depth. An inner annotation's text is a subset of its ancestor's,
    # so only the outermost match is read, and the empty chain falls
    # through to alttext. Red under iterating every find_all match: the
    # text is read once per level.
    from bs4 import BeautifulSoup
    from bs4.element import Tag

    depth = 200
    soup = BeautifulSoup(
        _page_with(
            '<math alttext="ALT_TEX"><semantics><mi>v</mi>'
            + f'<annotation encoding="{encoding}">' * depth
            + "</annotation>" * depth
            + "</semantics></math>"
        ),
        "lxml",
    )
    assert len(soup.find_all("annotation")) == depth
    get_text = mocker.spy(Tag, "get_text")

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    assert get_text.call_count == 1
    assert "ALT_TEX" in soup.get_text()


def test_tex_annotation_after_an_empty_nested_run_is_still_read(downloader):
    # Outermost-only reading still examines every disjoint match in
    # document order: an empty nested run does not hide a usable sibling.
    from bs4 import BeautifulSoup

    soup = BeautifulSoup(
        _page_with(
            '<math alttext="ALT_TEX"><semantics>'
            '<annotation encoding="application/x-tex">'
            '<annotation encoding="application/x-tex"> </annotation>'
            "</annotation>"
            '<annotation encoding="application/x-tex">SIBLING_TEX</annotation>'
            "</semantics></math>"
        ),
        "lxml",
    )

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    text = soup.get_text()
    assert "SIBLING_TEX" in text
    assert "ALT_TEX" not in text


def test_visible_text_tier_does_not_deepcopy(downloader, mocker):
    # bs4's Tag.__deepcopy__ re-walks the copy on every insert, quadratic
    # in the depth of nested markup; the visible-text tier walks the
    # element in place instead, skipping annotation subtrees, and leaves
    # the tree untouched until the rewrite itself.
    from bs4 import BeautifulSoup
    from bs4.element import Tag

    soup = BeautifulSoup(
        _page_with(
            "<math><semantics><mrow><mi>WVAR</mi><!-- CMT_HIDDEN --></mrow>"
            '<annotation-xml encoding="MathML-Content"><ci>XML_HIDDEN</ci>'
            "</annotation-xml></semantics></math>"
        ),
        "lxml",
    )
    deepcopy = mocker.spy(Tag, "__deepcopy__")

    assert downloader._rewrite_math_to_tex(soup, "2501.12345v2") is True
    deepcopy.assert_not_called()
    text = soup.get_text()
    assert "WVAR" in text
    assert "XML_HIDDEN" not in text
    assert "CMT_HIDDEN" not in text


def test_download_full_text_reports_html_text(downloader, mocker):
    from local_deep_research.research_library.downloaders.arxiv import (
        ArxivFullTextOutcome,
        ArxivFullTextStatus,
    )

    mocker.patch.object(
        downloader, "_download_html_text", return_value="H" * 3000
    )
    pdf_download = mocker.patch.object(downloader, "_download_pdf")
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    outcome = downloader.download_full_text("https://arxiv.org/abs/2301.12345")

    assert outcome == ArxivFullTextOutcome(
        ArxivFullTextStatus.TEXT,
        ArxivTextResult("H" * 3000, ArxivTextSource.ARXIV_HTML),
    )
    pdf_download.assert_not_called()
    api_fetch.assert_not_called()


@pytest.mark.parametrize(
    ("pdf_bytes", "pdf_text", "expected_status", "extracted"),
    (
        pytest.param(
            b"%PDF-scan", None, "PDF_WITHOUT_TEXT", True, id="pdf_no_text"
        ),
        pytest.param(
            b"%PDF-scan", "", "PDF_WITHOUT_TEXT", True, id="pdf_empty_text"
        ),
        pytest.param(None, "unused", "NOT_FETCHED", False, id="pdf_failed"),
    ),
)
def test_download_full_text_tells_no_text_from_a_failed_fetch(
    downloader, mocker, pdf_bytes, pdf_text, expected_status, extracted
):
    # A PDF that arrived but yielded no text is PDF_WITHOUT_TEXT; no PDF
    # bytes, with no failed request reported, is NOT_FETCHED. The API leg
    # is never asked either way.
    from local_deep_research.research_library.downloaders.arxiv import (
        ArxivFullTextOutcome,
        ArxivFullTextStatus,
    )

    mocker.patch.object(downloader, "_download_html_text", return_value=None)
    mocker.patch.object(downloader, "_download_pdf", return_value=pdf_bytes)
    extract = mocker.patch.object(
        downloader, "extract_text_from_pdf", return_value=pdf_text
    )
    api_fetch = mocker.patch.object(downloader, "_fetch_from_arxiv_api")

    outcome = downloader.download_full_text("https://arxiv.org/abs/2301.12345")

    assert outcome == ArxivFullTextOutcome(ArxivFullTextStatus[expected_status])
    assert extract.called is extracted
    api_fetch.assert_not_called()


def test_download_full_text_rejects_a_url_naming_no_paper(downloader, mocker):
    from local_deep_research.research_library.downloaders.arxiv import (
        ArxivFullTextStatus,
    )

    html = mocker.patch.object(downloader, "_download_html_text")

    outcome = downloader.download_full_text("https://example.org/nothing")

    assert outcome.status is ArxivFullTextStatus.NOT_FETCHED
    assert outcome.result is None
    html.assert_not_called()


@pytest.mark.parametrize("status_code", (429, 503))
def test_download_full_text_makes_one_pdf_attempt_when_rate_limited(
    mocker, status_code
):
    # Under arXiv rate limiting the full-text fetch must not retry the PDF
    # into the limit: one HTML request, one PDF request, then RATE_LIMITED
    # so a caller fetching several papers can stop.
    from unittest.mock import Mock

    from local_deep_research.research_library.downloaders.arxiv import (
        ARXIV_EXPORT_HOST,
        ArxivFullTextStatus,
    )

    export = ArxivDownloader(fetch_host=ARXIV_EXPORT_HOST)
    export.rate_tracker = Mock()
    export.rate_tracker.apply_rate_limit.return_value = 0
    requested = []

    def fake_get(url, **kwargs):
        requested.append(url)
        return Mock(status_code=status_code, headers={})

    mocker.patch.object(export.session, "get", side_effect=fake_get)
    api_fetch = mocker.patch.object(export, "_fetch_from_arxiv_api")

    outcome = export.download_full_text("https://arxiv.org/abs/2301.12345")

    assert outcome.status is ArxivFullTextStatus.RATE_LIMITED
    assert outcome.result is None
    assert requested == [
        "https://export.arxiv.org/html/2301.12345",
        "https://export.arxiv.org/pdf/2301.12345",
    ]
    api_fetch.assert_not_called()
    export.close()


def _requests_error(kind):
    import requests

    return {
        "timeout": requests.exceptions.Timeout("export.arxiv.org hung"),
        "connection": requests.exceptions.ConnectionError("unreachable"),
        # SafeSession's refusal shape, also raised on a failed DNS lookup
        "refused": ValueError(
            "URL failed security validation (possible SSRF): "
            "https://export.arxiv.org/pdf/2301.12345"
        ),
        "unexpected": RuntimeError("boom"),
    }[kind]


@pytest.mark.parametrize(
    ("html_answer", "pdf_answer", "expected_status"),
    (
        pytest.param("timeout", "timeout", "FETCH_FAILED", id="host_hung"),
        pytest.param(
            "connection", "connection", "FETCH_FAILED", id="host_unreachable"
        ),
        pytest.param(500, 502, "FETCH_FAILED", id="both_5xx"),
        pytest.param(504, 404, "FETCH_FAILED", id="html_5xx_pdf_404"),
        pytest.param("timeout", 404, "FETCH_FAILED", id="html_hung_pdf_404"),
        pytest.param(404, 500, "FETCH_FAILED", id="html_404_pdf_5xx"),
        pytest.param("refused", "refused", "FETCH_FAILED", id="dns_failure"),
        pytest.param(404, "refused", "FETCH_FAILED", id="html_404_pdf_dns"),
        pytest.param(
            404, "unexpected", "FETCH_FAILED", id="html_404_pdf_unexpected"
        ),
        pytest.param(404, 404, "NOT_FETCHED", id="both_absent"),
        pytest.param(410, 410, "NOT_FETCHED", id="both_gone"),
        pytest.param(404, "not_pdf", "NOT_FETCHED", id="pdf_not_a_pdf"),
    ),
)
def test_download_full_text_tells_a_failed_request_from_an_answer(
    mocker, html_answer, pdf_answer, expected_status, loguru_caplog
):
    # NOT_FETCHED (refundable) only when arXiv answered both legs; any leg
    # that failed instead of being answered is FETCH_FAILED, so a caller
    # fetching several papers stops rather than paying a timeout per
    # paper. Red under reporting every no-bytes ending as NOT_FETCHED.
    from unittest.mock import Mock

    from local_deep_research.research_library.downloaders.arxiv import (
        ARXIV_EXPORT_HOST,
        ArxivFullTextStatus,
    )

    export = ArxivDownloader(fetch_host=ARXIV_EXPORT_HOST)
    export.rate_tracker = Mock()
    export.rate_tracker.apply_rate_limit.return_value = 0
    requested = []

    def answer(spec):
        if isinstance(spec, str) and spec != "not_pdf":
            raise _requests_error(spec)
        if spec == "not_pdf":
            return Mock(
                status_code=200,
                headers={"content-type": "text/html"},
                content=b"<html>no pdf here</html>",
                raw=None,
            )
        return Mock(status_code=spec, headers={})

    def fake_get(url, **kwargs):
        requested.append(url)
        return answer(html_answer if "/html/" in url else pdf_answer)

    mocker.patch.object(export.session, "get", side_effect=fake_get)
    api_fetch = mocker.patch.object(export, "_fetch_from_arxiv_api")

    with loguru_caplog.at_level("DEBUG"):
        outcome = export.download_full_text("https://arxiv.org/abs/2301.12345")

    assert outcome.status is ArxivFullTextStatus[expected_status]
    assert outcome.result is None
    # One HTML and one (unretried) PDF request, whatever the ending
    assert requested == [
        "https://export.arxiv.org/html/2301.12345",
        "https://export.arxiv.org/pdf/2301.12345",
    ]
    api_fetch.assert_not_called()
    # The caller handles every ending, so none is logged at ERROR (ERROR
    # records reach the user's browser via frontend_progress_sink).
    assert not [r for r in loguru_caplog.records if r.levelno >= 40]
    export.close()


def test_library_text_path_keeps_pdf_retries_when_rate_limited(mocker):
    # The single-attempt PDF leg is specific to download_full_text; the
    # library's text path keeps BaseDownloader's three attempts.
    from unittest.mock import Mock

    downloader = ArxivDownloader()
    downloader.rate_tracker = Mock()
    downloader.rate_tracker.apply_rate_limit.return_value = 0
    requested = []

    def fake_get(url, **kwargs):
        requested.append(url)
        return Mock(status_code=429, headers={})

    mocker.patch.object(downloader.session, "get", side_effect=fake_get)
    mocker.patch.object(downloader, "_fetch_from_arxiv_api", return_value=None)

    assert (
        downloader.download_text_with_source("https://arxiv.org/abs/2301.12345")
        is None
    )
    pdf_requests = [url for url in requested if "/pdf/" in url]
    assert pdf_requests == ["https://arxiv.org/pdf/2301.12345.pdf"] * 3
    downloader.close()


def test_export_host_downloader_fetches_html_and_pdf_from_export(mocker):
    # arXiv asks programmatic clients to use export.arxiv.org. Its PDF URL
    # carries no ".pdf" suffix, which that host would answer with an
    # unnecessary 301.
    from unittest.mock import Mock

    from local_deep_research.research_library.downloaders.arxiv import (
        ARXIV_EXPORT_HOST,
    )

    export = ArxivDownloader(fetch_host=ARXIV_EXPORT_HOST)
    export.rate_tracker = Mock()
    requested = []

    def fake_get(url, **kwargs):
        requested.append(url)
        return Mock(status_code=404, headers={})

    mocker.patch.object(export.session, "get", side_effect=fake_get)
    mocker.patch.object(export, "_fetch_from_arxiv_api")

    export.download_full_text("https://arxiv.org/abs/2301.12345v2")

    assert requested[0] == "https://export.arxiv.org/html/2301.12345v2"
    assert requested[1:] and all(
        url == "https://export.arxiv.org/pdf/2301.12345v2"
        for url in requested[1:]
    )
    export.close()


def test_default_downloader_keeps_arxiv_org_urls():
    downloader = ArxivDownloader()
    try:
        assert (
            downloader._pdf_url("2301.12345")
            == "https://arxiv.org/pdf/2301.12345.pdf"
        )
    finally:
        downloader.close()


def test_fetch_host_outside_arxiv_is_rejected():
    with pytest.raises(ValueError):
        ArxivDownloader(fetch_host="evil.example")