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.

9.3 KB
Raw Blame
"""Regression tests for issue #4691: the atomic download-queue claim.

Two simultaneous ``/library/api/download-bulk`` requests for the same user
(the user clicks Download twice, or a second bulk run starts while the first
is still going) both read the ``PENDING`` rows and, with no claim, both call
``download_resource`` for every one of them. The ``COMPLETED``-Document dedup
inside ``download_resource`` only guards the state *after* one stream
finishes, so during the in-flight window both streams download the same
resource.

The fix flips each row ``PENDING -> PROCESSING`` in a single conditional
UPDATE and checks the affected row count: exactly one stream sees ``1`` and
wins, the loser sees ``0`` and skips. Everything here rests on that row count
being produced by a real database.

WHY THIS FILE EXISTS. The Flask-era suite covered this in
``tests/research_library/routes/test_download_bulk_concurrent_claim_4691.py``
(809 lines, deleted in the FastAPI migration), whose own docstring insisted:
"These tests use a real on-disk SQLite database so the conditional-UPDATE
claim runs against a real engine, not a mock." The surviving FastAPI-era
tests that mention this code (``test_download_service_3827_fix.py``,
``test_download_service_coverage.py``) drive it with ``MagicMock()`` sessions
and a canned ``queue_q.update.return_value = 1``. That mock *tells the code
its claim won*. It cannot fail if the SQL predicate stops being atomic, if the
status filter is dropped, or if the commit disappears -- which is the entire
content of the fix. So the guarantee was lost even though the code kept
working.

These tests call the production helpers directly against a real engine. They
need no HTTP client and no auth: the helpers take a plain ``Session``, which
is what makes a faithful port cheaper here than it was under Flask, where the
equivalent had to drive the whole view.
"""

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from local_deep_research.database.models import Base
from local_deep_research.database.models.library import (
    DocumentStatus,
    DownloadQueue,
)
from local_deep_research.database.models.research import (
    ResearchHistory,
    ResearchResource,
)
from local_deep_research.web.routers.library import (
    _claim_download_queue_item,
    _finalize_download_queue_item,
    _release_download_queue_item,
)

RESEARCH_ID = "r1"


def _seeded_db(tmp_path, request, status=DocumentStatus.PENDING, name="queue"):
    """A real on-disk SQLite DB holding one download-queue row.

    On-disk rather than ``:memory:`` deliberately: an in-memory database is
    per-connection, so the two-session test below could not see one
    another's committed claim and would pass vacuously.

    ``name`` keeps each call in its own database file, so a test that seeds
    several rows at different statuses does not collide on ``resource_id``,
    which is UNIQUE on this table.
    """
    engine = create_engine(f"sqlite:///{tmp_path}/{name}.db")
    request.addfinalizer(engine.dispose)
    Base.metadata.create_all(engine)
    Session = sessionmaker(bind=engine)

    session = Session()
    session.add(
        ResearchHistory(
            id=RESEARCH_ID,
            query="test query",
            mode="quick",
            status="completed",
            created_at="2026-05-09T00:00:00",
        )
    )
    resource = ResearchResource(
        research_id=RESEARCH_ID,
        title="Test Paper",
        url="https://arxiv.org/abs/2401.0001",
        source_type="academic",
        created_at="2026-05-09T00:00:00",
    )
    session.add(resource)
    session.commit()
    row = DownloadQueue(
        resource_id=resource.id, research_id=RESEARCH_ID, status=status
    )
    session.add(row)
    session.commit()
    queue_item_id = row.id
    session.close()
    return Session, queue_item_id


def _status_of(Session, queue_item_id):
    with Session() as session:
        row = session.get(DownloadQueue, queue_item_id)
        return None if row is None else row.status


# ---------------------------------------------------------------------------
# The claim
# ---------------------------------------------------------------------------


def test_claim_flips_pending_to_processing_and_reports_the_win(
    tmp_path, request
):
    Session, item = _seeded_db(tmp_path, request)
    session = Session()

    won = _claim_download_queue_item(session, item)

    assert won is True
    assert _status_of(Session, item) is DocumentStatus.PROCESSING
    session.close()


def test_two_independent_sessions_cannot_both_claim_the_same_row(
    tmp_path, request
):
    """The race fix itself, driven the only way that can actually prove it.

    Two separate ``Session``s on the same on-disk database stand in for the
    two concurrent bulk streams. The second must observe the first's
    committed claim and lose. A ``MagicMock`` session with a canned
    ``update.return_value = 1`` returns True for BOTH callers here and
    reports the bug as fixed.
    """
    Session, item = _seeded_db(tmp_path, request)
    first, second = Session(), Session()

    first_won = _claim_download_queue_item(first, item)
    second_won = _claim_download_queue_item(second, item)

    assert (first_won, second_won) == (
        True,
        False,
    ), "exactly one stream may claim a row; both winning is issue #4691"
    assert _status_of(Session, item) is DocumentStatus.PROCESSING
    first.close()
    second.close()


def test_claiming_a_missing_row_loses_rather_than_raising(tmp_path, request):
    Session, item = _seeded_db(tmp_path, request)
    session = Session()

    assert _claim_download_queue_item(session, item + 10_000) is False
    session.close()


def test_only_pending_rows_are_claimable(tmp_path, request):
    """The status filter is half the predicate; without it a row another
    stream is already downloading, or one already finished, gets re-claimed.
    """
    for status in (
        DocumentStatus.PROCESSING,
        DocumentStatus.COMPLETED,
        DocumentStatus.FAILED,
    ):
        Session, item = _seeded_db(tmp_path, request, status, name=status.value)
        with Session() as session:
            assert _claim_download_queue_item(session, item) is False, status
            assert _status_of(Session, item) is status, (
                f"a failed claim must leave a {status.value} row untouched"
            )


# ---------------------------------------------------------------------------
# Releasing a claim
# ---------------------------------------------------------------------------


def test_release_returns_a_claimed_row_to_pending(tmp_path, request):
    """A download that raised before recording a terminal status must leave
    the row retryable, matching pre-fix behaviour."""
    Session, item = _seeded_db(tmp_path, request)
    session = Session()
    _claim_download_queue_item(session, item)

    _release_download_queue_item(session, item)

    assert _status_of(Session, item) is DocumentStatus.PENDING
    session.close()


def test_release_is_scoped_to_processing_and_cannot_resurrect_a_finished_row(
    tmp_path, request
):
    """Release must only ever undo *this* stream's own claim.

    Unscoped, a late release from a stream that lost the race would drag a
    COMPLETED row back to PENDING and have it downloaded all over again.
    """
    Session, item = _seeded_db(tmp_path, request, DocumentStatus.COMPLETED)
    session = Session()

    _release_download_queue_item(session, item)

    assert _status_of(Session, item) is DocumentStatus.COMPLETED
    session.close()


# ---------------------------------------------------------------------------
# Finalising a claim
# ---------------------------------------------------------------------------


def test_finalize_success_completes_a_claimed_row(tmp_path, request):
    """Paths like ``mode="text_only"`` never write a terminal status of their
    own, so without this the row is stranded in PROCESSING forever -- and
    nothing in src/ reaps PROCESSING rows."""
    Session, item = _seeded_db(tmp_path, request)
    session = Session()
    _claim_download_queue_item(session, item)

    _finalize_download_queue_item(session, item, True)

    assert _status_of(Session, item) is DocumentStatus.COMPLETED
    completed = Session()
    assert completed.get(DownloadQueue, item).completed_at is not None
    completed.close()
    session.close()


def test_finalize_failure_returns_the_row_to_pending_not_failed(
    tmp_path, request
):
    """Deliberately PENDING, not FAILED: pre-fix these paths left the row
    PENDING and merely re-scanned it, and that retryability is preserved."""
    Session, item = _seeded_db(tmp_path, request)
    session = Session()
    _claim_download_queue_item(session, item)

    _finalize_download_queue_item(session, item, False)

    assert _status_of(Session, item) is DocumentStatus.PENDING
    row_session = Session()
    assert row_session.get(DownloadQueue, item).completed_at is None
    row_session.close()
    session.close()


def test_finalize_does_not_touch_a_row_that_recorded_its_own_status(
    tmp_path, request
):
    """pdf-mode rows go terminal inside ``download_resource``. Finalise must
    be a no-op for them rather than overwriting a FAILED row as COMPLETED.
    """
    Session, item = _seeded_db(tmp_path, request, DocumentStatus.FAILED)
    session = Session()

    _finalize_download_queue_item(session, item, True)

    assert _status_of(Session, item) is DocumentStatus.FAILED
    session.close()