Files
alzeo/dashboard/tests/pagejs.py
T
Hasan Basbunar e2f1f324b6 cockpit redesign: the one-page front, its tests, and the one-click load
The monolithic app.js and the tab <select> become a rail of views:
index.html plus css/cockpit.css and nine scripts sharing one scope
(base, now, lanes, ops, agent, decide, image, video, boot). Every
regression the swap left behind is fixed here, not papered over.

The one-click lane change (the point of the redesign): the page walks
switch, stop, start as one journey, one job per step, stopping at the
first gate that refuses. In dry run the state never moves, so the wait
for the engine to go down was a 90 s stall against a wall; dry run now
skips that wait and the sheet says up front that a later gate can
refuse because the box still reads as it was.

Tests that read app.js read the page the browser loads instead:
pagejs concatenates the nine scripts in the order index.html asks for
them, and the CI target step reads LANE_TARGETS from base.js, the one
list the UI actually keeps. No assertion was weakened or deleted to
get here; the app.js readers were moved to the new markup, and the
video journal gained an end-to-end class over the real 12-minute
journal captured 2026-09-29.

House style reached the new files before they were tracked: five en
dashes in ops.js became the ellipsis the rest of the page already
uses for no-data cells.

Held by: ci-local 40 ok 0 fail (5 skip = missing packages on this
runner), ruff F/E9 clean, page tests 4/4, mobile-check 184/184 on four
iPhone viewports against this front, monkey 58/58, lifecycle mutation
93.4% over the 91% floor.
2026-09-30 12:00:59 +02:00

103 lines
3.9 KiB
Python

"""Run the cockpit's page scripts in node, against a small DOM built from index.html.
A test hands over JavaScript that runs in the page's own global scope once the
page's scripts have loaded, drives it (renderers, clicks, fetch answers, virtual time) and ends with
report(value); run() returns that value. What this cannot see is layout: every box
measures 0 (fakedom.js says what it covers). node is in the CI image, which already
checks the opencode plugin's syntax with it, so a missing node fails there instead of
skipping a test that then counts as run.
"""
import html.parser
import json
import re
import os
import pathlib
import shutil
import subprocess
import tempfile
DASH = pathlib.Path(__file__).resolve().parents[1]
STATIC = DASH / "static"
FAKEDOM = pathlib.Path(__file__).with_name("fakedom.js")
def page_scripts() -> tuple:
"""The page's own scripts, in the order index.html loads them: the page is several
files sharing one global scope, and a test runs them as the browser does."""
markup = (STATIC / "index.html").read_text()
return tuple(re.findall(r'<script src="/static/([^"]+)"', markup))
SCRIPTS = page_scripts()
VOID = {"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "source",
"track", "wbr"}
class _Tree(html.parser.HTMLParser):
"""[tag, [[name, value], ...], children] per element, str per text node. The contents
of <script> and <style> are dropped: the scripts run from their own files."""
def __init__(self):
super().__init__(convert_charrefs=True)
self.root = ["#root", [], []]
self.stack = [self.root]
def handle_starttag(self, tag, attrs):
node = [tag, [[k, v if v is not None else ""] for k, v in attrs], []]
self.stack[-1][2].append(node)
if tag not in VOID:
self.stack.append(node)
def handle_startendtag(self, tag, attrs):
self.stack[-1][2].append([tag, [[k, v if v is not None else ""] for k, v in attrs], []])
def handle_endtag(self, tag):
for i in range(len(self.stack) - 1, 0, -1):
if self.stack[i][0] == tag:
del self.stack[i:]
return
def handle_data(self, data):
if self.stack[-1][0] not in ("script", "style"):
self.stack[-1][2].append(data)
def tree(markup: str):
p = _Tree()
p.feed(markup)
p.close()
return next(n for n in p.root[2] if isinstance(n, list))
def node_binary(test):
node = shutil.which("node")
if node:
return node
if os.environ.get("CI"):
test.fail("node is needed for the page tests, and the CI image has it")
test.skipTest("node is not installed here")
def run(test, body: str, *, setup: str = "", scripts=SCRIPTS, markup: str | None = None,
opts: dict | None = None, timeout: float = 60.0):
"""Load the page, run `setup`, then the page scripts, then `body`; return what `body`
passed to report(). A page script that throws while loading fails the test, and so
does an exception in a timer callback."""
node = node_binary(test)
page = markup if markup is not None else (STATIC / "index.html").read_text()
job = {"tree": tree(page), "opts": opts or {}, "setup": setup,
"scripts": [str(STATIC / s) for s in scripts], "body": body}
with tempfile.TemporaryDirectory(prefix="pagejs-") as d:
path = pathlib.Path(d) / "job.json"
path.write_text(json.dumps(job))
p = subprocess.run([node, str(FAKEDOM), str(path)], capture_output=True, text=True,
timeout=timeout)
out = p.stdout.rsplit("\n__RESULT__", 1)
if p.returncode != 0 or len(out) != 2:
err = p.stdout.rsplit("\n__ERROR__", 1)[-1] if "__ERROR__" in p.stdout else ""
test.fail(f"node exited {p.returncode}: {err or p.stderr[-2000:] or p.stdout[-2000:]}")
res = json.loads(out[1])
if res["errors"]:
test.fail("a timer callback threw: " + res["errors"][0][:1500])
return res["result"]