'use strict';

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

require('source-map-support/register');

var _sinon = require('sinon');

var _sinon2 = _interopRequireDefault(_sinon);

var _mocha = require('mocha');

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _Executor = require('../../src/agent/Executor');

var _ExecutorLaunchDarkly = require('../../src/executors/ExecutorLaunchDarkly');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

(0, _mocha.describe)('ExecutorLaunchDarkly', function () {
    var executorLaunchDarkly = void 0;
    var eventData = void 0;

    (0, _mocha.beforeEach)(function () {
        eventData = {
            executor: 'LaunchDarkly',
            context: 'toggle features',
            options: {
                project: 'myProject',
                environment: 'dev',
                flags: {
                    'test-one': true,
                    'test-two': false
                }
            }
        };
        executorLaunchDarkly = new _ExecutorLaunchDarkly.ExecutorLaunchDarkly(eventData, console);
    });

    (0, _mocha.describe)('constructor', function () {
        (0, _mocha.it)('should instantiate as Executor', function () {
            _assert2.default.equal(executorLaunchDarkly instanceof _Executor.Executor, true);
        });
    });

    (0, _mocha.describe)('getLDUtils', function () {
        (0, _mocha.it)('should return a promise', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
            var ldUtils;
            return _regenerator2.default.wrap(function _callee$(_context) {
                while (1) {
                    switch (_context.prev = _context.next) {
                        case 0:
                            ldUtils = executorLaunchDarkly.getLDUtils();

                            _assert2.default.equal(ldUtils instanceof _promise2.default, true);

                        case 2:
                        case 'end':
                            return _context.stop();
                    }
                }
            }, _callee, undefined);
        })));
    });

    (0, _mocha.describe)('getFlagValue', function () {
        (0, _mocha.it)('should return a promise', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            var flagValue;
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            flagValue = executorLaunchDarkly.getFlagValue(eventData, 'test-one');

                            _assert2.default.equal(flagValue instanceof _promise2.default, true);

                        case 2:
                        case 'end':
                            return _context2.stop();
                    }
                }
            }, _callee2, undefined);
        })));
    });

    (0, _mocha.describe)('setFlagValue', function () {
        (0, _mocha.it)('should return a promise', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
            var flagValue;
            return _regenerator2.default.wrap(function _callee3$(_context3) {
                while (1) {
                    switch (_context3.prev = _context3.next) {
                        case 0:
                            flagValue = executorLaunchDarkly.setFlagValue(eventData, 'test-one', true);

                            _assert2.default.equal(flagValue instanceof _promise2.default, true);

                        case 2:
                        case 'end':
                            return _context3.stop();
                    }
                }
            }, _callee3, undefined);
        })));
    });

    (0, _mocha.describe)('getLDUtils', function () {
        (0, _mocha.it)('should return a promise', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
            var ldUtils;
            return _regenerator2.default.wrap(function _callee4$(_context4) {
                while (1) {
                    switch (_context4.prev = _context4.next) {
                        case 0:
                            ldUtils = executorLaunchDarkly.getLDUtils();

                            _assert2.default.equal(ldUtils instanceof _promise2.default, true);

                        case 2:
                        case 'end':
                            return _context4.stop();
                    }
                }
            }, _callee4, undefined);
        })));
    });

    (0, _mocha.describe)('executeTask', function () {
        var stubCall = void 0;
        var customResult = void 0;
        (0, _mocha.before)(function () {
            customResult = {
                passed: true,
                url: 'https://launchdarkly.com'
            };
            stubCall = _sinon2.default.stub(executorLaunchDarkly, 'executeTask').returns(_promise2.default.resolve(customResult));
        });

        (0, _mocha.it)('should invoke mocked ExecuteLaunchDarkly.executeTask', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() {
            var result;
            return _regenerator2.default.wrap(function _callee5$(_context5) {
                while (1) {
                    switch (_context5.prev = _context5.next) {
                        case 0:
                            _context5.next = 2;
                            return stubCall(eventData).then(function (data) {
                                return data;
                            });

                        case 2:
                            result = _context5.sent;

                            _assert2.default.equal(result, customResult);

                        case 4:
                        case 'end':
                            return _context5.stop();
                    }
                }
            }, _callee5, undefined);
        })));

        (0, _mocha.after)(function () {
            stubCall.restore();
        });
    });
});