require 'spec_helper'
require 'webmock/rspec'

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

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

  before 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 ME" do
      response_body = mock_latency_response([{ user: user1, ars_total_latency: 1.0, ars_internet_latency: 0.1, audio_latency: 0.5 }]) 
      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(user1.first_name, expand: true)
      find("#search-results a[user-id=\"#{user1.id}\"][hoveraction=\"musician\"]", text: user1.name).hover_intent
      find('h3', text: user1.name)
      find("#musician-latency-badge .latency", text: 'ME')
      find("#musician-latency-badge .latency-info").should have_content("")
    end
    
    
    it "show FAILED" do
      stub_request(:post, latency_data_uri)
      .with(:headers => {'Accept'=>'*/*', 'Content-Type'=>'application/json', 'User-Agent'=>'Ruby'})
      .to_raise("some error")

      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: 'N/A')
      find("#musician-latency-badge .latency-info").should have_content("")
    end  

    it "show UNKNOWN" do
      response_body = mock_latency_response([]) #sessionUtils.LATENCY.UNKNOWN: {description: "UNKNOWN", style: "latency-unknown", min: -2, max: -2}
      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: 'UNKNOWN')
      find("#musician-latency-badge .latency-info").should have_content("")
    end
 
    it "show HIGH" do
      response_body = mock_latency_response([{ user: user2, ars_total_latency: 101.0, ars_internet_latency: 81, audio_latency: 20 }]) #sessionUtils.LATENCY.UNACCEPTABLE : {description: "UNACCEPTABLE", style: "latency-unacceptable", min: 100.0, max: 10000000},
      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-unaccptable.png")
      find("#musician-latency-badge .latency-info", text: 'Internet 81ms + Audio 20ms')
    end 
  
  end

end
