#!/usr/bin/env node

var should = require('should');

var dictionary = require('./index.js');

var verbose = !!(process.argv.indexOf('--verbose') !== -1 || process.argv.indexOf('-v') !== -1);

var letter = 'b';
var version = '1.0.0';

// exported functions
function test_exported(){
  dictionary.should.have.property('load').which.is.a.Function();
  dictionary.should.have.property('loadSync').which.is.a.Function();
  dictionary.should.have.property('version').which.is.a.String();
  should(dictionary.letter).be.exactly(letter);
  should(dictionary.version).be.exactly(version);
}


function _test_the_data(data) {
  if(verbose) process.stdout.write('length: '+data.length+'\r\n');
  data[0].should.have.property('letter').which.is.a.String().with.lengthOf(1);
  should(data.length).be.exactly(dictionary.length);

  data.should.be.instanceof(Array);
  for (var j = 0; j < /*data.length*/1; j++) {
    if (verbose) process.stdout.write('>>> index: '+j+'\r\n');
    data[j].should.be.an.instanceOf(Object);
    data[j].should.have.property('letter').which.is.a.String().with.lengthOf(1);
    if (verbose) process.stdout.write('letter: '+data[j].letter+'\r\n');
    data[j].should.have.property('word_url').which.is.a.String();
    if (verbose) process.stdout.write('word_url: '+data[j].url+'\r\n');
    data[j].should.have.property('datetime').which.is.a.String().and.have.lengthOf(19);
    if (verbose) process.stdout.write('datetime: '+data[j].datetime+'\r\n');
    data[j].should.have.property('page_number').which.is.a.Number();
    data[j].should.have.property('page_url').which.is.a.String();
    data[j].should.have.property('key').which.is.a.String();
    if (verbose) process.stdout.write('key+'+data[j].key+'\r\n');
  }
}

// LOAD ASYNC
function test_load() {
  process.stdout.write('>>> '+dictionary.letter+'\r\n');
  dictionary.load(function(err, data) {
    if (err) throw err;
    _test_the_data(data);
  });

}

// LOAD SYNC
function test_loadSync() {
  process.stdout.write('>>> '+dictionary.letter+'\r\n');
  _test_the_data(dictionary.loadSync());
}

test_exported();
// test_load(); // fix this test to finish before 'All tests passed!' without adding dependencies
test_loadSync();

process.stdout.write('All tests passed! 👍 <<<\r\n');
