text: #!/usr/bin/env node
  /**
   * @license Copyright 2016 Google Inc. All Rights Reserved.
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
   * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
   */
  'use strict';
  
  /**
   * @typedef {{path: string, actual: *, expected: *}} Difference
   */
  
  /**
   * @typedef {{category: string, actual: *, expected: *, equal: boolean, diff?: Difference | null}} Comparison
   */
  
  /**
   * @typedef {Pick<LH.Result, 'audits' | 'finalUrl' | 'requestedUrl'> & {errorCode?: string}} ExpectedLHR
   */
  
  /**
   * @typedef {{audits: Comparison[], errorCode: Comparison, finalUrl: Comparison}} LHRComparison
   */
  
  /* eslint-disable no-console */
  
  const fs = require('fs');
  const path = require('path');
  const spawnSync = require('child_process').spawnSync;
  const yargs = require('yargs');
  const log = require('lighthouse-logger');
  
  const PROTOCOL_TIMEOUT_EXIT_CODE = 67;
  const PAGE_HUNG_EXIT_CODE = 68;
  const INSECURE_DOCUMENT_REQUEST_EXIT_CODE = 69;
  const RETRIES = 3;
  const VERBOSE = Boolean(process.env.LH_SMOKE_VERBOSE);
  const NUMBER_REGEXP = /(?:\d|\.)+/.source;
  const OPS_REGEXP = /<=?|>=?|\+\/-/.source;
  // An optional number, optional whitespace, an operator, optional whitespace, a number.
  const NUMERICAL_EXPECTATION_REGEXP =
    new RegExp(`^(${NUMBER_REGEXP})?\\s*(${OPS_REGEXP})\\s*(${NUMBER_REGEXP})$`);
  
  /**
   * Attempt to resolve a path locally. If this fails, attempts to locate the path
   * relative to the current working directory.
   * @param {string} payloadPath
   * @return {string}
   */
  function resolveLocalOrCwd(payloadPath) {
    let resolved;
    try {
      resolved = require.resolve('./' + payloadPath);
    } catch (e) {
      const cwdPath = path.resolve(process.cwd(), payloadPath);
      resolved = require.resolve(cwdPath);
    }
  
    return resolved;
  }
  
  /**
   * Launch Chrome and do a full Lighthouse run.
   * @param {string} url
   * @param {string} configPath
   * @param {boolean=} isDebug
   * @return {ExpectedLHR}
   */
  function runLighthouse(url, configPath, isDebug) {
    isDebug = isDebug || Boolean(process.env.LH_SMOKE_DEBUG);
  
    const command = 'node';
    const outputPath = `smokehouse-${Math.round(Math.random() * 100000)}.report.json`;
    const args = [
      'lighthouse-cli/index.js',
      url,
      `--config-path=${configPath}`,
      `--output-path=${outputPath}`,
      '--output=json',
      '--quiet',
      '--port=0',
    ];
  
    if (isDebug) {
      args.push('-GA');
    }
  
    if (process.env.APPVEYOR) {
      // Appveyor is hella slow already, disable CPU throttling so we're not 16x slowdown
      // see https://github.com/GoogleChrome/lighthouse/issues/4891
      args.push('--throttling.cpuSlowdownMultiplier=1');
    }
  
    // Lighthouse sometimes times out waiting to for a connection to Chrome in CI.
    // Watch for this error and retry relaunching Chrome and running Lighthouse up
    // to RETRIES times. See https://github.com/GoogleChrome/lighthouse/issues/833
    let runResults;
    let runCount = 0;
    do {
      if (runCount > 0) {
        console.log('  Lighthouse error: timed out waiting for debugger connection. Retrying...');
      }
  
      runCount++;
      console.log(`${log.dim}$ ${command} ${args.join(' ')} ${log.reset}`);
      runResults = spawnSync(command, args, {encoding: 'utf8', stdio: ['pipe', 'pipe', 'inherit']});
    } while (runResults.status === PROTOCOL_TIMEOUT_EXIT_CODE && runCount <= RETRIES);
  
    if (runResults.status === PROTOCOL_TIMEOUT_EXIT_CODE) {
      console.error(`Lighthouse debugger connection timed out ${RETRIES} times. Giving up.`);
      process.exit(1);
    } else if (runResults.status !== 0
       && runResults.status !== PAGE_HUNG_EXIT_CODE
       && runResults.status !== INSECURE_DOCUMENT_REQUEST_EXIT_CODE) {
      console.error(`Lighthouse run failed with exit code ${runResults.status}. stderr to follow:`);
      console.error(runResults.stderr);
      process.exit(runResults.status);
    }
  
    if (isDebug) {
      console.log(`STDOUT: ${runResults.stdout}`);
      console.error(`STDERR: ${runResults.stderr}`);
    }
  
    if (runResults.status === PAGE_HUNG_EXIT_CODE) {
      return {requestedUrl: url, finalUrl: url, errorCode: 'PAGE_HUNG', audits: {}};
    }
  
    if (runResults.status === INSECURE_DOCUMENT_REQUEST_EXIT_CODE) {
      return {requestedUrl: url, finalUrl: url, errorCode: 'INSECURE_DOCUMENT_REQUEST', audits: {}};
    }
  
    const lhr = fs.readFileSync(outputPath, 'utf8');
    if (isDebug) {
      console.log('LHR output available at: ', outputPath);
    } else if (fs.existsSync(outputPath)) {
      fs.unlinkSync(outputPath);
    }
  
    return JSON.parse(lhr);
  }
  
  /**
   * Checks if the actual value matches the expectation. Does not recursively search. This supports
   *    - Greater than/less than operators, e.g. "<100", ">90"
   *    - Regular expressions
   *    - Strict equality
   *
   * @param {*} actual
   * @param {*} expected
   * @return {boolean}
   */
  function matchesExpectation(actual, expected) {
    if (typeof actual === 'number' && NUMERICAL_EXPECTATION_REGEXP.test(expected)) {
      const parts = expected.match(NUMERICAL_EXPECTATION_REGEXP);
      const [, prefixNumber, operator, postfixNumber] = parts;
      switch (operator) {
        case '>':
          return actual > postfixNumber;
        case '>=':
          return actual >= postfixNumber;
        case '<':
          return actual < postfixNumber;
        case '<=':
          return actual <= postfixNumber;
        case '+/-':
          return Math.abs(actual - prefixNumber) <= postfixNumber;
        default:
          throw new Error(`unexpected operator ${operator}`);
      }
    } else if (typeof actual === 'string' && expected instanceof RegExp && expected.test(actual)) {
      return true;
    } else {
      // Strict equality check, plus NaN equivalence.
      return Object.is(actual, expected);
    }
  }
  
  /**
   * Walk down expected result, comparing to actual result. If a difference is found,
   * the path to the difference is returned, along with the expected primitive value
   * and the value actually found at that location. If no difference is found, returns
   * null.
   *
   * Only checks own enumerable properties, not object prototypes, and will loop
   * until the stack is exhausted, so works best with simple objects (e.g. parsed JSON).
   * @param {string} path
   * @param {*} actual
   * @param {*} expected
   * @return {(Difference|null)}
   */
  function findDifference(path, actual, expected) {
    if (matchesExpectation(actual, expected)) {
      return null;
    }
  
    // If they aren't both an object we can't recurse further, so this is the difference.
    if (actual === null || expected === null || typeof actual !== 'object' ||
        typeof expected !== 'object' || expected instanceof RegExp) {
      return {
        path,
        actual,
        expected,
      };
    }
  
    // We only care that all expected's own properties are on actual (and not the other way around).
    for (const key of Object.keys(expected)) {
      // Bracket numbers, but property names requiring quotes will still be unquoted.
      const keyAccessor = /^\d+$/.test(key) ? `[${key}]` : `.${key}`;
      const keyPath = path + keyAccessor;
      const expectedValue = expected[key];
  
      if (!(key in actual)) {
        return {path: keyPath, actual: undefined, expected: expectedValue};
      }
  
      const actualValue = actual[key];
      const subDifference = findDifference(keyPath, actualValue, expectedValue);
  
      // Break on first difference found.
      if (subDifference) {
        return subDifference;
      }
    }
  
    return null;
  }
  
  /**
   * Collate results into comparisons of actual and expected scores on each audit.
   * @param {ExpectedLHR} actual
   * @param {ExpectedLHR} expected
   * @return {LHRComparison}
   */
  function collateResults(actual, expected) {
    const auditNames = Object.keys(expected.audits);
    const collatedAudits = auditNames.map(auditName => {
      const actualResult = actual.audits[auditName];
      if (!actualResult) {
        throw new Error(`Config did not trigger run of expected audit ${auditName}`);
      }
  
      const expectedResult = expected.audits[auditName];
      const diff = findDifference(auditName, actualResult, expectedResult);
  
      return {
        category: auditName,
        actual: actualResult,
        expected: expectedResult,
        equal: !diff,
        diff,
      };
    });
  
    return {
      audits: collatedAudits,
      errorCode: {
        category: 'error code',
        actual: actual.errorCode,
        expected: expected.errorCode,
        equal: actual.errorCode === expected.errorCode,
      },
      finalUrl: {
        category: 'final url',
        actual: actual.finalUrl,
        expected: expected.finalUrl,
        equal: actual.finalUrl === expected.finalUrl,
      },
    };
  }
  
  /**
   * @param {unknown} obj
   */
  function isPlainObject(obj) {
    return Object.prototype.toString.call(obj) === '[object Object]';
  }
  
  /**
   * Log the result of an assertion of actual and expected results.
   * @param {Comparison} assertion
   */
  function reportAssertion(assertion) {
    // @ts-ignore - this doesn't exist now but could one day, so try not to break the future
    const _toJSON = RegExp.prototype.toJSON;
    // @ts-ignore
    // eslint-disable-next-line no-extend-native
    RegExp.prototype.toJSON = RegExp.prototype.toString;
  
    if (assertion.equal) {
      if (isPlainObject(assertion.actual)) {
        console.log(`  ${log.greenify(log.tick)} ${assertion.category}`);
      } else {
        console.log(`  ${log.greenify(log.tick)} ${assertion.category}: ` +
            log.greenify(assertion.actual));
      }
    } else {
      if (assertion.diff) {
        const diff = assertion.diff;
        const fullActual = JSON.stringify(assertion.actual, null, 2).replace(/\n/g, '\n      ');
        const msg = `
    ${log.redify(log.cross)} difference at ${log.bold}${diff.path}${log.reset}
                expected: ${JSON.stringify(diff.expected)}
                   found: ${JSON.stringify(diff.actual)}
  
            found result:
        ${log.redify(fullActual)}
  `;
        console.log(msg);
      } else {
        console.log(`  ${log.redify(log.cross)} ${assertion.category}:
                expected: ${JSON.stringify(assertion.expected)}
                   found: ${JSON.stringify(assertion.actual)}
  `);
      }
    }
  
    // @ts-ignore
    // eslint-disable-next-line no-extend-native
    RegExp.prototype.toJSON = _toJSON;
  }
  
  /**
   * Log all the comparisons between actual and expected test results, then print
   * summary. Returns count of passed and failed tests.
   * @param {LHRComparison} results
   * @return {{passed: number, failed: number}}
   */
  function report(results) {
    let correctCount = 0;
    let failedCount = 0;
  
    [results.finalUrl, results.errorCode, ...results.audits].forEach(auditAssertion => {
      if (auditAssertion.equal) {
        correctCount++;
      } else {
        failedCount++;
      }
  
      if (!auditAssertion.equal || VERBOSE) {
        reportAssertion(auditAssertion);
      }
    });
  
    const plural = correctCount === 1 ? '' : 's';
    const correctStr = `${correctCount} assertion${plural}`;
    const colorFn = correctCount === 0 ? log.redify : log.greenify;
    console.log(`  Correctly passed ${colorFn(correctStr)}\n`);
  
    return {
      passed: correctCount,
      failed: failedCount,
    };
  }
  
  const cli = yargs
    .help('help')
    .describe({
      'config-path': 'The path to the config JSON file',
      'expectations-path': 'The path to the expected audit results file',
      'debug': 'Save the artifacts along with the output',
    })
    .require('config-path', true)
    .require('expectations-path', true)
    .argv;
  
  const configPath = resolveLocalOrCwd(cli['config-path']);
  /** @type {ExpectedLHR[]} */
  const expectations = require(resolveLocalOrCwd(cli['expectations-path']));
  
  // Loop sequentially over expectations, comparing against Lighthouse run, and
  // reporting result.
  let passingCount = 0;
  let failingCount = 0;
  expectations.forEach(expected => {
    console.log(`Doing a run of '${expected.requestedUrl}'...`);
    const results = runLighthouse(expected.requestedUrl, configPath, cli.debug);
  
    console.log(`Asserting expected results match those found. (${expected.requestedUrl})`);
    const collated = collateResults(results, expected);
    const counts = report(collated);
    passingCount += counts.passed;
    failingCount += counts.failed;
  });
  
  if (passingCount) {
    console.log(log.greenify(`${passingCount} passing`));
  }
  if (failingCount) {
    console.log(log.redify(`${failingCount} failing`));
    process.exit(1);
  }
  