<commit_before>const expect = require('expect')
const Context = require('../lib/context')
const createRobot = require('../lib/robot')

describe('Robot', function () {
  let robot
  let event
  let event2
  let spy

  beforeEach(function () {
    robot = createRobot()
    robot.auth = () => {}

    event = {
      event: 'test',
      payload: {
        action: 'foo',
        installation: {id: 1}
      }
    }

    event2 = {
      event: 'arrayTest',
      payload: {
        action: 'bar',
        installation: {id: 2}
      }
    }

    spy = expect.createSpy()
  })

  describe('constructor', () => {
    it('takes a logger', () => {
      const logger = {
        trace: expect.createSpy(),
        debug: expect.createSpy(),
        info: expect.createSpy(),
        warn: expect.createSpy(),
        error: expect.createSpy(),
        fatal: expect.createSpy()
      }
      robot = createRobot({logger})

      robot.log('hello world')
      expect(logger.debug).toHaveBeenCalledWith('hello world')
    })
  })

  describe('on', function () {
    it('calls callback when no action is specified', async function () {
      robot.on('test', spy)

      expect(spy).toNotHaveBeenCalled()
      await robot.receive(event)
      expect(spy).toHaveBeenCalled()
      expect(spy.calls[0].arguments[0]).toBeA(Context)
      expect(spy.calls[0].arguments[0].payload).toBe(event.payload)
    })

    it('(context, event) will be removed in 0.10', () => {
      // This test will fail in version 0.10 to remind us to
      // remove the deprecated (context, event)
      const semver = require('semver')
      const pkg = require('../package')
      expect(semver.satisfies(pkg.version, '< 0.10')).toBe(true)
    })

    it('calls callback with same action', async function () {
      robot.on('test.foo', spy)

      await robot.receive(event)
      expect(spy).toHaveBeenCalled()
    })

    it('does not call callback with different action', async function () {
      robot.on('test.nope', spy)

      await robot.receive(event)
      expect(spy).toNotHaveBeenCalled()
    })

    it('calls callback with *', async function () {
      robot.on('*', spy)

      await robot.receive(event)
      expect(spy).toHaveBeenCalled()
    })

    it('calls callback x amount of times when an array of x actions is passed', async function () {
      robot.on(['test.foo', 'arrayTest.bar'], spy)

      await robot.receive(event)
      await robot.receive(event2)
      expect(spy.calls.length).toEqual(2)
    })
  })

  describe('receive', () => {
    it('delivers the event', async () => {
      const spy = expect.createSpy()
      robot.on('test', spy)

      await robot.receive(event)

      expect(spy).toHaveBeenCalled()
    })

    it('waits for async events to resolve', async () => {<commit_msg>Move event2 fixture into test<commit_after>const expect = require('expect')
const Context = require('../lib/context')
const createRobot = require('../lib/robot')

describe('Robot', function () {
  let robot
  let event
  let spy

  beforeEach(function () {
    robot = createRobot()
    robot.auth = () => {}

    event = {
      event: 'test',
      payload: {
        action: 'foo',
        installation: {id: 1}
      }
    }

    spy = expect.createSpy()
  })

  describe('constructor', () => {
    it('takes a logger', () => {
      const logger = {
        trace: expect.createSpy(),
        debug: expect.createSpy(),
        info: expect.createSpy(),
        warn: expect.createSpy(),
        error: expect.createSpy(),
        fatal: expect.createSpy()
      }
      robot = createRobot({logger})

      robot.log('hello world')
      expect(logger.debug).toHaveBeenCalledWith('hello world')
    })
  })

  describe('on', function () {
    it('calls callback when no action is specified', async function () {
      robot.on('test', spy)

      expect(spy).toNotHaveBeenCalled()
      await robot.receive(event)
      expect(spy).toHaveBeenCalled()
      expect(spy.calls[0].arguments[0]).toBeA(Context)
      expect(spy.calls[0].arguments[0].payload).toBe(event.payload)
    })

    it('(context, event) will be removed in 0.10', () => {
      // This test will fail in version 0.10 to remind us to
      // remove the deprecated (context, event)
      const semver = require('semver')
      const pkg = require('../package')
      expect(semver.satisfies(pkg.version, '< 0.10')).toBe(true)
    })

    it('calls callback with same action', async function () {
      robot.on('test.foo', spy)

      await robot.receive(event)
      expect(spy).toHaveBeenCalled()
    })

    it('does not call callback with different action', async function () {
      robot.on('test.nope', spy)

      await robot.receive(event)
      expect(spy).toNotHaveBeenCalled()
    })

    it('calls callback with *', async function () {
      robot.on('*', spy)

      await robot.receive(event)
      expect(spy).toHaveBeenCalled()
    })

    it('calls callback x amount of times when an array of x actions is passed', async function () {
      const event2 = {
        event: 'arrayTest',
        payload: {
          action: 'bar',
          installation: {id: 2}
        }
      }

      robot.on(['test.foo', 'arrayTest.bar'], spy)

      await robot.receive(event)
      await robot.receive(event2)
      expect(spy.calls.length).toEqual(2)
    })
  })

  describe('receive', () => {
    it('delivers the event', async () => {
      const spy = expect.createSpy()
      robot.on('test', spy)

      await robot.receive(event)

      expect(spy).toHaveBeenCalled()
    })

    it('waits for async events to resolve', async () => {
{'fraction_non_alphanumeric': 0.15228607009428927, 'fraction_numerical': 0.006760362924746487, 'mean_word_length': 2.51375, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '6762668', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1757, 'n_words': 397}