<commit_before>require('source-map-support').install {
  handleUncaughtExceptions: false,
  environment: 'node'
}

require('es6-promise').polyfill()

Helper = require('hubot-test-helper')

# helper loads a specific script if it's a file
helper = new Helper('../scripts/cron_events.coffee')

path   = require 'path'
sinon  = require 'sinon'
expect = require('chai').use(require('sinon-chai')).expect

room = null

# ---------------------------------------------------------------------------------
describe 'cron_events module', ->

  hubot = (message, userName = 'momo', tempo = 40) ->
    beforeEach (done) ->
      room.user.say userName, "@hubot #{message}"
      setTimeout (done), tempo

  hubotResponse = (i = 1) ->
    room.messages[i]?[1]

  hubotResponseCount = ->
    room.messages?.length - 1

  beforeEach ->
    room = helper.createRoom { httpd: false }
    room.robot.logger.error = sinon.stub()

  # ---------------------------------------------------------------------------------
  context 'at robot launch', ->
    beforeEach ->
      room.robot.brain.data.cron = {
        somejob: {
          cronTime: '0 0 1 1 *',
          eventName: 'event1',
          eventData: { },
          started: false
        },
        other: {
          cronTime: '0 0 1 1 *',
          eventName: 'event2',
          eventData: { },
          started: true,
          tz: 'UTC'
        }
      }
      room.robot.brain.emit 'loaded'
      room.robot.cron.loadAll()

      afterEach ->
        room.robot.brain.data.cron = { }
        room.robot.cron.jobs = { }

    context 'when brain is loaded', ->
      it 'jobs stored as not started are not started', ->
        expect(room.robot.cron.jobs.somejob).not.to.be.defined
      it 'jobs stored as started are started', ->
        expect(room.robot.cron.jobs.other).to.be.defined
      it 'job in brain should have a tz recorded', ->
        expect(room.robot.brain.data.cron.other.tz).to.eql 'UTC'


  # ---------------------------------------------------------------------------------
  context 'user wants to know hubot-cron-events version', ->

    context 'cron version', ->
      hubot 'cron version'
      it 'should reply version number', ->
        expect(hubotResponse()).
          to.match /hubot-cron-events module is version [0-9]+\.[0-9]+\.[0-9]+/
        expect(hubotResponseCount()).to.eql 1

  # ---------------------------------------------------------------------------------
  context 'user adds a new job', ->

    context 'with an invalid period', ->
      hubot 'cron somejob 80 // 80 80 80 some.event'
      it 'should complain about the period syntax', ->
        expect(hubotResponse()).to.eql "Sorry, '80 // 80 80 80' is not a valid pattern."
      it 'should log an error', ->
        expect(room.robot.logger.error).calledOnce

    context 'with a valid period', ->
      hubot 'cron somejob 0 0 1 1 * some.event'
      it 'should not complain about the period syntax', ->
        expect(hubotResponse()).
          to.eql 'The job somejob is created. It will stay paused until you start it.'
      it 'records the new job in the brain', ->
        expect(room.robot.brain.data.cron.somejob).to.exist

    context 'and job already runs', ->
      beforeEach ->
        room.robot.brain.data.cron = {
          somejob: {
            cronTime: '0 0 1 1 *',
            eventName: 'event2',
            eventData: { },
            started: true
          }
        }
        room.robot.brain.emit 'loaded'
        room.robot.cron.loadAll()

        afterEach ->
          room.robot.brain.data.cron = { }
          room.robot.cron.jobs = { }

      hubot 'cron somejob 0 0 1 1 * some.event'
      it 'should change the job', ->
        expect(hubotResponse()).
          to.eql 'The job somejob is created. It will stay paused until you start it.'
      it 'should have still have the job in the jobs queue', ->
        expect(room.robot.cron.jobs.somejob).to.be.defined

  # ---------------------------------------------------------------------------------
  context 'user starts a job', ->
    beforeEach ->
      room.robot.brain.data.cron = {
        somejob: {
          cronTime: '0 0 1 1 *',
          eventName: 'event1',
          eventData: { },
          started: false
        }
      }
      room.robot.brain.emit 'loaded'
      room.robot.cron.loadAll()

      afterEach ->
        room.robot.brain.data.cron = { }
        room.robot.cron.jobs = { }

    context 'but job is not known', ->
      hubot 'cron start nojob'
      it 'should complain about the inexistence of that job', ->
        expect(hubotResponse()).to.eql 'startJob: There is no such job named nojob'
      it 'should not have added a job in the jobs queue', ->
        expect(room.robot.cron.jobs.somejob).not.to.be.defined

    context 'and job exists', ->
      hubot 'cron start somejob'
      it 'should not complain about the period syntax', ->
        expect(hubotResponse()).to.eql 'The job somejob is now in service.'
      it 'should change brain to record it\'s started', ->
        expect(room.robot.brain.data.cron.somejob.started).to.be.true
      it 'should have added a job in the jobs queue', ->
        expect(room.robot.cron.jobs.somejob).to.be.defined<commit_msg>cover edge case<commit_after>require('source-map-support').install {
  handleUncaughtExceptions: false,
  environment: 'node'
}

require('es6-promise').polyfill()

Helper = require('hubot-test-helper')

# helper loads a specific script if it's a file
helper = new Helper('../scripts/cron_events.coffee')

path   = require 'path'
sinon  = require 'sinon'
expect = require('chai').use(require('sinon-chai')).expect

room = null

# ---------------------------------------------------------------------------------
describe 'cron_events module', ->

  hubot = (message, userName = 'momo', tempo = 40) ->
    beforeEach (done) ->
      room.user.say userName, "@hubot #{message}"
      setTimeout (done), tempo

  hubotResponse = (i = 1) ->
    room.messages[i]?[1]

  hubotResponseCount = ->
    room.messages?.length - 1

  beforeEach ->
    room = helper.createRoom { httpd: false }
    room.robot.logger.error = sinon.stub()

  # ---------------------------------------------------------------------------------
  context 'at robot launch', ->
    beforeEach ->
      room.robot.brain.data.cron = {
        somejob: {
          cronTime: '0 0 1 1 *',
          eventName: 'event1',
          eventData: { },
          started: false
        },
        other: {
          cronTime: '0 0 1 1 *',
          eventName: 'event2',
          eventData: { },
          started: true,
          tz: 'UTC'
        }
      }
      room.robot.brain.emit 'loaded'
      room.robot.cron.loadAll()

      afterEach ->
        room.robot.brain.data.cron = { }
        room.robot.cron.jobs = { }

    context 'when brain is loaded', ->
      it 'jobs stored as not started are not started', ->
        expect(room.robot.cron.jobs.somejob).not.to.be.defined
      it 'jobs stored as started are started', ->
        expect(room.robot.cron.jobs.other).to.be.defined
      it 'job in brain should have a tz recorded', ->
        expect(room.robot.brain.data.cron.other.tz).to.eql 'UTC'


  # ---------------------------------------------------------------------------------
  context 'user wants to know hubot-cron-events version', ->

    context 'cron version', ->
      hubot 'cron version'
      it 'should reply version number', ->
        expect(hubotResponse()).
          to.match /hubot-cron-events module is version [0-9]+\.[0-9]+\.[0-9]+/
        expect(hubotResponseCount()).to.eql 1

  # ---------------------------------------------------------------------------------
  context 'user adds a new job', ->

    context 'with an invalid period', ->
      hubot 'cron somejob 80 // 80 80 80 some.event'
      it 'should complain about the period syntax', ->
        expect(hubotResponse()).to.eql "Sorry, '80 // 80 80 80' is not a valid pattern."
      it 'should log an error', ->
        expect(room.robot.logger.error).calledOnce

    context 'with a valid period', ->
      hubot 'cron somejob 0 0 1 1 * some.event'
      it 'should not complain about the period syntax', ->
        expect(hubotResponse()).
          to.eql 'The job somejob is created. It will stay paused until you start it.'
      it 'records the new job in the brain', ->
        expect(room.robot.brain.data.cron.somejob).to.exist

    context 'and job already runs', ->
      beforeEach ->
        room.robot.brain.data.cron = {
          somejob: {
            cronTime: '0 0 1 1 *',
            eventName: 'event2',
            eventData: { },
            started: true
          }
        }
        room.robot.brain.emit 'loaded'
        room.robot.cron.loadAll()

        afterEach ->
          room.robot.brain.data.cron = { }
          room.robot.cron.jobs = { }

      hubot 'cron somejob 0 0 1 1 * some.event'
      it 'should change the job', ->
        expect(hubotResponse()).
          to.eql 'The job somejob is created. It will stay paused until you start it.'
      it 'should have still have the job in the jobs queue', ->
        expect(room.robot.cron.jobs.somejob).to.be.defined

  # ---------------------------------------------------------------------------------
  context 'user starts a job', ->
    beforeEach ->
      room.robot.brain.data.cron = {
        somejob: {
          cronTime: '0 0 1 1 *',
          eventName: 'event1',
          eventData: { },
          started: false
        },
        another: {
          cronTime: '0 0 1 1 *',
          eventName: 'event2',
          eventData: { },
          started: true
        }
      }
      room.robot.brain.emit 'loaded'
      room.robot.cron.loadAll()

      afterEach ->
        room.robot.brain.data.cron = { }
        room.robot.cron.jobs = { }

    context 'but job is not known', ->
      hubot 'cron start nojob'
      it 'should complain about the inexistence of that job', ->
        expect(hubotResponse()).to.eql 'startJob: There is no such job named nojob'
      it 'should not have added a job in the jobs queue', ->
        expect(room.robot.cron.jobs.somejob).not.to.be.defined

    context 'and job exists', ->
      hubot 'cron start somejob'
      it 'should not complain about the period syntax', ->
        expect(hubotResponse()).to.eql 'The job somejob is now in service.'
      it 'should change brain to record it\'s started', ->
        expect(room.robot.brain.data.cron.somejob.started).to.be.true
      it 'should have added a job in the jobs queue', ->
        expect(room.robot.cron.jobs.somejob).to.be.defined

    context 'and job exists and runs', ->
      hubot 'cron start another'
      it 'should change brain to record it\'s still started', ->
        expect(room.robot.brain.data.cron.another.started).to.be.true
      it 'should have added a job in the jobs queue', ->
        expect(room.robot.cron.jobs.another).to.be.defined
{'fraction_non_alphanumeric': 0.19434564523483813, 'fraction_numerical': 0.010852713178294573, 'mean_word_length': 1.6616504854368932, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 82, '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': '2825324', 'n_tokens_mistral': 3374, 'n_tokens_neox': 3192, 'n_words': 881}