<commit_before><commit_msg>test: ensure error location is logged on log error<commit_after>var path = require('path');
var should = require('chai').should();
var Happner = require('../../..');
var Promise = require('bluebird');
var fs = require('fs');

describe(require('../../__fixtures/utils/test_helper').create().testName(__filename, 3), function () {

  this.timeout(5000);

  var test_id = Date.now() + '_' + require('shortid').generate();
  var logFileName = './temp/' + test_id + '.nedb';

  before('start server', function (done) {

    try {
      fs.unlinkSync(logFileName);
    } catch (e) {
    }
    Happner.create({
      name: 'Server',
      util: {
          logFile:logFileName
      },
      happn: {
        secure: true
      },
      modules: {
        'ComponentName': {
          instance: {
            logMethod: function ($happn, callback) { // "max-nasty" injection
              $happn.log.error('test error');
              callback();
            }
          }
        }
      },
      components: {
        'ComponentName': {}
      }
    })
      .then(function (mesh) {
        var security = mesh.exchange.security;
        server = mesh;
        return Promise.all([
          security.addGroup({
            name: 'group',
            permissions: {
              events: {},
              data: {
                '/allowed/get/*': {actions:['get']},
                '/allowed/on/*': {actions:['on','set']},
                '/allowed/remove/*': {actions:['set','remove','get']},
                '/allowed/all/*': {actions:['*']}
              },
              methods: {
                '/Server/ComponentName/logMethod': {authorized: true}
              }
            }
          }),
          security.addUser({
            username: 'username',
            password: 'password'
          })
        ]).spread(function (group, user) {
          return security.linkGroup(group, user);
        });
      })
      .then(function () {
        done();
      })
      .catch(done);
  });

  it('logs an error without an Error object, we read the logfile and check we have a location the error occurred on', function (done) {
    var client = new Happner.MeshClient();
    client.login({
      username: 'username',
      password: 'password'
    })
    .then(function(){
      client.exchange.ComponentName.logMethod().then(function(){
        var logged = fs.readFileSync(logFileName).toString();
        logged.should.match(/\/test\/integration\/logger\/logs-line-number-error.js:32:26/);
        done();
      });
    })
    .catch(done);
  });

  after('stop server', function (done) {

    try {
      fs.unlinkSync(dbFileName);
    } catch (e) {}

    if (server) server.stop({reconnect: false}).then(function(){
      done();
    });
    else done();

  });
});
{'fraction_non_alphanumeric': 0.16946129147342134, 'fraction_numerical': 0.003210845522654299, 'mean_word_length': 1.8553971486761711, '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': '3132220', 'n_tokens_mistral': 814, 'n_tokens_neox': 785, 'n_words': 162}