Files
seismo-relay/tests/test_webapp_static.py
T
serversdownandClaude Opus 5.5 2b886c4f39 refactor(sfm): move the page's JavaScript out of sfm_webapp.html into sfm/static/ (no code change)
Stack-wide rule since 2026-11-02 (terra-view docs/tmi-stack.md, Terra-View's
rule 4): no inline JavaScript, because a test cannot load it.  This page's tests
had to regex their code out of the HTML.  Batch 2 edits both of the page's
scripts, so they come out first, verbatim:

- sfm/static/device_routing.js: the tested routing rules (157 lines)
- sfm/static/sfm_webapp.js: the rest of the page (2,274 lines)

The page loads them with <script src="static/..."> where the inline scripts
were, so load order and the onclick handlers are unchanged.  Verified: putting
the files back inside those tags reproduces the old sfm_webapp.html byte for
byte, and a jsdom page check (served at http://sfm.local/, /static from disk,
no network) gives identical output before and after.

The server serves sfm/static/ at /static/, with Cache-Control: no-cache so a
browser revalidates (a cheap 304) and a new SFM never runs a cached old
script.  Opened from disk, the page needs its static/ folder beside it.

Tests: the routing tests load the file instead of regexing the HTML; new
test_webapp_static pins no inline <script> in the page and the /static route
(through the whole app, middleware included).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SyJ2ip3Krb6dtdv2cXua44
2026-11-02 16:17:44 -05:00

60 lines
2.4 KiB
Python

"""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"<script([^>]*)>(.*?)</script>", html, re.S)
assert tags and all("src=" in attrs and not body.strip() for attrs, body in tags), \
"an inline <script> is back in sfm_webapp.html -- put it in sfm/static/"
assert re.findall(r'<script src="static/([^"]+)"></script>', 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