<commit_before>"use strict";

var sand = require('../../');
var path = require('path');
var child = require('child_process');

process.env.SAND_APP_PATH = path.resolve(__dirname + '/../');

describe('Events', function() {
  "use strict";
  it ('should emit start event', function(done) {
    child.fork('test/sand/helpers/testStartEvent')
      .on('exit', function(code) {
        code.should.be.eql(2);
        done();
      });
  });

  it ('should emit shutdown event', function(done) {
    child.fork('test/sand/helpers/testShutdownEvent')
      .on('exit', function(code) {
        code.should.be.eql(3);
        done();
      });
  });
});

describe('Application', function() {
  "use strict";
  it('should kill init after bad module', function(done) {
    child.fork('test/sand/helpers/testInitTimeout')
      .on('exit', function(code) {
        code.should.be.eql(1);
        done();
      });
  });

  it('should kill shutdown after timeout', function(done) {
    child.fork('test/sand/helpers/testShutdownTimeout')
      .on('exit', function(code) {
        code.should.be.eql(1);
        done();
      });
  });

  it ('should listen to shutdown event', function(done) {
    let c = child.fork('test/sand/helpers/testSignalEvents')
      .on('exit', function(code) {
        code.should.be.eql(4);
        done();
      });

    c.send('shutdown');
  });

  let signals = ['SIGTERM', 'SIGINT'];

  for (let signal of signals) {
    it(`should listen to ${signal} event`, function (done) {
      let c = child.fork('test/sand/helpers/testSignalEvents')
        .on('exit', function (code, signal) {
          code.should.be.eql(4);
          done();
        });

      // Make sure sand started and bound to event
      setTimeout(function () {
        c.kill(signal);
      }, 200);
    });
  }

  for (let signal of signals) {
    it(`should listen to ${signal} events from node-pm`, function (done) {
      let c = child.spawn(path.normalize(__dirname + '/../../node_modules/node-pm/bin/node-pm'), [path.normalize(__dirname + '/helpers/testSignalEvents.js')])
        .on('exit', function (code, signal) {
          code.should.be.eql(0);
          done();
        });

      // Make sure sand started and bound to event
      setTimeout(function () {
        c.kill(signal);
      }, 200);
    });
  }

});

describe('Config', function() {
  "use strict";
  it ('should set defaults', function() {
    var app = new sand();
    app.env.should.be.equal('test');
    app.config.initTimeout.should.be.a.Number;
  });

  it ('should use passed in config', function() {
    var app = new sand({
      env: 'mine'
    });

    app.config.env.should.be.equal('mine');
  });

  it('should load from file', function() {
    var app = new sand({
      configPath: path.resolve(__dirname + '/helpers/config.js')
    });

    app.config.env.should.be.equal('mine')
  });

  it('should load log level correctly', function() {
    var app = new sand({
      configPath: path.resolve(__dirname + '/helpers/config.js')
    });

    app.config.log.should.be.eql('unknown');
  });
});

describe('app.inspect()', function(){
  it('should work', function(){
    var app = new sand();
    var util = require('util');
    util.inspect(app);
  });
});

describe.only('Context', () => {
  let app = new sand({
    configPath: path.resolve(__dirname + '/helpers/config.js')
  });

  it ('should run in separate context', (done) => {
    app.ctx.test = 1;
    app.ctx.test.should.be.eql(1);

    app.runInContext(() => {
      global.sand.ctx.test = 2;
      global.sand.ctx.test.should.be.eql(2);
      done();
    });

    app.ctx.test.should.be.eql(1);<commit_msg>remove '.only()' from tests<commit_after>"use strict";

var sand = require('../../');
var path = require('path');
var child = require('child_process');

process.env.SAND_APP_PATH = path.resolve(__dirname + '/../');

describe('Events', function() {
  "use strict";
  it ('should emit start event', function(done) {
    child.fork('test/sand/helpers/testStartEvent')
      .on('exit', function(code) {
        code.should.be.eql(2);
        done();
      });
  });

  it ('should emit shutdown event', function(done) {
    child.fork('test/sand/helpers/testShutdownEvent')
      .on('exit', function(code) {
        code.should.be.eql(3);
        done();
      });
  });
});

describe('Application', function() {
  "use strict";
  it('should kill init after bad module', function(done) {
    child.fork('test/sand/helpers/testInitTimeout')
      .on('exit', function(code) {
        code.should.be.eql(1);
        done();
      });
  });

  it('should kill shutdown after timeout', function(done) {
    child.fork('test/sand/helpers/testShutdownTimeout')
      .on('exit', function(code) {
        code.should.be.eql(1);
        done();
      });
  });

  it ('should listen to shutdown event', function(done) {
    let c = child.fork('test/sand/helpers/testSignalEvents')
      .on('exit', function(code) {
        code.should.be.eql(4);
        done();
      });

    c.send('shutdown');
  });

  let signals = ['SIGTERM', 'SIGINT'];

  for (let signal of signals) {
    it(`should listen to ${signal} event`, function (done) {
      let c = child.fork('test/sand/helpers/testSignalEvents')
        .on('exit', function (code, signal) {
          code.should.be.eql(4);
          done();
        });

      // Make sure sand started and bound to event
      setTimeout(function () {
        c.kill(signal);
      }, 200);
    });
  }

  for (let signal of signals) {
    it(`should listen to ${signal} events from node-pm`, function (done) {
      let c = child.spawn(path.normalize(__dirname + '/../../node_modules/node-pm/bin/node-pm'), [path.normalize(__dirname + '/helpers/testSignalEvents.js')])
        .on('exit', function (code, signal) {
          code.should.be.eql(0);
          done();
        });

      // Make sure sand started and bound to event
      setTimeout(function () {
        c.kill(signal);
      }, 200);
    });
  }

});

describe('Config', function() {
  "use strict";
  it ('should set defaults', function() {
    var app = new sand();
    app.env.should.be.equal('test');
    app.config.initTimeout.should.be.a.Number;
  });

  it ('should use passed in config', function() {
    var app = new sand({
      env: 'mine'
    });

    app.config.env.should.be.equal('mine');
  });

  it('should load from file', function() {
    var app = new sand({
      configPath: path.resolve(__dirname + '/helpers/config.js')
    });

    app.config.env.should.be.equal('mine')
  });

  it('should load log level correctly', function() {
    var app = new sand({
      configPath: path.resolve(__dirname + '/helpers/config.js')
    });

    app.config.log.should.be.eql('unknown');
  });
});

describe('app.inspect()', function(){
  it('should work', function(){
    var app = new sand();
    var util = require('util');
    util.inspect(app);
  });
});

describe('Context', () => {
  let app = new sand({
    configPath: path.resolve(__dirname + '/helpers/config.js')
  });

  it ('should run in separate context', (done) => {
    app.ctx.test = 1;
    app.ctx.test.should.be.eql(1);

    app.runInContext(() => {
      global.sand.ctx.test = 2;
      global.sand.ctx.test.should.be.eql(2);
      done();
    });

    app.ctx.test.should.be.eql(1);
{'fraction_non_alphanumeric': 0.18790113341526696, 'fraction_numerical': 0.004916018025399427, 'mean_word_length': 2.6007866273353, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '4600479', 'n_tokens_mistral': 2427, 'n_tokens_neox': 2354, 'n_words': 496}