"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
/**
 *  Tests for Indication Controller
 *
 *  Copyright (C) 2010-2020 R-T Specialty, LLC.
 *
 *  This file is part of the Liza Data Collection Framework.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
var sinon = require("sinon");
var events_1 = require("events");
var HttpClient_1 = require("../../../src/system/network/HttpClient");
var chai_1 = require("chai");
var IndicationController_1 = require("../../../src/dullahan/controllers/IndicationController");
var sinon_express_mock_1 = require("sinon-express-mock");
describe('IndicationController', function () {
    describe('create', function () {
        it('fails when an invalid webhook is provided', function () {
            var sut = createSutCreate();
            var req = sinon_express_mock_1.mockReq({});
            var res = sinon_express_mock_1.mockRes({});
            chai_1.expect(function () { return sut(req, res); }).to.throw(Error, 'The provided URL is invalid:');
            chai_1.expect(res.status.withArgs(422).callCount).to.equal(1);
        });
        it('calls webhook when a valid request is processed', function () {
            var http_client = createHttpClient();
            var given = 'https://some.websitethatdoesnotexist.joe';
            sinon.stub(http_client, 'post').callsFake(function (url, _payload, _options) {
                chai_1.expect(url).to.be.equal(given);
                return Promise.resolve({});
            });
            var sut = createSutCreate({ http_client: http_client });
            var req = sinon_express_mock_1.mockReq({});
            var res = sinon_express_mock_1.mockRes({});
            req.query.callback = given;
            sut(req, res);
            chai_1.expect(res.status.withArgs(202).callCount).to.equal(1);
        });
        it('decodes and calls webhook when a valid request is processsed', function () {
            var http_client = createHttpClient();
            var given = 'https%3A%2F%2Fsome.websitethatdoesnotexist.joe';
            sinon.stub(http_client, 'post').callsFake(function (url, _payload, _options) {
                chai_1.expect(url).to.be.equal('https://some.websitethatdoesnotexist.joe');
                return Promise.resolve({});
            });
            var sut = createSutCreate({ http_client: http_client });
            var req = sinon_express_mock_1.mockReq({});
            var res = sinon_express_mock_1.mockRes({});
            req.query.callback = given;
            sut(req, res);
            chai_1.expect(res.status.withArgs(202).callCount).to.equal(1);
        });
    });
});
var createEventEmitter = function () {
    return new (/** @class */ (function (_super) {
        __extends(class_1, _super);
        function class_1() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        return class_1;
    }(events_1.EventEmitter)))();
};
var createHttpClient = function () {
    return new (/** @class */ (function (_super) {
        __extends(class_2, _super);
        function class_2() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        return class_2;
    }(HttpClient_1.HttpClient)))();
};
var createProgramFactory = function () {
    return {
        createProgram: function () {
            return {
                bucket: {
                    getData: function () {
                        return {};
                    },
                },
                program: {
                    classify: function (_data) { },
                },
            };
        },
    };
};
var createRaterFactory = function () {
    return {
        createRater: function () {
            return ({
                rate: function () { },
            });
        },
    };
};
var createSutCreate = function (dependencies) {
    var _a, _b, _c, _d;
    if (dependencies === void 0) { dependencies = {}; }
    var emitter = dependencies.emitter, http_client = dependencies.http_client, program_factory = dependencies.program_factory, rater_factory = dependencies.rater_factory;
    return IndicationController_1.indication.create((_a = emitter) !== null && _a !== void 0 ? _a : createEventEmitter())((_b = http_client) !== null && _b !== void 0 ? _b : createHttpClient())((_c = program_factory) !== null && _c !== void 0 ? _c : createProgramFactory())((_d = rater_factory) !== null && _d !== void 0 ? _d : createRaterFactory());
};
//# sourceMappingURL=data:application/json;base64, 