// Copyright, 2013-2014, by Tomas Korcak. <korczis@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

(function () {
    var chai = require('chai')
        , expect = chai.expect;

    var Engine = require('../lib/engine')
        , Mc = require('./../lib');

    describe('Engine', function () {
        var testData1 = {
            url: 'http://google.com',
            processor: 'seznam.listing'
        };

        var testData2 = {
            url: 'http://seznam.cz',
            processor: 'seznam.listing'
        };

        var testData3 = {
            url: 'http://google.com',
            processor: 'google.details'
        };

        it('Module is defined', function () {
            Mc.Engine.should.not.equal(null);
        });

        it('Default constructor works', function () {
            var instance = new Mc.Engine();
            instance.should.not.equal(null);
            instance.should.be.an.instanceof(Engine);
            instance.opts.should.equal(Engine.defaultOptions);
        });

        describe('enqueueUrl()', function () {
            it('Is defined()', function () {
                var engine = new Engine();
                engine.enqueueUrl.should.not.equal(null);
            });

            it('Should enqueue unique URL', function () {
                var engine = new Engine();

                engine.enqueueUrl.should.not.equal(null);

                var res = engine.enqueueUrl(testData1.url, testData1.processor, null);
                chai.expect(res).to.equal(true);
            });

            it('Should enqueue unique URL together with data', function () {
                var engine = new Engine();

                engine.enqueueUrl.should.not.equal(null);

                var data = {
                    name: "John Doe"
                };

                var res = engine.enqueueUrl(testData1.url, testData1.processor, data);
                chai.expect(res).to.equal(true);

                chai.expect(engine.queue.requested[0].data).to.equal(data);
            });

            it('Should enqueue unique same URL only once', function () {
                var engine = new Engine();
                engine.enqueueUrl.should.not.equal(null);

                var res = engine.enqueueUrl(testData1.url, testData1.processor, null);
                chai.expect(res).to.equal(true);

                res = engine.enqueueUrl(testData1.url, testData1.processor, null);
                chai.expect(res).to.equal(false);
            });
        });

        describe('isDone()', function () {
            it('Is defined', function () {
                var engine = new Mc.Engine();
                engine.isDone.should.not.equal(null);
            });
        });

        describe('loadProcessors()', function () {
            it('Is defined', function () {
                var engine = new Engine();
                engine.loadProcessors.should.not.equal(null);
            });
        });

        describe('main()', function () {
            it('Is defined', function () {
                var engine = new Mc.Engine();
                engine.main.should.not.equal(null);
            });

            it('Returns promise');

            it('Throws exception if invalid argv passed');
        });

        describe('registerProcessor()', function () {
            it('Is defined', function () {
                var engine = new Engine();
                engine.registerProcessor.should.not.equal(null);
            });

            it('Can be called without arguments', function () {
                var engine = new Engine();
                chai.expect(engine.registerProcessor.bind('engine')).to.throw(TypeError);
            });
        });

        describe('run()', function () {
            it('Works', function () {
                var instance = new Engine();
                instance.run();
            });
        });

        describe('wasAlreadyEnqueued()', function () {
            it('Should be defined', function () {
                var engine = new Engine();
                engine.wasAlreadyEnqueued.should.not.equal(null);
            });

            it('Should return true for same url and same processor', function () {
                var engine = new Engine();

                engine.enqueueUrl(testData1.url, testData1.processor, null);
                var res = engine.wasAlreadyEnqueued(testData1.url, testData1.processor);
                chai.expect(res).to.equal(true);
            });

            it('Should return false for same url and different processor', function () {
                var engine = new Engine();

                engine.enqueueUrl(testData1.url, testData1.processor, null);
                var res = engine.wasAlreadyEnqueued(testData3.url, testData3.processor);
                chai.expect(res).to.equal(false);
            });

            it('Should return false for different url and same processor', function () {
                var engine = new Engine();

                engine.enqueueUrl(testData1.url, testData1.processor, null);
                var res = engine.wasAlreadyEnqueued(testData2.url, testData2.processor);
                chai.expect(res).to.equal(false);
            });
        });
    });
}());

@@ -1191,28 +1191,30 @@
   , 
-expect = chai.expect
+path = require('path')
 ;%0A%0A 
@@ -3968,32 +3968,467 @@
 %0A            %7D);
+%0A%0A            it('Loads example processors', function(done) %7B%0A                var engine = new Engine();%0A                engine.loadProcessors(path.join(__dirname, '..', 'examples'))%0A                    .done(function(result)%7B%0A                        chai.expect(result.length).to.equal(7);%0A                        done();%0A                    %7D, function(err) %7B%0A                        throw err;%0A                    %7D)%0A            %7D);
 %0A        %7D);%0A%0A  
@@ -4974,14 +4974,12 @@
 it('
-Can be
+When
  cal
@@ -4994,17 +4994,25 @@
 out 
-arguments
+then throws error
 ', f

Test for loading of processors