diff --git a/Dockerfile b/Dockerfile index cb49be9..4763f28 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,3 @@ -FROM node +FROM node:4 RUN npm install -g yarn diff --git a/README.md b/README.md index 8a93d49..e94b562 100644 --- a/README.md +++ b/README.md @@ -12,46 +12,50 @@ Helper for testing Hubot script. If you have a following hubot script: -```coffee -module.exports = (robot) -> - robot.respond /hi$/i, (msg) -> - msg.reply 'hi' +```javascript +module.exports = robot => + robot.respond(/hi$/i, msg => msg.reply('hi')) ``` You can test it like: -```coffee -Helper = require('hubot-test-helper') -# helper loads all scripts passed a directory -helper = new Helper('./scripts') - -# helper loads a specific script if it's a file -scriptHelper = new Helper('./scripts/specific-script.coffee') - -co = require('co') -expect = require('chai').expect - -describe 'hello-world', -> - - beforeEach -> - @room = helper.createRoom() - - afterEach -> - @room.destroy() - - context 'user says hi to hubot', -> - beforeEach -> - co => - yield @room.user.say 'alice', '@hubot hi' - yield @room.user.say 'bob', '@hubot hi' - - it 'should reply to user', -> - expect(@room.messages).to.eql [ - ['alice', '@hubot hi'] - ['hubot', '@alice hi'] - ['bob', '@hubot hi'] +```javascript +const Helper = require('hubot-test-helper'); +// helper loads all scripts passed a directory +const helper = new Helper('./scripts'); + +// helper loads a specific script if it's a file +const scriptHelper = new Helper('./scripts/specific-script.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('hello-world', function() { + beforeEach(function() { + this.room = helper.createRoom(); + }); + afterEach(function() { + this.room.destroy(); + }); + + context('user says hi to hubot', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot hi'); + yield this.room.user.say('bob', '@hubot hi'); + }.bind(this)); + }); + + it('should reply to user', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot hi'], + ['hubot', '@alice hi'], + ['bob', '@hubot hi'], ['hubot', '@bob hi'] - ] + ]); + }); + }); +}); ``` #### HTTPD @@ -62,7 +66,7 @@ tests and so requires it to be shutdown during teardown using `room.destroy()`. This feature can be turned off in tests that don't need it by passing using `helper.createRoom(httpd: false)`. -See [the tests](test/httpd-world_test.coffee) for an example of testing the +See [the tests](test/httpd-world_test.js) for an example of testing the HTTP server. @@ -74,47 +78,53 @@ in testing we may anticipate the delayed reply with a manual time delay. For example we have the following script: -```coffee -module.exports = (robot) -> - robot.hear /(http(?:s?):\/\/(\S*))/i, (res) -> - url = res.match[1] - res.send "ok1: #{url}" - robot.http(url).get() (err, response, body) -> - res.send "ok2: #{url}" +```javascript +module.exports = robot => + robot.hear(/(http(?:s?):\/\/(\S*))/i, res => { + const url = res.match[1]; + res.send(`ok1: ${url}`); + robot.http(url).get()((err, response, body) => res.send(`ok2: ${url}`)); + }); ``` To test the second callback response "ok2: ..." we use the following script: -```coffee -Helper = require('hubot-test-helper') -helper = new Helper('../scripts/http.coffee') - -Promise= require('bluebird') -co = require('co') -expect = require('chai').expect - -# test ping -describe 'http', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - # Test case - context 'user posts link', -> - beforeEach -> - co => - yield @room.user.say 'user1', 'http://google.com' - # delay one second for the second - # callback message to be posted to @room - yield new Promise.delay(1000) - - # response - it 'expects deplayed callback from ok2', -> - console.log @room.messages - expect(@room.messages).to.eql [ - ['user1', 'http://google.com'] - ['hubot', 'ok1: http://google.com'] +```javascript +const Helper = require('hubot-test-helper'); +const helper = new Helper('../scripts/http.js'); + +const Promise = require('bluebird'); +const co = require('co'); +const expect = require('chai').expect; + +// test ping +describe('http', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + // Test case + context('user posts link', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('user1', 'http://google.com'); + // delay one second for the second + // callback message to be posted to @room + yield new Promise.delay(1000); + }.bind(this)); + }); + + // response + it('expects deplayed callback from ok2', function() { + console.log(this.room.messages); + expect(this.room.messages).to.eql([ + ['user1', 'http://google.com'], + ['hubot', 'ok1: http://google.com'], ['hubot', 'ok2: http://google.com'] - ] + ]); + }); + }); +}); ``` Note that `yield` and *generators* are part of [**ECMA6**](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*), so it may not work on older node.js versions. It will wait for the delay to complete the `beforeEach` before proceeding to the test `it`. @@ -128,37 +138,41 @@ may want to test the creation of a Given the following script: -```coffee -module.exports = (robot) -> - - robot.respond /check status$/i, (msg) -> - robot.emit 'slack.attachment', +```javascript +module.exports = robot => + robot.respond(/check status$/i, msg => + robot.emit('slack.attachment', { message: msg.message, content: { - color: "good" + color: "good", text: "It's all good!" } + }) + ) ``` you could test the emitted event like this: -```coffee -Helper = require 'hubot-test-helper' -helper = new Helper('../scripts/status_check.coffee') +```javascript +const Helper = require('hubot-test-helper'); +const helper = new Helper('../scripts/status_check.js'); -expect = require('chai').expect +const expect = require('chai').expect; -describe 'status check', -> - beforeEach -> - @room = helper.createRoom(httpd: false) +describe('status check', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); - it 'should send a slack event', -> - response = null - @room.robot.on 'slack.attachment', (event) -> - response = event.content + it('should send a slack event', function() { + let response = null; + this.room.robot.on('slack.attachment', event => response = event.content); - @room.user.say('bob', '@hubot check status').then => - expect(response.text).to.eql("It's all good!") + this.room.user.say('bob', '@hubot check status').then(() => { + expect(response.text).to.eql("It's all good!"); + }); + }); +}); ``` ## Development diff --git a/package.json b/package.json index dbe72d9..1f7902b 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,22 @@ { "name": "hubot-test-helper", "description": "Helper for testing hubot script", - "main": "./lib/index.js", + "main": "./src/index.js", "scripts": { "test": "mocha --compilers coffee:coffee-script/register test", - "semantic-release": "semantic-release pre && npm publish && semantic-release post", - "prepublish": "coffee --compile --output lib/ src/" + "semantic-release": "semantic-release pre && npm publish && semantic-release post" }, "keywords": [ "hubot" ], "dependencies": { - "hubot": ">=2.6.0 <10 || 0.0.0-development" + "hubot": ">=3.0.0 <10 || 0.0.0-development" }, "devDependencies": { "chai": "latest", "co": "latest", - "coffee-script": "latest", "mocha": "latest", + "coffee-script": "latest", "semantic-release": "latest" }, "author": "Fumiaki MATSUSHIMA", diff --git a/src/index.coffee b/src/index.coffee deleted file mode 100644 index 404071f..0000000 --- a/src/index.coffee +++ /dev/null @@ -1,105 +0,0 @@ -Fs = require('fs') -Path = require('path') -Hubot = require('hubot') - -process.setMaxListeners(0) - -class MockResponse extends Hubot.Response - sendPrivate: (strings...) -> - @robot.adapter.sendPrivate @envelope, strings... - -class MockRobot extends Hubot.Robot - constructor: (httpd=true) -> - super null, null, httpd, 'hubot' - - @Response = MockResponse - - loadAdapter: -> - @adapter = new Room(@) - -class Room extends Hubot.Adapter - constructor: (@robot) -> - @messages = [] - - @privateMessages = {} - - @user = - say: (userName, message, userParams) => - @receive(userName, message, userParams) - - enter: (userName, userParams) => - @enter(userName, userParams) - - leave: (userName, userParams) => - @leave(userName, userParams) - - receive: (userName, message, userParams = {}) -> - new Promise (resolve) => - textMessage = null - if typeof message is 'object' and message - textMessage = message - else - userParams.room = @name - user = new Hubot.User(userName, userParams) - textMessage = new Hubot.TextMessage(user, message) - - @messages.push [userName, textMessage.text] - @robot.receive(textMessage, resolve) - - destroy: -> - @robot.server.close() if @robot.server - - reply: (envelope, strings...) -> - @messages.push ['hubot', "@#{envelope.user.name} #{str}"] for str in strings - - send: (envelope, strings...) -> - @messages.push ['hubot', str] for str in strings - - sendPrivate: (envelope, strings...) -> - if envelope.user.name not of @privateMessages - @privateMessages[envelope.user.name] = [] - @privateMessages[envelope.user.name].push ['hubot', str] for str in strings - - robotEvent: () -> - @robot.emit.apply(@robot, arguments) - - enter: (userName, userParams = {}) -> - new Promise (resolve) => - userParams.room = @name - user = new Hubot.User(userName, userParams) - @robot.receive(new Hubot.EnterMessage(user), resolve) - - leave: (userName, userParams = {}) -> - new Promise (resolve) => - userParams.room = @name - user = new Hubot.User(userName, userParams) - @robot.receive(new Hubot.LeaveMessage(user), resolve) - -class Helper - @Response = MockResponse - - constructor: (scriptsPaths) -> - if not Array.isArray(scriptsPaths) - scriptsPaths = [scriptsPaths] - @scriptsPaths = scriptsPaths - - createRoom: (options={}) -> - robot = new MockRobot(options.httpd) - - if 'response' of options - robot.Response = options.response - - for script in @scriptsPaths - script = Path.resolve(Path.dirname(module.parent.filename), script) - if Fs.statSync(script).isDirectory() - for file in Fs.readdirSync(script).sort() - robot.loadFile script, file - else - robot.loadFile Path.dirname(script), Path.basename(script) - - robot.brain.emit 'loaded' - - robot.adapter.name = options.name or 'room1' - robot.adapter - -module.exports = Helper diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..dd29a51 --- /dev/null +++ b/src/index.js @@ -0,0 +1,154 @@ +'use strict' + +const Fs = require('fs'); +const Path = require('path'); +const Hubot = require('hubot/es2015'); + +process.setMaxListeners(0); + +class MockResponse extends Hubot.Response { + sendPrivate(/* ...strings*/) { + const strings = [].slice.call(arguments, 0); + + this.robot.adapter.sendPrivate.apply(this.robot.adapter, [this.envelope].concat(strings)); + } +} + +class MockRobot extends Hubot.Robot { + constructor(httpd) { + if (httpd == null) { httpd = true; } + super(null, null, httpd, 'hubot'); + + this.Response = MockResponse; + } + + loadAdapter() { + this.adapter = new Room(this); + } +} + +class Room extends Hubot.Adapter { + // XXX: https://github.com/hubotio/hubot/pull/1390 + static messages(obj) { + if (obj instanceof MockRobot) { + return obj.adapter.messages; + } else { + return obj.messages; + } + } + + constructor(robot) { + super(); + this.robot = robot; + this.messages = []; + + this.privateMessages = {}; + + this.user = { + say: (userName, message, userParams) => this.receive(userName, message, userParams), + enter: (userName, userParams) => this.enter(userName, userParams), + leave: (userName, userParams) => this.leave(userName, userParams) + }; + } + + receive(userName, message, userParams) { + if (userParams == null) { userParams = {}; } + return new Promise(resolve => { + let textMessage = null; + if ((typeof message === 'object') && message) { + textMessage = message; + } else { + userParams.room = this.name; + const user = new Hubot.User(userName, userParams); + textMessage = new Hubot.TextMessage(user, message); + } + + this.messages.push([userName, textMessage.text]); + this.robot.receive(textMessage, resolve); + }); + } + + destroy() { + if (this.robot.server) { this.robot.server.close(); } + } + + reply(envelope/*, ...strings*/) { + const strings = [].slice.call(arguments, 1); + + strings.forEach((str) => Room.messages(this).push(['hubot', `@${envelope.user.name} ${str}`])); + } + + send(envelope/*, ...strings*/) { + const strings = [].slice.call(arguments, 1); + + strings.forEach((str) => Room.messages(this).push(['hubot', str])); + } + + sendPrivate(envelope/*, ...strings*/) { + const strings = [].slice.call(arguments, 1); + + if (!(envelope.user.name in this.privateMessages)) { + this.privateMessages[envelope.user.name] = []; + } + strings.forEach((str) => this.privateMessages[envelope.user.name].push(['hubot', str])); + } + + robotEvent() { + this.robot.emit.apply(this.robot, arguments); + } + + enter(userName, userParams) { + if (userParams == null) { userParams = {}; } + return new Promise(resolve => { + userParams.room = this.name; + const user = new Hubot.User(userName, userParams); + this.robot.receive(new Hubot.EnterMessage(user), resolve); + }); + } + + leave(userName, userParams) { + if (userParams == null) { userParams = {}; } + return new Promise(resolve => { + userParams.room = this.name; + const user = new Hubot.User(userName, userParams); + this.robot.receive(new Hubot.LeaveMessage(user), resolve); + }); + } +} + +class Helper { + constructor(scriptsPaths) { + if (!Array.isArray(scriptsPaths)) { + scriptsPaths = [scriptsPaths]; + } + this.scriptsPaths = scriptsPaths; + } + + createRoom(options) { + if (options == null) { options = {}; } + const robot = new MockRobot(options.httpd); + + if ('response' in options) { + robot.Response = options.response; + } + + for (let script of this.scriptsPaths) { + script = Path.resolve(Path.dirname(module.parent.filename), script); + if (Fs.statSync(script).isDirectory()) { + for (let file of Fs.readdirSync(script).sort()) { + robot.loadFile(script, file); + } + } else { + robot.loadFile(Path.dirname(script), Path.basename(script)); + } + } + + robot.brain.emit('loaded'); + + robot.adapter.name = options.name || 'room1'; + return robot.adapter; + } +} +Helper.Response = MockResponse; + +module.exports = Helper; diff --git a/test/custom-text-message_test.coffee b/test/custom-text-message_test.coffee deleted file mode 100644 index 89f5ec0..0000000 --- a/test/custom-text-message_test.coffee +++ /dev/null @@ -1,21 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts/custom-text-message.coffee') -Hubot = require('hubot') - -co = require('co') -expect = require('chai').expect - -describe 'custom-text-message', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - context 'Passing a custom text message object', -> - beforeEach -> - co => - textMessage = new Hubot.TextMessage({}, '') - textMessage.isCustom = true - textMessage.custom = 'custom' - yield @room.user.say 'user', textMessage - - it 'sends back', -> - expect(@room.messages[1][1]).to.be.equal('custom') diff --git a/test/custom-text-message_test.js b/test/custom-text-message_test.js new file mode 100644 index 0000000..208dfd6 --- /dev/null +++ b/test/custom-text-message_test.js @@ -0,0 +1,27 @@ +const Helper = require('../src/index'); +const helper = new Helper('./scripts/custom-text-message.js'); +const Hubot = require('hubot'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('custom-text-message', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + context('Passing a custom text message object', function() { + beforeEach(function() { + return co(function*() { + const textMessage = new Hubot.TextMessage({}, ''); + textMessage.isCustom = true; + textMessage.custom = 'custom'; + yield this.room.user.say('user', textMessage); + }.bind(this)); + }); + + it('sends back', function() { + expect(this.room.messages[1][1]).to.be.equal('custom'); + }); + }); +}); diff --git a/test/enter-leave_test.coffee b/test/enter-leave_test.coffee deleted file mode 100644 index b559aad..0000000 --- a/test/enter-leave_test.coffee +++ /dev/null @@ -1,21 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts/enter-leave.coffee') - -co = require('co') -expect = require('chai').expect - -describe 'enter-leave', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - context 'user entering then leaving the room', -> - beforeEach -> - co => - yield @room.user.enter 'user1' - yield @room.user.leave 'user1' - - it 'greets the user', -> - expect(@room.messages).to.eql [ - ['hubot', 'Hi user1!'] - ['hubot', 'Bye user1!'] - ] diff --git a/test/enter-leave_test.js b/test/enter-leave_test.js new file mode 100644 index 0000000..b5ccd4d --- /dev/null +++ b/test/enter-leave_test.js @@ -0,0 +1,29 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/enter-leave.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('enter-leave', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + context('user entering then leaving the room', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.enter('user1'); + yield this.room.user.leave('user1'); + }.bind(this)); + }); + + it('greets the user', function() { + expect(this.room.messages).to.eql([ + ['hubot', 'Hi user1!'], + ['hubot', 'Bye user1!'] + ]); + }); + }); +}); diff --git a/test/events_test.coffee b/test/events_test.coffee deleted file mode 100644 index 54e1de7..0000000 --- a/test/events_test.coffee +++ /dev/null @@ -1,29 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts/events.coffee') - -co = require('co') -expect = require('chai').expect - -describe 'events', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - context 'should post on an event', -> - beforeEach -> - @room.robotEvent 'some-event', 'event', 'data' - - it 'should reply to user', -> - expect(@room.messages).to.eql [ - ['hubot', 'got event with event data'] - ] - - context 'should hear events emitted by responses', -> - - - it 'should trigger an event', -> - response = null - @room.robot.on 'response-event', (event) -> - response = event.content - - @room.user.say('bob', '@hubot send event').then => - expect(response).to.eql('hello') diff --git a/test/events_test.js b/test/events_test.js new file mode 100644 index 0000000..3ed588e --- /dev/null +++ b/test/events_test.js @@ -0,0 +1,36 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/events.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('events', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + context('should post on an event', function() { + beforeEach(function() { + this.room.robotEvent('some-event', 'event', 'data'); + }); + + it('should reply to user', function() { + expect(this.room.messages).to.eql([ + ['hubot', 'got event with event data'] + ]); + }); + }); + + context('should hear events emitted by responses', () => + it('should trigger an event', function() { + let response = null; + this.room.robot.on('response-event', event => response = event.content); + + this.room.user.say('bob', '@hubot send event').then(() => { + expect(response).to.eql('hello'); + }); + }) + ); +}); diff --git a/test/hello-world-listener_test.coffee b/test/hello-world-listener_test.coffee deleted file mode 100644 index c9314d1..0000000 --- a/test/hello-world-listener_test.coffee +++ /dev/null @@ -1,22 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts/hello-world-listener.coffee') - -co = require('co') -expect = require('chai').expect - -describe 'hello-world', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - context 'user says hi to hubot', -> - beforeEach -> - co => - yield @room.user.say 'alice', '@hubot hi' - yield @room.user.say 'bob', '@hubot hi' - - it 'should reply to user', -> - expect(@room.messages).to.eql [ - ['alice', '@hubot hi'] - ['bob', '@hubot hi'] - ['hubot', '@bob hi'] - ] diff --git a/test/hello-world-listener_test.js b/test/hello-world-listener_test.js new file mode 100644 index 0000000..6cf4ef7 --- /dev/null +++ b/test/hello-world-listener_test.js @@ -0,0 +1,30 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/hello-world-listener.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('hello-world', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + context('user says hi to hubot', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot hi'); + yield this.room.user.say('bob', '@hubot hi'); + }.bind(this)); + }); + + it('should reply to user', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot hi'], + ['bob', '@hubot hi'], + ['hubot', '@bob hi'] + ]); + }); + }); +}); diff --git a/test/hello-world_test.js b/test/hello-world_test.js new file mode 100644 index 0000000..4c57d89 --- /dev/null +++ b/test/hello-world_test.js @@ -0,0 +1,34 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/hello-world.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('hello-world', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + afterEach(function() { + this.room.destroy(); + }); + + context('user says hi to hubot', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot hi'); + yield this.room.user.say('bob', '@hubot hi'); + }.bind(this)); + }); + + it('should reply to user', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot hi'], + ['hubot', '@alice hi'], + ['bob', '@hubot hi'], + ['hubot', '@bob hi'] + ]); + }); + }); +}); diff --git a/test/httpd-world_test.coffee b/test/httpd-world_test.coffee deleted file mode 100644 index 892b17d..0000000 --- a/test/httpd-world_test.coffee +++ /dev/null @@ -1,22 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts') -http = require('http') - -expect = require('chai').expect - -process.env.EXPRESS_PORT = 8080 - -describe 'httpd-world', -> - beforeEach -> - @room = helper.createRoom() - - afterEach -> - @room.destroy() - - context 'GET /hello/world', -> - beforeEach (done) -> - http.get 'http://localhost:8080/hello/world', (@response) => done() - .on 'error', done - - it 'responds with status 200', -> - expect(@response.statusCode).to.equal 200 diff --git a/test/httpd-world_test.js b/test/httpd-world_test.js new file mode 100644 index 0000000..2dcc8e2 --- /dev/null +++ b/test/httpd-world_test.js @@ -0,0 +1,32 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts'); +const http = require('http'); + +const expect = require('chai').expect; + +process.env.EXPRESS_PORT = 8080; + +describe('httpd-world', function() { + beforeEach(function() { + this.room = helper.createRoom(); + }); + + afterEach(function() { + this.room.destroy(); + }); + + context('GET /hello/world', function() { + beforeEach(function(done) { + http.get('http://localhost:8080/hello/world', response => { + this.response = response; + done(); + }).on('error', done); + }); + + it('responds with status 200', function() { + expect(this.response.statusCode).to.equal(200); + }); + }); +}); diff --git a/test/load-multiple-scripts_test.coffee b/test/load-multiple-scripts_test.coffee deleted file mode 100644 index 19b80ca..0000000 --- a/test/load-multiple-scripts_test.coffee +++ /dev/null @@ -1,23 +0,0 @@ -Helper = require('../src/index') -helper = new Helper(['./scripts/hello-world.coffee', './scripts/bye.coffee']) - -co = require('co') -expect = require('chai').expect - -describe 'hello-world', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - context 'user says hi to hubot', -> - beforeEach -> - co => - yield @room.user.say 'alice', '@hubot hi' - yield @room.user.say 'bob', '@hubot bye' - - it 'should reply to user', -> - expect(@room.messages).to.eql [ - ['alice', '@hubot hi'] - ['hubot', '@alice hi'] - ['bob', '@hubot bye'] - ['hubot', '@bob bye'] - ] diff --git a/test/load-multiple-scripts_test.js b/test/load-multiple-scripts_test.js new file mode 100644 index 0000000..427e85e --- /dev/null +++ b/test/load-multiple-scripts_test.js @@ -0,0 +1,31 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper(['./scripts/hello-world.js', './scripts/bye.js']); + +const co = require('co'); +const expect = require('chai').expect; + +describe('hello-world', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + context('user says hi to hubot', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot hi'); + yield this.room.user.say('bob', '@hubot bye'); + }.bind(this)); + }); + + it('should reply to user', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot hi'], + ['hubot', '@alice hi'], + ['bob', '@hubot bye'], + ['hubot', '@bob bye'] + ]); + }); + }); +}); diff --git a/test/mock-response_test.coffee b/test/mock-response_test.coffee deleted file mode 100644 index 50ef691..0000000 --- a/test/mock-response_test.coffee +++ /dev/null @@ -1,27 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts/mock-response.coffee') - -co = require('co') -expect = require('chai').expect - -class NewMockResponse extends Helper.Response - random: (items) -> - 3 - -describe 'mock-response', -> - beforeEach -> - @room = helper.createRoom(response: NewMockResponse, httpd: false) - - context 'user says "give me a random" number to hubot', -> - beforeEach -> - co => - yield @room.user.say 'alice', '@hubot give me a random number' - yield @room.user.say 'bob', '@hubot give me a random number' - - it 'should reply to user with a random number', -> - expect(@room.messages).to.eql [ - ['alice', '@hubot give me a random number'] - ['hubot', '@alice 3'] - ['bob', '@hubot give me a random number'] - ['hubot', '@bob 3'] - ] diff --git a/test/mock-response_test.js b/test/mock-response_test.js new file mode 100644 index 0000000..d116ab7 --- /dev/null +++ b/test/mock-response_test.js @@ -0,0 +1,37 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/mock-response.js'); + +const co = require('co'); +const expect = require('chai').expect; + +class NewMockResponse extends Helper.Response { + random(items) { + return 3; + } +} + +describe('mock-response', function() { + beforeEach(function() { + this.room = helper.createRoom({response: NewMockResponse, httpd: false}); + }); + + context('user says "give me a random" number to hubot', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot give me a random number'); + yield this.room.user.say('bob', '@hubot give me a random number'); + }.bind(this)); + }); + + it('should reply to user with a random number', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot give me a random number'], + ['hubot', '@alice 3'], + ['bob', '@hubot give me a random number'], + ['hubot', '@bob 3'] + ]); + }); + }); +}); diff --git a/test/private-message_test.coffee b/test/private-message_test.coffee deleted file mode 100644 index 09d8b48..0000000 --- a/test/private-message_test.coffee +++ /dev/null @@ -1,26 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts/private-message.coffee') - -co = require('co') -expect = require('chai').expect - -describe 'private-message', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - context 'user asks hubot for a secret', -> - beforeEach -> - co => - yield @room.user.say 'alice', '@hubot tell me a secret' - - it 'should not post to the public channel', -> - expect(@room.messages).to.eql [ - ['alice', '@hubot tell me a secret'] - ] - - it 'should private message user', -> - expect(@room.privateMessages).to.eql { - 'alice': [ - ['hubot', 'whisper whisper whisper'] - ] - } diff --git a/test/private-message_test.js b/test/private-message_test.js new file mode 100644 index 0000000..cd4ba61 --- /dev/null +++ b/test/private-message_test.js @@ -0,0 +1,35 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/private-message.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('private-message', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + context('user asks hubot for a secret', function() { + beforeEach(function() { + return co(function*() { + yield this.room.user.say('alice', '@hubot tell me a secret'); + }.bind(this)); + }); + + it('should not post to the public channel', function() { + expect(this.room.messages).to.eql([ + ['alice', '@hubot tell me a secret'] + ]); + }); + + it('should private message user', function() { + expect(this.room.privateMessages).to.eql({ + 'alice': [ + ['hubot', 'whisper whisper whisper'] + ] + }); + }); + }); +}); diff --git a/test/scripts/bye.coffee b/test/scripts/bye.coffee deleted file mode 100644 index a9f85cd..0000000 --- a/test/scripts/bye.coffee +++ /dev/null @@ -1,5 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.respond /bye$/i, (msg) -> - msg.reply 'bye' diff --git a/test/scripts/bye.js b/test/scripts/bye.js new file mode 100644 index 0000000..e160cba --- /dev/null +++ b/test/scripts/bye.js @@ -0,0 +1,5 @@ +// Description: +// Test script +module.exports = robot => + robot.respond(/bye$/i, msg => msg.reply('bye')) +; diff --git a/test/scripts/custom-text-message.coffee b/test/scripts/custom-text-message.coffee deleted file mode 100644 index 0908e4d..0000000 --- a/test/scripts/custom-text-message.coffee +++ /dev/null @@ -1,7 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.listen( - (message) -> message.isCustom - (response) -> response.send response.message.custom - ) diff --git a/test/scripts/custom-text-message.js b/test/scripts/custom-text-message.js new file mode 100644 index 0000000..6c482ef --- /dev/null +++ b/test/scripts/custom-text-message.js @@ -0,0 +1,7 @@ +// Description: +// Test script +module.exports = robot => + robot.listen( + message => message.isCustom, + response => response.send(response.message.custom)) +; diff --git a/test/scripts/enter-leave.coffee b/test/scripts/enter-leave.coffee deleted file mode 100644 index 840c9ce..0000000 --- a/test/scripts/enter-leave.coffee +++ /dev/null @@ -1,8 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.enter (res) -> - res.send "Hi #{res.message.user.name}!" - - robot.leave (res) -> - res.send "Bye #{res.message.user.name}!" diff --git a/test/scripts/enter-leave.js b/test/scripts/enter-leave.js new file mode 100644 index 0000000..3f0f1c7 --- /dev/null +++ b/test/scripts/enter-leave.js @@ -0,0 +1,7 @@ +// Description: +// Test script +module.exports = function(robot) { + robot.enter(res => res.send(`Hi ${res.message.user.name}!`)); + + return robot.leave(res => res.send(`Bye ${res.message.user.name}!`)); +}; diff --git a/test/scripts/events.coffee b/test/scripts/events.coffee deleted file mode 100644 index 9b971c5..0000000 --- a/test/scripts/events.coffee +++ /dev/null @@ -1,9 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.on 'some-event', (some, data) -> - robot.messageRoom 'room1', "got event with #{some} #{data}" - - robot.respond /send event$/i, (msg) -> - robot.emit 'response-event', - content: 'hello' diff --git a/test/scripts/events.js b/test/scripts/events.js new file mode 100644 index 0000000..e77ba88 --- /dev/null +++ b/test/scripts/events.js @@ -0,0 +1,7 @@ +// Description: +// Test script +module.exports = function(robot) { + robot.on('some-event', (some, data) => robot.messageRoom('room1', `got event with ${some} ${data}`)); + + robot.respond(/send event$/i, msg => robot.emit('response-event', {content: 'hello'})); +}; diff --git a/test/scripts/hello-world-listener.coffee b/test/scripts/hello-world-listener.coffee deleted file mode 100644 index 0d73573..0000000 --- a/test/scripts/hello-world-listener.coffee +++ /dev/null @@ -1,9 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.listen( - (message) -> - message.user.name is 'bob' - (response) -> - response.reply 'hi' - ) diff --git a/test/scripts/hello-world-listener.js b/test/scripts/hello-world-listener.js new file mode 100644 index 0000000..d513071 --- /dev/null +++ b/test/scripts/hello-world-listener.js @@ -0,0 +1,4 @@ +// Description: +// Test script +module.exports = robot => + robot.listen(message => message.user.name === 'bob', response => response.reply('hi')) diff --git a/test/scripts/hello-world.js b/test/scripts/hello-world.js new file mode 100644 index 0000000..67dce1d --- /dev/null +++ b/test/scripts/hello-world.js @@ -0,0 +1,5 @@ +// Description: +// Test script +module.exports = robot => + robot.respond(/hi$/i, msg => msg.reply('hi')) +; diff --git a/test/scripts/httpd-world.coffee b/test/scripts/httpd-world.coffee deleted file mode 100644 index 9e772a6..0000000 --- a/test/scripts/httpd-world.coffee +++ /dev/null @@ -1,5 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.router.get "/hello/world", (req, res) -> - res.status(200).send("Hello World!") diff --git a/test/scripts/httpd-world.js b/test/scripts/httpd-world.js new file mode 100644 index 0000000..2325990 --- /dev/null +++ b/test/scripts/httpd-world.js @@ -0,0 +1,5 @@ +// Description: +// Test script +module.exports = robot => + robot.router.get("/hello/world", (req, res) => res.status(200).send("Hello World!")) +; diff --git a/test/scripts/mock-response.coffee b/test/scripts/mock-response.coffee deleted file mode 100644 index 0215b67..0000000 --- a/test/scripts/mock-response.coffee +++ /dev/null @@ -1,6 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.respond /give me a random number$/i, (msg) -> - randomNumber = msg.random [1, 2, 3, 4, 5] - msg.reply randomNumber diff --git a/test/scripts/mock-response.js b/test/scripts/mock-response.js new file mode 100644 index 0000000..cbb7679 --- /dev/null +++ b/test/scripts/mock-response.js @@ -0,0 +1,8 @@ +// Description: +// Test script +module.exports = robot => + robot.respond(/give me a random number$/i, function(msg) { + const randomNumber = msg.random([1, 2, 3, 4, 5]); + msg.reply(randomNumber); + }) +; diff --git a/test/scripts/private-message.coffee b/test/scripts/private-message.coffee deleted file mode 100644 index 5f0c64e..0000000 --- a/test/scripts/private-message.coffee +++ /dev/null @@ -1,5 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.respond /tell me a secret$/i, (msg) -> - msg.sendPrivate 'whisper whisper whisper' diff --git a/test/scripts/private-message.js b/test/scripts/private-message.js new file mode 100644 index 0000000..b73b1ff --- /dev/null +++ b/test/scripts/private-message.js @@ -0,0 +1,5 @@ +// Description: +// Test script +module.exports = robot => + robot.respond(/tell me a secret$/i, msg => msg.sendPrivate('whisper whisper whisper')) +; diff --git a/test/scripts/user-params.coffee b/test/scripts/user-params.coffee deleted file mode 100644 index 0eb6a98..0000000 --- a/test/scripts/user-params.coffee +++ /dev/null @@ -1,8 +0,0 @@ -# Description: -# Test script -module.exports = (robot) -> - robot.listen( - () -> true - (response) -> - response.send JSON.stringify(response.message.user) - ) diff --git a/test/scripts/user-params.js b/test/scripts/user-params.js new file mode 100644 index 0000000..db8eaee --- /dev/null +++ b/test/scripts/user-params.js @@ -0,0 +1,7 @@ +// Description: +// Test script +module.exports = robot => + robot.listen( + () => true, + response => response.send(JSON.stringify(response.message.user))) +; diff --git a/test/user-params_test.coffee b/test/user-params_test.coffee deleted file mode 100644 index d648adb..0000000 --- a/test/user-params_test.coffee +++ /dev/null @@ -1,22 +0,0 @@ -Helper = require('../src/index') -helper = new Helper('./scripts/user-params.coffee') - -co = require('co') -expect = require('chai').expect - -describe 'enter-leave', -> - beforeEach -> - @room = helper.createRoom(httpd: false) - - context 'user entering, leaving the room and sending a message', -> - params = { id: 1, name: 2, profile: 3 } - beforeEach -> - co => - yield @room.user.enter 'user1', params - yield @room.user.say 'user1', 'Hi', params - yield @room.user.leave 'user1', params - - it 'sends back', -> - for msg in @room.messages - if msg[0] is 'hubot' - expect(JSON.parse msg[1]).to.include(params) diff --git a/test/user-params_test.js b/test/user-params_test.js new file mode 100644 index 0000000..9aa221a --- /dev/null +++ b/test/user-params_test.js @@ -0,0 +1,32 @@ +'use strict' + +const Helper = require('../src/index'); +const helper = new Helper('./scripts/user-params.js'); + +const co = require('co'); +const expect = require('chai').expect; + +describe('enter-leave', function() { + beforeEach(function() { + this.room = helper.createRoom({httpd: false}); + }); + + context('user entering, leaving the room and sending a message', function() { + const params = { id: 1, name: 2, profile: 3 }; + beforeEach(function() { + return co(function*() { + yield this.room.user.enter('user1', params); + yield this.room.user.say('user1', 'Hi', params); + yield this.room.user.leave('user1', params); + }.bind(this)); + }); + + it('sends back', function() { + for (let msg of this.room.messages) { + if (msg[0] === 'hubot') { + expect(JSON.parse(msg[1])).to.include(params) + } + } + }); + }); +});