~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
"""Vite asset pipeline: dev/prod switch, manifest handling, packaging.
Scope: ``web/utils/vite_helper.py``, the Vite manifest it reads from
``web/static/dist/.vite/manifest.json``, the ``vite_asset`` / ``vite_hmr``
Jinja globals it registers on the FastAPI template environment, and the
CI path that is supposed to put ``dist/`` inside a released artefact.
Two facts these tests build on rather than assume:
* ``src/local_deep_research/web/static/dist/`` is a build output. It is
gitignored and absent from a fresh checkout, so "manifest missing" is
the DEFAULT state for anyone running from source.
* ``vite_helper`` still carries the Flask-era ``init_app`` /
``_load_manifest`` pair reading ``app.config``. The FastAPI port calls
``init_for_fastapi`` instead; nothing in ``src/`` reaches the Flask
pair.
Tests are grouped so the packaging reality (does a released install even
have a manifest?) comes first, then what the app does when it does not.
"""
import ast
import hashlib
import io
import json
import re
import subprocess
import types
from contextlib import contextmanager
from html.parser import HTMLParser
from urllib.parse import unquote, urlsplit
from pathlib import Path
import jinja2
import pytest
from loguru import logger
REPO_ROOT = Path(__file__).resolve().parents[2]
SRC_PKG = REPO_ROOT / "src" / "local_deep_research"
VITE_HELPER_PY = SRC_PKG / "web" / "utils" / "vite_helper.py"
FASTAPI_APP_PY = SRC_PKG / "web" / "fastapi_app.py"
STATIC_DIR = SRC_PKG / "web" / "static"
DIST_DIR = STATIC_DIR / "dist"
VITE_CONFIG_JS = REPO_ROOT / "vite.config.js"
DOCKERFILE = REPO_ROOT / "Dockerfile"
WORKFLOWS = REPO_ROOT / ".github" / "workflows"
PUBLISH_WF = WORKFLOWS / "publish.yml"
RELEASE_GATE_WF = WORKFLOWS / "release-gate.yml"
DEV_MODE_ENV_VAR = "LDR_VITE_DEV_MODE"
MANIFEST_REL = Path("dist") / ".vite" / "manifest.json"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _fresh_helper():
"""A ViteHelper that has never been initialised.
The module also exposes a process-wide ``vite`` singleton, which
``fastapi_app`` mutates at import time. Tests use their own instance
so they neither depend on nor disturb that.
"""
from local_deep_research.web.utils.vite_helper import ViteHelper
return ViteHelper()
def _templates_stub():
"""Stand-in for ``fastapi.templating.Jinja2Templates``.
``init_for_fastapi`` only touches ``.env.globals``, and Jinja2Templates
autoescapes by default, so an autoescaping Environment is a faithful
substitute and avoids importing the whole FastAPI app.
"""
env = jinja2.Environment(autoescape=True)
return types.SimpleNamespace(env=env), env
def _write_manifest(static_dir: Path, payload, *, raw: str | None = None):
"""Write ``<static_dir>/dist/.vite/manifest.json``."""
path = static_dir / MANIFEST_REL
path.parent.mkdir(parents=True, exist_ok=True)
path.write_bytes(
raw.encode("utf-8") if raw is not None else json.dumps(payload).encode()
)
return path
@contextmanager
def _captured_warnings():
"""Collect WARNING-and-above loguru messages emitted in the block.
``local_deep_research/__init__.py`` calls
``logger.disable("local_deep_research")``, so package logging has to
be re-enabled to observe anything -- the same dance
``conftest.loguru_caplog`` does. The original disabled state is
restored on exit.
"""
messages: list[str] = []
logger.enable("local_deep_research")
sink_id = logger.add(messages.append, level="WARNING")
try:
yield messages
finally:
logger.remove(sink_id)
logger.disable("local_deep_research")
def _module_ast(path: Path) -> ast.Module:
return ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
def _literal_class_attr(path: Path, class_name: str, attr: str):
"""Read a literal class attribute straight out of the source AST.
Used instead of importing ``fastapi_app`` (which builds the whole app
at import time) so the assertion still reads the real production
value rather than a copy of it.
"""
for node in ast.walk(_module_ast(path)):
if isinstance(node, ast.ClassDef) and node.name == class_name:
for stmt in node.body:
targets = getattr(stmt, "targets", [])
if any(
isinstance(t, ast.Name) and t.id == attr for t in targets
):
return ast.literal_eval(stmt.value)
raise AssertionError(f"{class_name}.{attr} not found in {path.name}")
def _literal_module_regex(path: Path, name: str) -> str:
"""Extract the pattern string from ``NAME = re.compile(r"...")``."""
for node in _module_ast(path).body:
if not isinstance(node, ast.Assign):
continue
if not any(
isinstance(t, ast.Name) and t.id == name for t in node.targets
):
continue
assert isinstance(node.value, ast.Call), (
f"{name} is no longer a re.compile() call"
)
return ast.literal_eval(node.value.args[0])
raise AssertionError(f"{name} not found in {path.name}")
def _shell_if_block(lines: list[str], predicate) -> list[str]:
"""Return the lines of the first ``if ...; then`` block matching.
Both blocks this file inspects are flat (no nested ``if``), so
stopping at the first ``fi`` is exact.
"""
for idx, line in enumerate(lines):
if line.lstrip().startswith("if ") and predicate(line):
block = [line]
for follow in lines[idx + 1 :]:
block.append(follow)
if follow.strip() == "fi":
return block
raise AssertionError("unterminated if block")
raise AssertionError("no matching if block found")
def _dockerfile_stages() -> dict[str, list[str]]:
"""Map each ``FROM ... AS <name>`` stage to its instruction lines."""
stages: dict[str, list[str]] = {}
current = None
for line in DOCKERFILE.read_text(encoding="utf-8").splitlines():
match = re.match(r"^FROM\s+.*\s+AS\s+(\S+)\s*$", line)
if match:
current = match.group(1)
stages[current] = []
continue
if current is not None:
stages[current].append(line)
return stages
# ---------------------------------------------------------------------------
# What a released install actually gets
# ---------------------------------------------------------------------------
class TestReleasedInstallGetsDist:
"""Whether ``dist/`` reaches a wheel / Docker image at all."""
def test_dist_is_an_untracked_build_output(self):
"""A checkout may be built locally; Git must not track the output."""
assert STATIC_DIR.is_dir(), (
"static/ itself must be in the repo; only dist/ is generated"
)
gitignore = (REPO_ROOT / ".gitignore").read_text(encoding="utf-8")
ignore_lines = [ln.strip() for ln in gitignore.splitlines()]
assert "dist/" in ignore_lines, (
".gitignore no longer carries a bare `dist/` rule; static/dist "
"may now be committable"
)
assert not any(
ln.startswith("!") and "static/dist" in ln for ln in ignore_lines
), (
"a negation re-includes static/dist -- build output could be committed"
)
# Authoritative cross-check when git is usable in this checkout.
try:
tracked = subprocess.run(
["git", "ls-files", "--", str(DIST_DIR)],
cwd=REPO_ROOT,
capture_output=True,
timeout=30,
)
if tracked.returncode == 0:
assert not tracked.stdout.strip(), (
"static/dist/ contains tracked files; frontend assets "
"must be generated during the build"
)
completed = subprocess.run(
["git", "check-ignore", "-q", str(DIST_DIR)],
cwd=REPO_ROOT,
capture_output=True,
timeout=30,
)
except (OSError, subprocess.SubprocessError): # pragma: no cover
return
if completed.returncode in (0, 1):
assert completed.returncode == 0, (
"git does not consider src/.../web/static/dist ignored"
)
def test_release_builds_the_frontend_and_fails_closed_before_packaging(
self,
):
"""The gate builds the exact distributions later downloaded by publish.yml."""
text = RELEASE_GATE_WF.read_text(encoding="utf-8")
lines = text.splitlines()
publish = PUBLISH_WF.read_text(encoding="utf-8")
assert (
"artifact-ids: ${{ github.event.client_payload.artifact_id }}"
in publish
)
assert "pdm build" not in publish
assert "npm run build" in text, (
"release-gate.yml no longer builds the frontend; a PyPI release would "
"ship without static/dist"
)
pre_pack_check = _shell_if_block(
lines,
lambda ln: (
"src/local_deep_research/web/static/dist/.vite/manifest.json"
in ln
and "wheel_contents" not in ln
),
)
assert any("exit 1" in ln for ln in pre_pack_check), (
"the post-npm-build manifest check stopped being fatal; a failed "
"Vite build would now flow into `pdm build` unnoticed"
)
assert "python .github/scripts/release_package.py create" in text
def test_publisher_verifies_the_tested_frontend_artifact(self):
"""The producer and publisher both enforce the frontend artifact contract.
Real wheel fixtures exercise missing manifest, JS and CSS failures in
tests/ci/standalone/test_release_package.py without loading the app.
"""
text = PUBLISH_WF.read_text(encoding="utf-8")
assert "release_package.py verify" in text
assert (
"EXPECTED_HASHES: ${{ needs.build-package.outputs.file_hashes }}"
in text
)
assert "release_package.py published" in text
assert "npm run build" not in text
assert "pdm build" not in text
def test_release_gate_verifies_installed_frontend_assets(self):
"""Wheel and sdist installation must preserve the Vite manifest."""
text = RELEASE_GATE_WF.read_text(encoding="utf-8")
assert "npm run build" in text and "pdm build" in text
assert "pip install --no-cache-dir" in text
assert "release_package.py create" in text
assert 'assets.joinpath(".vite/manifest.json").is_file()' in text
assert (
'resources.files("local_deep_research").joinpath("web/static/dist")'
in text
)
def test_production_image_gets_dist_only_through_the_wheel(self):
"""The runtime Docker stage has no independent copy of dist/.
``ldr-test`` copies ``static/dist/`` out of ``builder`` explicitly.
The production ``ldr`` stage does not: it copies only
``/install/.venv/``, so every byte of ``dist/`` -- manifest
included -- reaches production solely via what ``pdm install
--prod --no-editable`` packaged. Nothing in the image verifies
the manifest survived that step.
"""
stages = _dockerfile_stages()
assert {"builder", "ldr-test", "ldr"} <= set(stages), (
f"Dockerfile stages changed: {sorted(stages)}"
)
builder = "\n".join(stages["builder"])
build_at = builder.index("npm run build")
install_at = builder.index("pdm install --prod --no-editable")
assert build_at < install_at, (
"pdm install now runs before npm run build; the wheel would be "
"packaged from a source tree with no dist/"
)
test_stage = "\n".join(stages["ldr-test"])
assert "COPY --from=builder" in test_stage
assert "web/static/dist/" in test_stage, (
"ldr-test lost its explicit dist copy"
)
runtime = "\n".join(stages["ldr"])
assert "/install/.venv/" in runtime, (
"the runtime stage no longer copies the venv from builder"
)
assert "static/dist" not in runtime, (
"the runtime stage now copies dist/ directly -- if so it no "
"longer depends on wheel packaging and this test should say so"
)
assert "manifest.json" not in runtime, (
"the runtime stage now checks for the manifest; assert it "
"instead of pinning its absence"
)
class TestManifestPathContract:
"""The helper's hardcoded path vs. where Vite actually writes."""
def test_helper_reads_the_path_vite_is_configured_to_write(self, tmp_path):
"""``vite.config.js`` outDir + manifest -> ``dist/.vite/manifest.json``.
The helper hardcodes that path, so a change to ``build.outDir`` or
to Vite's manifest location silently produces the
missing-manifest fallback. Executed: the warning names the exact
path the helper looked at.
"""
config = VITE_CONFIG_JS.read_text(encoding="utf-8")
assert re.search(
r"root:\s*'src/local_deep_research/web/static'", config
)
assert re.search(r"outDir:\s*'dist'", config), (
"Vite's build.outDir is no longer 'dist'; vite_helper's "
"hardcoded dist/.vite/manifest.json path is now wrong"
)
assert re.search(r"manifest:\s*true", config), (
"Vite no longer emits a manifest; vite_asset can never resolve "
"a hashed filename"
)
helper = _fresh_helper()
stub, _env = _templates_stub()
with _captured_warnings() as warnings:
helper.init_for_fastapi(str(tmp_path), stub)
expected = str(tmp_path / MANIFEST_REL)
assert any(expected in str(msg) for msg in warnings), (
f"expected the warning to name {expected}; got {list(warnings)}"
)
def test_vite_hashed_output_names_qualify_for_immutable_caching(self):
"""Real Vite filenames must match the immutable-cache regex.
``serve_static`` grants ``max-age=31536000, immutable`` only to
paths matching ``_HASHED_FILENAME_RE``. Vite's configured
``[name].[hash]`` templates produce 8-character hashes from the
base64url alphabet; if the regex and the hash width ever drift,
every built asset silently drops to ``must-revalidate``.
"""
pattern = re.compile(
_literal_module_regex(
FASTAPI_APP_PY.with_name("static_files.py"),
"_HASHED_FILENAME_RE",
)
)
config = VITE_CONFIG_JS.read_text(encoding="utf-8")
assert "js/[name].[hash].js" in config
assert "css/[name].[hash][extname]" in config
for built in (
"js/app.Dk3xY1aB.js",
"js/vendor.a_b-c1D2.js",
"css/styles.Ab12Cd34.css",
"fonts/fa-solid.9zQw8ErT.woff2",
):
assert pattern.search(built), (
f"{built} is a plausible Vite output but would be served "
"must-revalidate instead of immutable"
)
for unhashed in ("js/app.js", "css/themes.css", "sounds/success.mp3"):
assert not pattern.search(unhashed)
# ---------------------------------------------------------------------------
# Manifest handling
# ---------------------------------------------------------------------------
class TestMissingOrBrokenManifest:
"""Missing / empty / malformed manifest: loud or silently broken?"""
def test_a_rebuild_landing_during_startup_reaches_the_first_render(
self, tmp_path, monkeypatch
):
"""Catches fingerprinting the file instead of the bytes that were read.
The startup read returns the old manifest and the rebuild lands
immediately afterwards. The fingerprint stored at startup must be
sha256 of the bytes now in ``helper.manifest`` (the old ones), not of
whatever the file holds by the time it is taken: pair the old data
with the new file's key and the next request sees "unchanged" and
serves OLDHASH URLs at files ``emptyOutDir`` has already deleted.
"""
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
old_manifest = {"js/app.js": {"file": "js/app.OLDHASH.js"}}
new_manifest = {"js/app.js": {"file": "js/app.NEWHASH.js"}}
manifest_path = _write_manifest(tmp_path, old_manifest)
for relative_path in ("js/app.OLDHASH.js", "js/app.NEWHASH.js"):
output = tmp_path / "dist" / relative_path
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("// built", encoding="utf-8")
original = manifest_path.read_bytes()
replacement = json.dumps(new_manifest).encode("utf-8")
real_open = Path.open
swapped = False
def open_then_swap(path, *args, **kwargs):
"""Serve the pre-rebuild bytes, then let the rebuild land."""
nonlocal swapped
if path != manifest_path or swapped:
return real_open(path, *args, **kwargs)
swapped = True
manifest_path.write_bytes(replacement)
return io.BytesIO(original)
monkeypatch.setattr(Path, "open", open_then_swap)
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
assert manifest_path.read_bytes() == replacement
assert helper.manifest == old_manifest
assert (
helper._manifest_fingerprint == hashlib.sha256(original).hexdigest()
)
html = env.from_string("{{ vite_asset('js/app.js') }}").render()
assert helper.manifest == new_manifest
assert "js/app.NEWHASH.js" in html
def test_missing_manifest_warns_then_serves_a_page_with_no_assets(
self, tmp_path, monkeypatch
):
"""The from-source default: one warning, then a JS-less page.
The app does NOT refuse to start and does NOT surface anything to
the user: every template that calls ``vite_asset`` renders an HTML
comment where its script and stylesheet tags should be.
"""
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
helper = _fresh_helper()
stub, env = _templates_stub()
with _captured_warnings() as warnings:
helper.init_for_fastapi(str(tmp_path), stub)
assert helper.manifest == {}
joined = "\n".join(str(m) for m in warnings)
assert "npm run build" in joined, (
"the prod missing-manifest warning lost its remediation hint"
)
assert DEV_MODE_ENV_VAR in joined
html = env.from_string(
"{{ vite_hmr() }}{{ vite_asset('js/app.js') }}"
).render()
assert "Vite build not found" in html
assert "<script" not in html, (
"the fallback now emits a script tag; the whole point of this "
"test is that a manifest-less prod install ships zero JS"
)
assert "<link" not in html, "the fallback now emits a stylesheet link"
def test_missing_manifest_is_expected_and_silent_in_dev_mode(
self, tmp_path, monkeypatch
):
"""Dev mode must not cry wolf: Vite serves from memory there."""
monkeypatch.setenv(DEV_MODE_ENV_VAR, "true")
helper = _fresh_helper()
stub, _env = _templates_stub()
with _captured_warnings() as warnings:
helper.init_for_fastapi(str(tmp_path), stub)
assert helper.is_dev is True
assert not [m for m in warnings if "Vite manifest not found" in str(m)]
def test_an_empty_json_object_manifest_is_refused_and_warns(
self, tmp_path, monkeypatch
):
"""Catches gating the startup warning on ``manifest_path.exists()``.
A ``{}`` manifest passes an existence check, so a presence-gated
warning stays silent while ``vite_asset`` falls through to the empty
comment: a truncated or partially-written build ships a completely
silent, completely JS-less deployment. The warning has to key off
"is there a usable manifest in memory", and it has to name the file
so the operator is not sent looking for a missing one.
"""
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
manifest_path = _write_manifest(tmp_path, {})
helper = _fresh_helper()
stub, env = _templates_stub()
with _captured_warnings() as warnings:
helper.init_for_fastapi(str(tmp_path), stub)
assert helper.manifest == {}
joined = "\n".join(str(m) for m in warnings)
assert str(manifest_path) in joined
assert "not a non-empty JSON object" in joined
html = env.from_string("{{ vite_asset('js/app.js') }}").render()
assert "Vite build not found" in html
assert "<script" not in html
def test_an_entry_present_but_unknown_also_degrades_silently(
self, tmp_path, monkeypatch
):
"""A manifest keyed on a renamed entry point yields no tags."""
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
_write_manifest(
tmp_path, {"js/main.js": {"file": "js/main.abc12345.js"}}
)
output = tmp_path / "dist" / "js" / "main.abc12345.js"
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("// built", encoding="utf-8")
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
html = env.from_string("{{ vite_asset('js/app.js') }}").render()
assert "Vite build not found" in html
assert "js/main.abc12345.js" not in html
@pytest.mark.parametrize(
("label", "raw"),
[
("truncated", '{"js/app.js": {"file": "js/app.abc12345.js"'),
("zero-byte", ""),
("html-error-page", "<!doctype html><h1>502</h1>"),
],
)
def test_a_malformed_manifest_fails_closed_at_startup(
self, tmp_path, monkeypatch, label, raw
):
"""Malformed bytes must degrade at startup and allow later recovery.
Truncated JSON, an empty file and an HTML error page all fail JSON
decoding. Without the ValueError guard, each aborts initialization.
The separate empty-object case covers the manifest-shape check.
A later valid build must be adopted without restarting the helper.
"""
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
_write_manifest(tmp_path, None, raw=raw)
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
assert helper.manifest == {}
html = env.from_string("{{ vite_asset('js/app.js') }}").render()
assert "Vite build not found" in html
assert "ldr-vite-missing-assets-banner" in str(
helper.missing_assets_banner()
)
payload = {"js/app.js": {"file": "js/app.abc12345.js"}}
_write_manifest(tmp_path, payload)
output = tmp_path / "dist" / "js" / "app.abc12345.js"
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("// built", encoding="utf-8")
assert (
"js/app.abc12345.js"
in env.from_string("{{ vite_asset('js/app.js') }}").render()
)
def test_a_bom_prefixed_manifest_is_tolerated_by_the_one_reader(
self, tmp_path, monkeypatch
):
"""Catches a revert to ``decode("utf-8")`` in the single reader.
The Flask and FastAPI entry points both delegate to
``_adopt_manifest_from_disk``, so BOM tolerance lives in exactly one
place -- asserting it twice would only exercise the same branch
twice. What is worth pinning is that the two entry points really do
converge: they must end up with the same parsed manifest *and* the
same fingerprint for identical bytes, because a second reader with
its own decoding rules is how the two paths would drift apart.
"""
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
payload = {"js/app.js": {"file": "js/app.abc12345.js"}}
_write_manifest(tmp_path, None, raw="\ufeff" + json.dumps(payload))
output = tmp_path / "dist" / "js" / "app.abc12345.js"
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("// built", encoding="utf-8")
helper = _fresh_helper()
stub, _env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
assert helper.manifest == payload, (
"the BOM a Windows editor leaves on manifest.json is no longer "
"stripped; a valid build now reads as malformed"
)
flask_helper = _fresh_helper()
flask_helper.app = types.SimpleNamespace(
config={"STATIC_DIR": str(tmp_path)}
)
flask_helper._load_manifest()
assert (
flask_helper.manifest,
flask_helper._manifest_fingerprint,
) == (helper.manifest, helper._manifest_fingerprint), (
"the Flask and FastAPI entry points no longer share one reader"
)
def test_a_manifest_entry_without_a_file_key_fails_closed_at_render_time(
self, tmp_path, monkeypatch
):
"""Catches dropping the per-entry `'file'` string check.
This manifest is perfectly good JSON and its stylesheet really is on
disk, so adoption succeeds and startup says nothing - the entry is
only unservable at render time, which is exactly the case
`_entry_problem()` exists for. Remove that check and the render path
emits the <link> tags and then subscripts a key that isn't there,
turning a degraded page into a KeyError out of Jinja on every
request.
"""
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
_write_manifest(
tmp_path, {"js/app.js": {"css": ["css/styles.abc12345.css"]}}
)
stylesheet = tmp_path / "dist" / "css" / "styles.abc12345.css"
stylesheet.parent.mkdir(parents=True, exist_ok=True)
stylesheet.write_text("/* built */", encoding="utf-8")
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
html = env.from_string("{{ vite_asset('js/app.js') }}").render()
assert "Vite build not found" in html
assert "ldr-vite-missing-assets-banner" in str(
helper.missing_assets_banner()
)
def test_manifest_setup_runs_at_import_and_its_caller_does_not_catch(
self,
):
"""The manifest read happens at import, unguarded *by its caller*.
``init_for_fastapi`` has plenty of error handling of its own -- it
fails closed on every unusable manifest -- so the property here is
narrower than "no error handling": ``_setup_template_globals`` runs
at module scope and does not wrap the call in ``try``, which is what
makes an unexpected failure abort the import rather than degrade
invisibly. Both halves are asserted below; the name used to promise
the broader, false claim.
"""
tree = _module_ast(FASTAPI_APP_PY)
called_at_module_scope = any(
isinstance(node, ast.Expr)
and isinstance(node.value, ast.Call)
and isinstance(node.value.func, ast.Name)
and node.value.func.id == "_setup_template_globals"
for node in tree.body
)
assert called_at_module_scope, (
"_setup_template_globals is no longer called at import time; "
"the manifest read may have moved to startup/lifespan"
)
setup = next(
node
for node in tree.body
if isinstance(node, ast.FunctionDef)
and node.name == "_setup_template_globals"
)
guarded = [
call
for guard in ast.walk(setup)
if isinstance(guard, ast.Try)
for call in ast.walk(guard)
if isinstance(call, ast.Call)
and isinstance(call.func, ast.Attribute)
and call.func.attr == "init_for_fastapi"
]
all_calls = [
call
for call in ast.walk(setup)
if isinstance(call, ast.Call)
and isinstance(call.func, ast.Attribute)
and call.func.attr == "init_for_fastapi"
]
assert len(all_calls) == 1, (
"expected exactly one vite.init_for_fastapi call in "
f"_setup_template_globals, found {len(all_calls)}"
)
assert not guarded, (
"init_for_fastapi is now wrapped in try/except -- a malformed "
"manifest no longer aborts import; re-check what it degrades to"
)
# ---------------------------------------------------------------------------
# The dev/prod switch
# ---------------------------------------------------------------------------
class TestDevProdSwitch:
"""``LDR_VITE_DEV_MODE`` and what it emits."""
def test_env_var_flips_the_helper_to_dev_server_script_tags(
self, tmp_path, monkeypatch
):
"""Executed end to end through the real env-settings registry."""
monkeypatch.setenv(DEV_MODE_ENV_VAR, "true")
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
html = env.from_string(
"{{ vite_hmr() }}{{ vite_asset('js/app.js') }}"
).render()
assert (
'<script type="module" '
'src="http://localhost:5173/@vite/client"></script>' in html
), f"vite_hmr did not emit the dev-server client tag: {html!r}"
assert 'src="http://localhost:5173/js/app.js"' in html
def test_dev_mode_overrides_a_perfectly_good_built_manifest(
self, tmp_path, monkeypatch
):
"""Dev mode wins even when ``dist/`` is present and valid.
``init_for_fastapi`` still reads the manifest into memory, but
``vite_asset`` never consults it -- so a production deployment
that inherits ``LDR_VITE_DEV_MODE=true`` (a stray value in a
compose file or .env) serves dev-server URLs while a complete,
correct build sits unused on disk.
"""
monkeypatch.setenv(DEV_MODE_ENV_VAR, "1")
payload = {
"js/app.js": {
"file": "js/app.abc12345.js",
"css": ["css/styles.def67890.css"],
}
}
_write_manifest(tmp_path, payload)
for relative_path in (
"js/app.abc12345.js",
"css/styles.def67890.css",
):
output = tmp_path / "dist" / relative_path
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("// built", encoding="utf-8")
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
assert helper.manifest == payload, (
"the manifest is read regardless of dev mode; if that changed, "
"this test's premise did too"
)
html = env.from_string("{{ vite_asset('js/app.js') }}").render()
assert "js/app.abc12345.js" not in html
assert "css/styles.def67890.css" not in html
assert "http://localhost:5173/js/app.js" in html
def test_dev_mode_tags_are_plain_http_and_violate_the_apps_own_csp(
self, tmp_path, monkeypatch
):
"""DEFECT: the dev switch emits assets the app's CSP then blocks.
``vite_hmr``/``vite_asset`` hardcode ``http://localhost:5173``,
but ``SecurityHeadersMiddleware`` sends an unconditional
``script-src 'self'`` and ``connect-src 'self'``
on every response, dev mode included. A browser loading the
Python-rendered page therefore refuses both the module script and
the HMR websocket, and the ``http://`` scheme is additionally
mixed content behind any TLS terminator. Whatever the intent,
turning the flag on in production yields a page with no working
JS -- the same end state as a missing manifest, with no warning.
"""
monkeypatch.setenv(DEV_MODE_ENV_VAR, "true")
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
html = env.from_string(
"{{ vite_hmr() }}{{ vite_asset('js/app.js') }}"
).render()
origins = set(re.findall(r'src="(https?://[^/"]+)', html))
assert origins == {"http://localhost:5173"}, (
f"unexpected dev asset origins: {origins}"
)
csp = _literal_class_attr(
FASTAPI_APP_PY, "SecurityHeadersMiddleware", "CSP"
)
directives = {}
for part in csp.split(";"):
part = part.strip()
if part:
name, _, value = part.partition(" ")
directives[name] = value.strip()
assert directives["script-src"] == "'self'", (
f"script-src changed to {directives['script-src']!r}; recheck "
"whether the dev-server origin is now allowed"
)
assert "localhost:5173" not in csp
assert directives["connect-src"] == "'self'", (
"connect-src changed; the HMR websocket may now be permitted"
)
def test_the_flask_era_config_key_is_not_the_env_var_that_works(
self, monkeypatch
):
"""``VITE_DEV_MODE`` (Flask config) vs ``LDR_VITE_DEV_MODE`` (env).
The dead ``init_app`` reads ``app.config["VITE_DEV_MODE"]``. The
live path reads the registry setting ``vite.dev_mode``, whose
environment variable is ``LDR_VITE_DEV_MODE`` -- the one
documented in CONFIGURATION.md. Exporting the bare
``VITE_DEV_MODE`` name on a deployment, as the surviving Flask
code still suggests, does nothing at all.
"""
from local_deep_research.settings.env_registry import registry
assert registry.get_env_var("vite.dev_mode") == DEV_MODE_ENV_VAR
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
monkeypatch.setenv("VITE_DEV_MODE", "true")
assert registry.get("vite.dev_mode") is False, (
"the bare Flask-era name now toggles dev mode; the two config "
"surfaces have been unified and this test should say so"
)
monkeypatch.setenv(DEV_MODE_ENV_VAR, "true")
assert registry.get("vite.dev_mode") is True
helper_src = VITE_HELPER_PY.read_text(encoding="utf-8")
assert 'app.config.get("VITE_DEV_MODE"' in helper_src, (
"the Flask config key is gone from vite_helper; drop the "
"first half of this test"
)
docs = (REPO_ROOT / "docs" / "CONFIGURATION.md").read_text(
encoding="utf-8"
)
assert DEV_MODE_ENV_VAR in docs
assert "| `VITE_DEV_MODE` |" not in docs, (
"docs advertise the bare Flask config key, which the FastAPI "
"path never reads"
)
# ---------------------------------------------------------------------------
# Asset integrity
# ---------------------------------------------------------------------------
class TestManifestAssetUrls:
"""Manifest filenames stay URL data inside generated script/link tags.
The helper now percent-encodes asset paths before marking the tags safe.
These regressions require exactly the intended attributes and elements,
while retaining the original filename after URL decoding.
"""
class Tags(HTMLParser):
def __init__(self):
super().__init__()
self.tags = []
def handle_starttag(self, tag, attrs):
self.tags.append((tag, dict(attrs)))
PAYLOAD_FILE = 'js/app.abc12345.js" onload="alert(1)'
PAYLOAD_CSS = 'css/x.css"><script>alert(1)</script><link href="'
def _render_with(self, tmp_path, entry, monkeypatch):
monkeypatch.delenv(DEV_MODE_ENV_VAR, raising=False)
_write_manifest(tmp_path, {"js/app.js": entry})
for relative_path in [entry["file"], *entry.get("css", [])]:
output = tmp_path / "dist" / relative_path
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("// built", encoding="utf-8")
helper = _fresh_helper()
stub, env = _templates_stub()
helper.init_for_fastapi(str(tmp_path), stub)
return env, env.from_string("{{ vite_asset('js/app.js') }}").render()
def test_the_template_environment_does_escape_when_not_bypassed(
self, tmp_path, monkeypatch
):
"""Positive control for the two tests below.
Proves the escaping that ``vite_asset`` loses is otherwise
present: the identical payload rendered as an ordinary variable
comes back with its quote and angle brackets encoded.
"""
_env, _ = self._render_with(
tmp_path, {"file": "js/app.abc12345.js"}, monkeypatch
)
escaped = _env.from_string('<script src="{{ v }}">').render(
v=self.PAYLOAD_CSS
)
assert ""><script>" in escaped, (
f"the Jinja environment is not autoescaping: {escaped!r}"
)
assert "<script>alert(1)</script>" not in escaped
def test_file_value_stays_inside_the_encoded_src_attribute(
self, tmp_path, monkeypatch
):
"""Encoding replaces the previous raw filename interpolation.
A quote in a valid local filename must not introduce an event
attribute, and the emitted URL must still address that filename.
"""
_env, html = self._render_with(
tmp_path, {"file": self.PAYLOAD_FILE}, monkeypatch
)
parser = self.Tags()
parser.feed(html)
assert len(parser.tags) == 1
tag, attrs = parser.tags[0]
assert tag == "script"
assert set(attrs) == {"type", "src"}
assert attrs["type"] == "module"
url = urlsplit(attrs["src"])
assert not url.query and not url.fragment
assert unquote(url.path) == f"/static/dist/{self.PAYLOAD_FILE}"
def test_css_filename_cannot_create_additional_elements(
self, tmp_path, monkeypatch
):
"""CSS filenames are encoded by the same production URL contract.
The previous raw interpolation could terminate a link tag. The
filename now stays in href while the ordinary app script survives.
"""
_env, html = self._render_with(
tmp_path,
{"file": "js/app.abc12345.js", "css": [self.PAYLOAD_CSS]},
monkeypatch,
)
parser = self.Tags()
parser.feed(html)
assert [tag for tag, _ in parser.tags] == ["link", "script"]
attrs = parser.tags[0][1]
assert set(attrs) == {"rel", "href"}
assert attrs["rel"] == "stylesheet"
url = urlsplit(attrs["href"])
assert not url.query and not url.fragment
assert unquote(url.path) == f"/static/dist/{self.PAYLOAD_CSS}"
assert parser.tags[1][1] == {
"type": "module",
"src": "/static/dist/js/app.abc12345.js",
}
# ---------------------------------------------------------------------------
# The dead Flask half
# ---------------------------------------------------------------------------
class TestFlaskPathIsUnreachable:
"""``init_app`` / ``_load_manifest`` are dead but still shipped."""
def test_no_module_under_src_reaches_the_flask_entry_points(self):
"""Only ``init_for_fastapi`` is called from production code."""
importers = []
constructions = []
for path in SRC_PKG.rglob("*.py"):
text = path.read_text(encoding="utf-8")
if "vite_helper" in text and path != VITE_HELPER_PY:
importers.append(path.relative_to(SRC_PKG).as_posix())
if path == VITE_HELPER_PY:
continue
if re.search(r"\bViteHelper\s*\(", text):
constructions.append(path.relative_to(SRC_PKG).as_posix())
assert importers == ["web/fastapi_app.py"], (
f"vite_helper gained importers: {importers}"
)
assert constructions == [], (
f"production code constructs ViteHelper directly: {constructions} "
"-- ViteHelper(app) would run the Flask init_app path"
)
app_src = FASTAPI_APP_PY.read_text(encoding="utf-8")
assert "vite.init_for_fastapi(" in app_src
assert "vite.init_app(" not in app_src
assert "_load_manifest" not in app_src
def test_the_shipped_singleton_is_constructed_without_an_app(self):
"""``vite = ViteHelper()`` -- so ``init_app`` cannot run at import.
Asserted on the live object, and stable whether or not
``fastapi_app`` was imported earlier in the session: that path
sets ``is_dev``/``manifest`` but never ``app``.
"""
from local_deep_research.web.utils.vite_helper import ViteHelper, vite
assert isinstance(vite, ViteHelper)
assert vite.app is None, (
"something bound a Flask app onto the shipped ViteHelper "
"singleton -- the dead init_app path is live again"
)
def test_the_dead_pair_is_kept_covered_by_its_own_legacy_unit_test(self):
"""Why nothing flags this code as dead.
``tests/web/utils/test_vite_helper.py`` calls ``init_app`` and
``_load_manifest`` directly, so coverage reports them as fully
exercised even though no caller exists outside tests. Recorded
here so that deleting the Flask pair is understood to require
deleting those tests too -- and so a green coverage number is not
mistaken for evidence the code is reachable.
"""
legacy = REPO_ROOT / "tests" / "web" / "utils" / "test_vite_helper.py"
legacy_src = legacy.read_text(encoding="utf-8")
assert "helper.init_app(mock_app)" in legacy_src
assert "helper._load_manifest()" in legacy_src
assert "init_for_fastapi" not in legacy_src, (
"the legacy unit test now covers the FastAPI path too; this "
"file's coverage-illusion note needs updating"
)
helper_src = VITE_HELPER_PY.read_text(encoding="utf-8")
assert "def init_app(self, app):" in helper_src
assert "def _load_manifest(self):" in helper_src