"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
let assert = require('assert');
const redisMock_1 = require("../mocks/redisMock");
const mockObjects_1 = require("../mockObjects");
let config = {
    statusCheckInterval: 1,
    keys: {
        rediskey_connections: "test:"
    }
};
describe('test redis', function () {
    it('lock_sesion with lua', function (done) {
        let redis = mockObjects_1.default.makeRedis(config);
        let session = "1234";
        let free_session_list = "free_set";
        let used_session_key = "used_key";
        let lock_time = 200;
        redis.add_session(session, free_session_list, function (success, result) {
            assert.equal(success, true);
            redis.lock_session_lua(free_session_list, used_session_key, lock_time, function (success, result) {
                assert.equal(success, true);
                assert.equal(result, "1234"); //from redisMock.ts
                done();
            });
        });
    });
    it('lock_sesion with watch', function (done) {
        let redis = mockObjects_1.default.makeRedis(config);
        let session = "1234";
        let free_session_list = "free_set";
        let used_session_key = "used_key";
        let lock_time = 200;
        redis.add_session(session, free_session_list, function (success, result) {
            assert.equal(success, true);
            redis.lock_session_with_watch(free_session_list, used_session_key, lock_time, function (success, result) {
                assert.equal(success, true);
                assert.equal(result, "1234"); //from redisMock.ts
                done();
            });
        });
    });
    it('lock_sesion', function (done) {
        let redis = mockObjects_1.default.makeRedis(config);
        let session = "1234";
        let free_session_list = "free_set";
        let used_session_key = "used_key";
        let lock_time = 200;
        redis.add_session(session, free_session_list, function (success, result) {
            assert.equal(success, true);
            redis.lock_session(free_session_list, used_session_key, lock_time, function (success, result) {
                assert.equal(success, true);
                assert.equal(result, "1234"); //from redisMock.ts
                done();
            });
        });
    });
    it('create destroy', function (done) {
        class SpecialCase extends redisMock_1.default {
            quit(callback) {
                done();
                return super.quit();
            }
        }
        let redis = mockObjects_1.default.makeRedis(config, SpecialCase);
        redis.destroy();
    });
    it('test functions: set, get, delete', function (done) {
        let redis = mockObjects_1.default.makeRedis(config);
        redis.close();
        redis.set("unit_test", "unit_test_re");
        redis.get("unit_test", function (isSucces, result) {
            assert.equal(isSucces, true);
            assert.equal(result, "unit_test_re");
        });
        redis.delete("unit_test");
        redis.get("unit_test", function (isSucces, result) {
            assert.equal(isSucces, false);
            assert.notEqual(result, null);
            assert.equal(result.message, "Undefined value");
            done();
        });
    });
    it('status change', function (done) {
        let getStatus = function () {
            return "default";
        };
        let destroy = function () { };
        class SpecialCase extends redisMock_1.default {
            constructor() {
                super(...arguments);
                this.i = 0;
            }
            quit(callback) {
                done();
                return super.quit();
            }
            set(key, value, ...args) {
                let callback = args[0];
                let status = getStatus();
                this.i += 1;
                if (status == "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 (typeof callback === "function")
                            callback(null, null);
                        break;
                    case 2:
                        assert.equal(getStatus(), "connected");
                        if (typeof callback === "function")
                            callback(new Error(), null);
                        break;
                    case 3:
                        assert.equal(getStatus(), "disconnected");
                        destroy();
                        break;
                }
            }
        }
        let redis = mockObjects_1.default.makeRedis(config, SpecialCase);
        getStatus = function () { return redis.getStatus(); };
        destroy = function () { redis.destroy(); };
    });
    it('test hset, hget', function (done) {
        class SpecialCase extends redisMock_1.default {
            constructor() {
                super(...arguments);
                this.memory = {};
            }
            hset(key, field, value, callback) {
                assert.equal(key, "key");
                this.memory[field] = value;
                if (callback != undefined)
                    callback(null, 1);
                return super.hset(key, field, value);
            }
            hget(key, field, callback) {
                assert.equal(key, "key");
                let val = this.memory[field];
                if (val != null) {
                    if (callback != undefined)
                        callback(null, val);
                }
                else {
                    if (callback != undefined)
                        callback(new Error("error"), null);
                }
                return super.hget(key, field);
            }
            hgetall(key, callback) {
                assert.equal(key, "key");
                if (callback != undefined)
                    callback(null, this.memory);
                return super.hgetall(key);
            }
            ;
            hdel(key, field, ...fields) {
                let neededField = "";
                if (field == undefined) {
                    done("expected field");
                }
                else {
                    neededField = field;
                }
                let callback = fields[0];
                assert.equal(key, "key");
                let val = this.memory[neededField];
                if (val != null) {
                    delete this.memory[neededField];
                    if (callback != undefined)
                        callback(null, 1);
                }
                else {
                    if (callback != undefined)
                        callback(new Error("error"), null);
                }
            }
            ;
        }
        let redis = mockObjects_1.default.makeRedis(config, SpecialCase);
        redis.hset("key", "label", "value", function (isSucces, response) {
            assert.equal(isSucces, true);
            assert.equal(response, 1);
            redis.hset("key", "label2", "value2", function (isSucces, response) {
                assert.equal(isSucces, true);
                assert.equal(response, 1);
                redis.hget("key", "label", function (isSucces, response) {
                    assert.equal(isSucces, true);
                    assert.equal(response, "value");
                    redis.hgetall("key", function (isSucces, response) {
                        assert.equal(isSucces, true);
                        assert.equal(JSON.stringify(response), JSON.stringify({ label: "value", label2: "value2" }));
                        redis.hdel("key", "label", function (isSucces, response) {
                            assert.equal(isSucces, true);
                            assert.equal(response, 1);
                            redis.hget("key", "label", function (isSucces, response) {
                                assert.equal(isSucces, false);
                                if (response instanceof Error) {
                                    assert.equal(response.message, "error");
                                }
                                else {
                                    done("expected Error");
                                }
                                done();
                            });
                        });
                    });
                });
            });
        });
    });
    it('test hset, hget (error case)', function (done) {
        class SpecialCase extends redisMock_1.default {
            constructor() {
                super(...arguments);
                this.memory = {};
            }
            hset(key, field, value, callback) {
                assert.equal(key, "key");
                assert.equal(field, "label");
                assert.equal(value, "value");
                this.memory[field] = value;
                if (callback != undefined)
                    callback(new Error("error"), null);
                return super.hset(key, field, value);
            }
            hget(key, field, callback) {
                assert.equal(key, "key");
                assert.equal(field, "label");
                if (callback != undefined)
                    callback(new Error("error"), null);
                return super.hget(key, field);
            }
            hgetall(key, callback) {
                assert.equal(key, "key");
                if (callback != undefined)
                    callback(new Error("error"), null);
                return super.hgetall(key);
            }
            ;
            hdel(key, field, ...fields) {
                let callback = fields[0];
                assert.equal(key, "key");
                assert.equal(field, "label");
                if (callback != undefined)
                    callback(new Error("error"), null);
            }
            ;
        }
        let redis = mockObjects_1.default.makeRedis(config, SpecialCase);
        redis.hset("key", "label", "value", function (isSucces, response) {
            assert.equal(isSucces, false);
            if (response instanceof Error) {
                assert.equal(response.message, "error");
            }
            else {
                done("expected Error");
            }
            redis.hget("key", "label", function (isSucces, response) {
                assert.equal(isSucces, false);
                if (response instanceof Error) {
                    assert.equal(response.message, "error");
                }
                else {
                    done("expected Error");
                }
                redis.hgetall("key", function (isSucces, response) {
                    assert.equal(isSucces, false);
                    if (response instanceof Error) {
                        assert.equal(response.message, "error");
                    }
                    else {
                        done("expected Error");
                    }
                    redis.hdel("key", "label", function (isSucces, response) {
                        assert.equal(isSucces, false);
                        if (response instanceof Error) {
                            assert.equal(response.message, "error");
                        }
                        else {
                            done("expected Error");
                        }
                        done();
                    });
                });
            });
        });
    });
});
//# sourceMappingURL=test-redis.js.map