-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
141 lines (125 loc) · 3.57 KB
/
test.js
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
/* globals describe, expect, jasmine, it */
var nock = require('nock')
var qs = require('qs')
var handler = require('./handler')
handler.setConfig('./config-example.json')
var trevorbot = handler.trevorbot
const TIMBUKTU = JSON.stringify({
location: {
people: {
city: 'Timbuktu',
country: 'Mali',
latitude: 17,
longitude: -3,
epoch_start: 1500249600,
epoch_end: 1500854400,
date_start: '2017-07-17',
date_end: '2017-07-24'
}
}
})
function makeQuery (text) {
return {
body: qs.stringify({
channel_id: 'TEST',
channel_name: 'test',
service_id: '80586480853',
team_domain: 'test',
team_id: 'TEST',
text: `@trevorbot ${text}`,
timestamp: '1500397663.929595',
token: 'TEST',
trigger_word: '@trevorbot',
user_id: 'TEST',
user_name: 'test_user'
})
}
}
function timeSafeSnapshot (s) {
expect(s.replace(/there is \*\d\d:\d\d\*/, 'there is **')).toMatchSnapshot()
}
describe('trevorbot', () => {
describe('problems', function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000
// needs to have function so mocha will provide this.timeout?
it('should handle server connection timeout', done => {
var scope = nock('https://nomadlist.com')
.get('/@trevorgerhardt.json')
.delayConnection(60000)
.reply(200, TIMBUKTU)
trevorbot(makeQuery('where is Trevor?'), null, (err, result) => {
scope.done()
expect(err).not.toBeTruthy()
expect(result).toMatchSnapshot()
done()
})
})
it('should handle server response timeout', done => {
var scope = nock('https://nomadlist.com')
.get('/@trevorgerhardt.json')
.socketDelay(60000)
.reply(200, TIMBUKTU)
trevorbot(makeQuery('where is Trevor?'), null, (err, result) => {
scope.done()
expect(err).not.toBeTruthy()
expect(result).toMatchSnapshot()
done()
})
})
})
describe('successes', () => {
it('unrecognized command should return something', done => {
trevorbot(makeQuery('Arblegarbl'), null, (err, result) => {
expect(err).not.toBeTruthy()
expect(result).toMatchSnapshot()
done()
})
})
it('where query for non-trevor person should not work', done => {
trevorbot(
makeQuery('where in the world is Carmen SanDiego?'),
null,
(err, result) => {
expect(err).not.toBeTruthy()
expect(result).toMatchSnapshot()
done()
}
)
})
it('where query for trevor should work', done => {
var scope = nock('https://nomadlist.com')
.get('/@trevorgerhardt.json')
.reply(200, TIMBUKTU)
trevorbot(makeQuery('where is Trevor?'), null, (err, result) => {
expect(err).not.toBeTruthy()
const body = JSON.parse(result.body)
timeSafeSnapshot(body.text)
scope.done()
done()
})
})
it('trevor should tell a Chuck Norris joke', done => {
var scope = nock('http://api.icndb.com').get('/jokes/random').reply(
200,
JSON.stringify({
type: 'success',
value: {
id: 554,
joke: 'Chuck Norris can install a 64 bit OS on 32 bit machines.',
categories: ['nerdy']
}
})
)
trevorbot(
makeQuery('tell me about Chuck Norris'),
null,
(err, result) => {
expect(err).not.toBeTruthy()
expect(result).toMatchSnapshot()
scope.done()
done()
}
)
})
})
})