async function testScript(
code: string,
expectedResult: unknown,
tools: Record<string, Function>
) {
const enclave = new Enclave({
securityLevel: 'SECURE',
toolHandler: async (name, args) => {
const tool = tools[name];
if (!tool) throw new Error(`Unknown tool: ${name}`);
return tool(...Object.values(args));
},
});
const result = await enclave.run(code);
enclave.dispose();
if (!result.success) {
throw new Error(`Script failed: ${result.error?.message}`);
}
expect(result.value).toEqual(expectedResult);
}
// Usage
await testScript(
`return await callTool('add', { a: 1, b: 2 })`,
3,
{ add: (a: number, b: number) => a + b }
);