<commit_before>var util = require('util');
var fs = require('fs');
var should = require('should');
var winston = require('winston');
var elasticsearch = require('elasticsearch');

require('../index');
var defaultTransformer = require('../transformer');

var logMessage = JSON.parse(fs.readFileSync('./test/request_logentry_1.json', 'utf8'));

/*
 * Note: To run the tests, a running elasticsearch instance is required.
 */

describe('winston-elasticsearch:', function() {
  describe('the default transformer', function() {
    it('should transform logdata from winston into a lostash like structure', function (done) {
      var transformed = defaultTransformer({
        message: 'some message',
        level: 'error',
        meta: {
          someField: true
        }
      });
      should.exist(transformed['@timestamp']);
      transformed.severity.should.equal('error');
      transformed.fields.someField.should.be.true();
      done();
    });
  });

  describe('a transport given a faulty ES client', function() {
    it('should throw an exception', function(done) {
      try {
        new (winston.transports.Elasticsearch)({
          client: new elasticsearch.Client({
            host: 'http://localhost:9300'
          })
        });
      } catch (error) {
        done();
      }
    });
  });

  var logger = new (winston.Logger)({
    transports: [
      new (winston.transports.Elasticsearch)({
      })
    ]
  });

  describe('a logger', function() {
    it('should log to Elasticsearch', function(done) {
      this.timeout(6000);
      logger.log(logMessage.level, logMessage.message, logMessage.meta,
        function (err, level, msg, meta) {
            should.not.exist(err);
            level.should.equal(logMessage.level);
            msg.should.equal(logMessage.message);
            // Short wait phase to make sure data is already written.
            setTimeout(function() {
              done();
            }, 1500);
        });
    });

    describe('the logged message', function() {
      it('should be found in the index', function(done) {
        logger.transports.elasticsearch.search('message:logmessage1').then(
          (res) => {
            res.hits.total.should.be.above(0);
            done();
          },
          (err) => {
            should.not.exist(err);
            done();
          });
      });
    });
  });
});
<commit_msg>Update test.js<commit_after>var util = require('util');
var fs = require('fs');
var should = require('should');
var winston = require('winston');
var elasticsearch = require('elasticsearch');

require('../index');
var defaultTransformer = require('../transformer');

var logMessage = JSON.parse(fs.readFileSync('./test/request_logentry_1.json', 'utf8'));

/*
 * Note: To run the tests, a running elasticsearch instance is required.
 */

describe('winston-elasticsearch:', function() {
  describe('the default transformer', function() {
    it('should transform logdata from winston into a lostash like structure', function (done) {
      var transformed = defaultTransformer({
        message: 'some message',
        level: 'error',
        meta: {
          someField: true
        }
      });
      should.exist(transformed['@timestamp']);
      transformed.severity.should.equal('error');
      transformed.fields.someField.should.be.true();
      done();
    });
  });

  var logger = new (winston.Logger)({
    transports: [
      new (winston.transports.Elasticsearch)({
      })
    ]
  });

  describe('a logger', function() {
    it('should log to Elasticsearch', function(done) {
      this.timeout(6000);
      logger.log(logMessage.level, logMessage.message, logMessage.meta,
        function (err, level, msg, meta) {
            should.not.exist(err);
            level.should.equal(logMessage.level);
            msg.should.equal(logMessage.message);
            // Short wait phase to make sure data is already written.
            setTimeout(function() {
              done();
            }, 1500);
        });
    });

    describe('the logged message', function() {
      it('should be found in the index', function(done) {
        logger.transports.elasticsearch.search('message:logmessage1').then(
          (res) => {
            res.hits.total.should.be.above(0);
            done();
          },
          (err) => {
            should.not.exist(err);
            done();
          });
      });
    });
  });
});

{'fraction_non_alphanumeric': 0.15124153498871332, 'fraction_numerical': 0.006320541760722348, 'mean_word_length': 2.3902065799540932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '6761337', 'n_tokens_mistral': 1249, 'n_tokens_neox': 1218, 'n_words': 275}