81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import { buildFixtureWasm } from "./build-wasm.js";
|
|
|
|
describe("buildFixtureWasm", () => {
|
|
it("emits the WASM magic + version preamble", { timeout: 5000 }, () => {
|
|
const bytes = buildFixtureWasm({ importProcExit: false });
|
|
expect([...bytes.subarray(0, 4)]).toEqual([0x00, 0x61, 0x73, 0x6d]);
|
|
expect([...bytes.subarray(4, 8)]).toEqual([0x01, 0x00, 0x00, 0x00]);
|
|
});
|
|
|
|
it("produces a module valid under WebAssembly.validate", {
|
|
timeout: 5000,
|
|
}, () => {
|
|
expect(
|
|
WebAssembly.validate(buildFixtureWasm({ importProcExit: true }))
|
|
).toBe(true);
|
|
expect(
|
|
WebAssembly.validate(buildFixtureWasm({ importProcExit: false }))
|
|
).toBe(true);
|
|
expect(
|
|
WebAssembly.validate(
|
|
buildFixtureWasm({
|
|
capabilityImports: ["secrets_get", "secrets_require"],
|
|
})
|
|
)
|
|
).toBe(true);
|
|
});
|
|
|
|
it("threads the requested WASI version into the import module name", {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
const bytes = buildFixtureWasm({
|
|
wasiVersion: "wasi_snapshot_preview0",
|
|
importProcExit: true,
|
|
});
|
|
const module = await WebAssembly.compile(bytes);
|
|
const imports = WebAssembly.Module.imports(module);
|
|
expect(imports.some((i) => i.module === "wasi_snapshot_preview0")).toBe(
|
|
true
|
|
);
|
|
});
|
|
|
|
it("omits the WASI import when importProcExit is false", {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
const module = await WebAssembly.compile(
|
|
buildFixtureWasm({ importProcExit: false })
|
|
);
|
|
const imports = WebAssembly.Module.imports(module);
|
|
expect(imports.some((i) => i.module.startsWith("wasi_"))).toBe(false);
|
|
});
|
|
|
|
it("emits capability imports under suaveplan_caps", {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
const module = await WebAssembly.compile(
|
|
buildFixtureWasm({ capabilityImports: ["secrets_get"] })
|
|
);
|
|
const imports = WebAssembly.Module.imports(module);
|
|
expect(imports.find((i) => i.name === "secrets_get")?.module).toBe(
|
|
"suaveplan_caps"
|
|
);
|
|
});
|
|
|
|
it("always exports _start", { timeout: 5000 }, async () => {
|
|
const module = await WebAssembly.compile(buildFixtureWasm());
|
|
const exports = WebAssembly.Module.exports(module);
|
|
expect(exports.find((e) => e.name === "_start")?.kind).toBe("function");
|
|
});
|
|
|
|
it("defaults wasiVersion to wasi_snapshot_preview1", {
|
|
timeout: 5000,
|
|
}, async () => {
|
|
const module = await WebAssembly.compile(buildFixtureWasm());
|
|
const imports = WebAssembly.Module.imports(module);
|
|
expect(imports.some((i) => i.module === "wasi_snapshot_preview1")).toBe(
|
|
true
|
|
);
|
|
});
|
|
});
|