require 'spec_helper'

describe MQRouter do

  before do
    @mq_router = MQRouter.new()
  end

  it "user_publish_to_session works (but faking MQ)" do

    user1 = FactoryGirl.create(:user)  # in the jam session
    user2 = FactoryGirl.create(:user)  # in the jam session

    music_session = FactoryGirl.create(:music_session, :creator => user1)

    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1")
    music_session_member2 = FactoryGirl.create(:connection, :user => user2, :music_session => music_session, :ip_address => "2.2.2.2", :client_id => "2")

    @mq_router.should_receive(:publish_to_session).with(music_session.id, [music_session_member2.client_id], "a message", :client_id => music_session_member1.client_id)

    @mq_router.user_publish_to_session(music_session, user1, "a message" ,:client_id => music_session_member1.client_id)
  end

  it "user_publish_to_session works (checking exchange callbacks)" do

    user1 = FactoryGirl.create(:user)  # in the jam session
    user2 = FactoryGirl.create(:user)  # in the jam session

    music_session = FactoryGirl.create(:music_session, :creator => user1)

    music_session_member1 = FactoryGirl.create(:connection, :user => user1, :music_session => music_session, :ip_address => "1.1.1.1", :client_id => "1")
    music_session_member2 = FactoryGirl.create(:connection, :user => user2, :music_session => music_session, :ip_address => "2.2.2.2", :client_id => "2")

    EM.run do

      # mock up exchange
      MQRouter.client_exchange = double("client_exchange")

      MQRouter.client_exchange.should_receive(:publish).with("a message", :routing_key => "client.#{music_session_member2.client_id}")

      @mq_router.user_publish_to_session(music_session, user1, "a message", :client_id => music_session_member1.client_id)

      EM.stop
    end
  end

end
