<commit_before>import 'babel-polyfill'
import { expect } from 'chai'
import { EventEmitter } from 'events'
import _ from 'lodash'

import { FileConfig } from '../src/config'
import DictPlugin from '../plugins/dict'

describe('plugin: dict', function () {
  this.timeout(3000)

  let config, emitter, plugin
  const nick = 'chameleon'
  const channel = '#test'

  before(async function () {
    config = await FileConfig.temp()
    expect(config).to.exist
  })

  beforeEach(async function () {
    emitter = new EventEmitter()
    plugin = new DictPlugin()
    await plugin.load(emitter, config)
    expect(plugin).to.exist
  })

  afterEach(async function () {
    await plugin.reset()
    plugin = null
    emitter = null
  })

  after(async function () {
    config.removeDir()
  })

  it('value() [empty]', async function () {
    const result = await plugin.value('term')
    expect(result).to.not.exist
  })

  it('values() [empty]', async function () {
    const result = await plugin.value('term')
    expect(result).to.be.empty
  })

  it('add(), value()', async function () {
    const value = {
      key: 'term',
      value: 'test value',
      user: 'testuser'
    }

    await plugin.add(value.key, value.value, value.user)

    const result = await plugin.value('term')
    expect(result).to.include(value)
  })

  it('add(), value(user = null)', async function () {
    const value = {
      key: 'term',
      value: 'test value',
      user: null
    }

    await plugin.add(value.key, value.value, value.user)

    const result = await plugin.value('term')
    expect(result).to.include(value)
  })

  it('add(), value(), remove()', async function () {
    const value = {
      key: 'term',
      value: 'test value',
      user: 'testuser'
    }

    await plugin.add(value.key, value.value, value.user)

    const result = await plugin.value('term')
    expect(result).to.include(value)

    expect(await plugin.remove(value.key, 1)).to.be.true

    const resultAfter = await plugin.value('term')
    expect(resultAfter).to.not.exist
  })

  function testResponse (text, key, value, done) {
    emitter.on('dict.respond', (fromNick, to, name, fromValue) => {
      try {
        expect(fromNick).to.equal(nick)
        expect(to).to.equal(channel)
        expect(name).to.equal(key)
        expect(fromValue).to.equal(value)
        done()
      } catch (err) {
        done(err)
      }
    })

    emitter.emit('message#', nick, channel, text)
  }

  const tests = {
    'exists?': 'exists',
    'two words?': 'two words',
    'multiple  spaces?': 'multiple  spaces',
    ' padded?  ': 'padded',
    '  three padded words? ': 'three padded words',
    'multiple question marks???': 'multiple question marks'
  }

  for (const [text, key] of _.toPairs(tests)) {
    it(`lookup: "${text}"`, function (done) {
      const value = 'this value'
      const user = 'testuser'

      plugin.add(key, value, user)<commit_msg>Add missing await on DictPlugin test<commit_after>import 'babel-polyfill'
import { expect } from 'chai'
import { EventEmitter } from 'events'
import _ from 'lodash'

import { FileConfig } from '../src/config'
import DictPlugin from '../plugins/dict'

describe('plugin: dict', function () {
  this.timeout(3000)

  let config, emitter, plugin
  const nick = 'chameleon'
  const channel = '#test'

  before(async function () {
    config = await FileConfig.temp()
    expect(config).to.exist
  })

  beforeEach(async function () {
    emitter = new EventEmitter()
    plugin = new DictPlugin()
    await plugin.load(emitter, config)
    expect(plugin).to.exist
  })

  afterEach(async function () {
    await plugin.reset()
    plugin = null
    emitter = null
  })

  after(async function () {
    config.removeDir()
  })

  it('value() [empty]', async function () {
    const result = await plugin.value('term')
    expect(result).to.not.exist
  })

  it('values() [empty]', async function () {
    const result = await plugin.value('term')
    expect(result).to.be.empty
  })

  it('add(), value()', async function () {
    const value = {
      key: 'term',
      value: 'test value',
      user: 'testuser'
    }

    await plugin.add(value.key, value.value, value.user)

    const result = await plugin.value('term')
    expect(result).to.include(value)
  })

  it('add(), value(user = null)', async function () {
    const value = {
      key: 'term',
      value: 'test value',
      user: null
    }

    await plugin.add(value.key, value.value, value.user)

    const result = await plugin.value('term')
    expect(result).to.include(value)
  })

  it('add(), value(), remove()', async function () {
    const value = {
      key: 'term',
      value: 'test value',
      user: 'testuser'
    }

    await plugin.add(value.key, value.value, value.user)

    const result = await plugin.value('term')
    expect(result).to.include(value)

    expect(await plugin.remove(value.key, 1)).to.be.true

    const resultAfter = await plugin.value('term')
    expect(resultAfter).to.not.exist
  })

  function testResponse (text, key, value, done) {
    emitter.on('dict.respond', (fromNick, to, name, fromValue) => {
      try {
        expect(fromNick).to.equal(nick)
        expect(to).to.equal(channel)
        expect(name).to.equal(key)
        expect(fromValue).to.equal(value)
        done()
      } catch (err) {
        done(err)
      }
    })

    emitter.emit('message#', nick, channel, text)
  }

  const tests = {
    'exists?': 'exists',
    'two words?': 'two words',
    'multiple  spaces?': 'multiple  spaces',
    ' padded?  ': 'padded',
    '  three padded words? ': 'three padded words',
    'multiple question marks???': 'multiple question marks'
  }

  for (const [text, key] of _.toPairs(tests)) {
    it(`lookup: "${text}"`, async function (done) {
      const value = 'this value'
      const user = 'testuser'

      await plugin.add(key, value, user)
{'fraction_non_alphanumeric': 0.1567832404122318, 'fraction_numerical': 0.0016894745734076701, 'mean_word_length': 2.8541666666666665, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6906721', 'n_tokens_mistral': 1932, 'n_tokens_neox': 1869, 'n_words': 462}