<commit_before>/*
Name:         overheid.io - test.js
Description:  Access overheid.io endpoints like OpenKvK, RDW and BAG with node.js
Author:       <NAME> (https://frankl.in)
Source code:  https://github.com/fvdm/nodejs-overheid.io
Feedback:     https://github.com/fvdm/nodejs-overheid.io/issues
License:      Unlicense (Public Domain) - see LICENSE file
              (https://github.com/fvdm/nodejs-overheid.io/raw/master/LICENSE)
*/

// Setup
var app = require ('./');
var ovio = new app (
  process.env.OVIO_APIKEY || null,
  'rdw',
  {
    timeout: process.env.OVIO_TIMEOUT || 5000
  }
);


// handle exits
var errors = 0;
process.on ('exit', function () {
  if (errors === 0) {
    console.log ('\n\033[1mDONE, no errors.\033[0m\n');
    process.exit (0);
  } else {
    console.log ('\n\033[1mFAIL, '+ errors +' error'+ (errors > 1 ? 's' : '') +' occurred!\033[0m\n');
    process.exit (1);
  }
});

// prevent errors from killing the process
process.on ('uncaughtException', function (err) {
  console.log ();
  console.error (err.stack);
  console.trace ();
  console.log ();
  errors++;
});

// Queue to prevent flooding
var queue = [];
var next = 0;

function doNext () {
  next++;
  if (queue [next]) {
    queue [next] ();
  }
}

// doTest( passErr, 'methods', [
//   ['feeds', typeof feeds === 'object']
// ])
function doTest (err, label, tests) {
  if (err instanceof Error) {
    console.error ('\033[1m\033[31mERROR\033[0m - '+ label +'\n');
    console.dir (err, { depth: null, colors: true });
    console.log ();
    console.error (err.stack);
    console.log ();
    errors++;
  } else {
    var testErrors = [];
    for (var i = 0; i < tests.length; i++) {
      if (tests [i] [1] !== true) {
        testErrors.push (tests [i] [0]);
        errors++;
      }
    }

    if(testErrors.length === 0) {
      console.log ('\033[1m\033[32mgood\033[0m - '+ label);
    } else {
      console.error ('\033[1m\033[31mFAIL\033[0m - '+ label +' ('+ testErrors.join (', ') +')');
    }
  }

  doNext ();
}


queue.push (function () {
  var tmp = new app (
    process.env.OVIO_APIKEY || null,
    'rdw',
    {
      timeout: 1
    }
  );

  tmp ('4-TFL-24', { fields: ['eerstekleur'] }, function (err) {
    doTest (null, 'config.timeout', [
      ['type', err instanceof Error],
      ['message', err && err.message === 'request failed'],
      ['error', err && err.error instanceof Object],
      ['code', err && err.error && err.error.code === 'TIMEOUT']
    ]);
  });
});


queue.push (function () {
  ovio ('error.test', function (err) {
    doTest (null, 'error handling', [
      ['type', err instanceof Error],
      ['message', err && err.message === 'no result']
    ]);
  });
});


queue.push (function () {
  ovio ('4-TFL-24', { fields: ['eerstekleur'] }, function (err, data) {
    doTest (err, 'item', [
      ['data', data && data != null],
      ['type', data instanceof Object],
      ['property', data && data.kenteken === '4-TFL-24'],
      ['field', data && typeof data.eerstekleur === 'string']
    ]);
  })
});


queue.push (function () {
  ovio (
    {
      filters: { merk: 'bmw' },
      fields: ['eerstekleur', 'vermogen']
    },
    function (err, data) {
      doTest (err, 'list', [
        ['type', data && data instanceof Object],
        ['total', data && data.totalItemCount >= 1],<commit_msg>Test: renamed 'error handling' to 'no result'<commit_after>/*
Name:         overheid.io - test.js
Description:  Access overheid.io endpoints like OpenKvK, RDW and BAG with node.js
Author:       <NAME> (https://frankl.in)
Source code:  https://github.com/fvdm/nodejs-overheid.io
Feedback:     https://github.com/fvdm/nodejs-overheid.io/issues
License:      Unlicense (Public Domain) - see LICENSE file
              (https://github.com/fvdm/nodejs-overheid.io/raw/master/LICENSE)
*/

// Setup
var app = require ('./');
var ovio = new app (
  process.env.OVIO_APIKEY || null,
  'rdw',
  {
    timeout: process.env.OVIO_TIMEOUT || 5000
  }
);


// handle exits
var errors = 0;
process.on ('exit', function () {
  if (errors === 0) {
    console.log ('\n\033[1mDONE, no errors.\033[0m\n');
    process.exit (0);
  } else {
    console.log ('\n\033[1mFAIL, '+ errors +' error'+ (errors > 1 ? 's' : '') +' occurred!\033[0m\n');
    process.exit (1);
  }
});

// prevent errors from killing the process
process.on ('uncaughtException', function (err) {
  console.log ();
  console.error (err.stack);
  console.trace ();
  console.log ();
  errors++;
});

// Queue to prevent flooding
var queue = [];
var next = 0;

function doNext () {
  next++;
  if (queue [next]) {
    queue [next] ();
  }
}

// doTest( passErr, 'methods', [
//   ['feeds', typeof feeds === 'object']
// ])
function doTest (err, label, tests) {
  if (err instanceof Error) {
    console.error ('\033[1m\033[31mERROR\033[0m - '+ label +'\n');
    console.dir (err, { depth: null, colors: true });
    console.log ();
    console.error (err.stack);
    console.log ();
    errors++;
  } else {
    var testErrors = [];
    for (var i = 0; i < tests.length; i++) {
      if (tests [i] [1] !== true) {
        testErrors.push (tests [i] [0]);
        errors++;
      }
    }

    if(testErrors.length === 0) {
      console.log ('\033[1m\033[32mgood\033[0m - '+ label);
    } else {
      console.error ('\033[1m\033[31mFAIL\033[0m - '+ label +' ('+ testErrors.join (', ') +')');
    }
  }

  doNext ();
}


queue.push (function () {
  var tmp = new app (
    process.env.OVIO_APIKEY || null,
    'rdw',
    {
      timeout: 1
    }
  );

  tmp ('4-TFL-24', { fields: ['eerstekleur'] }, function (err) {
    doTest (null, 'config.timeout', [
      ['type', err instanceof Error],
      ['message', err && err.message === 'request failed'],
      ['error', err && err.error instanceof Object],
      ['code', err && err.error && err.error.code === 'TIMEOUT']
    ]);
  });
});


queue.push (function () {
  ovio ('error.test', function (err) {
    doTest (null, 'no result', [
      ['type', err instanceof Error],
      ['message', err && err.message === 'no result']
    ]);
  });
});


queue.push (function () {
  ovio ('4-TFL-24', { fields: ['eerstekleur'] }, function (err, data) {
    doTest (err, 'item', [
      ['data', data && data != null],
      ['type', data instanceof Object],
      ['property', data && data.kenteken === '4-TFL-24'],
      ['field', data && typeof data.eerstekleur === 'string']
    ]);
  })
});


queue.push (function () {
  ovio (
    {
      filters: { merk: 'bmw' },
      fields: ['eerstekleur', 'vermogen']
    },
    function (err, data) {
      doTest (err, 'list', [
        ['type', data && data instanceof Object],
        ['total', data && data.totalItemCount >= 1],
{'fraction_non_alphanumeric': 0.20309938906273284, 'fraction_numerical': 0.02384145432871405, 'mean_word_length': 2.6105433028509952, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 8, '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': '149186', 'n_tokens_mistral': 2478, 'n_tokens_neox': 2349, 'n_words': 563}