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.

2.0 KB
Raw Blame
"""Downloaded PDFs retain their prefix when no more text can fit."""

import pytest

from local_deep_research.research_library.downloaders import base
from local_deep_research.research_library.services import download_service
from tests.document_loaders.test_bounded_pdf_bytes import _text_pdf


@pytest.fixture(params=["base", "pdfplumber", "fallback"])
def extractor(request, monkeypatch):
    if request.param == "base":
        return base, base.BaseDownloader.extract_text_from_pdf, "\n"
    if request.param == "fallback":
        from pdfplumber.page import Page

        # Exercise the real pypdf fallback after a textless first attempt.
        monkeypatch.setattr(Page, "extract_text", lambda self: "")
    service = download_service.DownloadService.__new__(
        download_service.DownloadService
    )
    return download_service, service._extract_text_from_pdf, "\n\n"


@pytest.mark.parametrize("extra_chars", [0, 1, 2])
def test_no_next_page_extraction_when_only_separator_fits(
    extractor, monkeypatch, extra_chars
):
    module, extract, separator = extractor
    if extra_chars > len(separator):
        pytest.skip("One text character still fits on this path")
    monkeypatch.setattr(module, "MAX_PDF_EXTRACTED_CHARS", 5 + extra_chars)

    text = extract(_text_pdf("Hello", "unread", invalid_page=1))

    assert text == "Hello"


def test_extract_next_page_when_one_character_still_fits(
    extractor, monkeypatch
):
    module, extract, separator = extractor
    monkeypatch.setattr(
        module, "MAX_PDF_EXTRACTED_CHARS", 5 + len(separator) + 1
    )

    text = extract(_text_pdf("Hello", "World"))

    assert text == "Hello" + separator + "W"


def test_empty_pages_do_not_spend_separator_budget(extractor, monkeypatch):
    module, extract, separator = extractor
    monkeypatch.setattr(
        module, "MAX_PDF_EXTRACTED_CHARS", 5 + len(separator) + 1
    )

    text = extract(_text_pdf("", "Hello", "", "World"))

    assert text == "Hello" + separator + "W"