<commit_before>process.env.HOME = `${__dirname}/home`
process.env.USERPROFILE = `${__dirname}/home`

let assert = require('assert')
let cp = require('child_process')
let fs = require('fs')
let path = require('path')
let supertest = require('supertest')
let untildify = require('untildify')
let rmrf = require('rimraf')
let pkg = require('../package.json')

let timeout = process.env.TRAVIS ? 10000 : 5000

// Used to give some time to the system and commands
function wait (done) {
  setTimeout(done, timeout)
}

function hotel (cmd) {
  // Execute hotel cmd
  let out = cp.execSync(`node ${__dirname}/../${pkg.bin} ${cmd}`, {
    cwd: `${__dirname}/app`
  }).toString()

  // Log output
  // .replace() used to enhance tests readability
  if (!process.env.QUIET) {
    console.log(out
      .replace(/\n  /g, '\n    ')
      .replace(/\n$/, ''))
  }

  // Return output
  return out
}

let request = supertest(`http://localhost:2000`)

describe('hotel', function () {

  this.timeout(timeout + 1000)

  before((done) => {
    hotel('stop') // Just in case
    rmrf.sync(untildify('~/.hotel'))
    wait(done)
  })

  after(() => hotel('stop'))

  describe('$ hotel start', () => {

    before(done => {
      hotel('start')
      wait(done)
    })

    it('should start daemon', done => {
      request.get('/').expect(200, done)
    })
  })

  describe('app$ hotel add "node index.js"', () => {

    before(done => {
      hotel('add "node index.js"')
      wait(done)
    })

    it('should create a redirection from /app to app server', done => {
      request
        .get('/app')
        .expect(302, (err, res) => {
          if (err) throw err<commit_msg>Make waiting for 'hotel start' in tests faster<commit_after>process.env.HOME = `${__dirname}/home`
process.env.USERPROFILE = `${__dirname}/home`

let assert = require('assert')
let cp = require('child_process')
let fs = require('fs')
let path = require('path')
var http = require('http')
let supertest = require('supertest')
let untildify = require('untildify')
let rmrf = require('rimraf')
let pkg = require('../package.json')

let timeout = process.env.TRAVIS ? 10000 : 5000
let url = `http://localhost:2000`

// Used to give some time to the system and commands
function wait (done) {
  setTimeout(done, timeout)
}

// Used to give some time to the system and commands,
// but specify a condition to wait for
function waitFor (condition, done) {
  let start = new Date()
  retry()

  function retry() {
    if (new Date() - start > timeout) return done()
    setTimeout(() => {
      condition((err) => {
        if (!err) return done()
        else retry()
      })
    }, 250)
  }
}

function forUp (cb) {
  http.get(url, () => { cb() })
    .on('error', (e) => { cb(e) })
}

function forDown (cb) {
  http.get(url, () => { cb(new Error()) })
    .on('error', (e) => { cb() })
}

function hotel (cmd) {
  // Execute hotel cmd
  let out = cp.execSync(`node ${__dirname}/../${pkg.bin} ${cmd}`, {
    cwd: `${__dirname}/app`
  }).toString()

  // Log output
  // .replace() used to enhance tests readability
  if (!process.env.QUIET) {
    console.log(out
      .replace(/\n  /g, '\n    ')
      .replace(/\n$/, ''))
  }

  // Return output
  return out
}

let request = supertest(url)

describe('hotel', function () {

  this.timeout(timeout + 1000)

  before((done) => {
    hotel('stop') // Just in case
    rmrf.sync(untildify('~/.hotel'))
    waitFor(forDown, done)
  })

  after(() => hotel('stop'))

  describe('$ hotel start', () => {

    before(done => {
      hotel('start')
      waitFor(forUp, done)
    })

    it('should start daemon', done => {
      request.get('/').expect(200, done)
    })
  })

  describe('app$ hotel add "node index.js"', () => {

    before(done => {
      hotel('add "node index.js"')
      wait(done)
    })

    it('should create a redirection from /app to app server', done => {
      request
        .get('/app')
        .expect(302, (err, res) => {
          if (err) throw err
{'fraction_non_alphanumeric': 0.18590065228299046, 'fraction_numerical': 0.012293025589563472, 'mean_word_length': 2.8080229226361033, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2678166', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1321, 'n_words': 351}