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(:active_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

  describe "double MQRouter" do
    before(:all) do
      @original_client_exchange = MQRouter.client_exchange
      @original_user_exchange = MQRouter.user_exchange
      @original_subscription_exchange = MQRouter.subscription_exchange
    end

    after(:all) do
      MQRouter.client_exchange = @original_client_exchange
      MQRouter.user_exchange = @original_user_exchange
      MQRouter.subscription_exchange = @original_subscription_exchange
    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(:active_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")


      # this is necessary because other tests will call EM.schedule indirectly as they fiddle with AR models, since some of our
      # notifications are tied to model activity.    So, the issue here is that you'll have an unknown known amount of
      # queued up messages ready to be sent to MQRouter (because EM.schedule will put deferred blocks onto @next_tick_queue),
      # resulting in messages from other tests being sent to client_exchange or user_exchange

      # there is no API I can see to clear out the EM queue.  so just open up the EM module and do it manually
      module EM
        @next_tick_queue = []
      end

      # bad thing about a static singleton is that we have to 'repair' it by taking back off the double
      EM.run do

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

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

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

        EM.stop
      end
    end
  end

end
