{"size": 3795, "ext": "js", "max_stars_repo_path": "test/logger.test.js", "max_stars_repo_name": "pmalouin/auth0-instrumentation", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "test/logger.test.js", "max_issues_repo_name": "pmalouin/auth0-instrumentation", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "test/logger.test.js", "max_forks_repo_name": "pmalouin/auth0-instrumentation", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.95, "max_line_length": 118, "alphanum_fraction": 0.6250329381}
'use strict';

const assert = require('assert');
const sentry = require('../lib/error_reporter')({}, {});
const logger = require('../lib/logger')({ name: 'test' }, 
                                        { LOG_LEVEL: 'fatal', 
                                          PURPOSE: 'test-purpose',
                                          ENVIRONMENT: 'test-env',
                                          AWS_REGION: 'test-region'
                                        });
const spy = require('sinon').spy;

describe('logger', function() {
  beforeEach(function() {
    sentry.captureException = spy();
    sentry.captureMessage = spy();
    sentry.captureException.resetHistory();
    sentry.captureMessage.resetHistory();
  });

  describe('logger.child()', function() {
    it('should support creating child loggers', function() {
      const childLogger = logger.child({
        child: 'child'
      });

      childLogger.error({
        log_type: 'not really an error'
      }, 'test');
      assert(sentry.captureException.calledOnce === false);
      assert(sentry.captureMessage.calledOnce);
      assert(sentry.captureMessage.getCall(0).args[1].tags.log_type, 'not really an error');
      assert(sentry.captureMessage.getCall(0).args[1].extra.child, 'child');
    });

    it('should support creating nested child loggers', function() {
      const childLogger = logger.child({
        child: 'child'
      });
      const grandChildLogger = childLogger.child({
        child: 'grandchild',
        parent: 'child',
      });

      grandChildLogger.error({
        log_type: 'not really an error'
      }, 'test');
      assert(sentry.captureException.calledOnce === false);
      assert(sentry.captureMessage.calledOnce);
      assert(sentry.captureMessage.getCall(0).args[1].tags.log_type, 'not really an error');
      assert(sentry.captureMessage.getCall(0).args[1].extra.child, 'grandchild');
      assert(sentry.captureMessage.getCall(0).args[1].extra.parent, 'child');
    });
  });

  describe('SentryStream', function() {
    it('should call captureException on error when level is error', function() {
      logger.error(new Error('test'));
      assert(sentry.captureException.calledOnce);
      assert(sentry.captureMessage.calledOnce === false);
    });

    it('should call captureMessage on string when level is error', function() {
      logger.error('test');
      assert(sentry.captureException.calledOnce === false);
      assert(sentry.captureMessage.calledOnce);
    });

    it('should add a log_type tag when the log entry has an error and has a log_type property', function() {
      logger.error({
        log_type: 'uncaughtException',
        err: new Error('test err')
      }, 'test');
      assert(sentry.captureException.calledOnce);
      assert(sentry.captureMessage.calledOnce === false);
      assert(sentry.captureException.getCall(0).args[1].tags.log_type, 'uncaughtException');
    });

    it('should add a log_type tag when the log entry does not have an error and has a log_type property', function() {
      logger.error({
        log_type: 'not really an error'
      }, 'test');
      assert(sentry.captureException.calledOnce === false);
      assert(sentry.captureMessage.calledOnce);
      assert(sentry.captureMessage.getCall(0).args[1].tags.log_type, 'not really an error');
    });
  });

  it('should add default values from environment', function() {
    logger.error('testing');
    assert(sentry.captureException.calledOnce === false);
    assert(sentry.captureMessage.calledOnce);
    const captured = sentry.captureMessage.getCall(0).args[1];
    assert.equal(captured.extra.purpose, 'test-purpose');
    assert.equal(captured.extra.environment, 'test-env');
    assert.equal(captured.extra.region, 'test-region');
  });

});
