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.

48.1 KB
Raw Blame
"""The library router's user-visible download outcomes and page wiring.

Ported from ``tests/research_library/routes/test_library_routes_coverage.py``,
deleted in the Flask->FastAPI migration. Nothing on the branch replaced it:
every string asserted here lives in ``web/routers/library.py`` (or, for
``favorites_only``, in ``research_library/services/library_service.py``) and
appeared in no test on the branch.

Seven guarantees, all of them things a user reads or a template branches on:

* ``"Already downloaded"`` -- ``/library/api/download-source`` short-circuits
  only when the resource's Document counts as downloaded: it is
  **completed** and does not await its PDF (``document_counts_as_downloaded``).
  The status half is load-bearing: a resource whose previous attempt is still
  ``pending`` or came back ``failed`` must be re-downloaded, not reported as
  already in the library. Drop the status check and every failed download
  becomes permanently unretryable through this endpoint, with the UI
  cheerfully reporting success. A loaded status is a ``DocumentStatus``
  member, which the string ``"completed"`` never equals, so the documents
  here are real ``Document`` rows with member statuses: compared with that
  string, the guard never short-circuited. A completed document that awaits
  its PDF (the library sync kept its text after its PDF file went missing)
  is downloaded again.
* ``"Download completed"`` / ``"Download failed"`` -- the two terminal
  messages of the same endpoint. ``DownloadService.download_resource``
  returns ``(success, message)`` where ``message`` is an internal diagnostic
  (HTTP status, parser error, filesystem path). The handler must never
  forward it: on failure it substitutes the fixed ``"Download failed"``.
* ``"Failed to download resource"`` -- the same sanitisation on
  ``/library/api/download-text/{resource_id}``. The route logs the real
  ``error`` and returns this one generic string.
* ``enable_pdf_storage`` -- ``pdf_storage_mode != "none"``, computed
  *twice*, once in ``library_page`` and once in ``download_manager_page``.
  Both templates gate the download controls on it. If either copy drifts,
  a user who has turned PDF storage off is shown download buttons on one
  page and not the other.
* ``load_error`` -- a document read failure renders an honest retry state,
  not either empty-library claim, through the real ``library.html`` template.
* ``"pages/document_details.html"`` -- ``document_details_page`` renders it
  for a document that exists and must answer a JSON 404 for one that does
  not, *without* rendering the page. A details template rendered against a
  missing document is how "document not found" turns into a 500 or, worse,
  a half-populated page for a document belonging to someone else.
* ``favorites_only`` -- the ``?favorites=`` query parameter is compared
  against the exact string ``"true"`` before being handed to
  ``LibraryService.get_documents``, which turns it into
  ``Document.favorite.is_(True)``.

The handlers are called directly. None of these branches touch the request
beyond its query string or JSON body, so HTTP would only add an auth dance;
this is the pattern already used by ``test_research_status_error_guidance.py``
and ``test_news_strategy_dropdown.py``. Route-wiring tests at the bottom tie
each handler back to the URL the UI actually calls, so a handler that stops
being mounted fails loudly rather than leaving these tests green against
dead code.
"""

import ast
import asyncio
import inspect
import json
from contextlib import contextmanager
from pathlib import Path
from unittest.mock import MagicMock, Mock, patch

import pytest
from bs4 import BeautifulSoup
from starlette.requests import Request

from local_deep_research.constants import FILE_PATH_TEXT_ONLY
from local_deep_research.database.models.library import (
    Document,
    DocumentStatus,
)
from local_deep_research.research_library.services import (
    library_service as library_service_module,
)
from local_deep_research.research_library.services.library_service import (
    LibraryService,
)
from local_deep_research.web.routers import library as library_module
from local_deep_research.web.routers.library import (
    document_details_page,
    download_manager_page,
    download_source,
    download_text_single,
    get_documents,
    library_page,
    router,
)

SERVICE_PATH = Path(library_service_module.__file__).resolve()

#: A URL ``is_downloadable_domain`` accepts, so ``download_source`` reaches
#: the branches under test instead of its 400 "not a downloadable domain".
ARXIV_URL = "https://arxiv.org/abs/2301.00001"

#: Everything the download-source handler can put in ``message``. Used to
#: assert that a given case emits its own message and none of the others.
ALL_DOWNLOAD_SOURCE_MESSAGES = [
    "Already downloaded",
    "Download completed",
    "Download failed",
    "Download already in progress",
]


# ---------------------------------------------------------------------------
# Request builders
# ---------------------------------------------------------------------------


def _get_request(path="/library/", query_string=b""):
    return Request(
        {
            "type": "http",
            "method": "GET",
            "path": path,
            "headers": [],
            "query_string": query_string,
            "session": {},
        }
    )


def _post_json_request(payload, path="/library/api/download-source"):
    """A real Starlette request whose ``await .json()`` yields ``payload``."""
    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": {},
        },
        receive,
    )


# ---------------------------------------------------------------------------
# Test doubles for the DB boundary
# ---------------------------------------------------------------------------


class _RecordingQuery:
    """Stands in for a SQLAlchemy query chain.

    Records the dicts passed to ``.update()`` so a test can tell a claim
    from a finalise, and returns a fixed ``first()`` row. It implements no
    filtering of its own -- the point is to observe what the handler does,
    never to re-derive it.
    """

    def __init__(self, first_result=None, update_result=1):
        self._first_result = first_result
        self._update_result = update_result
        self.updates = []
        self.filter_bys = []

    def filter_by(self, **kwargs):
        self.filter_bys.append(kwargs)
        return self

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

    def first(self):
        return self._first_result

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


class _FakeSession:
    """Routes ``query(Model)`` to a per-model ``_RecordingQuery``."""

    def __init__(self, resource=None, queue_entry=None, claim_result=1):
        self.resource_query = _RecordingQuery(resource)
        self.queue_query = _RecordingQuery(queue_entry, claim_result)
        self.added = []
        self.commits = 0

    def query(self, *entities):
        if entities and entities[0] is library_module.LibraryDownloadQueue:
            return self.queue_query
        return self.resource_query

    def add(self, obj):
        self.added.append(obj)

    def commit(self):
        self.commits += 1

    def rollback(self):
        pass


def _resource(resource_id=1, research_id="r1"):
    row = Mock()
    row.id = resource_id
    row.research_id = research_id
    row.url = ARXIV_URL
    return row


def _queue_entry(queue_id=42):
    """A pre-existing ``LibraryDownloadQueue`` row for ``_resource``.

    Distinct from any ``resource_id`` used alongside it on purpose: the
    resource that gets downloaded and the queue row whose claim is
    released are different rows, so tests can tell an id swap apart from
    the correct behaviour.
    """
    row = Mock()
    row.id = queue_id
    return row


def _document(document_id="doc-1", status=DocumentStatus.COMPLETED, **columns):
    """The resource's ``Document``, as ``get_document_for_resource`` loads it.

    A real row, whose ``status`` is a ``DocumentStatus`` member. By default
    a completed download of resource 1 with its PDF stored in the database.
    """
    values = {
        "id": document_id,
        "resource_id": 1,
        "status": status,
        "file_type": "pdf",
        "storage_mode": "database",
        "file_path": None,
    }
    values.update(columns)
    return Document(**values)


def _download_service(result):
    """A patched ``DownloadService`` whose ``with`` body returns ``result``."""
    service = MagicMock()
    service.download_resource.return_value = result
    service.download_as_text.return_value = result
    context = MagicMock()
    context.__enter__.return_value = service
    context.__exit__.return_value = False
    return context, service


# ---------------------------------------------------------------------------
# Handler drivers
# ---------------------------------------------------------------------------


def _call_download_source(
    *,
    resource=None,
    existing_document=None,
    download_result=(True, "saved 1 file"),
    queue_entry=None,
    claim_result=1,
):
    """Run the real ``download_source`` end to end over fake DB rows.

    Returns ``(response, session, service)``. ``response`` is whatever the
    handler returned -- a plain dict on the branches under test, a
    ``JSONResponse`` on the error branches -- so a test that subscripts it
    fails loudly if the handler took a different path.
    """
    session = _FakeSession(
        resource if resource is not None else _resource(),
        queue_entry,
        claim_result,
    )
    context, service = _download_service(download_result)

    @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,
            "get_document_for_resource",
            return_value=existing_document,
        ),
        patch.object(library_module, "DownloadService", return_value=context),
    ):
        response = asyncio.run(
            download_source(
                _post_json_request(
                    {"research_id": "r1", "url": ARXIV_URL},
                ),
                username="alice",
            )
        )
    return response, session, service


def _call_download_text(result):
    context, service = _download_service(result)
    with (
        patch.object(
            library_module,
            "get_authenticated_user_password",
            return_value="pw",
        ),
        patch.object(library_module, "DownloadService", return_value=context),
    ):
        response = download_text_single(
            _get_request(path="/library/api/download-text/7"),
            resource_id=7,
            username="alice",
        )
    return response, service


def _call_document_details(document):
    """Returns ``(response, rendered)`` where ``rendered`` is the kwargs the
    handler passed to ``templates.TemplateResponse``, or ``None`` if it never
    rendered anything."""
    service = Mock()
    service.get_document_by_id.return_value = document
    rendered = {}

    def fake_template_response(**kwargs):
        rendered.update(kwargs)
        return "rendered"

    with (
        patch.object(library_module, "LibraryService", return_value=service),
        patch.object(
            library_module.templates,
            "TemplateResponse",
            side_effect=fake_template_response,
        ),
    ):
        response = document_details_page(
            _get_request(path="/library/document/d1"),
            "d1",
            username="alice",
        )
    return response, (rendered or None)


def _settings_manager(pdf_storage_mode):
    values = {
        "research_library.pdf_storage_mode": pdf_storage_mode,
        "research_library.shared_library": False,
    }
    manager = Mock()
    manager.get_setting.side_effect = lambda key, default=None: values.get(
        key, default
    )
    return manager


def _library_service_for_pages():
    service = Mock()
    service.get_library_stats.return_value = {"storage_path": "/tmp/library"}
    service.count_documents.return_value = 0
    service.get_documents.return_value = []
    service.get_unique_domains.return_value = []
    service.get_research_list_for_dropdown.return_value = []
    service.get_all_collections.return_value = []
    service.get_download_manager_summary_stats.return_value = {
        "total_researches": 0,
        "total_resources": 0,
        "already_downloaded": 0,
        "available_to_download": 0,
    }
    service.get_research_list_with_stats.return_value = []
    service.get_pdf_previews_batch.return_value = {}
    return service


def _page_context(handler, pdf_storage_mode):
    """Render one of the two library pages and return its template context."""
    rendered = {}

    def fake_template_response(**kwargs):
        rendered.update(kwargs)
        return "rendered"

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

    with (
        patch.object(
            library_module, "get_user_db_session", side_effect=fake_db_session
        ),
        patch.object(
            library_module,
            "get_settings_manager",
            return_value=_settings_manager(pdf_storage_mode),
        ),
        patch.object(
            library_module,
            "LibraryService",
            return_value=_library_service_for_pages(),
        ),
        patch(
            "local_deep_research.database.library_init.get_default_library_id",
            return_value="col-default",
        ),
        patch.object(
            library_module.templates,
            "TemplateResponse",
            side_effect=fake_template_response,
        ),
    ):
        handler(_get_request(), username="alice")

    assert rendered, (
        f"{handler.__name__} never called templates.TemplateResponse; the "
        "assertions below would be reading an empty context"
    )
    return rendered["context"]


def _documents_call_kwargs(query_string):
    """Run ``get_documents`` and return the kwargs it passed to the service."""
    service = Mock()
    service.get_documents.return_value = []
    with patch.object(library_module, "LibraryService", return_value=service):
        response = get_documents(
            _get_request(
                path="/library/api/documents", query_string=query_string
            ),
            username="alice",
        )
    assert response == {"documents": []}
    service.get_documents.assert_called_once()
    return service.get_documents.call_args.kwargs


# ===========================================================================
# Premise: the URL used by every download-source test is actually accepted
# ===========================================================================


def test_the_test_url_passes_the_downloadable_domain_filter():
    """``download_source`` returns 400 before touching the DB for a URL that
    fails this filter. If ARXIV_URL ever stops passing it, every
    download-source test below would be asserting against that 400 instead."""
    assert library_module.is_downloadable_domain(ARXIV_URL) is True


# ===========================================================================
# Positive controls -- asserted before any of the failure/short-circuit cases
# ===========================================================================


class TestDownloadSourcePositiveControls:
    """A clean download must reach the network and report success.

    Without these, "Already downloaded appears when a completed document
    exists" and "Download failed appears on failure" would both be satisfied
    by a handler that returned those strings unconditionally.
    """

    def test_a_resource_with_no_document_is_actually_downloaded(self):
        response, session, service = _call_download_source(
            existing_document=None, download_result=(True, "saved 1 file")
        )

        assert response == {"success": True, "message": "Download completed"}
        service.download_resource.assert_called_once_with(1)

    def test_that_download_queues_the_resource_before_fetching_it(self):
        """Premise guard for the case above: it would also pass if the
        handler skipped the queue entirely, which is what serialises this
        endpoint against a concurrent bulk run (#4691)."""
        response, session, service = _call_download_source(
            existing_document=None
        )

        assert response["success"] is True
        assert len(session.added) == 1
        queued = session.added[0]
        assert isinstance(queued, library_module.LibraryDownloadQueue)
        assert queued.resource_id == 1
        assert queued.research_id == "r1"
        assert queued.status is library_module.DocumentStatus.PENDING
        assert session.queue_query.updates, (
            "the queue row was never claimed; _claim_download_queue_item "
            "should have issued a PENDING -> PROCESSING update"
        )

    def test_a_successful_download_emits_no_other_outcome_message(self):
        response, _session, _service = _call_download_source(
            existing_document=None
        )

        rendered = json.dumps(response, default=str)
        for message in ALL_DOWNLOAD_SOURCE_MESSAGES:
            if message == "Download completed":
                continue
            assert message not in rendered, (
                f"the success response leaked another outcome: {message!r}"
            )


@pytest.mark.parametrize("download_raises", [False, True])
def test_manual_download_releases_db_scope_during_transfer(download_raises):
    """A claimed queue row must not pin a connection during remote I/O."""
    session = _FakeSession(_resource(), queue_entry=None)
    scope = {"active": False, "entries": 0}

    @contextmanager
    def fake_db_session(*args, **kwargs):
        assert not scope["active"]
        scope["active"] = True
        scope["entries"] += 1
        try:
            yield session
        finally:
            scope["active"] = False

    context, service = _download_service((True, None))

    def download(resource_id):
        assert resource_id == 1
        assert session.queue_query.updates, "queue claim must precede download"
        assert not scope["active"], "download held the queue's DB scope"
        if download_raises:
            raise RuntimeError("transfer failed")
        return True, None

    service.download_resource.side_effect = download
    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, "get_document_for_resource", return_value=None
        ),
        patch.object(library_module, "DownloadService", return_value=context),
    ):
        request = _post_json_request({"research_id": "r1", "url": ARXIV_URL})
        if download_raises:
            with pytest.raises(RuntimeError, match="transfer failed"):
                asyncio.run(download_source(request, username="alice"))
        else:
            response = asyncio.run(download_source(request, username="alice"))
            assert response == {
                "success": True,
                "message": "Download completed",
            }

    assert not scope["active"]
    assert scope["entries"] == 2  # claim, then finalise or release
    assert len(session.queue_query.updates) == 2


class TestManualDownloadQueueRetryability:
    """What the manual route leaves in ``LibraryDownloadQueue`` when its
    transfer raises.

    The claim flips the row to PROCESSING before the download starts, and
    nothing in ``src/`` reaps a PROCESSING row. If a raising manual download
    does not hand the row back on its way out, it is stranded for good: the
    manual reset guard refuses PROCESSING rows and the bulk pre-pass skips
    them (issue #4691).

    The release re-enters the DB after the transfer, so it must carry the
    password captured at request start (a multi-minute transfer can outlive
    the session password store, and the store-less lookup raises
    ``DatabaseSessionError`` before the release ever runs), and a failure
    inside the release must not mask the original transfer error -- the
    sibling bulk route guards its release the same way.
    """

    #: Distinct on purpose: the resource that gets downloaded and the queue
    #: row whose claim is released are different rows, so swapping their ids
    #: fails the assertions below instead of satisfying them.
    RESOURCE_ID = 7
    QUEUE_ITEM_ID = 42

    def test_a_raising_download_returns_the_queue_row_to_pending(self):
        session = _FakeSession(
            _resource(resource_id=self.RESOURCE_ID),
            _queue_entry(queue_id=self.QUEUE_ITEM_ID),
        )
        scope_calls = []

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

        context, service = _download_service((True, None))
        service.download_resource.side_effect = RuntimeError("transfer failed")
        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, "get_document_for_resource", return_value=None
            ),
            patch.object(
                library_module, "DownloadService", return_value=context
            ),
        ):
            request = _post_json_request(
                {"research_id": "r1", "url": ARXIV_URL}
            )
            with pytest.raises(RuntimeError, match="transfer failed"):
                asyncio.run(download_source(request, username="alice"))

        service.download_resource.assert_called_once_with(self.RESOURCE_ID)
        # The post-raise re-entry must carry the request's password: after a
        # multi-minute transfer the session password store can be gone
        # (logout, password change, idle sweep) and the password-less lookup
        # raises DatabaseSessionError before the release ever runs (#4691).
        assert scope_calls == [
            ("alice",),
            ("alice", "pw"),
        ]
        # reset -> claim -> release: the last update hands the row back to
        # PENDING, a retryable state, and not a terminal COMPLETED/FAILED
        # one.
        updates = session.queue_query.updates
        assert len(updates) == 3
        assert updates[-1] == {
            library_module.LibraryDownloadQueue.status: (
                library_module.DocumentStatus.PENDING
            )
        }, "a raising manual download must leave its queue row PENDING"
        # It is this stream's own claim (the QUEUE_ITEM_ID row, still
        # PROCESSING) that is released -- not the downloaded resource.
        assert session.queue_query.filter_bys[-1] == {
            "id": self.QUEUE_ITEM_ID,
            "status": library_module.DocumentStatus.PROCESSING,
        }

    def test_a_successful_download_leaves_a_terminal_status(self):
        """Positive control for the case above: the same code path over a
        successful transfer finalises the row to COMPLETED instead of
        returning it to the queue."""
        session = _FakeSession(
            _resource(resource_id=self.RESOURCE_ID),
            _queue_entry(queue_id=self.QUEUE_ITEM_ID),
        )
        scope_calls = []

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

        context, service = _download_service((True, None))
        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, "get_document_for_resource", return_value=None
            ),
            patch.object(
                library_module, "DownloadService", return_value=context
            ),
        ):
            request = _post_json_request(
                {"research_id": "r1", "url": ARXIV_URL}
            )
            response = asyncio.run(download_source(request, username="alice"))

        assert response == {"success": True, "message": "Download completed"}
        service.download_resource.assert_called_once_with(self.RESOURCE_ID)
        assert scope_calls == [
            ("alice",),
            ("alice", "pw"),
        ]
        updates = session.queue_query.updates
        assert len(updates) == 3
        status_column = library_module.LibraryDownloadQueue.status
        assert (
            updates[-1][status_column]
            is library_module.DocumentStatus.COMPLETED
        )
        assert (
            library_module.LibraryDownloadQueue.completed_at in updates[-1]
        ), "finalise must record a terminal status, not requeue the row"

    def test_a_failing_release_does_not_mask_the_download_error(self):
        """If the release re-entry itself raises, the caller must still see
        the original transfer error, not the release failure."""
        session = _FakeSession(
            _resource(resource_id=self.RESOURCE_ID),
            _queue_entry(queue_id=self.QUEUE_ITEM_ID),
        )
        scope_calls = []

        @contextmanager
        def fake_db_session(*args, **kwargs):
            scope_calls.append(args)
            if len(scope_calls) == 2:
                raise RuntimeError("password resolution failed")
            yield session

        context, service = _download_service((True, None))
        service.download_resource.side_effect = RuntimeError("transfer failed")
        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, "get_document_for_resource", return_value=None
            ),
            patch.object(
                library_module, "DownloadService", return_value=context
            ),
        ):
            request = _post_json_request(
                {"research_id": "r1", "url": ARXIV_URL}
            )
            with pytest.raises(RuntimeError, match="transfer failed"):
                asyncio.run(download_source(request, username="alice"))

        assert len(scope_calls) == 2, (
            "the release re-entry never happened, so this test exercised "
            "nothing about masking"
        )


class TestDownloadTextPositiveControl:
    def test_a_successful_text_extraction_reports_no_error(self):
        response, service = _call_download_text((True, None))

        assert response == {"success": True, "error": None}
        service.download_as_text.assert_called_once_with(7)
        assert "Failed to download resource" not in json.dumps(
            response, default=str
        ), (
            "the success response carries the failure string, so the failure "
            "assertions below prove nothing"
        )


class TestDocumentDetailsPositiveControl:
    def test_an_existing_document_renders_the_details_template(self):
        response, rendered = _call_document_details(
            {"id": "d1", "title": "Attention Is All You Need"}
        )

        assert response == "rendered"
        assert rendered is not None
        assert rendered["name"] == "pages/document_details.html"
        assert rendered["context"]["document"] == {
            "id": "d1",
            "title": "Attention Is All You Need",
        }


# ===========================================================================
# "Already downloaded" -- and the status check that decides it
# ===========================================================================


class TestAlreadyDownloaded:
    def test_a_completed_document_short_circuits_with_its_id(self):
        response, session, service = _call_download_source(
            existing_document=_document("doc-1")
        )

        assert response == {
            "success": True,
            "message": "Already downloaded",
            "document_id": "doc-1",
        }

    def test_the_short_circuit_does_no_work(self):
        """The whole point of the branch: no second fetch, no queue row."""
        _response, session, service = _call_download_source(
            existing_document=_document("doc-1")
        )

        service.download_resource.assert_not_called()
        assert session.added == []
        assert session.queue_query.updates == []

    @pytest.mark.parametrize(
        "status",
        [
            DocumentStatus.PENDING,
            DocumentStatus.PROCESSING,
            DocumentStatus.FAILED,
        ],
        ids=lambda status: status.value,
    )
    def test_a_non_completed_document_is_re_downloaded(self, status):
        """The completed status is the load-bearing half of the guard. A
        previous attempt that failed or is stuck must not be reported to the
        user as already in their library."""
        response, _session, service = _call_download_source(
            existing_document=_document("doc-1", status=status),
            download_result=(True, "saved 1 file"),
        )

        assert response["message"] != "Already downloaded"
        assert response == {"success": True, "message": "Download completed"}
        service.download_resource.assert_called_once_with(1)

    def test_a_document_that_awaits_its_pdf_is_downloaded_again(self):
        """A completed download that "Sync library" left awaiting its PDF
        does not count as downloaded: its PDF file was removed, the sync kept
        its text, stored it text-only and reported the file missing (see
        ``document_awaits_pdf``). The download puts its PDF back."""
        response, _session, service = _call_download_source(
            existing_document=_document(
                "doc-1", storage_mode="none", file_path=FILE_PATH_TEXT_ONLY
            ),
            download_result=(True, None),
        )

        assert response == {"success": True, "message": "Download completed"}
        service.download_resource.assert_called_once_with(1)

    def test_no_existing_document_at_all_is_not_already_downloaded(self):
        response, _session, _service = _call_download_source(
            existing_document=None
        )

        assert response["message"] != "Already downloaded"


# ===========================================================================
# "Download completed" / "Download failed" -- and the message that is hidden
# ===========================================================================


class TestDownloadSourceOutcomeMessages:
    def test_a_failed_download_reports_the_fixed_failure_message(self):
        response, _session, service = _call_download_source(
            existing_document=None,
            download_result=(False, "HTTP 403 from export.arxiv.org"),
        )

        assert response == {"success": False, "message": "Download failed"}
        service.download_resource.assert_called_once_with(1)

    def test_the_internal_failure_detail_never_reaches_the_client(self):
        internal = "OSError: [Errno 28] No space left on device: /srv/pdfs"
        response, _session, _service = _call_download_source(
            existing_document=None, download_result=(False, internal)
        )

        rendered = json.dumps(response, default=str)
        assert internal not in rendered
        assert "No space left on device" not in rendered
        assert "/srv/pdfs" not in rendered

    def test_a_storage_limit_refusal_says_which_setting_to_raise(self):
        """The fixed ``pdf_storage_size_limit`` reason is not a generic
        failure: the operator can lift it, so the message names the
        setting instead of "Download failed" or the raw token."""
        response, _session, _service = _call_download_source(
            existing_document=None,
            download_result=(False, "pdf_storage_size_limit"),
        )

        assert response["success"] is False
        assert "Maximum PDF Size (MB)" in response["message"]
        assert "pdf_storage_size_limit" not in response["message"]

    def test_a_failure_is_not_reported_as_completed(self):
        response, _session, _service = _call_download_source(
            existing_document=None, download_result=(False, "parser error")
        )

        assert response["message"] != "Download completed"
        assert response["message"] != "Already downloaded"

    def test_a_lost_claim_is_a_409_not_a_completion(self):
        """Cross-branch control for "Download completed": when a concurrent
        bulk stream already owns the queue row, the claim update matches zero
        rows and this request must back off instead of downloading again
        (#4691)."""
        response, _session, service = _call_download_source(
            existing_document=None, claim_result=0
        )

        assert response.status_code == 409
        body = json.loads(response.body)
        assert body == {
            "success": False,
            "message": "Download already in progress",
        }
        service.download_resource.assert_not_called()


# ===========================================================================
# "Failed to download resource"
# ===========================================================================


class TestDownloadTextFailureIsSanitised:
    def test_an_extraction_failure_returns_the_generic_message(self):
        response, _service = _call_download_text(
            (False, "PyMuPDF: cannot open broken.pdf")
        )

        assert response == {
            "success": False,
            "error": "Failed to download resource",
        }

    def test_the_extractor_detail_never_reaches_the_client(self):
        internal = "MuPDF error: /srv/library/alice/9f2.pdf is not a PDF"
        response, _service = _call_download_text((False, internal))

        rendered = json.dumps(response, default=str)
        assert internal not in rendered
        assert "/srv/library/alice" not in rendered

    def test_a_failure_with_no_error_text_still_reports_the_message(self):
        """``(False, None)`` skips the logging arm but must not skip the
        response: the ``if error:`` inside the failure branch guards only the
        log line."""
        response, _service = _call_download_text((False, None))

        assert response == {
            "success": False,
            "error": "Failed to download resource",
        }

    def test_a_failure_is_never_reported_as_success(self):
        response, _service = _call_download_text((False, "boom"))

        assert response["success"] is False
        assert response["error"] is not None


# ===========================================================================
# "pages/document_details.html"
# ===========================================================================


class TestDocumentDetailsMissingDocument:
    def test_a_missing_document_is_an_html_404_not_json(self):
        """This route is reached from library.html as a plain ``<a href>``, so
        a stale link is a browser navigation, not an API call.

        It used to answer with ``{"error": "Document not found"}``, which the
        browser rendered as a raw JSON body -- a regression from main, which
        returned ``"Document not found", 404`` as text/html. The sibling
        ``/library/api/document/{id}/text`` route keeps JSON, which is correct
        for an API caller.
        """
        response, _rendered = _call_document_details(None)

        assert response.status_code == 404
        assert b"Document not found" in response.body
        assert response.media_type == "text/html", (
            f"browser navigation must not get {response.media_type}"
        )
        assert not response.body.strip().startswith(b"{"), (
            "a browser navigation must not receive a JSON body"
        )

    def test_a_missing_document_never_renders_the_details_template(self):
        _response, rendered = _call_document_details(None)

        assert rendered is None, (
            "document_details_page rendered "
            f"{rendered.get('name')!r} for a document that does not exist"
        )


# ===========================================================================
# enable_pdf_storage
# ===========================================================================

#: Both pages compute ``enable_pdf_storage`` from their own copy of
#: ``pdf_storage_mode != "none"``. Parametrising over both is what catches
#: one copy drifting from the other.
PDF_STORAGE_PAGES = [
    pytest.param(library_page, id="library-page"),
    pytest.param(download_manager_page, id="download-manager-page"),
]


class TestEnablePdfStorage:
    @pytest.mark.parametrize("handler", PDF_STORAGE_PAGES)
    @pytest.mark.parametrize("mode", ["database", "filesystem"])
    def test_a_storage_mode_enables_the_download_controls(
        self, monkeypatch, handler, mode
    ):
        """Positive control, asserted before the "none" case."""
        if mode == "filesystem":
            monkeypatch.setenv(
                "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE", "true"
            )
        context = _page_context(handler, mode)

        assert context["enable_pdf_storage"] is True
        assert context["pdf_storage_mode"] == mode

    @pytest.mark.parametrize("handler", PDF_STORAGE_PAGES)
    def test_gate_off_coerces_filesystem_mode_to_database(
        self, monkeypatch, handler
    ):
        """When the operator gate is off, stored filesystem mode resolves to database."""
        monkeypatch.delenv(
            "LDR_RESEARCH_LIBRARY_ALLOW_FILESYSTEM_PDF_STORAGE", raising=False
        )
        context = _page_context(handler, "filesystem")

        assert context["enable_pdf_storage"] is True
        assert context["pdf_storage_mode"] == "database"

    @pytest.mark.parametrize("handler", PDF_STORAGE_PAGES)
    def test_mode_none_disables_the_download_controls(self, handler):
        context = _page_context(handler, "none")

        assert context["enable_pdf_storage"] is False
        assert context["pdf_storage_mode"] == "none"

    @pytest.mark.parametrize("handler", PDF_STORAGE_PAGES)
    def test_both_pages_agree_for_the_same_setting(self, handler):
        """The two copies must not drift: a user with storage off seeing
        download buttons on one page and not the other is the failure this
        catches."""
        assert (
            _page_context(library_page, "none")["enable_pdf_storage"]
            == _page_context(download_manager_page, "none")[
                "enable_pdf_storage"
            ]
        )
        assert (
            _page_context(library_page, "database")["enable_pdf_storage"]
            == _page_context(download_manager_page, "database")[
                "enable_pdf_storage"
            ]
        )

    def test_the_library_page_reports_a_healthy_load(self):
        """Premise guard: ``library_page`` swallows every exception from the
        service layer and degrades to an empty page. ``enable_pdf_storage``
        is computed before that ``try``, so the assertions above would still
        pass against a page that failed to load anything. This pins that the
        mocks above are actually driving the normal path."""
        context = _page_context(library_page, "database")

        assert context["load_error"] is False
        assert context["storage_path"] == "/tmp/library"


class TestLibraryPageLoadFailure:
    def test_service_read_failure_renders_an_honest_retry_state(self, app):
        service = Mock()
        service.get_library_stats.return_value = {
            "total_pdfs": 3,
            "total_size_mb": 4.25,
            "storage_path": "/tmp/library",
        }
        service.count_documents.side_effect = RuntimeError(
            "injected library read failure"
        )

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

        requests = [
            _get_request(),
            _get_request(query_string=b"domain=example.org"),
        ]
        for request in requests:
            request.scope.update(
                {
                    "app": app,
                    "router": app.router,
                    "scheme": "http",
                    "server": ("testserver", 80),
                    "root_path": "",
                }
            )

        with (
            patch.object(
                library_module,
                "get_user_db_session",
                side_effect=fake_db_session,
            ),
            patch.object(
                library_module,
                "get_settings_manager",
                return_value=_settings_manager("database"),
            ),
            patch.object(
                library_module, "LibraryService", return_value=service
            ),
            patch(
                "local_deep_research.database.library_init.get_default_library_id",
                return_value="col-default",
            ),
        ):
            responses = [
                library_page(request, username="alice") for request in requests
            ]

        for response in responses:
            assert response.status_code == 200
            empty_state = BeautifulSoup(
                response.body.decode("utf-8"), "html.parser"
            ).select_one(".ldr-empty-state")
            assert empty_state is not None
            assert empty_state.find("h3").get_text(strip=True) == (
                "Your library could not be loaded"
            )
            assert [
                paragraph.get_text(" ", strip=True)
                for paragraph in empty_state.find_all("p")
            ] == [
                "Something went wrong reading your documents, so this page "
                "cannot show them right now. Your documents have not been "
                "deleted."
            ]
            retry = empty_state.find("a")
            assert retry is not None
            assert retry.get_text(" ", strip=True) == "Try Again"
            assert retry.get("href", "").endswith("/library/")

            visible_copy = empty_state.get_text(" ", strip=True)
            assert "No documents in your library yet" not in visible_copy
            assert "No documents match the current filters" not in visible_copy

        assert service.get_library_stats.call_count == 2
        assert service.count_documents.call_count == 2
        service.get_documents.assert_not_called()


# ===========================================================================
# favorites_only
# ===========================================================================

#: The exact source of the ``favorites_only`` arm in
#: ``LibraryService.get_documents``. Pinned so the flag cannot quietly stop
#: filtering while the route keeps passing it.
FAVORITES_FILTER_SOURCE = (
    "if favorites_only:\n"
    "    doc_subq = doc_subq.filter(Document.favorite.is_(True))"
)


def _favorites_arm_source():
    """Read the ``if favorites_only:`` arm out of the service source."""
    tree = ast.parse(SERVICE_PATH.read_text(encoding="utf-8"))
    fn = next(
        (
            node
            for node in ast.walk(tree)
            if isinstance(node, ast.FunctionDef)
            and node.name == "get_documents"
        ),
        None,
    )
    if fn is None:
        return None
    for node in ast.walk(fn):
        if isinstance(node, ast.If) and ast.unparse(node.test) == (
            "favorites_only"
        ):
            return ast.unparse(node)
    return None


class TestFavoritesOnlyQueryParam:
    def test_favorites_defaults_to_false(self):
        """Positive control: the library listing is unfiltered unless the
        caller asks for favourites. If this flipped, every user's library
        would silently show only starred documents."""
        assert _documents_call_kwargs(b"")["favorites_only"] is False

    def test_the_exact_string_true_enables_the_filter(self):
        assert (
            _documents_call_kwargs(b"favorites=true")["favorites_only"] is True
        )

    @pytest.mark.parametrize(
        "query_string",
        [b"favorites=false", b"favorites=", b"favorites=1", b"favorites=True"],
        ids=["false", "empty", "one", "capital-True"],
    )
    def test_other_values_do_not_enable_the_filter(self, query_string):
        """The comparison is ``== "true"``, exact and case-sensitive. This is
        the contract the front end is written against; widening it silently
        would be a behaviour change, narrowing it breaks the star filter."""
        assert _documents_call_kwargs(query_string)["favorites_only"] is False

    def test_the_flag_travels_alongside_the_other_filters(self):
        """Premise guard: ``favorites_only`` reaching the service is only
        useful if the rest of the filter set does too."""
        kwargs = _documents_call_kwargs(
            b"favorites=true&research_id=r1&domain=arxiv.org&file_type=pdf"
            b"&search=quantum&limit=50&offset=10"
        )

        assert kwargs == {
            "research_id": "r1",
            "domain": "arxiv.org",
            "file_type": "pdf",
            "favorites_only": True,
            "search_query": "quantum",
            "limit": 50,
            "offset": 10,
        }


class TestFavoritesOnlyReachesTheQuery:
    def test_the_service_still_accepts_the_keyword(self):
        """The route tests above drive a mocked ``LibraryService``, so they
        would keep passing if the service renamed or dropped the parameter."""
        parameters = inspect.signature(LibraryService.get_documents).parameters

        assert "favorites_only" in parameters, (
            "LibraryService.get_documents no longer takes favorites_only; "
            "the route in web/routers/library.py still passes it, which "
            "would be a TypeError on every /library/api/documents request"
        )
        assert parameters["favorites_only"].default is False

    def test_the_arm_was_found_at_all(self):
        """Premise guard for the pin below."""
        assert _favorites_arm_source() is not None, (
            "could not find an 'if favorites_only:' branch in "
            f"{SERVICE_PATH}; the pin below is matching nothing"
        )

    def test_the_flag_still_filters_on_document_favorite(self):
        assert _favorites_arm_source() == FAVORITES_FILTER_SOURCE, (
            "the favorites_only branch of LibraryService.get_documents "
            "changed. The flag is what the library UI's star filter sends; "
            "confirm it still restricts the query before updating this pin."
        )


# ===========================================================================
# Route wiring -- ties the handlers above to the URLs the UI calls
# ===========================================================================


@pytest.mark.parametrize(
    "path,method,handler",
    [
        ("/library/api/download-source", "POST", download_source),
        (
            "/library/api/download-text/{resource_id}",
            "POST",
            download_text_single,
        ),
        ("/library/document/{document_id}", "GET", document_details_page),
        ("/library/api/documents", "GET", get_documents),
        ("/library/", "GET", library_page),
        ("/library/download-manager", "GET", download_manager_page),
    ],
)
def test_handler_is_mounted_at_its_url(path, method, handler):
    matches = [
        route
        for route in router.routes
        if getattr(route, "path", None) == path
        and method in getattr(route, "methods", ())
    ]

    assert len(matches) == 1, (
        f"expected exactly one {method} {path} route on the library router, "
        f"found {len(matches)}"
    )
    assert matches[0].endpoint is handler