<commit_before>Helper = require('hubot-test-helper')
chai = require 'chai'
http = require 'http'
nock = require 'nock'
fs = require 'fs'

expect = chai.expect

helper = new Helper('../src/esa.coffee')
process.env.EXPRESS_PORT = 8039

describe 'esa', ->
  room = null

  beforeEach ->
    process.env.HUBOT_ESA_ACCESS_TOKEN = 'dummy'
    process.env.HUBOT_ESA_TEAM = 'ginger'
    process.env.HUBOT_ESA_WEBHOOK_DEFAULT_ROOM = 'general'
    process.env.HUBOT_ESA_WEBHOOK_ENDPOINT = '/hubot/ginger'
    # process.env.HUBOT_ESA_WEBHOOK_JUST_EMIT = 'true'
    room = helper.createRoom()

  afterEach ->
    room.destroy()

  describe 'Response to chatroom', ->
    nockScope = null
    beforeEach ->
      nock.disableNetConnect()
      nockScope = nock('https://api.esa.io')

    afterEach ->
      nock.cleanAll()

    context 'someone requests stats', ->
      beforeEach (done) ->
        nockScope
          .get("/v1/teams/#{process.env.HUBOT_ESA_TEAM}/stats")
          .query(access_token: process.env.HUBOT_ESA_ACCESS_TOKEN)
          .replyWithFile(200, "#{__dirname}/fixtures/stats.json")
        room.user.say('gingy', '@hubot esa stats')
        setTimeout done, 200

      it 'responds stats', ->
        expect(room.messages).to.eql [
          ['gingy', '@hubot esa stats']
          ['hubot', "Members: 20\nPosts: 1959\nComments: 2695\nStars: 3115\nDaily Active Users: 8\nWeekly Active Users: 14\nMonthly Active Users: 15"]
        ]

    describe 'post', ->
      context 'in own team', ->
        beforeEach ->
          nockScope
            .get("/v1/teams/#{process.env.HUBOT_ESA_TEAM}/posts/1390")
            .query(access_token: process.env.HUBOT_ESA_ACCESS_TOKEN)
            .replyWithFile(200, "#{__dirname}/fixtures/post.json")

        context 'someone says post url', ->
          beforeEach (done) ->
            room.user.say('gingy', 'https://ginger.esa.io/posts/1390')
            setTimeout done, 200

          it 'send message about post', ->
            expect(room.messages).to.eql [
              ['gingy', 'https://ginger.esa.io/posts/1390']
              ['hubot', 'esa: 日報/2015/05/09/hi! #api #dev']
            ]

        context 'someone says post url wtih anchor', ->
          beforeEach (done) ->
            room.user.say('gingy', 'https://ginger.esa.io/posts/1390#1-1-1')
            setTimeout done, 200

          it 'send message about post', ->
            expect(room.messages).to.eql [
              ['gingy', 'https://ginger.esa.io/posts/1390#1-1-1']
              ['hubot', 'esa: 日報/2015/05/09/hi! #api #dev']
            ]

      context 'in other team, someone says post url', ->
        beforeEach (done) ->
          room.user.say('gingy', 'https://zachary.esa.io/posts/1390')
          setTimeout done, 200

        it 'nothing to say', ->
          expect(room.messages).to.eql [
            ['gingy', 'https://zachary.esa.io/posts/1390']
          ]

    describe 'comment', ->
      context 'in own team', ->
        beforeEach (done) ->
          nockScope
            .get("/v1/teams/#{process.env.HUBOT_ESA_TEAM}/comments/2121")
            .query(access_token: process.env.HUBOT_ESA_ACCESS_TOKEN)
            .replyWithFile(200, "#{__dirname}/fixtures/comment.json")
          room.user.say('gingy', 'https://ginger.esa.io/posts/1390#comment-2121')
          setTimeout done, 200

        it 'send message about comment', ->
          expect(room.messages).to.eql [
            ['gingy', 'https://ginger.esa.io/posts/1390#comment-2121']
            ['hubot', 'esa: 読みたい']
          ]

      context 'in other team, someone says comment url', -><commit_msg>Add tests emitting each event<commit_after>Helper = require('hubot-test-helper')
chai = require 'chai'
http = require 'http'
nock = require 'nock'
fs = require 'fs'

expect = chai.expect

helper = new Helper('../src/esa.coffee')
process.env.EXPRESS_PORT = 8039

describe 'esa', ->
  room = null

  beforeEach ->
    process.env.HUBOT_ESA_ACCESS_TOKEN = '<PASSWORD>'
    process.env.HUBOT_ESA_TEAM = 'ginger'
    process.env.HUBOT_ESA_WEBHOOK_DEFAULT_ROOM = 'general'
    process.env.HUBOT_ESA_WEBHOOK_ENDPOINT = '/hubot/ginger'
    # process.env.HUBOT_ESA_WEBHOOK_JUST_EMIT = 'true'
    room = helper.createRoom()

  afterEach ->
    room.destroy()

  describe 'Response to chatroom', ->
    nockScope = null
    beforeEach ->
      nock.disableNetConnect()
      nockScope = nock('https://api.esa.io')

    afterEach ->
      nock.cleanAll()

    context 'someone requests stats', ->
      emitted = false
      emitted_stats = null
      beforeEach (done) ->
        room.robot.on 'esa.hear.stats', (res, stats) ->
          emitted = true
          emitted_stats = stats
        nockScope
          .get("/v1/teams/#{process.env.HUBOT_ESA_TEAM}/stats")
          .query(access_token: process.env.HUBOT_ESA_ACCESS_TOKEN)
          .replyWithFile(200, "#{__dirname}/fixtures/stats.json")
        room.user.say('gingy', '@hubot esa stats')
        setTimeout done, 200

      it 'responds stats', ->
        expect(room.messages).to.eql [
          ['gingy', '@hubot esa stats']
          ['hubot', "Members: 20\nPosts: 1959\nComments: 2695\nStars: 3115\nDaily Active Users: 8\nWeekly Active Users: 14\nMonthly Active Users: 15"]
        ]

      it 'emits esa.hear.stats event with args', ->
        expect(emitted).to.equal true
        expect(emitted_stats.members).to.equal 20

    describe 'post', ->
      context 'in own team', ->
        emitted = false
        emitted_post = null
        beforeEach ->
          room.robot.on 'esa.hear.post', (res, post) ->
            emitted = true
            emitted_post = post
          nockScope
            .get("/v1/teams/#{process.env.HUBOT_ESA_TEAM}/posts/1390")
            .query(access_token: process.env.HUBOT_ESA_ACCESS_TOKEN)
            .replyWithFile(200, "#{__dirname}/fixtures/post.json")

        context 'someone says post url', ->
          beforeEach (done) ->
            room.user.say('gingy', 'https://ginger.esa.io/posts/1390')
            setTimeout done, 200

          it 'send message about post', ->
            expect(room.messages).to.eql [
              ['gingy', 'https://ginger.esa.io/posts/1390']
              ['hubot', 'esa: 日報/2015/05/09/hi! #api #dev']
            ]

          it 'emits esa.hear.post event with args', ->
            expect(emitted).to.equal true
            expect(emitted_post.name).to.equal 'hi!'

        context 'someone says post url wtih anchor', ->
          beforeEach (done) ->
            room.user.say('gingy', 'https://ginger.esa.io/posts/1390#1-1-1')
            setTimeout done, 200

          it 'send message about post', ->
            expect(room.messages).to.eql [
              ['gingy', 'https://ginger.esa.io/posts/1390#1-1-1']
              ['hubot', 'esa: 日報/2015/05/09/hi! #api #dev']
            ]

      context 'in other team, someone says post url', ->
        beforeEach (done) ->
          room.user.say('gingy', 'https://zachary.esa.io/posts/1390')
          setTimeout done, 200

        it 'nothing to say', ->
          expect(room.messages).to.eql [
            ['gingy', 'https://zachary.esa.io/posts/1390']
          ]

    describe 'comment', ->
      context 'in own team', ->
        emitted = false
        emitted_comment = null
        beforeEach (done) ->
          room.robot.on 'esa.hear.comment', (res, comment) ->
            emitted = true
            emitted_comment = comment
          nockScope
            .get("/v1/teams/#{process.env.HUBOT_ESA_TEAM}/comments/2121")
            .query(access_token: process.env.HUBOT_ESA_ACCESS_TOKEN)
            .replyWithFile(200, "#{__dirname}/fixtures/comment.json")
          room.user.say('gingy', 'https://ginger.esa.io/posts/1390#comment-2121')
          setTimeout done, 200

        it 'send message about comment', ->
          expect(room.messages).to.eql [
            ['gingy', 'https://ginger.esa.io/posts/1390#comment-2121']
            ['hubot', 'esa: 読みたい']
          ]

        it 'emits esa.hear.comment event with args', ->
          expect(emitted).to.equal true
          expect(emitted_comment.body_md).to.equal '読みたい'

      context 'in other team, someone says comment url', ->
{'fraction_non_alphanumeric': 0.14535944532295342, 'fraction_numerical': 0.029436808174188055, 'mean_word_length': 2.110858872493379, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 62, 'https://': 18, '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': '3059073', 'n_tokens_mistral': 2913, 'n_tokens_neox': 2710, 'n_words': 508}