require 'spec_helper'

describe MusicSessionManager do

  let (:music_session) { FactoryGirl.create(:music_session) }

  before(:each) do
    @music_session_manager = MusicSessionManager.new(:conn => @conn)
  end

  describe "manage session" do
    
    before(:each) do
      @user = FactoryGirl.create(:user, :musician => true)
      @band = FactoryGirl.create(:band)
      @genre = FactoryGirl.create(:genre)
      @instrument = FactoryGirl.create(:instrument)
      @tracks = [{"instrument_id" => @instrument.id, "sound" => "mono", "client_track_id" => "abcd"}]
      @connection = FactoryGirl.create(:connection, :user => @user)
    end

    it "creates a session properly" do
      active_music_session = @music_session_manager.create(music_session, @user, @connection.client_id, "description", true, false, true, true, @band, [@genre], @tracks, true, 10)
      
      ActiveMusicSession.find(active_music_session.id) # shouldn't throw an exception
    end
    
    it "updates a session properly" do
      active_music_session = @music_session_manager.create(music_session, @user, @connection.client_id, "description", true, false, true, true, @band, [@genre], @tracks, true, 10)
      @music_session_manager.update(music_session, nil, "updated description", nil, nil, nil, nil, nil, nil)
      music_session.reload
      music_session.description.should == "updated description"
      # Verify that this didnt change
      music_session.approval_required.should == false
      genre2 = FactoryGirl.create(:genre)
      @music_session_manager.update(music_session, nil, nil, @genre, nil, nil, nil, nil, nil)
      music_session.reload
      music_session.description.should == "updated description"
      music_session.genre.should == @genre
    end

    it "deletes a session properly" do
      active_music_session = @music_session_manager.create(music_session, @user, @connection.client_id, "description", true, false, true, true, @band, [@genre], @tracks, true, 10)
      music_session = MusicSession.find_by_music_session_id(active_music_session.id)
      music_session.should_not be_nil
      active_music_session.destroy
      music_session.reload
      music_session.session_removed_at.should_not == nil
    end
    
  end

end
