var assert = require('assert');
var Buffer = require('buffer').Buffer;
var fs = require('fs');
var blend = require('..');

if (process.setMaxListeners) process.setMaxListeners(0);

exports['test blending bogus JPEG image'] = function(beforeExit) {
    var completed = false;
    var buffer = new Buffer(32);
    buffer[0] = 255;
    buffer[1] = 216;
    blend([
        buffer,
        fs.readFileSync('test/fixture/2.png')
    ], function(err, data) {
        completed = true;
        assert.ok(err);
        assert.equal(err.message, "Premature end of JPEG file");
    });

    beforeExit(function() { assert.ok(completed); });
};

exports['test blending a malformed jpeg image'] = function(beforeExit) {
    var completed = false;

    blend([
        fs.readFileSync('test/fixture/1d.jpg'),
        fs.readFileSync('test/fixture/2.png')
    ], function(err, data) {
        completed = true;
        assert.ok(err);
        assert.equal(err.message, "Corrupt JPEG data: bad Huffman code");
    });

    beforeExit(function() { assert.ok(completed); });
};

exports['test blending a jpeg image'] = function(beforeExit) {
    var completed = false;

    blend([
        fs.readFileSync('test/fixture/1a.jpg'),
        fs.readFileSync('test/fixture/2.png')
    ], function(err, data) {
        completed = true;
        if (err) throw err;
    });

    beforeExit(function() { assert.ok(completed); });
};

exports['test blending a grayscale jpeg image'] = function(beforeExit) {
    var completed = false;

    blend([
        fs.readFileSync('test/fixture/1b.jpg'),
        fs.readFileSync('test/fixture/2.png')
    ], function(err, data) {
        completed = true;
        if (err) throw err;
    });

    beforeExit(function() { assert.ok(completed); });
};


exports['test blending a color jpeg image'] = function(beforeExit) {
    var completed = false;

    blend([
        fs.readFileSync('test/fixture/1c.jpg'),
        fs.readFileSync('test/fixture/2.png')
    ], function(err, data) {
        completed = true;
        if (err) throw err;
    });

    beforeExit(function() { assert.ok(completed); });
};


@@ -64,16 +64,48 @@
 Buffer;%0A
+var crypto = require('crypto');%0A
 var fs =
@@ -148,16 +148,108 @@
 '..');%0A%0A
+function md5(buffer) %7B%0A    return crypto.createHash('md5').update(buffer).digest('hex');%0A%7D%0A%0A
 if (proc
@@ -1455,32 +1455,101 @@
  throw err;%0A
+        assert.equal('4e43abdd243ccafc80a7da9b0efe3086', md5(data));%0A
     %7D);%0A
 %0A    beforeE
@@ -1528,32 +1528,32 @@
 data));%0A    %7D);%0A
-
 %0A    beforeExit(
@@ -1877,32 +1877,101 @@
 err) throw err;%0A
+        assert.equal('6a11a65c397f9f3929791703dd61645f', md5(data));%0A
     %7D);%0A%0A    bef
@@ -2272,32 +2272,32 @@
 mpleted = true;%0A
-
         if (err)
@@ -2304,24 +2304,93 @@
  throw err;%0A
+        assert.equal('393a40fd34bfd58f4d5c5e658f6f00f1', md5(data));%0A
     %7D);%0A%0A   

check output with md5