<commit_before>require 'spec_helper'

module Adhearsion
  describe Call do
    let(:mock_client) { flexmock('Client').tap &:should_ignore_missing }

    let(:call_id) { rand }
    let(:headers) { nil }
    let(:to)      { 'sip:<EMAIL>' }
    let(:from)    { 'sip:<EMAIL>' }
    let :offer do
      Punchblock::Event::Offer.new :call_id => call_id,
                                   :to      => to,
                                   :from    => from,
                                   :headers => headers
    end

    subject { Adhearsion::Call.new offer }

    before do
      flexmock(offer).should_receive(:client).and_return(mock_client)
    end

    after do
      Adhearsion.active_calls.clear!
    end

    it { should respond_to :<< }

    its(:end_reason) { should == nil }
    it { should be_active }

    its(:commands) { should be_a Call::CommandRegistry }
    its(:commands) { should be_empty }

    its(:id)      { should == call_id }
    its(:to)      { should == to }
    its(:from)    { should == from }
    its(:offer)   { should be offer }
    its(:client)  { should be mock_client }

    describe "its variables" do
      context "with an offer with headers" do
        let(:headers)   { {:x_foo => 'bar'} }
        its(:variables) { should == headers }

        it "should be made available via []" do
          subject[:x_foo].should == 'bar'
        end

        it "should be alterable using []=" do
          subject[:x_foo] = 'baz'
          subject[:x_foo].should == 'baz'
        end

        context "when receiving an event with headers" do
          let(:event) { Punchblock::Event::End.new :headers => {:x_bar => 'foo'} }

          it "should merge later headers" do
            subject << event
            subject.variables.should == {:x_foo => 'bar', :x_bar => 'foo'}
          end
        end

        context "when sending a command with headers" do
          let(:command) { Punchblock::Command::Accept.new :headers => {:x_bar => 'foo'} }

          it "should merge later headers" do
            subject.write_command command
            subject.variables.should == {:x_foo => 'bar', :x_bar => 'foo'}
          end
        end
      end

      context "with an offer without headers" do
        let(:headers)   { nil }
        its(:variables) { should == {} }
      end

      context "without an offer" do
        let(:offer)     { nil }
        its(:variables) { should == {} }
      end
    end

    describe 'without an offer' do
      it 'should not raise an exception' do
        lambda { Adhearsion::Call.new }.should_not raise_error
      end
    end

    it 'allows the registration of event handlers which are called when messages are delivered' do
      event = flexmock 'Event'
      event.should_receive(:foo?).and_return true
      response = flexmock 'Response'
      response.should_receive(:call).once
      subject.register_event_handler(:foo?) { response.call }
      subject << event
    end

    describe "event handlers" do
      let(:response) { flexmock 'Response' }

      describe "for end events" do
        let :event do
          Punchblock::Event::End.new.tap do |e|
            flexmock e, :reason => :hangup
          end
        end

        it "should trigger any on_end callbacks set" do
          response.should_receive(:call).once.with(event)
          subject.on_end { |event| response.call event }
          subject << event
        end
      end
    end

    describe "#<<" do
      describe "with a Punchblock End" do
        let :end_event do
          Punchblock::Event::End.new.tap do |e|
            flexmock e, :reason => :hangup
          end
        end

        it "should mark the call as ended" do
          subject << end_event
          subject.should_not be_active
        end

        it "should set the end reason" do
          subject << end_event
          subject.end_reason.should == :hangup
        end

        it "should instruct the command registry to terminate" do
          flexmock(subject.commands).should_receive(:terminate).once
          subject << end_event
        end

        it "removes itself from the active calls" do
          size_before = Adhearsion.active_calls.size

          Adhearsion.active_calls << subject
          Adhearsion.active_calls.size.should > size_before

          subject << end_event
          Adhearsion.active_calls.size.should == size_before
        end

        it "shuts down the actor" do
          subject << end_event
          sleep 5<commit_msg>[BUGFIX] Ensure no race condition in a spec<commit_after>require 'spec_helper'

module Adhearsion
  describe Call do
    let(:mock_client) { flexmock('Client').tap &:should_ignore_missing }

    let(:call_id) { rand }
    let(:headers) { nil }
    let(:to)      { 'sip:<EMAIL>' }
    let(:from)    { 'sip:<EMAIL>' }
    let :offer do
      Punchblock::Event::Offer.new :call_id => call_id,
                                   :to      => to,
                                   :from    => from,
                                   :headers => headers
    end

    subject { Adhearsion::Call.new offer }

    before do
      flexmock(offer).should_receive(:client).and_return(mock_client)
    end

    after do
      Adhearsion.active_calls.clear!
    end

    it { should respond_to :<< }

    its(:end_reason) { should == nil }
    it { should be_active }

    its(:commands) { should be_a Call::CommandRegistry }
    its(:commands) { should be_empty }

    its(:id)      { should == call_id }
    its(:to)      { should == to }
    its(:from)    { should == from }
    its(:offer)   { should be offer }
    its(:client)  { should be mock_client }

    describe "its variables" do
      context "with an offer with headers" do
        let(:headers)   { {:x_foo => 'bar'} }
        its(:variables) { should == headers }

        it "should be made available via []" do
          subject[:x_foo].should == 'bar'
        end

        it "should be alterable using []=" do
          subject[:x_foo] = 'baz'
          subject[:x_foo].should == 'baz'
        end

        context "when receiving an event with headers" do
          let(:event) { Punchblock::Event::End.new :headers => {:x_bar => 'foo'} }

          it "should merge later headers" do
            subject << event
            subject.variables.should == {:x_foo => 'bar', :x_bar => 'foo'}
          end
        end

        context "when sending a command with headers" do
          let(:command) { Punchblock::Command::Accept.new :headers => {:x_bar => 'foo'} }

          it "should merge later headers" do
            subject.write_command command
            subject.variables.should == {:x_foo => 'bar', :x_bar => 'foo'}
          end
        end
      end

      context "with an offer without headers" do
        let(:headers)   { nil }
        its(:variables) { should == {} }
      end

      context "without an offer" do
        let(:offer)     { nil }
        its(:variables) { should == {} }
      end
    end

    describe 'without an offer' do
      it 'should not raise an exception' do
        lambda { Adhearsion::Call.new }.should_not raise_error
      end
    end

    it 'allows the registration of event handlers which are called when messages are delivered' do
      event = flexmock 'Event'
      event.should_receive(:foo?).and_return true
      response = flexmock 'Response'
      response.should_receive(:call).once
      subject.register_event_handler(:foo?) { response.call }
      subject << event
    end

    describe "event handlers" do
      let(:response) { flexmock 'Response' }

      describe "for end events" do
        let :event do
          Punchblock::Event::End.new.tap do |e|
            flexmock e, :reason => :hangup
          end
        end

        it "should trigger any on_end callbacks set" do
          response.should_receive(:call).once.with(event)
          subject.on_end { |event| response.call event }
          subject << event
        end
      end
    end

    describe "#<<" do
      describe "with a Punchblock End" do
        let :end_event do
          Punchblock::Event::End.new.tap do |e|
            flexmock e, :reason => :hangup
          end
        end

        it "should mark the call as ended" do
          subject << end_event
          subject.should_not be_active
        end

        it "should set the end reason" do
          subject << end_event
          subject.end_reason.should == :hangup
        end

        it "should instruct the command registry to terminate" do
          flexmock(subject.commands).should_receive(:terminate).once
          subject << end_event
        end

        it "removes itself from the active calls" do
          size_before = Adhearsion.active_calls.size

          Adhearsion.active_calls << subject
          Adhearsion.active_calls.size.should > size_before

          subject << end_event
          Adhearsion.active_calls.size.should == size_before
        end

        it "shuts down the actor" do
          subject << end_event
          sleep 5.1
{'fraction_non_alphanumeric': 0.10325365205843294, 'fraction_numerical': 0.00033200531208499334, 'mean_word_length': 1.9085934985516575, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2107253', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2521, 'n_words': 744}