/**
 * XadillaX created at 2016-06-06 21:40:48 With ♥
 *
 * Copyright (c) 2016 Souche.com, all rights
 * reserved.
 */
"use strict";

require("should");

var config = require("./config");

describe("#consumer", function() {
    var consumer = require("./common").consumer;
    var date = new Date();
    var DATE = "" + date.getUTCFullYear() + date.getUTCMonth() + date.getUTCDate();
    var ack = [];
    var message = [];
    var leftReconsume = true;

    this.timeout(0);

    function listener(_message, _ack) {
        // console.log(_message, _ack);
        // console.log(message, ack);
        message.push(_message);
        ack.push(_ack);

        if(ack.length >= 3) {
            _ack.done();
        }
    }

    it("should start consumer", function(done) {
        consumer.on("message", listener);
        consumer.init(function() {
            consumer.listen();
            done();
        });
    });

    it("should get one message", function(done) {
        function verify() {
            if(ack.length < 1) return setTimeout(verify, 500);

            var _message = message[0];
            var _ack = ack[0];

            // console.log(_message);

            _ack.done(false);

            _message.topic.should.be.eql(config.topic);
            _message.tag.should.be.eql("tagA");
            _message.key.should.be.eql("");
            _message.msgId.should.match(/^[0-9A-Z]{32}$/);
            _message.body.should.be.eql("Hello " + DATE);
            _message.reconsumeTimes.should.be.eql(0);
            _message.startDeliverTime.should.be.eql(0);

            done();
        }

        setTimeout(verify, 500);
    });

    it("should get another message", function(done) {
        function verify() {
            if(ack.length < 2) return setTimeout(verify, 500);

            var _message = message[1];
            var _ack = ack[1];

            // console.log(_message);

            _ack.done(true);

            _message.topic.should.be.eql(config.topic);
            _message.tag.should.be.eql("tagA");
            _message.key.should.be.eql("");
            _message.msgId.should.match(/^[0-9A-Z]{32}$/);

            if(_message.reconsumeTimes === 0) {
                _message.body.should.be.eql("World " + DATE);
                _message.reconsumeTimes.should.be.eql(0);
                _message.startDeliverTime.should.not.be.eql(0);
            } else {
                _message.body.should.be.eql("Hello " + DATE);
                _message.reconsumeTimes.should.be.eql(1);
                _message.startDeliverTime.should.be.eql(0);
                leftReconsume = false;
            }

            done();
        }

        setTimeout(verify, 500);
    });

    it("should reconsume", function(done) {
        function verify() {
            if(ack.length < 3) return setTimeout(verify, 500);

            var _message = message[2];
            var _ack = ack[2];

            // console.log(_message);

            _ack.done();

            _message.topic.should.be.eql(config.topic);
            _message.tag.should.be.eql("tagA");
            _message.key.should.be.eql("");
            _message.msgId.should.match(/^[0-9A-Z]{32}$/);

            if(leftReconsume) {
                _message.body.should.be.eql("Hello " + DATE);
                _message.reconsumeTimes.should.be.eql(1);
                _message.startDeliverTime.should.be.eql(0);
            } else {
                _message.body.should.be.eql("World " + DATE);
                _message.reconsumeTimes.should.be.eql(0);
                _message.startDeliverTime.should.not.be.eql(0);
            }

            done();
        }

        setTimeout(verify, 500);
    });
});

@@ -465,16 +465,19 @@
 timeout(
+100
 0);%0A%0A   
@@ -3687,13 +3687,271 @@
 %0A    %7D);
+%0A%0A    it(%22should use default options%22, function(done) %7B%0A        const consumer = new require(%22../%22).Consumer(config.consumerId, config.topic, %22*%22, config.accessKey, config.secretKey);%0A        consumer.stop(function() %7B%0A            done();%0A        %7D);%0A    %7D);
 %0A%7D);%0A

add a new test case for consumer with no option