1
0
forked from rosa/hakurei

13 Commits

Author SHA1 Message Date
Kat
0eee3450ff TODO: consider dom sandbox for testing ui (see long desc)
see https://discord.com/channels/1388415325036609617/1458088018232479824/1484195713347879012
2026-03-20 08:26:47 +11:00
Kat
fd85d6cb4f TODO: target-specific tests 2026-03-20 08:26:47 +11:00
Kat
5ca5289faf TODO: actually write tests lol (see long desc)
need to deal with https://discord.com/channels/1388415325036609617/1458088018232479824/1484194296897994753 https://discord.com/channels/1388415325036609617/1458088018232479824/1484201110418751660
2026-03-20 08:26:46 +11:00
Kat
18b8d15edf TODO: relocate test code 2026-03-20 08:26:46 +11:00
Kat
d884d6e7e4 TODO: parallel execution 2026-03-20 08:26:46 +11:00
Kat
316a2d43fa TODO: implement skipping from TestController 2026-03-20 08:26:46 +11:00
Kat
ee847c8b86 TODO: implement skipping from run() (use substring-matching for entering tests to skip) 2026-03-20 08:26:46 +11:00
Kat
cdcc3af55a TODO: pre-fill the DOM in DOMReporter's register() to avoid O(n³) traversal and moving UI 2026-03-20 08:26:46 +11:00
Kat
2a0507dddc TODO: implement proper nesting in StreamReporter's finalize() via register() tree 2026-03-20 08:26:46 +11:00
Kat
2c9d87706d cmd/pkgserver: serialize TestController methods to go test 2026-03-20 08:26:46 +11:00
Kat
31a9e9d014 cmd/pkgserver: add JSON reporter to facilitate go test integration 2026-03-20 08:26:46 +11:00
Kat
8cf2421f30 cmd/pkgserver: fix multi-line JS test output display 2026-03-20 08:26:46 +11:00
Kat
6bc11098b1 cmd/pkgserver: implement JS test DSL and runner 2026-03-20 08:26:46 +11:00
7 changed files with 239 additions and 23 deletions

View File

@@ -29,6 +29,10 @@ func serveStaticContent(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, content, "ui/static/test.js") http.ServeFileFS(w, r, content, "ui/static/test.js")
case "/static/test.css": case "/static/test.css":
http.ServeFileFS(w, r, content, "ui/static/test.css") http.ServeFileFS(w, r, content, "ui/static/test.css")
case "/static/all_tests.js":
http.ServeFileFS(w, r, content, "ui/static/all_tests.js")
case "/static/test_tests.js":
http.ServeFileFS(w, r, content, "ui/static/test_tests.js")
default: default:
http.NotFound(w, r) http.NotFound(w, r)

View File

@@ -0,0 +1 @@
import "./test_tests.js";

View File

@@ -0,0 +1,7 @@
#!/usr/bin/env node
// Many editors have terminal emulators built in, so running tests with NodeJS
// provides faster iteration, especially for those acclimated to test-driven
// development.
import "./all_tests.js";
import { run, StreamReporter } from "./test.js";
run(new StreamReporter({ writeln: console.log }));

View File

@@ -21,6 +21,9 @@ details.test-node {
p.test-desc { p.test-desc {
margin: 0 0 0 1rem; margin: 0 0 0 1rem;
padding: 2px 0; padding: 2px 0;
> pre {
margin: 0;
}
} }
.italic { .italic {

View File

@@ -1,12 +1,136 @@
// =============================================================================
// DSL
type TestTree = TestGroup | Test;
type TestGroup = { name: string, children: TestTree[] };
type Test = { name: string, test: (TestController) => void };
let TESTS: ({ name: string } & TestGroup)[] = [];
export function suite(name: string, children: TestTree[]) {
checkDuplicates(name, children)
TESTS.push({ name, children });
}
export function context(name: string, children: TestTree[]): TestTree {
checkDuplicates(name, children)
return { name, children };
}
export const group = context;
export function test(name: string, test: (TestController) => void): TestTree {
return { name, test };
}
function checkDuplicates(parent: string, names: { name: string }[]) {
let seen = new Set<string>();
for (const { name } of names) {
if (seen.has(name)) {
throw new RangeError(`duplicate name '${name}' in '${parent}'`);
}
seen.add(name);
}
}
class FailNowSentinel {}
export type Journal = ({ method: "fail" } | { method: "log", message: string })[];
function formatJournal(journal: Journal): string {
return journal
.filter((e) => e.method === "log")
.map((e) => e.message)
.join("\n");
}
export class TestController {
journal: Journal;
#failed: boolean;
constructor() {
this.journal = [];
this.#failed = false;
}
fail() {
this.#failed = true;
this.journal.push({ method: "fail" });
}
failed(): boolean {
return this.#failed;
}
failNow(): never {
this.fail();
throw new FailNowSentinel();
}
log(message: string) {
this.journal.push({ method: "log", message });
}
error(message: string) {
this.log(message);
this.fail();
}
fatal(message: string): never {
this.log(message);
this.failNow();
}
}
// =============================================================================
// Execution
export interface TestResult { export interface TestResult {
success: boolean; success: boolean;
output: string; journal: Journal;
}
export function run(reporter: Reporter) {
reporter.register(TESTS);
for (const suite of TESTS) {
for (const c of suite.children) runTests(reporter, [suite.name], c);
}
reporter.finalize();
}
function runTests(reporter: Reporter, parents: string[], node: TestTree) {
const path = [...parents, node.name];
if ("children" in node) {
for (const c of node.children) runTests(reporter, path, c);
return;
}
let controller = new TestController();
let excStr: string;
try {
node.test(controller);
} catch (e) {
if (!(e instanceof FailNowSentinel)) {
excStr = extractExceptionString(e);
}
}
if (excStr !== undefined) controller.error(excStr);
reporter.update(path, { success: !controller.failed(), journal: controller.journal });
}
function extractExceptionString(e: any): string {
// String() instead of .toString() as null and undefined don't have
// properties.
const s = String(e);
if (!(e instanceof Error && "stack" in e)) return s;
// v8 (Chromium, NodeJS) include the error message, while Firefox and WebKit
// do not.
if (e.stack.startsWith(s)) return e.stack;
return `${s}\n${e.stack}`;
} }
// ============================================================================= // =============================================================================
// Reporting // Reporting
export interface Reporter { export interface Reporter {
register(suites: TestGroup[]): void
update(path: string[], result: TestResult): void; update(path: string[], result: TestResult): void;
finalize(): void; finalize(): void;
} }
@@ -30,6 +154,8 @@ export class StreamReporter implements Reporter {
this.counts = { successes: 0, failures: 0 }; this.counts = { successes: 0, failures: 0 };
} }
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) { update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty"); if (path.length === 0) throw new RangeError("path is empty");
const pathStr = path.join(SEP); const pathStr = path.join(SEP);
@@ -59,16 +185,10 @@ export class StreamReporter implements Reporter {
for (const [path, tests] of pathMap) { for (const [path, tests] of pathMap) {
if (tests.length === 1) { if (tests.length === 1) {
const t = tests[0]; this.#writeOutput(tests[0], path ? `${path}${SEP}` : "", false);
const pathStr = path ? `${path}${SEP}` : "";
const output = t.output ? `: ${t.output}` : "";
this.stream.writeln(`${pathStr}${t.name}${output}`);
} else { } else {
this.stream.writeln(path); this.stream.writeln(path);
for (const t of tests) { for (const t of tests) this.#writeOutput(t, " - ", true);
const output = t.output ? `: ${t.output}` : "";
this.stream.writeln(` - ${t.name}${output}`);
}
} }
} }
@@ -76,9 +196,26 @@ export class StreamReporter implements Reporter {
const { successes, failures } = this.counts; const { successes, failures } = this.counts;
this.stream.writeln(`${successes} succeeded, ${failures} failed`); this.stream.writeln(`${successes} succeeded, ${failures} failed`);
} }
#writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) {
let output = "";
let logOutput = formatJournal(test.journal);
if (logOutput) {
const lines = logOutput.split("\n");
if (lines.length <= 1) {
output = `: ${logOutput}`;
} else {
const padding = nested ? " " : " ";
output = ":\n" + lines.map((line) => padding + line).join("\n");
}
}
this.stream.writeln(`${prefix}${test.name}${output}`);
}
} }
export class DOMReporter implements Reporter { export class DOMReporter implements Reporter {
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) { update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty"); if (path.length === 0) throw new RangeError("path is empty");
const counter = document.getElementById(result.success ? "successes" : "failures"); const counter = document.getElementById(result.success ? "successes" : "failures");
@@ -110,10 +247,11 @@ export class DOMReporter implements Reporter {
} }
const p = document.createElement("p"); const p = document.createElement("p");
p.classList.add("test-desc"); p.classList.add("test-desc");
if (result.output) { const logOutput = formatJournal(result.journal);
const code = document.createElement("code"); if (logOutput) {
code.appendChild(document.createTextNode(result.output)); const pre = document.createElement("pre");
p.appendChild(code); pre.appendChild(document.createTextNode(logOutput));
p.appendChild(pre);
} else { } else {
p.classList.add("italic"); p.classList.add("italic");
p.appendChild(document.createTextNode("No output.")); p.appendChild(document.createTextNode("No output."));
@@ -124,12 +262,31 @@ export class DOMReporter implements Reporter {
finalize() {} finalize() {}
} }
let r = typeof document !== "undefined" ? new DOMReporter() : new StreamReporter({ writeln: console.log }); interface GoNode {
r.update(["alien", "can walk"], { success: false, output: "assertion failed" }); name: string;
r.update(["alien", "can speak"], { success: false, output: "Uncaught ReferenceError: larynx is not defined" }); subtests?: GoNode[];
r.update(["alien", "sleep"], { success: true, output: "" }); }
r.update(["Tetromino", "generate", "tessellates"], { success: false, output: "assertion failed: 1 != 2" });
r.update(["Tetromino", "solve", "works"], { success: true, output: "" }); // Used to display results via `go test`, via some glue code from the Go side.
r.update(["discombobulate"], { success: false, output: "hippopotamonstrosesquippedaliophobia" }); export class GoTestReporter implements Reporter {
r.update(["recombobulate"], { success: true, output: "" }); // Convert a test tree into the one expected by the Go code.
r.finalize(); static serialize(node: TestTree): GoNode {
if (!("children" in node)) return { name: node.name };
return {
name: node.name,
subtests: node.children.map(GoTestReporter.serialize),
};
}
register(suites: TestGroup[]) {
console.log(JSON.stringify(suites.map(GoTestReporter.serialize)));
}
update(path: string[], result: TestResult) {
console.log(JSON.stringify({ "path": path, ...result }));
}
finalize() {
console.log("null");
}
}

View File

@@ -0,0 +1,40 @@
import { context, group, suite, test } from "./test.js";
suite("dog", [
group("tail", [
test("wags when happy", (t) => {
if (0 / 0 !== Infinity / Infinity) {
t.fatal("undefined must not be defined");
}
}),
test("idle when down", (t) => {
t.log("test test");
t.error("dog whining noises go here");
}),
]),
test("likes headpats", (t) => {
if (2 !== 2) {
t.error("IEEE 754 violated: 2 is NaN");
}
}),
context("near cat", [
test("is ecstatic", (t) => {
if (("b" + "a" + + "a" + "a").toLowerCase() === "banana") {
t.error("🍌🍌🍌");
t.error("🍌🍌🍌");
t.error("🍌🍌🍌");
t.failNow();
}
}),
test("playfully bites cats' tails", (t) => {
t.log("arf!");
throw new Error("nom");
}),
]),
]);
suite("cat", [
test("likes headpats", (t) => {
t.log("meow");
}),
]);

View File

@@ -18,7 +18,11 @@
<div id="root"> <div id="root">
</div> </div>
<script type="module" src="./static/test.js"></script> <script type="module" src="./static/all_tests.js"></script>
<script type="module">
import { DOMReporter, run } from "./static/test.js";
run(new DOMReporter());
</script>
</main> </main>
</body> </body>
</html> </html>