"use strict";

var fs      = require('fs');
var path    = require('path');
var expect  = require('chai').expect();
var should  = require('chai').should();
var pdf2img = require('../index.js');

var input   = __dirname + path.sep + 'test.pdf';

pdf2img.setOptions({
  outputdir: __dirname + path.sep + '/output',
  outputname: 'test'
});

describe('Split and convert pdf into images', function() {
  it ('Create jpg files', function(done) {
    this.timeout(100000);
    pdf2img.convert(input, function(err, info) {
      if (info.result !== 'success') {
        info.result.should.equal('success');
        done();
      } else {
        var n = 1;
        info.message.forEach(function(file) {
          file.page.should.equal(n);
          file.name.should.equal('test_' + n + '.jpg');
          isFileExists(file.path).should.to.be.true;
          if (n === 3) done();
          n++;
        });
      }
    });
  });
  it ('Create png files', function(done) {
    this.timeout(100000);
    pdf2img.setOptions({ type: 'png' });
    pdf2img.convert(input, function(err, info) {
      if (info.result !== 'success') {
        info.result.should.equal('success');
        done();
      } else {
        var n = 1;
        info.message.forEach(function(file) {
          file.page.should.equal(n);
          file.name.should.equal('test_' + n + '.png');
          isFileExists(file.path).should.to.be.true;
          if (n === 3) done();
          n++;
        });
      }
    });
  });
  it ('Create jpg file only for given page', function(done) {
    this.timeout(100000);
    pdf2img.setOptions({ type: 'jpg', page: 2 });
    pdf2img.convert(input, function(err, info) {
      if (info.result !== 'success') {
        info.result.should.equal('success');
        done();
      } else {
        info.message.length.should.equal(1)
        var file = info.message[0];
        file.page.should.equal(2);
        file.name.should.equal('test_2.jpg');
        isFileExists(file.path).should.to.be.true;
        done();
      }
    });
  });
});

var isFileExists = function(path) {
  try {
    return fs.statSync(path).isFile();
  } catch (e) {
    return false;
  }
}

@@ -1614,15 +1614,567 @@
 age:
+ 1 %7D);%0A    pdf2img.convert(input, function(err, info) %7B%0A      if (info.result !== 'success') %7B%0A        info.result.should.equal('success');%0A        done();%0A      %7D else %7B%0A        info.message.length.should.equal(1)%0A        var file = info.message%5B0%5D;%0A        file.page.should.equal(1);%0A        file.name.should.equal('test_1.jpg');%0A        isFileExists(file.path).should.to.be.true;%0A        done();%0A      %7D%0A    %7D);%0A  %7D);%0A  it ('Create png file only for given page', function(done) %7B%0A    this.timeout(100000);%0A    pdf2img.setOptions(%7B type: 'png', page:
  2 %7D);%0A
-
     
@@ -2487,18 +2487,18 @@
 'test_2.
-j
 p
+n
 g');%0A   

Update test.