import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; import test from "node:test"; import ts from "typescript"; const root = path.resolve(import.meta.dirname, "../../.."); const loaderPath = path.join(root, "web/app/src/engine-client/wasm-module-loader.ts"); const protocolPath = path.join(root, "web/protocol/wasm-modules.ts"); const transpile = (file) => ts.transpileModule(fs.readFileSync(file, "utf8"), { compilerOptions: { module: ts.ModuleKind.ES2022, target: ts.ScriptTarget.ES2022 }, fileName: file, reportDiagnostics: true, }); const protocol = transpile(protocolPath); assert.deepEqual(protocol.diagnostics, []); const loader = transpile(loaderPath); assert.deepEqual(loader.diagnostics, []); const loaderSource = loader.outputText.replace('from "../../../protocol/wasm-modules"', `from "data:text/javascript;base64,${Buffer.from(protocol.outputText).toString("base64")}"`); const loaderModule = await import(`data:text/javascript;base64,${Buffer.from(loaderSource).toString("base64")}`); const bytes = new TextEncoder().encode("completed"); const digest = await crypto.subtle.digest("SHA-256", bytes); const sha256 = [...new Uint8Array(digest)].map((value) => value.toString(16).padStart(2, "0")).join(""); const manifest = { schemaVersion: 1, releaseId: "blender-wasm-0.1.0-rc.1", modules: [ { id: "complete", lifecycle: "complete", preload: true, resources: { single: { fileName: "complete.wasm", url: "/complete.wasm", sha256 }, pthread: { fileName: "complete.wasm", url: "/complete.wasm", sha256 } } }, { id: "debug", lifecycle: "debug", preload: false, resources: { single: { fileName: "debug.wasm", url: "/debug.wasm", sha256 }, pthread: { fileName: "debug.wasm", url: "/debug.wasm", sha256 } } }, ], }; const fetcher = async (url) => new Response(bytes, { status: 200 }); test("completed modules preload once and debug modules load independently", async () => { const loader = loaderModule.createWasmModuleLoader(manifest, "single", fetcher); const states = await loader.preloadCompleted(); assert.equal(states.find((state) => state.id === "complete")?.status, "loaded"); assert.equal(states.find((state) => state.id === "debug")?.status, "pending"); assert.equal((await loader.load("complete")).byteLength, bytes.byteLength); assert.equal((await loader.loadDebug("debug")).byteLength, bytes.byteLength); assert.equal(loader.state().find((state) => state.id === "debug")?.status, "loaded"); }); test("debug loading rejects a completed module id", async () => { const loader = loaderModule.createWasmModuleLoader(manifest, "single", fetcher); await assert.rejects(() => loader.loadDebug("complete"), /WASM_MODULE_DEBUG_NOT_FOUND/); });