From 61c24a5bfd520089f4e8eb2de1bb056791d469b0 Mon Sep 17 00:00:00 2001 From: kat <00-kat@proton.me> Date: Sun, 29 Mar 2026 03:07:05 +1100 Subject: [PATCH] cmd/mbf: jstest: implement DSL and runner --- cmd/mbf/internal/pkgserver/ui/all_tests.ts | 2 + cmd/mbf/internal/pkgserver/ui/jstest/cli.ts | 48 ++++++ .../internal/pkgserver/ui/jstest/index.html | 6 +- .../internal/pkgserver/ui/jstest/jstest.ts | 158 ++++++++++++++++-- cmd/mbf/internal/pkgserver/ui/sample_test.ts | 64 +++++++ .../internal/pkgserver/ui/tsconfig.ui.json | 2 +- 6 files changed, 267 insertions(+), 13 deletions(-) create mode 100644 cmd/mbf/internal/pkgserver/ui/all_tests.ts create mode 100644 cmd/mbf/internal/pkgserver/ui/jstest/cli.ts create mode 100644 cmd/mbf/internal/pkgserver/ui/sample_test.ts diff --git a/cmd/mbf/internal/pkgserver/ui/all_tests.ts b/cmd/mbf/internal/pkgserver/ui/all_tests.ts new file mode 100644 index 00000000..002f5d93 --- /dev/null +++ b/cmd/mbf/internal/pkgserver/ui/all_tests.ts @@ -0,0 +1,2 @@ +// Import all test files to register their test suites. +import "./sample_test.js"; diff --git a/cmd/mbf/internal/pkgserver/ui/jstest/cli.ts b/cmd/mbf/internal/pkgserver/ui/jstest/cli.ts new file mode 100644 index 00000000..01551322 --- /dev/null +++ b/cmd/mbf/internal/pkgserver/ui/jstest/cli.ts @@ -0,0 +1,48 @@ +#!/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, GLOBAL_REGISTRAR } from "./jstest.js"; + +// TypeScript doesn't like process and Deno as their type definitions aren't +// installed, but doesn't seem to complain if they're accessed through +// globalThis. +const process: any = (globalThis as any).process; +const Deno: any = (globalThis as any).Deno; + +function getArgs(): string[] { + if (process) { + const [runtime, program, ...args] = process.argv; + return args; + } + if (Deno) return Deno.args; + return []; +} + +function exit(code?: number): never { + if (Deno) Deno.exit(code); + if (process) process.exit(code); + throw `exited with code ${code ?? 0}`; +} + +const args = getArgs(); +let verbose = false; +if (args.length > 1) { + console.error("Too many arguments"); + exit(1); +} +if (args.length === 1) { + if (args[0] === "-v" || args[0] === "--verbose" || args[0] === "-verbose") { + verbose = true; + } else if (args[0] !== "--") { + console.error(`Unknown argument '${args[0]}'`); + exit(1); + } +} + +let reporter = new StreamReporter({ writeln: console.log }, verbose); +GLOBAL_REGISTRAR.run(reporter); +exit(reporter.succeeded() ? 0 : 1); diff --git a/cmd/mbf/internal/pkgserver/ui/jstest/index.html b/cmd/mbf/internal/pkgserver/ui/jstest/index.html index 066ab18c..ae2539b5 100644 --- a/cmd/mbf/internal/pkgserver/ui/jstest/index.html +++ b/cmd/mbf/internal/pkgserver/ui/jstest/index.html @@ -24,7 +24,11 @@