Files
seismo-relay/tests/test_webapp_static.py
serversdownandClaude Opus 5.5 385716d8e1 feat(webapp): edit a unit's settings by serial: preview, send, read back
The Config tab by serial is now the unit's edit form, headed 'On the
unit <serial> -- read HH:MM' and built from the server's editable
fields.  Preview runs a dry run and shows old -> new, notes and anything
that blocks the change; Send (confirm) sends exactly what was previewed,
with its revision, then shows what the unit now holds and re-reads it.
An edit after a preview disables Send until the next preview.  The
by-host form loses its write button; the read stays.  Logic in
device_routing.js (Node-tested), glue in sfm_webapp.js, no inline JS.

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

74 lines
3.1 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
def test_the_config_write_is_the_audited_preview_and_send():
# Phase 5: the legacy by-host "Write to Device" is gone; the serial-mode edit
# form's Preview and Send are wired from sfm_webapp.js, not inline handlers.
html = PAGE.read_text()
assert 'id="cfg-write-btn"' not in html
for el in ("cfg-edit", "cfg-preview-btn", "cfg-send-btn", "cfg-preview", "cfg-result"):
tag = re.search(r'<[^>]*id="%s"[^>]*>' % el, html)
assert tag is not None and "onclick" not in tag.group(0), el
js = (PAGE.parent / "static" / "sfm_webapp.js").read_text()
assert "config_write" not in js and "function writeConfig" not in js
assert "getElementById('cfg-preview-btn').addEventListener" in js
assert "getElementById('cfg-send-btn').addEventListener" in js