"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Nock = require("nock");
const Assert = require("assert");
let base = require("../index");
describe('consul', function () {
    it("regular case", function (done) {
        let response = JSON.stringify([{
                "ID": "q62zdtr-6824-3e12-1bc4-yfdak9j34kdc",
                "Node": "localhost",
                "Address": "127.0.0.1",
                "Datacenter": "dc1",
                "TaggedAddresses": {
                    "lan": "127.0.0.1",
                    "wan": "127.0.0.1"
                },
                "NodeMeta": {
                    "consul-network-segment": ""
                },
                "ServiceKind": "",
                "ServiceID": "localhost:service-name_service.4.zzq9agra55xwjycw7edsdpv67:8500",
                "ServiceName": "service-name",
                "ServiceTags": [
                    "tag1",
                    "tag2",
                    "tag3"
                ],
                "ServiceAddress": "127.0.0.1",
                "ServiceMeta": {},
                "ServicePort": 8500,
                "ServiceEnableTagOverride": false,
                "ServiceProxyDestination": "",
                "ServiceConnect": {
                    "Native": false,
                    "Proxy": null
                },
                "CreateIndex": 86,
                "ModifyIndex": 86
            }]);
        let nock = Nock('http://localhost:8500')
            .filteringPath(function (path) {
            if (path.substring(0, 32) == "/v1/catalog/service/service-name") {
                return '/v1/catalog/service/service-name';
            }
            return "/";
        })
            .get("/v1/catalog/service/service-name")
            .reply(200, function (uri, requestBody) {
            return response;
        }, {
            "Content-Encoding": "gzip",
            "Content-Length": "540",
            "Content-Type": "application/json",
            "Date": "Fri, 20 Jul 2018 14:49:19 GMT",
            "Vary": "Accept-Encoding",
            "X-Consul-Effective-Consistency": "leader",
            "X-Consul-Index": "569341",
            "X-Consul-Knownleader": "true",
            "X-Consul-Lastcontact": "0"
        });
        let observer = base.consul().newObserver("service-name");
        observer.setListener(function (list, error) {
            Assert.equal(error, null);
            Assert.notEqual(list, null);
            Assert.equal(list.length, 1);
            let secondlist = observer.getServices();
            Assert.notEqual(secondlist, null);
            Assert.equal(secondlist.length, 1);
            base.consul().deleteObserver("service-name");
            let secondlist2 = observer.getServices();
            Assert.notEqual(secondlist2, null);
            Assert.equal(secondlist2.length, 0);
            let secondError = observer.getError();
            Assert.equal(secondError, null);
            done();
        });
    });
    it("error case", function (done) {
        let nock = Nock('http://localhost:8500')
            .filteringPath(function (path) {
            if (path.substring(0, 35) == "/v1/catalog/service/invalid-service") {
                return '/v1/catalog/service/invalid-service';
            }
            return "/";
        })
            .get("/v1/catalog/service/invalid-service")
            .reply(200, function (uri, requestBody) {
            return "";
        });
        let observer = base.consul().newObserver("invalid-service");
        observer.setListener(function (list, error) {
            Assert.notEqual(list, null);
            Assert.equal(list.length, 0);
            Assert.notEqual(error, null);
            Assert.equal(error.message, "Watch not supported");
            let secondlist = observer.getServices();
            Assert.notEqual(secondlist, null);
            Assert.equal(secondlist.length, 0);
            let secondError = observer.getError();
            Assert.notEqual(secondError, null);
            Assert.equal(secondError.message, "Watch not supported");
            done();
        });
    });
});
//# sourceMappingURL=test-consul.js.map