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.

19.7 KB
Raw Blame
"""What the two library SSE streams actually SAY to the user.

Ported from ``tests/research_library/routes/test_library_routes_extra_coverage.py``
(``TestDownloadBulkSseStream`` / ``TestDownloadAllTextSseStream``), deleted in
the Flask->FastAPI migration.

The branch has exactly one pinned property across both generators: they
fail closed with an ``Authentication required`` event
(``tests/security/test_library_notes_authz_fastapi.py::TestSseStreamsFailClosedOnAuthFailure``).
Everything the streams emit on the *working* paths is unpinned, and each of
these was a bug once:

* ``total`` on the first event is the PENDING count taken AFTER
  ``queue_research_downloads`` populates the queue (issue #4660: counting
  first made the UI show "X / 0 files"). The count alone is not enough to
  pin this -- a stub that always answers 3 satisfies it under the old
  ordering too -- so the ORDER is asserted structurally, via a recorded
  call sequence, the same technique ``tests/web/test_pagination_bounds.py``
  uses for a property invisible in the output.
* ``total`` accumulates across research ids (``total += count``). Replacing
  it with ``total =`` is invisible with a single research id, which is all
  any test on the branch passes.
* ``total == 0`` splits into two DIFFERENT terminal messages: a queueing
  failure ("queueing failed for N of M") versus a clean empty state ("No
  new papers"). Collapsing them tells a user whose queue call crashed that
  there was simply nothing to download.
* A download exception is CLASSIFIED: paywall-family phrases become
  ``skipped`` (not the user's problem, not an error) and everything else
  becomes ``failed``. And in every branch only ``type(e).__name__`` reaches
  the client -- never ``str(e)`` (CWE-209).
* ``download_all_text`` reports per-item ``failed`` both for a ``(False,
  err)`` return and for a raise. On the branch only the terminal
  ``complete`` event of an EMPTY library is asserted, so the entire loop
  body is untested.

The handlers are called directly and the SSE body is drained: the generator
is lazy, so nothing runs until it is consumed. This is the pattern already
used by ``test_library_download_outcomes.py``.
"""

import asyncio
import json
from contextlib import contextmanager
from unittest.mock import MagicMock, Mock, patch

import pytest
from starlette.requests import Request

from local_deep_research.web.routers import library as library_module
from local_deep_research.web.routers.library import (
    download_all_text,
    download_bulk,
)


# ---------------------------------------------------------------------------
# Harness
# ---------------------------------------------------------------------------


def _post_json_request(payload, path="/library/api/download-bulk"):
    body = json.dumps(payload).encode("utf-8")

    async def receive():
        return {"type": "http.request", "body": body, "more_body": False}

    return Request(
        {
            "type": "http",
            "method": "POST",
            "path": path,
            "headers": [(b"content-type", b"application/json")],
            "query_string": b"",
            "session": {"session_id": "sid"},
        },
        receive,
    )


def _sse_events(body):
    """Parse ``data: <json>`` SSE lines into a list of dicts."""
    return [
        json.loads(line[6:])
        for line in body.split("\n")
        if line.startswith("data: ")
    ]


def _drain(response):
    async def _collect():
        chunks = []
        async for chunk in response.body_iterator:
            chunks.append(
                chunk if isinstance(chunk, bytes) else str(chunk).encode()
            )
        return b"".join(chunks).decode()

    return asyncio.run(_collect())


class _QueueQuery:
    """Stands in for the ``LibraryDownloadQueue`` query chain.

    ``count()`` and ``all()`` are driven from caller-supplied callables so a
    test can record WHEN they run relative to ``queue_research_downloads``
    (issue #4660) or vary them per research id.
    """

    def __init__(self, count_fn, all_fn, update_result=1):
        self._count_fn = count_fn
        self._all_fn = all_fn
        self._update_result = update_result

    def filter_by(self, **kwargs):
        return self

    def filter(self, *args, **kwargs):
        return self

    def all(self):
        return self._all_fn()

    def count(self):
        return self._count_fn()

    def update(self, values, **kwargs):
        return self._update_result


class _BulkSession:
    """A session that answers the queue query and ``get(ResearchResource, id)``."""

    def __init__(self, queue_query, resource_title="Paper"):
        self.queue_query = queue_query
        self.resource_title = resource_title

    def query(self, *entities):
        return self.queue_query

    def get(self, model, ident):
        row = Mock()
        row.title = self.resource_title
        return row

    def commit(self):
        pass


def _queue_item(item_id, resource_id):
    row = Mock()
    row.id = item_id
    row.resource_id = resource_id
    return row


def _run_download_bulk(session, download_service, payload):
    @contextmanager
    def fake_db_session(*args, **kwargs):
        yield session

    with (
        patch.object(
            library_module, "get_user_db_session", side_effect=fake_db_session
        ),
        patch.object(
            library_module, "get_authenticated_user_password", return_value="pw"
        ),
        patch.object(
            library_module, "DownloadService", return_value=download_service
        ),
    ):
        response = asyncio.run(
            download_bulk(_post_json_request(payload), username="alice")
        )
        return _sse_events(_drain(response))


# ===========================================================================
# issue #4660 -- the initial `total` is the POST-queue count
# ===========================================================================


def test_initial_total_is_the_post_queue_count_and_the_queue_runs_first():
    """Both halves matter, and only the second one is a real guard.

    Pre-fix, ``download_bulk`` counted PENDING rows BEFORE the pre-pass
    populated them, so the first SSE event carried ``total: 0`` and the UI
    showed "X / 0 files". The count assertion alone would pass under the old
    ordering with any stub that answers 3, so the ordering is recorded and
    asserted directly -- the property is invisible in the output otherwise.
    """
    call_order = []

    def record_queue(*args, **kwargs):
        call_order.append("queue")
        return 3

    def record_count():
        call_order.append("count")
        return 3

    items = [_queue_item(i, 100 + i) for i in range(3)]
    session = _BulkSession(_QueueQuery(record_count, lambda: items))

    download_service = MagicMock()
    download_service.queue_research_downloads.side_effect = record_queue
    download_service.download_resource.return_value = (True, None)

    events = _run_download_bulk(
        session, download_service, {"research_ids": ["r1"], "mode": "pdf"}
    )

    assert events, "no SSE events emitted"
    assert events[0].get("total") == 3, (
        f"the first event must carry the post-queue count, got {events[0]}"
    )
    assert call_order == ["queue", "count"], (
        "queue_research_downloads must run BEFORE the PENDING count, or the "
        "count sees an empty queue and the UI shows 'X / 0 files' (#4660). "
        f"Got order: {call_order}"
    )


def test_total_sums_across_multiple_research_ids():
    """``total += count`` per research id. With a single research id -- all
    any test on the branch passes -- ``total = count`` is indistinguishable.
    """
    counts = iter([2, 3])
    batches = iter([[_queue_item(1, 101), _queue_item(2, 102)], []])
    session = _BulkSession(
        _QueueQuery(lambda: next(counts), lambda: next(batches))
    )

    download_service = MagicMock()
    download_service.queue_research_downloads.return_value = 0
    download_service.download_resource.return_value = (True, None)

    events = _run_download_bulk(
        session,
        download_service,
        {"research_ids": ["r1", "r2"], "mode": "pdf"},
    )

    assert events[0].get("total") == 5, (
        f"initial total should be 2+3=5 across r1+r2; events={events[:2]}"
    )
    assert download_service.queue_research_downloads.call_count == 2


# ===========================================================================
# total == 0 -- two different terminal messages
# ===========================================================================


def _run_empty_queue_bulk(queue_side_effect):
    session = _BulkSession(_QueueQuery(lambda: 0, list))
    download_service = MagicMock()
    if isinstance(queue_side_effect, BaseException) or (
        isinstance(queue_side_effect, type)
        and issubclass(queue_side_effect, BaseException)
    ):
        download_service.queue_research_downloads.side_effect = (
            queue_side_effect
        )
    else:
        download_service.queue_research_downloads.return_value = (
            queue_side_effect
        )
    return _run_download_bulk(
        session, download_service, {"research_ids": ["r1"], "mode": "pdf"}
    )


def test_a_total_queueing_failure_is_reported_as_a_queueing_failure():
    """If ``queue_research_downloads`` raises for every research id and
    nothing was already queued, the stream must alert instead of silently
    completing with "0 / 0 files" success."""
    events = _run_empty_queue_bulk(RuntimeError("db locked"))

    terminal = events[-1]
    assert terminal.get("complete") is True, events
    assert terminal.get("total") == 0
    assert terminal.get("error"), (
        f"a total queueing failure must carry an error: {terminal}"
    )
    assert "queueing failed" in terminal["error"], terminal["error"]


def test_a_clean_empty_queue_is_not_reported_as_a_queueing_failure():
    """Discriminator for the test above: ``queue_research_downloads``
    succeeded, nothing ended up PENDING. That is the legitimate "everything
    is already downloaded" state and must NOT borrow the failure wording."""
    events = _run_empty_queue_bulk(0)

    terminal = events[-1]
    assert terminal.get("complete") is True, events
    assert terminal.get("total") == 0
    assert terminal.get("error"), terminal
    assert "No new papers" in terminal["error"], terminal["error"]
    assert "queueing failed" not in terminal["error"], (
        f"clean empty state misreported as a queue failure: {terminal['error']}"
    )


# ===========================================================================
# Per-item exception classification
# ===========================================================================


def _run_bulk_with_download_exception(exc, mode="pdf"):
    session = _BulkSession(
        _QueueQuery(lambda: 1, lambda: [_queue_item(1, 101)])
    )
    download_service = MagicMock()
    download_service.queue_research_downloads.return_value = 0
    download_service.download_resource.side_effect = exc
    download_service.download_as_text.side_effect = exc
    return _run_download_bulk(
        session, download_service, {"research_ids": ["r1"], "mode": mode}
    )


@pytest.mark.parametrize(
    "message",
    [
        "paywall detected - cannot access",
        "subscription required",
        "not available in this region",
        "no free full text",
        "embargoed until 2027",
        "forbidden",
        "not accessible",
    ],
)
def test_an_access_restriction_is_skipped_not_failed(message):
    """These are not the user's problem and not a defect; reporting them as
    ``failed`` turns a normal paywalled paper into a red error row."""
    events = _run_bulk_with_download_exception(Exception(message))

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "skipped", f"{message!r} -> {item}"
    assert "paywall or access restriction" in item["error"]


@pytest.mark.parametrize(
    "message",
    ["server returned 503", "failed to download", "could not parse", "invalid"],
)
def test_a_download_error_is_failed_not_skipped(message):
    events = _run_bulk_with_download_exception(Exception(message))

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "failed", f"{message!r} -> {item}"
    assert item["error"].startswith("Download failed - ")


def test_an_unclassified_error_is_failed():
    """The else arm: an exception matching neither phrase list is still a
    failure, with its own wording."""
    events = _run_bulk_with_download_exception(Exception("something odd"))

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "failed"
    assert item["error"].startswith("Processing failed - ")


def test_the_raw_exception_text_never_reaches_the_client():
    """CWE-209: only ``type(e).__name__`` is surfaced. The classification
    reads ``str(e).lower()``, so it is easy to leak it into the message the
    branch above builds."""
    secret = "OSError: /srv/pdfs/alice/secret.pdf denied by server"
    events = _run_bulk_with_download_exception(Exception(secret))

    rendered = json.dumps(events)
    assert secret not in rendered
    assert "/srv/pdfs/alice" not in rendered
    item = [e for e in events if "status" in e][0]
    assert item["error"] == "Download failed - Exception"


def test_a_successful_item_reports_success():
    """Positive control for the three classification tests: the same harness
    with no exception must produce ``success``, so "everything is failed"
    would not satisfy them."""
    session = _BulkSession(
        _QueueQuery(lambda: 1, lambda: [_queue_item(1, 101)])
    )
    download_service = MagicMock()
    download_service.queue_research_downloads.return_value = 0
    download_service.download_resource.return_value = (True, None)

    events = _run_download_bulk(
        session, download_service, {"research_ids": ["r1"], "mode": "pdf"}
    )

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "success"
    assert "error" not in item


def test_a_storage_limit_refusal_is_shown_as_how_to_lift_it():
    """``download_resource`` returns the fixed ``pdf_storage_size_limit``
    reason for a PDF the storage size limit refuses. The row must tell the
    person which setting to raise, not show them the raw token."""
    session = _BulkSession(
        _QueueQuery(lambda: 1, lambda: [_queue_item(1, 101)])
    )
    download_service = MagicMock()
    download_service.queue_research_downloads.return_value = 0
    download_service.download_resource.return_value = (
        False,
        "pdf_storage_size_limit",
    )

    events = _run_download_bulk(
        session, download_service, {"research_ids": ["r1"], "mode": "pdf"}
    )

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "skipped"
    assert "Maximum PDF Size (MB)" in item["error"]
    assert "pdf_storage_size_limit" not in item["error"]


def test_text_only_mode_extracts_text_instead_of_downloading():
    """``mode`` selects the service method. A mode check that collapsed would
    silently download PDFs for a user who asked for text only."""
    session = _BulkSession(
        _QueueQuery(lambda: 1, lambda: [_queue_item(1, 101)])
    )
    download_service = MagicMock()
    download_service.queue_research_downloads.return_value = 0
    download_service.download_as_text.return_value = (True, None)
    download_service.download_resource.return_value = (True, None)

    events = _run_download_bulk(
        session,
        download_service,
        {"research_ids": ["r1"], "mode": "text_only"},
    )

    download_service.download_as_text.assert_called_once_with(101)
    download_service.download_resource.assert_not_called()
    assert events[-1].get("complete") is True


# ===========================================================================
# download_all_text -- the per-item loop
# ===========================================================================


class _TextSession:
    def __init__(self, rows):
        self._rows = rows

    def query(self, *entities):
        return self

    def all(self):
        return self._rows


def _resource_row(rid, url, title):
    row = Mock()
    row.id = rid
    row.url = url
    row.title = title
    return row


def _run_download_all_text(rows, download_service, tmp_path):
    session = _TextSession(rows)
    download_service.library_root = str(tmp_path / "lib")

    @contextmanager
    def fake_db_session(*args, **kwargs):
        yield session

    with (
        patch.object(
            library_module, "get_user_db_session", side_effect=fake_db_session
        ),
        patch.object(
            library_module, "get_authenticated_user_password", return_value="pw"
        ),
        patch.object(
            library_module, "DownloadService", return_value=download_service
        ),
        patch.object(library_module, "is_downloadable_url", return_value=True),
    ):
        # download_all_text is a sync def returning a StreamingResponse.
        response = download_all_text(
            _post_json_request({}, path="/library/api/download-all-text"),
            username="alice",
        )
        return _sse_events(_drain(response))


def test_download_all_text_reports_success_per_resource(tmp_path):
    download_service = MagicMock()
    download_service.download_as_text.return_value = (True, None)

    events = _run_download_all_text(
        [_resource_row(5, "https://arxiv.org/abs/1", "My Paper")],
        download_service,
        tmp_path,
    )

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "success"
    assert item["error"] is None
    assert item["file"] == "My Paper"
    assert events[-1] == {"complete": True, "total": 1}


def test_download_all_text_reports_a_returned_failure(tmp_path):
    download_service = MagicMock()
    download_service.download_as_text.return_value = (False, "Timeout error")

    events = _run_download_all_text(
        [_resource_row(6, "https://arxiv.org/abs/2", "Failing Paper")],
        download_service,
        tmp_path,
    )

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "failed"
    assert item["error"] == "Timeout error"
    assert events[-1]["complete"] is True


def test_download_all_text_reports_a_raise_as_failed_without_leaking(tmp_path):
    """An exception must not abort the stream, and CWE-209 applies here too:
    only the exception class name reaches the client."""
    secret = "MuPDF: /srv/library/alice/9f2.pdf is not a PDF"
    download_service = MagicMock()
    download_service.download_as_text.side_effect = RuntimeError(secret)

    events = _run_download_all_text(
        [_resource_row(7, "https://arxiv.org/abs/3", "Exploding Paper")],
        download_service,
        tmp_path,
    )

    item = [e for e in events if "status" in e][0]
    assert item["status"] == "failed"
    assert item["error"] == "Text extraction failed - RuntimeError"
    rendered = json.dumps(events)
    assert secret not in rendered
    assert "/srv/library/alice" not in rendered
    assert events[-1]["complete"] is True


def test_download_all_text_skips_resources_already_extracted(tmp_path):
    """The ``*_{id}.txt`` pre-scan replaced a per-resource glob. A resource
    whose text file already exists must not be re-extracted, and one whose
    filename has a non-numeric suffix must not be mistaken for it."""
    txt_dir = tmp_path / "lib" / "txt"
    txt_dir.mkdir(parents=True)
    (txt_dir / "article_5.txt").touch()
    (txt_dir / "notes_abc.txt").touch()

    download_service = MagicMock()
    download_service.download_as_text.return_value = (True, None)

    events = _run_download_all_text(
        [
            _resource_row(5, "https://arxiv.org/abs/1", "Already Extracted"),
            _resource_row(9, "https://arxiv.org/abs/2", "Still Needed"),
        ],
        download_service,
        tmp_path,
    )

    download_service.download_as_text.assert_called_once_with(9)
    assert events[-1] == {"complete": True, "total": 1}