require 'spec_helper'

describe MusicSessionManager do

  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"}]
      @connection = FactoryGirl.create(:connection, :user => @user)
    end

    it "creates a session properly" do
      music_session = @music_session_manager.create(@user, @connection.client_id, "description", true, false, true, true, @band, [@genre], @tracks, true)
      
      MusicSession.count.should == 1
      MusicSession.first.id.should == music_session.id
    end
    
    it "updates a session properly" do
      music_session = @music_session_manager.create(@user, @connection.client_id, "description", true, false, true, true, @band, [@genre], @tracks, true)
      @music_session_manager.update(music_session, "updated description", 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, [@genre, genre2], nil, nil, nil, nil)
      music_session.reload
      music_session.description.should == "updated description"
      music_session.genres.count.should == 2
      music_session.genres.should == [@genre, genre2]
    end

    it "deletes a session properly" do
      music_session = @music_session_manager.create(@user, @connection.client_id, "description", true, false, true, true, @band, [@genre], @tracks, true)
      MusicSessionHistory.count.should == 1
      MusicSessionHistory.first.music_session_id.should == music_session.id
      music_session.destroy
      MusicSessionHistory.first.session_removed_at.should_not == nil
    end
    
  end

end
