require 'spec_helper'

describe "Musician Search API", :type => :api do

  include Requests::JsonHelpers

  def get_query(query={})
    qstr = ''
    query.each { |kk,vv| qstr += "#{kk}=#{CGI::escape(vv.to_s)}&" }
    uri = "/api/search.json?#{Search::PARAM_MUSICIAN}=1&#{qstr}clientid=#{@conn.client_id}"
    get uri, "CONTENT_TYPE" => 'application/json'
  end

  describe "musician search page" do

    before(:each) do
      # 2.downto(1) { FactoryGirl.create(:geocoder) }
      User.delete_all
      Connection.delete_all
      Score.delete_all
      Score.connection.execute('delete from current_network_scores').check

      @user = FactoryGirl.create(:user, last_jam_locidispid: 1)
      @conn = FactoryGirl.create(:connection, user_id: @user.id, locidispid: 1)

      @user1 = FactoryGirl.create(:user, last_jam_locidispid: 1)
      @user2 = FactoryGirl.create(:user, last_jam_locidispid: 1)
      @user3 = FactoryGirl.create(:user, last_jam_locidispid: 2)
      @user4 = FactoryGirl.create(:user, last_jam_locidispid: 2)

      @users = []
      @users << @user1
      @users << @user2
      @users << @user3
      @users << @user4

      # 0-40 good, 40-80 moderate, 80-120 poor, 120-? unacceptable
      Score.createx(1, 'a', 1, 1, 'a', 1, 10)
      Score.createx(1, 'a', 1, 2, 'b', 2, 31)
      Score.createx(2, 'b', 2, 2, 'b', 2, 40)

      post '/sessions', "session[email]" => @user.email, "session[password]" => @user.password
      expect(rack_mock_session.cookie_jar["remember_token"]).to eq(@user.remember_token)
    end

    it "default search" do
      get_query
      good_response
      expect(json['musicians'].count).to be [Search::M_PER_PAGE, User.musicians_geocoded.count].min
      # puts json.inspect

    end

    context 'score filtering' do

      it "gets no musicians" do
        get_query({score_limit: Search::POOR_SCORE})
        good_response
        (json['musicians'] || []).count.should == 0
      end

      it "gets musicians for long-distance locations" do
        get_query({score_limit: Search::GOOD_SCORE})
        good_response
        (json['musicians'] || []).count.should == 3 # only users that have the 10 latency are low enough
      end

    end

    context 'instrument filtering' do 

      it "gets musicians for default instrument" do
        get_query({:instrument => 'electric guitar'})
        good_response
        expect(json['musicians'].count).to be >= 1
      end

      it "gets no musicians for unused instruments" do
        get_query({:instrument => 'tuba'})
        good_response
        expect(json['musicians'].count).to eq(0)
      end

    end

    context 'results have expected data' do
      it "has follower stats " do
        # @user4
        f1 = Follow.new
        f1.user = @user2
        f1.followable = @user4
        f1.save

        f2 = Follow.new
        f2.user = @user3
        f2.followable = @user4
        f2.save

        f3 = Follow.new
        f3.user = @user4
        f3.followable = @user4
        f3.save

        f5 = Follow.new
        f5.user = @user4
        f5.followable = @user3
        f5.save

        expect(@user4.followers.count).to be 3
        get_query(orderby: :followed)
        good_response
        musician = json["musicians"][0]
        expect(musician["id"]).to eq(@user4.id)
        followings = musician['followings']
        expect(followings.length).to be 2
        expect(musician['follow_count'].to_i).to be > 0
      end

      it "has friend stats" do
        Friendship.save(@user1.id, @user2.id)
        get_query
        good_response
        friend = json['musicians'].detect { |mm| mm['id'] == @user1.id }
        expect(friend['friend_count']).to be 1
      end
    end
  end
end
