-
Notifications
You must be signed in to change notification settings - Fork 5
/
04-scene_test.coffee
604 lines (480 loc) · 20.6 KB
/
04-scene_test.coffee
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
sinon = require 'sinon'
chai = require 'chai'
should = chai.should()
chai.use require 'sinon-chai'
co = require 'co'
_ = require 'lodash'
pretend = require 'hubot-pretend'
Dialogue = require '../../src/modules/dialogue'
Scene = require '../../src/modules/scene'
clock = null
matchRes = null
matchAny = /.*/
describe 'Scene', ->
beforeEach ->
pretend.start()
pretend.log.level = 'silent'
clock = sinon.useFakeTimers()
matchRes = sinon.match.instanceOf pretend.robot.Response
.and sinon.match.has 'dialogue'
Object.getOwnPropertyNames(Scene.prototype).map (key) ->
sinon.spy Scene.prototype, key
afterEach ->
pretend.shutdown()
clock.restore()
Object.getOwnPropertyNames(Scene.prototype).map (key) ->
Scene.prototype[key].restore()
describe 'constructor', ->
context 'without options', ->
it 'defaults to `user` scope', ->
scene = new Scene pretend.robot
scene.config.scope.should.equal 'user'
it 'attaches receive middleware to robot', ->
new Scene pretend.robot
pretend.robot.receiveMiddleware.should.have.calledOnce
context 'with options', ->
it 'stored options in config object', ->
scene = new Scene pretend.robot, sendReplies: true
scene.config.sendReplies.should.be.true
context 'with room scope option', ->
it 'accepts given room scope', ->
scene = new Scene pretend.robot, scope: 'room'
scene.config.scope.should.equal 'room'
it 'stores config with default options for scope', ->
scene = new Scene pretend.robot, scope: 'room'
scene.config.sendReplies.should.be.true
context 'with invalid scope', ->
it 'throws error when given invalid scope', ->
try new Scene pretend.robot, scope: 'monkey'
Scene.prototype.constructor.should.throw
describe '.listen', ->
it 'accepts a string that can be cast as RegExp', ->
scene = new Scene pretend.robot
scene.listen 'hear', '/test/i', () -> null
pretend.robot.listeners.pop().regex.should.eql /test/i
context 'with hear type and message matching regex', ->
it 'registers a robot hear listener with scene as attribute', ->
scene = new Scene pretend.robot
scene.listen 'hear', /test/, () -> null
pretend.robot.hear.should.have.calledWithMatch sinon.match.regexp
, sinon.match.has 'scene', scene
, sinon.match.func
it 'calls callback from listener when matched', -> co ->
scene = new Scene pretend.robot
callback = sinon.spy()
scene.listen 'hear', /test/, callback
yield pretend.user('tester').send 'test'
callback.should.have.calledOnce
it 'callback should receive res and dialogue', ->
scene = new Scene pretend.robot
callback = sinon.spy()
scene.listen 'hear', /test/, callback
pretend.user('tester').send 'test'
.then ->
callback.should.have.calledWith matchRes
context 'with respond type and message matching regex', ->
it 'registers a robot respond listener with scene as attribute', ->
scene = new Scene pretend.robot
callback = sinon.spy()
id = scene.listen 'respond', /test/, callback
pretend.user('tester').send 'hubot test'
.then ->
pretend.robot.respond.should.have.calledWithMatch sinon.match.regexp
, sinon.match.has 'scene', scene
, sinon.match.func
it 'calls callback from listener when matched', -> co ->
scene = new Scene pretend.robot
callback = sinon.spy()
id = scene.listen 'respond', /test/, callback
yield pretend.user('tester').send 'hubot test'
callback.should.have.calledOnce
it 'callback should receive res and dialogue', -> co ->
scene = new Scene pretend.robot
callback = sinon.spy()
id = scene.listen 'respond', /test/, callback
yield pretend.user('tester').send 'hubot test'
callback.should.have.calledWith matchRes
context 'with an invalid listener type', ->
it 'throws', ->
scene = new Scene pretend.robot
try scene.listen 'smell', /test/, -> null
scene.listen.should.throw
context 'with an invalid regex', ->
it 'throws', ->
scene = new Scene pretend.robot
try scene.listen 'hear', 'test', -> null
scene.listen.should.throw
context 'with an invalid callback', ->
it 'throws', ->
scene = new Scene pretend.robot
try scene.listen 'hear', /test/, { not: 'a function '}
scene.listen.should.throw
describe '.hear', ->
it 'calls .listen with hear listen type and arguments', ->
scene = new Scene pretend.robot
scene.hear /test/, -> null
expectedArgs = ['hear', /test/, sinon.match.func]
scene.listen.getCall(0).should.have.calledWith expectedArgs...
describe '.respond', ->
it 'calls .listen with respond listen type and arguments', ->
scene = new Scene pretend.robot
scene.respond /test/, -> null
expectedArgs = ['respond', /test/, sinon.match.func]
scene.listen.getCall(0).should.have.calledWith expectedArgs...
describe '.whoSpeaks', ->
beforeEach ->
pretend.user('tester', { id: 'user_111', room: 'testing' }).send('test')
context 'user scene', ->
it 'returns the ID of engaged user', ->
scene = new Scene pretend.robot, scope: 'user'
scene.whoSpeaks pretend.lastReceive()
.should.equal 'user_111'
context 'room sceene', ->
it 'returns the room ID', ->
scene = new Scene pretend.robot, scope: 'room'
scene.whoSpeaks pretend.lastReceive()
.should.equal 'testing'
context 'direct scene', ->
it 'returns the concatenated user ID and room ID', ->
scene = new Scene pretend.robot, scope: 'direct'
scene.whoSpeaks pretend.lastReceive()
.should.equal 'user_111_testing'
describe '.registerMiddleware', ->
it 'accepts function for enter middleware stack', ->
piece = (context, next, done) -> null
scene = new Scene pretend.robot
scene.registerMiddleware piece
scene.enterMiddleware.stack[0].should.eql piece
it 'throws when given function with incorrect arguments', ->
piece = (notEnoughArgs) -> null
scene = new Scene pretend.robot
try scene.registerMiddleware piece
scene.registerMiddleware.should.throw
scene.enterMiddleware.stack.length.should.equal 0
describe '.enter', ->
beforeEach ->
pretend.user('tester', { id: 'user_111', room: 'testing' }).send('test')
it 'returns a promise (then-able)', ->
scene = new Scene pretend.robot
promise = scene.enter pretend.lastReceive()
promise.then.should.be.a 'function'
it 'calls .processEnter with context', -> co ->
scene = new Scene pretend.robot
yield scene.enter pretend.lastReceive()
scene.processEnter.should.have.calledOnce
it 'resolves with created dialogue', -> co ->
scene = new Scene pretend.robot
result = yield scene.enter pretend.lastReceive()
result.should.be.instanceof Dialogue
context 'with callback', ->
it 'calls callback with final enter process context', (done) ->
scene = new Scene pretend.robot
keys = ['response', 'participants', 'options', 'arguments', 'dialogue']
callback = (result) ->
result.should.have.all.keys keys...
done()
scene.enter pretend.lastReceive(), callback
return
context 'with passing middleware', ->
it 'completes processing to resolve with dialogue', -> co ->
scene = new Scene pretend.robot
scene.registerMiddleware (context, next, done) -> next()
result = yield scene.enter pretend.lastReceive()
result.should.be.instanceof Dialogue
it 'calls callback with final enter process context', (done) ->
scene = new Scene(pretend.robot)
keys = ['response', 'participants', 'options', 'arguments', 'dialogue']
callback = (result) ->
result.should.have.all.keys keys...
done()
scene.registerMiddleware (context, next, done) -> next()
scene.enter pretend.lastReceive(), callback
return
context 'with blocking middleware', ->
it 'does not complete or call .processEnter', -> co ->
scene = new Scene pretend.robot
scene.registerMiddleware (context, next, done) -> done()
yield scene.enter pretend.lastReceive()
scene.processEnter.should.not.have.called
context 'with custom done function override', ->
it 'completes processing and calls custom done', -> co ->
scene = new Scene pretend.robot
custom = sinon.spy()
scene.registerMiddleware (context, next, done) -> next () ->
custom()
done()
yield scene.enter pretend.lastReceive()
custom.should.have.calledOnce
context 'with multiple middleware pieces', ->
it 'calls each sequentially passing ammended context', (done) ->
scene = new Scene pretend.robot
scene.registerMiddleware (context, next, done) ->
context.trace = ['A']
next()
scene.registerMiddleware (context, next, done) ->
context.trace.push('B')
next()
scene.registerMiddleware (context, next, done) ->
context.trace.push('C')
next()
scene.enter pretend.lastReceive(), (result) ->
result.trace.should.eql ['A', 'B', 'C']
done()
return
it 'does not process enter if any middleware blocks', -> co ->
scene = new Scene pretend.robot
scene.registerMiddleware (context, next, done) -> next()
scene.registerMiddleware (context, next, done) -> done()
scene.registerMiddleware (context, next, done) -> next()
yield scene.enter pretend.lastReceive()
scene.processEnter.should.not.have.called
context 'user scene', ->
it 'saves engaged Dialogue instance with user ID', -> co ->
scene = new Scene pretend.robot, scope: 'user'
yield scene.enter pretend.lastReceive()
scene.engaged['user_111'].should.be.instanceof Dialogue
context 'room scene', ->
it 'saves engaged Dialogue instance with room key', -> co ->
scene = new Scene pretend.robot, scope: 'room'
yield scene.enter pretend.lastReceive()
scene.engaged['testing'].should.be.instanceof Dialogue
context 'direct scene', ->
it 'saves engaged Dialogue instance with composite key', -> co ->
scene = new Scene pretend.robot, scope: 'direct'
yield scene.enter pretend.lastReceive()
scene.engaged['user_111_testing'].should.be.instanceof Dialogue
context 'with timeout options', ->
it 'passes the options to dialogue config', ->
scene = new Scene pretend.robot
dialogue = yield scene.enter pretend.lastReceive(),
timeout: 100
timeoutText: 'foo'
dialogue.config.timeout.should.equal 100
dialogue.config.timeoutText.should.equal 'foo'
context 'dialogue allowed to timeout after branch added', ->
it 'calls .exit first on "timeout"', (done) ->
scene = new Scene pretend.robot
scene.enter pretend.lastReceive(),
timeout: 10,
timeoutText: null
.then (dialogue)->
dialogue.on 'end', ->
scene.exit.getCall(0)
.should.have.calledWith pretend.lastReceive(), 'timeout'
done()
dialogue.startTimeout()
clock.tick 11
return
it 'calls .exit again on "incomplete"', (done) ->
scene = new Scene pretend.robot
scene.enter pretend.lastReceive(),
timeout: 10,
timeoutText: null
.then (dialogue) ->
dialogue.on 'end', ->
scene.exit.getCall(1)
.should.have.calledWith pretend.lastReceive(), 'incomplete'
done()
dialogue.startTimeout()
clock.tick 11
return
context 'dialogue completed (by message matching branch)', ->
it 'calls .exit once only', -> co ->
scene = new Scene pretend.robot
dialogue = yield scene.enter pretend.lastReceive()
dialogue.addBranch matchAny, ''
yield pretend.user('tester').send 'test'
yield pretend.user('tester').send 'testing again'
scene.exit.should.have.calledOnce
it 'calls .exit once with last (matched) res and "complete"', -> co ->
scene = new Scene pretend.robot
dialogue = yield scene.enter pretend.lastReceive()
dialogue.addBranch matchAny, ''
yield pretend.user('tester').send 'test'
yield pretend.user('tester').send 'testing again'
scene.exit.should.have.calledWith dialogue.res, 'complete'
context 're-enter currently engaged participants', ->
it 'returns error the second time', -> co ->
scene = new Scene pretend.robot
resultA = yield scene.enter pretend.lastReceive()
try resultB = yield scene.enter pretend.lastReceive()
scene.enter.should.throw
it 'rejected enter can be caught', (done) ->
scene = new Scene pretend.robot
scene.enter pretend.lastReceive()
.then ->
scene.enter pretend.lastReceive()
.catch (err) ->
err.should.be.instanceof Error
done()
return
context 're-enter previously engaged participants', ->
it 'returns Dialogue instance (as per normal)', -> co ->
scene = new Scene pretend.robot
dialogueA = yield scene.enter pretend.lastReceive()
scene.exit pretend.lastReceive() # no reason given
dialogueB = yield scene.enter pretend.lastReceive()
dialogueB.should.be.instanceof Dialogue
describe '.exit', ->
beforeEach ->
pretend.user('tester', { id: 'user_111', room: 'testing' }).send('test')
context 'with user in scene, called manually', ->
it 'does not call onTimeout on dialogue', -> co ->
scene = new Scene pretend.robot
dialogue = yield scene.enter pretend.lastReceive(), timeout: 10
dialogue.addBranch matchAny, '' # starts timeout
timeout = sinon.spy()
dialogue.onTimeout timeout
scene.exit pretend.lastReceive(), 'testing exits'
clock.tick 11
timeout.should.not.have.called
it 'removes the dialogue instance from engaged array', -> co ->
scene = new Scene pretend.robot
yield scene.enter pretend.lastReceive(), timeout: 10
scene.exit pretend.lastReceive(), 'testing exits'
should.not.exist scene.engaged['user_111']
it 'returns true', -> co ->
scene = new Scene pretend.robot
yield scene.enter pretend.lastReceive(), timeout: 10
scene.exit pretend.lastReceive(), 'testing exits'
clock.tick 11
scene.exit.returnValues.pop().should.be.true
it 'logged the reason', -> co ->
scene = new Scene pretend.robot
scene.id = 'scene_111'
yield scene.enter pretend.lastReceive(), timeout: 10
scene.exit pretend.lastReceive(), 'testing exits'
clock.tick 11
pretend.logs.pop().should.eql [
'info', 'Disengaged user user_111 (testing exits) (id: scene_111)'
]
it 'dialogue does not continue receiving after scene exit', -> co ->
scene = new Scene pretend.robot
dialogue = yield scene.enter pretend.lastReceive(), timeout: 10
dialogue.addBranch matchAny, '' # starts timeout
dialogue.receive = sinon.spy()
scene.exit pretend.lastReceive(), 'tester'
pretend.user('tester').send 'test'
clock.tick 11
dialogue.receive.should.not.have.called
context 'with user in scene, called from events', ->
it 'gets called twice (on timeout and end)', (done) ->
scene = new Scene pretend.robot
scene.enter pretend.lastReceive(), timeout: 10
.then (dialogue) ->
dialogue.on 'end', ->
scene.exit.should.have.calledTwice
done()
dialogue.addBranch matchAny, '' # starts timeout
clock.tick 11
return
it 'returns true the first time', (done) ->
scene = new Scene pretend.robot
scene.enter pretend.lastReceive(), timeout: 10
.then (dialogue) ->
dialogue.on 'end', ->
scene.exit.getCall(0).should.have.returned true
done()
dialogue.addBranch matchAny, '' # starts timeout
clock.tick 11
return
it 'returns false the second time (because already disengaged)', (done) ->
scene = new Scene pretend.robot
scene.enter pretend.lastReceive(), timeout: 10
.then (dialogue) ->
dialogue.on 'end', ->
scene.exit.getCall(1).should.have.returned false
done()
dialogue.addBranch matchAny, '' # starts timeout
clock.tick 11
return
context 'user not in scene, called manually', ->
it 'returns false', ->
scene = new Scene pretend.robot
scene.exit pretend.lastReceive(), 'testing exits'
scene.exit.returnValues.pop().should.be.false
describe '.exitAll', ->
context 'with two users in scene', ->
it 'created two dialogues', -> co ->
scene = new Scene pretend.robot
dialogueB = yield scene.enter pretend.response 'A', 'test'
dialogueA = yield scene.enter pretend.response 'B', 'test'
scene.exitAll()
dialogueA.should.be.instanceof Dialogue
dialogueB.should.be.instanceof Dialogue
it 'calls clearTimeout on both dialogues', -> co ->
scene = new Scene pretend.robot
dialogueB = yield scene.enter pretend.response 'A', 'test'
dialogueA = yield scene.enter pretend.response 'B', 'test'
dialogueA.clearTimeout = sinon.spy()
dialogueB.clearTimeout = sinon.spy()
scene.exitAll()
dialogueA.clearTimeout.should.have.calledOnce
dialogueB.clearTimeout.should.have.calledOnce
it 'has no remaining engaged dialogues', -> co ->
scene = new Scene pretend.robot
dialogueB = yield scene.enter pretend.response 'A', 'test'
dialogueA = yield scene.enter pretend.response 'B', 'test'
scene.exitAll()
scene.engaged.length.should.equal 0
describe '.getDialogue', ->
beforeEach ->
pretend.user('tester', { id: 'user_111', room: 'testing' }).send('test')
context 'with user in scene', ->
it 'returns the matching dialogue', -> co ->
scene = new Scene pretend.robot
dialogueA = yield scene.enter pretend.lastReceive()
dialogueB = scene.getDialogue 'user_111'
dialogueB.should.eql dialogueA
context 'no user in scene', ->
it 'returns undefined', ->
scene = new Scene pretend.robot
dialogue = scene.getDialogue 'user_111'
should.not.exist dialogue
describe '.inDialogue', ->
beforeEach ->
pretend.user('tester', { id: 'user_111', room: 'testing' }).send('test')
context 'in engaged user scene', ->
it 'returns true with user ID', -> co ->
scene = new Scene pretend.robot
yield scene.enter pretend.lastReceive()
scene.inDialogue 'user_111'
.should.be.true
it 'returns false with room name', -> co ->
scene = new Scene pretend.robot
yield scene.enter pretend.lastReceive()
scene.inDialogue 'testing'
.should.be.false
context 'participant not in scene', ->
it 'returns false', -> co ->
scene = new Scene pretend.robot
yield scene.enter pretend.lastReceive()
scene.inDialogue 'user_222'
.should.be.false
context 'room scene, in scene', ->
it 'returns true with roomname', -> co ->
scene = new Scene pretend.robot, scope: 'room'
yield scene.enter pretend.lastReceive()
scene.inDialogue 'testing'
.should.be.true
it 'returns false with user ID', -> co ->
scene = new Scene pretend.robot, scope: 'room'
yield scene.enter pretend.lastReceive()
scene.inDialogue 'user_111'
.should.be.false
context 'direct scene, in scene', ->
it 'returns true with userID_roomID', -> co ->
scene = new Scene pretend.robot, scope: 'direct'
yield scene.enter pretend.lastReceive()
scene.inDialogue 'user_111_testing'
.should.be.true
it 'returns false with roomname', -> co ->
scene = new Scene pretend.robot, scope: 'direct'
yield scene.enter pretend.lastReceive()
scene.inDialogue 'testing'
.should.be.false
it 'returns false with user ID', -> co ->
scene = new Scene pretend.robot, scope: 'direct'
yield scene.enter pretend.lastReceive()
scene.inDialogue 'user_111'
.should.be.false