<commit_before>#
#    Copyright 2016 <NAME>
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#

assert = require('chai').assert
SymphonyAdapter = require '../src/adapter'
NockServer = require './nock-server'
{FakeRobot} = require './fakes'

process.env['HUBOT_SYMPHONY_HOST'] = 'foundation.symphony.com'
process.env['HUBOT_SYMPHONY_PUBLIC_KEY'] = './test/resources/publicKey.pem'
process.env['HUBOT_SYMPHONY_PRIVATE_KEY'] = './test/resources/privateKey.pem'
process.env['HUBOT_SYMPHONY_PASSPHRASE'] = 'changeit'

describe 'Constructor test', () ->
  constructorProps = ['HUBOT_SYMPHONY_HOST', 'HUBOT_SYMPHONY_PUBLIC_KEY', 'HUBOT_SYMPHONY_PRIVATE_KEY', 'HUBOT_SYMPHONY_PASSPHRASE']

  for constructorProp in constructorProps
    it "should throw on construction if #{constructorProp} missing", () ->
      prop = process.env[constructorProp]
      delete process.env[constructorProp]
      assert.throws(SymphonyAdapter.use, new RegExp("#{constructorProp} undefined"))
      process.env[constructorProp] = prop

describe 'Adapter test suite with helloWorld message', () ->
  nock = null
  symphony = null

  beforeEach ->
    nock = new NockServer({host: 'https://foundation.symphony.com'})

  afterEach ->
    nock.close()

  it 'should connect and receive message', (done) ->
    robot = new FakeRobot
    adapter = SymphonyAdapter.use(robot)
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)
      robot.on 'received', () ->
        assert.isAtLeast((m for m in robot.received when m.text is 'Hello World').length, 1)
        adapter.close()
        done()
    adapter.run()

  it 'should ignore http 400 errors when reading datafeed', (done) ->
    nock.datafeedReadHttp400Count = 1
    robot = new FakeRobot
    adapter = SymphonyAdapter.use(robot)
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)
      robot.on 'received', () ->
        assert.isAtLeast((m for m in robot.received when m.text is 'Hello World').length, 1)
        adapter.close()
        done()
    adapter.run()

  it 'should retry if datafeed cannot be created', (done) ->
    nock.datafeedCreateHttp400Count = 1
    robot = new FakeRobot
    adapter = SymphonyAdapter.use robot
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)
      robot.on 'received', () ->
        assert.isAtLeast((m for m in robot.received when m.text is 'Hello World').length, 1)
        adapter.close()
        done()
    adapter.run()

describe 'Adapter test suite', () ->
  nock = null
  symphony = null

  beforeEach ->
    nock = new NockServer({host: 'https://foundation.symphony.com', startWithHelloWorldMessage: false})

  afterEach ->
    nock.close()

  it 'should send with no adornment', (done) ->
    robot = new FakeRobot
    adapter = SymphonyAdapter.use(robot)
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)<commit_msg>test: 400 on datafeed poll now causes a reconnect<commit_after>#
#    Copyright 2016 <NAME>
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#

assert = require('chai').assert
SymphonyAdapter = require '../src/adapter'
NockServer = require './nock-server'
{FakeRobot} = require './fakes'

process.env['HUBOT_SYMPHONY_HOST'] = 'foundation.symphony.com'
process.env['HUBOT_SYMPHONY_PUBLIC_KEY'] = './test/resources/publicKey.pem'
process.env['HUBOT_SYMPHONY_PRIVATE_KEY'] = './test/resources/privateKey.pem'
process.env['HUBOT_SYMPHONY_PASSPHRASE'] = 'changeit'

describe 'Constructor test', () ->
  constructorProps = ['HUBOT_SYMPHONY_HOST', 'HUBOT_SYMPHONY_PUBLIC_KEY', 'HUBOT_SYMPHONY_PRIVATE_KEY', 'HUBOT_SYMPHONY_PASSPHRASE']

  for constructorProp in constructorProps
    it "should throw on construction if #{constructorProp} missing", () ->
      prop = process.env[constructorProp]
      delete process.env[constructorProp]
      assert.throws(SymphonyAdapter.use, new RegExp("#{constructorProp} undefined"))
      process.env[constructorProp] = prop

describe 'Adapter test suite with helloWorld message', () ->
  nock = null
  symphony = null

  beforeEach ->
    nock = new NockServer({host: 'https://foundation.symphony.com'})

  afterEach ->
    nock.close()

  it 'should connect and receive message', (done) ->
    robot = new FakeRobot
    adapter = SymphonyAdapter.use(robot)
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)
      robot.on 'received', () ->
        assert.isAtLeast((m for m in robot.received when m.text is 'Hello World').length, 1)
        adapter.close()
        done()
    adapter.run()

  it 'should retry on http 400 errors when reading datafeed', (done) ->
    nock.datafeedReadHttp400Count = 1
    robot = new FakeRobot
    adapter = SymphonyAdapter.use(robot)
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)
      robot.on 'error', () ->
        adapter.on 'connected', () ->
          robot.on 'received', () ->
            assert.isAtLeast((m for m in robot.received when m.text is 'Hello World').length, 1)
            adapter.close()
            done()
    adapter.run()

  it 'should retry if datafeed cannot be created', (done) ->
    nock.datafeedCreateHttp400Count = 1
    robot = new FakeRobot
    adapter = SymphonyAdapter.use(robot)
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)
      robot.on 'received', () ->
        assert.isAtLeast((m for m in robot.received when m.text is 'Hello World').length, 1)
        adapter.close()
        done()
    adapter.run()

describe 'Adapter test suite', () ->
  nock = null
  symphony = null

  beforeEach ->
    nock = new NockServer({host: 'https://foundation.symphony.com', startWithHelloWorldMessage: false})

  afterEach ->
    nock.close()

  it 'should send with no adornment', (done) ->
    robot = new FakeRobot
    adapter = SymphonyAdapter.use(robot)
    adapter.on 'connected', () ->
      assert.isDefined(adapter.symphony)
{'fraction_non_alphanumeric': 0.11990787390240391, 'fraction_numerical': 0.006765510292212466, 'mean_word_length': 3.383596214511041, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, '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': '4520211', 'n_tokens_mistral': 2203, 'n_tokens_neox': 2139, 'n_words': 581}