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