kernel-sandbox/src/index.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

2023-07-02 06:42:43 +00:00
import * as path from 'path';
import * as kernel from '@lumeweb/libkernel/kernel';
2022-06-30 20:12:43 +00:00
// @ts-ignore
2023-07-02 06:42:43 +00:00
import StaticServer from 'static-server';
import { Page } from 'puppeteer';
import { bufToHex, ed25519, ErrTuple } from '@lumeweb/libkernel';
import * as url from 'url';
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
2022-07-20 18:01:54 +00:00
2022-06-30 20:12:43 +00:00
export function generateSeedPhrase() {
2023-07-02 06:42:43 +00:00
return ed25519.utils.randomPrivateKey();
2022-06-30 20:12:43 +00:00
}
export async function login(page: Page, seed = generateSeedPhrase()) {
2023-07-02 06:42:43 +00:00
await page.goto('http://kernel.lumeweb.com');
2022-06-30 20:12:43 +00:00
2023-07-02 06:42:43 +00:00
let seedHex = bufToHex(seed);
2022-06-30 20:12:43 +00:00
await page.evaluate((seed: string) => {
2023-07-02 06:42:43 +00:00
window.localStorage.setItem('v1-key', seed);
2022-06-30 20:12:43 +00:00
}, seedHex);
}
export async function loadTester(page: Page, port = 8080) {
const server = new StaticServer({
2023-07-02 06:42:43 +00:00
rootPath: path.resolve(__dirname, '..', 'public'),
2022-06-30 20:12:43 +00:00
port,
2023-07-02 06:42:43 +00:00
host: 'localhost',
2022-06-30 20:12:43 +00:00
});
await new Promise((resolve) => {
server.start(resolve);
});
const stop = () => server.stop();
2023-07-02 06:42:43 +00:00
process.on('SIGTERM', stop);
page.browser().on('disconnected', stop);
2022-06-30 20:12:43 +00:00
await page.goto(`http://localhost:${port}/`);
await page.evaluate(() => {
return kernel.init();
});
}
class Tester {
private page: Page;
constructor(page: Page) {
this.page = page;
}
2023-07-02 06:42:43 +00:00
async callModule(id: string, method: string, data = {}): Promise<ErrTuple> {
2022-06-30 20:12:43 +00:00
return this.page.evaluate(
async (id, method, data) => {
return kernel.callModule(id, method, data);
},
id,
method,
2023-07-02 06:42:43 +00:00
data,
2022-06-30 20:12:43 +00:00
);
}
}
export const tester = (page: Page) => new Tester(page);