require 'spec_helper'
require 'webmock/rspec'

#NOTE: The rest of the tests related to musician latency badge are in 
#musician_hover_spec_2.rb file. For some uncertain issue getting all these 
#tests in a single file doesn't work because some of them fail without a reason. 
#Probably because of webmock behaving wiredly. 

describe "Musician Hover", :js => true, :type => :feature, :capybara_feature => true  do
  let(:user1) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }
  let(:user3) { FactoryGirl.create(:user) }
  let(:user4) { FactoryGirl.create(:user) }
  let(:user5) { FactoryGirl.create(:user) }
  let(:user6) { FactoryGirl.create(:user) }

  let(:latency_data_uri) { /\S+\/user_latencies/ }

  before(:all) do
   Capybara.default_max_wait_time = 30 # these tests are SLOOOOOW
  end

  before(:each) do
    fast_signin(user1, "/client")
    wait_until_curtain_gone
  end

  describe "Latency badge" do
        
    it "show GOOD" do
      response_body = mock_latency_response([{ user: user2, ars_total_latency: 1.0, ars_internet_latency: 0.5, audio_latency: 0.5 }]) #sessionUtils.LATENCY.GOOD : {description: "GOOD", style: "latency-good", min: 0.0, max: 40.0},
      
      stub_request(:post, latency_data_uri)
      .with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'})
      .to_return( body: response_body, status: 200)

      site_search(user2.first_name, expand: true)
      find("#search-results a[user-id=\"#{user2.id}\"][hoveraction=\"musician\"]", text: user2.name).hover_intent
      find('h3', text: user2.name)
      find("#musician-latency-badge .latency", text: 'GOOD')
      #sleep(2)
      #save_screenshot("latency-good.png")
      find("#musician-latency-badge .latency-info", text: 'Internet 0ms + Audio 0ms')
      
    end

    it "show HIGH" do
      response_body = mock_latency_response([ {user: user2, ars_total_latency: 71.0, ars_internet_latency: 35, audio_latency: 36 }]) #sessionUtils.LATENCY.POOR : {description: "POOR", style: "latency-poor", min: 70.0, max: 100},
      stub_request(:post, latency_data_uri)
      .with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'})
      .to_return( body: response_body, status: 200)

      site_search(user2.first_name, expand: true)
      find("#search-results a[user-id=\"#{user2.id}\"][hoveraction=\"musician\"]", text: user2.name).hover_intent
      find('h3', text: user2.name)
      find("#musician-latency-badge .latency", text: 'HIGH')
      #sleep(2)
      #save_screenshot("latency-poor.png")
      find("#musician-latency-badge .latency-info", text: 'Internet 35ms + Audio 36ms')
    end

    it "show FAIR" do
      response_body = mock_latency_response([ { user: user2, ars_total_latency: 41.0, ars_internet_latency: 21.0, audio_latency: 20.0 }]) #sessionUtils.LATENCY.FAIR : {description: "FAIR", style: "latency-fair", min: 40.0, max: 70.0},
      stub_request(:post, latency_data_uri)
      .with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'})
      .to_return( body: response_body, status: 200)

      site_search(user2.first_name, expand: true)
      find("#search-results a[user-id=\"#{user2.id}\"][hoveraction=\"musician\"]", text: user2.name).hover_intent
      find('h3', text: user2.name)
      find("#musician-latency-badge .latency", text: 'FAIR')
      #sleep(2)
      #save_screenshot("latency-fair.png")
      find("#musician-latency-badge .latency-info", text: 'Internet 21ms + Audio 20ms')
    end
    
    it "show latency badge when there are set of musicians in search result" do
      response_body = mock_latency_response([
        { user: user2, ars_total_latency: 1.0, ars_internet_latency: 0.4, audio_latency: 0.6 }, #GOOD
        { user: user3, ars_total_latency: 71.0, ars_internet_latency: 35, audio_latency: 36 }, #POOR
        { user: user4, ars_total_latency: 41.0, ars_internet_latency: 15, audio_latency: 26.0 }, #FAIR
        { user: user5, ars_total_latency: 101.0, ars_internet_latency: 31.75, audio_latency: 70.25 } #UNACCEPTABLE
        ])
      
      stub_request(:post, latency_data_uri)
      .with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'})
      .to_return( body: response_body, status: 200)

      site_search(user2.first_name, expand: true)

      #Show badge - GOOD
      selector = "#search-results a[user-id=\"#{user2.id}\"][hoveraction=\"musician\"]"
      find(selector, text: user2.name).hover_intent
      find('h3', text: user2.name)
      find("#musician-latency-badge .latency", text: 'GOOD')
      find("#musician-latency-badge .latency-info", text: 'Internet 0ms + Audio 0ms')
      page.execute_script("$('#{selector}').mouseleave();")
      sleep(1)
      
      #Show badge - HIGH
      selector = "#search-results a[user-id=\"#{user3.id}\"][hoveraction=\"musician\"]"
      find(selector, text: user3.name).hover_intent
      find('h3', text: user3.name)
      find("#musician-latency-badge .latency", text: 'HIGH')
      find("#musician-latency-badge .latency-info", text: 'Internet 35ms + Audio 36ms')
      page.execute_script("$('#{selector}').mouseleave();")
      sleep(1)

      #Show badge - FAIR
      selector = "#search-results a[user-id=\"#{user4.id}\"][hoveraction=\"musician\"]"
      find(selector, text: user4.name).hover_intent
      find('h3', text: user4.name)
      find("#musician-latency-badge .latency", text: 'FAIR')
      find("#musician-latency-badge .latency-info", text: 'Internet 15ms + Audio 26ms')
      page.execute_script("$('#{selector}').mouseleave();")
      sleep(1)

      #Show badge - HIGH
      selector = "#search-results a[user-id=\"#{user5.id}\"][hoveraction=\"musician\"]"
      find(selector, text: user5.name).hover_intent
      find('h3', text: user5.name)
      find("#musician-latency-badge .latency", text: 'HIGH')
      find("#musician-latency-badge .latency-info", text: 'Internet 31ms + Audio 70ms')
      page.execute_script("$('#{selector}').mouseleave();")
      sleep(1)

    end
  
  end

end
