spec/punchblock/translator/asterisk/component/asterisk/agi_command_spec.rb in punchblock-1.8.2 vs spec/punchblock/translator/asterisk/component/asterisk/agi_command_spec.rb in punchblock-1.9.0

- old
+ new

@@ -9,82 +9,79 @@ module Asterisk describe AGICommand do include HasMockCallbackConnection let(:channel) { 'SIP/foo' } - let(:translator) { Punchblock::Translator::Asterisk.new mock('AMI'), connection } - let(:mock_call) { Punchblock::Translator::Asterisk::Call.new channel, translator } + let(:ami_client) { stub('AMI Client').as_null_object } + let(:translator) { Punchblock::Translator::Asterisk.new ami_client, connection } + let(:mock_call) { Punchblock::Translator::Asterisk::Call.new channel, translator, ami_client, connection } let(:component_id) { Punchblock.new_uuid } before { stub_uuids component_id } let :original_command do Punchblock::Component::Asterisk::AGI::Command.new :name => 'EXEC ANSWER' end subject { AGICommand.new original_command, mock_call } - let :expected_action do - RubyAMI::Action.new 'AGI', 'Channel' => channel, 'Command' => 'EXEC ANSWER', 'CommandID' => component_id + let :response do + RubyAMI::Response.new end context 'initial execution' do before { original_command.request! } - it 'should send the appropriate RubyAMI::Action' do - mock_call.should_receive(:send_ami_action).once.with(expected_action).and_return(expected_action) + it 'should send the appropriate action' do + ami_client.should_receive(:send_action).once.with('AGI', 'Channel' => channel, 'Command' => 'EXEC ANSWER', 'CommandID' => component_id).and_return(response) subject.execute end context 'with some parameters' do let(:params) { [1000, 'foo'] } - let :expected_action do - RubyAMI::Action.new 'AGI', 'Channel' => channel, 'Command' => 'WAIT FOR DIGIT "1000" "foo"', 'CommandID' => component_id - end - let :original_command do Punchblock::Component::Asterisk::AGI::Command.new :name => 'WAIT FOR DIGIT', :params => params end - it 'should send the appropriate RubyAMI::Action' do - mock_call.should_receive(:send_ami_action).once.with(expected_action).and_return(expected_action) + it 'should send the appropriate action' do + ami_client.should_receive(:send_action).once.with('AGI', 'Channel' => channel, 'Command' => 'WAIT FOR DIGIT "1000" "foo"', 'CommandID' => component_id).and_return(response) subject.execute end end end context 'when the AMI action completes' do before do original_command.request! - original_command.execute! end let :expected_response do Ref.new :id => component_id end let :response do - RubyAMI::Response.new.tap do |r| - r['ActionID'] = "552a9d9f-46d7-45d8-a257-06fe95f48d99" - r['Message'] = 'Added AGI original_command to queue' - end + RubyAMI::Response.new 'ActionID' => "552a9d9f-46d7-45d8-a257-06fe95f48d99", + 'Message' => 'Added AGI original_command to queue' end it 'should send the component node a ref with the action ID' do - original_command.should_receive(:response=).once.with(expected_response) - subject.action << response + ami_client.should_receive(:send_action).once.and_return response + subject.execute + original_command.response(1).should eql(expected_response) end context 'with an error' do - let :error do + let :response do RubyAMI::Error.new.tap { |e| e.message = 'Action failed' } end it 'should send the component node false' do - original_command.should_receive(:response=).once.with false - subject.action << error + ami_client.should_receive(:send_action).once.and_raise response + subject.execute + original_command.response(1).should be_false + subject.should_not be_alive end end end describe 'when receiving an AsyncAGI event' do @@ -95,17 +92,16 @@ context 'of type start' context 'of type Exec' do let(:ami_event) do - RubyAMI::Event.new("AsyncAGI").tap do |e| - e["SubEvent"] = "Exec" - e["Channel"] = channel - e["CommandId"] = component_id - e["Command"] = "EXEC ANSWER" - e["Result"] = "200%20result=123%20(timeout)%0A" - end + RubyAMI::Event.new 'AsyncAGI', + "SubEvent" => "Exec", + "Channel" => channel, + "CommandId" => component_id, + "Command" => "EXEC ANSWER", + "Result" => "200%20result=123%20(timeout)%0A" end let :expected_complete_reason do Punchblock::Component::Asterisk::AGI::Command::Complete::Success.new :code => 200, :result => 123, @@ -119,9 +115,41 @@ original_command.should be_complete complete_event.component_id.should be == component_id.to_s complete_event.reason.should be == expected_complete_reason + end + + context "when the command was ASYNCAGI BREAK" do + let :original_command do + Punchblock::Component::Asterisk::AGI::Command.new :name => 'ASYNCAGI BREAK' + end + + let(:chan_var) { nil } + + before do + response = RubyAMI::Response.new 'Value' => chan_var + ami_client.should_receive(:send_action).once.with('GetVar', 'Channel' => channel, 'Variable' => 'PUNCHBLOCK_END_ON_ASYNCAGI_BREAK').and_return response + end + + it 'should not send an end (hangup) event to the translator' do + translator.should_receive(:handle_pb_event).once.with kind_of(Punchblock::Event::Complete) + translator.should_receive(:handle_pb_event).never.with kind_of(Punchblock::Event::End) + subject.handle_ami_event ami_event + end + + context "when the PUNCHBLOCK_END_ON_ASYNCAGI_BREAK channel var is set" do + let(:chan_var) { 'true' } + + it 'should send an end (hangup) event to the translator' do + expected_end_event = Punchblock::Event::End.new reason: :hangup, + target_call_id: mock_call.id + + translator.should_receive(:handle_pb_event).once.with kind_of(Punchblock::Event::Complete) + translator.should_receive(:handle_pb_event).once.with expected_end_event + subject.handle_ami_event ami_event + end + end end end end end end