<commit_before>import test from 'tape'

import plugin from '../src'

test('plugin: export', (t) => {
  t.equal(
    typeof plugin,
    'function',
    'must be a function'
  )

  t.end()
})<commit_msg>👾 plugin: add tests<commit_after>import EventEmitter from 'events'
import test from 'tape'
import { spy, stub } from 'sinon'

import plugin from '../src'

const files = [
  {
    path: 'foo',
    data: 'bar',
    map: null
  }
]

test('plugin: export', (t) => {
  t.equal(
    typeof plugin,
    'function',
    'must be a function'
  )

  t.end()
})

test('plugin: props', async (t) => {
  const name = 'testName'
  const pluginFn = stub().returns(files)
  const reporter = new EventEmitter()
  const beforeProps = { files, reporter }

  await plugin(name, pluginFn)(beforeProps)

  t.ok(
    pluginFn.calledOnce,
    'plugin function should be called once'
  )

  const afterProps = pluginFn.firstCall.args[0]

  t.deepEqual(
    afterProps.files,
    files,
    '`files` should be passed through'
  )

  t.deepEqual(
    afterProps.reporter,
    reporter,
    '`reporter` should be passed through'
  )

  t.equal(
    typeof afterProps.logMessage,
    'function',
    '`logMessage` should be a function'
  )

  t.equal(
    typeof afterProps.logFile,
    'function',
    '`logFile` should be a function'
  )

  t.end()
})

test('plugin: done', async (t) => {
  const name = 'testName'
  const pluginFn = stub().returns(files)
  const eventStartSpy = spy()
  const eventDoneSpy = spy()
  const reporter = new EventEmitter()
  const props = { files, reporter }

  reporter.on('start', eventStartSpy)
  reporter.on('done', eventDoneSpy)

  await plugin(name, pluginFn)(props)

  t.ok(
    eventStartSpy.calledOnceWith(name),
    'should emit `start` event'
  )

  t.ok(
    eventDoneSpy.calledOnceWith(name),
    'should emit `done` event'
  )

  t.end()
})

test('plugin: error', async (t) => {
  const name = 'testName'
  const testError = new Error('oops')
  const pluginFn = stub().throws(testError)
  const eventStartSpy = spy()
  const eventErrorSpy = spy()
  const reporter = new EventEmitter()
  const props = { files, reporter }

  reporter.on('start', eventStartSpy)
  reporter.on('error', eventErrorSpy)

  try {
    await plugin(name, pluginFn)(props)
  } catch (error) {
    t.equal(
      error,
      null,
      'should swallow original error'
    )

    t.ok(
      eventStartSpy.calledOnceWith(name),
      'should emit `start` event'
    )

    t.ok(
      eventErrorSpy.calledOnceWith(name, testError),
      'should emit `error` event'
    )

    t.end()
  }
})

test('plugin: log message', async (t) => {
  const name = 'testName'
  const message = 'hello'
  const pluginFn = stub().callsFake(({ logMessage }) => {
    logMessage(message)

    return files
  })
  const eventMessageSpy = spy()
  const reporter = new EventEmitter()
  const props = { files, reporter }

  reporter.on('message', eventMessageSpy)

  await plugin(name, pluginFn)(props)

  t.ok(
    eventMessageSpy.calledOnceWith(name, message),
    'should emit `message` event'
  )

  t.end()
})

test('plugin: log file', async (t) => {
  const name = 'testName'
  const filePath = 'file/path'
  const pluginFn = stub().callsFake(({ logFile }) => {
    logFile(filePath)

    return files
  })
  const eventFileSpy = spy()
  const reporter = new EventEmitter()
  const props = { files, reporter }

  reporter.on('file', eventFileSpy)

  await plugin(name, pluginFn)(props)

  t.ok(
    eventFileSpy.calledOnceWith(name, filePath),
    'should emit `file` event'
  )

  t.end()
})
{'fraction_non_alphanumeric': 0.13873318385650224, 'fraction_numerical': 0.0002802690582959641, 'mean_word_length': 3.1355735805330243, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '4096120', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1230, 'n_words': 314}