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
This commit is contained in:
1 parent
f98c4ff129
commit
2b886c4f39
6 files changed
+2517
-2444
No files matched your search
@@ -21,6 +21,9 @@ from pathlib import Path
|
||||
import pytest
|
||||
|
||||
PAGE = Path(__file__).resolve().parent.parent / "sfm" / "sfm_webapp.html"
|
||||
STATIC = PAGE.parent / "static"
|
||||
ROUTING_JS = STATIC / "device_routing.js" # the page's routing rules, loaded by <script src>
|
||||
PAGE_JS = STATIC / "sfm_webapp.js" # the rest of the page's script
|
||||
NODE = shutil.which("node")
|
||||
pytestmark = pytest.mark.skipif(NODE is None, reason="node is not installed; the page-logic tests need it")
|
||||
|
||||
@@ -29,9 +32,7 @@ EXPORTS = ["seriesFromSerial", "unitQuery", "deviceUrl", "confirmedSeries", "con
|
||||
|
||||
|
||||
def _block() -> str:
|
||||
m = re.search(r"/\* <device-routing> \*/(.*?)/\* </device-routing> \*/", PAGE.read_text(), re.S)
|
||||
assert m, "no device-routing block in sfm_webapp.html"
|
||||
return m.group(1)
|
||||
return ROUTING_JS.read_text()
|
||||
|
||||
|
||||
def run(*cases):
|
||||
@@ -162,11 +163,12 @@ def test_the_block_touches_no_dom():
|
||||
|
||||
|
||||
def test_the_page_scripts_parse_together(tmp_path):
|
||||
"""The page's inline scripts share one global scope: a syntax slip, or a name
|
||||
declared in both, breaks the whole page. Joined, they must pass node --check."""
|
||||
scripts = re.findall(r"<script>(.*?)</script>", PAGE.read_text(), re.S)
|
||||
"""The page's scripts share one global scope: a syntax slip, or a name declared
|
||||
in both, breaks the whole page. Joined in load order, they must pass node --check."""
|
||||
srcs = re.findall(r'<script src="(static/[^"]+)"></script>', PAGE.read_text())
|
||||
assert srcs == ["static/device_routing.js", "static/sfm_webapp.js"]
|
||||
js = tmp_path / "page.js"
|
||||
js.write_text("\n;\n".join(scripts))
|
||||
js.write_text("\n;\n".join((PAGE.parent / s).read_text() for s in srcs))
|
||||
r = subprocess.run([NODE, "--check", str(js)], capture_output=True, text=True, timeout=30)
|
||||
assert r.returncode == 0, r.stderr
|
||||
|
||||
@@ -174,8 +176,7 @@ def test_the_page_scripts_parse_together(tmp_path):
|
||||
def test_every_device_url_is_built_by_deviceUrl():
|
||||
"""One place builds /device/ URLs, so the series rules cannot be bypassed:
|
||||
outside the device-routing block the page holds no hand-built device path."""
|
||||
src = PAGE.read_text()
|
||||
outside = re.sub(r"/\* <device-routing> \*/.*?/\* </device-routing> \*/", "", src, flags=re.S)
|
||||
outside = PAGE.read_text() + PAGE_JS.read_text()
|
||||
hand_built = [m.group(0) for m in re.finditer(r"(`\$\{api\(\)\}/device/[^`]*`|'/device/[^']*'|\"/device/[^\"]*\")", outside)]
|
||||
assert hand_built == [], hand_built
|
||||
|
||||
|
||||
Reference in new issue
Block a user