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.

2.7 KB
Raw Blame
"""Regression fences for Flask idioms inside Jinja templates.

The templates render under Starlette's Jinja2Templates with a Starlette
Request in context. Starlette's Request has no `.args`/`.form` attributes
(Flask idioms) — referencing them raises UndefinedError at render time and
500s the page. library.html's pagination block did exactly that, breaking
/library/ whenever there was more than one page of documents.
"""

from pathlib import Path

from starlette.requests import Request

TEMPLATES_DIR = (
    Path(__file__).resolve().parents[3]
    / "src/local_deep_research/web/templates"
)


def _request(query_string: bytes) -> Request:
    return Request(
        scope={
            "type": "http",
            "method": "GET",
            "path": "/library/",
            "query_string": query_string,
            "headers": [],
        }
    )


def test_no_template_uses_flask_request_attributes():
    """No template may reference request.args / request.form — the
    Starlette Request in template context does not have them."""
    offenders = []
    for path in TEMPLATES_DIR.rglob("*.html"):
        source = path.read_text()
        if "request.args" in source or "request.form" in source:
            offenders.append(str(path.relative_to(TEMPLATES_DIR)))
    assert offenders == []


def test_library_pagination_filter_params_render_with_starlette_request():
    """The library.html filter_params block must build the pagination query
    string from a real Starlette Request's query_params."""
    from local_deep_research.web.template_config import templates

    source = (TEMPLATES_DIR / "pages/library.html").read_text()
    start = source.index('{% set filter_params = "" %}')
    end = source.index("<nav", start)
    snippet = source[start:end] + "{{ filter_params }}"

    rendered = templates.env.from_string(snippet).render(
        request=_request(b"page=2&collection=c%201&domain=example.org")
    )

    # Autoescape renders & as &amp; — correct inside the href attributes
    # the real template interpolates filter_params into.
    assert "&amp;collection=c%201" in rendered
    assert "&amp;domain=example.org" in rendered
    # Filters not present in the URL are not emitted.
    assert "research" not in rendered
    assert "date" not in rendered


def test_library_pagination_renders_without_filters():
    from local_deep_research.web.template_config import templates

    source = (TEMPLATES_DIR / "pages/library.html").read_text()
    start = source.index('{% set filter_params = "" %}')
    end = source.index("<nav", start)
    snippet = source[start:end] + "[{{ filter_params }}]"

    rendered = templates.env.from_string(snippet).render(
        request=_request(b"page=3")
    )

    assert "[]" in rendered