"""The SFM page's JavaScript lives in sfm/static/, served at /static/ (2026-11-02). Stack-wide rule: no inline JavaScript -- inline code cannot be loaded by a test, so it breaks where only a user clicking finds it. The page's two scripts were moved out verbatim; this pins that none comes back, and that the server serves them so a browser always revalidates (a new SFM never runs the old page's JS). """ from __future__ import annotations import asyncio import re from pathlib import Path import pytest SFM_DIR = Path(__file__).resolve().parent.parent / "sfm" PAGE = SFM_DIR / "sfm_webapp.html" SCRIPTS = ["device_routing.js", "sfm_webapp.js"] def _get(path: str): """One GET through the whole app (middleware included), without an HTTP client.""" from sfm.server import app msgs = [] async def receive(): return {"type": "http.request", "body": b"", "more_body": False} async def send(message): msgs.append(message) scope = {"type": "http", "asgi": {"version": "3.0"}, "http_version": "1.1", "method": "GET", "scheme": "http", "path": path, "raw_path": path.encode(), "root_path": "", "query_string": b"", "headers": [(b"host", b"sfm.local")], "server": ("sfm.local", 80), "client": ("127.0.0.1", 1)} asyncio.run(app(scope, receive, send)) start = next(m for m in msgs if m["type"] == "http.response.start") headers = {k.decode().lower(): v.decode() for k, v in start["headers"]} return start["status"], headers, b"".join(m.get("body", b"") for m in msgs if m["type"] == "http.response.body") def test_the_page_has_no_inline_javascript(): html = PAGE.read_text() tags = re.findall(r"]*)>(.*?)", html, re.S) assert tags and all("src=" in attrs and not body.strip() for attrs, body in tags), \ "an inline ', html) == SCRIPTS @pytest.mark.parametrize("name", SCRIPTS) def test_the_page_scripts_are_served_from_static(name): status, headers, body = _get(f"/static/{name}") assert status == 200 assert "javascript" in headers["content-type"] assert headers["cache-control"] == "no-cache" assert body == (SFM_DIR / "static" / name).read_bytes() def test_a_missing_static_file_is_a_404(): assert _get("/static/nope.js")[0] == 404