index.js 3.28 KB
require('./setup');
var index = require('../index');
var Promise = require('bluebird');

describe('Main index.js', function() {

  var validData = {}
  before(function(done) {
    validData = {
      dirname: './log_test/',
      log: {},
      stat: {
        interval: 2,
        data: [
          {key: 'a', threshold: 2, threshold_inv: 1},
          {key: 'b', threshold: 2 }
        ]
      },
      alarm: {}
    }
    done();
  });

  it('init function should return true on cb properly', function(done) {
    var object = index.init(validData);
    expect(object).to.be.an('object');
    expect(object).to.have.property('log');
    expect(object).to.have.property('stat');
    done();
  });

  it('init function should return err on cb properly (case 1)', function(done) {
    try {
      var object = index.init({log: 'hi'});
    }
    catch (err) {
      var my_err = err.message.split(',');
      expect(my_err).to.include.members(['log is invalid format', 'dirname is required', 'alarm is required']);
      done();
    }
  });

  it('init function should return err on cb properly (case 2)', function(done) {
    try {
      var object = index.init({pupu: 'hi'});
    }
    catch (err) {
      var my_err = err.message.split(',');
      expect(my_err).to.include.members(['pupu is not allowed']);
      done();
    }
  });

  it('init function should return err on cb properly (case 3)', function(done) {
    try {
      var object = index.init({pupu: 'hi'});
    }
    catch (err) {
      var my_err = err.message.split(',');
      expect(my_err).to.include.members(['log is required', 'stat is required']);
      done();
    }
  });

  it('should fail to create stat object if validdata object contain no key with threshold', function(done) {
    var testData = {
      dirname: './log_test/',
      log: {},
      stat: {
        interval: 2,
        data: [
          {key: 'a', threshold: 2, threshold_inv: 1},
          {key: 'b', threshold: 2},
          {key: 'c', threshold_inv: 1},
          {key: 'd', threshold: 2},
          {threshold: 2},
        ]
      }
    }
    try {
      var object = index.init(testData);
    }
    catch (err) {
      var my_err = err.message.split(',');
      expect(my_err).to.include.members(['stat.data.4.key is required']);
      done();
    }
  });

  it('should run the whole flow correctly', function(done) {
    var initData = {
      dirname: './whole_flow/',
      log: {
        rotation: 500,
        maxsize: 500
      },
      stat: {
        rotation: 500,
        maxsize: 500,
        interval: 5,
        data: [
          {key: 'testIncrement1', threshold: 3, threshold_inv: 1},
          {key: 'testIncrement2', threshold: 2 }
        ]
      },
      alarm: {
        rotation: 500,
        maxsize: 500
      }
    };
    var logObj = index.init(initData);
    Promise.try(function() {})
      .then(function() {
        logObj.stat.start();
        logObj.log.append({hi: 'test'});
        logObj.stat.increment('testIncrement1');
        return Promise.delay(100);
      })
      .then(function() {
        logObj.log.append({hi: 'test2'});
        logObj.stat.increment('testIncrement2');
        logObj.log.append({hi: 'test2'});
        logObj.stat.increment('testIncrement2');
      })
      .then(function() {
        logObj.stat.stop();
        done();
      })


  });


});