"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const chai_1 = require("chai");
const StaticDiscovery_1 = require("./StaticDiscovery");
const decorators_1 = require("../decorators");
let TestService = class TestService {
};
TestService = __decorate([
    decorators_1.BaseRoute('/test'),
    decorators_1.ServiceName('test')
], TestService);
let FooService = class FooService {
    constructor(tags) {
        this.tags = tags;
    }
};
FooService = __decorate([
    decorators_1.BaseRoute('/foo'),
    decorators_1.ServiceName('foo'),
    __metadata("design:paramtypes", [Array])
], FooService);
describe("StaticDiscovery", () => {
    let discovery;
    const config = {
        sync: {
            default: "http://127.0.0.1:7800",
            alpha: "http://127.0.0.1:7801",
        },
        realms: "http://127.0.0.1:9081",
        default: "http://127.0.0.1:9080",
    };
    beforeEach(() => {
        discovery = new StaticDiscovery_1.StaticDiscovery({ config });
    });
    describe("registerService", () => {
        let fooMasterService;
        beforeEach(() => {
            fooMasterService = new FooService(['master']);
        });
        describe("with tags", () => __awaiter(this, void 0, void 0, function* () {
            beforeEach(() => __awaiter(this, void 0, void 0, function* () {
                yield discovery.registerService(fooMasterService, "192.168.0.1", 9080);
            }));
            it("does nothing", () => __awaiter(this, void 0, void 0, function* () {
                const handle = yield discovery.find("foo", ['master']);
                chai_1.assert.isDefined(handle);
                chai_1.assert.equal(handle.address, "127.0.0.1");
            }));
        }));
    });
    describe("find", () => {
        let mockService;
        beforeEach(() => __awaiter(this, void 0, void 0, function* () {
            mockService = new TestService();
        }));
        it("should find the TestService", () => __awaiter(this, void 0, void 0, function* () {
            const result = yield discovery.find("test");
            chai_1.assert.equal(result.name, "test");
            chai_1.assert.equal(`http://${result.address}:${result.port}/test`, "http://127.0.0.1:9080/test");
        }));
        describe("with sync", () => {
            it("should find the default sync service", () => __awaiter(this, void 0, void 0, function* () {
                const result = yield discovery.find("sync", ["label=default"]);
                chai_1.assert.equal(result.name, "sync");
                chai_1.assert.equal(`http://${result.address}:${result.port}`, config.sync.default);
            }));
        });
    });
    describe("findAll", () => {
        let mockService;
        beforeEach(() => __awaiter(this, void 0, void 0, function* () {
            mockService = new TestService();
        }));
        it("should find all realm services", () => __awaiter(this, void 0, void 0, function* () {
            const result = yield discovery.findAll("realms");
            chai_1.assert.isArray(result);
            chai_1.assert.equal(result.length, 1);
        }));
        describe("with sync", () => {
            it("should find all sync services", () => __awaiter(this, void 0, void 0, function* () {
                const result = yield discovery.findAll("sync");
                chai_1.assert.equal(result.length, 2);
                chai_1.assert.deepInclude(result, {
                    name: 'sync',
                    tags: ["label=default"],
                    address: '127.0.0.1',
                    port: 7800
                });
                chai_1.assert.deepInclude(result, {
                    name: 'sync',
                    tags: ["label=alpha"],
                    address: '127.0.0.1',
                    port: 7801
                });
            }));
        });
    });
    describe("unregisterService", () => {
        let mockService;
        beforeEach(() => __awaiter(this, void 0, void 0, function* () {
            mockService = new TestService();
            yield discovery.registerService(mockService, "127.0.0.1", 9080);
        }));
        it("does nothing", () => __awaiter(this, void 0, void 0, function* () {
            yield discovery.deregisterService(mockService);
            const result = yield discovery.findAll('test');
            chai_1.assert.isArray(result);
            chai_1.assert.equal(result.length, 1);
        }));
    });
});
//# sourceMappingURL=StaticDiscovery.spec.js.map