<commit_before>require('es6-promise').polyfill()

Helper = require('hubot-test-helper')
Hubot = require('../node_modules/hubot-test-helper/node_modules/hubot')

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

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

room = null

describe 'hubot-phabs module', ->

  hubotHear = (message, userName='momo') ->
    beforeEach (done) ->
      room.messages = []
      room.user.say userName, message
      room.messages.shift()
      setTimeout (done), 50

  hubot = (message, userName='momo') ->
    hubotHear "@hubot #{message}", userName

  hubotResponse = ->
    room.messages[0][1]

  hubotResponseCount = ->
    room.messages.length

  beforeEach ->
    process.env.PHABRICATOR_URL = 'http://example.com'
    process.env.PHABRICATOR_API_KEY = 'xxx'
    process.env.PHABRICATOR_BOT_PHID = 'PHID-USER-xxx'
    process.env.PHABRICATOR_PROJECTS = 'PHID-PROJ-xxx:proj1,PHID-PROJ-yyy:proj2'
    room = helper.createRoom { httpd: false }
    room.robot.brain.userForId 'user',
      name: 'user'
    room.robot.brain.userForId 'user_with_email',
      name: 'user_with_email',
      email_address: '<EMAIL>'
    room.robot.brain.userForId 'user_with_phid',
      name: 'user_with_phid',
      phid: 'PHID-USER-123456789'
    room.receive = (userName, message) ->
      new Promise (resolve) =>
        @messages.push [userName, message]
        user = room.robot.brain.userForId userName
        @robot.receive(new Hubot.TextMessage(user, message), resolve)

  afterEach ->
    delete process.env.PHABRICATOR_URL
    delete process.env.PHABRICATOR_API_KEY
    delete process.env.PHABRICATOR_BOT_PHID
    delete process.env.PHABRICATOR_PROJECTS

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

    context 'phab version', ->
      hubot 'phab version'
      it 'should reply version number', ->
        expect(hubotResponse()).to.match /hubot-phabs module is version [0-9]+\.[0-9]+\.[0-9]+/

    context 'ph version', ->
      hubot 'ph version'
      it 'should reply version number', ->
        expect(hubotResponse()).to.match /hubot-phabs module is version [0-9]+\.[0-9]+\.[0-9]+/

  # ---------------------------------------------------------------------------------
  context 'user requests the list of known projects', ->

    context 'phab list projects', ->
      hubot 'phab list projects'
      it 'should reply the list of known projects according to PHABRICATOR_PROJECTS', ->
        expect(hubotResponseCount()).to.eql 1
        expect(hubotResponse()).to.eql 'Known Projects: proj1, proj2'

  # ---------------------------------------------------------------------------------
  context 'user asks for task info', ->
    beforeEach ->
      do nock.disableNetConnect
      nock(process.env.PHABRICATOR_URL)
        .get('/api/maniphest.info')
        .reply(200, { result: { 
          status: 'open',
          priority: 'Low',
          name: '<NAME>',
          ownerPHID: 'PHID-USER-42'
          } })
        .get('/api/user.query')
        .reply(200, { result: [{ userName: 'toto' }]})

    afterEach ->
      nock.cleanAll()

    context 'phab T42', ->
      hubot 'phab T42'
      it 'gives information about the task Txxx', ->
        expect(hubotResponse()).to.eql 'T42 has status open, priority Low, owner toto'

    context 'ph T42 # with an ending space', ->
      hubot 'ph T42 '
      it 'gives information about the task Txxx', ->
        expect(hubotResponse()).to.eql 'T42 has status open, priority Low, owner toto'


  # ---------------------------------------------------------------------------------
  context 'user asks about a user', ->

    context 'phab toto', ->
      hubot 'phab toto'
      it 'warns when that user is unknown', ->
        expect(hubotResponse()).to.eql 'Sorry, I have no idea who toto is. Did you mistype it?'

    context 'phab user', -><commit_msg>some more tests<commit_after>require('es6-promise').polyfill()

Helper = require('hubot-test-helper')
Hubot = require('../node_modules/hubot-test-helper/node_modules/hubot')

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

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

room = null

describe 'hubot-phabs module', ->

  hubotHear = (message, userName='momo') ->
    beforeEach (done) ->
      room.messages = []
      room.user.say userName, message
      room.messages.shift()
      setTimeout (done), 50

  hubot = (message, userName='momo') ->
    hubotHear "@hubot #{message}", userName

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

  hubotResponseCount = ->
    room.messages.length

  beforeEach ->
    process.env.PHABRICATOR_URL = 'http://example.com'
    process.env.PHABRICATOR_API_KEY = 'xxx'
    process.env.PHABRICATOR_BOT_PHID = 'PHID-USER-xxx'
    process.env.PHABRICATOR_PROJECTS = 'PHID-PROJ-xxx:proj1,PHID-PROJ-yyy:proj2'
    room = helper.createRoom { httpd: false }
    room.robot.brain.userForId 'user',
      name: 'user'
    room.robot.brain.userForId 'user_with_email',
      name: 'user_with_email',
      email_address: '<EMAIL>'
    room.robot.brain.userForId 'user_with_phid',
      name: 'user_with_phid',
      phid: 'PHID-USER-123456789'
    room.receive = (userName, message) ->
      new Promise (resolve) =>
        @messages.push [userName, message]
        user = room.robot.brain.userForId userName
        @robot.receive(new Hubot.TextMessage(user, message), resolve)

  afterEach ->
    delete process.env.PHABRICATOR_URL
    delete process.env.PHABRICATOR_API_KEY
    delete process.env.PHABRICATOR_BOT_PHID
    delete process.env.PHABRICATOR_PROJECTS

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

    context 'phab version', ->
      hubot 'phab version'
      it 'should reply version number', ->
        expect(hubotResponse()).to.match /hubot-phabs module is version [0-9]+\.[0-9]+\.[0-9]+/

    context 'ph version', ->
      hubot 'ph version'
      it 'should reply version number', ->
        expect(hubotResponse()).to.match /hubot-phabs module is version [0-9]+\.[0-9]+\.[0-9]+/

  # ---------------------------------------------------------------------------------
  context 'user requests the list of known projects', ->

    context 'phab list projects', ->
      hubot 'phab list projects'
      it 'should reply the list of known projects according to PHABRICATOR_PROJECTS', ->
        expect(hubotResponseCount()).to.eql 1
        expect(hubotResponse()).to.eql 'Known Projects: proj1, proj2'

  # ---------------------------------------------------------------------------------
  context 'user asks for task info', ->

    context 'task id is provided', ->
      beforeEach ->
        do nock.disableNetConnect
        nock(process.env.PHABRICATOR_URL)
          .get('/api/maniphest.info')
          .reply(200, { result: { 
            status: 'open',
            priority: 'Low',
            name: '<NAME>',
            ownerPHID: 'PHID-USER-42'
            } })
          .get('/api/user.query')
          .reply(200, { result: [{ userName: 'toto' }]})

      afterEach ->
        nock.cleanAll()

      context 'phab T42', ->
        hubot 'phab T42'
        it 'gives information about the task Txxx', ->
          expect(hubotResponse()).to.eql 'T42 has status open, priority Low, owner toto'

      context 'ph T42 # with an ending space', ->
        hubot 'ph T42 '
        it 'gives information about the task Txxx', ->
          expect(hubotResponse()).to.eql 'T42 has status open, priority Low, owner toto'

    # context 'task id is implicit', ->
    #   beforeEach ->
    #     do nock.disableNetConnect
    #     nock(process.env.PHABRICATOR_URL)
    #       .get('/api/maniphest.info')
    #       .reply(200, { result: { 
    #         status: 'open',
    #         priority: 'Low',
    #         name: '<NAME>',
    #         ownerPHID: 'PHID-USER-42'
    #         } })
    #       .get('/api/user.query')
    #       .reply(200, { result: [{ userName: 'toto' }]})
    #       .get('/api/maniphest.info')
    #       .reply(200, { result: { 
    #         status: 'open',
    #         priority: 'Low',
    #         name: '<NAME>',
    #         ownerPHID: 'PHID-USER-42'
    #         } })
    #       .get('/api/user.query')
    #       .reply(200, { result: [{ userName: 'toto' }]})

    #   afterEach ->
    #     nock.cleanAll()

    #   context 'ph', ->
    #     hubot 'ph T42'
    #     hubot 'ph'
    #     it 'gives information about the task Txxx', ->
    #       expect(hubotResponse(1)).to.eql 'T42 has status open, priority Low, owner toto'



  # ---------------------------------------------------------------------------------
  context 'user asks about a user', ->

    context 'phab toto', ->
      hubot 'phab toto'
      it 'warns when that user is unknown', ->
        expect(hubotResponse()).to.eql 'Sorry, I have no idea who toto is. Did you mistype it?'

    context 'phab user', ->
{'fraction_non_alphanumeric': 0.20307093425605535, 'fraction_numerical': 0.013300173010380624, 'mean_word_length': 1.9768265207595752, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 73, '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': '2722381', 'n_tokens_mistral': 3001, 'n_tokens_neox': 2863, 'n_words': 658}