1
0
forked from rosa/hakurei

15 Commits

Author SHA1 Message Date
Kat
c3229d02e6 TODO: docs maybe?? 2026-03-21 01:31:54 +11:00
Kat
a26fd08c02 TODO: consider dom sandbox for testing ui (see long desc)
see https://discord.com/channels/1388415325036609617/1458088018232479824/1484195713347879012
2026-03-21 01:31:54 +11:00
Kat
1bea21d404 TODO: target-specific tests 2026-03-21 01:31:54 +11:00
Kat
0aa49dba1e TODO: actually write tests lol 2026-03-21 01:31:54 +11:00
Kat
af518a080d TODO: relocate test code 2026-03-21 01:31:54 +11:00
Kat
31cbab277c TODO: parallel execution 2026-03-21 01:31:54 +11:00
Kat
f2e4a20d1a TODO: implement skipping from TestController 2026-03-21 01:31:54 +11:00
Kat
cfd2284e8d TODO: implement skipping from run() (use substring-matching for entering tests to skip) 2026-03-21 01:31:54 +11:00
Kat
9b7b4419a3 TODO: pre-fill the DOM in DOMReporter's register() to avoid O(n³) traversal and moving UI 2026-03-21 01:31:54 +11:00
Kat
90ee17b861 TODO: implement proper nesting in StreamReporter's finalize() via register() tree 2026-03-21 01:31:54 +11:00
Kat
877be3308e cmd/pkgserver: allow non-global js test suites 2026-03-21 01:31:54 +11:00
Kat
e0f32ead04 cmd/pkgserver: serialize raw log list for go test consumption 2026-03-21 01:31:54 +11:00
Kat
7fa87e69fe cmd/pkgserver: add JSON reporter to facilitate go test integration 2026-03-21 01:31:54 +11:00
Kat
2e6f562dd5 cmd/pkgserver: fix multi-line JS test output display 2026-03-21 01:31:54 +11:00
Kat
7438de2e58 cmd/pkgserver: implement JS test DSL and runner 2026-03-21 01:31:54 +11:00
7 changed files with 306 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")
case "/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:
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 { StreamReporter, TESTS } from "./test.js";
TESTS.run(new StreamReporter({ writeln: console.log }));

View File

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

View File

@@ -1,16 +1,166 @@
// =============================================================================
// DSL
type TestTree = TestGroup | Test;
type TestGroup = { name: string, children: TestTree[] };
type Test = { name: string, test: (TestController) => void };
export class TestRegistrar {
#suites: TestGroup[];
constructor() {
this.#suites = [];
}
suite(name: string, children: TestTree[]) {
checkDuplicates(name, children)
this.#suites.push({ name, children });
}
run(reporter: Reporter) {
reporter.register(this.#suites);
for (const suite of this.#suites) {
for (const c of suite.children) runTests(reporter, [suite.name], c);
}
reporter.finalize();
}
}
export let TESTS = new TestRegistrar();
// Register a suite in the global registrar.
export function suite(name: string, children: TestTree[]) {
TESTS.suite(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 class TestController {
logs: string[];
#failed: boolean;
constructor() {
this.logs = [];
this.#failed = false;
}
fail() {
this.#failed = true;
}
failed(): boolean {
return this.#failed;
}
failNow(): never {
this.fail();
throw new FailNowSentinel();
}
log(message: string) {
this.logs.push(message);
}
error(message: string) {
this.log(message);
this.fail();
}
fatal(message: string): never {
this.log(message);
this.failNow();
}
}
// =============================================================================
// Execution
export interface TestResult {
success: boolean;
output: string;
logs: string[];
}
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();
try {
node.test(controller);
} catch (e) {
if (!(e instanceof FailNowSentinel)) {
controller.error(extractExceptionString(e));
}
}
reporter.update(path, { success: !controller.failed(), logs: controller.logs });
}
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
export interface Reporter {
register(suites: TestGroup[]): void
update(path: string[], result: TestResult): void;
finalize(): void;
}
export class NoOpReporter implements Reporter {
suites: TestGroup[];
results: ({ path: string[] } & TestResult)[];
finalized: boolean;
constructor() {
this.suites = [];
this.results = [];
this.finalized = false;
}
register(suites: TestGroup[]) {
this.suites = suites;
}
update(path: string[], result: TestResult) {
this.results.push({ path, ...result });
}
finalize() {
this.finalized = true;
}
}
export interface Stream {
writeln(s: string): void;
}
@@ -30,6 +180,8 @@ export class StreamReporter implements Reporter {
this.counts = { successes: 0, failures: 0 };
}
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty");
const pathStr = path.join(SEP);
@@ -59,16 +211,10 @@ export class StreamReporter implements Reporter {
for (const [path, tests] of pathMap) {
if (tests.length === 1) {
const t = tests[0];
const pathStr = path ? `${path}${SEP}` : "";
const output = t.output ? `: ${t.output}` : "";
this.stream.writeln(`${pathStr}${t.name}${output}`);
this.#writeOutput(tests[0], path ? `${path}${SEP}` : "", false);
} else {
this.stream.writeln(path);
for (const t of tests) {
const output = t.output ? `: ${t.output}` : "";
this.stream.writeln(` - ${t.name}${output}`);
}
for (const t of tests) this.#writeOutput(t, " - ", true);
}
}
@@ -76,9 +222,28 @@ export class StreamReporter implements Reporter {
const { successes, failures } = this.counts;
this.stream.writeln(`${successes} succeeded, ${failures} failed`);
}
#writeOutput(test: { name: string } & TestResult, prefix: string, nested: boolean) {
let output = "";
if (test.logs.length) {
// Individual logs might span multiple lines, so join them together
// then split it again.
const logStr = test.logs.join("\n");
const lines = logStr.split("\n");
if (lines.length <= 1) {
output = `: ${logStr}`;
} 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 {
register(suites: TestGroup[]) {}
update(path: string[], result: TestResult) {
if (path.length === 0) throw new RangeError("path is empty");
const counter = document.getElementById(result.success ? "successes" : "failures");
@@ -110,10 +275,10 @@ export class DOMReporter implements Reporter {
}
const p = document.createElement("p");
p.classList.add("test-desc");
if (result.output) {
const code = document.createElement("code");
code.appendChild(document.createTextNode(result.output));
p.appendChild(code);
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."));
@@ -124,12 +289,30 @@ export class DOMReporter implements Reporter {
finalize() {}
}
let r = typeof document !== "undefined" ? new DOMReporter() : new StreamReporter({ writeln: console.log });
r.update(["alien", "can walk"], { success: false, output: "assertion failed" });
r.update(["alien", "can speak"], { success: false, output: "Uncaught ReferenceError: larynx is not defined" });
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: "" });
r.update(["discombobulate"], { success: false, output: "hippopotamonstrosesquippedaliophobia" });
r.update(["recombobulate"], { success: true, output: "" });
r.finalize();
interface GoNode {
name: string;
subtests?: GoNode[];
}
// Used to display results via `go test`, via some glue code from the Go side.
export class GoTestReporter implements Reporter {
// Convert a test tree into the one expected by the Go code.
static serialize(node: TestTree): GoNode {
return {
name: node.name,
subtests: "children" in node ? node.children.map(GoTestReporter.serialize) : null,
};
}
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,81 @@
import { NoOpReporter, TestRegistrar, 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");
}),
test("tester tester", (t) => {
const r = new TestRegistrar();
r.suite("explod", [
test("with yarn", (t) => {
t.log("YAY");
}),
]);
const reporter = new NoOpReporter();
r.run(reporter);
if (reporter.suites.length !== 1) {
t.fatal(`incorrect number of suites registered got=${reporter.suites.length} want=1`);
}
const suite = reporter.suites[0];
if (suite.name !== "explod") {
t.error(`suite name incorrect got='${suite.name}' want='explod'`);
}
if (suite.children.length !== 1) {
t.fatal(`incorrect number of suite children got=${suite.children.length} want=1`);
}
const test_ = suite.children[0];
if (test_.name !== "with yarn") {
t.error(`incorrect test name got='${test_.name}' want='with yarn'`);
}
if ("children" in test_) {
t.error(`expected leaf node, got group of ${test_.children.length} children`);
}
if (!reporter.finalized) t.error(`expected reporter to have been finalized`);
if (reporter.results.length !== 1) {
t.fatal(`incorrect result count got=${reporter.results.length} want=1`);
}
const result = reporter.results[0];
if (!(result.path.length === 2 &&
result.path[0] === "explod" &&
result.path[1] === "with yarn")) {
t.error(`incorrect result path got=${result.path} want=["explod", "with yarn"]`);
}
if (!result.success) t.error(`expected test to succeed`);
if (!(result.logs.length === 1 && result.logs[0] === "YAY")) {
t.error(`incorrect result logs got=${result.logs} want=["YAY"]`);
}
}),
]);

View File

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