<commit_before>import path from 'path'
import redis from 'redis-mock'
import expected from './mocks/expected.json'
import fixture from './mocks/message.json'
import rota from './mocks/rota.json'
import rsmq from './mocks/rsmq'

function alert () {
  const Alert = injectr('../../ingress/alert/index.js', {
    redis,
    '../../rota.json': rota
  }, {
    console,
    rsmq,
    path,
    __dirname: path.join(__dirname, '../')
  }).default

  return new Alert(rsmq)
}

describe('Alert', () => {

  describe('#process', () => {
    describe('less than three attempts', () => {
      let subject
      let next

      beforeEach(() => {
        subject = alert()
        sinon.spy(subject.rsmq, 'sendMessageAsync')
        next = sinon.stub()
      })

      it('should set meta data person correctly', async () => {
        await subject.process(fixture, next)
        assert.isNotNull(fixture.meta.person)
      })

      it('should send message for egress worker correctly', async () => {
        const expectedMessage = JSON.stringify({ body: expected.body, meta: expected.meta })

        await subject.process(fixture, next)
        assert.isTrue(rsmq.sendMessageAsync.calledWith({
          qname: 'phone_call',
          message: expectedMessage,
          delay: 0
        }))
      })

      it('should remove message from the queue', async () => {
        await subject.process(fixture, next)
        assert.isTrue(next.calledWith(null))
      })

      afterEach(() => {
        sinon.restore(subject.rsmq)
      })
    })

    describe('greater than or equal to three attempts', () => {
      let subject
      let next

      beforeEach(() => {
        subject = alert()
        next = sinon.stub()
        sinon.stub(subject.redisClient, 'getAsync').returns(3)
      })

      it('should increment meta index by 1', async () => {
        await subject.process(fixture, next)
        assert.equal(fixture.meta.index, 1)
      })

      it('should be next person in rota', async () => {
        await subject.process(fixture, next)
        assert.deepEqual(fixture.meta.person, rota[1])
      })

      afterEach(() => {
        fixture.meta.index = 0
        sinon.restore(subject.rsmq)
        sinon.restore(subject.redisClient)
      })
    })

    describe('ran out of people to call', () => {
      let subject
      let next

      beforeEach(() => {
        fixture.meta.index = 3
        subject = alert()
        next = sinon.stub()
      })

      it('should remove message from queue and stop', async () => {
        await subject.process(fixture, next)
        assert.isTrue(next.calledWith(null))
      })

      it('should not increment index further', async () => {
        await subject.process(fixture, next)
        assert.equal(fixture.meta.index, 3)
      })

      afterEach(() => {
        fixture.meta.index = 0
        sinon.restore(subject.rsmq)
        sinon.restore(subject.redisClient)
      })
    })
  })
})

<commit_msg>Fix linting issue in alert spec<commit_after>import path from 'path'
import redis from 'redis-mock'
import expected from './mocks/expected.json'
import fixture from './mocks/message.json'
import rota from './mocks/rota.json'
import rsmq from './mocks/rsmq'

function alert () {
  const Alert = injectr('../../ingress/alert/index.js', {
    redis,
    '../../rota.json': rota
  }, {
    console,
    rsmq,
    path,
    __dirname: path.join(__dirname, '../')
  }).default

  return new Alert(rsmq)
}

describe('Alert', () => {
  describe('#process', () => {
    describe('less than three attempts', () => {
      let subject
      let next

      beforeEach(() => {
        subject = alert()
        sinon.spy(subject.rsmq, 'sendMessageAsync')
        next = sinon.stub()
      })

      it('should set meta data person correctly', async () => {
        await subject.process(fixture, next)
        assert.isNotNull(fixture.meta.person)
      })

      it('should send message for egress worker correctly', async () => {
        const expectedMessage = JSON.stringify({ body: expected.body, meta: expected.meta })

        await subject.process(fixture, next)
        assert.isTrue(rsmq.sendMessageAsync.calledWith({
          qname: 'phone_call',
          message: expectedMessage,
          delay: 0
        }))
      })

      it('should remove message from the queue', async () => {
        await subject.process(fixture, next)
        assert.isTrue(next.calledWith(null))
      })

      afterEach(() => {
        sinon.restore(subject.rsmq)
      })
    })

    describe('greater than or equal to three attempts', () => {
      let subject
      let next

      beforeEach(() => {
        subject = alert()
        next = sinon.stub()
        sinon.stub(subject.redisClient, 'getAsync').returns(3)
      })

      it('should increment meta index by 1', async () => {
        await subject.process(fixture, next)
        assert.equal(fixture.meta.index, 1)
      })

      it('should be next person in rota', async () => {
        await subject.process(fixture, next)
        assert.deepEqual(fixture.meta.person, rota[1])
      })

      afterEach(() => {
        fixture.meta.index = 0
        sinon.restore(subject.rsmq)
        sinon.restore(subject.redisClient)
      })
    })

    describe('ran out of people to call', () => {
      let subject
      let next

      beforeEach(() => {
        fixture.meta.index = 3
        subject = alert()
        next = sinon.stub()
      })

      it('should remove message from queue and stop', async () => {
        await subject.process(fixture, next)
        assert.isTrue(next.calledWith(null))
      })

      it('should not increment index further', async () => {
        await subject.process(fixture, next)
        assert.equal(fixture.meta.index, 3)
      })

      afterEach(() => {
        fixture.meta.index = 0
        sinon.restore(subject.rsmq)
        sinon.restore(subject.redisClient)
      })
    })
  })
})


{'fraction_non_alphanumeric': 0.1420158169274777, 'fraction_numerical': 0.003028773346794548, 'mean_word_length': 2.1735184196476243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '243615', 'n_tokens_mistral': 1805, 'n_tokens_neox': 1758, 'n_words': 403}