require 'spec_helper'

# these tests should be idempotent, and not spammy to other JK users
# because they DO use actual production user accounts on www (see the TestUsers below)

# Jenkins executes rspec on this folder every 15 minutes or so.
# SO don't use this to test something like a public session unless you want all the world to see
# (theoretically) Google Analytics does not track requests made from this fake jam client

www = ENV['MONITOR_URL'] || 'http://www.jamkazam.com'

describe "Deployed site at #{www}", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  before(:all) do
    Capybara.javascript_driver = :poltergeist
    Capybara.current_driver = Capybara.javascript_driver
    Capybara.app_host = www
    Capybara.run_server = false
  end

  before { puts "\n" }
  after { puts "\n\n" }

  TestUser = Class.new do
    attr_accessor :email, :password, :first_name, :last_name, :id

    def initialize(h)
      h.each {|k,v| send("#{k}=",v)}
    end

    alias :to_s :first_name

    def name
      first_name + ' ' + last_name
    end
  end

  if www =~ /staging/
    # use staging accounts
    user1 = TestUser.new({ email: 'anthony+jim@jamkazam.com', password: 'j4m!t3st3r', first_name: 'Jim', last_name: 'Smith', id: '43fb60a7-7b6a-4aa6-af3c-59b1e58a842b' })
    user2 = TestUser.new({ email: 'anthony+john@jamkazam.com', password: 'j4m!t3st3r', first_name: 'John', last_name: 'Jones', id: '54c5d5c1-aa44-4db8-9bac-922ead64162e' })
    user3 = TestUser.new({ email: 'anthony+jesse@jamkazam.com', password: 'j4m!t3st3r', first_name: 'Jesse', last_name: 'Smith', id: '8208b467-ef59-4601-a4e1-1198180ffe82' })
  else
    # use production accounts
    user1 = TestUser.new({ email: 'anthony+jim@jamkazam.com', password: 'j4m!t3st3r', first_name: 'Jim', last_name: 'Smith', id: '68e8eea2-140d-44c1-b711-10d07ce70f96' })
    user2 = TestUser.new({ email: 'anthony+john@jamkazam.com', password: 'j4m!t3st3r', first_name: 'John', last_name: 'Jones', id: '5bbcf689-2f73-452d-815a-c4f44e9e7f3e' })
    user3 = TestUser.new({ email: 'anthony+jesse@jamkazam.com', password: 'j4m!t3st3r', first_name: 'Jesse', last_name: 'Smith', id: 'e8342f87-04ac-432f-8af4-abf485fddb9e' })
  end

  it "is possible for #{user3} to sign in and not get disconnected within 30 seconds" do
    pending "continual failures - need to debug - try using Selenium instead of PhantomJS"
    as_monitor(user3) do
      sign_in_poltergeist(user3)
      repeat_for(30.seconds) do
        expect(page).to_not have_selector('.no-websocket-connection') #looks for reconnect dialog every 1 second
      end
    end
  end

  it "is possible for #{user1} to see and to send a message to #{user2}" do
    pending "need to debug client switching done by as_monitor in the wake of VRFS-2016"
    # this example heavily based on text_message_spec.rb in 'web'

    as_monitor(user1) do
      sign_in_poltergeist(user1)
    end

    test_message = "#{SecureRandom.uuid} - Hey #{user1}!"
    test_response = "#{SecureRandom.uuid} - Hey yourself, #{user2}!"
    test_goodbye = "#{SecureRandom.uuid} - OK bye!"

    as_monitor(user2) do
      sign_in_poltergeist(user2)
      expect(page).to have_xpath(
                          "//div[@class='friend-name' and @user-id='#{user1.id}']/span[@class='friend-status']",
                          :text => "Available" )

      site_search(user1.name, expand: true)
      find("#search-results a[user-id=\"#{user1.id}\"][hoveraction=\"musician\"]", text: user1.name).hover_intent
      find('#musician-hover #btnMessage').trigger(:click)
      find('h1', text: 'conversation with ' + user1.name)
      send_text_message(test_message, close_on_send: true)
    end

    as_monitor(user1) do
      expect(page).to have_xpath(
                          "//div[@class='friend-name' and @user-id='#{user2.id}']/span[@class='friend-status']",
                          :text => "Available" )
      find('#notification #btn-reply').trigger(:click)
      find('h1', text: 'conversation with ' + user2.name)
      find('.previous-message-text', text: test_message)
      find('#text-message-dialog .btn-close-dialog', text: 'CLOSE').trigger(:click)
      sign_out_poltergeist(validate: true)
    end

    as_monitor(user2) do
      sign_out_poltergeist(validate: true)
    end
  end

  let(:queue_limit) { 5 }

  specify "no stuck workers found in resque queues" do
    page.driver.basic_authorize('jamjam','blueberryjam')
    visit '/admin/resque/queues'
    page.all('tr:not(.failure) .size').each do |job_queue|
      expect(job_queue.text.to_i).to be < queue_limit
    end
    #this (maybe mistakenly) ignores the .failure queue, since it can be in the hundreds - VRFS-1930
  end
end
