require 'spec_helper'

# tests what happens when the websocket connection goes away
describe "Reconnect", :js => true, :type => :feature, :capybara_feature => true do

  subject { page }

  let(:user1) { FactoryGirl.create(:user) }
  let(:user2) { FactoryGirl.create(:user) }

  before(:all) do
    User.delete_all
  end

  before(:each) do
    Diagnostic.delete_all
    emulate_client

    FactoryGirl.create(:friendship, :user => user1, :friend => user2)
  end

  it "websocket connection is down on initial connection" do
    pending

    FactoryGirl.create(:friendship, :user => user1, :friend => user2)
    FactoryGirl.create(:friendship, :user => user2, :friend => user1)

    Rails.application.config.stub(:websocket_gateway_uri).and_return('ws://localhost:99/websocket') # bogus port

    sign_in_poltergeist(user1, validate: false)

    page.should have_selector('.no-websocket-connection')

    find('.homecard.createsession').trigger(:click)
    find('h1', text:'create session')
    find('#btn-create-session').trigger(:click)
    find('#notification h2', text: 'Not Connected') # get notified you can't go to create session
    page.evaluate_script('window.history.back()')

    find('.homecard.findsession').trigger(:click)
    find('#notification h2', text: 'Not Connected') # get notified you can't go to find session
    find('h2', text: 'create session') # and be back on home screen

    find('.homecard.feed').trigger(:click)
    find('h1', text:'feed')
    page.evaluate_script('window.history.back()')

    find('.homecard.musicians').trigger(:click)
    find('h1', text:'musicians')
    page.evaluate_script('window.history.back()')

    find('.homecard.profile').trigger(:click)
    find('h1', text:'profile')
    page.evaluate_script('window.history.back()')

    find('.homecard.account').trigger(:click)
    find('h1', text:'account')
    page.evaluate_script('window.history.back()')

    initiate_text_dialog user2

    find('span.disconnected-msg', text: 'DISCONNECTED FROM SERVER')
  end

  it "websocket goes down on home page" do

    sign_in_poltergeist(user1)

    2.times do |i|
      close_websocket

      # we should see indication that the websocket is down
      page.should have_selector('.no-websocket-connection')

      # but.. after a few seconds, it should reconnect on it's own
      page.should_not have_selector('.no-websocket-connection')
    end

    # then verify we can create a session

    create_join_session(user1, [user2])

    formal_leave_by user1

    # websocket goes down while chatting
    in_client(user1) do
      initiate_text_dialog user2

      # normal, happy dialog
      page.should_not have_selector('span.disconnected-msg', text: 'DISCONNECTED FROM SERVER')

      close_websocket

      # dialog-specific disconnect should show
      page.should have_selector('span.disconnected-msg', text: 'DISCONNECTED FROM SERVER')
      # and generic disconnect
      page.should have_selector('.no-websocket-connection')

      # after a few seconds, the page should reconnect on it's own
      page.should_not have_selector('span.disconnected-msg', text: 'DISCONNECTED FROM SERVER')
      page.should_not have_selector('.no-websocket-connection')
    end
  end

  it "websocket goes down on session page", intermittent: true do

    create_session(creator: user1)

    2.times do
      close_websocket

      # we should see indication that the websocket is down
      page.should have_selector('h2', text: 'Disconnected from Server')

      # but.. after a few seconds, it should reconnect on it's own
      page.should_not have_selector('h2', text: 'Disconnected from Server')

      find('div[layout-id="session"] h1', text:'session')
    end
  end
end
