Skip to content

Commit

Permalink
Fix basic messaging tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Kinnane committed Jan 15, 2017
1 parent 7d90f69 commit 5145a7d
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 42 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripts": {
"test": "gulp test",
"watch": "gulp watch",
"hubot": "coffee --nodejs '--debug' ./test/scripts/shell.coffee",
"kill": "kill -9 `ps ax | grep hubot | awk '{print $1}'`",
"coveralls": "gulp test && cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
Expand Down
6 changes: 0 additions & 6 deletions src/modules/Dialogue.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ class Dialogue extends EventEmitter

# kick-off dialogue exchange
@send opts.prompt if opts.prompt?

return opts.key # allow path to be queried by key

# add a dialogue branch (usually through path) with response and/or callback
Expand Down Expand Up @@ -118,11 +117,6 @@ class Dialogue extends EventEmitter
return false if @ended # dialogue is over, don't process

line = res.message.text
if typeof line is 'undefined'
console.log ">>>> \n"
console.log res
console.log "<<<< \n"

@log.debug "Dialogue received #{ line }"
match = false

Expand Down
4 changes: 1 addition & 3 deletions src/modules/Scene.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ class Scene
# hubot middleware re-routes to internal matching while engaged
@robot.receiveMiddleware (c, n, d) => @middleware @, c, n, d

# this is not called directly, but passed as a property
# not called as method, but copied as a property
middleware: (scene, context, next, done) =>
console.log context.response.message.text
console.log scene.engaged.length
res = context.response
audience = @whoSpeaks res

Expand Down
24 changes: 24 additions & 0 deletions test/scripts/shell.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Description:
# Testing hubot in the shell with Playbook
#
# Dependencies:
# N/A
#
# Configuration:
# N/A
#
# Commands:
# hubot ping - responds with pong
#
# Author:
# timkinnane
#

{Robot} = require 'hubot'
robot = new Robot 'hubot/src/adapters', 'shell'
robot.name = 'hublet'

module.exports = ->

# talk when talked to
robot.respond /ping/, (res) -> res.reply 'pong'
17 changes: 17 additions & 0 deletions test/scripts/shh.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Description:
# Silence is golden.
#
# Dependencies:
# N/A
#
# Configuration:
# N/A
#
# Commands:
# N/A
#
# Author:
# timkinnane
#

module.exports = (robot) ->
7 changes: 4 additions & 3 deletions test/unit/03-Scene.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# credit to lmarkus/hubot-conversation for the original concept

Q = require 'q'
_ = require 'underscore'
mute = require 'mute'
Expand All @@ -10,7 +12,6 @@ chai.use require 'sinon-chai'
Helper = require 'hubot-test-helper'
helper = new Helper "../scripts/ping.coffee"
Observer = require '../utils/observer'

Dialogue = require "../../src/modules/Dialogue"
Scene = require "../../src/modules/Scene"

Expand Down Expand Up @@ -112,12 +113,12 @@ describe '#Scene', ->
context 'dialogue allowed to timeout after branch added', ->

beforeEach (done) ->
# unmute = mute()
unmute = mute()
@dialogue = @scene.enter @res,
timeout: 10,
timeoutLine: null
@dialogue.on 'end', ->
# unmute()
unmute()
done()
@dialogue.branch /.*/, ''

Expand Down
102 changes: 72 additions & 30 deletions test/unit/08-Usage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,48 @@ chai = require 'chai'
should = chai.should()
chai.use require 'sinon-chai'

{Robot, TextMessage, User} = require 'hubot'
{Robot, User, TextMessage} = require 'hubot'
Playbook = require '../../src/Playbook'

describe 'Playbook usage (messaging test cases)', ->

before ->

# helper to send messages to bot, returns promise
@send = (user, text) =>
deferred = Q.defer()
msg = new TextMessage user, text, generate 12 # hash message ID
msg = new TextMessage user, text, generate 6 # random id
@robot.receive msg, -> deferred.resolve() # resolve as callback
return deferred.promise

# same username in two rooms should be recognised as same user
@nimaInFoo = new User 'nima', room: 'foo'
@pemaInFoo = new User 'pema', room: 'foo'
@nemaInBar = new User 'nima', room: 'bar'
@pemaInFoo = new User 'pema', room: 'bar'
@nimaInFoo = new User 1,
name: 'nima'
room: '#foo'
@pemaInFoo = new User 2,
name: 'pema'
room: '#foo'
@nimaInBar = new User 1,
name: 'nima'
room: '#bar'
@pemaInBar = new User 2,
name: 'pema'
room: '#bar'

beforeEach ->
# prepare robot to store every message
@messages = []
@robot = new Robot 'hubot/src/adapters', 'shell'
@playbook = new Playbook @robot
@spy = _.mapObject Playbook.prototype, (val, key) ->
sinon.spy Playbook.prototype, key # spy on all the class methods
require('../scripts/ping.coffee') @robot # run hubot through test script
@messages = [] # store every listen and response...
@robot.on 'receive', (res) =>
@messages.push [res.message.room, res.message.user.name, res.message.text]
msg = res.message if res.message instanceof TextMessage
@messages.push [ msg.room, msg.user.name, msg.text ] if msg?
@robot.on 'respond', (res, strings) =>
console.log '<>><>><'
@messages.push [res.message.room, 'hubot', strings]
msg = res.message if res.message instanceof TextMessage and strings?
@messages.push [ msg.room, 'hubot', strings[0] ] if msg?

@playbook = new Playbook @robot
@spy = _.mapObject Playbook.prototype, (val, key) ->
sinon.spy Playbook.prototype, key # spy on all the class methods
@messages = [] # reset store of receits and responses

afterEach ->
_.invoke @spy, 'restore' # restore all the methods
Expand All @@ -55,29 +65,61 @@ describe 'Playbook usage (messaging test cases)', ->
@scene.exitAll()
unmute()

context 'user enters dialogue, they and another user respond to prompt', ->
context 'enter dialogue and another user responds to prompt', ->

beforeEach ->
# unmute = mute()
@robot.hear /knock/, (res1) =>
@dialogue = @scene.enter res1
@dialogue.branch /.*/, (res2) =>
@dialogue.send "#{ res2.match[0] } who?"
unmute = mute()
@robot.hear /knock/, (res) =>
@dialogue = @scene.enter res
@dialogue.send "Who's there?"
@dialogue.branch /.*/, (res) =>
@dialogue.send "#{ res.match[0] } who?"
@dialogue.branch /.*/, "hahaha, good one."

@send @nimaInFoo, 'knock knock' # who's there?
@send @pemaInFoo, 'Pema' # ignored
@send @nimaInFoo, 'Nima' # Nima who?
@send @nimaInFoo, 'Nima in Foo' # haha
# .then -> unmute()
.then => @send @pemaInFoo, 'Pema' # ignored
.then => @send @nimaInFoo, 'Nima' # Nima who?
.then => @send @nimaInFoo, 'Nima in Foo' # haha
.then -> unmute()

it 'responds to first user only', ->
console.log inspect @messages
@messages.should.eql [
[ '#foo', 'nima', 'knock knock' ],
[ '#foo', 'hubot', 'Who\'s there?' ],
[ '#foo', 'pema', 'Pema' ],
[ '#foo', 'nima', 'Nima' ],
[ '#foo', 'hubot', 'Nima who?' ],
[ '#foo', 'nima', 'Nima in Foo' ],
[ '#foo', 'hubot', 'hahaha, good one.' ]
]

context 'enter dialogue and continue in another room', ->

beforeEach ->
unmute = mute()
@robot.hear /knock/, (res) =>
@dialogue = @scene.enter res
@dialogue.send "Who's there?"
@dialogue.branch /.*/, (res) =>
@dialogue.send "Hi #{ res.match[0] }"

@send @nimaInFoo, 'knock knock' # who's there?
.then => @send @pemaInFoo, 'Pema in Foo' # ignored
.then => @send @nimaInBar, 'Nima in Bar' # Nima who? (in other room)
.then => @send @pemaInBar, 'Pema in Bar' # ignored
.then -> unmute()

it 'responds to the first user only', ->
@messages.should.eql [
[ '#foo', 'nima', 'knock knock' ],
[ '#foo', 'hubot', 'Who\'s there?' ],
[ '#foo', 'pema', 'Pema in Foo' ],
[ '#bar', 'nima', 'Nima in Bar' ],
[ '#foo', 'hubot', 'Hi Nima in Bar' ],
[ '#bar', 'pema', 'Pema in Bar' ]
]


# it 'responds to the first user outside the room', ->
#
# it 'does not respond to other users', ->
#
# context 'scene type "room"', ->
#
# beforeEach ->
Expand Down

0 comments on commit 5145a7d

Please sign in to comment.