"use strict";
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 sinon = require("sinon");
const chai = require("chai");
chai.use(require("chai-as-promised"));
const HealthService_1 = require("../services/HealthService");
describe('HealthService', () => {
    let service;
    let statusCode;
    let response;
    let json;
    let send;
    let clientStub;
    beforeEach(() => __awaiter(this, void 0, void 0, function* () {
        clientStub = sinon.stub();
        service = new HealthService_1.HealthService();
        service['start']({
            healthClient: {
                hasServerStarted: clientStub
            },
            version: '1.2.3'
        });
        statusCode = 0;
        send = sinon.stub();
        json = sinon.stub();
        response = {
            status: function (code) {
                statusCode = code;
                return { send, json };
            },
        };
    }));
    describe('when other services are ready', () => {
        beforeEach(() => {
            clientStub.returns(Promise.resolve(true));
        });
        it("should return 503 initially", () => __awaiter(this, void 0, void 0, function* () {
            yield service['getHealth'](response);
            chai_1.assert.strictEqual(statusCode, 503);
        }));
        it("should switch to 200 after the server starts", () => __awaiter(this, void 0, void 0, function* () {
            service['live']();
            yield service['getHealth'](response);
            chai_1.assert.strictEqual(statusCode, 200);
            chai_1.assert.isDefined(json.getCall(0).args[0].version);
        }));
    });
    describe('when other services are not ready', () => __awaiter(this, void 0, void 0, function* () {
        beforeEach(() => {
            clientStub.returns(Promise.resolve(false));
            service['live']();
        });
        it('should return 503', () => __awaiter(this, void 0, void 0, function* () {
            yield service['getHealth'](response);
            chai_1.assert.strictEqual(statusCode, 503);
        }));
        it('should return 200 if invoked with thisInstance = true', () => __awaiter(this, void 0, void 0, function* () {
            yield service['getHealth'](response, 'true');
            chai_1.assert.strictEqual(statusCode, 200);
        }));
    }));
});
//# sourceMappingURL=HealthService.spec.js.map