-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
512 lines (451 loc) · 14.5 KB
/
command_test.go
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
package goagi
import (
"bytes"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func stubReaderWriter(response string) (*bytes.Buffer, *stubReader, *stubWriter) {
buf := new(bytes.Buffer)
response += "\n"
reader := &stubReader{strings.NewReader(response)}
writer := &stubWriter{buf}
return buf, reader, writer
}
func mockAGI(response string) (*AGI, *bytes.Buffer) {
buf, r, w := stubReaderWriter(response)
return &AGI{reader: r, writer: w}, buf
}
const respOk = "200 result=1"
func TestCmdCommand(t *testing.T) {
buf, r, w := stubReaderWriter(respOk)
agi := &AGI{reader: r, writer: w}
resp, err := agi.Command("ANSWER")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "ANSWER\n", buf.String())
}
func TestCmdAnswer(t *testing.T) {
agi, buf := mockAGI("511 Command Not Permitted")
resp, err := agi.Answer()
assert.Nil(t, err)
assert.Equal(t, 511, resp.Code())
assert.Equal(t, "ANSWER\n", buf.String())
}
func TestCmdAsyncAGIBreak(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.AsyncAGIBreak()
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "ASYNCAGI BREAK\n", buf.String())
}
func TestCmdChannelStatus(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.ChannelStatus("SIP/0001-FA878E0")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "CHANNEL STATUS SIP/0001-FA878E0\n", buf.String())
}
func TestCmdControlStreamFile(t *testing.T) {
agi, buf := mockAGI("200 result=1 endpos=10")
resp, err := agi.ControlStreamFile("welcome", "")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.EqualValues(t, 10, resp.EndPos())
assert.Equal(t, "CONTROL STREAM FILE welcome \"\"\n", buf.String())
agi, buf = mockAGI("200 result=1 endpos=998877")
resp, _ = agi.ControlStreamFile("welcome", "123", "1500")
assert.EqualValues(t, 998877, resp.EndPos())
assert.Equal(t, `CONTROL STREAM FILE welcome "123" "1500"`+"\n", buf.String())
agi, buf = mockAGI("200 result=1 endpos=10")
resp, _ = agi.ControlStreamFile("welcome", "123", "1500", "#", "0", "*", "1600")
assert.Equal(t, `CONTROL STREAM FILE welcome "123" "1500" "#" "0" "*" "1600"`+"\n",
buf.String())
_, err = agi.ControlStreamFile("welcome", "123", "1500", "#", "0", "*", "1600", "a", "1")
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "[a 1]")
}
func TestCmdDatabaseDel(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.DatabaseDel("channel/sip", "foo")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "DATABASE DEL channel/sip foo\n", buf.String())
}
func TestCmdDatabaseDelTree(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.DatabaseDelTree("channel", "sip")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "DATABASE DELTREE channel sip\n", buf.String())
}
func TestCmdDatabaseGet(t *testing.T) {
agi, buf := mockAGI("200 result=1 (SIP/router01-000e57a5)\n")
resp, err := agi.DatabaseGet("channel", "sip/111")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "SIP/router01-000e57a5", resp.Value())
assert.Equal(t, "DATABASE GET channel sip/111\n", buf.String())
}
func TestCmdDatabasePut(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.DatabasePut("callwait", "sip/1111", "on")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "DATABASE PUT callwait sip/1111 on\n", buf.String())
}
func TestCmdExec(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.Exec("GoTo", "default,s,1")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, "EXEC GoTo \"default,s,1\"\n", buf.String())
}
func TestCmdGetData(t *testing.T) {
tests := []struct {
response string
file string
tout int
max int
code int
result int
value string
data string
exec string
}{
{
"200 result= (timeout)\n",
"mainmenu", 0, 1,
200, 0, "timeout", "",
"GET DATA mainmenu 0 1\n",
}, {
"200 result=*123 (timeout)\n",
"mainmenu", 1500, 4,
200, 0, "timeout", "*123",
"GET DATA mainmenu 1500 4\n",
}, {
"200 result=23\n",
"hollidays", 2000, 2,
200, 0, "", "23",
"GET DATA hollidays 2000 2\n",
}, {
"200 result=-1\n",
"hollidays", 3000, 3,
200, -1, "", "",
"GET DATA hollidays 3000 3\n",
}, {
"200 result=\n", // user press # only
"hollidays", 3000, 3,
200, 0, "", "",
"GET DATA hollidays 3000 3\n",
}, {
"511 Command Not Permitted on a dead channel or intercept routine\n",
"hollidays", 3000, 3,
511, 0, "Command Not Permitted on a dead channel or intercept routine",
"Command Not Permitted on a dead channel or intercept routine",
"GET DATA hollidays 3000 3\n",
},
}
for _, tc := range tests {
agi, buf := mockAGI(tc.response)
resp, err := agi.GetData(tc.file, tc.tout, tc.max)
assert.Nil(t, err)
assert.Equal(t, tc.code, resp.Code(), "Code:"+tc.response)
assert.Equal(t, tc.result, resp.Result(), "Result:"+tc.response)
assert.Equal(t, tc.value, resp.Value(), "Value:"+tc.response)
assert.Equal(t, tc.data, resp.Data(), "Data:"+tc.response)
assert.Equal(t, tc.exec, buf.String(), tc.response)
}
agi, _ := mockAGI("foo")
resp, err := agi.GetData("hello", 1000, 3)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Invalid input")
assert.Nil(t, resp)
}
func TestCmdGetFullVariable(t *testing.T) {
agi, buf := mockAGI("200 result=1 (fr)")
resp, err := agi.GetFullVariable("CHANNEL(language)", "")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "fr", resp.Value())
assert.Equal(t, "GET FULL VARIABLE CHANNEL(language)\n", buf.String())
agi, buf = mockAGI("200 result=1 (Today is sunny)")
resp, err = agi.GetFullVariable("WEATHER", "SIP/2222-12-00000008")
assert.Nil(t, err)
assert.Equal(t, "Today is sunny", resp.Value())
assert.Equal(t, "GET FULL VARIABLE WEATHER SIP/2222-12-00000008\n", buf.String())
}
func TestCmdGetOption(t *testing.T) {
agi, buf := mockAGI("200 result=0 endpos=7680")
resp, err := agi.GetOption("welcome_menu", "", 500)
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, 0, resp.Result())
assert.Equal(t, "GET OPTION welcome_menu \"\" 500\n", buf.String())
}
func TestCmdGetVariable(t *testing.T) {
agi, buf := mockAGI(`200 result=1 ("Alice Foo" <5145553322>)`)
resp, err := agi.GetVariable("CALLERID(all)")
assert.Nil(t, err)
assert.Equal(t, 200, resp.Code())
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "\"Alice Foo\" <5145553322>", resp.Value())
assert.Equal(t, "GET VARIABLE CALLERID(all)\n", buf.String())
}
func TestCmdHangup(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.Hangup()
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "HANGUP\n", buf.String())
agi, buf = mockAGI(respOk)
resp, err = agi.Hangup("SIP/111-222")
assert.Nil(t, err)
assert.Equal(t, "HANGUP SIP/111-222\n", buf.String())
agi, buf = mockAGI(respOk)
resp, err = agi.Hangup("IAX/333-444", "SIP/111-222")
assert.Nil(t, err)
assert.Equal(t, "HANGUP IAX/333-444\n", buf.String())
}
func TestCmdReceiveChar(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.ReceiveChar(2000)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "RECEIVE CHAR 2000\n", buf.String())
}
func TestCmdReceiveText(t *testing.T) {
agi, buf := mockAGI("200 result=1 (Hello Bob)")
resp, err := agi.ReceiveText(2000)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "Hello Bob", resp.Value())
assert.Equal(t, "RECEIVE TEXT 2000\n", buf.String())
}
func TestCmdRecordFile(t *testing.T) {
tests := []struct {
input string
file, ftype, digit string
tout, offset int
beep bool
silence int
result int
value, data string
endpos int64
cmd string
}{
{
"200 result=1 (timeout) endpos=86435\n",
"new_rec", "wav", "029", -1, 0, false, 0,
1, "timeout", "", 86435,
"RECORD FILE new_rec wav \"029\" -1\n",
}, {
"200 result=* (dtmf) endpos=1554\n",
"new_rec", "wav", "*#", 1000, 600, false, 0,
1, "dtmf", "*", 1554,
"RECORD FILE new_rec wav \"*#\" 1000 600\n",
}, {
"200 result=-1 (hangup) endpos=0\n",
"new_rec", "wav", "*#", 1000, 600, true, 500,
-1, "hangup", "", 0,
"RECORD FILE new_rec wav \"*#\" 1000 600 BEEP s=500\n",
}, {
"200 result=4 (dtmf) endpos=0\n",
"new_rec", "wav", "*#4", 1000, 600, false, 500,
1, "dtmf", "4", 0,
"RECORD FILE new_rec wav \"*#4\" 1000 600 s=500\n",
},
}
for _, tc := range tests {
agi, buf := mockAGI(tc.input)
resp, err := agi.RecordFile(tc.file, tc.ftype, tc.digit, tc.tout, tc.offset,
tc.beep, tc.silence)
assert.Nil(t, err)
assert.Equal(t, tc.result, resp.Result())
assert.Equal(t, tc.value, resp.Value())
assert.Equal(t, tc.data, resp.Data())
assert.Equal(t, tc.endpos, resp.EndPos())
assert.Equal(t, tc.cmd, buf.String())
}
agi, buf := mockAGI("")
resp, err := agi.RecordFile("new_rec", "wav", "", 100, 0, false, 0)
assert.NotNil(t, err)
assert.Nil(t, resp)
assert.Equal(t, "RECORD FILE new_rec wav \"\" 100\n", buf.String())
}
func TestCmdSayAlpha(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SayAlpha("abc", "*1")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SAY ALPHA abc \"*1\"\n", buf.String())
}
func TestCmdSayDate(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SayDate("1563844045", "")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SAY DATE 1563844045 \"\"\n", buf.String())
}
func TestCmdSayDatetime(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SayDatetime("1563844045", "*#0", "dB", "UTC")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SAY DATETIME 1563844045 \"*#0\" \"dB\" \"UTC\"\n", buf.String())
}
func TestCmdSayDigits(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SayDigits("2234", "")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SAY DIGITS 2234 \"\"\n", buf.String())
}
func TestCmdSayNumber(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SayNumber("1000", "01")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SAY NUMBER 1000 \"01\"\n", buf.String())
}
func TestCmdSayPhonetic(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SayPhonetic("welcome", "")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SAY PHONETIC welcome \"\"\n", buf.String())
}
func TestCmdSayTime(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SayTime("1563844046", "*#0123456789")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SAY TIME 1563844046 \"*#0123456789\"\n", buf.String())
}
func TestCmdSendImage(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SendImage("logo.png")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SEND IMAGE \"logo.png\"\n", buf.String())
}
func TestCmdSendText(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SendText("Hello there!")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SEND TEXT \"Hello there!\"\n", buf.String())
}
func TestCmdSetAutoHangup(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SetAutoHangup(0)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SET AUTOHANGUP 0\n", buf.String())
}
func TestCmdSetCallerid(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SetCallerid("5145553322")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SET CALLERID \"5145553322\"\n", buf.String())
}
func TestCmdSetContext(t *testing.T) {
agi, buf := mockAGI("200 result=0")
resp, err := agi.SetContext("default")
assert.Nil(t, err)
assert.Equal(t, 0, resp.Result())
assert.Equal(t, "SET CONTEXT default\n", buf.String())
}
func TestCmdSetExtension(t *testing.T) {
agi, buf := mockAGI("200 result=0")
resp, err := agi.SetExtension("s")
assert.Nil(t, err)
assert.Equal(t, 0, resp.Result())
assert.Equal(t, "SET EXTENSION s\n", buf.String())
}
func TestCmdSetMusic(t *testing.T) {
tests := []struct {
enable bool
class string
cmd string
}{
{true, "", "SET MUSIC on \"\"\n"},
{true, "jazz", "SET MUSIC on \"jazz\"\n"},
{false, "lounge", "SET MUSIC off \"lounge\"\n"},
}
for _, tc := range tests {
agi, buf := mockAGI(respOk)
resp, err := agi.SetMusic(tc.enable, tc.class)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, tc.cmd, buf.String())
}
}
func TestCmdSetPriority(t *testing.T) {
agi, buf := mockAGI("200 result=0")
resp, err := agi.SetPriority("1")
assert.Nil(t, err)
assert.Equal(t, 0, resp.Result())
assert.Equal(t, "SET PRIORITY 1\n", buf.String())
}
func TestCmdSetVariable(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.SetVariable("CITY", "Toronto")
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "SET VARIABLE CITY \"Toronto\"\n", buf.String())
agi, buf = mockAGI(respOk)
resp, err = agi.SetVariable("CALLERID(all)", `"Alice Foo" <5145553322>`)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, `SET VARIABLE CALLERID(all) "\"Alice Foo\" <5145553322>"`+"\n",
buf.String())
}
func TestCmdStreamFile(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.StreamFile("rec109234", "", 0)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "STREAM FILE rec109234 \"\" 0\n", buf.String())
}
func TestCmdTDDMode(t *testing.T) {
tests := []struct {
mode, cmd string
}{
{"on", "TDD MODE on\n"},
{"off", "TDD MODE off\n"},
{"mate", "TDD MODE mate\n"},
{"tdd", "TDD MODE tdd\n"},
{"foo", "TDD MODE off\n"},
}
for _, tc := range tests {
agi, buf := mockAGI(respOk)
resp, err := agi.TDDMode(tc.mode)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, tc.cmd, buf.String())
}
}
func TestCmdVerbose(t *testing.T) {
agi, buf := mockAGI(respOk)
agi.Verbose("Debug message", 3)
assert.Equal(t, "VERBOSE \"Debug message\" 3\n", buf.String())
agi, buf = mockAGI(respOk)
agi.Verbose("Debug message")
assert.Equal(t, "VERBOSE \"Debug message\" 1\n", buf.String())
agi, buf = mockAGI(respOk)
agi.Verbose("Debug message", 0)
assert.Equal(t, "VERBOSE \"Debug message\" 1\n", buf.String())
agi, buf = mockAGI(respOk)
agi.Verbose("Debug message", 10)
assert.Equal(t, "VERBOSE \"Debug message\" 1\n", buf.String())
}
func TestCmdWaitForDigit(t *testing.T) {
agi, buf := mockAGI(respOk)
resp, err := agi.WaitForDigit(1000)
assert.Nil(t, err)
assert.Equal(t, 1, resp.Result())
assert.Equal(t, "WAIT FOR DIGIT 1000\n", buf.String())
}