require 'spec_helper'

describe GoogleAnalyticsEvent do

  let(:ga) { GoogleAnalyticsEvent.new }

  after(:each) do
    Timecop.return
  end

  describe "track band analytics" do
    it 'reports first recording' do
      pending "job is commented out"
      ResqueSpec.reset!
      user = FactoryGirl.create(:user)
      band = FactoryGirl.create(:band)
      music_session = FactoryGirl.create(:active_music_session,
                                         :creator => user, 
                                         :musician_access => true, 
                                         :band => band)
      recording = Recording.start(music_session, user)
      expect(Recording.where(:band_id => band.id).count).to eq(1)

      GoogleAnalyticsEvent.should have_queued(GoogleAnalyticsEvent::CAT_BAND, 
                                              GoogleAnalyticsEvent::ACTION_BAND_REC,
                                              nil)
    end

    it 'reports first real session' do
      pending "job is commented out"
      ResqueSpec.reset!
      JamRuby::GoogleAnalyticsEvent::BandSessionTracker.should have_schedule_size_of(0)
      user = FactoryGirl.create(:user)
      user1 = FactoryGirl.create(:user)
      band = FactoryGirl.create(:band)
      band.users << user
      band.users << user1
      band.reload
      music_session = FactoryGirl.create(:active_music_session, :creator => user,
                                         :musician_access => true, :band => band)
      expect(band.band_musicians.count).to eq(2)
      expect(band.did_real_session).to eq(false)
      connection = FactoryGirl.create(:connection, :user => user, :as_musician => true, 
                                      :aasm_state => Connection::CONNECT_STATE.to_s, 
                                      :music_session => music_session)
      connection = FactoryGirl.create(:connection, :user => user1, :as_musician => true, 
                                      :aasm_state => Connection::CONNECT_STATE.to_s, 
                                      :music_session => music_session)
      music_session.reload
      expect(music_session.connected_participant_count).to eq(2)
      expect(band.did_real_session).to eq(false)

      ResqueSpec.queues["#{GoogleAnalyticsEvent::QUEUE_BAND_TRACKER}_scheduled"].select do |qq|
        qq[:class] == GoogleAnalyticsEvent::BandSessionTracker.name
      end.count.should eq(1)
      # GoogleAnalyticsEvent::BandSessionTracker.should have_schedule_size_of_at_least(1)
      GoogleAnalyticsEvent.should_not have_queued(GoogleAnalyticsEvent::CAT_BAND, GoogleAnalyticsEvent::ACTION_BAND_SESS, nil)
      Timecop.freeze((GoogleAnalyticsEvent::BAND_SESSION_MIN_DURATION + 1).minutes.from_now)

      qname = "#{ResqueSpec.queue_name(JamRuby::GoogleAnalyticsEvent::BandSessionTracker)}_scheduled"
      expect(ResqueSpec.peek(qname).present?).to eq(true)
      ResqueSpec.perform_next(qname)
      GoogleAnalyticsEvent.should have_queued(GoogleAnalyticsEvent::CAT_BAND, 
                                              GoogleAnalyticsEvent::ACTION_BAND_SESS, 
                                              nil)
      band.reload
      expect(band.did_real_session).to eq(true)
    end

  end

  describe "track session analytics" do
    before :each do
      ResqueSpec.reset!
    end
    it 'reports size increment' do
      pending "job is commented out"
      user = FactoryGirl.create(:user)
      music_session = FactoryGirl.create(:active_music_session,
                                         :creator => user, 
                                         :musician_access => true)
      connection = FactoryGirl.create(:connection, :user => user, 
                                      :as_musician => true, 
                                      :aasm_state => Connection::CONNECT_STATE.to_s, 
                                      :music_session => music_session)
      GoogleAnalyticsEvent.should have_queued(GoogleAnalyticsEvent::CAT_SESS_SIZE, 
                                              GoogleAnalyticsEvent::ACTION_SESS_SIZE,
                                              music_session.connected_participant_count)
    end

    it 'reports duration' do
      pending "job is commented out"
      user = FactoryGirl.create(:user)
      JamRuby::GoogleAnalyticsEvent::SessionDurationTracker.should have_schedule_size_of(0)
      music_session = FactoryGirl.create(:active_music_session,
                                         :creator => user, 
                                         :musician_access => true)
      GoogleAnalyticsEvent::SessionDurationTracker.should have_schedule_size_of(1)

      GoogleAnalyticsEvent::SESSION_INTERVALS.each do |interval|
        Timecop.travel((interval + 1).minutes.from_now)
        qname = "#{ResqueSpec.queue_name(JamRuby::GoogleAnalyticsEvent::SessionDurationTracker)}_scheduled"
        next unless ResqueSpec.peek(qname).present?
        ResqueSpec.perform_next(qname)
        GoogleAnalyticsEvent.should have_queued(GoogleAnalyticsEvent::CAT_SESS_DUR, 
                                                GoogleAnalyticsEvent::ACTION_SESS_DUR,
                                                interval)
      end
      GoogleAnalyticsEvent.should have_queue_size_of(GoogleAnalyticsEvent::SESSION_INTERVALS.count - 1)
    end
  end

end
