Skip to content

Commit

Permalink
chore: decaffeinate
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsmfm committed Oct 8, 2017
1 parent aaa5072 commit fe2f0fe
Show file tree
Hide file tree
Showing 42 changed files with 641 additions and 410 deletions.
105 changes: 0 additions & 105 deletions src/index.coffee

This file was deleted.

162 changes: 162 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* decaffeinate suggestions:
* DS001: Remove Babel/TypeScript constructor workaround
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* DS206: Consider reworking classes to avoid initClass
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const Fs = require('fs');
const Path = require('path');
const Hubot = require('hubot');

process.setMaxListeners(0);

class MockResponse extends Hubot.Response {
sendPrivate(...strings) {
return this.robot.adapter.sendPrivate(this.envelope, ...Array.from(strings));
}
}

class MockRobot extends Hubot.Robot {
constructor(httpd) {
if (httpd == null) { httpd = true; }
super(null, null, httpd, 'hubot');

this.Response = MockResponse;
}

loadAdapter() {
return this.adapter = new Room(this);
}
}

class Room extends Hubot.Adapter {
constructor(robot) {
{
// Hack: trick Babel/TypeScript into allowing this before super.
if (false) { super(); }
let thisFn = (() => { this; }).toString();
let thisName = thisFn.slice(thisFn.indexOf('{') + 1, thisFn.indexOf(';')).trim();
eval(`${thisName} = this;`);
}
this.robot = robot;
this.messages = [];

this.privateMessages = {};

this.user = {
say: (userName, message, userParams) => {
return this.receive(userName, message, userParams);
},

enter: (userName, userParams) => {
return this.enter(userName, userParams);
},

leave: (userName, userParams) => {
return 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]);
return this.robot.receive(textMessage, resolve);
});
}

destroy() {
if (this.robot.server) { return this.robot.server.close(); }
}

reply(envelope, ...strings) {
return Array.from(strings).map((str) => this.messages.push(['hubot', `@${envelope.user.name} ${str}`]));
}

send(envelope, ...strings) {
return Array.from(strings).map((str) => this.messages.push(['hubot', str]));
}

sendPrivate(envelope, ...strings) {
if (!(envelope.user.name in this.privateMessages)) {
this.privateMessages[envelope.user.name] = [];
}
return Array.from(strings).map((str) => this.privateMessages[envelope.user.name].push(['hubot', str]));
}

robotEvent() {
return 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);
return 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);
return this.robot.receive(new Hubot.LeaveMessage(user), resolve);
});
}
}

class Helper {
static initClass() {
this.Response = MockResponse;
}

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 Array.from(this.scriptsPaths)) {
script = Path.resolve(Path.dirname(module.parent.filename), script);
if (Fs.statSync(script).isDirectory()) {
for (let file of Array.from(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.initClass();

module.exports = Helper;
21 changes: 0 additions & 21 deletions test/custom-text-message_test.coffee

This file was deleted.

32 changes: 32 additions & 0 deletions test/custom-text-message_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const Helper = require('../src/index');
const helper = new Helper('./scripts/custom-text-message.coffee');
const Hubot = require('hubot');

const co = require('co');
const { expect } = require('chai');

describe('custom-text-message', function() {
beforeEach(function() {
return this.room = helper.createRoom({httpd: false});
});

return context('Passing a custom text message object', function() {
beforeEach(function() {
return co(function*() {
const textMessage = new Hubot.TextMessage({}, '');
textMessage.isCustom = true;
textMessage.custom = 'custom';
return yield this.room.user.say('user', textMessage);
}.bind(this));
});

return it('sends back', function() {
return expect(this.room.messages[1][1]).to.be.equal('custom');
});
});
});
21 changes: 0 additions & 21 deletions test/enter-leave_test.coffee

This file was deleted.

32 changes: 32 additions & 0 deletions test/enter-leave_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const Helper = require('../src/index');
const helper = new Helper('./scripts/enter-leave.coffee');

const co = require('co');
const { expect } = require('chai');

describe('enter-leave', function() {
beforeEach(function() {
return this.room = helper.createRoom({httpd: false});
});

return context('user entering then leaving the room', function() {
beforeEach(function() {
return co(function*() {
yield this.room.user.enter('user1');
return yield this.room.user.leave('user1');
}.bind(this));
});

return it('greets the user', function() {
return expect(this.room.messages).to.eql([
['hubot', 'Hi user1!'],
['hubot', 'Bye user1!']
]);
});
});
});
Loading

0 comments on commit fe2f0fe

Please sign in to comment.