UNPKG

3 kBJavaScriptView Raw
1// @ts-check
2
3const { CLI_MODE } = require('./constants');
4
5// @ts-ignore
6const Promise = require('bluebird');
7const os = require('os');
8const localRunnerCache = require('./runnerFileCache');
9const utils = require('../utils');
10const Ajv = require('ajv');
11const prepareRunnerAndTestimStartUtils = require('./prepareRunnerAndTestimStartUtils');
12const mockNetworkRuleFileSchema = require('./mockNetworkRuleFileSchema.json');
13
14const MAX_RULE_FILE_SIZE_IN_MB = 1;
15const PREPARE_MOCK_NETWORK_ERROR_PREFIX = 'JSON file supplied to --mock-network-pattern';
16const MSEC_IN_HALF_DAY = 1000 * 60 * 60 * 12;
17
18const logger = require('./logger').getLogger('prepare runner');
19
20Promise.resolve().then(() => {
21 // @ts-ignore
22 global.xhr2 = require('./xhr2'); // this is inside a `then` to not block and let network requests start
23});
24
25async function prepare(options) {
26 /**
27 * @type {Promise}
28 */
29 let chromedriverPromise = Promise.resolve();
30
31 const hasNoGrid = !options.host && !options.gridId && !options.grid && (!options.testPlan || options.testPlan.length === 0);
32 const isTdkRun = options.files.length !== 0;
33 if ((hasNoGrid && isTdkRun) || options.useLocalChromeDriver) {
34 chromedriverPromise = prepareRunnerAndTestimStartUtils.prepareChromeDriver();
35 options.useLocalChromeDriver = true;
36 }
37
38 if (!options.playerRequirePath && options.mode !== CLI_MODE.EXTENSION) {
39 await prepareRunnerAndTestimStartUtils.preparePlayer(options.playerLocation, options.canary);
40 }
41 if (options.mode === CLI_MODE.EXTENSION && !options.ext) {
42 await localRunnerCache.memoize(() => prepareRunnerAndTestimStartUtils.prepareExtension(options.extensionLocation), 'prepareExtension', MSEC_IN_HALF_DAY)();
43 }
44
45 let customExtensionLocation;
46
47 if (options.installCustomExtension) {
48 customExtensionLocation = await prepareRunnerAndTestimStartUtils.prepareCustomExtension(options.installCustomExtension);
49 }
50
51 await chromedriverPromise;
52
53 return customExtensionLocation;
54}
55
56async function prepareMockNetwork(location) {
57 logger.info('prepare MockNetwork', { location });
58 const rulesJsonBuf = await utils.getSourceAsBuffer(location);
59 if (rulesJsonBuf.byteLength > MAX_RULE_FILE_SIZE_IN_MB * 1000000) {
60 throw new Error(`${PREPARE_MOCK_NETWORK_ERROR_PREFIX} exceeded ${MAX_RULE_FILE_SIZE_IN_MB}MB`);
61 }
62
63 let rulesJson;
64 try {
65 rulesJson = JSON.parse(rulesJsonBuf);
66 } catch (error) {
67 throw new Error(`${PREPARE_MOCK_NETWORK_ERROR_PREFIX} cannot be parsed.${os.EOL}${error}`);
68 }
69
70 const ajv = new Ajv();
71 const valid = ajv.validate(mockNetworkRuleFileSchema, rulesJson);
72 if (!valid) {
73 const errors = ajv.errors.map((e, i) => `${++i}) ${e.dataPath} ${e.message}`).join(os.EOL);
74 throw new Error(`${PREPARE_MOCK_NETWORK_ERROR_PREFIX} is malformed.${os.EOL}${errors}`);
75 }
76
77 return rulesJson.entries;
78}
79
80module.exports = {
81 prepare,
82 prepareMockNetwork,
83};