#!/usr/bin/env node

var program = require('commander');
var spawn = require('child_process').spawn;
var fs = require('fs');
var execSync = require('child_process').execSync;

program
  .option('--headless', 'Run in headless mode.')
  .parse(process.argv);

let withServer = false;
let args = [];
const ENVIRONMENT_FILENAME = '.suite'; // This file is created by the start command.

if (fs.existsSync(ENVIRONMENT_FILENAME)) {
  const suite = fs.readFileSync(ENVIRONMENT_FILENAME);
  console.log('Suite %s detected (dev env running). Running test related to this suite.', suite);
  args.push('test/suites/' + suite + '/*.ts');
}
else if (program.args.length > 0) {
  console.log('No suite detected. Running selected tests against built server.');
  withServer = true;
  args = program.args;

  // Render the production version of the nginx portal configuration
  execSync('./example/compose/nginx/portal/render.js --production');
  // Prepare the environment
  execSync('./scripts/utils/prepare-environment.sh');
}
else {
  console.log('No suite detected but no tests have been selected...');
  process.exit(1);
}

mocha = spawn('./node_modules/.bin/mocha', ['--exit', '--colors', '--require', 'ts-node/register', ...args], {
  env: {
    ...process.env,
    TS_NODE_PROJECT: 'test/tsconfig.json',
    WITH_SERVER: (withServer) ? 'y' : 'n',
    HEADLESS: (program.headless) ? 'y' : 'n',
  }
});

mocha.stdout.on('data', (data) => {
  process.stdout.write(`${data}`);
});

mocha.stderr.on('data', (data) => {
  process.stderr.write(`${data}`);
});

mocha.on('exit', function(statusCode) {
  if (statusCode != 0) {
    console.error("The tests failed... Mocha exited with status code " + statusCode);
    fs.readFile('/tmp/authelia-server.log', function(err, data) {
      if (err) {
        console.error(err);
        return;
      }
      console.log(data.toString('utf-8'));
    });
  }
  process.exit(statusCode);
})