'use strict';

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

var _regenerator2 = _interopRequireDefault(_regenerator);

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

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _fs = require('fs');

var _fs2 = _interopRequireDefault(_fs);

var _chai = require('chai');

var _assert = require('assert');

var _assert2 = _interopRequireDefault(_assert);

var _mocha = require('mocha');

var _nock = require('nock');

var _nock2 = _interopRequireDefault(_nock);

var _LaunchDarklyUtils = require('../src/LaunchDarklyUtils');

var _LaunchDarklyLogger = require('../src/LaunchDarklyLogger');

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

var log = _LaunchDarklyLogger.LaunchDarklyLogger.logger();

(0, _mocha.describe)('LaunchDarklyUtilsFlags', function () {
    var _this = this;

    var ldutils = void 0;
    (0, _mocha.beforeEach)((0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee() {
        return _regenerator2.default.wrap(function _callee$(_context) {
            while (1) {
                switch (_context.prev = _context.next) {
                    case 0:
                        _context.next = 2;
                        return new _LaunchDarklyUtils.LaunchDarklyUtils().create('MOCK', log);

                    case 2:
                        ldutils = _context.sent;

                    case 3:
                    case 'end':
                        return _context.stop();
                }
            }
        }, _callee, _this);
    })));

    (0, _mocha.describe)('getFeatureFlags', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/flags/sample-project').replyWithFile(200, __dirname + '/fixtures/feature-flags-list.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee2() {
            var expected;
            return _regenerator2.default.wrap(function _callee2$(_context2) {
                while (1) {
                    switch (_context2.prev = _context2.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/feature-flags-list.json', 'utf-8'));
                            return _context2.abrupt('return', ldutils.flags.getFeatureFlags('sample-project').then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

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

    (0, _mocha.describe)('getFeatureFlag', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/flags/sample-project/sort.order').replyWithFile(200, __dirname + '/fixtures/feature-flags-get.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee3() {
            var expected;
            return _regenerator2.default.wrap(function _callee3$(_context3) {
                while (1) {
                    switch (_context3.prev = _context3.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/feature-flags-get.json', 'utf-8'));
                            return _context3.abrupt('return', ldutils.flags.getFeatureFlag('sample-project', 'sort.order').then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

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

    (0, _mocha.describe)('getFeatureFlagState', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/flags/sample-project/sort.order?env=test').replyWithFile(200, __dirname + '/fixtures/feature-flags-get.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee4() {
            var expected;
            return _regenerator2.default.wrap(function _callee4$(_context4) {
                while (1) {
                    switch (_context4.prev = _context4.next) {
                        case 0:
                            expected = true;
                            return _context4.abrupt('return', ldutils.flags.getFeatureFlagState('sample-project', 'sort.order', 'test').then(function (actual) {
                                _assert2.default.equal(actual, expected);
                            }));

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

    (0, _mocha.describe)('updateFeatureFlag', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').patch('/api/v2/flags/sample-project/sort.order').replyWithFile(200, __dirname + '/fixtures/feature-flags-update.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee5() {
            var expected;
            return _regenerator2.default.wrap(function _callee5$(_context5) {
                while (1) {
                    switch (_context5.prev = _context5.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/feature-flags-update.json', 'utf-8'));
                            return _context5.abrupt('return', ldutils.flags.updateFeatureFlag('sample-project', 'sort.order', [{ op: 'replace', path: '/environments/production/on', value: false }]).then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

                        case 2:
                        case 'end':
                            return _context5.stop();
                    }
                }
            }, _callee5, this);
        })));
    });

    (0, _mocha.describe)('toggleFeatureFlag', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').patch('/api/v2/flags/sample-project/sort.order').replyWithFile(200, __dirname + '/fixtures/feature-flags-update.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee6() {
            var expected;
            return _regenerator2.default.wrap(function _callee6$(_context6) {
                while (1) {
                    switch (_context6.prev = _context6.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/feature-flags-update.json', 'utf-8'));
                            return _context6.abrupt('return', ldutils.flags.toggleFeatureFlag('sample-project', 'sort.order', 'test', true).then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

                        case 2:
                        case 'end':
                            return _context6.stop();
                    }
                }
            }, _callee6, this);
        })));
    });

    (0, _mocha.describe)('migrateFeatureFlag', function () {
        (0, _mocha.before)(function (done) {
            var scope = (0, _nock2.default)('https://app.launchdarkly.com').get('/api/v2/flags/sample-project/sort.order').replyWithFile(200, __dirname + '/fixtures/feature-flags-get.json', {
                'Content-Type': 'application/json'
            }).patch('/api/v2/flags/sample-project/sort.order').replyWithFile(200, __dirname + '/fixtures/feature-flags-update.json', {
                'Content-Type': 'application/json'
            });
            (0, _assert2.default)(scope);
            done();
        });

        (0, _mocha.it)('should make expected api call and return results', (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee7() {
            var expected;
            return _regenerator2.default.wrap(function _callee7$(_context7) {
                while (1) {
                    switch (_context7.prev = _context7.next) {
                        case 0:
                            expected = JSON.parse(_fs2.default.readFileSync(__dirname + '/fixtures/feature-flags-update.json', 'utf-8'));
                            return _context7.abrupt('return', ldutils.flags.migrateFeatureFlag('sample-project', 'sort.order', 'test', 'production').then(function (actual) {
                                (0, _chai.expect)(actual).to.deep.equal(expected);
                            }));

                        case 2:
                        case 'end':
                            return _context7.stop();
                    }
                }
            }, _callee7, this);
        })));
    });
});