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.

7.0 KB
Raw Blame
"""Regression fence for the FastAPI url_for template shim.

The FastAPI app replaces Flask's url_for with a shim registered as a Jinja
global (fastapi_app._setup_template_globals). The shim maps Flask-style
'blueprint.endpoint' names to hard-coded paths via an explicit _URL_MAP;
unknown names fall through to `/{name.replace('.', '/')}`, which silently
produces dead links.

This file pins the migration audit's "22 endpoint names" claim as an
executable invariant:

* the exact set of url_for() names used across all templates is pinned, so
  adding/renaming a template link forces a deliberate look at _URL_MAP;
* every name used by a template must resolve through the shim without
  raising and yield an absolute path ("/..."), so template rendering can't
  crash or emit relative/protocol-relative links;
* every dotted name must be served by the explicit map, not the
  dot-to-slash fallback — the fallback occasionally coincides with a real
  route (benchmark.results), so the mounted-routes check in
  test_url_for_links.py alone cannot catch a name silently dropped from
  the map.

Complementary to test_url_for_links.py, which asserts the resolved paths
are actually mounted on the app; nothing here duplicates that check.
"""

import re
from pathlib import Path
from urllib.parse import parse_qs, urlsplit

import pytest

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

_URL_FOR_RE = re.compile(r"url_for\(\s*['\"]([^'\"]+)['\"]")

# The audited set of endpoint names referenced by templates at the time of
# the Flask→FastAPI migration. If this assertion fails you edited a
# template's links: update fastapi_app._URL_MAP (and test_url_for_links.py
# will verify the path is real), then update this pin.
PINNED_TEMPLATE_ENDPOINTS = frozenset(
    {
        # Auth
        "auth.change_password",
        "auth.login",
        "auth.logout",
        "auth.register",
        # Pages
        "index",
        "chat.chat_page",
        "history.history_page",
        # Library / RAG
        "library.library_page",
        "library.download_manager_page",
        "rag.collections_page",
        "rag.embedding_settings_page",
        "zotero.zotero_page",
        "unified_search.unified_search_page",
        "notes.notes_page",
        # News
        "news.news_page",
        "news.subscriptions_page",
        # Metrics
        "metrics.metrics_dashboard",
        "metrics.journal_quality",
        # Benchmark
        "benchmark.index",
        "benchmark.results",
        # Settings
        "settings.settings_page",
        "settings.save_settings",
    }
)


def _template_url_for_names() -> set:
    names = set()
    for path in TEMPLATES_DIR.rglob("*.html"):
        names.update(_URL_FOR_RE.findall(path.read_text()))
    return names


def _shim_url_for():
    # Importing fastapi_app runs the module-level _setup_template_globals()
    # call that registers the shim on the shared Jinja environment.
    import local_deep_research.web.fastapi_app  # noqa: F401
    from local_deep_research.web.template_config import templates

    return templates.env.globals["url_for"]


def _shim_url_map(url_for) -> dict:
    """Locate the shim's explicit name→path map.

    Today _URL_MAP is a closure cell of the shim; tolerate a refactor that
    hoists it to module scope.
    """
    for cell in url_for.__closure__ or ():
        try:
            contents = cell.cell_contents
        except ValueError:  # pragma: no cover — unfilled cell
            continue
        if isinstance(contents, dict):
            return contents

    import local_deep_research.web.fastapi_app as fastapi_app

    for attr in ("_URL_MAP", "URL_MAP"):
        candidate = getattr(fastapi_app, attr, None)
        if isinstance(candidate, dict):
            return candidate

    pytest.fail(
        "could not locate the url_for shim's explicit name->path map; "
        "update _shim_url_map() in this test to match the new layout"
    )


def test_audit_pin_has_exactly_22_endpoint_names():
    """The migration audit counted 22 distinct url_for endpoint names."""
    assert len(PINNED_TEMPLATE_ENDPOINTS) == 22


def test_template_url_for_name_set_matches_pin():
    """Every url_for name in templates is pinned, and vice versa."""
    extracted = _template_url_for_names()
    assert extracted, "no url_for usages found — template dir moved?"

    added = sorted(extracted - PINNED_TEMPLATE_ENDPOINTS)
    removed = sorted(PINNED_TEMPLATE_ENDPOINTS - extracted)
    assert extracted == PINNED_TEMPLATE_ENDPOINTS, (
        f"template url_for names drifted from the audited pin: "
        f"added={added} removed={removed}. Update fastapi_app._URL_MAP "
        f"for any added name, then update PINNED_TEMPLATE_ENDPOINTS."
    )


# Parametrize over the union so a freshly added template name is exercised
# even before the pin above is updated.
@pytest.mark.parametrize(
    "name", sorted(_template_url_for_names() | PINNED_TEMPLATE_ENDPOINTS)
)
def test_shim_resolves_endpoint_to_absolute_path(name):
    """The shim must resolve every template endpoint name without raising
    and return an absolute same-origin path."""
    url_for = _shim_url_for()

    path = url_for(name)  # must not raise (KeyError, TypeError, ...)

    assert isinstance(path, str)
    assert path.startswith("/"), f"{name} -> {path!r} is not absolute"
    assert not path.startswith("//"), (
        f"{name} -> {path!r} is protocol-relative (browser would treat it "
        f"as a cross-origin URL)"
    )


def test_every_template_endpoint_is_in_the_explicit_map():
    """No template name may rely on the dot-to-slash fallback.

    The fallback can coincide with a mounted route (benchmark.results), so
    the dead-link check in test_url_for_links.py cannot detect a name that
    silently drops out of _URL_MAP; a later route-function rename would
    then break the link with no test failing.
    """
    url_for = _shim_url_for()
    url_map = _shim_url_map(url_for)

    unmapped = sorted(
        name
        for name in _template_url_for_names()
        if name != "static" and name not in url_map
    )
    assert unmapped == [], (
        f"template url_for names missing from fastapi_app._URL_MAP "
        f"(currently served by the fragile dot-to-slash fallback): "
        f"{unmapped}"
    )


def test_shim_appends_kwargs_as_query_string():
    """auth/login.html calls url_for('auth.login', next=next_page); the
    shim must emit the kwarg as a URL-encoded query parameter."""
    url_for = _shim_url_for()

    result = url_for("auth.login", next="/settings/?tab=llm")

    parts = urlsplit(result)
    assert parts.path == "/auth/login"
    assert parse_qs(parts.query) == {"next": ["/settings/?tab=llm"]}


def test_shim_builds_static_paths_from_filename():
    """Flask-style url_for('static', filename=...) must map onto the
    /static mount, with the filename in the path — not the query string."""
    url_for = _shim_url_for()

    result = url_for("static", filename="css/styles.css")

    assert result == "/static/css/styles.css"