<commit_before>'use strict'
const chai = require('chai')
const expect = chai.expect
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
const plugiator = require('plugiator')
const Remi = require('../')

chai.use(sinonChai)

describe('Remi', function() {
  it('should register plugin with no version', function() {
    let plugin = plugiator.anonymous((app, options, next) => next())

    let remi = new Remi()
    let app = {}
    return remi
      .register(app, { register: plugin })
      .then(() => {
        expect(app.registrations[plugin.attributes.name]).to.exist
      })
  })

  it('should register synchronous plugin', function() {
    let plugin = plugiator.anonymous((app, opts) => {})

    let remi = new Remi()
    let app = {}
    return remi
      .register(app, { register: plugin })
      .then(() => {
        expect(app.registrations[plugin.attributes.name]).to.exist
      })
  })

  it('should register plugin that returns a promise', function() {
    let plugin = plugiator.anonymous((app, opts) => Promise.resolve())

    let remi = new Remi()
    let app = {}
    return remi.register(app, { register: plugin })
      .then(() => {
        expect(app.registrations[plugin.attributes.name]).to.exist
      })
  })

  it('registers plugin with options', function() {
    let register = sinon.spy()
    let plugin = plugiator.anonymous(register)
    let pluginOpts = { something: true }

    let remi = new Remi()
    return remi
      .register({}, { register: plugin, options: pluginOpts })
      .then(() => {
        expect(register).to.have.been.calledOnce
        expect(register.args[0][1]).to.eql(pluginOpts)
      })
  })

  it('throws error if dependent plugin not present', function(done) {
    let plugin = plugiator.noop({
      name: 'plugin1',
      version: '0.0.0',
      dependencies: ['foo'],
    })

    let remi = new Remi()
    remi.register({}, plugin, function(err) {
      expect(err).to.be.an.instanceof(Error)
      done()
    })
  })

  it('should throw error if one the plugins didn\'t finished registering in time', function(done) {
    let remi = new Remi({
      registrationTimeout: 10,
    })
    remi.register({}, [
      {
        register: plugiator.anonymous((server, opts, next) => undefined),
      },
    ], err => {
      expect(err).to.be.an.instanceof(Error)
      done()
    })
  })

  it('register plugins in correct order when `before` specified', function() {
    let plugin2 = sinon.spy(plugiator.noop('plugin2'))
    let plugin1 = sinon.spy(plugiator.noop({
      name: 'plugin1',
      version: '0.0.0',
      before: ['plugin2'],
    }))

    let remi = new Remi()
    return remi
      .register({}, [plugin2, plugin1])
      .then(() => {
        expect(plugin1).to.have.been.calledBefore(plugin2)
      })
  })

  it('exposes the registrations object', function(done) {
    let plugin1 = plugiator.noop({
      name: 'plugin1',
      version: '0.0.0',
    })
    let plugin2 = plugiator.noop({
      name: 'plugin2',
      version: '0.1.0',
    })

    let app = {}
    let plugins = [
      {
        register: plugin1,
        options: {foo: 1},
      },
      {
        register: plugin2,
      },
    ]
    new Remi().register(app, plugins, function(err) {
      expect(err).to.not.exist

      expect(app.registrations).to.not.be.undefined
      expect(app.registrations.plugin1).to.not.be.undefined
      expect(app.registrations.plugin1.name).to.eq('plugin1')
      expect(app.registrations.plugin1.version).to.eq('0.0.0')
      expect(app.registrations.plugin1.options.foo).to.eq(1)

      expect(app.registrations.plugin2).to.not.be.undefined
      expect(app.registrations.plugin2.name).to.eq('plugin2')
      expect(app.registrations.plugin2.version).to.eq('0.1.0')

      done()
    })
  })

  it('should register plugin only once', function() {
    let plugin = sinon.spy(plugiator.noop())

    let remi = new Remi({<commit_msg>tests copies updated<commit_after>'use strict'
const chai = require('chai')
const expect = chai.expect
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
const plugiator = require('plugiator')
const Remi = require('../')

chai.use(sinonChai)

describe('Remi', function() {
  it('should register plugin with no version', function() {
    let plugin = plugiator.anonymous((app, options, next) => next())

    let remi = new Remi()
    let app = {}
    return remi
      .register(app, { register: plugin })
      .then(() => {
        expect(app.registrations[plugin.attributes.name]).to.exist
      })
  })

  it('should register synchronous plugin', function() {
    let plugin = plugiator.anonymous((app, opts) => {})

    let remi = new Remi()
    let app = {}
    return remi
      .register(app, { register: plugin })
      .then(() => {
        expect(app.registrations[plugin.attributes.name]).to.exist
      })
  })

  it('should register plugin that returns a promise', function() {
    let plugin = plugiator.anonymous((app, opts) => Promise.resolve())

    let remi = new Remi()
    let app = {}
    return remi.register(app, { register: plugin })
      .then(() => {
        expect(app.registrations[plugin.attributes.name]).to.exist
      })
  })

  it('should register plugin with options', function() {
    let register = sinon.spy()
    let plugin = plugiator.anonymous(register)
    let pluginOpts = { something: true }

    let remi = new Remi()
    return remi
      .register({}, { register: plugin, options: pluginOpts })
      .then(() => {
        expect(register).to.have.been.calledOnce
        expect(register.args[0][1]).to.eql(pluginOpts)
      })
  })

  it('should throw error if dependent plugin not present', function(done) {
    let plugin = plugiator.noop({
      name: 'plugin1',
      version: '0.0.0',
      dependencies: ['foo'],
    })

    let remi = new Remi()
    remi.register({}, plugin, function(err) {
      expect(err).to.be.an.instanceof(Error)
      done()
    })
  })

  it('should throw error if one the plugins didn\'t finished registering in time', function(done) {
    let remi = new Remi({
      registrationTimeout: 10,
    })
    remi.register({}, [
      {
        register: plugiator.anonymous((server, opts, next) => undefined),
      },
    ], err => {
      expect(err).to.be.an.instanceof(Error)
      done()
    })
  })

  it('should register plugins in correct order when `before` specified', function() {
    let plugin2 = sinon.spy(plugiator.noop('plugin2'))
    let plugin1 = sinon.spy(plugiator.noop({
      name: 'plugin1',
      version: '0.0.0',
      before: ['plugin2'],
    }))

    let remi = new Remi()
    return remi
      .register({}, [plugin2, plugin1])
      .then(() => {
        expect(plugin1).to.have.been.calledBefore(plugin2)
      })
  })

  it('should expose the registrations object', function(done) {
    let plugin1 = plugiator.noop({
      name: 'plugin1',
      version: '0.0.0',
    })
    let plugin2 = plugiator.noop({
      name: 'plugin2',
      version: '0.1.0',
    })

    let app = {}
    let plugins = [
      {
        register: plugin1,
        options: {foo: 1},
      },
      {
        register: plugin2,
      },
    ]
    let remi = new Remi()
    remi.register(app, plugins, function(err) {
      expect(err).to.not.exist

      expect(app.registrations).to.not.be.undefined
      expect(app.registrations.plugin1).to.not.be.undefined
      expect(app.registrations.plugin1.name).to.eq('plugin1')
      expect(app.registrations.plugin1.version).to.eq('0.0.0')
      expect(app.registrations.plugin1.options.foo).to.eq(1)

      expect(app.registrations.plugin2).to.not.be.undefined
      expect(app.registrations.plugin2.name).to.eq('plugin2')
      expect(app.registrations.plugin2.version).to.eq('0.1.0')

      done()
    })
  })

  it('should register plugin only once', function() {
    let plugin = sinon.spy(plugiator.noop())

    let remi = new Remi({
{'fraction_non_alphanumeric': 0.15746343923348463, 'fraction_numerical': 0.012355017650025214, 'mean_word_length': 2.657445827570309, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '5673364', 'n_tokens_mistral': 2626, 'n_tokens_neox': 2539, 'n_words': 544}