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.

37.0 KB
Raw Blame
"""Response shapes and filter wiring for the library router's plain API and
page routes.

Ported from the four library-route suites deleted in the Flask->FastAPI
migration (``test_library_routes.py``, ``test_library_routes_coverage.py``,
``test_library_routes_deep_coverage.py``,
``test_library_routes_view_coverage.py``).

Genuinely superseded, and therefore NOT re-ported here:

* ``?limit``/``?offset`` clamping (#4560) --
  ``tests/web/test_pagination_clamping_census.py::TestLibraryDocumentsClamp``.
* ``favorites_only`` and the rest of the ``/api/documents`` filter kwargs,
  ``enable_pdf_storage``, the document-details 404 --
  ``tests/web/routers/test_library_download_outcomes.py``.
* ``POST /api/open-folder`` -> 403 --
  ``tests/security/test_library_notes_authz_fastapi.py::TestOpenFolderIsHardDisabled``.
* ``/document/{id}/pdf`` blob + 404s --
  ``tests/web/routers/test_library_port_fidelity.py``.
* ``is_downloadable_domain`` --
  ``tests/security/test_library_rag_security_fastapi.py``.
* ``download-text`` failure sanitisation, ``download-source`` messages --
  ``tests/web/routers/test_library_download_outcomes.py``.

What remains is a set of response shapes and pass-throughs that the branch
only ever hits with a status-code or ``< 500`` smoke assertion, so the body
could be replaced with ``{}`` and stay green. Two of them are the reason a
test existed at all:

* **#3135** -- ``check_downloads`` must NOT return ``document.file_path``.
  It is the absolute server path, and leaking it hands every authenticated
  user the on-disk directory layout. The field is easy to reintroduce (the
  Document row is right there and the sibling keys come from it), and no
  test on the branch looks for it.
* ``get_research_sources`` -- the per-source dict the download-manager UI
  renders: ``domain`` derived from the URL with a ``""`` fallback for an
  unparseable one, and a ``"Source {n}"`` title default. A raise in either
  fallback takes out the whole page.

The handlers are called directly; none of these branches touch the request
beyond its query string or JSON body. That is the pattern established by
``test_library_download_outcomes.py``.
"""

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

import pytest
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.web.routers import library as library_module
from local_deep_research.web.routers.library import (
    check_downloads,
    download_bulk,
    download_manager_page,
    download_research_pdfs,
    download_single_resource,
    get_collections_list,
    get_library_stats,
    get_pdf_url,
    get_research_sources,
    library_page,
    mark_for_redownload,
    serve_text_api,
    sync_library,
    toggle_favorite,
    view_text_page,
)


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


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


def _post_json_request(payload, path="/library/api/x"):
    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,
    )


#: Filled by ``_patched`` with the kwargs the handler under test passed to
#: ``templates.TemplateResponse``. Rendering the real template would drag in
#: the whole ``base.html`` globals stack, which these tests do not exercise.
_RENDERED: dict = {}


@contextmanager
def _patched(session=None, service=None, **extra):
    """Patch the collaborators the handlers reach for.

    ``session`` stands in for ``get_user_db_session``; ``service`` for
    ``LibraryService``. ``extra`` is applied as further ``patch.object``
    targets on the router module. ``templates.TemplateResponse`` is captured
    rather than rendered -- the properties under test are template-context
    keys, not HTML.
    """

    @contextmanager
    def fake_db_session(*args, **kwargs):
        yield session if session is not None else MagicMock()

    _RENDERED.clear()

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

    patches = [
        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.templates,
            "TemplateResponse",
            side_effect=fake_template_response,
        ),
        # ``library_page`` imports this inside its body, so it has to be
        # patched at the source module. It opens the real encrypted user DB;
        # left alone it raises and the whole page degrades to load_error,
        # which would make every filter assertion below vacuous.
        patch(
            "local_deep_research.database.library_init.get_default_library_id",
            return_value=None,
        ),
    ]
    if service is not None:
        patches.append(
            patch.object(library_module, "LibraryService", return_value=service)
        )
    for name, value in extra.items():
        patches.append(patch.object(library_module, name, value))

    for p in patches:
        p.start()
    try:
        yield
    finally:
        for p in reversed(patches):
            p.stop()


def _rendered(response):
    """The context the handler passed to ``templates.TemplateResponse``."""
    assert response == "rendered", (
        f"handler did not render a template, it returned {response!r}"
    )
    assert _RENDERED, "TemplateResponse was never called"
    return _RENDERED["context"]


# ===========================================================================
# GET /library/ -- the filters reach the service
# ===========================================================================


def _library_service(total=0):
    service = Mock()
    service.get_library_stats.return_value = {"storage_path": "/tmp/lib"}
    service.count_documents.return_value = total
    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 = []
    return service


@pytest.mark.parametrize(
    "query,expected",
    [
        (b"domain=arxiv.org", {"domain": "arxiv.org"}),
        (b"research=42", {"research_id": "42"}),
        (b"date=week", {"date_filter": "week"}),
    ],
    ids=["domain", "research", "date"],
)
def test_library_page_passes_each_filter_to_the_service(query, expected):
    """Each ``?`` filter on the library page must reach
    ``LibraryService.get_documents``. A dropped one silently shows the user
    the unfiltered library while the control still reads as selected."""
    service = _library_service()

    with _patched(service=service):
        library_page(_get_request(query_string=query), username="alice")

    kwargs = service.get_documents.call_args.kwargs
    for key, value in expected.items():
        assert kwargs[key] == value, (
            f"?{query.decode()} did not reach get_documents as {key}={value!r}: "
            f"{kwargs}"
        )
    # The unset filters must stay unset rather than picking up a stray value.
    for key in ("research_id", "domain", "date_filter"):
        if key not in expected:
            assert kwargs[key] is None, kwargs


def test_library_page_collection_filter_wins_over_the_default_library():
    """``?collection=`` selects a collection AND is echoed back into the
    template as ``selected_collection`` so the dropdown keeps its choice.
    Without the echo the page silently resets to "All collections" on every
    reload while still filtering."""
    service = _library_service()

    with _patched(service=service):
        response = library_page(
            _get_request(query_string=b"collection=99"), username="alice"
        )

    assert service.get_documents.call_args.kwargs["collection_id"] == "99"
    assert _rendered(response)["selected_collection"] == "99"


def test_library_page_paginates_with_a_hundred_per_page():
    """First page is ``limit=100, offset=0``; page 2 is ``offset=100``. The
    offset is derived, so an off-by-one silently repeats or skips a page."""
    service = _library_service(total=250)

    with _patched(service=service):
        library_page(_get_request(query_string=b"page=2"), username="alice")

    kwargs = service.get_documents.call_args.kwargs
    assert (kwargs["limit"], kwargs["offset"]) == (100, 100), kwargs


# ===========================================================================
# GET /library/download-manager
# ===========================================================================


def _download_manager_service(total_researches=1):
    service = Mock()
    service.get_download_manager_summary_stats.return_value = {
        "total_researches": total_researches,
        "total_resources": 10,
        "already_downloaded": 3,
        "available_to_download": 5,
    }
    service.get_research_list_with_stats.return_value = []
    service.get_pdf_previews_batch.return_value = {}
    return service


def test_download_manager_summary_stats_reach_the_template():
    """The four counters at the top of the page. They render as bare ``{{ }}``
    interpolations, so dropping one shows the user a blank instead of a
    number -- no exception, no failing status-code test."""
    service = _download_manager_service()

    with _patched(service=service, get_settings_manager=Mock()):
        response = download_manager_page(_get_request(), username="alice")

    context = _rendered(response)
    assert context["total_researches"] == 1
    assert context["total_resources"] == 10
    assert context["already_downloaded"] == 3
    assert context["available_to_download"] == 5


def test_download_manager_page_two_offsets_by_fifty():
    """50 per page: ``?page=2`` must ask the service for rows 50-99 and echo
    ``page``/``total_pages`` back so the pager renders the right state."""
    service = _download_manager_service(total_researches=80)

    with _patched(service=service, get_settings_manager=Mock()):
        response = download_manager_page(
            _get_request(query_string=b"page=2"), username="alice"
        )

    service.get_research_list_with_stats.assert_called_once_with(
        limit=50, offset=50
    )
    context = _rendered(response)
    assert context["page"] == 2
    assert context["total_pages"] == 2


def test_download_manager_page_number_is_clamped_to_the_last_page():
    """An out-of-range ``?page=`` must clamp, not produce an empty page at a
    nonsense offset."""
    service = _download_manager_service(total_researches=80)

    with _patched(service=service, get_settings_manager=Mock()):
        response = download_manager_page(
            _get_request(query_string=b"page=9999"), username="alice"
        )

    service.get_research_list_with_stats.assert_called_once_with(
        limit=50, offset=50
    )
    assert _rendered(response)["page"] == 2


# ===========================================================================
# Small JSON APIs whose bodies nothing asserts
# ===========================================================================


def test_stats_endpoint_returns_the_service_stats_verbatim():
    service = Mock()
    service.get_library_stats.return_value = {
        "total_documents": 42,
        "total_size": 1024,
    }

    with _patched(service=service):
        result = get_library_stats(_get_request(), username="alice")

    assert result == {"total_documents": 42, "total_size": 1024}


def test_collections_list_returns_id_name_and_description():
    collection = Mock()
    collection.id = "c1"
    collection.name = "My Collection"
    collection.description = "Desc"

    session = MagicMock()
    session.query.return_value.order_by.return_value.all.return_value = [
        collection
    ]

    with _patched(session=session):
        result = get_collections_list(_get_request(), username="alice")

    assert result == {
        "success": True,
        "collections": [
            {"id": "c1", "name": "My Collection", "description": "Desc"}
        ],
    }


def test_collections_list_is_empty_not_absent_when_there_are_none():
    session = MagicMock()
    session.query.return_value.order_by.return_value.all.return_value = []

    with _patched(session=session):
        result = get_collections_list(_get_request(), username="alice")

    assert result == {"success": True, "collections": []}


@pytest.mark.parametrize("state", [True, False])
def test_toggle_favorite_returns_the_new_state(state):
    """The button's label flips on this boolean. Returning a constant would
    leave the star permanently stuck in one position."""
    service = Mock()
    service.toggle_favorite.return_value = state

    with _patched(service=service):
        result = toggle_favorite(_get_request(), "d1", username="alice")

    assert result == {"favorite": state}
    service.toggle_favorite.assert_called_once_with("d1")


def test_pdf_url_endpoint_points_at_the_api_pdf_route():
    """The URL the viewer opens. It must be the ``/api/`` sibling (JSON 404
    on failure), not the page route (HTML 404), or the viewer renders the
    error page inside its PDF frame."""
    with _patched():
        result = get_pdf_url(_get_request(), "abc123", username="alice")

    assert result == {
        "url": "/library/api/document/abc123/pdf",
        "title": "Document",
    }


def test_sync_library_returns_the_sync_stats():
    service = Mock()
    service.sync_library_with_filesystem.return_value = {
        "added": 2,
        "removed": 1,
    }

    with _patched(service=service):
        result = sync_library(_get_request(), username="alice")

    assert result == {"added": 2, "removed": 1}


# ===========================================================================
# GET /library/api/document/{id}/text and the /txt page sibling
# ===========================================================================


def _document(**kwargs):
    doc = Mock()
    doc.title = kwargs.get("title", "Paper")
    doc.text_content = kwargs.get("text_content", "Hello world")
    doc.extraction_method = kwargs.get("extraction_method", "pdfminer")
    doc.word_count = kwargs.get("word_count", 2)
    return doc


def _session_with_document(document):
    session = MagicMock()
    session.query.return_value.filter_by.return_value.first.return_value = (
        document
    )
    return session


def test_text_api_returns_the_content_and_its_metadata():
    with _patched(session=_session_with_document(_document())):
        result = serve_text_api(_get_request(), "d1", username="alice")

    assert result == {
        "text_content": "Hello world",
        "title": "Paper",
        "extraction_method": "pdfminer",
        "word_count": 2,
    }


def test_text_api_titles_an_untitled_document():
    with _patched(session=_session_with_document(_document(title=None))):
        result = serve_text_api(_get_request(), "d1", username="alice")

    assert result["title"] == "Document"


def test_text_api_distinguishes_missing_document_from_missing_text():
    """Two different 404s. Collapsing them tells a user whose extraction
    simply has not run yet that their document does not exist."""
    with _patched(session=_session_with_document(None)):
        missing = serve_text_api(_get_request(), "nope", username="alice")

    assert missing.status_code == 404
    assert json.loads(missing.body) == {"error": "Document not found"}

    with _patched(session=_session_with_document(_document(text_content=None))):
        empty = serve_text_api(_get_request(), "d1", username="alice")

    assert empty.status_code == 404
    assert json.loads(empty.body) == {"error": "Text content not available"}


@pytest.mark.parametrize("empty", [None, ""], ids=["none", "empty-string"])
def test_text_page_treats_empty_text_as_unavailable(empty):
    """The empty STRING matters as much as ``None``: a document whose
    extraction produced nothing must not render a blank page as if it had
    content."""
    with _patched(
        session=_session_with_document(_document(text_content=empty))
    ):
        response = view_text_page(_get_request(), "d1", username="alice")

    assert response.status_code == 404
    assert b"not available" in response.body.lower()
    assert response.media_type == "text/html", (
        "these page routes are reached as plain <a href> links, so the error "
        "must be a browser-readable body, not raw JSON"
    )


def test_text_page_404s_for_a_missing_document_in_html():
    with _patched(session=_session_with_document(None)):
        response = view_text_page(_get_request(), "ghost", username="alice")

    assert response.status_code == 404
    assert b"not found" in response.body.lower()
    assert response.media_type == "text/html"


def test_text_page_titles_an_untitled_document():
    with _patched(session=_session_with_document(_document(title=None))):
        response = view_text_page(_get_request(), "d1", username="alice")

    context = _rendered(response)
    assert context["title"] == "Document Text"
    assert context["text_content"] == "Hello world"


# ===========================================================================
# POST /library/api/download/{id} -- the failure branch must not leak
# ===========================================================================


def _download_service_context(result):
    service = MagicMock()
    service.download_resource.return_value = result
    context = MagicMock()
    context.__enter__.return_value = service
    context.__exit__.return_value = False
    return context, service


def test_download_single_resource_failure_is_a_500_without_the_detail():
    """``download_resource`` returns ``(False, <internal diagnostic>)``. The
    handler must substitute a fixed message: the diagnostic carries HTTP
    status lines, parser output and filesystem paths."""
    internal = "OSError: [Errno 28] No space left on device: /srv/pdfs/alice"
    context, _service = _download_service_context((False, internal))

    with _patched(DownloadService=Mock(return_value=context)):
        response = download_single_resource(
            _get_request(), 42, username="alice"
        )

    assert response.status_code == 500
    body = json.loads(response.body)
    assert body["success"] is False
    assert internal not in json.dumps(body)
    assert "/srv/pdfs/alice" not in json.dumps(body)


def test_download_single_resource_missing_resource_is_a_404_not_a_500():
    """Discriminator for the test above: ``"Resource not found"`` is the one
    error string that means "wrong id", and it has its own branch. Without
    it every nonexistent id reads to the UI as a server fault."""
    context, _service = _download_service_context((False, "Resource not found"))

    with _patched(DownloadService=Mock(return_value=context)):
        response = download_single_resource(
            _get_request(), 42, username="alice"
        )

    assert response.status_code == 404
    assert json.loads(response.body) == {
        "success": False,
        "error": "Resource not found",
    }


def test_download_single_resource_storage_limit_refusal_says_to_raise_it():
    """A PDF the storage size limit refuses is not a server fault, and the
    operator can lift it: the reply names the setting to raise, not the
    raw ``pdf_storage_size_limit`` token or "please try again"."""
    context, _service = _download_service_context(
        (False, "pdf_storage_size_limit")
    )

    with _patched(DownloadService=Mock(return_value=context)):
        response = download_single_resource(
            _get_request(), 42, username="alice"
        )

    assert response.status_code == 409
    body = json.loads(response.body)
    assert body["success"] is False
    assert "Maximum PDF Size (MB)" in body["error"]
    assert "pdf_storage_size_limit" not in body["error"]


def test_download_single_resource_success_is_a_plain_success():
    """Positive control: the two failure branches above are real branches,
    not a route that refuses everything."""
    context, service = _download_service_context((True, None))

    with _patched(DownloadService=Mock(return_value=context)):
        result = download_single_resource(_get_request(), 42, username="alice")

    assert result == {"success": True}
    service.download_resource.assert_called_once_with(42)


# ===========================================================================
# POST /library/api/download-research/{id}
# ===========================================================================


@pytest.mark.parametrize(
    "payload,expected_collection",
    [({"collection_id": "c1"}, "c1"), ({}, None)],
    ids=["with-collection", "without-collection"],
)
def test_download_research_forwards_the_collection_id(
    payload, expected_collection
):
    """The target collection is chosen in the UI and travels in the body.
    Dropping it silently files every download into the default library."""
    service = MagicMock()
    service.queue_research_downloads.return_value = 5
    context = MagicMock()
    context.__enter__.return_value = service
    context.__exit__.return_value = False

    with _patched(DownloadService=Mock(return_value=context)):
        result = asyncio.run(
            download_research_pdfs(
                _post_json_request(payload), "r1", username="alice"
            )
        )

    assert result == {"success": True, "queued": 5}
    service.queue_research_downloads.assert_called_once_with(
        "r1", expected_collection
    )


# ===========================================================================
# POST /library/api/mark-redownload
# ===========================================================================


def test_mark_redownload_reports_how_many_were_marked():
    service = Mock()
    service.mark_for_redownload.return_value = 3

    async def _fake_run_db_sync(fn, *args, **kwargs):
        return fn()

    with _patched(service=service, run_db_sync=_fake_run_db_sync):
        result = asyncio.run(
            mark_for_redownload(
                _post_json_request({"document_ids": ["d1", "d2", "d3"]}),
                username="alice",
            )
        )

    assert result == {"success": True, "marked": 3}
    service.mark_for_redownload.assert_called_once_with(["d1", "d2", "d3"])


@pytest.mark.parametrize(
    "payload", [{"document_ids": []}, {}], ids=["empty-list", "missing-key"]
)
def test_mark_redownload_rejects_an_empty_selection(payload):
    """Without this guard the route answers 200 / ``marked: 0`` for a request
    that selected nothing, which reads to the UI as a successful no-op
    instead of a mistake."""
    with _patched(service=Mock()):
        response = asyncio.run(
            mark_for_redownload(_post_json_request(payload), username="alice")
        )

    assert response.status_code == 400
    assert json.loads(response.body) == {"error": "No document IDs provided"}


@pytest.mark.parametrize(
    "document_ids",
    ["doc-1", {"id": "doc-1"}, [1], ["doc-1", ""]],
    ids=["string", "object", "non-string-item", "blank-item"],
)
def test_mark_redownload_rejects_malformed_id_lists_before_service_call(
    document_ids,
):
    service = Mock()
    run_db_sync = Mock(side_effect=AssertionError("database work started"))

    with _patched(service=service, run_db_sync=run_db_sync):
        response = asyncio.run(
            mark_for_redownload(
                _post_json_request({"document_ids": document_ids}),
                username="alice",
            )
        )

    assert response.status_code == 400
    assert json.loads(response.body) == {
        "error": "document_ids must be a list of non-empty strings"
    }
    run_db_sync.assert_not_called()
    service.mark_for_redownload.assert_not_called()


# ===========================================================================
# POST /library/api/download-bulk -- the empty-selection guard
# ===========================================================================


@pytest.mark.parametrize(
    "payload", [{"research_ids": []}, {"mode": "pdf"}], ids=["empty", "missing"]
)
def test_download_bulk_rejects_an_empty_selection(payload):
    """A 400 here, not an SSE stream: the guard runs before the generator, so
    without it the client opens an event stream that immediately completes
    with 0/0 and no error."""
    response = asyncio.run(
        download_bulk(_post_json_request(payload), username="alice")
    )

    assert response.status_code == 400
    assert json.loads(response.body) == {"error": "No research IDs provided"}


@pytest.mark.parametrize(
    "research_ids",
    ["r1", {"id": "r1"}, [1], ["r1", ""]],
    ids=["string", "object", "non-string-item", "blank-item"],
)
def test_download_bulk_rejects_malformed_id_lists_before_streaming(
    research_ids,
):
    response = asyncio.run(
        download_bulk(
            _post_json_request({"research_ids": research_ids}),
            username="alice",
        )
    )

    assert response.status_code == 400
    assert json.loads(response.body) == {
        "error": "research_ids must be a list of non-empty strings"
    }


def test_download_bulk_preserves_download_all_lists_above_one_thousand():
    research_ids = [f"research-{index}" for index in range(1001)]

    response = asyncio.run(
        download_bulk(
            _post_json_request({"research_ids": research_ids}),
            username="alice",
        )
    )

    assert response.status_code == 200
    assert response.media_type == "text/event-stream"


@pytest.mark.parametrize(
    "mode",
    ["text", "PDF", False, ["pdf"]],
    ids=["unknown", "case", "bool", "list"],
)
def test_download_bulk_rejects_unknown_modes_before_streaming(mode):
    response = asyncio.run(
        download_bulk(
            _post_json_request({"research_ids": ["r1"], "mode": mode}),
            username="alice",
        )
    )

    assert response.status_code == 400
    assert json.loads(response.body) == {
        "error": "mode must be either 'pdf' or 'text_only'"
    }


# ===========================================================================
# POST /library/api/check-downloads -- #3135, the file_path leak
# ===========================================================================


def _check_downloads_session(resource, document):
    session = MagicMock()
    chain = (
        session.query.return_value.filter_by.return_value.filter.return_value
    )
    chain.all.return_value = [resource]
    return session


def _resource(rid=1, url="https://arxiv.org/abs/2301.00001", title="Paper"):
    row = Mock()
    row.id = rid
    row.url = url
    row.title = title
    return row


def _completed_document(**columns):
    """A completed download of resource 1, as the handlers load it.

    A real ``Document``: its ``status`` is a ``DocumentStatus`` member, as on
    a loaded row, which the string ``"completed"`` never equals.
    """
    values = {
        "id": "doc-1",
        "resource_id": 1,
        "status": DocumentStatus.COMPLETED,
        "file_type": "pdf",
        "title": "Paper",
        "storage_mode": "filesystem",
        # The absolute server path. Present on the row the handler holds, so
        # the only thing keeping it out of the response is the handler not
        # copying it.
        "file_path": "/srv/library/alice/pdfs/2301.00001.pdf",
    }
    values.update(columns)
    return Document(**values)


def _document_awaiting_its_pdf(**columns):
    """A completed download that "Sync library" left awaiting its PDF.

    Its PDF file was removed while its text was not, so the sync stored it
    the way text-only documents are and reported the file missing (see
    ``document_awaits_pdf``). It does not count as downloaded.
    """
    return _completed_document(
        storage_mode="none", file_path=FILE_PATH_TEXT_ONLY, **columns
    )


def _run_check_downloads(session, document, payload):
    async def _fake_run_db_sync(fn, *args, **kwargs):
        return fn()

    with _patched(
        session=session,
        run_db_sync=_fake_run_db_sync,
        get_document_for_resource=Mock(return_value=document),
    ):
        return asyncio.run(
            check_downloads(
                _post_json_request(
                    payload, path="/library/api/check-downloads"
                ),
                username="alice",
            )
        )


def test_check_downloads_never_returns_the_server_file_path():
    """#3135. ``document.file_path`` is the absolute path on the server's
    disk; returning it hands any authenticated user the directory layout.
    The client only ever needs ``document_id`` -- it fetches through
    ``/document/{id}/pdf``.
    """
    resource = _resource()
    document = _completed_document()

    result = _run_check_downloads(
        _check_downloads_session(resource, document),
        document,
        {"research_id": "r1", "urls": [resource.url]},
    )

    entry = result["download_status"][resource.url]
    assert entry == {
        "downloaded": True,
        "document_id": "doc-1",
        "file_type": "pdf",
        "title": "Paper",
    }
    assert "file_path" not in entry, (
        "check-downloads must not return the absolute server path (#3135): "
        f"{entry}"
    )
    assert "/srv/library/alice" not in json.dumps(result), (
        f"the server path leaked somewhere else in the response: {result}"
    )


def test_check_downloads_reports_an_undownloaded_resource_by_id():
    """The other arm: nothing downloaded yet, so the client gets the
    ``resource_id`` it needs to trigger a download."""
    resource = _resource()

    result = _run_check_downloads(
        _check_downloads_session(resource, None),
        None,
        {"research_id": "r1", "urls": [resource.url]},
    )

    assert result["download_status"][resource.url] == {
        "downloaded": False,
        "resource_id": 1,
    }


def test_check_downloads_reports_a_document_awaiting_its_pdf_by_id():
    """A completed download that awaits its PDF is not downloaded.

    The Download Manager counts it as available to download, so the client
    gets the ``resource_id`` to download its PDF by, not the document.
    """
    resource = _resource()
    document = _document_awaiting_its_pdf()

    result = _run_check_downloads(
        _check_downloads_session(resource, document),
        document,
        {"research_id": "r1", "urls": [resource.url]},
    )

    assert result["download_status"][resource.url] == {
        "downloaded": False,
        "resource_id": 1,
    }


@pytest.mark.parametrize(
    "payload",
    [
        {"urls": ["https://arxiv.org/abs/1"]},
        {"research_id": "r1"},
        {"research_id": "r1", "urls": []},
    ],
    ids=["no-research-id", "no-urls", "empty-urls"],
)
def test_check_downloads_rejects_an_incomplete_request(payload):
    with _patched():
        response = _run_check_downloads(MagicMock(), None, payload)

    assert response.status_code == 400
    assert json.loads(response.body) == {"error": "Missing research_id or urls"}


@pytest.mark.parametrize(
    "urls",
    ["https://example.test", {"url": "https://example.test"}, [1], [""]],
    ids=["string", "object", "non-string-item", "blank-item"],
)
def test_check_downloads_rejects_malformed_url_lists_before_query(urls):
    session = MagicMock()

    response = _run_check_downloads(
        session,
        None,
        {"research_id": "r1", "urls": urls},
    )

    assert response.status_code == 400
    assert json.loads(response.body) == {
        "error": "urls must be a list of non-empty strings"
    }
    session.query.assert_not_called()


@pytest.mark.parametrize(
    "research_id",
    [7, True, ["r1"], {"id": "r1"}],
    ids=["integer", "boolean", "list", "object"],
)
def test_check_downloads_requires_a_string_research_id_before_query(
    research_id,
):
    session = MagicMock()

    response = _run_check_downloads(
        session,
        None,
        {"research_id": research_id, "urls": ["https://example.test"]},
    )

    assert response.status_code == 400
    assert json.loads(response.body) == {"error": "Missing research_id or urls"}
    session.query.assert_not_called()


# ===========================================================================
# GET /library/api/get-research-sources/{id}
# ===========================================================================


def _sources_session(resources):
    session = MagicMock()
    session.query.return_value.filter_by.return_value.order_by.return_value.all.return_value = resources
    return session


def _source_resource(rid, url, title, preview="Preview text", score=0.95):
    row = Mock()
    row.id = rid
    row.url = url
    row.title = title
    row.content_preview = preview
    row.relevance_score = score
    row.created_at = None
    return row


def test_research_sources_carries_the_download_state_of_each_source():
    resource = _source_resource(1, "https://arxiv.org/abs/1", "Test Paper")
    document = _completed_document(created_at=datetime(2026, 5, 9))

    with _patched(
        session=_sources_session([resource]),
        get_document_for_resource=Mock(return_value=document),
    ):
        result = get_research_sources(_get_request(), "r1", username="alice")

    assert result["success"] is True
    assert result["total"] == 1
    source = result["sources"][0]
    assert source["resource_id"] == 1
    assert source["title"] == "Test Paper"
    assert source["domain"] == "arxiv.org"
    assert source["downloaded"] is True
    assert source["document_id"] == "doc-1"
    assert source["file_type"] == "pdf"
    assert source["download_date"] == "2026-05-09T00:00:00"


def test_research_sources_marks_a_document_awaiting_its_pdf_not_downloaded():
    """A completed download that awaits its PDF is not downloaded, as the
    Download Manager counts it, and the source names no document."""
    resource = _source_resource(1, "https://arxiv.org/abs/1", "Test Paper")
    document = _document_awaiting_its_pdf(created_at=datetime(2026, 5, 9))

    with _patched(
        session=_sources_session([resource]),
        get_document_for_resource=Mock(return_value=document),
    ):
        result = get_research_sources(_get_request(), "r1", username="alice")

    source = result["sources"][0]
    assert source["downloaded"] is False
    assert source["document_id"] is None
    assert source["file_type"] is None
    assert "download_date" not in source


def test_research_sources_marks_an_undownloaded_source():
    """Discriminator: ``downloaded`` must actually depend on the document."""
    resource = _source_resource(1, "https://arxiv.org/abs/1", "Test Paper")

    with _patched(
        session=_sources_session([resource]),
        get_document_for_resource=Mock(return_value=None),
    ):
        result = get_research_sources(_get_request(), "r1", username="alice")

    source = result["sources"][0]
    assert source["downloaded"] is False
    assert source["document_id"] is None
    assert source["file_type"] is None


def test_research_sources_falls_back_for_a_missing_url_and_title():
    """``ResearchResource.url`` and ``.title`` are both nullable. The domain
    fallback is ``""`` and the title fallback is ``"Source {n}"`` -- a raise
    in either takes out the whole download-manager page."""
    resource = _source_resource(1, None, None, preview=None, score=None)

    with _patched(
        session=_sources_session([resource]),
        get_document_for_resource=Mock(return_value=None),
    ):
        result = get_research_sources(_get_request(), "r1", username="alice")

    source = result["sources"][0]
    assert source["domain"] == ""
    assert source["title"] == "Source 1"
    assert source["snippet"] == ""


@pytest.mark.parametrize(
    "url", ["not a url", "mailto:[email protected]", "/relative/path"]
)
def test_research_sources_domain_is_a_string_for_a_hostless_url(url):
    """A URL that parses but has no hostname yields ``None`` from
    ``urlparse(...).hostname``. The ``or ""`` keeps ``domain`` a string --
    without it the download-manager template renders the literal "None" as
    the source's domain."""
    resource = _source_resource(1, url, "Untitled-ish")

    with _patched(
        session=_sources_session([resource]),
        get_document_for_resource=Mock(return_value=None),
    ):
        result = get_research_sources(_get_request(), "r1", username="alice")

    assert result["sources"][0]["domain"] == "", url


def test_research_sources_is_empty_not_absent_for_a_research_with_none():
    with _patched(
        session=_sources_session([]),
        get_document_for_resource=Mock(return_value=None),
    ):
        result = get_research_sources(_get_request(), "empty", username="alice")

    assert result == {"success": True, "sources": [], "total": 0}