silverbullet/plugos/runtime.test.ts

68 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-01-14 20:38:39 +08:00
import { createSandbox } from "./sandboxes/deno_worker_sandbox.ts";
import { System } from "./system.ts";
import { assertEquals } from "../test_deps.ts";
import { compileManifest } from "./compile.ts";
import { esbuild } from "./deps.ts";
Deno.test("Run a deno sandbox", async () => {
const system = new System("server");
system.registerSyscalls([], {
addNumbers: (_ctx, a, b) => {
2024-01-14 20:38:39 +08:00
console.log("This is the context", _ctx.plug.name);
return a + b;
},
failingSyscall: () => {
throw new Error("#fail");
},
});
system.registerSyscalls(["restricted"], {
2022-03-25 19:03:06 +08:00
restrictedSyscall: () => {
return "restricted";
},
});
system.registerSyscalls(["dangerous"], {
2022-03-25 19:03:06 +08:00
dangerousSyscall: () => {
return "yay";
},
});
const tempDir = await Deno.makeTempDir();
2022-10-13 21:16:18 +08:00
const workerPath = await compileManifest(
new URL("test.plug.yaml", import.meta.url).pathname,
tempDir,
);
const plug = await system.load(
new URL(`file://${workerPath}`),
"test",
0,
createSandbox,
);
2024-01-14 20:38:39 +08:00
assertEquals({
addedNumbers: 3,
yamlMessage: "hello: world\n",
}, await plug.invoke("boot", []));
2024-01-14 20:38:39 +08:00
await system.unloadAll();
// Now load directly from module
const { plug: plugExport } = await import(
`file://${workerPath}`
);
const plug2 = await system.loadNoSandbox("test", plugExport);
assertEquals({
addedNumbers: 3,
yamlMessage: "hello: world\n",
}, await plug2.invoke("boot", []));
2022-03-25 19:03:06 +08:00
2022-03-21 22:21:34 +08:00
await system.unloadAll();
await Deno.remove(tempDir, { recursive: true });
esbuild.stop();
});