# encoding: utf-8 require 'spec_helper' module Punchblock module Translator class Freeswitch module Component describe Output do include HasMockCallbackConnection let(:translator) { Punchblock::Translator::Freeswitch.new connection } let(:mock_call) { Punchblock::Translator::Freeswitch::Call.new 'foo', translator } let :original_command do Punchblock::Component::Output.new command_options end let :ssml_doc do RubySpeech::SSML.draw do say_as(:interpret_as => :cardinal) { 'FOO' } end end let :command_options do { :render_document => {:value => ssml_doc} } end subject { Output.new original_command, mock_call } describe '#execute' do before { original_command.request! } def expect_playback(filename = audio_filename) subject.wrapped_object.should_receive(:application).once.with 'playback', "file_string://#{filename}" end let(:audio_filename) { 'http://foo.com/bar.mp3' } let :ssml_doc do RubySpeech::SSML.draw do audio :src => audio_filename end end let(:command_opts) { {} } let :command_options do { :render_document => {:value => ssml_doc} }.merge(command_opts) end let :original_command do Punchblock::Component::Output.new command_options end describe 'document' do context 'unset' do let(:ssml_doc) { nil } it "should return an error and not execute any actions" do subject.execute error = ProtocolError.new.setup 'option error', 'An SSML document is required.' original_command.response(0.1).should be == error end end context 'with a single audio SSML node' do let(:audio_filename) { 'http://foo.com/bar.mp3' } let :ssml_doc do RubySpeech::SSML.draw { audio :src => audio_filename } end it 'should playback the audio file using the playback application' do expect_playback subject.execute end it 'should send a complete event when the file finishes playback' do expect_playback subject.execute subject.handle_es_event RubyFS::Event.new(nil, :event_name => "CHANNEL_EXECUTE_COMPLETE", :application_response => 'FILE PLAYED') original_command.complete_event(0.1).reason.should be_a Punchblock::Component::Output::Complete::Finish end context "when playback returns an error" do let(:fs_event) { RubyFS::Event.new(nil, :event_name => "CHANNEL_EXECUTE_COMPLETE", :application_response => "PLAYBACK ERROR") } let(:complete_reason) { original_command.complete_event(0.1).reason } it "sends a complete event with an error reason" do expect_playback subject.execute subject.handle_es_event fs_event complete_reason.should be_a Punchblock::Event::Complete::Error complete_reason.details.should == 'Engine error: PLAYBACK ERROR' end end end context 'with multiple audio SSML nodes' do let(:audio_filename1) { 'http://foo.com/bar.mp3' } let(:audio_filename2) { 'http://foo.com/baz.mp3' } let :ssml_doc do RubySpeech::SSML.draw do audio :src => audio_filename1 audio :src => audio_filename2 end end it 'should playback all audio files using playback' do expect_playback [audio_filename1, audio_filename2].join('!') subject.execute end it 'should send a complete event when the files finish playback' do expect_playback([audio_filename1, audio_filename2].join('!')) subject.execute subject.handle_es_event RubyFS::Event.new(nil, :event_name => "CHANNEL_EXECUTE_COMPLETE", :application_response => "FILE PLAYED") original_command.complete_event(0.1).reason.should be_a Punchblock::Component::Output::Complete::Finish end end context "with an SSML document containing elements other than