<commit_before># encoding: utf-8

require 'spec_helper'

module Punchblock
  module Translator
    class Asterisk
      describe Call do
        let(:channel)         { 'SIP/foo' }
        let(:translator)      { stub_everything 'Translator::Asterisk' }
        let(:agi_env) do
          {
            :agi_request      => 'async',
            :agi_channel      => 'SIP/1234-00000000',
            :agi_language     => 'en',
            :agi_type         => 'SIP',
            :agi_uniqueid     => '1320835995.0',
            :agi_version      => '1.8.4.1',
            :agi_callerid     => '5678',
            :agi_calleridname => '<NAME>',
            :agi_callingpres  => '0',
            :agi_callingani2  => '0',
            :agi_callington   => '0',
            :agi_callingtns   => '0',
            :agi_dnid         => '1000',
            :agi_rdnis        => 'unknown',
            :agi_context      => 'default',
            :agi_extension    => '1000',
            :agi_priority     => '1',
            :agi_enhanced     => '0.0',
            :agi_accountcode  => '',
            :agi_threadid     => '4366221312'
          }
        end

        let :sip_headers do
          {
            :x_agi_request      => 'async',
            :x_agi_channel      => 'SIP/1234-00000000',
            :x_agi_language     => 'en',
            :x_agi_type         => 'SIP',
            :x_agi_uniqueid     => '1320835995.0',
            :x_agi_version      => '1.8.4.1',
            :x_agi_callerid     => '5678',
            :x_agi_calleridname => '<NAME>',
            :x_agi_callingpres  => '0',
            :x_agi_callingani2  => '0',
            :x_agi_callington   => '0',
            :x_agi_callingtns   => '0',
            :x_agi_dnid         => '1000',
            :x_agi_rdnis        => 'unknown',
            :x_agi_context      => 'default',
            :x_agi_extension    => '1000',
            :x_agi_priority     => '1',
            :x_agi_enhanced     => '0.0',
            :x_agi_accountcode  => '',
            :x_agi_threadid     => '4366221312'
          }
        end

        subject { Call.new channel, translator, agi_env }

        its(:id)          { should be_a String }
        its(:channel)     { should be == channel }
        its(:translator)  { should be translator }
        its(:agi_env)     { should be == agi_env }

        describe '#shutdown' do
          it 'should terminate the actor' do
            subject.shutdown
            sleep 0.5
            subject.should_not be_alive
          end
        end

        describe '#register_component' do
          it 'should make the component accessible by ID' do
            component_id = 'abc123'
            component    = mock 'Translator::Asterisk::Component', :id => component_id
            subject.register_component component
            subject.component_with_id(component_id).should be component
          end
        end

        describe '#send_offer' do
          it 'sends an offer to the translator' do
            expected_offer = Punchblock::Event::Offer.new :call_id  => subject.id,
                                                          :to       => '1000',
                                                          :from     => 'sip:5678',
                                                          :headers  => sip_headers
            translator.expects(:handle_pb_event!).with expected_offer
            subject.send_offer
          end

          it 'should make the call identify as inbound' do
            subject.send_offer
            subject.direction.should be == :inbound
            subject.inbound?.should be true
            subject.outbound?.should be false
          end
        end

        describe '#answer_if_not_answered' do
          let(:answer_command) { Command::Answer.new.tap { |a| a.request! } }

          context "with a call that is already answered" do
            it 'should not answer the call' do
              subject.wrapped_object.expects(:'answered?').returns true
              subject.wrapped_object.expects(:execute_command).never
              subject.answer_if_not_answered
            end
          end

          context "with an unanswered call" do
            before do
              subject.wrapped_object.expects(:'answered?').returns false
            end

            context "with a call that is outbound" do
              let(:dial_command) { Command::Dial.new }

              before do
                dial_command.request!
                subject.dial dial_command
              end

              it 'should not answer the call' do
                subject.wrapped_object.expects(:execute_command).never
                subject.answer_if_not_answered
              end
            end

            context "with a call that is inbound" do
              before do
                subject.send_offer
              end

              it 'should answer a call that is inbound and not answered' do
                subject.wrapped_object.expects(:execute_command).with(answer_command)
                subject.answer_if_not_answered
              end
            end
          end
        end

        describe '#dial' do
          let(:dial_command_options) { {} }

          let :dial_command do
            Punchblock::Command::Dial.new({:to => 'SIP/1234', :from => 'sip:<EMAIL>'}.merge(dial_command_options))
          end

          before { dial_command.request! }

          it 'sends an Originate AMI action' do
            expected_action = Punchblock::Component::Asterisk::AMI::Action.new :name => 'Originate',
                                                                               :params => {
                                                                                 :async       => true,
                                                                                 :application => 'AGI',
                                                                                 :data        => 'agi:async',
                                                                                 :channel     => 'SIP/1234',
                                                                                 :callerid    => 'sip:<EMAIL>',
                                                                                 :variable    => "punchblock_call_id=#{subject.id}"
                                                                               }

            translator.expects(:execute_global_command!).once.with expected_action
            subject.dial dial_command
          end

          context 'with a timeout specified' do
            let :dial_command_options do
              { :timeout => 10000 }
            end

            it 'includes the timeout in the Originate AMI action' do
              expected_action = Punchblock::Component::Asterisk::AMI::Action.new :name => 'Originate',
                                                                                 :params => {
                                                                                   :async       => true,
                                                                                   :application => 'AGI',
                                                                                   :data        => 'agi:async',
                                                                                   :channel     => 'SIP/1234',
                                                                                   :callerid    => 'sip:<EMAIL>',
                                                                                   :variable    => "punchblock_call_id=#{subject.id}",
                                                                                   :timeout     => 10000
                                                                                 }

              translator.expects(:execute_global_command!).once.with expected_action
              subject.dial dial_command
            end
          end

          it 'sends the call ID as a response to the Dial' do
            subject.dial dial_command
            dial_command.response
            dial_command.call_id.should be == subject.id
          end

          it 'should make the call identify as outbound' do
            subject.dial dial_command
            subject.direction.should be == :outbound
            subject.outbound?.should be true
            subject.inbound?.should be false
          end

          it 'causes accepting the call to be a null operation' do
            subject.dial dial_command
            accept_command = Command::Accept.new
            accept_command.request!
            subject.wrapped_object.expects(:send_agi_action).never
            subject.execute_command accept_command
            accept_command.response(0.5).should be true
          end
        end<commit_msg>[BUGFIX] Some tests were incorrectly asserting command equality<commit_after># encoding: utf-8

require 'spec_helper'

module Punchblock
  module Translator
    class Asterisk
      describe Call do
        let(:channel)         { 'SIP/foo' }
        let(:translator)      { stub_everything 'Translator::Asterisk' }
        let(:agi_env) do
          {
            :agi_request      => 'async',
            :agi_channel      => 'SIP/1234-00000000',
            :agi_language     => 'en',
            :agi_type         => 'SIP',
            :agi_uniqueid     => '1320835995.0',
            :agi_version      => '1.8.4.1',
            :agi_callerid     => '5678',
            :agi_calleridname => '<NAME>',
            :agi_callingpres  => '0',
            :agi_callingani2  => '0',
            :agi_callington   => '0',
            :agi_callingtns   => '0',
            :agi_dnid         => '1000',
            :agi_rdnis        => 'unknown',
            :agi_context      => 'default',
            :agi_extension    => '1000',
            :agi_priority     => '1',
            :agi_enhanced     => '0.0',
            :agi_accountcode  => '',
            :agi_threadid     => '4366221312'
          }
        end

        let :sip_headers do
          {
            :x_agi_request      => 'async',
            :x_agi_channel      => 'SIP/1234-00000000',
            :x_agi_language     => 'en',
            :x_agi_type         => 'SIP',
            :x_agi_uniqueid     => '1320835995.0',
            :x_agi_version      => '1.8.4.1',
            :x_agi_callerid     => '5678',
            :x_agi_calleridname => '<NAME>',
            :x_agi_callingpres  => '0',
            :x_agi_callingani2  => '0',
            :x_agi_callington   => '0',
            :x_agi_callingtns   => '0',
            :x_agi_dnid         => '1000',
            :x_agi_rdnis        => 'unknown',
            :x_agi_context      => 'default',
            :x_agi_extension    => '1000',
            :x_agi_priority     => '1',
            :x_agi_enhanced     => '0.0',
            :x_agi_accountcode  => '',
            :x_agi_threadid     => '4366221312'
          }
        end

        subject { Call.new channel, translator, agi_env }

        its(:id)          { should be_a String }
        its(:channel)     { should be == channel }
        its(:translator)  { should be translator }
        its(:agi_env)     { should be == agi_env }

        describe '#shutdown' do
          it 'should terminate the actor' do
            subject.shutdown
            sleep 0.5
            subject.should_not be_alive
          end
        end

        describe '#register_component' do
          it 'should make the component accessible by ID' do
            component_id = 'abc123'
            component    = mock 'Translator::Asterisk::Component', :id => component_id
            subject.register_component component
            subject.component_with_id(component_id).should be component
          end
        end

        describe '#send_offer' do
          it 'sends an offer to the translator' do
            expected_offer = Punchblock::Event::Offer.new :call_id  => subject.id,
                                                          :to       => '1000',
                                                          :from     => 'sip:5678',
                                                          :headers  => sip_headers
            translator.expects(:handle_pb_event!).with expected_offer
            subject.send_offer
          end

          it 'should make the call identify as inbound' do
            subject.send_offer
            subject.direction.should be == :inbound
            subject.inbound?.should be true
            subject.outbound?.should be false
          end
        end

        describe '#answer_if_not_answered' do
          let(:answer_command) { Command::Answer.new.tap { |a| a.request! } }

          context "with a call that is already answered" do
            it 'should not answer the call' do
              subject.wrapped_object.expects(:'answered?').returns true
              subject.wrapped_object.expects(:execute_command).never
              subject.answer_if_not_answered
            end
          end

          context "with an unanswered call" do
            before do
              subject.wrapped_object.expects(:'answered?').returns false
            end

            context "with a call that is outbound" do
              let(:dial_command) { Command::Dial.new }

              before do
                dial_command.request!
                subject.dial dial_command
              end

              it 'should not answer the call' do
                subject.wrapped_object.expects(:execute_command).never
                subject.answer_if_not_answered
              end
            end

            context "with a call that is inbound" do
              before do
                subject.send_offer
              end

              it 'should answer a call that is inbound and not answered' do
                subject.wrapped_object.expects(:execute_command).with(answer_command)
                subject.answer_if_not_answered
              end
            end
          end
        end

        describe '#dial' do
          let(:dial_command_options) { {} }

          let :dial_command do
            Punchblock::Command::Dial.new({:to => 'SIP/1234', :from => 'sip:<EMAIL>'}.merge(dial_command_options))
          end

          before { dial_command.request! }

          it 'sends an Originate AMI action' do
            expected_action = Punchblock::Component::Asterisk::AMI::Action.new(:name => 'Originate',
                                                                               :params => {
                                                                                 :async       => true,
                                                                                 :application => 'AGI',
                                                                                 :data        => 'agi:async',
                                                                                 :channel     => 'SIP/1234',
                                                                                 :callerid    => 'sip:<EMAIL>',
                                                                                 :variable    => "punchblock_call_id=#{subject.id}"
                                                                               }).tap { |a| a.request! }

            translator.expects(:execute_global_command!).once.with expected_action
            subject.dial dial_command
          end

          context 'with a timeout specified' do
            let :dial_command_options do
              { :timeout => 10000 }
            end

            it 'includes the timeout in the Originate AMI action' do
              expected_action = Punchblock::Component::Asterisk::AMI::Action.new(:name => 'Originate',
                                                                                 :params => {
                                                                                   :async       => true,
                                                                                   :application => 'AGI',
                                                                                   :data        => 'agi:async',
                                                                                   :channel     => 'SIP/1234',
                                                                                   :callerid    => 'sip:<EMAIL>',
                                                                                   :variable    => "punchblock_call_id=#{subject.id}",
                                                                                   :timeout     => 10000
                                                                                 }).tap { |a| a.request! }

              translator.expects(:execute_global_command!).once.with expected_action
              subject.dial dial_command
            end
          end

          it 'sends the call ID as a response to the Dial' do
            subject.dial dial_command
            dial_command.response
            dial_command.call_id.should be == subject.id
          end

          it 'should make the call identify as outbound' do
            subject.dial dial_command
            subject.direction.should be == :outbound
            subject.outbound?.should be true
            subject.inbound?.should be false
          end

          it 'causes accepting the call to be a null operation' do
            subject.dial dial_command
            accept_command = Command::Accept.new
            accept_command.request!
            subject.wrapped_object.expects(:send_agi_action).never
            subject.execute_command accept_command
            accept_command.response(0.5).should be true
          end
        end
{'fraction_non_alphanumeric': 0.08351105623526771, 'fraction_numerical': 0.017061398585699855, 'mean_word_length': 1.0434633027522935, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 143, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 114, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5556345', 'n_tokens_mistral': 4397, 'n_tokens_neox': 4068, 'n_words': 938}