forked from openvoice/openvoice2
/
dial_spec.rb
284 lines (225 loc) · 8.85 KB
/
dial_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require "spec_helper"
describe "Dialing" do
class Dialer
include Connfu::Dsl
class << self; attr_accessor :stash; end
end
before do
setup_connfu(handler_class = nil)
@call_id = "call-id"
@call_jid = "#{@call_id}@#{PRISM_HOST}"
end
it "should send a dial command" do
Dialer.dial :to => "recipient", :from => "caller" do |call|
end
last_command.should == Connfu::Commands::Dial.new(:to => "recipient", :from => "caller", :client_jid => Connfu.connection.jid.to_s, :rayo_host => Connfu.connection.jid.domain)
end
it "should pass any headers to the dial command" do
Dialer.dial :to => "to", :from => "from", :headers => {"foo" => "bar"} do |call|
end
last_command.should == Connfu::Commands::Dial.new(:to => "to", :from => "from", :client_jid => Connfu.connection.jid.to_s, :rayo_host => Connfu.connection.jid.domain, :headers => {"foo" => "bar"})
end
it "should not run the start behaviour before the call has begun" do
Dialer.any_instance.should_not_receive(:start_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_start { start_happened }
end
end
it "should run the start behaviour when the call has begun" do
Dialer.any_instance.should_receive(:start_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_start { start_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
end
it 'should not run the ringing behaviour before the call starts ringing' do
Dialer.any_instance.should_not_receive(:ringing_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_ringing { ringing_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
end
it 'should run the ringing behaviour when the call starts ringing' do
Dialer.any_instance.should_receive(:ringing_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_ringing { ringing_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
end
it 'should run the reject behaviour when the call is rejected' do
Dialer.any_instance.should_receive(:reject_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_reject { reject_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :reject_presence, @call_jid
end
it 'should run the timeout behaviour when the call is timed out' do
Dialer.any_instance.should_receive(:timeout_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_timeout { timeout_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :timeout_presence, @call_jid
end
it 'should run the busy behaviour when the call is busy' do
Dialer.any_instance.should_receive(:busy_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_busy { busy_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :busy_presence, @call_jid
end
it 'should not run the answer behaviour before the call is answered' do
Dialer.any_instance.should_not_receive(:answer_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_answer { answer_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
end
it 'should run the answer behaviour when the call is answered' do
Dialer.any_instance.should_receive(:answer_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_answer { answer_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
end
it 'should not run the hangup behaviour before the call is hung up' do
Dialer.any_instance.should_not_receive(:hangup_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_hangup { hangup_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
end
it 'should run the hangup behaviour when the call is hung up' do
Dialer.any_instance.should_receive(:hangup_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_hangup { hangup_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
incoming :hangup_presence, @call_jid
end
it 'should run all behaviours that are defined' do
Dialer.any_instance.should_receive(:start_happened)
Dialer.any_instance.should_receive(:ringing_happened)
Dialer.any_instance.should_receive(:answer_happened)
Dialer.any_instance.should_receive(:hangup_happened)
Dialer.dial :to => "to", :from => "from" do |c|
c.on_start { start_happened }
c.on_ringing { ringing_happened }
c.on_answer { answer_happened }
c.on_hangup { hangup_happened }
end
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
incoming :hangup_presence, @call_jid
end
it "should make the call id available when the on_start block is triggered" do
Dialer.dial :to => "to", :from => "from" do |c|
c.on_start { Dialer.stash = call_id }
end
incoming :dial_result_iq, @call_id, last_command.id
Dialer.stash.should == @call_id
end
context "when no behaviour is specified" do
before do
Dialer.dial :to => "someone-remote", :from => "my-number"
end
it 'should not crash when receiving a ringing event' do
lambda {
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
}.should_not raise_error
end
it 'should not crash when receiving an answered event' do
lambda {
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
}.should_not raise_error
end
it 'should not crash when receiving a hangup event' do
lambda {
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :hangup_presence, @call_jid
}.should_not raise_error
end
end
context "behaviour blocks" do
before do
Dialer.dial :to => "to", :from => "from" do |c|
c.on_answer do
say "hello"
say "is it me you're looking for?"
end
end
@call_jid = "call-id@#{PRISM_HOST}"
end
it "should allow running commands" do
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid, "client-jid"
incoming :answered_presence, @call_jid
say_command = last_command
say_command.should be_instance_of Connfu::Commands::Say
say_command.text.should == "hello"
say_command.call_jid.should == @call_jid
say_command.client_jid.should == "client-jid"
end
it "should not execute next command until the previous is completed" do
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
last_command.should be_instance_of Connfu::Commands::Say
last_command.text.should == "hello"
end
it "should continue executing next command when the previous has completed" do
incoming :dial_result_iq, @call_id, last_command.id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
incoming :say_result_iq, @call_jid
incoming :say_success_presence, @call_jid
last_command.should be_instance_of Connfu::Commands::Say
last_command.text.should == "is it me you're looking for?"
end
end
end
describe "dialing with instance-specific call behaviour" do
class DiallerWithInstanceSpecificBehaviour
include Connfu::Dsl
class << self
def execute(instance_specific_argument)
dial :to => "recipient", :from => "caller" do |call|
call.on_answer { say instance_specific_argument }
end
end
end
end
before do
setup_connfu(handler_class = nil)
@call_id = "call-id"
@call_jid = "#{@call_id}@#{PRISM_HOST}"
end
it "should retain the specific behaviour for each dial statement" do
DiallerWithInstanceSpecificBehaviour.execute "first behaviour"
first_dial_command_id = last_command.id
DiallerWithInstanceSpecificBehaviour.execute "second behaviour"
incoming :dial_result_iq, @call_id, first_dial_command_id
incoming :ringing_presence, @call_jid
incoming :answered_presence, @call_jid
last_command.should be_instance_of Connfu::Commands::Say
last_command.text.should == "first behaviour"
end
end