require 'spec_helper'

describe "ActiveMusicSessionCleaner" do

  before(:all) do
    MusicSession.delete_all
    ActiveMusicSession.delete_all
    @cleaner = ActiveMusicSessionCleaner.new
    @cleaner.interval = "INTERVAL '1 second'"
  end

  describe "active session cleaning" do

    it "should remove active sessions that meet criteria" do
      ms = FactoryGirl.create(:single_user_session)
      ActiveMusicSession.all.count.should == 1

      @cleaner.run
      ActiveMusicSession.all.count.should == 1

      # delete all connections
      ms.connections.each do |c|
        c.delete
      end

      # hasn't been 1 second yet
      @cleaner.run
      ActiveMusicSession.all.count.should == 1
      ams = ActiveMusicSession.first
      ams.updated_at = 3.seconds.ago
      ams.save!

      @cleaner.run
      ActiveMusicSession.all.count.should == 0

    end
  end
end