1
0
forked from rosa/hakurei

cmd/pkgserver/ui_test: add DOM reporter

This commit is contained in:
Kat
2026-03-29 01:48:59 +11:00
parent 8d759f654d
commit f2fded0620
6 changed files with 208 additions and 1 deletions

View File

@@ -90,7 +90,63 @@ export class StreamReporter implements Reporter {
}
}
const r = new StreamReporter({ writeln: console.log }, true);
function assertGetElementById(id: string): HTMLElement {
let elem = document.getElementById(id);
if (elem == null) throw new ReferenceError(`element with ID '${id}' missing from DOM`);
return elem;
}
export class DOMReporter implements Reporter {
update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty");
const counter = assertGetElementById(result.success ? "successes" : "failures");
counter.innerText = (Number(counter.innerText) + 1).toString();
let parent = assertGetElementById("root");
for (const node of path) {
let child: HTMLDetailsElement | null = null;
let d: Element;
outer: for (d of parent.children) {
if (!(d instanceof HTMLDetailsElement)) continue;
for (const s of d.children) {
if (!(s instanceof HTMLElement)) continue;
if (!(s.tagName === "SUMMARY" && s.innerText === node)) continue;
child = d;
break outer;
}
}
if (!child) {
child = document.createElement("details");
child.className = "test-node";
child.ariaRoleDescription = "test";
const summary = document.createElement("summary");
summary.appendChild(document.createTextNode(node));
summary.ariaRoleDescription = "test name";
child.appendChild(summary);
parent.appendChild(child);
}
if (!result.success) {
child.open = true;
child.classList.add("failure");
}
parent = child;
}
const p = document.createElement("p");
p.classList.add("test-desc");
if (result.logs.length) {
const pre = document.createElement("pre");
pre.appendChild(document.createTextNode(result.logs.join("\n")));
p.appendChild(pre);
} else {
p.classList.add("italic");
p.appendChild(document.createTextNode("No output."));
}
parent.appendChild(p);
}
finalize() {}
}
let r = globalThis.document ? new DOMReporter() : new StreamReporter({ writeln: console.log });
r.update(["alien", "can walk"], { success: false, logs: ["assertion failed"] });
r.update(["alien", "can speak"], { success: false, logs: ["Uncaught ReferenceError: larynx is not defined"] });
r.update(["alien", "sleep"], { success: true, logs: [] });