require 'spec_helper'

describe "Music Session", :js => true, :type => :feature, :capybara_feature => true, :slow => true do

  def leave_music_session_cleanly(usr)
    expect(usr.music_session_histories.count).to eq 1
    expect(usr.music_session_user_histories.count).to eq 1
    expect(usr.music_session_histories[0].session_removed_at).not_to be_nil
    expect(usr.music_session_user_histories[0].session_removed_at).not_to be_nil
  end

  subject { page }

  describe "recorded session" do

    before(:each) do
      ActiveMusicSession.delete_all
      MusicSession.delete_all
    end

    let(:searcher) { FactoryGirl.create(:user) }
    let(:creator) { FactoryGirl.create(:user) }
    let(:conn) { FactoryGirl.create(:connection, :user => creator) }
    let(:description) {'hot recordings in here'}
    let(:session) {FactoryGirl.create(:active_music_session, creator:creator, description: description)}
    let(:recording) {FactoryGirl.create(:recording, music_session: session, owner: creator)}

    it "won't let user join" do
      recording.touch
      join_session(searcher, description:'hot recordings in here', no_verify:true)
      find('#notification p').text('The session is currently recording.')
    end
  end

  context "last person" do 
    before(:each) do
      UserMailer.deliveries.clear
      ##### XXXXXX IF ANY OF THESE ARE UNMARKED PENDING, THEN UNCOMMENT THIS.  But AS IT IS IT ADDS
      # @user1, session_description = create_session
    end

    describe "cleanly leaves music session" do
      it "should update music session user session history" do
        pending "session leave is not removing user's connection"

        should have_link('session-leave')
        click_link('session-leave')
        leave_music_session_sleep_delay

        @user1.reload
        expect(@user1.connections.count).to eq 0
        leave_music_session_cleanly(@user1)
      end
    end

    describe "abruptly leaves music session" do 
      it "should delete connection and update music session user session history" do
        pending "still intermittently fails on build server"
        should have_link('session-leave')
        page.evaluate_script("JK.JamServer.close(true)")
        leave_music_session_sleep_delay

        @user1.reload
        expect(@user1.connections.count).to eq 0
        leave_music_session_cleanly(@user1)
      end
    end
  end

  context "second-to-last person" do
    before(:each) do
      UserMailer.deliveries.clear
      in_client(:user1_music_session) do
        # @user1, session_description = create_session ##### XXXXXX IF ANY OF THESE ARE UNMARKED PENDING, THEN UNCOMMENT THIS.  But AS IT IS IT ADDS
      end
    end

    describe "cleanly leaves"  do
      it "should update music session and user session history" do
        pending "session leave is not removing user's connection"
        in_client(:user2_music_session) do
          @user2, session_description = create_session
          should have_link('session-leave')
          click_link('session-leave')
          leave_music_session_sleep_delay

          @user2.reload
          expect(@user2.connections.count).to eq 1
          leave_music_session_cleanly(@user2)
        end
      end
    end

    describe "abruptly leaves" do
      it "should update music session and user session history" do
        pending "failing on build server"
        in_client(:user2_music_session) do
          @user2, session_description = create_session
          should have_link('session-leave')
          page.evaluate_script("JK.JamServer.close(true)")
          leave_music_session_sleep_delay

          @user2.reload
          expect(@user2.connections.count).to eq 0
          leave_music_session_cleanly(@user2)
        end
      end
    end
  end

end
