let assert = require('assert');
import Elastic from '../elastic';
import Utils from '../utils';
import ElasticMock from "../mocks/elasticMock";
import MockObjects from '../mockObjects';
import * as Elasticsearch from "elasticsearch";

let config = {
    statusCheckInterval: 1,
    autoConnect: true
};


describe('test elastic', function () {

    it('create destroy', function (done) {
        class SpecialCase extends ElasticMock {
            public close(){
                done();
            }
        }

        let elastic = MockObjects.makeElastic(config, SpecialCase);
        elastic.destroy();
    });

    it('status change', function (done) {
        let getStatus = function(){
            return "default";
        }
        let destroy = function(){}

        class SpecialCase extends ElasticMock {
            private i = 0;

            public close(){
                done();
            }

            public ping(params: Elasticsearch.PingParams, callback?: (err: any, response: any, status: any) => void): Promise<any> {
                this.i += 1;
                if(getStatus() == "default") this.i = 1;//Start to increment just after chenge getStatus function
                switch(this.i){
                    case 1:
                        //We cant know if is first or secand call
                        if(callback != null) callback(null, null, null);
                        break;
                    case 2:
                        assert.equal(getStatus(), "connected");
                        if(callback != null) callback(new Error(), null, null);
                        break;
                    case 3:
                        assert.equal(getStatus(), "error");              
                        destroy();                    
                        break;
                }

                return super.ping(params);
            }
        }

        let elastic = MockObjects.makeElastic(config, SpecialCase);
        getStatus = function() { return elastic.getStatus(); };
        destroy = function() { elastic.destroy(); };
    });
});