text: /**
   * Copyright (c) 2016 - 2018 Sqreen. All Rights Reserved.
   * Please refer to our terms for more information: https://www.sqreen.io/terms.html
   */
  'use strict';
  const Code = require('code');
  const Lab = require('lab');
  const lab = exports.lab = Lab.script();
  
  const describe = lab.describe;
  const it = lab.it;
  const expect = Code.expect;
  
  const Record = require('../../../lib/instrumentation/record');
  
  const Proxyquire = require('proxyquire');
  
  describe('Event.queue', () => {
  
      describe('writeEvent', () => {
  
          it('should report a simple event', { plan: 2 }, (done) => {
  
              const MockedEvent = Proxyquire('../../../lib/events/index', {
                  './action': {
                      trigger: function (queue) {
  
                          expect(queue).to.be.an.array();
                          expect(queue).to.have.length(1);
                          done();
                      }
                  }
              });
              MockedEvent.writeEvent('a', {});
          });
  
          it('should report a simple event', { plan: 3 }, (done) => {
  
              const MockedEvent = Proxyquire('../../../lib/events/index', {
                  './action': {
                      trigger: function (queue, f) {
  
                          expect(queue).to.be.an.array();
                          expect(queue).to.have.length(1);
                          expect(f).to.be.true();
  
                          done();
                      }
                  }
              });
              MockedEvent.writeEvent('ATTACK', { rule_name: 'a' });
          });
  
          it('should report a simple event', { plan: 2 }, (done) => {
  
              const MockedEvent = Proxyquire('../../../lib/events/index', {
                  './action': {
                      trigger: function (queue) {
  
                          expect(queue).to.be.an.array();
                          expect(queue).to.have.length(1);
                          done();
                      }
                  }
              });
              MockedEvent.writeEvent('ATTACK', { rule_name: 'a' });
          });
  
          it('should report a simple event', { plan: 2 }, (done) => {
  
              const MockedEvent = Proxyquire('../../../lib/events/index', {
                  './action': {
                      trigger: function (queue) {
  
                          expect(queue).to.be.an.array();
                          expect(queue).to.have.length(1);
                          done();
                      }
                  }
              });
              MockedEvent.writeEvent('ATTACK', { klass: 'a' });
          });
  
          it('should report a simple event', { plan: 2 }, (done) => {
  
              const MockedEvent = Proxyquire('../../../lib/events/index', {
                  './action': {
                      trigger: function (queue) {
  
                          expect(queue).to.be.an.array();
                          expect(queue).to.have.length(1);
                          done();
                      }
                  }
              });
              MockedEvent.writeEvent('ATTACK', { klass: 'a' });
          });
  
          it('should report a request_record', { plan: 12 }, (done) => {
  
              const record = new Record.Record({
                  __sqreen_uuid: 1
              });
  
              record.attack({
                  rule_name: 'my-record-rule'
              });
  
              const record2 = new Record.Record({
                  __sqreen_uuid: 1
              });
  
              record2.attack({
                  rule_name: 'my-record-rule'
              });
  
              const record3 = new Record.Record({
                  __sqreen_uuid: 1
              });
  
              record3.except({
                  rule_name: 'my-record-rule'
              });
  
              const record4 = new Record.Record({
                  __sqreen_uuid: 1
              });
  
              record4.except({
                  rule_name: 'my-record-rule'
              });
  
              let count = 0;
              const MockedEvent = Proxyquire('../../../lib/events/index', {
                  './action': {
                      trigger: function (queue, force) {
  
                          expect(queue).to.be.an.array();
                          expect(queue).to.have.length(1 + count);
                          if (count === 0) {
                              expect(force).to.be.true();
                          }
                          else if (count === 1) {
                              expect(force).to.be.false();
                          }
                          else if (count === 2) {
                              expect(force).to.be.true();
                          }
                          else {
                              expect(force).to.be.false();
                              done();
                          }
  
                          count++;
                      }
                  }
              });
              MockedEvent.writeEvent('request_record', record);
              MockedEvent.writeEvent('request_record', record2);
              MockedEvent.writeEvent('request_record', record3);
              MockedEvent.writeEvent('request_record', record4);
          });
  
  
          it('should drop first events when the queue is too long', { plan: 4 }, (done) => {
  
              const MockedEvent = Proxyquire('../../../lib/events/index', {
                  './action': {
                      trigger: function (queue) { }
                  }
              });
  
              for (let i = 0; i < 500; ++i) {
  
                  MockedEvent._eventQueue.push({ type: i, event: i });
              }
  
              expect(MockedEvent._eventQueue).to.have.length(500);
              MockedEvent.writeEvent('a', {})
                  .then(() => {
  
                      expect(MockedEvent._eventQueue).to.have.length(500);
                      expect(MockedEvent._eventQueue.pop()).to.equal({ type: 'a', event: {} });
                      expect(MockedEvent._eventQueue.shift()).to.equal({ type: 1, event: 1 });
  
                      done();
                  });
          });
      });
  });
  