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.

4.6 KB
Raw Blame
"""End-to-end rendering test for the PDF export endpoint (FastAPI port).

Ported from ``tests/web/routes/test_export_pdf_rendering.py``, deleted by the
FastAPI migration. Drives POST /api/v1/research/{id}/export/pdf — the endpoint
the UI's "Download PDF" button calls — through the real exporter chain
(ExporterRegistry -> PDFExporter -> PDFService -> WeasyPrint). Only the data
layer (DB session, report assembly) is mocked, so the returned bytes are a
genuine WeasyPrint render.

Regression coverage for the digit-capture bug (#5050): listing an emoji family
in the PDF font stacks made Pango draw every digit 0-9 with Noto Color Emoji,
so numbers in exported reports rendered as wide, square emoji glyphs
("2 0 2 6" instead of "2026"). ``tests/web/services/test_pdf_service.py``
asserts the same property on PDFService directly; this test pins it to the
HTTP surface so a change anywhere in the export chain (route, exporter
registry, service) that reintroduces the bug still fails CI.
"""

import io
from contextlib import contextmanager
from unittest.mock import MagicMock, patch

import pytest
from fastapi.testclient import TestClient

MODULE = "local_deep_research.web.routers.research"
ASSEMBLY_MOD = "local_deep_research.web.services.report_assembly_service"

# Digit-heavy markdown mirroring a real research report (dates, prices,
# token counts, citation markers) — the shapes that were unreadable.
REPORT_MARKDOWN = (
    "# Pricing report 2026-07-11\n\n"
    "As of July 11, 2026, plans cost $20/month and allow 44,000 tokens "
    "per rolling 5-hour window [21][30].\n"
)


@pytest.fixture()
def client():
    """TestClient authenticated as ``testuser`` via dependency override."""
    from local_deep_research.web.dependencies.auth import require_auth
    from local_deep_research.web.fastapi_app import app

    app.dependency_overrides[require_auth] = lambda: "testuser"
    try:
        test_client = TestClient(app)
        # CSRFMiddleware is unconditional on the FastAPI app; a POST needs a
        # session-bound token. Flask's test client bypassed CSRF in TESTING
        # mode, so the original had no equivalent step.
        token = test_client.get("/auth/csrf-token").json()["csrf_token"]
        test_client.headers.update({"X-CSRFToken": token})
        yield test_client
    finally:
        app.dependency_overrides.pop(require_auth, None)


@contextmanager
def _ctx(session):
    yield session


def _digit_fonts(pdf_bytes):
    """Return the set of font names used to draw digit glyphs."""
    from pdfminer.high_level import extract_pages
    from pdfminer.layout import LTChar, LTTextContainer, LTTextLine

    fonts = set()
    for page in extract_pages(io.BytesIO(pdf_bytes)):
        for element in page:
            if not isinstance(element, LTTextContainer):
                continue
            for line in element:
                if not isinstance(line, LTTextLine):
                    continue
                for char in line:
                    if isinstance(char, LTChar) and char.get_text().isdigit():
                        fonts.add(char.fontname)
    return fonts


def test_export_pdf_endpoint_renders_digits_with_text_font(client):
    """The exported PDF must draw digits with a text font, not an emoji font.

    On hosts with no emoji font installed the emoji-font assertion passes
    trivially, which is correct — there is no emoji font to capture the
    digits.
    """
    from local_deep_research.web.services.pdf_service import (
        weasyprint_available,
    )

    if not weasyprint_available():
        pytest.skip("WeasyPrint system libraries not available")
    pytest.importorskip("pdfminer.high_level")

    research = MagicMock()
    research.id = "res-1"
    research.title = "Pricing report"
    research.query = "pricing"
    mock_session = MagicMock()
    mock_session.query.return_value.filter_by.return_value.first.return_value = research

    with (
        patch(f"{MODULE}.get_user_db_session", return_value=_ctx(mock_session)),
        patch(
            f"{ASSEMBLY_MOD}.assemble_full_report",
            return_value=REPORT_MARKDOWN,
        ),
    ):
        resp = client.post("/api/v1/research/res-1/export/pdf")

    assert resp.status_code == 200, resp.text[:500]
    assert resp.headers["content-type"].startswith("application/pdf")
    assert resp.content[:4] == b"%PDF"

    digit_fonts = _digit_fonts(resp.content)
    assert digit_fonts, "expected the rendered PDF to contain digits"
    emoji_fonts = {f for f in digit_fonts if "emoji" in f.lower()}
    assert not emoji_fonts, (
        f"digits in the exported PDF were rendered with an emoji font: "
        f"{emoji_fonts}"
    )