~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Fence: every url_for() name used by templates must resolve to a real route.
The FastAPI app keeps a Flask-compat url_for shim with an explicit
name→path map; unknown names fall back to `/{name.replace('.', '/')}`,
which silently produces dead links (the sidebar Chat link rendered as
/chat/chat_page — a 404 — because 'chat.chat_page' was missing from the
map). This test extracts every url_for name referenced by any template
and asserts the shim resolves it to a path actually mounted on the app.
"""
import re
from pathlib import Path
import pytest
from starlette.routing import Route
TEMPLATES_DIR = (
Path(__file__).resolve().parents[3]
/ "src/local_deep_research/web/templates"
)
_URL_FOR_RE = re.compile(r"url_for\(\s*['\"]([^'\"]+)['\"]")
def _template_url_for_names():
names = set()
for path in TEMPLATES_DIR.rglob("*.html"):
names.update(_URL_FOR_RE.findall(path.read_text(encoding="utf-8")))
return names
def test_all_template_url_for_names_resolve_to_mounted_routes():
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.template_config import templates
url_for = templates.env.globals["url_for"]
route_paths = set()
for route in app.routes:
if isinstance(route, Route):
route_paths.add(route.path)
# Pages are referenced both with and without trailing slash.
route_paths.add(route.path.rstrip("/") or "/")
names = _template_url_for_names()
assert names, "no url_for usages found — template dir moved?"
dead = []
for name in sorted(names):
path = (
url_for(name, filename="x") if name == "static" else url_for(name)
)
# static files are served by a Mount, not a Route
if path.startswith("/static"):
continue
lookup = path.split("?", 1)[0]
if lookup not in route_paths and lookup.rstrip("/") not in route_paths:
dead.append(f"{name} -> {path}")
assert dead == [], f"url_for produces dead links: {dead}"
def test_chat_sidebar_link_resolves():
"""Regression: 'chat.chat_page' fell through to the /chat/chat_page
fallback, 404ing the sidebar Chat link."""
from local_deep_research.web.template_config import templates
url_for = templates.env.globals["url_for"]
assert url_for("chat.chat_page") == "/chat/"
# ---------------------------------------------------------------------------
# Startup validator (_validate_url_for_bindings): this is the module-level
# guard that runs the same check as
# test_all_template_url_for_names_resolve_to_mounted_routes above, but at
# *app-import* time -- so a dead url_for() name refuses to boot the process
# instead of only being caught by remembering to run this test file. These
# tests exercise it directly, including the deliberate-break proof that it
# actually fires (and with a useful message) rather than being a no-op.
# ---------------------------------------------------------------------------
def test_validator_passes_against_the_real_app_and_templates():
"""The validator already ran once at import time (module body calls
it after _mount_all(app)) -- if it were going to false-positive against
the real template set, importing fastapi_app would already have raised
and this test module would have failed to collect. Calling it again
here proves it is also idempotent / independently callable, not just
an import-time side effect."""
from local_deep_research.web.fastapi_app import (
_validate_url_for_bindings,
app,
)
_validate_url_for_bindings(app) # must not raise
def test_validator_catches_a_missing_or_broken_url_map_entry(monkeypatch):
"""Deliberately break one _URL_MAP entry (mirrors the historical
chat.chat_page bug) and confirm the validator refuses to pass, naming
both the offending template name and the bad resolved path."""
from local_deep_research.web.fastapi_app import (
_validate_url_for_bindings,
app,
templates,
)
# Patch the `url_for` closure rather than the map it reads. `_URL_MAP`
# deliberately lives INSIDE _setup_template_globals as a single dict
# literal, because .pre-commit-hooks/check-url-for-targets.py parses that
# function statically and requires exactly one there — so the dict is not
# importable, and it should not be made importable just to satisfy a test.
# Patching the closure is also closer to the real failure anyway: what
# matters is the path a name RESOLVES to, whichever way it got there.
real_url_for = templates.env.globals["url_for"]
def broken_url_for(name, **kwargs):
if name == "chat.chat_page":
return "/chat/totally-wrong-path"
return real_url_for(name, **kwargs)
templates.env.globals["url_for"] = broken_url_for
try:
# The validator only RAISES under LDR_STRICT_TEMPLATE_LINKS; by
# default it logs and keeps serving, because a dead nav link must not
# be able to make the process un-bootable. CI and this test opt in.
monkeypatch.setenv("LDR_STRICT_TEMPLATE_LINKS", "1")
with pytest.raises(RuntimeError) as exc_info:
_validate_url_for_bindings(app)
message = str(exc_info.value)
assert "chat.chat_page" in message
assert "/chat/totally-wrong-path" in message
finally:
templates.env.globals["url_for"] = real_url_for
# Restored: validator passes again.
_validate_url_for_bindings(app)
def test_validator_logs_broken_links_without_stopping_production(monkeypatch):
"""Non-strict mode favors availability but must leave a diagnostic."""
from local_deep_research.web import fastapi_app
real_url_for = fastapi_app.templates.env.globals["url_for"]
def broken_url_for(name, **kwargs):
if name == "chat.chat_page":
return "/chat/production-broken-link"
return real_url_for(name, **kwargs)
monkeypatch.delenv("LDR_STRICT_TEMPLATE_LINKS", raising=False)
fastapi_app.templates.env.globals["url_for"] = broken_url_for
try:
with monkeypatch.context() as patch_context:
errors = []
patch_context.setattr(
fastapi_app.logger,
"error",
lambda message: errors.append(message),
)
result = fastapi_app._validate_url_for_bindings(fastapi_app.app)
finally:
fastapi_app.templates.env.globals["url_for"] = real_url_for
assert result is None
assert len(errors) == 1
assert "chat.chat_page" in errors[0]
assert "/chat/production-broken-link" in errors[0]
fastapi_app._validate_url_for_bindings(fastapi_app.app)
def test_validator_tolerates_an_empty_packaged_template_directory(
monkeypatch, tmp_path
):
"""Missing package templates stay diagnosable even in strict CI mode."""
from local_deep_research.web import fastapi_app
warnings = []
monkeypatch.setattr(fastapi_app, "TEMPLATE_DIR", str(tmp_path))
monkeypatch.setenv("LDR_STRICT_TEMPLATE_LINKS", "1")
monkeypatch.setattr(
fastapi_app.logger,
"warning",
lambda message: warnings.append(message),
)
result = fastapi_app._validate_url_for_bindings(fastapi_app.app)
assert result is None
assert len(warnings) == 1
assert str(tmp_path) in warnings[0]
def test_validator_does_not_treat_a_sibling_param_route_as_a_wildcard(
monkeypatch,
):
"""/chat/{session_id} is a plain string-converter route (binds one
specific, known value) that sits right next to /chat/ in the route
table. Starlette's own Route.matches() would happily match ANY string
under /chat/, including a wrong resolved path -- which would silently
swallow exactly the shape of bug this validator exists to catch. This
directly proves the previous test's failure isn't accidental: a broken
chat.chat_page entry is caught precisely because {session_id} is
excluded from the validator's wildcard-prefix matching (only Mounts and
Starlette's catch-all {name:path} converter, e.g. /static/{path:path},
are treated as wildcards)."""
from local_deep_research.web.fastapi_app import (
_validate_url_for_bindings,
app,
templates,
)
real_url_for = templates.env.globals["url_for"]
def broken_url_for(name, **kwargs):
# Any non-empty slug is a legal value for {session_id} and would
# match Route.matches() -- proving the validator uses stricter
# matching than Starlette's own route resolution.
if name == "chat.chat_page":
return "/chat/this-is-not-a-real-page"
return real_url_for(name, **kwargs)
templates.env.globals["url_for"] = broken_url_for
try:
monkeypatch.setenv("LDR_STRICT_TEMPLATE_LINKS", "1")
with pytest.raises(RuntimeError):
_validate_url_for_bindings(app)
finally:
templates.env.globals["url_for"] = real_url_for
def test_validator_accepts_static_and_mount_paths():
"""url_for("static", filename=...) resolves under the catch-all
/static/{path:path} route, and anything under a Mount (e.g. Socket.IO
at /ws) is matched by prefix -- neither is a Route object with an exact
matching path, so both must be handled without being falsely flagged."""
from starlette.routing import Mount, Route
from local_deep_research.web.fastapi_app import app
from local_deep_research.web.template_config import templates
# Sanity: the app really does mount /ws via a Mount (not a Route), and
# /static is served via a Route with a catch-all {path:path} converter
# -- i.e. this test is exercising the two genuinely different cases the
# validator has to special-case correctly instead of two Routes that
# would already be covered by plain exact-path matching.
assert any(isinstance(r, Mount) and r.path == "/ws" for r in app.routes)
assert any(
isinstance(r, Route) and r.path == "/static/{path:path}"
for r in app.routes
)
url_for = templates.env.globals["url_for"]
assert url_for("static", filename="js/app.js") == "/static/js/app.js"