<commit_before>'use strict';

var sinon = require('sinon');

var metricsModule = require('../../lib/metrics');

describe('Metrics Middleware Tests', function() {

  var logStub,
      oldConsoleLog = console.log;

  beforeEach(function(done) {
    process.domain = {
      // timer.js issues with domain
      enter: function() {},
      exit: function() {}
    };
    console.log = oldConsoleLog;
    done();
  });

  it('should log the metrics information if the middleware has a logger instance' +
      ' and metrics information' , function(done) {

    var writeMetricsMethod;
    logStub = {
      info: function(msg, context) {
      }
    };

    var nextMiddlewareStub = {
      next: function() {}
    };

    var logSpy = sinon.spy(logStub, 'info');
    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    var res = {
      on: function(eventType, writeMetrics) {
        writeMetricsMethod = writeMetrics;
      }
    };

    var resSpy = sinon.spy(res, 'on');

    metricsModule(logStub)(null, res, nextMiddlewareStub.next);

    process.domain.metrics.info = 'info';
    process.domain.metrics.status = 'ok';

    expect(resSpy).to.have.calledOnce;
    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(logSpy).to.not.have.called;
    expect(writeMetricsMethod).to.exist;

    // call writeMetrics method
    writeMetricsMethod();
    expect(logSpy).to.have.calledWith('metrics', {info: 'info', status: 'ok'});
    done();

  });


  it('should call next middleware without domain', function() {
    process.domain = null;
    logStub = {
      info: function(msg, context) {
      }
    };

    var nextMiddlewareStub = {
      next: function() {}
    };

    var logSpy = sinon.spy(logStub, 'info');
    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    metricsModule(logStub)(null, null, nextMiddlewareStub.next);

    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(logSpy).to.not.have.called;

  });

  it('should write to console the metric trace if the logger does not exist' , function(done) {

    var writeMetricsMethod;

    var nextMiddlewareStub = {
      next: function() {}
    };

    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    var res = {
      on: function(eventType, writeMetrics) {
        writeMetricsMethod = writeMetrics;
      }
    };

    var consoleStub = {
      log: function(msg, context) {}
    };

    var resSpy = sinon.spy(res, 'on');
    var consoleSpy = sinon.spy(consoleStub, 'log');
    console.log = consoleStub.log;
    metricsModule()(null, res, nextMiddlewareStub.next);

    process.domain.metrics.info = 'info';
    process.domain.metrics.status = 'ok';

    expect(resSpy).to.have.calledOnce;
    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(consoleSpy).to.not.have.called;
    expect(writeMetricsMethod).to.exist;

    // call writeMetrics method
    writeMetricsMethod();
    expect(consoleSpy).to.have.calledOnce;;
    expect(consoleSpy).to.have.calledWith('metrics', {info: 'info', status: 'ok'});
    done();

  });


  it('should not log the metrics information if the middleware has a logger instance' +
      ' and metrics is empty' , function(done) {

    var writeMetricsMethod;
    logStub = {
      info: function(msg, context) {
      }
    };

    var nextMiddlewareStub = {
      next: function() {}
    };

    var logSpy = sinon.spy(logStub, 'info');
    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    var res = {
      on: function(eventType, writeMetrics) {
        writeMetricsMethod = writeMetrics;
      }
    };

    var resSpy = sinon.spy(res, 'on');

    metricsModule(logStub)(null, res, nextMiddlewareStub.next);


    expect(resSpy).to.have.calledOnce;
    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(logSpy).to.not.have.called;
    expect(writeMetricsMethod).to.exist;

    // call writeMetrics method
    writeMetricsMethod();
    expect(logSpy).to.not.have.called;
    done();

  });


  after(function(done) {<commit_msg>FIX unit tests.<commit_after>'use strict';

var sinon = require('sinon');

var metricsModule = require('../../lib/metrics');

describe('Metrics Middleware Tests', function() {

  var logStub,
      oldConsoleLog = console.log;

  beforeEach(function(done) {
    process.domain = {
      // timer.js issues with domain
      enter: function() {},
      exit: function() {}
    };
    console.log = oldConsoleLog;
    done();
  });

  it('should log the metrics information if the middleware has a logger instance' +
      ' and metrics information' , function() {

    var writeMetricsMethod;
    logStub = {
      info: function(msg, context) {
      }
    };

    var nextMiddlewareStub = {
      next: function() {}
    };

    var logSpy = sinon.spy(logStub, 'info');
    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    var res = {
      on: function(eventType, writeMetrics) {
        writeMetricsMethod = writeMetrics;
      }
    };

    var resSpy = sinon.spy(res, 'on');

    metricsModule(logStub)(null, res, nextMiddlewareStub.next);

    process.domain.metrics.info = 'info';
    process.domain.metrics.status = 'ok';

    expect(resSpy).to.have.calledOnce;
    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(logSpy).to.not.have.called;
    expect(writeMetricsMethod).to.exist;

    // call writeMetrics method
    writeMetricsMethod();
    expect(logSpy).to.have.calledWith('metrics', {info: 'info', status: 'ok'});

  });


  it('should call next middleware without domain', function() {
    process.domain = null;
    logStub = {
      info: function(msg, context) {
      }
    };

    var nextMiddlewareStub = {
      next: function() {}
    };

    var logSpy = sinon.spy(logStub, 'info');
    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    metricsModule(logStub)(null, null, nextMiddlewareStub.next);

    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(logSpy).to.not.have.called;

  });

  it('should write to console the metrics trace if the logger does not exist' , function() {

    var writeMetricsMethod;

    var nextMiddlewareStub = {
      next: function() {}
    };

    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    var res = {
      on: function(eventType, writeMetrics) {
        writeMetricsMethod = writeMetrics;
      }
    };

    var consoleStub = {
      log: function(msg, context) {}
    };

    var resSpy = sinon.spy(res, 'on');
    var consoleSpy = sinon.spy(consoleStub, 'log');
    console.log = consoleStub.log;
    metricsModule()(null, res, nextMiddlewareStub.next);

    process.domain.metrics.info = 'info';
    process.domain.metrics.status = 'ok';

    expect(resSpy).to.have.calledOnce;
    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(consoleSpy).to.not.have.called;
    expect(writeMetricsMethod).to.exist;

    // call writeMetrics method
    writeMetricsMethod();
    expect(consoleSpy).to.have.calledOnce;;
    expect(consoleSpy).to.have.calledWith('metrics', {info: 'info', status: 'ok'});

  });


  it('should not log the metrics information if the middleware has a logger instance' +
      ' and metrics is empty' , function() {

    var writeMetricsMethod;
    logStub = {
      info: function(msg, context) {
      }
    };

    var nextMiddlewareStub = {
      next: function() {}
    };

    var logSpy = sinon.spy(logStub, 'info');
    var nextMiddlewareSpy = sinon.spy(nextMiddlewareStub, 'next');

    var res = {
      on: function(eventType, writeMetrics) {
        writeMetricsMethod = writeMetrics;
      }
    };

    var resSpy = sinon.spy(res, 'on');

    metricsModule(logStub)(null, res, nextMiddlewareStub.next);


    expect(resSpy).to.have.calledOnce;
    expect(nextMiddlewareSpy).to.have.calledOnce;
    expect(logSpy).to.not.have.called;
    expect(writeMetricsMethod).to.exist;

    // call writeMetrics method
    writeMetricsMethod();
    expect(logSpy).to.not.have.called;

  });


  after(function(done) {
{'fraction_non_alphanumeric': 0.13639197107592568, 'fraction_numerical': 0.0, 'mean_word_length': 3.0250878073256398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4480421', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2550, 'n_words': 507}