"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

var _global = require("aws-sdk/global");

var _kinesis = _interopRequireDefault(require("aws-sdk/clients/kinesis"));

var _client = _interopRequireWildcard(require("../client"));

describe('kinesis client helper', function () {
  var fakeCreds = {
    access_key_id: 'id',
    expiration: 4000,
    secret_access_key: 'secret',
    session_token: 'token'
  };
  var fakeConfiguration = {
    stream_identifiers: {
      events: 'hi',
      responses: 'bye'
    },
    server_time: Date.now()
  };
  var fakeResponse = (0, _objectSpread2.default)({
    credentials: fakeCreds
  }, fakeConfiguration);
  var credentials = (0, _client.buildCredentials)(fakeCreds);
  describe('buildCredentials', function () {
    it('builds a credential', function () {
      expect(credentials).to.be.an.instanceof(_global.Credentials);
      expect(credentials.expireTime).to.eq(Date.parse(fakeCreds.expiration));
    });
  });
  describe('buildKinesisConfiguration', function () {
    context('creates global aws offset', function () {
      it('works with client time > server time', function () {
        var fakeClock = sinon.useFakeTimers();
        var configWithOffset = Object.assign({}, fakeConfiguration, {
          server_time: new Date(Date.now() - 1000)
        });
        (0, _client.buildKinesisConfiguration)({
          configuration: configWithOffset,
          credentials: credentials
        });
        expect(_global.config.systemClockOffset).to.eq(-1000);
        fakeClock.restore();
      });
      it('works with client time < server time', function () {
        var fakeClock = sinon.useFakeTimers();
        var configWithOffset = Object.assign({}, fakeConfiguration, {
          server_time: new Date(Date.now() + 1000)
        });
        (0, _client.buildKinesisConfiguration)({
          configuration: configWithOffset,
          credentials: credentials
        });
        expect(_global.config.systemClockOffset).to.be.eq(1000);
        fakeClock.restore();
      });
    });
    var kinesisConfig = (0, _client.buildKinesisConfiguration)({
      configuration: fakeConfiguration,
      credentials: credentials
    });
    it('returns a kinesisClient', function () {
      expect(kinesisConfig.client).to.be.an.instanceof(_kinesis.default);
    });
    it('returns some streamConfigs', function () {
      expect(kinesisConfig.streamConfigs.length).to.equal(2);
    });
  });
  describe('buildKinesisClient', function () {
    var client = (0, _client.buildKinesisClient)({}, credentials);
    it('builds a kinesis client', function () {
      expect(client).to.be.an.instanceof(_kinesis.default);
    });
  });
  describe('getKinesisConfig', function () {
    var fakeResolver = function fakeResolver(value) {
      return {
        then: function then(callback) {
          callback(value);
        }
      };
    };

    var fakeFetcher = {
      post: sinon.stub().returns(fakeResolver(fakeResponse))
    };
    it('calls for credentials for new data', function () {
      (0, _client.default)('1', fakeFetcher);
      expect(fakeFetcher.post.called).to.be.true();
    });
    it('returns a kinesis client', function (done) {
      var responsePromise = (0, _client.default)('1', fakeFetcher);
      responsePromise.then(function (config) {
        expect(config.client).to.be.an.instanceof(_kinesis.default);
        done();
      });
    });
  });
});