1
0
silverbullet/plugos/runtime.test.ts

165 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-10-12 09:47:13 +00:00
import { createSandbox } from "./environments/deno_sandbox.ts";
import { System } from "./system.ts";
import {
assert,
assertEquals,
} from "https://deno.land/std@0.158.0/testing/asserts.ts";
Deno.test("Run a deno sandbox", async () => {
const system = new System("server");
system.registerSyscalls([], {
addNumbers: (_ctx, a, b) => {
return a + b;
},
failingSyscall: () => {
throw new Error("#fail");
},
});
system.registerSyscalls(["restricted"], {
2022-03-25 11:03:06 +00:00
restrictedSyscall: () => {
return "restricted";
},
});
system.registerSyscalls(["dangerous"], {
2022-03-25 11:03:06 +00:00
dangerousSyscall: () => {
return "yay";
},
});
const plug = await system.load(
{
2022-04-26 17:04:36 +00:00
name: "test",
2022-03-25 11:03:06 +00:00
requiredPermissions: ["dangerous"],
functions: {
addTen: {
code: `(() => {
return {
default: (n) => {
return n + 10;
}
};
})()`,
},
addNumbersSyscall: {
code: `(() => {
return {
default: async (a, b) => {
2022-03-24 09:48:56 +00:00
return await self.syscall("addNumbers", a, b);
}
};
})()`,
},
errorOut: {
code: `(() => {
return {
default: () => {
throw Error("BOOM");
}
};
})()`,
},
errorOutSys: {
code: `(() => {
return {
default: async () => {
2022-03-24 09:48:56 +00:00
await self.syscall("failingSyscall");
}
};
})()`,
},
2022-03-25 11:03:06 +00:00
restrictedTest: {
code: `(() => {
return {
default: async () => {
await self.syscall("restrictedSyscall");
}
};
})()`,
},
dangerousTest: {
code: `(() => {
return {
default: async () => {
return await self.syscall("dangerousSyscall");
}
};
})()`,
},
},
},
createSandbox,
);
assertEquals(await plug.invoke("addTen", [10]), 20);
for (let i = 0; i < 100; i++) {
assertEquals(await plug.invoke("addNumbersSyscall", [10, i]), 10 + i);
}
try {
await plug.invoke("errorOut", []);
assert(false);
} catch (e: any) {
assert(e.message.indexOf("BOOM") !== -1);
}
try {
await plug.invoke("errorOutSys", []);
assert(false);
} catch (e: any) {
assert(e.message.indexOf("#fail") !== -1);
}
2022-03-25 11:03:06 +00:00
try {
await plug.invoke("restrictedTest", []);
assert(false);
2022-03-25 11:03:06 +00:00
} catch (e: any) {
assert(
e.message.indexOf(
"Missing permission 'restricted' for syscall restrictedSyscall",
) !== -1,
2022-03-25 11:03:06 +00:00
);
}
assertEquals(await plug.invoke("dangerousTest", []), "yay");
await system.unloadAll();
});
import { bundle as plugOsBundle } from "./bin/plugos-bundle.ts";
import { esbuild } from "./compile.ts";
2022-10-13 13:16:18 +00:00
const __dirname = new URL(".", import.meta.url).pathname;
Deno.test("Preload dependencies", async () => {
const globalModules = await plugOsBundle(
`${__dirname}../plugs/global.plug.yaml`,
);
const testPlugManifest = await plugOsBundle(
`${__dirname}test.plug.yaml`,
2022-10-13 13:16:18 +00:00
{
imports: [globalModules],
},
);
esbuild.stop();
const system = new System("server");
system.on({
plugLoaded: async (plug) => {
for (
const [modName, code] of Object.entries(globalModules.dependencies!)
) {
await plug.sandbox.loadDependency(modName, code as string);
}
},
});
// Load test module
console.log("Loading test module");
const testPlug = await system.load(
testPlugManifest,
createSandbox,
);
console.log("Running");
const result = await testPlug.invoke("boot", []);
console.log("Result", result);
2022-03-25 11:03:06 +00:00
2022-03-21 14:21:34 +00:00
await system.unloadAll();
});