<commit_before>if typeof require != 'undefined'
  chai = require 'chai'
  chaiAsPromised = require 'chai-as-promised'
  Services = require 'service'
else
  chai = window.chai
  chaiAsPromised = window.chaiAsPromised
  Services = window.Services

chai.use(chaiAsPromised)
expect = chai.expect

describe 'Services', ->
  describe 'registering', ->
    service = null
    beforeEach ->
      service = Object.create(Services.Service)
    afterEach ->
      Services.unregister('TestService')

    it 'Allows a service to be registered', ->
      expect(Services.register("TestService", service)).to.equal(1)

    it 'Allows a service to be unregistered', ->
      Services.register('TestService', service)
      Services.register('TestService', Object.create(Services.Service))
      expect(Services.unregister('TestService', service)).to.equal(1)

    it 'Allows all of a particular service to be unregistered', ->
      Services.register('TestService', service)
      Services.register('TestService', Object.create(Services.Service))
      expect(Services.unregister('TestService')).to.equal(0)

  describe 'starting', ->
    service = null
    beforeEach ->
      service = Object.create(Services.Service)
      Services.register('TestService', service)
    afterEach ->
      Services.stop('TestService').then -> Services.unregister('TestService')

    it 'returns a promise that resolves if nothing happens in onStart', ->
      expect(Services.start('TestService')).to.eventually.eql([service])

    it 'returns a promise that resolves when a promise returned from onStart is resolved', ->
      defer = Q.defer()
      service.onStart = -> defer.promise
      setTimeout(
        -> defer.resolve()
      , 2)
      expect(Services.start('TestService')).to.eventually.eql([service])

    it 'does not allow you to double start a service', ->
      Services.start('TestService').then ->
        expect(-> Services.start('TestService')).to.throw(Error)

    describe 'isUsable', ->
      service2 = null
      beforeEach ->
        service2 = Object.create(Services.Service)
        Services.register('TestService', service2)

      it 'uses the second implementation if the first is not usable', ->
        service.isUsable = -> false
        expect(Services.start('TestService')).to.eventually.eql([service2])

      it 'uses the second implementation if the first is not usable async', ->
        deferred = Q.defer()
        service.isUsable = -> deferred.promise
        setTimeout(
          -> deferred.resolve(false)
        , 2)
        expect(Services.start('TestService')).to.eventually.eql([service2])

      it 'rejects the start if no implementations are usable', ->
        deferred = Q.defer()
        service.isUsable = -> false
        service2.isUsable = -> deferred.promise
        setTimeout(
          -> deferred.resolve(false)
        , 2)
        expect(Services.start('TestService')).to.eventually.be.rejected

      it 'wont behave as if things succeeded if there was an exception', ->
        service.isUsable = -> throw new Error("Something bad happened")
        expect(-> Services.start('TestService')).to.throw(Error, "Something bad happened")

  describe 'stopping', ->
    service = null
    beforeEach ->
      service = Object.create(Services.Service)
      @onStopCalled = false
      service.onStop = => @onStopCalled = true
      Services.register('TestService', service)
    afterEach -> Services.unregister('TestService')

    it 'allows all services to be stopped', (done) ->
      Services.start('TestService')
      Services.stop().done =>
        expect(@onStopCalled).to.be.true
        done()

    it 'allows a single type of service to be stopped', ->
      service2 = Object.create(Services.Service)
      secondStopCalled = false
      service2.onStop = -> secondStopCalled = true
      Services.register('TestService2', service2)<commit_msg>Make sure services are totally stopped before unregistering<commit_after># Some nonsense to make these tests run in both browser and node environments
if typeof require != 'undefined'
  chai = require 'chai'
  chaiAsPromised = require 'chai-as-promised'
  Services = require 'service'
else
  chai = window.chai
  chaiAsPromised = window.chaiAsPromised
  Services = window.Services

chai.use(chaiAsPromised)
expect = chai.expect

describe 'Services', ->
  describe 'registering', ->
    service = null
    beforeEach ->
      service = Object.create(Services.Service)
    afterEach ->
      Services.unregister('TestService')

    it 'Allows a service to be registered', ->
      expect(Services.register("TestService", service)).to.equal(1)

    it 'Allows a service to be unregistered', ->
      Services.register('TestService', service)
      Services.register('TestService', Object.create(Services.Service))
      expect(Services.unregister('TestService', service)).to.equal(1)

    it 'Allows all of a particular service to be unregistered', ->
      Services.register('TestService', service)
      Services.register('TestService', Object.create(Services.Service))
      expect(Services.unregister('TestService')).to.equal(0)

  describe 'starting', ->
    service = null
    beforeEach ->
      service = Object.create(Services.Service)
      Services.register('TestService', service)
    afterEach ->
      Services.stop('TestService').then -> Services.unregister('TestService')

    it 'returns a promise that resolves if nothing happens in onStart', ->
      expect(Services.start('TestService')).to.eventually.eql([service])

    it 'returns a promise that resolves when a promise returned from onStart is resolved', ->
      defer = Q.defer()
      service.onStart = -> defer.promise
      setTimeout(
        -> defer.resolve()
      , 2)
      expect(Services.start('TestService')).to.eventually.eql([service])

    it 'does not allow you to double start a service', ->
      Services.start('TestService').then ->
        expect(-> Services.start('TestService')).to.throw(Error)

    describe 'isUsable', ->
      service2 = null
      beforeEach ->
        service2 = Object.create(Services.Service)
        Services.register('TestService', service2)

      it 'uses the second implementation if the first is not usable', ->
        service.isUsable = -> false
        expect(Services.start('TestService')).to.eventually.eql([service2])

      it 'uses the second implementation if the first is not usable async', ->
        deferred = Q.defer()
        service.isUsable = -> deferred.promise
        setTimeout(
          -> deferred.resolve(false)
        , 2)
        expect(Services.start('TestService')).to.eventually.eql([service2])

      it 'rejects the start if no implementations are usable', ->
        deferred = Q.defer()
        service.isUsable = -> false
        service2.isUsable = -> deferred.promise
        setTimeout(
          -> deferred.resolve(false)
        , 2)
        expect(Services.start('TestService')).to.eventually.be.rejected

      it 'wont behave as if things succeeded if there was an exception', ->
        service.isUsable = -> throw new Error("Something bad happened")
        expect(-> Services.start('TestService')).to.throw(Error, "Something bad happened")

  describe 'stopping', ->
    service = null
    beforeEach ->
      service = Object.create(Services.Service)
      @onStopCalled = false
      service.onStop = => @onStopCalled = true
      Services.register('TestService', service)
    afterEach -> Services.stop().then -> Services.unregister('TestService')

    it 'allows all services to be stopped', (done) ->
      Services.start('TestService')
      Services.stop().done =>
        expect(@onStopCalled).to.be.true
        done()

    it 'allows a single type of service to be stopped', ->
      service2 = Object.create(Services.Service)
      secondStopCalled = false
      service2.onStop = -> secondStopCalled = true
      Services.register('TestService2', service2)
{'fraction_non_alphanumeric': 0.12109375, 'fraction_numerical': 0.004032258064516129, 'mean_word_length': 3.0268899036022323, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 84, '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': '2315689', 'n_tokens_mistral': 2173, 'n_tokens_neox': 2143, 'n_words': 545}