<commit_before>require 'spec_helper'

class MockMessage
  attr_accessor :from, :to, :body

  def initialize(from, to, body = nil)
    @from, @to, @body = from, to, body
  end

  def reply
    r = self.dup
    r.to, r.from = self.from, self.to
    r
  end
end

describe MessageHandler do
  describe 'showing help message' do
    it 'should respond with help text' do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'help'
      MessageHandler.respond_to(@input).body.include?('Things you can ask me:').should be true
    end
  end

  describe 'receiving an invalid request' do
    it 'should show a reasonable error message' do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'j23ijlksdvjlqk23;lkja;wlkj'
      MessageHandler.respond_to(@input).body.should == "Now you're not making any sense at all!"
    end
  end

  describe 'fetching Status messages' do
    before :each do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'get status for blangfeld'
    end

    it 'should request the latest status for a given user' do
      mock_status = [{'text' => 'Ben Was Here', 'user' => {'screen_name' => 'blangfeld'}}]
      StatusMessages.expects(:last_status_for).once.with('blangfeld').returns mock_status

      MessageHandler.respond_to(@input).body.should == '@blangfeld: "Ben Was Here"'
    end

    it 'should respond with a generic message if no status messages are available' do
      mock_status = []
      StatusMessages.expects(:last_status_for).once.with("blangfeld").returns mock_status

      MessageHandler.respond_to(@input).body.should == "No status updates found for blangfeld"
    end

    it 'should properly handle server error messages' do
      @input.body = 'get status for wdrexler'
      mock_status = {"error"=>"No such user.",
                     "request"=>"/api/statuses/user_timeline.json?screen_name=wdrexler&count=1"}
      StatusMessages.expects(:last_status_for).once.with('wdrexler').returns mock_status

      MessageHandler.respond_to(@input).body.should == "Server reported an error: No such user."
    end
  end

  describe 'listing projects' do
    before :each do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'list projects'
    end<commit_msg>Add failing test for generic status update<commit_after>require 'spec_helper'

class MockMessage
  attr_accessor :from, :to, :body

  def initialize(from, to, body = nil)
    @from, @to, @body = from, to, body
  end

  def reply
    r = self.dup
    r.to, r.from = self.from, self.to
    r
  end
end

describe MessageHandler do
  describe 'showing help message' do
    it 'should respond with help text' do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'help'
      MessageHandler.respond_to(@input).body.include?('Things you can ask me:').should be true
    end
  end

  describe 'receiving an invalid request' do
    it 'should show a reasonable error message' do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'j23ijlksdvjlqk23;lkja;wlkj'
      MessageHandler.respond_to(@input).body.should == "Now you're not making any sense at all!"
    end
  end

  describe 'fetching Status messages' do
    before :each do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'get status for blangfeld'
    end

    it 'should fetch the latest status from the public timeline' do
      @input.body = 'get status'
      mock_status = [{"text"=>
   "<NAME> (https://status.mojolingo.com/url/6) started following Adam Snark Rabbit (https://status.mojolingo.com/url/23).",
  "truncated"=>false,
  "created_at"=>"Fri Feb 17 15:37:43 -0500 2012",
  "in_reply_to_status_id"=>nil,
  "source"=>"activity",
  "id"=>308,
  "in_reply_to_user_id"=>nil,
  "in_reply_to_screen_name"=>nil,
  "geo"=>nil,
  "favorited"=>false,
  "user"=>
   {"id"=>4,
    "name"=>"<NAME>",
    "screen_name"=>"blangfeld",
    "location"=>"Preston, UK",
    "description"=>nil,
    "profile_image_url"=>
     "https://secure.gravatar.com/avatar.php?gravatar_id=f4971cb1f368ba9a03f227469768bdf6&default=http%3A%2F%2Fstatus.mojolingo.com",
    "url"=>"http://langfeld.me",
    "protected"=>false,
    "followers_count"=>7,
    "friends_count"=>10,
    "created_at"=>"Mon Oct 17 07:35:54 -0400 2011",
    "favourites_count"=>0,
    "utc_offset"=>"0",
    "time_zone"=>"UTC",
    "statuses_count"=>45,
    "following"=>true,
    "statusnet_blocking"=>false,
    "notifications"=>true,
    "statusnet_profile_url"=>"https://status.mojolingo.com/blangfeld"},
  "statusnet_html"=>
   "<a href=\"https://status.mojolingo.com/blangfeld\">Ben Langfeld</a> started following arabbit",
  "statusnet_conversation_id"=>268}]

      StatusMessages.expects(:timeline).once.with(:public, 1).returns mock_status

      MessageHandler.respond_to(@input).body.should == '@blangfeld: "<NAME> (https://status.mojolingo.com/url/6) started following <NAME> (https://status.mojolingo.com/url/23)."'
    end

    it 'should request the latest status for a given user' do
      mock_status = [{'text' => 'Ben Was Here', 'user' => {'screen_name' => 'blangfeld'}}]
      StatusMessages.expects(:last_status_for).once.with('blangfeld').returns mock_status

      MessageHandler.respond_to(@input).body.should == '@blangfeld: "Ben Was Here"'
    end

    it 'should respond with a generic message if no status messages are available' do
      mock_status = []
      StatusMessages.expects(:last_status_for).once.with("blangfeld").returns mock_status

      MessageHandler.respond_to(@input).body.should == "No status updates found for blangfeld"
    end

    it 'should properly handle server error messages' do
      @input.body = 'get status for wdrexler'
      mock_status = {"error"=>"No such user.",
                     "request"=>"/api/statuses/user_timeline.json?screen_name=wdrexler&count=1"}
      StatusMessages.expects(:last_status_for).once.with('wdrexler').returns mock_status

      MessageHandler.respond_to(@input).body.should == "Server reported an error: No such user."
    end
  end

  describe 'listing projects' do
    before :each do
      @input = MockMessage.new '<EMAIL>', '<EMAIL>', 'list projects'
    end
{'fraction_non_alphanumeric': 0.1394966277348248, 'fraction_numerical': 0.01431156440203981, 'mean_word_length': 3.735202492211838, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 67, 'https://': 7, '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': '5428139', 'n_tokens_mistral': 2079, 'n_tokens_neox': 1987, 'n_words': 496}