"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _fs = _interopRequireDefault(require("fs"));

var _chai = require("chai");

var _assert = _interopRequireDefault(require("assert"));

var _mocha = require("mocha");

var _nock = _interopRequireDefault(require("nock"));

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

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

var log = _LaunchDarklyLogger.LaunchDarklyLogger.logger();

(0, _mocha.describe)('LaunchDarklyUtilsFlags', function () {
  var ldutils;
  (0, _mocha.beforeEach)( /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
    return _regenerator["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);
  })));
  (0, _mocha.describe)('getFeatureFlags', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["default"])('https://app.launchdarkly.com').get('/api/v2/flags/sample-project').replyWithFile(200, __dirname + '/fixtures/feature-flags-list.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
      var expected;
      return _regenerator["default"].wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              expected = JSON.parse(_fs["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);
    })));
  });
  (0, _mocha.describe)('getFeatureFlag', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3() {
      var expected;
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              expected = JSON.parse(_fs["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);
    })));
  });
  (0, _mocha.describe)('getFeatureFlagState', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4() {
      var expected;
      return _regenerator["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) {
                _assert["default"].equal(actual, expected);
              }));

            case 2:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    })));
  });
  (0, _mocha.describe)('updateFeatureFlag', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5() {
      var expected;
      return _regenerator["default"].wrap(function _callee5$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              expected = JSON.parse(_fs["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);
    })));
  });
  (0, _mocha.describe)('toggleFeatureFlag', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6() {
      var expected;
      return _regenerator["default"].wrap(function _callee6$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              expected = JSON.parse(_fs["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);
    })));
  });
  (0, _mocha.describe)('migrateFeatureFlag', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7() {
      var expected;
      return _regenerator["default"].wrap(function _callee7$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              expected = JSON.parse(_fs["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);
    })));
  });
  (0, _mocha.describe)('bulkMigrateFeatureFlags', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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'
      }).get('/api/v2/flags/sample-project/sort.order2').replyWithFile(200, __dirname + '/fixtures/feature-flags-get-two.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/flags/sample-project/sort.order2').replyWithFile(200, __dirname + '/fixtures/feature-flags-update-two.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8() {
      var expected;
      return _regenerator["default"].wrap(function _callee8$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              expected = [];
              expected.push(JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/feature-flags-update.json', 'utf-8')));
              expected.push(JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/feature-flags-update-two.json', 'utf-8')));
              return _context8.abrupt("return", ldutils.flags.bulkMigrateFeatureFlags('sample-project', 'sort.order,sort.order2', 'test', 'production').then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 4:
            case "end":
              return _context8.stop();
          }
        }
      }, _callee8);
    })));
  });
  (0, _mocha.describe)('restoreFeatureFlags', function () {
    (0, _mocha.before)(function (done) {
      var scope = (0, _nock["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'
      }).get('/api/v2/flags/sample-project/sort.order2').replyWithFile(200, __dirname + '/fixtures/feature-flags-get-two.json', {
        'Content-Type': 'application/json'
      }).patch('/api/v2/flags/sample-project/sort.order2').replyWithFile(200, __dirname + '/fixtures/feature-flags-update-two.json', {
        'Content-Type': 'application/json'
      });
      (0, _assert["default"])(scope);
      done();
    });
    (0, _mocha.it)('should make expected api call and return results', /*#__PURE__*/(0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9() {
      var expected;
      return _regenerator["default"].wrap(function _callee9$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              expected = [];
              expected.push(JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/feature-flags-update.json', 'utf-8')));
              expected.push(JSON.parse(_fs["default"].readFileSync(__dirname + '/fixtures/feature-flags-update-two.json', 'utf-8')));
              return _context9.abrupt("return", ldutils.flags.restoreFeatureFlags('sample-project', 'sort.order,sort.order2', 'test', './test/fixtures/sampleBackup.json', true).then(function (actual) {
                (0, _chai.expect)(actual).to.deep.equal(expected);
              }));

            case 4:
            case "end":
              return _context9.stop();
          }
        }
      }, _callee9);
    })));
  });
});