spec-helpers.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { BrowserWindow } from 'electron/main';
  2. import { AssertionError } from 'chai';
  3. import { SuiteFunction, TestFunction } from 'mocha';
  4. import * as childProcess from 'node:child_process';
  5. import * as http from 'node:http';
  6. import * as http2 from 'node:http2';
  7. import * as https from 'node:https';
  8. import * as net from 'node:net';
  9. import * as path from 'node:path';
  10. import { setTimeout } from 'node:timers/promises';
  11. import * as url from 'node:url';
  12. import * as v8 from 'node:v8';
  13. const addOnly = <T>(fn: Function): T => {
  14. const wrapped = (...args: any[]) => {
  15. return fn(...args);
  16. };
  17. (wrapped as any).only = wrapped;
  18. (wrapped as any).skip = wrapped;
  19. return wrapped as any;
  20. };
  21. export const ifit = (condition: boolean) => (condition ? it : addOnly<TestFunction>(it.skip));
  22. export const ifdescribe = (condition: boolean) => (condition ? describe : addOnly<SuiteFunction>(describe.skip));
  23. type CleanupFunction = (() => void) | (() => Promise<void>)
  24. const cleanupFunctions: CleanupFunction[] = [];
  25. export async function runCleanupFunctions () {
  26. for (const cleanup of cleanupFunctions) {
  27. const r = cleanup();
  28. if (r instanceof Promise) { await r; }
  29. }
  30. cleanupFunctions.length = 0;
  31. }
  32. export function defer (f: CleanupFunction) {
  33. cleanupFunctions.unshift(f);
  34. }
  35. class RemoteControlApp {
  36. process: childProcess.ChildProcess;
  37. port: number;
  38. constructor (proc: childProcess.ChildProcess, port: number) {
  39. this.process = proc;
  40. this.port = port;
  41. }
  42. remoteEval = (js: string): Promise<any> => {
  43. return new Promise((resolve, reject) => {
  44. const req = http.request({
  45. host: '127.0.0.1',
  46. port: this.port,
  47. method: 'POST'
  48. }, res => {
  49. const chunks = [] as Buffer[];
  50. res.on('data', chunk => { chunks.push(chunk); });
  51. res.on('end', () => {
  52. const ret = v8.deserialize(Buffer.concat(chunks));
  53. if (Object.hasOwn(ret, 'error')) {
  54. reject(new Error(`remote error: ${ret.error}\n\nTriggered at:`));
  55. } else {
  56. resolve(ret.result);
  57. }
  58. });
  59. });
  60. req.write(js);
  61. req.end();
  62. });
  63. };
  64. remotely = (script: Function, ...args: any[]): Promise<any> => {
  65. return this.remoteEval(`(${script})(...${JSON.stringify(args)})`);
  66. };
  67. }
  68. export async function startRemoteControlApp (extraArgs: string[] = [], options?: childProcess.SpawnOptionsWithoutStdio) {
  69. const appPath = path.join(__dirname, '..', 'fixtures', 'apps', 'remote-control');
  70. const appProcess = childProcess.spawn(process.execPath, [appPath, ...extraArgs], options);
  71. appProcess.stderr.on('data', d => {
  72. process.stderr.write(d);
  73. });
  74. const port = await new Promise<number>(resolve => {
  75. appProcess.stdout.on('data', d => {
  76. const m = /Listening: (\d+)/.exec(d.toString());
  77. if (m && m[1] != null) {
  78. resolve(Number(m[1]));
  79. }
  80. });
  81. });
  82. defer(() => { appProcess.kill('SIGINT'); });
  83. return new RemoteControlApp(appProcess, port);
  84. }
  85. export function waitUntil (
  86. callback: () => boolean|Promise<boolean>,
  87. opts: { rate?: number, timeout?: number } = {}
  88. ) {
  89. const { rate = 10, timeout = 10000 } = opts;
  90. return (async () => {
  91. const ac = new AbortController();
  92. const signal = ac.signal;
  93. let checkCompleted = false;
  94. let timedOut = false;
  95. const check = async () => {
  96. let result;
  97. try {
  98. result = await callback();
  99. } catch (e) {
  100. ac.abort();
  101. throw e;
  102. }
  103. return result;
  104. };
  105. setTimeout(timeout, { signal })
  106. .then(() => {
  107. timedOut = true;
  108. checkCompleted = true;
  109. });
  110. while (checkCompleted === false) {
  111. const checkSatisfied = await check();
  112. if (checkSatisfied === true) {
  113. ac.abort();
  114. checkCompleted = true;
  115. return;
  116. } else {
  117. await setTimeout(rate);
  118. }
  119. }
  120. if (timedOut) {
  121. throw new Error(`waitUntil timed out after ${timeout}ms`);
  122. }
  123. })();
  124. }
  125. export async function repeatedly<T> (
  126. fn: () => Promise<T>,
  127. opts?: { until?: (x: T) => boolean, timeLimit?: number }
  128. ) {
  129. const { until = (x: T) => !!x, timeLimit = 10000 } = opts ?? {};
  130. const begin = Date.now();
  131. while (true) {
  132. const ret = await fn();
  133. if (until(ret)) { return ret; }
  134. if (Date.now() - begin > timeLimit) { throw new Error(`repeatedly timed out (limit=${timeLimit})`); }
  135. }
  136. }
  137. async function makeRemoteContext (opts?: any) {
  138. const { webPreferences, setup, url = 'about:blank', ...rest } = opts ?? {};
  139. const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true, contextIsolation: false, ...webPreferences }, ...rest });
  140. await w.loadURL(url.toString());
  141. if (setup) await w.webContents.executeJavaScript(setup);
  142. return w;
  143. }
  144. const remoteContext: BrowserWindow[] = [];
  145. export async function getRemoteContext () {
  146. if (remoteContext.length) { return remoteContext[0]; }
  147. const w = await makeRemoteContext();
  148. defer(() => w.close());
  149. return w;
  150. }
  151. export function useRemoteContext (opts?: any) {
  152. before(async () => {
  153. remoteContext.unshift(await makeRemoteContext(opts));
  154. });
  155. after(() => {
  156. const w = remoteContext.shift();
  157. w!.close();
  158. });
  159. }
  160. async function runRemote (type: 'skip' | 'none' | 'only', name: string, fn: Function, args?: any[]) {
  161. const wrapped = async () => {
  162. const w = await getRemoteContext();
  163. const { ok, message } = await w.webContents.executeJavaScript(`(async () => {
  164. try {
  165. const chai_1 = require('chai')
  166. const promises_1 = require('node:timers/promises')
  167. chai_1.use(require('chai-as-promised'))
  168. chai_1.use(require('dirty-chai'))
  169. await (${fn})(...${JSON.stringify(args ?? [])})
  170. return {ok: true};
  171. } catch (e) {
  172. return {ok: false, message: e.message}
  173. }
  174. })()`);
  175. if (!ok) { throw new AssertionError(message); }
  176. };
  177. let runFn: any = it;
  178. if (type === 'only') {
  179. runFn = it.only;
  180. } else if (type === 'skip') {
  181. runFn = it.skip;
  182. }
  183. runFn(name, wrapped);
  184. }
  185. export const itremote = Object.assign(
  186. (name: string, fn: Function, args?: any[]) => {
  187. runRemote('none', name, fn, args);
  188. }, {
  189. only: (name: string, fn: Function, args?: any[]) => {
  190. runRemote('only', name, fn, args);
  191. },
  192. skip: (name: string, fn: Function, args?: any[]) => {
  193. runRemote('skip', name, fn, args);
  194. }
  195. });
  196. export async function listen (server: http.Server | https.Server | http2.Http2SecureServer) {
  197. const hostname = '127.0.0.1';
  198. await new Promise<void>(resolve => server.listen(0, hostname, () => resolve()));
  199. const { port } = server.address() as net.AddressInfo;
  200. const protocol = (server instanceof http.Server) ? 'http' : 'https';
  201. return { port, hostname, url: url.format({ protocol, hostname, port }) };
  202. }