Instantly share code, notes, and snippets.

Embed
What would you like to do?
hubot script example with test
# Description:
# Get help from @yourorg/it for your information technology problems. Oh by the way.... Your Welcome.
#
# Commands:
# hubot it me <problem> - get help from @yourorg/it about your information technology <problem>
module.exports = (robot) ->
robot.respond /it(?: me)?(?: (.*))?/i, (msg) ->
problem = msg.match[1]
output = if problem?
"That sounds totally reasonable. I'm sure @yourorg/it can help with that. Head over to https://github.com/yourorg/it/issues/new?title=#{encodeURIComponent problem} to create an issue and try to be as detailed as possible, including what you need and by when. Thanks!"
else
"Have a problem? @yourorg/it has solutions! Head over to https://github.com/yourorg/it/issues/new to create an issue and try to be as detailed as possible, including what you need and by when. Thanks!"
msg.send output
assert = require('chai').assert
helper = require '../test-helper'
{TextMessage} = require 'hubot'
describe 'IT commands', ->
beforeEach (done) ->
@robot = helper.robot()
@user = helper.testUser @robot
@id = @user.id.toString()
@robot.adapter.on 'connected', ->
@robot.loadFile helper.SCRIPTS_PATH, 'it.coffee'
@robot.parseHelp "#{helper.SCRIPTS_PATH}/it.coffee"
done()
@robot.run()
afterEach ->
@robot.shutdown()
it "is included in /help", ->
assert.include @robot.commands[0], "it me"
describe "with a problem", ->
it "links you to start a new issue on yourorg/it", (done) ->
helper.converse @robot, @user, '/it the office internet is slow', (envelope, response) ->
assert.include response, "That sounds totally reasonable. I'm sure @yourorg/it can help with that. Head over to https://github.com/yourorg/it/issues/new?title=the%20office%20internet%20is%20slow to create an issue and try to be as detailed as possible, including what you need and by when. Thanks!"
done()
it "doesn't include the word 'me' in the new issues title", (done) ->
helper.converse @robot, @user, '/it me the office internet is slow', (envelope, response) ->
assert.include response, "That sounds totally reasonable. I'm sure @yourorg/it can help with that. Head over to https://github.com/yourorg/it/issues/new?title=the%20office%20internet%20is%20slow to create an issue and try to be as detailed as possible, including what you need and by when. Thanks!"
done()
describe "without a problem", ->
it "links the person to start an issue on yourorg/it", (done) ->
helper.converse @robot, @user, '/it', (envelope, response) ->
assert.include response, "Have a problem? @yourorg/it has solutions! Head over to https://github.com/yourorg/it/issues/new to create an issue and try to be as detailed as possible, including what you need and by when. Thanks!"
done()
Path = require 'path'
hubot = require 'hubot'
# Creates a Robot instance for testing. robot.adapter will be a
# hubot-mock-adapter instance, which can be used for sending messages to the
# robot and listening for replies.
exports.robot = ->
# We specify the shell adapter here but point the robot at the test
# directory. This will make it find our shell.coffee symlink, which points
# to our test-adapter.coffee module.
robot = hubot.loadBot null, 'mock-adapter', false, 'TestHubot'
robot.alias = '/'
robot.rooms = {}
# Rethrow any errors that are generated while handling messages. Normally
# Hubot catches these and logs them.
error = null
robot.error (e) ->
error = e
originalReceive = robot.adapter.receive
robot.adapter.receive = ->
error = null
result = originalReceive.apply this, arguments
throw error if error?
result
robot
# Creates a new User instance for testing.
exports.testUser = (robot) ->
ids = Object.keys robot.brain.users()
highestID = if ids.length
Math.max ids...
else
0
id = highestID + 1
user = robot.brain.userForId id, name: "TestUser#{id}", room: 'TestRoom'
user.githubLogin = "TestUser#{id}GitHubLogin"
user
# The path to the top-level scripts/ directory. Useful in conjunction with
# robot.loadFile().
exports.SCRIPTS_PATH = Path.join __dirname, '..', 'scripts'
# Sends one or more TextMessages to the robot. Waits for a response to each
# message before sending the next one. The callback is called when the response
# to the final message is received.
#
# robot - a Robot instance (usually from helper.robot())
# user - a User instance (usually from helper.testUser())
# messages - one or more String arguments
# callback - the optional callback to call once the last response has been sent
#
# helper.converse @robot, @user, '/image me giraffe', '/ping', (envelope, response) ->
# assert.equal response, 'PONG'
#
# helper.converse @robot, @user, '/corgi bomb', (envelope, responses...) ->
# assert.equal responses.length, 5
# for response in responses
# assert.include response, 'corgi'
exports.converse = (robot, user, messages..., callback) ->
EVENTS = ['send', 'reply']
unless messages.length
messages = [callback]
callback = null
receivedResponse = (envelope, strings) ->
for event in EVENTS
robot.adapter.removeListener event, receivedResponse
if messages.length
sendNextMessage()
else
callback? envelope, strings...
sendNextMessage = ->
for event in EVENTS
robot.adapter.once event, receivedResponse
message = messages.shift()
robot.adapter.receive new hubot.TextMessage user, message
sendNextMessage()
@willdurand

This comment has been minimized.

Show comment
Hide comment
@willdurand

willdurand Oct 23, 2015

@technicalpickles it looks like Hubot 2.14+ cannot be used with this helper, right?

willdurand commented Oct 23, 2015

@technicalpickles it looks like Hubot 2.14+ cannot be used with this helper, right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment