2020-06-09 18:42:53 +00:00
|
|
|
import * as childProcess from 'child_process';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as v8 from 'v8';
|
2020-10-23 07:34:19 +00:00
|
|
|
import { SuiteFunction, TestFunction } from 'mocha';
|
2022-08-05 00:20:17 +00:00
|
|
|
import { BrowserWindow } from 'electron/main';
|
|
|
|
import { AssertionError } from 'chai';
|
2020-06-09 18:42:53 +00:00
|
|
|
|
2020-10-23 07:34:19 +00:00
|
|
|
const addOnly = <T>(fn: Function): T => {
|
|
|
|
const wrapped = (...args: any[]) => {
|
|
|
|
return fn(...args);
|
|
|
|
};
|
|
|
|
(wrapped as any).only = wrapped;
|
|
|
|
(wrapped as any).skip = wrapped;
|
|
|
|
return wrapped as any;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const ifit = (condition: boolean) => (condition ? it : addOnly<TestFunction>(it.skip));
|
|
|
|
export const ifdescribe = (condition: boolean) => (condition ? describe : addOnly<SuiteFunction>(describe.skip));
|
2019-09-16 23:08:01 +00:00
|
|
|
|
2020-06-26 20:59:54 +00:00
|
|
|
export const delay = (time: number = 0) => new Promise(resolve => setTimeout(resolve, time));
|
2020-06-09 18:42:53 +00:00
|
|
|
|
|
|
|
type CleanupFunction = (() => void) | (() => Promise<void>)
|
|
|
|
const cleanupFunctions: CleanupFunction[] = [];
|
|
|
|
export async function runCleanupFunctions () {
|
|
|
|
for (const cleanup of cleanupFunctions) {
|
|
|
|
const r = cleanup();
|
|
|
|
if (r instanceof Promise) { await r; }
|
|
|
|
}
|
|
|
|
cleanupFunctions.length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function defer (f: CleanupFunction) {
|
2020-06-11 18:35:50 +00:00
|
|
|
cleanupFunctions.unshift(f);
|
2020-06-09 18:42:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class RemoteControlApp {
|
|
|
|
process: childProcess.ChildProcess;
|
|
|
|
port: number;
|
|
|
|
|
|
|
|
constructor (proc: childProcess.ChildProcess, port: number) {
|
|
|
|
this.process = proc;
|
|
|
|
this.port = port;
|
|
|
|
}
|
|
|
|
|
|
|
|
remoteEval = (js: string): Promise<any> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const req = http.request({
|
|
|
|
host: '127.0.0.1',
|
|
|
|
port: this.port,
|
|
|
|
method: 'POST'
|
|
|
|
}, res => {
|
|
|
|
const chunks = [] as Buffer[];
|
|
|
|
res.on('data', chunk => { chunks.push(chunk); });
|
|
|
|
res.on('end', () => {
|
|
|
|
const ret = v8.deserialize(Buffer.concat(chunks));
|
|
|
|
if (Object.prototype.hasOwnProperty.call(ret, 'error')) {
|
|
|
|
reject(new Error(`remote error: ${ret.error}\n\nTriggered at:`));
|
|
|
|
} else {
|
|
|
|
resolve(ret.result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
req.write(js);
|
|
|
|
req.end();
|
|
|
|
});
|
|
|
|
}
|
2020-07-09 18:25:43 +00:00
|
|
|
|
2020-06-09 18:42:53 +00:00
|
|
|
remotely = (script: Function, ...args: any[]): Promise<any> => {
|
|
|
|
return this.remoteEval(`(${script})(...${JSON.stringify(args)})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-17 21:17:25 +00:00
|
|
|
export async function startRemoteControlApp (extraArgs: string[] = [], options?: childProcess.SpawnOptionsWithoutStdio) {
|
2023-01-25 21:01:25 +00:00
|
|
|
const appPath = path.join(__dirname, '..', 'fixtures', 'apps', 'remote-control');
|
2021-06-17 21:17:25 +00:00
|
|
|
const appProcess = childProcess.spawn(process.execPath, [appPath, ...extraArgs], options);
|
2020-06-09 18:42:53 +00:00
|
|
|
appProcess.stderr.on('data', d => {
|
|
|
|
process.stderr.write(d);
|
|
|
|
});
|
|
|
|
const port = await new Promise<number>(resolve => {
|
|
|
|
appProcess.stdout.on('data', d => {
|
|
|
|
const m = /Listening: (\d+)/.exec(d.toString());
|
|
|
|
if (m && m[1] != null) {
|
|
|
|
resolve(Number(m[1]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
defer(() => { appProcess.kill('SIGINT'); });
|
|
|
|
return new RemoteControlApp(appProcess, port);
|
|
|
|
}
|
2021-04-22 16:00:58 +00:00
|
|
|
|
|
|
|
export function waitUntil (
|
|
|
|
callback: () => boolean,
|
|
|
|
opts: { rate?: number, timeout?: number } = {}
|
|
|
|
) {
|
|
|
|
const { rate = 10, timeout = 10000 } = opts;
|
|
|
|
return new Promise<void>((resolve, reject) => {
|
|
|
|
let intervalId: NodeJS.Timeout | undefined; // eslint-disable-line prefer-const
|
|
|
|
let timeoutId: NodeJS.Timeout | undefined;
|
|
|
|
|
|
|
|
const cleanup = () => {
|
|
|
|
if (intervalId) clearInterval(intervalId);
|
|
|
|
if (timeoutId) clearTimeout(timeoutId);
|
|
|
|
};
|
|
|
|
|
|
|
|
const check = () => {
|
|
|
|
let result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = callback();
|
|
|
|
} catch (e) {
|
|
|
|
cleanup();
|
|
|
|
reject(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result === true) {
|
|
|
|
cleanup();
|
|
|
|
resolve();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (check()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
intervalId = setInterval(check, rate);
|
|
|
|
|
|
|
|
timeoutId = setTimeout(() => {
|
|
|
|
timeoutId = undefined;
|
|
|
|
cleanup();
|
|
|
|
reject(new Error(`waitUntil timed out after ${timeout}ms`));
|
|
|
|
}, timeout);
|
|
|
|
});
|
|
|
|
}
|
2021-07-28 15:56:15 +00:00
|
|
|
|
|
|
|
export async function repeatedly<T> (
|
|
|
|
fn: () => Promise<T>,
|
|
|
|
opts?: { until?: (x: T) => boolean, timeLimit?: number }
|
|
|
|
) {
|
|
|
|
const { until = (x: T) => !!x, timeLimit = 10000 } = opts ?? {};
|
|
|
|
const begin = +new Date();
|
|
|
|
while (true) {
|
|
|
|
const ret = await fn();
|
|
|
|
if (until(ret)) { return ret; }
|
|
|
|
if (+new Date() - begin > timeLimit) { throw new Error(`repeatedly timed out (limit=${timeLimit})`); }
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 00:20:17 +00:00
|
|
|
|
|
|
|
async function makeRemoteContext (opts?: any) {
|
2022-08-09 07:39:14 +00:00
|
|
|
const { webPreferences, setup, url = 'about:blank', ...rest } = opts ?? {};
|
2022-08-05 00:20:17 +00:00
|
|
|
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false, ...webPreferences }, ...rest });
|
2022-08-09 07:39:14 +00:00
|
|
|
await w.loadURL(url.toString());
|
|
|
|
if (setup) await w.webContents.executeJavaScript(setup);
|
2022-08-05 00:20:17 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
const remoteContext: BrowserWindow[] = [];
|
|
|
|
export async function getRemoteContext () {
|
|
|
|
if (remoteContext.length) { return remoteContext[0]; }
|
|
|
|
const w = await makeRemoteContext();
|
|
|
|
defer(() => w.close());
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useRemoteContext (opts?: any) {
|
|
|
|
before(async () => {
|
|
|
|
remoteContext.unshift(await makeRemoteContext(opts));
|
|
|
|
});
|
|
|
|
after(() => {
|
|
|
|
const w = remoteContext.shift();
|
|
|
|
w!.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function itremote (name: string, fn: Function, args?: any[]) {
|
|
|
|
it(name, async () => {
|
|
|
|
const w = await getRemoteContext();
|
|
|
|
const { ok, message } = await w.webContents.executeJavaScript(`(async () => {
|
|
|
|
try {
|
|
|
|
const chai_1 = require('chai')
|
2022-08-09 07:39:14 +00:00
|
|
|
chai_1.use(require('chai-as-promised'))
|
2022-08-05 00:20:17 +00:00
|
|
|
chai_1.use(require('dirty-chai'))
|
|
|
|
await (${fn})(...${JSON.stringify(args ?? [])})
|
|
|
|
return {ok: true};
|
|
|
|
} catch (e) {
|
|
|
|
return {ok: false, message: e.message}
|
|
|
|
}
|
|
|
|
})()`);
|
|
|
|
if (!ok) { throw new AssertionError(message); }
|
|
|
|
});
|
|
|
|
}
|