Files
pdf/wasm/hello.test.mjs
T

47 lines
1.8 KiB
JavaScript
Raw Normal View History

2026-05-16 11:01:20 +05:30
// Phase 0 WASM smoke test.
//
// Loads the Emscripten-built hello module and asserts the C export is callable
// from JS. This is the test CI runs after `cmake --build --preset wasm`.
//
// Run from the repo root:
// node wasm/hello.test.mjs
//
// Or set HELLO_MJS to point elsewhere if your build directory differs.
import { strict as assert } from "node:assert";
import { existsSync } from "node:fs";
import { fileURLToPath, pathToFileURL } from "node:url";
import { dirname, resolve } from "node:path";
const here = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(here, "..");
const helloPath =
process.env.HELLO_MJS ?? resolve(repoRoot, "out/build/wasm/bin/hello.mjs");
if (!existsSync(helloPath)) {
console.error(
`[wasm-smoke] hello.mjs not found at: ${helloPath}\n` +
`Did you run \`cmake --build --preset wasm\` first?\n` +
`Override with HELLO_MJS=/path/to/hello.mjs if your build dir differs.`,
);
process.exit(1);
}
const { default: createModule } = await import(pathToFileURL(helloPath).href);
const Module = await createModule();
// add(): the core "does it run?" check.
const addResult = Module.ccall("add", "number", ["number", "number"], [2, 3]);
assert.equal(addResult, 5, `add(2, 3) returned ${addResult}, expected 5`);
// hello_version(): guards against loading a stale module from a previous build.
const version = Module.ccall("hello_version", "number", [], []);
assert.equal(version, 1, `hello_version() returned ${version}, expected 1`);
// cwrap-style wrapping should also work — frontend will use this pattern.
const addWrapped = Module.cwrap("add", "number", ["number", "number"]);
assert.equal(addWrapped(40, 2), 42);
console.log("[wasm-smoke] OK — add(2,3)=5, hello_version()=1, cwrap add(40,2)=42");