1
|
|
|
package goagi |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"testing" |
5
|
|
|
|
6
|
|
|
"github.com/stretchr/testify/assert" |
7
|
|
|
) |
8
|
|
|
|
9
|
|
|
// command Command |
10
|
|
|
func TestCmdCommandOk(t *testing.T) { |
11
|
|
|
resp := "200 result=25 endpos=542268\n" |
12
|
|
|
rw := dummyReadWrite(resp) |
13
|
|
|
agi := &AGI{io: rw} |
14
|
|
|
code, result, respStr, err := agi.Command("STREAM FILE welcome 09# \"554\"") |
15
|
|
|
assert.Nil(t, err) |
16
|
|
|
assert.Equal(t, 200, code) |
17
|
|
|
assert.Equal(t, 25, result) |
18
|
|
|
assert.Equal(t, resp, respStr) |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
func TestCmdCommandFail(t *testing.T) { |
22
|
|
|
rw := dummyReadWriteWError() |
23
|
|
|
agi := &AGI{io: rw} |
24
|
|
|
code, result, respStr, err := agi.Command("STREAM FILE welcome 09# \"554\"") |
25
|
|
|
assert.NotNil(t, err) |
26
|
|
|
assert.Equal(t, -1, code) |
27
|
|
|
assert.Equal(t, -1, result) |
28
|
|
|
assert.Equal(t, "", respStr) |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
// command Answer |
32
|
|
|
func TestCmdAnswerOk(t *testing.T) { |
33
|
|
|
resp := "200 result=0\n" |
34
|
|
|
rw := dummyReadWrite(resp) |
35
|
|
|
|
36
|
|
|
agi := &AGI{io: rw} |
37
|
|
|
ok, err := agi.Answer() |
38
|
|
|
assert.Nil(t, err) |
39
|
|
|
assert.True(t, ok) |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
func TestCmdAnswerFail(t *testing.T) { |
43
|
|
|
resp := "200 result=-1\n" |
44
|
|
|
rw := dummyReadWrite(resp) |
45
|
|
|
|
46
|
|
|
agi := &AGI{io: rw} |
47
|
|
|
ok, err := agi.Answer() |
48
|
|
|
assert.Nil(t, err) |
49
|
|
|
assert.False(t, ok) |
50
|
|
|
|
51
|
|
|
rw = dummyReadWriteWError() |
52
|
|
|
agi = &AGI{io: rw} |
53
|
|
|
ok, err = agi.Answer() |
54
|
|
|
assert.NotNil(t, err) |
55
|
|
|
assert.False(t, ok) |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// command AsyncAGIBreak |
59
|
|
|
func TestCmdAsyncAGIBreakOk(t *testing.T) { |
60
|
|
|
resp := "200 result=0\n" |
61
|
|
|
rw := dummyReadWrite(resp) |
62
|
|
|
|
63
|
|
|
agi := &AGI{io: rw} |
64
|
|
|
ok, err := agi.AsyncAGIBreak() |
65
|
|
|
assert.Nil(t, err) |
66
|
|
|
assert.True(t, ok) |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
func TestCmdAsyncAGIBreakFail(t *testing.T) { |
70
|
|
|
resp := "200 result=-1\n" |
71
|
|
|
rw := dummyReadWrite(resp) |
72
|
|
|
|
73
|
|
|
agi := &AGI{io: rw} |
74
|
|
|
ok, err := agi.AsyncAGIBreak() |
75
|
|
|
assert.Nil(t, err) |
76
|
|
|
assert.False(t, ok) |
77
|
|
|
|
78
|
|
|
rw = dummyReadWriteWError() |
79
|
|
|
agi = &AGI{io: rw} |
80
|
|
|
ok, err = agi.AsyncAGIBreak() |
81
|
|
|
assert.NotNil(t, err) |
82
|
|
|
assert.False(t, ok) |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// command ChannelStatus |
86
|
|
|
func TestCmdChannelStatusOk(t *testing.T) { |
87
|
|
|
resp := "200 result=6\n" |
88
|
|
|
rw := dummyReadWrite(resp) |
89
|
|
|
|
90
|
|
|
agi := &AGI{io: rw} |
91
|
|
|
status, err := agi.ChannelStatus("") |
92
|
|
|
assert.Nil(t, err) |
93
|
|
|
assert.EqualValues(t, 6, status) |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
func TestCmdChannelStatusFail(t *testing.T) { |
97
|
|
|
resp := "200 result=-1\n" |
98
|
|
|
rw := dummyReadWrite(resp) |
99
|
|
|
|
100
|
|
|
agi := &AGI{io: rw} |
101
|
|
|
status, err := agi.ChannelStatus("SIP/00001-44330") |
102
|
|
|
assert.NotNil(t, err) |
103
|
|
|
assert.EqualValues(t, -1, status) |
104
|
|
|
assert.Contains(t, err.Error(), "No channel name matched") |
105
|
|
|
|
106
|
|
|
rw = dummyReadWriteWError() |
107
|
|
|
agi = &AGI{io: rw} |
108
|
|
|
status, err = agi.ChannelStatus("") |
109
|
|
|
assert.NotNil(t, err) |
110
|
|
|
assert.Equal(t, -1, status) |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// command ControlStreamFile |
114
|
|
|
func TestCmdControlStreamFileOk(t *testing.T) { |
115
|
|
|
resp := "200 result=0 endpos=2541236\n" |
116
|
|
|
rw := dummyReadWrite(resp) |
117
|
|
|
|
118
|
|
|
agi := &AGI{io: rw} |
119
|
|
|
endpos, err := agi.ControlStreamFile("welcome", "") |
120
|
|
|
assert.Nil(t, err) |
121
|
|
|
assert.EqualValues(t, 2541236, endpos) |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
func TestCmdControlStreamFileFail(t *testing.T) { |
125
|
|
|
resp := "200 result=-1 endpos=2541236\n" |
126
|
|
|
rw := dummyReadWrite(resp) |
127
|
|
|
agi := &AGI{io: rw} |
128
|
|
|
_, err := agi.ControlStreamFile("welcome", "123") |
129
|
|
|
assert.NotNil(t, err) |
130
|
|
|
|
131
|
|
|
rw = dummyReadWriteRError() |
132
|
|
|
agi = &AGI{io: rw} |
133
|
|
|
_, err = agi.ControlStreamFile("welcome", "") |
134
|
|
|
assert.NotNil(t, err) |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// command DatabaseDel |
138
|
|
|
func TestCmdDatabaseDelOk(t *testing.T) { |
139
|
|
|
resp := "200 result=1\n" |
140
|
|
|
rw := dummyReadWrite(resp) |
141
|
|
|
agi := &AGI{io: rw} |
142
|
|
|
r, err := agi.DatabaseDel("channel", "foo") |
143
|
|
|
assert.Nil(t, err) |
144
|
|
|
assert.True(t, r) |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
func TestCmdDatabaseDelFail(t *testing.T) { |
148
|
|
|
resp := "200 result=0\n" |
149
|
|
|
rw := dummyReadWrite(resp) |
150
|
|
|
agi := &AGI{io: rw} |
151
|
|
|
r, err := agi.DatabaseDel("channel", "foo") |
152
|
|
|
assert.Nil(t, err) |
153
|
|
|
assert.False(t, r) |
154
|
|
|
|
155
|
|
|
rw = dummyReadWriteWError() |
156
|
|
|
agi = &AGI{io: rw} |
157
|
|
|
r, err = agi.DatabaseDel("hello", "world") |
158
|
|
|
assert.NotNil(t, err) |
159
|
|
|
assert.False(t, r) |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// command DatabaseDel |
163
|
|
|
func TestCmdDatabaseDelTreeOk(t *testing.T) { |
164
|
|
|
resp := "200 result=1\n" |
165
|
|
|
rw := dummyReadWrite(resp) |
166
|
|
|
agi := &AGI{io: rw} |
167
|
|
|
r, err := agi.DatabaseDelTree("channel", "foo") |
168
|
|
|
assert.Nil(t, err) |
169
|
|
|
assert.True(t, r) |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
func TestCmdDatabaseDelTreeFail(t *testing.T) { |
173
|
|
|
resp := "200 result=0\n" |
174
|
|
|
rw := dummyReadWrite(resp) |
175
|
|
|
agi := &AGI{io: rw} |
176
|
|
|
r, err := agi.DatabaseDelTree("channel", "foo") |
177
|
|
|
assert.Nil(t, err) |
178
|
|
|
assert.False(t, r) |
179
|
|
|
|
180
|
|
|
rw = dummyReadWriteWError() |
181
|
|
|
agi = &AGI{io: rw} |
182
|
|
|
r, err = agi.DatabaseDelTree("hello", "world") |
183
|
|
|
assert.NotNil(t, err) |
184
|
|
|
assert.False(t, r) |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// command DatabaseDel |
188
|
|
|
func TestCmdDatabaseGetOk(t *testing.T) { |
189
|
|
|
resp := "200 result=1 (SIP/router01-000e57a5)\n" |
190
|
|
|
rw := dummyReadWrite(resp) |
191
|
|
|
agi := &AGI{io: rw} |
192
|
|
|
val, err := agi.DatabaseGet("channel", "foo") |
193
|
|
|
assert.Nil(t, err) |
194
|
|
|
assert.Equal(t, "SIP/router01-000e57a5", val) |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
func TestCmdDatabaseGetFail(t *testing.T) { |
198
|
|
|
resp := "200 result=0\n" |
199
|
|
|
rw := dummyReadWrite(resp) |
200
|
|
|
agi := &AGI{io: rw} |
201
|
|
|
val, err := agi.DatabaseGet("channel", "foo") |
202
|
|
|
assert.NotNil(t, err) |
203
|
|
|
assert.Equal(t, "", val) |
204
|
|
|
|
205
|
|
|
rw = dummyReadWriteWError() |
206
|
|
|
agi = &AGI{io: rw} |
207
|
|
|
val, err = agi.DatabaseGet("hello", "world") |
208
|
|
|
assert.NotNil(t, err) |
209
|
|
|
assert.Equal(t, "", val) |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// command DatabasePut |
213
|
|
|
func TestCmdDatabasePutOk(t *testing.T) { |
214
|
|
|
resp := "200 result=1\n" |
215
|
|
|
rw := dummyReadWrite(resp) |
216
|
|
|
agi := &AGI{io: rw} |
217
|
|
|
r, err := agi.DatabasePut("channel", "foo", "bar") |
218
|
|
|
assert.Nil(t, err) |
219
|
|
|
assert.Equal(t, true, r) |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
func TestCmdDatabasePutFail(t *testing.T) { |
223
|
|
|
resp := "200 result=0\n" |
224
|
|
|
rw := dummyReadWrite(resp) |
225
|
|
|
agi := &AGI{io: rw} |
226
|
|
|
r, err := agi.DatabasePut("channel", "foo", "bar") |
227
|
|
|
assert.Nil(t, err) |
228
|
|
|
assert.Equal(t, false, r) |
229
|
|
|
|
230
|
|
|
rw = dummyReadWriteWError() |
231
|
|
|
agi = &AGI{io: rw} |
232
|
|
|
r, err = agi.DatabasePut("channel", "foo", "bar") |
233
|
|
|
assert.NotNil(t, err) |
234
|
|
|
assert.Equal(t, false, r) |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// command Exec |
238
|
|
|
func TestCmdExecOk(t *testing.T) { |
239
|
|
|
resp := "200 result=1\n" |
240
|
|
|
rw := dummyReadWrite(resp) |
241
|
|
|
agi := &AGI{io: rw} |
242
|
|
|
r, err := agi.Exec("MusicOnHold", "default,15") |
243
|
|
|
assert.Nil(t, err) |
244
|
|
|
assert.Equal(t, 1, r) |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
func TestCmdExecFail(t *testing.T) { |
248
|
|
|
resp := "200 result=-2\n" |
249
|
|
|
rw := dummyReadWrite(resp) |
250
|
|
|
agi := &AGI{io: rw} |
251
|
|
|
r, err := agi.Exec("Dial", "PJSIP/bob,,Q(NO_ANSWER)") |
252
|
|
|
assert.NotNil(t, err) |
253
|
|
|
assert.Equal(t, -2, r) |
254
|
|
|
|
255
|
|
|
rw = dummyReadWriteWError() |
256
|
|
|
agi = &AGI{io: rw} |
257
|
|
|
r, err = agi.Exec("Dial", "PJSIP/bob,,Q(NO_ANSWER)") |
258
|
|
|
assert.NotNil(t, err) |
259
|
|
|
assert.Equal(t, -1, r) |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
// command GetData |
263
|
|
|
func TestCmdGetDataOk(t *testing.T) { |
264
|
|
|
resp := "200 result=42 (timeout)\n" |
265
|
|
|
rw := dummyReadWrite(resp) |
266
|
|
|
agi := &AGI{io: rw} |
267
|
|
|
res, tout, err := agi.GetData("prompt", 1000, 3) |
268
|
|
|
assert.Nil(t, err) |
269
|
|
|
assert.Equal(t, "*", res) |
270
|
|
|
assert.True(t, tout) |
271
|
|
|
|
272
|
|
|
resp = "200 result=49\n" |
273
|
|
|
rw = dummyReadWrite(resp) |
274
|
|
|
agi = &AGI{io: rw} |
275
|
|
|
res, tout, err = agi.GetData("prompt", 1000, 3) |
276
|
|
|
assert.Nil(t, err) |
277
|
|
|
assert.Equal(t, "1", res) |
278
|
|
|
assert.False(t, tout) |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
func TestCmdGetDataFail(t *testing.T) { |
282
|
|
|
resp := "200 result=-1\n" |
283
|
|
|
rw := dummyReadWrite(resp) |
284
|
|
|
agi := &AGI{io: rw} |
285
|
|
|
res, tout, err := agi.GetData("prompt", 1000, 3) |
286
|
|
|
assert.NotNil(t, err) |
287
|
|
|
assert.Equal(t, "", res) |
288
|
|
|
assert.False(t, tout) |
289
|
|
|
|
290
|
|
|
rw = dummyReadWriteWError() |
291
|
|
|
agi = &AGI{io: rw} |
292
|
|
|
res, tout, err = agi.GetData("prompt", 1000, 3) |
293
|
|
|
assert.NotNil(t, err) |
294
|
|
|
assert.Equal(t, "", res) |
295
|
|
|
assert.False(t, tout) |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
// command GetFullVariable |
299
|
|
|
func TestCmdGetFullVariableOk(t *testing.T) { |
300
|
|
|
resp := "200 result=1 (\"John Dow\" <12345>)\n" |
301
|
|
|
rw := dummyReadWrite(resp) |
302
|
|
|
agi := &AGI{io: rw} |
303
|
|
|
val, err := agi.GetFullVariable("CALLERID") |
304
|
|
|
assert.Nil(t, err) |
305
|
|
|
assert.Equal(t, "\"John Dow\" <12345>", val) |
306
|
|
|
|
307
|
|
|
resp = "200 result=1 (107.5.2.224)\n" |
308
|
|
|
rw = dummyReadWrite(resp) |
309
|
|
|
agi = &AGI{io: rw} |
310
|
|
|
val, err = agi.GetFullVariable("CHANNEL(rtp,dest)", "SIP/112003430-44432") |
311
|
|
|
assert.Nil(t, err) |
312
|
|
|
assert.Equal(t, "107.5.2.224", val) |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
func TestCmdGetFullVariableFail(t *testing.T) { |
316
|
|
|
resp := "200 result=0\n" |
317
|
|
|
rw := dummyReadWrite(resp) |
318
|
|
|
agi := &AGI{io: rw} |
319
|
|
|
val, err := agi.GetFullVariable("CALLERID(null)") |
320
|
|
|
assert.NotNil(t, err) |
321
|
|
|
assert.Equal(t, "", val) |
322
|
|
|
|
323
|
|
|
rw = dummyReadWriteWError() |
324
|
|
|
agi = &AGI{io: rw} |
325
|
|
|
_, err = agi.GetFullVariable("CALLERID(null)") |
326
|
|
|
assert.NotNil(t, err) |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
// command GetOption |
330
|
|
|
func TestCmdGetOptionOk(t *testing.T) { |
331
|
|
|
resp := "200 result=0 endpos=10245\n" |
332
|
|
|
rw := dummyReadWrite(resp) |
333
|
|
|
agi := &AGI{io: rw} |
334
|
|
|
dig, offset, err := agi.GetOption("welcome_prompt", "", 0) |
335
|
|
|
assert.Nil(t, err) |
336
|
|
|
assert.Equal(t, 0, dig) |
337
|
|
|
assert.EqualValues(t, 10245, offset) |
338
|
|
|
|
339
|
|
|
resp = "200 result=5 endpos=52417854\n" |
340
|
|
|
rw = dummyReadWrite(resp) |
341
|
|
|
agi = &AGI{io: rw} |
342
|
|
|
dig, offset, err = agi.GetOption("welcome_prompt", "12", 1800) |
343
|
|
|
assert.Nil(t, err) |
344
|
|
|
assert.Equal(t, 5, dig) |
345
|
|
|
assert.EqualValues(t, 52417854, offset) |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
func TestCmdGetOptionFail(t *testing.T) { |
349
|
|
|
resp := "200 result=-1 endpos=0\n" |
350
|
|
|
rw := dummyReadWrite(resp) |
351
|
|
|
agi := &AGI{io: rw} |
352
|
|
|
dig, offset, err := agi.GetOption("welcome_prompt", "12", 0) |
353
|
|
|
assert.NotNil(t, err) |
354
|
|
|
assert.Equal(t, -1, dig) |
355
|
|
|
assert.EqualValues(t, 0, offset) |
356
|
|
|
|
357
|
|
|
resp = "200 result=0 endpos=0\n" |
358
|
|
|
rw = dummyReadWrite(resp) |
359
|
|
|
agi = &AGI{io: rw} |
360
|
|
|
dig, offset, err = agi.GetOption("welcome_prompt", "12", 0) |
361
|
|
|
assert.NotNil(t, err) |
362
|
|
|
assert.Equal(t, -1, dig) |
363
|
|
|
assert.EqualValues(t, 0, offset) |
364
|
|
|
|
365
|
|
|
rw = dummyReadWriteWError() |
366
|
|
|
agi = &AGI{io: rw} |
367
|
|
|
dig, offset, err = agi.GetOption("welcome_prompt", "12", 0) |
368
|
|
|
assert.NotNil(t, err) |
369
|
|
|
assert.Equal(t, -1, dig) |
370
|
|
|
assert.EqualValues(t, 0, offset) |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
// command GetVariable |
374
|
|
|
func TestCmdGetVariableOk(t *testing.T) { |
375
|
|
|
resp := "200 result=1 (\"John Dow\" <12345>)\n" |
376
|
|
|
rw := dummyReadWrite(resp) |
377
|
|
|
agi := &AGI{io: rw} |
378
|
|
|
val, err := agi.GetVariable("CALLERID") |
379
|
|
|
assert.Nil(t, err) |
380
|
|
|
assert.Equal(t, "\"John Dow\" <12345>", val) |
381
|
|
|
|
382
|
|
|
resp = "200 result=1 (107.5.2.224)\n" |
383
|
|
|
rw = dummyReadWrite(resp) |
384
|
|
|
agi = &AGI{io: rw} |
385
|
|
|
val, err = agi.GetVariable("CHANNEL(rtp,dest)") |
386
|
|
|
assert.Nil(t, err) |
387
|
|
|
assert.Equal(t, "107.5.2.224", val) |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
func TestCmdGetVariableFail(t *testing.T) { |
391
|
|
|
resp := "200 result=0\n" |
392
|
|
|
rw := dummyReadWrite(resp) |
393
|
|
|
agi := &AGI{io: rw} |
394
|
|
|
val, err := agi.GetVariable("CALLERID(null)") |
395
|
|
|
assert.NotNil(t, err) |
396
|
|
|
assert.Equal(t, "", val) |
397
|
|
|
|
398
|
|
|
rw = dummyReadWriteWError() |
399
|
|
|
agi = &AGI{io: rw} |
400
|
|
|
_, err = agi.GetVariable("CALLERID(null)") |
401
|
|
|
assert.NotNil(t, err) |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
// command GoSub |
405
|
|
|
func TestCmdGoSubOk(t *testing.T) { |
406
|
|
|
resp := "200 result=1\n" |
407
|
|
|
rw := dummyReadWrite(resp) |
408
|
|
|
agi := &AGI{io: rw} |
409
|
|
|
ok, err := agi.GoSub("sub-default", "s", "1", "") |
410
|
|
|
assert.Nil(t, err) |
411
|
|
|
assert.True(t, ok) |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
func TestCmdGoSubFail(t *testing.T) { |
415
|
|
|
resp := "200 result=-1\n" |
416
|
|
|
rw := dummyReadWrite(resp) |
417
|
|
|
agi := &AGI{io: rw} |
418
|
|
|
ok, err := agi.GoSub("sub-default", "s", "1", "") |
419
|
|
|
assert.NotNil(t, err) |
420
|
|
|
assert.False(t, ok) |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
// command Hangup |
424
|
|
|
func TestCmdHangupOk(t *testing.T) { |
425
|
|
|
resp := "200 result=1\n" |
426
|
|
|
rw := dummyReadWrite(resp) |
427
|
|
|
agi := &AGI{io: rw} |
428
|
|
|
ok, err := agi.Hangup() |
429
|
|
|
assert.Nil(t, err) |
430
|
|
|
assert.True(t, ok) |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
func TestCmdHangupFail(t *testing.T) { |
434
|
|
|
resp := "200 result=-1\n" |
435
|
|
|
rw := dummyReadWrite(resp) |
436
|
|
|
agi := &AGI{io: rw} |
437
|
|
|
ok, err := agi.Hangup("SIP/0001-4578") |
438
|
|
|
assert.NotNil(t, err) |
439
|
|
|
assert.False(t, ok) |
440
|
|
|
|
441
|
|
|
rw = dummyReadWriteWError() |
442
|
|
|
agi = &AGI{io: rw} |
443
|
|
|
ok, err = agi.Hangup("SIP/0001-4578") |
444
|
|
|
assert.NotNil(t, err) |
445
|
|
|
assert.False(t, ok) |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
// command Noop |
449
|
|
|
func TestCmdNoopOk(t *testing.T) { |
450
|
|
|
resp := "200 result=0\n" |
451
|
|
|
rw := dummyReadWrite(resp) |
452
|
|
|
agi := &AGI{io: rw} |
453
|
|
|
err := agi.Noop() |
454
|
|
|
assert.Nil(t, err) |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
func TestCmdNoopFail(t *testing.T) { |
458
|
|
|
rw := dummyReadWriteWError() |
459
|
|
|
agi := &AGI{io: rw} |
460
|
|
|
err := agi.Noop() |
461
|
|
|
assert.NotNil(t, err) |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
// command ReceiveChar |
465
|
|
|
func TestCmdReceiveCharOk(t *testing.T) { |
466
|
|
|
rw := dummyReadWrite("200 result=5 (timeout)\n") |
467
|
|
|
agi := &AGI{io: rw} |
468
|
|
|
chr, err := agi.ReceiveChar(0) |
469
|
|
|
assert.Nil(t, err) |
470
|
|
|
assert.Equal(t, 5, chr) |
471
|
|
|
|
472
|
|
|
rw = dummyReadWrite("200 result=9\n") |
473
|
|
|
agi = &AGI{io: rw} |
474
|
|
|
chr, err = agi.ReceiveChar(500) |
475
|
|
|
assert.Nil(t, err) |
476
|
|
|
assert.Equal(t, 9, chr) |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
func TestCmdReceiveCharFail(t *testing.T) { |
480
|
|
|
rw := dummyReadWrite("200 result=-1 (hangup)\n") |
481
|
|
|
agi := &AGI{io: rw} |
482
|
|
|
chr, err := agi.ReceiveChar(1000) |
483
|
|
|
assert.NotNil(t, err) |
484
|
|
|
assert.Equal(t, -1, chr) |
485
|
|
|
|
486
|
|
|
rw = dummyReadWrite("200 result=0\n") |
487
|
|
|
agi = &AGI{io: rw} |
488
|
|
|
chr, err = agi.ReceiveChar(1000) |
489
|
|
|
assert.NotNil(t, err) |
490
|
|
|
assert.Equal(t, -1, chr) |
491
|
|
|
|
492
|
|
|
rw = dummyReadWriteWError() |
493
|
|
|
agi = &AGI{io: rw} |
494
|
|
|
chr, err = agi.ReceiveChar(1000) |
495
|
|
|
assert.NotNil(t, err) |
496
|
|
|
assert.Equal(t, -1, chr) |
497
|
|
|
} |
498
|
|
|
|
499
|
|
|
// command ReceiveText |
500
|
|
|
func TestCmdReceiveTextOk(t *testing.T) { |
501
|
|
|
rw := dummyReadWrite("200 result=1 (White fox is lost in the sea)\n") |
502
|
|
|
agi := &AGI{io: rw} |
503
|
|
|
text, err := agi.ReceiveText(0) |
504
|
|
|
assert.Nil(t, err) |
505
|
|
|
assert.Equal(t, "White fox is lost in the sea", text) |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
func TestCmdReceiveTextFail(t *testing.T) { |
509
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
510
|
|
|
agi := &AGI{io: rw} |
511
|
|
|
text, err := agi.ReceiveText(1000) |
512
|
|
|
assert.NotNil(t, err) |
513
|
|
|
assert.Equal(t, "", text) |
514
|
|
|
|
515
|
|
|
rw = dummyReadWriteWError() |
516
|
|
|
agi = &AGI{io: rw} |
517
|
|
|
text, err = agi.ReceiveText(1000) |
518
|
|
|
assert.NotNil(t, err) |
519
|
|
|
assert.Equal(t, "", text) |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
// command RecordFile |
523
|
|
|
func TestCmdRecordFileOk(t *testing.T) { |
524
|
|
|
rw := dummyReadWrite("200 result=48 (dtmf) endpos=554879\n") |
525
|
|
|
agi := &AGI{io: rw} |
526
|
|
|
err := agi.RecordFile("new_rec", "wav", "029", -1, 0, false, 0) |
527
|
|
|
assert.Nil(t, err) |
528
|
|
|
} |
529
|
|
|
|
530
|
|
|
func TestCmdRecordFileFail(t *testing.T) { |
531
|
|
|
rw := dummyReadWrite("200 result=-1 (hangup) endpos=554879\n") |
532
|
|
|
agi := &AGI{io: rw} |
533
|
|
|
err := agi.RecordFile("new_rec", "wav", "09", 1000, 1800, true, 500) |
534
|
|
|
assert.NotNil(t, err) |
535
|
|
|
|
536
|
|
|
rw = dummyReadWriteWError() |
537
|
|
|
agi = &AGI{io: rw} |
538
|
|
|
err = agi.RecordFile("new_rec", "wav", "09", 1000, 1800, true, 500) |
539
|
|
|
assert.NotNil(t, err) |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
// command SayAlpha |
543
|
|
|
func TestCmdSayAlphaOk(t *testing.T) { |
544
|
|
|
rw := dummyReadWrite("200 result=48\n") |
545
|
|
|
agi := &AGI{io: rw} |
546
|
|
|
err := agi.SayAlpha("abc", "10") |
547
|
|
|
assert.Nil(t, err) |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
func TestCmdSayAlphaFail(t *testing.T) { |
551
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
552
|
|
|
agi := &AGI{io: rw} |
553
|
|
|
err := agi.SayAlpha("abc", "10") |
554
|
|
|
assert.NotNil(t, err) |
555
|
|
|
|
556
|
|
|
rw = dummyReadWriteWError() |
557
|
|
|
agi = &AGI{io: rw} |
558
|
|
|
err = agi.SayAlpha("abc", "10") |
559
|
|
|
assert.NotNil(t, err) |
560
|
|
|
} |
561
|
|
|
|
562
|
|
|
// command SayDate |
563
|
|
|
func TestCmdSayDateOk(t *testing.T) { |
564
|
|
|
rw := dummyReadWrite("200 result=0\n") |
565
|
|
|
agi := &AGI{io: rw} |
566
|
|
|
err := agi.SayDate("1563844045", "0") |
567
|
|
|
assert.Nil(t, err) |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
// command SayDatetime |
571
|
|
|
func TestCmdSayDatetimeOk(t *testing.T) { |
572
|
|
|
rw := dummyReadWrite("200 result=0\n") |
573
|
|
|
agi := &AGI{io: rw} |
574
|
|
|
err := agi.SayDatetime("1563844045", "9", "", "") |
575
|
|
|
assert.Nil(t, err) |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
// command SayDigits |
579
|
|
|
func TestCmdSayDigitsOk(t *testing.T) { |
580
|
|
|
rw := dummyReadWrite("200 result=0\n") |
581
|
|
|
agi := &AGI{io: rw} |
582
|
|
|
err := agi.SayDigits("4045", "9") |
583
|
|
|
assert.Nil(t, err) |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
// command SayNumber |
587
|
|
|
func TestCmdSayNumberOk(t *testing.T) { |
588
|
|
|
rw := dummyReadWrite("200 result=0\n") |
589
|
|
|
agi := &AGI{io: rw} |
590
|
|
|
err := agi.SayNumber("4045", "9") |
591
|
|
|
assert.Nil(t, err) |
592
|
|
|
} |
593
|
|
|
|
594
|
|
|
// command SayPhonetic |
595
|
|
|
func TestCmdSayPhoneticOk(t *testing.T) { |
596
|
|
|
rw := dummyReadWrite("200 result=0\n") |
597
|
|
|
agi := &AGI{io: rw} |
598
|
|
|
err := agi.SayPhonetic("abcd", "9") |
599
|
|
|
assert.Nil(t, err) |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
// command SayTime |
603
|
|
|
func TestCmdSayTimeOk(t *testing.T) { |
604
|
|
|
rw := dummyReadWrite("200 result=0\n") |
605
|
|
|
agi := &AGI{io: rw} |
606
|
|
|
err := agi.SayTime("1563844046", "9") |
607
|
|
|
assert.Nil(t, err) |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
// command SendImage |
611
|
|
|
func TestCmdSendImageOk(t *testing.T) { |
612
|
|
|
rw := dummyReadWrite("200 result=0\n") |
613
|
|
|
agi := &AGI{io: rw} |
614
|
|
|
err := agi.SendImage("image_file") |
615
|
|
|
assert.Nil(t, err) |
616
|
|
|
} |
617
|
|
|
|
618
|
|
|
func TestCmdSendImageFail(t *testing.T) { |
619
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
620
|
|
|
agi := &AGI{io: rw} |
621
|
|
|
err := agi.SendImage("image_file") |
622
|
|
|
assert.NotNil(t, err) |
623
|
|
|
|
624
|
|
|
rw = dummyReadWriteWError() |
625
|
|
|
agi = &AGI{io: rw} |
626
|
|
|
err = agi.SendImage("image_file") |
627
|
|
|
assert.NotNil(t, err) |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
// command SendText |
631
|
|
|
func TestCmdSendTextOk(t *testing.T) { |
632
|
|
|
rw := dummyReadWrite("200 result=0\n") |
633
|
|
|
agi := &AGI{io: rw} |
634
|
|
|
err := agi.SendText("Hello there, friend!") |
635
|
|
|
assert.Nil(t, err) |
636
|
|
|
} |
637
|
|
|
|
638
|
|
|
func TestCmdSendTextFail(t *testing.T) { |
639
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
640
|
|
|
agi := &AGI{io: rw} |
641
|
|
|
err := agi.SendText("Hello there, friend!") |
642
|
|
|
assert.NotNil(t, err) |
643
|
|
|
|
644
|
|
|
rw = dummyReadWriteWError() |
645
|
|
|
agi = &AGI{io: rw} |
646
|
|
|
err = agi.SendText("Hello there, friend!") |
647
|
|
|
assert.NotNil(t, err) |
648
|
|
|
} |
649
|
|
|
|
650
|
|
|
// command SetAutoHangup |
651
|
|
|
func TestCmdSetAutoHangupOk(t *testing.T) { |
652
|
|
|
rw := dummyReadWrite("200 result=0\n") |
653
|
|
|
agi := &AGI{io: rw} |
654
|
|
|
err := agi.SetAutoHangup(0) |
655
|
|
|
assert.Nil(t, err) |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
func TestCmdSetAutoHangupFail(t *testing.T) { |
659
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
660
|
|
|
agi := &AGI{io: rw} |
661
|
|
|
err := agi.SetAutoHangup(1800) |
662
|
|
|
assert.NotNil(t, err) |
663
|
|
|
|
664
|
|
|
rw = dummyReadWriteWError() |
665
|
|
|
agi = &AGI{io: rw} |
666
|
|
|
err = agi.SetAutoHangup(800) |
667
|
|
|
assert.NotNil(t, err) |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
// command SetCallerid |
671
|
|
|
func TestCmdSetCalleridOk(t *testing.T) { |
672
|
|
|
rw := dummyReadWrite("200 result=1\n") |
673
|
|
|
agi := &AGI{io: rw} |
674
|
|
|
err := agi.SetCallerid("\"Achlie\" <5544>") |
675
|
|
|
assert.Nil(t, err) |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
func TestCmdSetCalleridFail(t *testing.T) { |
679
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
680
|
|
|
agi := &AGI{io: rw} |
681
|
|
|
err := agi.SetCallerid("5552452211") |
682
|
|
|
assert.NotNil(t, err) |
683
|
|
|
|
684
|
|
|
rw = dummyReadWriteWError() |
685
|
|
|
agi = &AGI{io: rw} |
686
|
|
|
err = agi.SetCallerid("5552452211") |
687
|
|
|
assert.NotNil(t, err) |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
// command SetContext |
691
|
|
|
func TestCmdSetContextOk(t *testing.T) { |
692
|
|
|
rw := dummyReadWrite("200 result=0\n") |
693
|
|
|
agi := &AGI{io: rw} |
694
|
|
|
err := agi.SetContext("default-inbound") |
695
|
|
|
assert.Nil(t, err) |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
func TestCmdSetContextFail(t *testing.T) { |
699
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
700
|
|
|
agi := &AGI{io: rw} |
701
|
|
|
err := agi.SetContext("default-inbound") |
702
|
|
|
assert.NotNil(t, err) |
703
|
|
|
|
704
|
|
|
rw = dummyReadWriteWError() |
705
|
|
|
agi = &AGI{io: rw} |
706
|
|
|
err = agi.SetContext("default-inbound") |
707
|
|
|
assert.NotNil(t, err) |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
// command SetExtension |
711
|
|
|
func TestCmdSetExtensionOk(t *testing.T) { |
712
|
|
|
rw := dummyReadWrite("200 result=0\n") |
713
|
|
|
agi := &AGI{io: rw} |
714
|
|
|
err := agi.SetExtension("55588") |
715
|
|
|
assert.Nil(t, err) |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
func TestCmdSetExtensionFail(t *testing.T) { |
719
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
720
|
|
|
agi := &AGI{io: rw} |
721
|
|
|
err := agi.SetExtension("s") |
722
|
|
|
assert.NotNil(t, err) |
723
|
|
|
|
724
|
|
|
rw = dummyReadWriteWError() |
725
|
|
|
agi = &AGI{io: rw} |
726
|
|
|
err = agi.SetExtension("VOICEMAIL") |
727
|
|
|
assert.NotNil(t, err) |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
// command SetMusic |
731
|
|
|
func TestCmdSetMusicOk(t *testing.T) { |
732
|
|
|
rw := dummyReadWrite("200 result=0\n") |
733
|
|
|
agi := &AGI{io: rw} |
734
|
|
|
err := agi.SetMusic("on", "hip-pop") |
735
|
|
|
assert.Nil(t, err) |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
func TestCmdSetMusicFail(t *testing.T) { |
739
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
740
|
|
|
agi := &AGI{io: rw} |
741
|
|
|
err := agi.SetMusic("off") |
742
|
|
|
assert.NotNil(t, err) |
743
|
|
|
|
744
|
|
|
agi = &AGI{io: rw} |
745
|
|
|
err = agi.SetMusic("foo", "bar") |
746
|
|
|
assert.NotNil(t, err) |
747
|
|
|
|
748
|
|
|
rw = dummyReadWriteWError() |
749
|
|
|
agi = &AGI{io: rw} |
750
|
|
|
err = agi.SetMusic("on") |
751
|
|
|
assert.NotNil(t, err) |
752
|
|
|
} |
753
|
|
|
|
754
|
|
|
// command SetPriority |
755
|
|
|
func TestCmdSetPriorityOk(t *testing.T) { |
756
|
|
|
rw := dummyReadWrite("200 result=0\n") |
757
|
|
|
agi := &AGI{io: rw} |
758
|
|
|
err := agi.SetPriority("1") |
759
|
|
|
assert.Nil(t, err) |
760
|
|
|
} |
761
|
|
|
|
762
|
|
|
func TestCmdSetPriorityFail(t *testing.T) { |
763
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
764
|
|
|
agi := &AGI{io: rw} |
765
|
|
|
err := agi.SetPriority("label") |
766
|
|
|
assert.NotNil(t, err) |
767
|
|
|
|
768
|
|
|
rw = dummyReadWriteWError() |
769
|
|
|
agi = &AGI{io: rw} |
770
|
|
|
err = agi.SetPriority("12") |
771
|
|
|
assert.NotNil(t, err) |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
// command SetVariable |
775
|
|
|
func TestCmdSetVariableOk(t *testing.T) { |
776
|
|
|
rw := dummyReadWrite("200 result=1\n") |
777
|
|
|
agi := &AGI{io: rw} |
778
|
|
|
err := agi.SetVariable("FOO", "1234") |
779
|
|
|
assert.Nil(t, err) |
780
|
|
|
} |
781
|
|
|
|
782
|
|
|
func TestCmdSetVariableFail(t *testing.T) { |
783
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
784
|
|
|
agi := &AGI{io: rw} |
785
|
|
|
err := agi.SetVariable("MOH", "hello") |
786
|
|
|
assert.NotNil(t, err) |
787
|
|
|
|
788
|
|
|
rw = dummyReadWriteWError() |
789
|
|
|
agi = &AGI{io: rw} |
790
|
|
|
err = agi.SetVariable("CALLERID(num)", "5558876541") |
791
|
|
|
assert.NotNil(t, err) |
792
|
|
|
} |
793
|
|
|
|
794
|
|
|
// command StreamFile |
795
|
|
|
func TestCmdStreamFileOk(t *testing.T) { |
796
|
|
|
rw := dummyReadWrite("200 result=48 endpos=123654\n") |
797
|
|
|
agi := &AGI{io: rw} |
798
|
|
|
r, err := agi.StreamFile("prompt.en", "0", 0) |
799
|
|
|
assert.Nil(t, err) |
800
|
|
|
assert.Equal(t, 48, r) |
801
|
|
|
} |
802
|
|
|
|
803
|
|
|
func TestCmdStreamFileFail(t *testing.T) { |
804
|
|
|
rw := dummyReadWrite("200 result=-1 endpos=1235\n") |
805
|
|
|
agi := &AGI{io: rw} |
806
|
|
|
r, err := agi.StreamFile("prompt.en", "", 1000) |
807
|
|
|
assert.NotNil(t, err) |
808
|
|
|
assert.Equal(t, -1, r) |
809
|
|
|
|
810
|
|
|
rw = dummyReadWriteWError() |
811
|
|
|
agi = &AGI{io: rw} |
812
|
|
|
r, err = agi.StreamFile("prompt.en", "123", 200) |
813
|
|
|
assert.NotNil(t, err) |
814
|
|
|
assert.Equal(t, -1, r) |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
// command TDDMode |
818
|
|
|
func TestCmdTDDModeOk(t *testing.T) { |
819
|
|
|
rw := dummyReadWrite("200 result=1\n") |
820
|
|
|
agi := &AGI{io: rw} |
821
|
|
|
err := agi.TDDMode("on") |
822
|
|
|
assert.Nil(t, err) |
823
|
|
|
} |
824
|
|
|
|
825
|
|
|
func TestCmdTDDModeFail(t *testing.T) { |
826
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
827
|
|
|
agi := &AGI{io: rw} |
828
|
|
|
err := agi.TDDMode("mate") |
829
|
|
|
assert.NotNil(t, err) |
830
|
|
|
|
831
|
|
|
rw = dummyReadWriteWError() |
832
|
|
|
agi = &AGI{io: rw} |
833
|
|
|
err = agi.TDDMode("off") |
834
|
|
|
assert.NotNil(t, err) |
835
|
|
|
} |
836
|
|
|
|
837
|
|
|
// command Verbose |
838
|
|
|
func TestCmdVerboseOk(t *testing.T) { |
839
|
|
|
rw := dummyReadWrite("200 result=1\n") |
840
|
|
|
agi := &AGI{io: rw} |
841
|
|
|
err := agi.Verbose("Hello World!") |
842
|
|
|
assert.Nil(t, err) |
843
|
|
|
|
844
|
|
|
rw = dummyReadWrite("200 result=1\n") |
845
|
|
|
agi = &AGI{io: rw} |
846
|
|
|
err = agi.Verbose("Hello World!", 4) |
847
|
|
|
assert.Nil(t, err) |
848
|
|
|
} |
849
|
|
|
|
850
|
|
|
func TestCmdVerboseFail(t *testing.T) { |
851
|
|
|
rw := dummyReadWriteWError() |
852
|
|
|
agi := &AGI{io: rw} |
853
|
|
|
err := agi.Verbose("gonna fail") |
854
|
|
|
assert.NotNil(t, err) |
855
|
|
|
} |
856
|
|
|
|
857
|
|
|
// command WaitForDigit |
858
|
|
|
func TestCmdWaitForDigitOk(t *testing.T) { |
859
|
|
|
rw := dummyReadWrite("200 result=0\n") |
860
|
|
|
agi := &AGI{io: rw} |
861
|
|
|
dig, err := agi.WaitForDigit(-1) |
862
|
|
|
assert.Nil(t, err) |
863
|
|
|
assert.Equal(t, "", dig) |
864
|
|
|
|
865
|
|
|
rw = dummyReadWrite("200 result=48\n") |
866
|
|
|
agi = &AGI{io: rw} |
867
|
|
|
dig, err = agi.WaitForDigit(1000) |
868
|
|
|
assert.Nil(t, err) |
869
|
|
|
assert.Equal(t, "0", dig) |
870
|
|
|
|
871
|
|
|
rw = dummyReadWrite("200 result=42\n") |
872
|
|
|
agi = &AGI{io: rw} |
873
|
|
|
dig, err = agi.WaitForDigit(2000) |
874
|
|
|
assert.Nil(t, err) |
875
|
|
|
assert.Equal(t, "*", dig) |
876
|
|
|
} |
877
|
|
|
|
878
|
|
|
func TestCmdWaitForDigitFail(t *testing.T) { |
879
|
|
|
rw := dummyReadWrite("200 result=-1\n") |
880
|
|
|
agi := &AGI{io: rw} |
881
|
|
|
dig, err := agi.WaitForDigit(-1) |
882
|
|
|
assert.NotNil(t, err) |
883
|
|
|
assert.Equal(t, "", dig) |
884
|
|
|
|
885
|
|
|
rw = dummyReadWriteWError() |
886
|
|
|
agi = &AGI{io: rw} |
887
|
|
|
dig, err = agi.WaitForDigit(-1) |
888
|
|
|
assert.NotNil(t, err) |
889
|
|
|
assert.Equal(t, "", dig) |
890
|
|
|
} |
891
|
|
|
|