require 'spec_helper'

describe MusicSessionUserHistory do

  let(:some_user) { FactoryGirl.create(:user) }
  let(:music_session) { FactoryGirl.create(:active_music_session_no_user_history) }
  let(:user_history1) { FactoryGirl.create(:music_session_user_history, :history => music_session.music_session, :user => music_session.creator) }
  let(:user_history2) { FactoryGirl.create(:music_session_user_history, :history => music_session.music_session, :user => some_user) }

  after {
    Timecop.return
  }
  describe "create" do
    pending
    it {user_history1.music_session_id.should == music_session.id }
    it {user_history1.created_at.should_not be_nil }
    it {user_history1.session_removed_at.should be_nil }
  end

  describe "rating" do

    it "success" do
      user_history1.update_attribute(:rating, 1)
      expect( user_history1.errors.any? ).to eq(false)
    end

    it "out of range"  do
      user_history1.rating = 2
      user_history1.save
      expect( user_history1.errors.any? ).to eq(true)
    end

    it 'should rate success' do
      users = [user_history1, user_history2]
      Timecop.travel(Time.now + (APP_CONFIG.rating_dialog_min_time * 1.5).seconds)
      expect( user_history1.should_rate_session? ).to eq(true)
    end

    it 'should rate fails' do
      users = [user_history1]
      expect( user_history1.should_rate_session? ).to eq(false)
      users = [user_history1, user_history2]
      expect( user_history2.should_rate_session? ).to eq(false)
    end

  end

  describe "end_history" do
    pending
    it "histories created at the same time" do
      user_history1.reload
      user_history2.reload
      user_history1.end_history
      user_history1.max_concurrent_connections.should == 2
    end

    it "history2 ends before history1 starts" do
      user_history2.created_at = user_history1.created_at - 2
      user_history2.session_removed_at = user_history1.created_at - 1
      user_history2.save.should be_true
      user_history1.reload
      user_history2.reload
      user_history1.end_history
      user_history1.max_concurrent_connections.should == 1
    end

    it "history2 starts after history1 starts" do
      user_history2.created_at = user_history1.created_at + 1
      user_history2.session_removed_at = user_history1.created_at + 2
      user_history2.save.should be_true
      user_history1.reload
      user_history2.reload
      user_history1.end_history
      user_history1.max_concurrent_connections.should == 1
    end

    it "history2 is within bounds of history1 " do
      user_history1.session_removed_at = user_history1.created_at + 3
      user_history2.created_at = user_history1.created_at + 1
      user_history2.session_removed_at = user_history1.created_at + 2
      user_history1.save.should be_true
      user_history2.save.should be_true
      user_history1.reload
      user_history2.reload
      user_history1.end_history
      user_history1.max_concurrent_connections.should == 2
    end

    it "two histories with same user within bounds of history1" do
      user_history3 = FactoryGirl.create(:music_session_user_history, :history => music_session.music_session, :user => some_user)

      # if user2 comes and goes 2 times while user one is there, it shouldn't be a false 3
      user_history1.session_removed_at = user_history1.created_at + 5
      user_history2.created_at = user_history1.created_at + 1
      user_history2.session_removed_at = user_history1.created_at + 2
      user_history3.created_at = user_history1.created_at + 3
      user_history3.session_removed_at = user_history1.created_at + 4
      user_history1.save.should be_true
      user_history2.save.should be_true
      user_history3.save.should be_true
      user_history1.reload
      user_history2.reload
      user_history3.reload
      user_history1.end_history
      user_history1.max_concurrent_connections.should == 2
    end

    it "two histories with different user within bounds of history1" do
      third_user = FactoryGirl.create(:user);

      user_history3 = FactoryGirl.create(:music_session_user_history, :history => music_session.music_session, :user => third_user)

      # if user2 comes and goes 2 times while user one is there, it shouldn't be a false 3
      user_history1.session_removed_at = user_history1.created_at + 5
      user_history2.created_at = user_history1.created_at + 1
      user_history2.session_removed_at = user_history1.created_at + 2
      user_history3.created_at = user_history1.created_at + 3
      user_history3.session_removed_at = user_history1.created_at + 4
      user_history1.save.should be_true
      user_history2.save.should be_true
      user_history3.save.should be_true
      user_history1.reload
      user_history2.reload
      user_history3.reload
      user_history1.end_history
      user_history1.max_concurrent_connections.should == 2
    end

    it "two overlapping histories with different user within bounds of history1" do
      third_user = FactoryGirl.create(:user);

      user_history3 = FactoryGirl.create(:music_session_user_history, :history => music_session.music_session, :user => third_user)

      # if user2 comes and goes 2 times while user one is there, it shouldn't be a false 3
      user_history1.session_removed_at = user_history1.created_at + 5
      user_history2.created_at = user_history1.created_at + 1
      user_history2.session_removed_at = user_history1.created_at + 3
      user_history3.created_at = user_history1.created_at + 2
      user_history3.session_removed_at = user_history1.created_at + 4
      user_history1.save.should be_true
      user_history2.save.should be_true
      user_history3.save.should be_true
      user_history1.reload
      user_history2.reload
      user_history3.reload
      user_history1.end_history
      user_history1.max_concurrent_connections.should == 3
    end
  end
end


