<commit_before>'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const BootBot = require('../lib/BootBot');

describe('BootBot', () => {
  let server;
  let userId;
  let bot;

  beforeEach(() => {
    const options = {
      accessToken: '1234',
      verifyToken: '5678',
      appSecret: 'foobar'
    };
    userId = 1234;
    bot = new BootBot(options);
    server = sinon.fakeServer.create();
    server.autoRespond = true;
  });

  afterEach(() => {
    server.restore();
  });

  it('creates a bot instance', () => {
    expect(bot instanceof BootBot).to.equal(true);
  });

  it('throws an error if there are missing tokens', () => {
    expect(() => new BootBot()).to.throw(Error);
  });

  it('can send a text message', () => {
    const spy = sinon.spy(bot, 'sendRequest');
    const text = 'Hello world!';
    const expected = {
      recipient: {
        id: userId
      },
      message: {
        text
      }
    };

    bot.sendTextMessage(userId, text);
    expect(spy.calledWith(expected)).to.equal(true);
  });

  it('can send a text message with quick replies', () => {
    const spy = sinon.spy(bot, 'sendRequest');
    const text = 'Hello world!';

    // Quick Replies as strings with auto-generated payload
    const quickReplies1 = ['Red', 'Green', 'Blue'];
    const expected1 = {
      recipient: {
        id: userId
      },
      message: {
        text,
        quick_replies: [{
          content_type: 'text',
          title: 'Red',
          payload: 'BOOTBOT_QR_RED'<commit_msg>Added tests for hear, using new handleFacebookData function<commit_after>'use strict';
const expect = require('chai').expect;
const sinon = require('sinon');
const BootBot = require('../lib/BootBot');

describe('BootBot', () => {
  let server;
  let userId;
  let bot;

  beforeEach(() => {
    const options = {
      accessToken: '1234',
      verifyToken: '5678',
      appSecret: '<PASSWORD>'
    };
    userId = 1234;
    bot = new BootBot(options);
    server = sinon.fakeServer.create();
    server.autoRespond = true;
  });

  afterEach(() => {
    server.restore();
  });

  it('creates a bot instance', () => {
    expect(bot instanceof BootBot).to.equal(true);
  });

  it('throws an error if there are missing tokens', () => {
    expect(() => new BootBot()).to.throw(Error);
  });

  it('can send a text message', () => {
    const spy = sinon.spy(bot, 'sendRequest');
    const text = 'Hello world!';
    const expected = {
      recipient: {
        id: userId
      },
      message: {
        text
      }
    };

    bot.sendTextMessage(userId, text);
    expect(spy.calledWith(expected)).to.equal(true);
  });

  it('responds to a hear', () => {
    const comment = sinon.spy();
    bot.hear(/hello/, (payload, chat) => {
      comment();
    })
    const data = {
      "object": "page",
      "entry": [
        {
          "id": "PAGE_ID",
          "time": 1458692752478,
          "messaging": [
            {
              "sender": {
                "id": "USER_ID"
              },
              "recipient": {
                "id": "PAGE_ID"
              },
              "timestamp": 1458692752478,
              "message": {
                "mid": "mid.1457764197618:41d102a3e1ae206a38",
                "text": "hello, world!"
              }
            }
          ]
        }
      ]
    }
    bot.handleFacebookData(data)
    expect(comment.calledOnce).to.equal(true);
  });

  it('hear doesn\'t respond on non-match', () => {
    const comment = sinon.spy();
    bot.hear(/hello/, (payload, chat) => {
      comment();
    })
    const data = {
      "object": "page",
      "entry": [
        {
          "id": "PAGE_ID",
          "time": 1458692752478,
          "messaging": [
            {
              "sender": {
                "id": "USER_ID"
              },
              "recipient": {
                "id": "PAGE_ID"
              },
              "timestamp": 1458692752478,
              "message": {
                "mid": "mid.1457764197618:41d102a3e1ae206a38",
                "text": "goodbye, world!"
              }
            }
          ]
        }
      ]
    }
    bot.handleFacebookData(data)
    expect(comment.called).to.equal(false);
  });

  it('can send a text message with quick replies', () => {
    const spy = sinon.spy(bot, 'sendRequest');
    const text = 'Hello world!';

    // Quick Replies as strings with auto-generated payload
    const quickReplies1 = ['Red', 'Green', 'Blue'];
    const expected1 = {
      recipient: {
        id: userId
      },
      message: {
        text,
        quick_replies: [{
          content_type: 'text',
          title: 'Red',
          payload: 'BOOTBOT_QR_RED'
{'fraction_non_alphanumeric': 0.15895159585711266, 'fraction_numerical': 0.027478334390192347, 'mean_word_length': 1.8906536346976175, 'pattern_counts': {'":': 26, '<': 4, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5070626', 'n_tokens_mistral': 1576, 'n_tokens_neox': 1477, 'n_words': 323}