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.

8.8 KB
Raw Blame
"""Regression tests: ``_download_pdf`` rolls back the *shared* session on a
success-path failure.

``_download_pdf`` receives the session owned by its caller ``download_resource``
and that caller keeps using it — it records a retry attempt, updates the queue
row, and commits — after ``_download_pdf`` returns. ``_download_pdf``'s broad
``except`` swallows the error and returns ``False`` rather than propagating, so
the rollback added to ``get_user_db_session`` cannot see it. If a DB error in
the success path (flush / save_pdf / ensure_in_collection) leaves the session in
``PendingRollbackError``, the caller's later commit cascades.

The fix rolls the shared session back inside that ``except`` and re-records the
failed ``DownloadAttempt`` (the rollback discards the pending one), so the
caller inherits a clean, committable session.
"""

import uuid
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.download_tracker import (
    DownloadAttempt,
    DownloadTracker,
)
from local_deep_research.database.models.library import SourceType
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.research_library.services.download_service import (
    DownloadService,
)

MODULE = "local_deep_research.research_library.services.download_service"


@pytest.fixture
def svc():
    """DownloadService stub wired just enough to reach the success path."""
    with patch.object(DownloadService, "__init__", lambda self, *a, **kw: None):
        s = DownloadService.__new__(DownloadService)
        s.username = "test_user"
        s.password = "test_pass"
        s.library_root = "/tmp/ldr-test-library"
        s._closed = False
        settings = MagicMock()
        settings.get_setting = lambda key, default=None: {
            "research_library.pdf_storage_mode": "none",
            "research_library.max_pdf_size_mb": 50,
        }.get(key, default)
        s.settings = settings
        # Allow every URL through the egress gate.
        s._check_url_against_policy = lambda url: (True, "ok")
        # One downloader that "succeeds" with PDF bytes -> enters success path.
        downloader = MagicMock()
        downloader.can_handle.return_value = True
        downloader.download_with_result.return_value = MagicMock(
            is_success=True,
            content=b"%PDF-1.4 fake",
            status_code=200,
            skip_reason=None,
        )
        s.downloaders = [downloader]
        return s


def test_download_pdf_rolls_back_shared_session_and_records_attempt(svc):
    """A success-path failure must roll back the shared session and still add a
    failed DownloadAttempt for the caller to commit.
    """
    session = MagicMock()
    resource = MagicMock(url="https://arxiv.org/pdf/2401.0001", id=7)
    tracker = MagicMock(url_hash="abc123")
    # MagicMock auto-creates download_attempts; make .count() a real int so the
    # attempt_number arithmetic doesn't blow up.
    tracker.download_attempts.count.return_value = 0

    with patch(f"{MODULE}.PDFStorageManager"):
        with patch(
            f"{MODULE}.get_document_for_resource",
            side_effect=RuntimeError("simulated flush failure"),
        ):
            success, error_msg, status_code = svc._download_pdf(
                resource, tracker, session
            )

    assert success is False
    assert status_code is None
    session.rollback.assert_called_once()

    # The failed attempt was re-recorded on the now-clean session.
    added = [c.args[0] for c in session.add.call_args_list if c.args]
    failed_attempts = [
        a
        for a in added
        if isinstance(a, DownloadAttempt) and a.succeeded is False
    ]
    assert failed_attempts, "a failed DownloadAttempt should be recorded"
    assert tracker.is_accessible is False


def test_download_pdf_real_session_recovered_after_db_error(svc):
    """End-to-end with a real SQLite session: after a success-path DB error
    poisons the shared session, ``_download_pdf`` returns it clean so the
    caller's subsequent commit succeeds (it would otherwise cascade).
    """
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    session.add(
        SourceType(
            id=str(uuid.uuid4()),
            name="research_download",
            display_name="Research Download",
        )
    )
    rh = ResearchHistory(
        id=str(uuid.uuid4()),
        query="q",
        mode="quick",
        status="completed",
        created_at="2026-05-09T00:00:00",
    )
    session.add(rh)
    # Row whose primary key the poison step will collide with.
    session.add(SourceType(id="dup", name="dup", display_name="Dup"))
    session.commit()

    resource = ResearchResource(
        research_id=rh.id,
        title="Test Paper",
        url="https://arxiv.org/pdf/2401.0002",
        source_type="academic",
        created_at="2026-05-09T00:00:00",
    )
    session.add(resource)
    session.commit()
    tracker = DownloadTracker(
        url=resource.url,
        url_hash="hash-2401-0002",
        first_resource_id=resource.id,
        is_downloaded=False,
    )
    session.add(tracker)
    session.commit()

    def poison(sess, res):
        # Mimic a failed flush deep in the success path: a duplicate primary
        # key leaves the real session in PendingRollbackError.
        sess.add(SourceType(id="dup", name="dup2", display_name="Dup2"))
        sess.flush()

    with patch(f"{MODULE}.PDFStorageManager"):
        with patch(f"{MODULE}.get_document_for_resource", side_effect=poison):
            success, error_msg, status_code = svc._download_pdf(
                resource, tracker, session
            )

    assert success is False

    # The caller commits the shared session after _download_pdf returns.
    # Pre-fix this raised PendingRollbackError; now it succeeds.
    session.commit()

    recorded = (
        session.query(DownloadAttempt)
        .filter_by(url_hash="hash-2401-0002", succeeded=False)
        .count()
    )
    assert recorded == 1

    session.close()
    engine.dispose()


def test_try_api_text_extraction_rolls_back_shared_session(svc):
    """Sibling swallow-then-reuse site in the download_as_text chain: a failed
    save/commit in _try_api_text_extraction must roll back the shared session
    so download_as_text's subsequent reuse (_record_retry_attempt) doesn't
    cascade. Like _download_pdf, the except swallows and returns, so the
    get_user_db_session rollback can't see it.
    """
    engine = create_engine("sqlite:///:memory:")
    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()

    rh = ResearchHistory(
        id=str(uuid.uuid4()),
        query="q",
        mode="quick",
        status="completed",
        created_at="2026-05-09T00:00:00",
    )
    session.add(rh)
    # Row whose primary key the poison step collides with. poison() MUST do a
    # real failed flush (not a bare ``raise IntegrityError``): a real flush
    # both poisons the session AND expires ``resource``, so the except's
    # ``logger.exception(... resource.id)`` would refresh resource.id on the
    # poisoned session and re-raise PendingRollbackError unless safe_rollback
    # runs FIRST. That ordering is exactly what this test pins — keep the
    # poison a real flush or the ordering coverage silently disappears.
    session.add(SourceType(id="dup", name="dup", display_name="Dup"))
    session.commit()

    resource = ResearchResource(
        research_id=rh.id,
        title="Test Paper",
        url="https://arxiv.org/abs/2401.0003",
        source_type="academic",
        created_at="2026-05-09T00:00:00",
    )
    session.add(resource)
    session.commit()

    # Downloader returns text content so we reach the save+commit path.
    downloader = MagicMock()
    downloader.download_with_result.return_value = MagicMock(
        is_success=True, content=b"some extracted text"
    )
    svc._get_downloader = lambda url: downloader

    def poison(*args, **kwargs):
        # Mimic a failed flush/commit in the save path on the shared session.
        session.add(SourceType(id="dup", name="dup2", display_name="Dup2"))
        session.flush()

    with patch.object(svc, "_save_text_with_db", side_effect=poison):
        result = svc._try_api_text_extraction(session, resource)

    # Swallowed into a (False, message) tuple, not raised.
    assert result is not None
    assert result[0] is False

    # download_as_text reuses the shared session next; pre-fix this raised
    # PendingRollbackError, now it succeeds.
    session.add(SourceType(id="ok", name="ok", display_name="OK"))
    session.commit()
    assert session.query(SourceType).filter_by(id="ok").count() == 1

    session.close()
    engine.dispose()