require 'spec_helper'

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

  subject { page }

  before(:all) do
    Capybara.javascript_driver = :poltergeist
    Capybara.current_driver = Capybara.javascript_driver
    Capybara.default_max_wait_time = 30 # these tests are SLOOOOOW
  end

  let(:creator) { FactoryGirl.create(:user) }
  let(:joiner1) { FactoryGirl.create(:user) }
  let(:joiner2) { FactoryGirl.create(:user) }
  let(:some_genre) { random_genre }

  before(:each) do
    ActiveMusicSession.delete_all
  end

  # creates a recording, and stops it, and confirms the 'Finished Recording' dialog shows for both
  it "creator start/stop", intermittent: true do
    start_recording_with(creator, [joiner1])
    in_client(creator) { stop_recording }
    check_recording_finished_for([creator, joiner1])
  end

  # confirms that anyone can start/stop a recording
  it "creator starts and other stops" do
    pending "popup problems"
    start_recording_with(creator, [joiner1])
    in_client(joiner1) { stop_recording }
    check_recording_finished_for([creator])
    check_recording_finished_for([joiner1])
  end

  # confirms that a formal leave (by hitting the 'Leave' button) will result in a good recording
  it "creator starts and then leaves", intermittent: true do
    start_recording_with(creator, [joiner1])
    in_client(creator) do
      find('.session-leave.leave').trigger(:click)
      expect(page).to have_selector('h2',    text: 'feed')
    end

    formal_leave_by creator
    check_recording_finished_for [creator, joiner1]
  end

  # confirms that if someone leaves 'ugly' (without calling 'Leave' REST API), that the recording is junked
  it "creator starts and then abruptly leave" do
    pending "shows 'recording finished'"
    start_recording_with(creator, [joiner1])

    in_client(creator) do
      visit "/request_reset_password" # kills websocket, looking like an abrupt leave
    end

    in_client(joiner1) do
      find('#notification').should have_content 'Recording Discarded'
      find('#notification').should have_content 'did not respond to the stop signal'
      find('#recording-status').should have_content 'Make Recording'
    end
  end

  it "creator starts/stops, with 3 total participants" do
    start_recording_with(creator, [joiner1, joiner2])

    in_client(creator) do
      stop_recording
    end

    check_recording_finished_for [creator, joiner1, joiner2]
  end

  it "creator starts with session leave to stop, with 3 total participants" do
    start_recording_with(creator, [joiner1, joiner2])

    formal_leave_by creator
    check_recording_finished_for [creator, joiner1, joiner2]
  end


  # confirms that if someone leaves 'ugly' (without calling 'Leave' REST API), that the recording is junked with 3 participants
  it "creator starts and then abruptly leave with 3 participants" do
    pending "shows 'recording finished'"
    start_recording_with(creator, [joiner1, joiner2])

    in_client(creator) do
      visit "/request_reset_password" # kills websocket, looking like an abrupt leave
    end

    in_client(joiner1) do
      find('#notification').should have_content 'Recording Discarded'
      find('#notification').should have_content 'did not respond to the stop signal'
      find('#recording-status').should have_content 'Make Recording'
    end

    in_client(joiner2) do
      find('#notification').should have_content 'Recording Discarded'
      find('#notification').should have_content 'did not respond to the stop signal'
      find('#recording-status').should have_content 'Make Recording'
    end
  end
  # POPUP PROBLEMS

=begin

  describe "mixing after the recording" do
    before(:each) do

      RecordedTrack.delete_all
      ClaimedRecording.delete_all
      MusicSession.delete_all
      ActiveMusicSession.delete_all
      QuickMix.delete_all

      start_recording_with(creator, [joiner1], some_genre)

      in_client(joiner1) do
        stop_recording
      end

      @users = [creator, joiner1]
      check_recording_finished_for @users

      @music_session = ActiveMusicSession.first()
      @recording = @music_session.recordings.first()

      @users.each do |user|
        name = "#{user.name}'s recording"
        desc = "#{user.name}'s description"

        in_client(user) do
          claim_recording(name, desc)
          claimed_recording = @recording.claimed_recordings.where(:user_id => user.id).first
          expect(claimed_recording.name).to eq(name)
          expect(claimed_recording.description).to eq(desc)
          expect(claimed_recording.is_public).to be true
          expect(claimed_recording.genre).to eq(@music_session.genre)
        end
      end
    end

    it "notifications show" do
      quick_mix = QuickMix.first
      quick_mix.finish(100, 'abc') # should send out notifications to all people who claimed the recording

      in_client(creator) do
        find('#notification h2', text: 'Recording Stream Mix Complete')
        find('#notification #buttons a', text: 'SHARE').trigger(:click)
      end

      in_client(joiner1) do
        find('#notification h2', text: 'Recording Stream Mix Complete')
        find('#notification #buttons a', text: 'SHARE').trigger(:click)
        find('h1', text: 'share this recording')
        find('.dialog-inner a.button-orange[layout-action="close"]', text: 'X CLOSE').trigger(:click)

        open_notifications

        notification = Notification.where(target_user_id: joiner1.id, recording_id: @recording.id).first!

        find("li[notification-id='#{notification.id}'] a#btn-notification-action", text: 'SHARE').trigger(:click)
        find('h1', text: 'share this recording')
        find('.dialog-inner a.button-orange[layout-action="close"]', text: 'X CLOSE').trigger(:click)

      end
    end
  end

  # creates a recording, and stops it, and confirms the 'Finished Recording' dialog shows for both
  describe "Finished Recording Dialog" do
    before(:each) do

      RecordedTrack.delete_all
      ClaimedRecording.delete_all
      MusicSession.delete_all
      ActiveMusicSession.delete_all
      QuickMix.delete_all

      start_recording_with(creator, [joiner1], some_genre)

      in_client(joiner1) do
        stop_recording
      end

      @users = [creator, joiner1]
      check_recording_finished_for @users
    end

    it "discard the recording" do
      # pending "fails intermittently on the build server"
      in_client(creator) do
        find('#recording-finished-dialog h1')
        find('#discard-session-recording').trigger(:click)
        should have_no_selector('h1',  text: 'recording finished')
        music_session = ActiveMusicSession.first()
        recording = music_session.recordings.first()
        tracks = recording.recorded_tracks_for_user(creator)
        tracks.length.should == 1
        tracks[0].discard.should be true
      end

      in_client(joiner1) do
        find('#recording-finished-dialog h1')
        find('#discard-session-recording').trigger(:click)
        should have_no_selector('h1',  text: 'recording finished')
        music_session = ActiveMusicSession.first()
        recording = music_session.recordings.first()
        tracks = recording.recorded_tracks_for_user(joiner1)
        tracks.length.should == 1
        tracks[0].discard.should be true
      end
    end

    it "claim recording with unique names/descriptions" do
      # pending "intermittent failure on build server, hard to repro on local system"
      @users.each do |user|
        name = "#{user.name}'s recording"
        desc = "#{user.name}'s description"

        in_client(user) do
          claim_recording(name, desc)
          music_session = ActiveMusicSession.first()
          recording = music_session.recordings.first()
          claimed_recording = recording.claimed_recordings.where(:user_id => user.id).first
          expect(claimed_recording.name).to eq(name)
          expect(claimed_recording.description).to eq(desc)
          expect(claimed_recording.is_public).to be true
          expect(claimed_recording.genre).to eq(music_session.genre)
        end
      end
    end

    it "a 'Recording Name' is required" do
      @users.each do |user|
        in_client(user) do
          find('#recording-finished-dialog h1')
          fill_in "claim-recording-name",    with: ''
          fill_in "claim-recording-description",    with: "my description"
          find('#keep-session-recording').trigger(:click)
          should have_content("can't be blank")
		  save_screenshot("tmp/name#{user.name}.png")
        end
      end
    end

    it "a 'Description' is optional" do
      # pending "intermittent failure on build server, hard to repro on local system"
      @users.each do |user|
        in_client(user) do
          claim_recording("my recording", '')
          music_session = ActiveMusicSession.first()
          recording = music_session.recordings.first()
          claimed_recording = recording.claimed_recordings.where(:user_id => user.id).first
          expect(claimed_recording.name).to eq("my recording")
          expect(claimed_recording.description).to eq('')
          expect(claimed_recording.is_public).to be true
          expect(claimed_recording.genre).to eq(music_session.genre)
        end
      end
    end

    it "genre is pre-set with the genre selected for the session" do
      @users.each do |user|
        in_client(user) do
          find('#recording-finished-dialog h1')
          g = selected_genres('#recording-finished-dialog div.genre-selector')
          expect(some_genre).to match(%r{#{g}}i)
        end
      end
    end
  end

=end

end


