spec-helpers.ts 6.1 KB

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