require 'spec_helper'

describe TransactionFeed::Twitter do
  let(:feed) { TransactionFeed::Twitter.new }
  let(:feed_result) { TransactionFeed::Result.new("text", "11111", "22222", Time.zone.now.to_s, "source_provider", "source_type", "55555") }

  describe "#poll" do
    let(:result) { double(:attrs => {:statuses => []}) }

    before do
      stub_const "TransactionFeed::Twitter::TWEETS_TO_RETRIEVE", 5
      stub_const "TransactionFeed::Twitter::SEARCH_TERM", "test"
    end

    context "no existing twitter transactions" do
      before do
        expect(Transaction).to receive(:most_recent_transaction_from_provider_and_type).with("twitter", "tweet").and_return(nil)
      end

      it "passes the correct arguments to the Twitter API (with no since_id argument)" do
        expect(feed.client).to receive(:search).with("test", :count => 5, :result_type => "recent").and_return(result)

        feed.poll
      end
    end

    context "existing twitter transactions" do
      before do
        expect(Transaction).to receive(:most_recent_transaction_from_provider_and_type).with("twitter", "tweet").and_return(double(:source_id => 42257))
      end

      it "passes the correct arguments to the Twitter API (with since_id argument)" do
        expect(feed.client).to receive(:search).with("test", :count => 5, :result_type => "recent", :since_id => 42257).and_return(result)

        feed.poll
      end
    end

    it "polls Twitter for tweets matching the search terms and builds transaction feed results with the response from the API" do
      expect(feed).to receive(:build_transaction_feed_result).exactly(5).times.and_return(feed_result)

      VCR.use_cassette(:twitter_api_search_for_tweets, :record => :new_episodes) do
        feed.poll do |result|
          result.should be feed_result
        end
      end
    end

    it "passes the values returned from Twitter correctly into #build_transaction_feed_result" do
      requested_at = Time.zone.now

      status = {
        :id => :source_id,
        :text => :text,
        :created_at => requested_at.to_s,
        :user => {
          :id => :payer_provider_id
        },
        :entities => {
          :user_mentions => [
            {
              :id => :payee_provider_id
            }
          ]
        }
      }
      response = Struct.new(:attrs).new({:statuses => [status]})

      expect(feed.client).to receive(:search).and_return(response)

      expect(feed).to receive(:build_transaction_feed_result).once.with(:text, :payer_provider_id, :payee_provider_id, requested_at.to_s, "twitter", "tweet", :source_id)

      feed.poll{|result| true}
    end
  end

  describe "#build_client" do
    it "sets into #client a configured Twitter REST client" do
      feed.client.should be_an_instance_of Twitter::REST::Client

      feed.client.consumer_key.should == Configuration.get(:TWITTER_CONSUMER_KEY)
      feed.client.consumer_secret.should == Configuration.get(:TWITTER_CONSUMER_SECRET)
    end
  end
end
