forked from longtao.wu/droboticflow
102 lines
3.4 KiB
JavaScript
102 lines
3.4 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { createRuntimeLoader, staticRuntimeBase } from "../../src/lib/static-runtime.js";
|
|
|
|
test("static delivery remains local by default and never accepts credential-bearing URLs", () => {
|
|
assert.equal(staticRuntimeBase("", "/vendor/v1"), "/vendor/v1");
|
|
assert.equal(staticRuntimeBase("https://assets.example/v1/", "/vendor/v1"), "https://assets.example/v1");
|
|
assert.equal(staticRuntimeBase("https://assets.example/v1", "/vendor/v1", true), "/vendor/v1");
|
|
for (const base of [
|
|
"http://assets.example/v1",
|
|
"https://token@assets.example/v1",
|
|
"https://assets.example/v1?secret=x",
|
|
]) {
|
|
assert.throws(() => staticRuntimeBase(base, "/vendor/v1"));
|
|
}
|
|
});
|
|
|
|
test("concurrent mounts share one remote attempt and one sequential local fallback", async () => {
|
|
const calls = [];
|
|
let ready = false;
|
|
let fallbacks = 0;
|
|
const load = createRuntimeLoader({
|
|
remote: "remote",
|
|
local: "local",
|
|
ready: () => ready,
|
|
fallback: () => fallbacks++,
|
|
load: async (url) => {
|
|
calls.push(url);
|
|
await Promise.resolve();
|
|
if (url === "remote") throw new Error("offline");
|
|
ready = true;
|
|
},
|
|
});
|
|
await Promise.all(Array.from({ length: 20 }, () => load()));
|
|
await load();
|
|
assert.deepEqual(calls, ["remote", "local"]);
|
|
assert.equal(fallbacks, 1);
|
|
});
|
|
|
|
test("failed fallback is recoverable without retrying the failed remote or hiding errors", async () => {
|
|
const calls = [];
|
|
let available = false;
|
|
let ready = false;
|
|
const load = createRuntimeLoader({
|
|
remote: "remote",
|
|
local: "local",
|
|
ready: () => ready,
|
|
fallback: () => {},
|
|
load: async (url) => {
|
|
calls.push(url);
|
|
if (!available) throw new Error("offline");
|
|
ready = true;
|
|
},
|
|
});
|
|
await assert.rejects(load(), /offline/);
|
|
available = true;
|
|
await load();
|
|
assert.deepEqual(calls, ["remote", "local", "local"]);
|
|
});
|
|
|
|
test("lazy WASM compilation shares bytes, falls back sequentially, and stops retrying external failures", async () => {
|
|
const { createWasmCompiler } = await import("../../src/lib/static-runtime.js");
|
|
const requests = [];
|
|
let localFails = true;
|
|
const compile = createWasmCompiler({
|
|
remote: "https://assets.test/player.wasm",
|
|
local: "/local.wasm",
|
|
fetchResource: async (url) => {
|
|
requests.push(url);
|
|
if (url.startsWith("https") || localFails) throw Error("offline");
|
|
return new Response(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]), {
|
|
headers: { "Content-Type": "application/octet-stream" },
|
|
});
|
|
},
|
|
});
|
|
assert.deepEqual(requests, []);
|
|
await Promise.all(Array.from({ length: 12 }, () => assert.rejects(compile())));
|
|
assert.deepEqual(requests, ["https://assets.test/player.wasm", "/local.wasm"]);
|
|
localFails = false;
|
|
const m = await compile();
|
|
assert.ok(m instanceof WebAssembly.Module);
|
|
assert.deepEqual(requests, ["https://assets.test/player.wasm", "/local.wasm", "/local.wasm"]);
|
|
assert.equal(await compile(), m);
|
|
});
|
|
|
|
test("a remote module without a registered player falls back to the local version", async () => {
|
|
const calls = [];
|
|
let ready = false;
|
|
const load = createRuntimeLoader({
|
|
remote: "remote",
|
|
local: "local",
|
|
ready: () => ready,
|
|
fallback: () => {},
|
|
load: async (url) => {
|
|
calls.push(url);
|
|
if (url === "local") ready = true;
|
|
},
|
|
});
|
|
await load();
|
|
assert.deepEqual(calls, ["remote", "local"]);
|
|
});
|