# encoding: utf-8 require 'spec_helper' module Punchblock module Connection describe Asterisk do let :options do { :host => '127.0.0.1', :port => 5038, :username => 'test', :password => 'test' } end let(:mock_event_handler) { double('Event Handler').as_null_object } subject(:connection) { Asterisk.new options } before do connection.event_handler = mock_event_handler end describe '#ami_client' do subject { connection.ami_client } it { should be_a RubyAMIStreamProxy } end describe '#ami_client' do describe '#stream' do subject { connection.ami_client.stream } it { should be_a RubyAMI::Stream } end end it 'should set the connection on the translator' do expect(subject.translator.connection).to be subject end describe '#run' do it 'starts the RubyAMI::Stream' do expect(subject.ami_client.async).to receive(:run).once do subject.ami_client.terminate end expect { subject.run }.to raise_error DisconnectedError end it 'rebuilds the RubyAMI::Stream if dead' do pending expect(subject.ami_client.async).to receive(:run).once do subject.ami_client.terminate end expect { subject.run }.to raise_error DisconnectedError expect(subject.ami_client.alive?).to be_false expect(subject).to receive(:new_ami_stream).once.and_return do expect(subject.ami_client.alive?).to be true expect(subject.ami_client.async).to receive(:run).once end expect { subject.run }.not_to raise_error end end describe '#stop' do it 'stops the RubyAMI::Stream' do expect(subject.ami_client).to receive(:terminate).once subject.stop end it 'shuts down the translator' do expect(subject.translator).to receive(:terminate).once subject.stop end end it 'sends events from RubyAMI to the translator' do event = RubyAMI::Event.new 'FullyBooted' expect(subject.translator.async).to receive(:handle_ami_event).once.with event expect(subject.translator.async).to receive(:handle_ami_event).once.with RubyAMI::Stream::Disconnected.new subject.ami_client.message_received event end describe '#write' do it 'sends a command to the translator' do command = double 'Command' options = {:foo => :bar} expect(subject.translator.async).to receive(:execute_command).once.with command, options subject.write command, options end end describe 'when a rayo event is received from the translator' do it 'should call the event handler with the event' do offer = Event::Offer.new offer.target_call_id = '9f00061' expect(mock_event_handler).to receive(:call).once.with offer subject.handle_event offer end end describe '#new_call_uri' do it "should return a random UUID" do stub_uuids 'foobar' expect(subject.new_call_uri).to eq('foobar') end end describe '#send_message' do it 'passes the message to the translator for dispatch' do expect(subject.translator).to receive(:send_message).once.with(:foo) subject.send_message :foo end end end end end