forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.coffee
75 lines (61 loc) · 1.97 KB
/
tests.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
Url = require 'url'
Path = require 'path'
Robot = require '../src/robot'
Adapter = require '../src/adapter'
User = require '../src/user'
Response = require '../src/response'
{TextMessage} = require '../src/message'
# A programmer's best friend.
# http://timenerdworld.files.wordpress.com/2010/12/joint-venture-s1e3_1.jpg
#
# Instantiates a test-only Robot that sends messages to an optional callback
# and a @sent array.
exports.helper = ->
new Helper "#{__dirname}/scripts"
# Training facility built for the Hubot scripts. Starts up a web server to
# emulate backends (like google images) so we can test that the response
# parsing code functions.
exports.danger = (helper, cb) ->
server = require('http').createServer (req, res) ->
url = Url.parse req.url, true
cb req, res, url
server.start = (tests, cb) ->
server.listen 9001, ->
helper.adapter.cb = (messages...) ->
tests.shift() messages...
server.close() if tests.length == 0
cb()
server.on 'close', -> helper.close()
server
class Helper extends Robot
constructor: (scriptPath) ->
super null, null, true, 'helper'
@load scriptPath
@id = 1
@Response = Helper.Response
@sent = []
@recipients = []
@adapter = new Danger @
@alias = 'alias'
stop: ->
process.exit 0
reset: ->
@sent = []
class Danger extends Adapter
send: (user, strings...) ->
@robot.sent.push str for str in strings
@robot.recipients.push user for str in strings
@cb? strings...
reply: (user, strings...) ->
@send user, "#{@robot.name}: #{str}" for str in strings
receive: (text) ->
if typeof text is 'string'
user = new User 1, 'helper'
super new TextMessage user, text
else
super text
if not process.env.HUBOT_LIVE
class Helper.Response extends Response
# This changes ever HTTP request to hit the danger server above
http: (url) ->
super(url).host('127.0.0.1').port(9001)