# encoding: utf-8
require 'spec_helper'
module Punchblock
module Component
describe Input do
it 'registers itself' do
RayoNode.class_from_registration(:input, 'urn:xmpp:rayo:input:1').should be == Input
end
describe "when setting options in initializer" do
subject do
Input.new :grammar => {:value => '[5 DIGITS]', :content_type => 'application/grammar+custom'},
:mode => :speech,
:terminator => '#',
:max_digits => 10,
:max_silence => 1000,
:recognizer => 'en-US',
:initial_timeout => 2000,
:inter_digit_timeout => 2000,
:term_timeout => 2000,
:complete_timeout => 2000,
:incomplete_timeout => 2000,
:sensitivity => 0.5,
:min_confidence => 0.5
end
its(:grammar) { should be == Input::Grammar.new(:value => '[5 DIGITS]', :content_type => 'application/grammar+custom') }
its(:mode) { should be == :speech }
its(:terminator) { should be == '#' }
its(:max_digits) { should be == 10 }
its(:max_silence) { should be == 1000 }
its(:recognizer) { should be == 'en-US' }
its(:initial_timeout) { should be == 2000 }
its(:inter_digit_timeout) { should be == 2000 }
its(:term_timeout) { should be == 2000 }
its(:complete_timeout) { should be == 2000 }
its(:incomplete_timeout) { should be == 2000 }
its(:sensitivity) { should be == 0.5 }
its(:min_confidence) { should be == 0.5 }
end
describe "from a stanza" do
let :stanza do
<<-MESSAGE
MESSAGE
end
subject { RayoNode.import parse_stanza(stanza).root, '9f00061', '1' }
it { should be_instance_of Input }
its(:grammar) { should be == Input::Grammar.new(:value => '[5 DIGITS]', :content_type => 'application/grammar+custom') }
its(:mode) { should be == :speech }
its(:terminator) { should be == '#' }
its(:max_digits) { should be == 10 }
its(:max_silence) { should be == 1000 }
its(:recognizer) { should be == 'en-US' }
its(:initial_timeout) { should be == 2000 }
its(:inter_digit_timeout) { should be == 2000 }
its(:term_timeout) { should be == 2000 }
its(:complete_timeout) { should be == 2000 }
its(:incomplete_timeout) { should be == 2000 }
its(:sensitivity) { should be == 0.5 }
its(:min_confidence) { should be == 0.5 }
end
def grxml_doc(mode = :dtmf)
RubySpeech::GRXML.draw :mode => mode.to_s, :root => 'digits' do
rule id: 'digits' do
one_of do
0.upto(1) { |d| item { d.to_s } }
end
end
end
end
describe Input::Grammar do
describe "when not passing a content type" do
subject { Input::Grammar.new :value => grxml_doc }
its(:content_type) { should be == 'application/grammar+grxml' }
end
describe 'with a simple grammar' do
subject { Input::Grammar.new :value => '[5 DIGITS]', :content_type => 'application/grammar+custom' }
let(:expected_message) { "" }
it "should wrap grammar in CDATA" do
subject.child.to_xml.should be == expected_message.strip
end
end
describe 'with a GRXML grammar' do
subject { Input::Grammar.new :value => grxml_doc, :content_type => 'application/grammar+grxml' }
its(:content_type) { should be == 'application/grammar+grxml' }
let(:expected_message) { "" }
it "should wrap GRXML in CDATA" do
subject.child.to_xml.should be == expected_message.strip
end
its(:value) { should be == grxml_doc }
describe "comparison" do
let(:grammar2) { Input::Grammar.new :value => '01' }
let(:grammar3) { Input::Grammar.new :value => grxml_doc }
let(:grammar4) { Input::Grammar.new :value => grxml_doc(:speech) }
it { should be == grammar2 }
it { should be == grammar3 }
it { should_not be == grammar4 }
end
end
end
describe "actions" do
let(:mock_client) { mock 'Client' }
let(:command) { Input.new :grammar => '[5 DIGITS]' }
before do
command.component_id = 'abc123'
command.target_call_id = '123abc'
command.client = mock_client
end
describe '#stop_action' do
subject { command.stop_action }
its(:to_xml) { should be == '' }
its(:component_id) { should be == 'abc123' }
its(:target_call_id) { should be == '123abc' }
end
describe '#stop!' do
describe "when the command is executing" do
before do
command.request!
command.execute!
end
it "should send its command properly" do
mock_client.expects(:execute_command).with(command.stop_action, :target_call_id => '123abc', :component_id => 'abc123')
command.stop!
end
end
describe "when the command is not executing" do
it "should raise an error" do
lambda { command.stop! }.should raise_error(InvalidActionError, "Cannot stop a Input that is not executing")
end
end
end
end
describe Input::Complete::Success do
let :stanza do
<<-MESSAGE
1234one two three four
MESSAGE
end
subject { RayoNode.import(parse_stanza(stanza).root).reason }
it { should be_instance_of Input::Complete::Success }
its(:name) { should be == :success }
its(:mode) { should be == :speech }
its(:confidence) { should be == 0.45 }
its(:interpretation) { should be == '1234' }
its(:utterance) { should be == 'one two three four' }
describe "when setting options in initializer" do
subject do
Input::Complete::Success.new :mode => :dtmf,
:confidence => 1,
:utterance => '123',
:interpretation => 'dtmf-1 dtmf-2 dtmf-3'
end
its(:mode) { should be == :dtmf }
its(:confidence) { should be == 1 }
its(:utterance) { should be == '123' }
its(:interpretation) { should be == 'dtmf-1 dtmf-2 dtmf-3' }
end
end
describe Input::Complete::NoMatch do
let :stanza do
<<-MESSAGE
MESSAGE
end
subject { RayoNode.import(parse_stanza(stanza).root).reason }
it { should be_instance_of Input::Complete::NoMatch }
its(:name) { should be == :nomatch }
end
describe Input::Complete::NoInput do
let :stanza do
<<-MESSAGE
MESSAGE
end
subject { RayoNode.import(parse_stanza(stanza).root).reason }
it { should be_instance_of Input::Complete::NoInput }
its(:name) { should be == :noinput }
end
end
end
end # Punchblock