Cross-check el() ids against index.html in the selftest
A knob wired in app.js but missing from the markup throws during wiring, aborts the module and leaves a blank page - a symptom pointing nowhere near its cause. It has now happened twice, so it gets a check rather than vigilance: the selftest fetches both files and compares the id sets in each direction. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
9a11eeabc1
commit
a146d271b6
3 changed files with 38 additions and 2 deletions
|
|
@ -45,6 +45,37 @@ const spreadX = (frames, slot) => {
|
|||
|
||||
/* ---- the tests ---- */
|
||||
|
||||
// Cross-check every el('id') in app.js against the ids in index.html.
|
||||
//
|
||||
// This bug class has bitten twice: a knob wired in app.js but absent from the
|
||||
// markup throws during wiring, which aborts the rest of the module and leaves a
|
||||
// blank page. The symptom ("nothing happens") points nowhere near the cause, so
|
||||
// it is worth an automated check rather than vigilance.
|
||||
export async function runWiring() {
|
||||
const out = [];
|
||||
try {
|
||||
const [app, html] = await Promise.all([
|
||||
fetch('./js/app.js').then((r) => r.text()),
|
||||
fetch('./index.html').then((r) => r.text()),
|
||||
]);
|
||||
const ids = new Set([...app.matchAll(/\bel\(\s*['"]([\w-]+)['"]\s*\)/g)].map((m) => m[1]));
|
||||
const list = app.match(/for \(const id of \[([\s\S]*?)\]\)/);
|
||||
if (list) {
|
||||
for (const m of list[1].matchAll(/'([\w]+)'/g)) { ids.add(m[1]); ids.add(m[1] + 'v'); }
|
||||
}
|
||||
const have = new Set([...html.matchAll(/id="([\w-]+)"/g)].map((m) => m[1]));
|
||||
const missing = [...ids].filter((i) => !have.has(i));
|
||||
out.push({ name: `every el() id exists in index.html (${ids.size} checked)`,
|
||||
pass: missing.length === 0, detail: missing.join(', ') });
|
||||
const unused = [...have].filter((i) => !ids.has(i));
|
||||
out.push({ name: 'no orphaned ids in index.html', pass: unused.length === 0,
|
||||
detail: unused.join(', ') });
|
||||
} catch (e) {
|
||||
out.push({ name: 'wiring check ran', pass: false, detail: e.message });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function run() {
|
||||
results.length = 0;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue