2022-10-12 09:47:13 +00:00
|
|
|
import { createSandbox } from "./environments/deno_sandbox.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
import { System } from "./system.ts";
|
2023-05-23 18:53:53 +00:00
|
|
|
import { assertEquals } from "../test_deps.ts";
|
|
|
|
import { compileManifest } from "./compile.ts";
|
|
|
|
import { esbuild } from "./deps.ts";
|
2022-10-10 12:50:21 +00:00
|
|
|
|
|
|
|
Deno.test("Run a deno sandbox", async () => {
|
|
|
|
const system = new System("server");
|
2022-04-03 16:42:12 +00:00
|
|
|
system.registerSyscalls([], {
|
2022-10-10 12:50:21 +00:00
|
|
|
addNumbers: (_ctx, a, b) => {
|
2022-03-20 08:56:28 +00:00
|
|
|
return a + b;
|
|
|
|
},
|
|
|
|
failingSyscall: () => {
|
|
|
|
throw new Error("#fail");
|
|
|
|
},
|
|
|
|
});
|
2022-04-03 16:42:12 +00:00
|
|
|
system.registerSyscalls(["restricted"], {
|
2022-03-25 11:03:06 +00:00
|
|
|
restrictedSyscall: () => {
|
|
|
|
return "restricted";
|
|
|
|
},
|
|
|
|
});
|
2022-04-03 16:42:12 +00:00
|
|
|
system.registerSyscalls(["dangerous"], {
|
2022-03-25 11:03:06 +00:00
|
|
|
dangerousSyscall: () => {
|
|
|
|
return "yay";
|
|
|
|
},
|
|
|
|
});
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
const tempDir = await Deno.makeTempDir();
|
2022-10-13 13:16:18 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
const workerPath = await compileManifest(
|
|
|
|
new URL("test.plug.yaml", import.meta.url).pathname,
|
|
|
|
tempDir,
|
2022-10-10 12:50:21 +00:00
|
|
|
);
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
const plug = await system.load(
|
|
|
|
new URL(`file://${workerPath}`),
|
2023-12-06 17:44:48 +00:00
|
|
|
"test",
|
|
|
|
0,
|
2022-10-10 12:50:21 +00:00
|
|
|
createSandbox,
|
|
|
|
);
|
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
console.log("Plug", plug.manifest);
|
2022-10-10 12:50:21 +00:00
|
|
|
|
2023-05-23 18:53:53 +00:00
|
|
|
assertEquals("hello", await plug.invoke("boot", []));
|
2022-03-25 11:03:06 +00:00
|
|
|
|
2022-03-21 14:21:34 +00:00
|
|
|
await system.unloadAll();
|
2023-05-23 18:53:53 +00:00
|
|
|
|
|
|
|
await Deno.remove(tempDir, { recursive: true });
|
|
|
|
|
|
|
|
esbuild.stop();
|
2022-03-20 08:56:28 +00:00
|
|
|
});
|