import { puppeteerLauncher } from '@web/test-runner-puppeteer';
import { fromRollup } from '@web/dev-server-rollup';
import rollupReplace from '@rollup/plugin-replace';

const replace = fromRollup(rollupReplace);

const filteredLogs = ['Running in dev mode', 'lit-html is in dev mode', 'Multiple versions of Lit loaded', 'The main \'lit-element\' module entrypoint is deprecated'];

export default /** @type {import("@web/test-runner").TestRunnerConfig} */ ({
  /** Test files to run */
  files: 'dist/src/**/*.test.js',

  /** Resolve bare module imports */
  nodeResolve: {
    exportConditions: ['browser', 'development'],
  },

  /** Filter out lit dev mode logs */
  filterBrowserLogs(log) {
    for (const arg of log.args) {
      if (typeof arg === 'string' && filteredLogs.some(l => arg.includes(l))) {
        return false;
      }
    }
    return true;
  },

  /** Amount of browsers to run concurrently */
  concurrentBrowsers: 1,

  /** Amount of test files per browser to test concurrently */
  concurrency: 1,

  testsStartTimeout: 60000,
  testsFinishTimeout: 300000,
  
  /** Browsers to run tests on */
  browsers: [
    puppeteerLauncher({
      launchOptions: {
        headless: true,
        args: ['--no-sandbox', '--disable-setuid-sandbox'],
      },
    }),
  ],

  /** Test framework configuration */
  testFramework: {
    config: {
      timeout: 15000 // Increase the default timeout to 5 seconds
    }
  },

  plugins: [
    replace({
      preventAssignment: true,
      'process.env.NODE_ENV': JSON.stringify('production'),
    }),
  ],

  coverage: true,

  /** Coverage configuration */
  coverageConfig: {
    include: ['dist/src/**/*.js'],
    exclude: ['dist/src/**/*.test.js'],
    reporters: ['html', 'text-summary', 'lcov', 'json', 'cobertura'],
    thresholds: {
      statements: 100,
      branches: 100,
      functions: 100,
      lines: 100,
    },
  },
});
