const argv = require('minimist')(process.argv.slice(2));
const isArray = require('lodash.isarray');
const commonjs = require('rollup-plugin-commonjs');
const includePaths = require('rollup-plugin-includepaths');
const rollupConfig = require('../rollup.config.js');
const customLaunchers = require('./karma.custom-launchers.config.js')
  .customLaunchers;
const browsers = require('./karma.custom-launchers.config.js').browsers;
const browser = require('./karma.custom-launchers.config.js').browser();

const shouldProbeOnly = argv.shouldProbeOnly !== '';
const shouldTestOnBrowserStack = argv.shouldTestOnBrowserStack !== '';
const browserStackBrowsers = shouldProbeOnly ? [browser] : browsers;
const karmaBrowsers = shouldTestOnBrowserStack
  ? browserStackBrowsers
  : isArray(argv.browsers) ? argv.browsers.split(' ') : ['Chrome'];

rollupConfig.plugins.push(
  commonjs(),
  includePaths({
    include: {
      purify: 'dist/purify.js',
      'purify.min': 'dist/purify.min.js',
    },
  })
);

module.exports = function(config) {
  config.set({
    autoWatch: true,
    basePath: '../',
    frameworks: ['qunit'],
    files: [
      'node_modules/jquery/dist/jquery.js',
      'node_modules/qunit-parameterize/qunit-parameterize.js',
      'test/config/setup.js',
      'test/**/*.spec.js',
    ],

    preprocessors: {
      'src/*.js': ['rollup'],
      'test/**/*.spec.js': ['rollup'],
    },

    reporters: ['progress'],

    exclude: [],
    port: 9876,

    browserStack: {
      project: 'DOMPurify',
      username: process.env.BS_USERNAME,
      accessKey: process.env.BS_ACCESSKEY,
    },

    rollupPreprocessor: rollupConfig,

    customLaunchers,
    browsers: karmaBrowsers,

    browserDisconnectTimeout: 10000,
    browserDisconnectTolerance: 1,
    browserNoActivityTimeout: 240000,
    captureTimeout: 240000,

    plugins: [
      'karma-chrome-launcher',
      'karma-browserstack-launcher',
      'karma-firefox-launcher',
      'karma-qunit',
      'karma-rollup-preprocessor',
    ],

    singleRun: true,
    colors: true,
    logLevel: config.LOG_INFO,
  });
};

@@ -495,16 +495,497 @@
 ser();%0A%0A
+/**%0A * Environment variables are passed into the script and the depth of testing%0A * is affected accordginly.%0A *%0A * - Whenever on a PR we only want to probe test with a rahdom browsers%0A * - Whenever we are on the most recent node version on Travis we test via BrowserStack%0A * - If none of the prior mentioned holds we assume to be running local and respect the passed%0A *   in borwsers argv%0A * - If we did not receive any argv browsers we succeed (other node versions on Travis)%0A */%0A
 const sh
@@ -1244,16 +1244,72 @@
 sers%0A  :
+ // eslint-disable-next-line unicorn/no-process-exit%0A   
  isArray
@@ -1357,18 +1357,23 @@
 ) : 
-%5B'Chrome'%5D
+process.exit(0)
 ;%0A%0Ar

document switching when testing