forked from code/member-dashboard
The dashboard carried the identical inline-handler line inside PAGE_HTML — the served JS was a syntax error from there on, so the page loaded and then did nothing — and a second casualty of the same sloppy one-shot replace: `location.reload()` had become `location.rebuildMetricButtons()\nload()` inside loadGuests(). Both are fixed, and the same gate fix applies (the gate now reads PAGE_HTML via ast.literal_eval, i.e. the string the app serves, not the source text between the quotes). Buttons are delegated data-metric clicks now, the harness proves the click reaches setTrendMetric, and the stub gained stateful attributes plus addEventListener/__fire to make that possible.
124 lines
5.7 KiB
Bash
Executable File
124 lines
5.7 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Pre-deploy check for the panel/dashboard PAGE_HTML frontends.
|
|
#
|
|
# scripts/check-page-js.sh <path/to/app/main.py> [harness.js]
|
|
#
|
|
# 1. Slices PAGE_HTML out of the SOURCE TEXT. Do NOT import the module instead:
|
|
# app/oidc.py needs authlib, which is not installed on the steward host.
|
|
# 2. Extracts the single <script> block and runs `node --check` on it.
|
|
# 3. Given a harness file, runs stub + page JS + harness in ONE node eval scope,
|
|
# so the harness can call the page's real functions against real data.
|
|
#
|
|
# Never skip step 2. It is load-bearing, not ritual: it has caught a broken
|
|
# string escape and a duplicate `const` that py_compile passed, either of which
|
|
# blanks every data table on the live page.
|
|
#
|
|
# Harness rules (learned the hard way — see references/panel-and-dashboard-patterns.md):
|
|
# * Assert invariants, not the shape you happened to observe. Parse the
|
|
# rendered markup into value/position pairs and check the RELATIONSHIP.
|
|
# * A regex ending in `<` swallows the closing bracket and garbles every
|
|
# captured label. Match `[^<]*` and stop.
|
|
# * parseFloat("10-28") is 10. Date labels impersonate numeric ticks; filter
|
|
# them out by their dash before trusting a numeric parse.
|
|
# * When a check fails, suspect the ASSERTION first and fix it — never loosen
|
|
# it to get a green run.
|
|
set -euo pipefail
|
|
|
|
MAIN="${1:?usage: check-page-js.sh <path/to/app/main.py> [harness.js]}"
|
|
HARNESS="${2:-}"
|
|
TMP="$(mktemp -d)"
|
|
trap 'rm -rf "$TMP"' EXIT
|
|
|
|
python3 - "$MAIN" "$TMP/page.js" <<'PY'
|
|
import ast, re, sys
|
|
# Read PAGE_HTML the way PYTHON does, not the way the file is typed. Slicing the
|
|
# source text between the triple quotes gives you the escapes as written; the app
|
|
# gets them INTERPRETED. A JS line written `\" onclick=...` in a plain triple-
|
|
# quoted string serves as `" onclick=...` — a syntax error that kills the whole
|
|
# page, while a text slice still looks like valid JS and node --check passes.
|
|
# That is not hypothetical: on 2026-11-13 this gate certified a page whose script
|
|
# died at the first button, and both UIs served no data at all.
|
|
tree = ast.parse(open(sys.argv[1]).read())
|
|
html = None
|
|
for node in ast.walk(tree):
|
|
if not isinstance(node, ast.Assign):
|
|
continue
|
|
if not any(getattr(t, "id", "") == "PAGE_HTML" for t in node.targets):
|
|
continue
|
|
# Only the triple-quoted literal — the module-splice line below it reassigns
|
|
# PAGE_HTML to a Call, which is not a literal and is not what we check.
|
|
if isinstance(node.value, ast.Constant) and isinstance(node.value.value, str):
|
|
html = node.value.value
|
|
if html is None:
|
|
sys.exit("no PAGE_HTML string literal found")
|
|
blocks = re.findall(r"<script>(.*?)</script>", html, re.S)
|
|
if len(blocks) != 1:
|
|
sys.exit(f"expected exactly 1 <script> block in PAGE_HTML, found {len(blocks)}")
|
|
js = blocks[0]
|
|
# Splice the shared chart module exactly as the app does at import time, so
|
|
# the checked/eval'd JS is what actually serves. The placeholder is a comment,
|
|
# and a missing module file leaves the placeholder in place (the harness then
|
|
# fails loudly on the missing CoOpChart instead of silently checking stale JS).
|
|
from pathlib import Path
|
|
placeholder = "/*@@CHART_MODULE@@*/"
|
|
if placeholder in js:
|
|
module = Path(sys.argv[1]).resolve().parent / "coop_chart.js"
|
|
if module.exists():
|
|
js = js.replace(placeholder, module.read_text())
|
|
print(f"spliced shared chart module ({module.name}) into the extracted page JS")
|
|
open(sys.argv[2], "w").write(js)
|
|
print(f"extracted {len(js)} chars of page JS from PAGE_HTML")
|
|
PY
|
|
|
|
node --check "$TMP/page.js"
|
|
echo "node --check: OK"
|
|
|
|
if [ -n "$HARNESS" ]; then
|
|
cat > "$TMP/stub.js" <<'JS'
|
|
// Minimal browser stubs, loaded BEFORE the page JS: these pages call
|
|
// load()/loadModels() at the end of the script, which need document + fetch.
|
|
const els = {};
|
|
const mkEl = (id) => {
|
|
const attrs = {};
|
|
const el = {
|
|
id, style: {}, textContent: "", innerHTML: "", className: "",
|
|
offsetWidth: 120, offsetHeight: 48,
|
|
// Stateful attributes, like a real element: a stub that always answers null
|
|
// cannot tell "wired once" from "wired on every call".
|
|
getAttribute: (k) => (k in attrs ? attrs[k] : null),
|
|
setAttribute: (k, v) => { attrs[k] = String(v); },
|
|
appendChild: () => {},
|
|
};
|
|
// Delegated listeners are how the pages wire their metric buttons now, so the
|
|
// stub must have them — and must be able to fire them, or the harness cannot
|
|
// prove the wiring reaches setTrendMetric at all.
|
|
el.addEventListener = (type, fn) => { (el.__handlers = el.__handlers || {})[type] = fn; };
|
|
el.__fire = (type, ev) => { const h = (el.__handlers || {})[type]; if (h) h(ev); };
|
|
// The pages build some markup by inserting HTML into a container
|
|
// (buildMetricButtons inserts the SHARED metric list), so the stub has to
|
|
// support it or the harness cannot exercise that path at all.
|
|
el.insertAdjacentHTML = (pos, html) => { el.innerHTML += html; };
|
|
return el;
|
|
};
|
|
globalThis.document = {
|
|
getElementById: (id) => (els[id] = els[id] || mkEl(id)),
|
|
querySelectorAll: () => [],
|
|
body: { innerHTML: "", appendChild: (el) => { globalThis.__lastAppended = el; } },
|
|
createElement: (tag) => mkEl(undefined),
|
|
addEventListener: (type, fn) => { (globalThis.__listeners = globalThis.__listeners || {})[type] = fn; },
|
|
};
|
|
globalThis.fetch = async () => ({
|
|
status: 200,
|
|
json: async () => ({ series: [], totals: {}, members: [], admins: [], models: [] }),
|
|
});
|
|
globalThis.window = {
|
|
innerWidth: 1440,
|
|
innerHeight: 900,
|
|
addEventListener: (type, fn) => {},
|
|
};
|
|
JS
|
|
node -e 'const fs=require("fs"), r=p=>fs.readFileSync(p,"utf8");
|
|
eval(r(process.argv[1]) + r(process.argv[2]) + r(process.argv[3]));' \
|
|
"$TMP/stub.js" "$TMP/page.js" "$HARNESS"
|
|
fi
|