import StreamQueue from '../StreamQueue'
import { fakeKinesisClientCreator, fakeStreamConfigCreator } from './kinesisTests'

describe('StreamQueue', () => {
  const config = fakeStreamConfigCreator()
  const kinesis = fakeKinesisClientCreator()
  let subject = null

  beforeEach(() => {
    subject = new StreamQueue({ config, kinesis })
  })

  it('accepts new events', () => {
    expect(subject.push('EVENT', {})).to.be.true()
  })

  it('sends to kinesis when a page is full', () => {
    kinesis.putRecords = sinon.spy()
    expect(kinesis.putRecords).to.not.have.been.called()
    subject.push('EVENT', {})
    subject.push('EVENT', {})
    subject.push('EVENT', {})
    subject.push('EVENT', {})
    expect(kinesis.putRecords).to.not.have.been.called()
    subject.push('EVENT', {})
    // Page limit here is 5, don't expect an event until it is hit
    expect(kinesis.putRecords).to.have.been.called()
  })

  it('requires a known type of event', () => {
    // eslint-disable-next-line no-unused-expressions
    expect(() => subject.push('NOT_A_KNOWN_EVENT_TYPE', {})).to.throw()
  })

  context('page size of 1', () => {
    beforeEach(() => {
      subject.config = { ...config, maxPageSize: 1 }
    })

    it('sends events to kinesis immediately', () => {
      kinesis.putRecords = sinon.spy()
      subject.push('EVENT', {})
      subject.push('EVENT', {})
      expect(kinesis.putRecords).to.have.been.calledTwice()
    })
  })
})
