Reorganize repo structure and standards references

This commit is contained in:
david committed 2026-06-29 05:03:42 +02:00
1 parent 3cb93a81be
commit be2054274d
5885 files changed
+2588 -195

No files matched your search

@@ -0,0 +1,176 @@
import { describe, expect, test } from "vitest";
import { createPluginSandbox } from "./runtime.js";
import type { PluginManifest } from "./manifest.js";
/**
* Minimal hand-assembled WASM module exporting `double(i32) -> i32`
* and `unreachable()` that traps. Keeping the bytes inline avoids a
* filesystem dependency in unit tests.
*
* WAT equivalent:
* (module
* (func (export "double") (param i32) (result i32)
* local.get 0 i32.const 2 i32.mul)
* (func (export "boom")
* unreachable))
*/
const HELLO_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
// type section: id=0x01, size=0x09, count=2, type0=(i32)->i32, type1=()->()
0x01, 0x09, 0x02, 0x60, 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x00, 0x00,
// function section: id=0x03, size=0x03, count=2, func0 uses type0, func1 uses type1
0x03, 0x03, 0x02, 0x00, 0x01,
// export section: id=0x07, size=0x11, count=2
// export "double" -> func 0; export "boom" -> func 1
0x07, 0x11, 0x02,
0x06, 0x64, 0x6f, 0x75, 0x62, 0x6c, 0x65, 0x00, 0x00,
0x04, 0x62, 0x6f, 0x6f, 0x6d, 0x00, 0x01,
// code section: id=0x0a, size=0x0d, count=2
// func0: size=7, 0 locals, body = local.get 0; i32.const 2; i32.mul; end
// func1: size=3, 0 locals, body = unreachable; end
0x0a, 0x0d, 0x02,
0x07, 0x00, 0x20, 0x00, 0x41, 0x02, 0x6c, 0x0b,
0x03, 0x00, 0x00, 0x0b,
]);
const PAYLOAD_IMPORT_WASM = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
// type section: () -> i32, (i32) -> i32
0x01, 0x0a, 0x02, 0x60, 0x00, 0x01, 0x7f, 0x60, 0x01, 0x7f, 0x01, 0x7f,
// import section: env.toolctrl_payload_len, env.toolctrl_payload_byte
0x02, 0x38, 0x02,
0x03, 0x65, 0x6e, 0x76,
0x14, 0x74, 0x6f, 0x6f, 0x6c, 0x63, 0x74, 0x72, 0x6c, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x6c, 0x65, 0x6e,
0x00, 0x00,
0x03, 0x65, 0x6e, 0x76,
0x15, 0x74, 0x6f, 0x6f, 0x6c, 0x63, 0x74, 0x72, 0x6c, 0x5f, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x62, 0x79, 0x74, 0x65,
0x00, 0x01,
// function section: two local functions, both () -> i32
0x03, 0x03, 0x02, 0x00, 0x00,
// export section: payload_len and payload_first
0x07, 0x1f, 0x02,
0x0b, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x6c, 0x65, 0x6e, 0x00, 0x02,
0x0d, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64, 0x5f, 0x66, 0x69, 0x72, 0x73, 0x74, 0x00, 0x03,
// code section
0x0a, 0x0d, 0x02,
0x04, 0x00, 0x10, 0x00, 0x0b,
0x06, 0x00, 0x41, 0x00, 0x10, 0x01, 0x0b,
]);
const FIXTURE_MANIFEST: PluginManifest = {
schemaVersion: 1,
id: "de.toolctrl.test",
version: "1.0.0",
name: "Test",
entry: "plugin.wasm",
wasmSha256: "0".repeat(64),
signature: "0".repeat(128),
signedBy: "0".repeat(64),
allowedHosts: [],
capabilities: [],
hooks: [{ name: "double", export: "double" }],
};
describe("createPluginSandbox", () => {
test("instantiates and invokes a numeric export", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: HELLO_WASM,
manifest: FIXTURE_MANIFEST,
memoryLimitMb: 1,
fuelLimit: 100,
});
const result = await sandbox.invoke("double", 21);
expect(result.ok).toBe(true);
if (result.ok) expect(result.output).toBe(42);
sandbox.close();
});
test("reports hook_not_exported for unknown hook names", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: HELLO_WASM,
manifest: FIXTURE_MANIFEST,
});
const result = await sandbox.invoke("missing", 0);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.reason).toBe("hook_not_exported");
});
test("classifies unreachable traps as 'trap'", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: HELLO_WASM,
manifest: FIXTURE_MANIFEST,
});
const result = await sandbox.invoke("boom", 0);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.reason).toBe("trap");
});
test("returns compile_failed for invalid wasm bytes", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),
manifest: FIXTURE_MANIFEST,
});
const result = await sandbox.invoke("double", 1);
expect(result.ok).toBe(false);
if (!result.ok) expect(result.reason).toBe("compile_failed");
});
test("rejects non-positive memory and fuel limits", () => {
expect(() =>
createPluginSandbox({
wasmBuffer: HELLO_WASM,
manifest: FIXTURE_MANIFEST,
memoryLimitMb: 0,
})
).toThrow();
expect(() =>
createPluginSandbox({
wasmBuffer: HELLO_WASM,
manifest: FIXTURE_MANIFEST,
fuelLimit: 0,
})
).toThrow();
});
test("close() makes subsequent invokes report trap with 'sandbox closed'", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: HELLO_WASM,
manifest: FIXTURE_MANIFEST,
});
sandbox.close();
const result = await sandbox.invoke("double", 2);
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.reason).toBe("trap");
expect(result.message).toMatch(/closed/);
}
});
test("fuelRemaining decrements on every successful call", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: HELLO_WASM,
manifest: FIXTURE_MANIFEST,
fuelLimit: 5,
});
expect(sandbox.fuelRemaining()).toBe(5);
await sandbox.invoke("double", 1);
expect(sandbox.fuelRemaining()).toBe(4);
});
test("exposes structured payloads through the JSON payload host imports", async () => {
const sandbox = createPluginSandbox({
wasmBuffer: PAYLOAD_IMPORT_WASM,
manifest: FIXTURE_MANIFEST,
fuelLimit: 5,
});
const lenResult = await sandbox.invoke("payload_len", { ok: true });
expect(lenResult.ok).toBe(true);
if (lenResult.ok) expect(lenResult.output).toBe(JSON.stringify({ ok: true }).length);
const firstByteResult = await sandbox.invoke("payload_first", { ok: true });
expect(firstByteResult.ok).toBe(true);
if (firstByteResult.ok) expect(firstByteResult.output).toBe("{".charCodeAt(0));
});
});