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.

14.5 KB
Raw Blame
"""Behaviour ``origin/main``'s Flask library routes had that the FastAPI port lost.

Three independent regressions were identified, all in
``src/local_deep_research/web/routers/library.py`` (plus the shared
body-parsing idiom in ``library_search.py``). These fixed regressions are
permanent passing tests.

1. Byte-range / conditional PDF serving.
   main returned the PDF through Flask's ``send_file(BytesIO(pdf_bytes), ...)``.
   werkzeug's ``send_file`` defaults to ``conditional=True`` and therefore
   emits ``Accept-Ranges: bytes`` and answers a ``Range:`` request with
   ``206 Partial Content`` + ``Content-Range``. Verified directly against the
   installed werkzeug 3.1.8 (the version main ran on)::

       >>> rv = werkzeug.utils.send_file(BytesIO(pdf), env_with_range,
       ...                               mimetype="application/pdf",
       ...                               as_attachment=False,
       ...                               download_name="report.pdf")
       >>> rv.status_code, rv.headers["Content-Range"]
       (206, 'bytes 0-99/2016')

   The port initially replaced that with a bare ``Response(content=pdf_bytes,
   ...)``. The in-memory response now handles single byte ranges again.

2. ``text/html`` on the two browser-navigation PDF/text page routes.
   ``pages/library.html`` links ``/library/document/<id>/pdf`` and
   ``/library/document/<id>/txt`` as ordinary ``<a href target="_blank">``.
   main answered both "not available" cases with a bare string return, which
   Flask serves as ``text/html``. The port converted the sibling
   "Document not found" branch of these very routes to ``HTMLResponse`` -- and
   left the "PDF not available" / "Text content not available" branch as
   ``JSONResponse``, so a stale link opens a new tab showing a raw JSON body.

3. A malformed request body is a 400, not a 500.
   main gated these routes with ``@require_json_body(...)``
   (``security/decorators.py``), which called ``request.get_json(silent=True)``
   and returned 400 whenever the body did not parse to a dict.
   ``UnicodeDecodeError`` is a ``ValueError`` subclass, so werkzeug's
   ``get_json`` swallowed it too and the decorator produced its normal 400.
   The port now uses the shared ``read_json_dict`` parser at these routes,
   preserving each route's 400 validation envelope for invalid UTF-8 too.
"""

import uuid

import pytest
from fastapi.testclient import TestClient

PDF_BYTES = b"%PDF-1.4\n" + b"A" * 2000 + b"\n%%EOF\n"


@pytest.fixture(scope="module")
def library_client():
    """Authenticated client plus three fixture documents in the user's DB.

    Registers and logs in a real user (these routes open the user's encrypted
    library database), then inserts:

    * a document WITH a stored PDF blob        -> exercises the served-PDF path
    * a document with NO blob and NO text      -> exercises both "not available"
                                                  branches
    """
    from local_deep_research.database.models.library import (
        Document,
        DocumentBlob,
        SourceType,
    )
    from local_deep_research.database.session_context import (
        get_user_db_session,
    )
    from local_deep_research.web.fastapi_app import app

    client = TestClient(app, raise_server_exceptions=False)
    username = f"libfid_{uuid.uuid4().hex[:8]}"
    password = "TestPassword123!"  # noqa: S105

    def _csrf():
        client.get("/auth/login")
        resp = client.get("/auth/csrf-token")
        return (
            resp.json().get("csrf_token", "") if resp.status_code == 200 else ""
        )

    client.post(
        "/auth/register",
        data={
            "username": username,
            "password": password,
            "confirm_password": password,
            "acknowledge": "true",
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    login = client.post(
        "/auth/login",
        data={
            "username": username,
            "password": password,
            "csrf_token": _csrf(),
        },
        follow_redirects=False,
    )
    if login.status_code != 302:
        pytest.fail(
            f"Login bootstrap failed: expected 302, got {login.status_code}: "
            f"{login.text[:400]}"
        )
    client.headers.update({"X-CSRFToken": _csrf()})

    with_pdf = str(uuid.uuid4())
    empty = str(uuid.uuid4())
    with get_user_db_session(username, password) as db_session:
        source_type = db_session.query(SourceType).first()
        if source_type is None:
            pytest.fail("library bootstrap created no SourceType rows")
        db_session.add(
            Document(
                id=with_pdf,
                source_type_id=source_type.id,
                document_hash=uuid.uuid4().hex,
                file_size=len(PDF_BYTES),
                file_type="pdf",
                filename="report.pdf",
                title="Doc with a stored PDF",
                status="completed",
            )
        )
        db_session.add(
            Document(
                id=empty,
                source_type_id=source_type.id,
                document_hash=uuid.uuid4().hex,
                file_size=0,
                file_type="pdf",
                filename="gone.pdf",
                title="Doc whose blob was deleted",
                status="completed",
            )
        )
        db_session.flush()
        db_session.add(DocumentBlob(document_id=with_pdf, pdf_binary=PDF_BYTES))
        db_session.commit()

    yield client, with_pdf, empty


# ---------------------------------------------------------------------------
# 1. Byte-range / conditional PDF serving
# ---------------------------------------------------------------------------


def test_pdf_route_serves_the_stored_blob(library_client):
    """Control for the two range tests below: the happy path still works, so a
    range failure means range handling is missing, not that the fixture is
    broken."""
    client, with_pdf, _ = library_client
    resp = client.get(f"/library/document/{with_pdf}/pdf")

    assert resp.status_code == 200, resp.text[:300]
    assert resp.headers["content-type"] == "application/pdf"
    assert resp.content == PDF_BYTES


@pytest.mark.parametrize("prefix", ["/library", "/library/api"])
def test_pdf_route_advertises_accept_ranges(library_client, prefix):
    client, with_pdf, _ = library_client
    resp = client.get(f"{prefix}/document/{with_pdf}/pdf")

    assert resp.status_code == 200
    assert resp.content == PDF_BYTES
    assert resp.headers.get("accept-ranges") == "bytes", (
        "main's send_file set 'Accept-Ranges: bytes' on this response; got "
        f"headers {dict(resp.headers)}"
    )


@pytest.mark.parametrize("prefix", ["/library", "/library/api"])
def test_pdf_route_honours_a_range_request(library_client, prefix):
    client, with_pdf, _ = library_client
    resp = client.get(
        f"{prefix}/document/{with_pdf}/pdf", headers={"Range": "bytes=0-99"}
    )

    assert resp.status_code == 206, (
        f"expected 206 Partial Content, got {resp.status_code} with "
        f"{len(resp.content)} bytes"
    )
    assert resp.headers.get("content-range") == f"bytes 0-99/{len(PDF_BYTES)}"
    assert resp.content == PDF_BYTES[:100]
    assert resp.headers["content-length"] == "100"
    assert resp.headers["content-type"] == "application/pdf"
    assert resp.headers["content-disposition"].startswith("inline;")


@pytest.mark.parametrize("prefix", ["/library", "/library/api"])
@pytest.mark.parametrize(
    ("value", "start", "stop"),
    [
        ("bytes=-10", len(PDF_BYTES) - 10, len(PDF_BYTES)),
        ("bytes=2000-", 2000, len(PDF_BYTES)),
        ("bytes=2000-999999", 2000, len(PDF_BYTES)),
        ("bytes=-999999", 0, len(PDF_BYTES)),
    ],
)
def test_pdf_route_clips_ranges(library_client, prefix, value, start, stop):
    client, with_pdf, _ = library_client
    resp = client.get(
        f"{prefix}/document/{with_pdf}/pdf", headers={"Range": value}
    )

    assert resp.status_code == 206
    assert resp.content == PDF_BYTES[start:stop]
    assert resp.headers["content-length"] == str(stop - start)
    assert resp.headers["content-range"] == (
        f"bytes {start}-{stop - 1}/{len(PDF_BYTES)}"
    )


@pytest.mark.parametrize("prefix", ["/library", "/library/api"])
def test_pdf_route_unsatisfiable_range_is_empty(library_client, prefix):
    client, with_pdf, _ = library_client
    resp = client.get(
        f"{prefix}/document/{with_pdf}/pdf",
        headers={"Range": f"bytes={len(PDF_BYTES)}-"},
    )

    assert resp.status_code == 416
    assert resp.content == b""
    assert resp.headers["content-range"] == f"bytes */{len(PDF_BYTES)}"
    assert resp.headers["content-length"] == "0"


@pytest.mark.parametrize("prefix", ["/library", "/library/api"])
@pytest.mark.parametrize(
    "headers",
    [
        {"Range": "bytes=invalid"},
        {"Range": "bytes=0-99,200-299"},
        {"Range": "bytes=0-99", "If-Range": '"older-document"'},
    ],
)
def test_pdf_route_ignored_range_keeps_complete_body(
    library_client, prefix, headers
):
    client, with_pdf, _ = library_client
    resp = client.get(f"{prefix}/document/{with_pdf}/pdf", headers=headers)

    assert resp.status_code == 200
    assert resp.content == PDF_BYTES
    assert resp.headers["content-length"] == str(len(PDF_BYTES))
    assert "content-range" not in resp.headers


@pytest.mark.parametrize("prefix", ["/library", "/library/api"])
def test_pdf_range_requires_authentication(library_client, prefix, monkeypatch):
    from local_deep_research.web.routers.library import PDFStorageManager

    client, with_pdf, _ = library_client

    def unexpected_load(*args, **kwargs):
        pytest.fail("An unauthenticated range request must not load a PDF")

    monkeypatch.setattr(PDFStorageManager, "load_pdf", unexpected_load)
    anonymous = TestClient(client.app, raise_server_exceptions=False)
    try:
        resp = anonymous.get(
            f"{prefix}/document/{with_pdf}/pdf",
            headers={"Range": "bytes=0-99"},
            follow_redirects=False,
        )
    finally:
        anonymous.close()

    assert resp.status_code in {302, 401}
    assert "content-range" not in resp.headers
    assert not resp.content.startswith(b"%PDF")


# ---------------------------------------------------------------------------
# 2. text/html on the browser-navigation page routes
# ---------------------------------------------------------------------------


def test_document_not_found_page_route_is_html(library_client):
    """The branch of these page routes the port DID convert -- pinned so the
    two xfails below are visibly about the *other* branch of the same two
    handlers, not about page routes in general."""
    client, _, _ = library_client
    resp = client.get("/library/document/no-such-document/pdf")

    assert resp.status_code == 404
    assert resp.headers["content-type"].startswith("text/html")
    assert resp.text == "Document not found"


def test_pdf_not_available_is_html_on_the_page_route(library_client):
    client, _, empty = library_client
    resp = client.get(f"/library/document/{empty}/pdf")

    assert resp.status_code == 404
    assert resp.headers["content-type"].startswith("text/html"), (
        "browser navigation must not be answered with JSON; got "
        f"{resp.headers.get('content-type')} / {resp.text[:120]}"
    )


def test_text_not_available_is_html_on_the_page_route(library_client):
    client, _, empty = library_client
    resp = client.get(f"/library/document/{empty}/txt")

    assert resp.status_code == 404
    assert resp.headers["content-type"].startswith("text/html"), (
        "browser navigation must not be answered with JSON; got "
        f"{resp.headers.get('content-type')} / {resp.text[:120]}"
    )


# ---------------------------------------------------------------------------
# 3. A malformed request body is a client error, not a server error
# ---------------------------------------------------------------------------

# Only routes main gated with @require_json_body(...), i.e. routes where main
# demonstrably answered 400 (the decorator ran before the handler and used
# get_json(silent=True), which swallows UnicodeDecodeError as a ValueError).
REQUIRE_JSON_BODY_ROUTES = [
    (
        "/library/api/download-bulk",
        {"error": "Request body must be valid JSON"},
    ),
    (
        "/library/api/mark-redownload",
        {"error": "Request body must be valid JSON"},
    ),
    (
        "/library/api/check-downloads",
        {"error": "Request body must be valid JSON"},
    ),
    (
        "/library/api/download-source",
        {"error": "Request body must be valid JSON"},
    ),
    (
        "/library/api/research/some-research-id/add-to-collection",
        {"success": False, "error": "Request body must be valid JSON"},
    ),
    (
        "/library/api/collections/some-collection-id/search",
        {"success": False, "error": "Request body must be valid JSON"},
    ),
]

# Well-formed JSON structure carrying one invalid UTF-8 byte inside a string.
# json.loads() raises UnicodeDecodeError for it -- a ValueError, but NOT a
# json.JSONDecodeError -- so the app's JSONDecodeError->400 handler never sees
# it. (A leading UTF-16 BOM would NOT do: json.detect_encoding() picks UTF-16
# and the failure comes back as a real JSONDecodeError, which IS handled.)
NON_UTF8_BODY = b'{"research_ids": ["\xff"]}'


def test_non_utf8_body_is_not_a_json_decode_error():
    """Anchor why route-level parsing must handle more than JSONDecodeError.

    Also pins main's side of the contract, read out of the installed werkzeug
    3.1.8 rather than restated: get_json(silent=True) -- exactly what
    @require_json_body called -- swallows this into None, which the decorator
    turned into its 400."""
    import json

    from werkzeug.test import EnvironBuilder
    from werkzeug.wrappers import Request

    with pytest.raises(UnicodeDecodeError) as excinfo:
        json.loads(NON_UTF8_BODY)
    assert not isinstance(excinfo.value, json.JSONDecodeError)

    environ = EnvironBuilder(
        path="/x",
        method="POST",
        data=NON_UTF8_BODY,
        content_type="application/json",
    ).get_environ()
    assert Request(environ).get_json(silent=True) is None


@pytest.mark.parametrize(("route", "envelope"), REQUIRE_JSON_BODY_ROUTES)
def test_non_utf8_body_is_400_not_500(library_client, route, envelope):
    client, _, _ = library_client
    resp = client.post(
        route,
        content=NON_UTF8_BODY,
        headers={"Content-Type": "application/json"},
    )

    assert resp.status_code == 400, (
        f"{route} answered {resp.status_code} for a body main rejected with "
        f"400: {resp.text[:200]}"
    )
    assert resp.json() == envelope