Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjpaz committed Nov 17, 2018
1 parent de2ca54 commit 56778d5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 64 deletions.
66 changes: 41 additions & 25 deletions packages/prathu-reaction/src/random-reaction.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
const random = (chance) => {
chance = chance || 0.5;
return (Math.random() < chance ? 0 : 1);
};

const CHANCE_TO_REACT = 0.15;
const CHANCE_TO_REACT_NONSENSE = 0.05;

const REACTIONS = [
'thumbsup',
'thumbsdown',
Expand All @@ -15,15 +7,49 @@ const REACTIONS = [

const getRandomReaction = () => REACTIONS[Math.floor(Math.random()*REACTIONS.length)];

module.exports = function(robot) {
let web;
class RandomReactionHandler {
constructor({ store, slackWebClient }) {
this.store = store;
this.slackWebClient = slackWebClient;
}

random(chance) {
chance = chance || 0.5;
return (Math.random() < chance ? 0 : 1);
}

reaction(msg, chanceRandom, chanceNormal) {
if(this.random(chanceRandom)) {
// React with a random emoji

return;
}

if(this.random(chanceNormal)) {
// React with a normal emoji
return;
}
}

addReaction(emoji, channel, timestamp) {
this.store.set("prathu-reaction.addReaction.emoji", emoji);

if(robot.adapter.client) {
({ web } = robot.adapter.client);
} else {
robot.logger.info("Web client not found");
return {};
this.slackWebClient.reactions.add(emoji, {
channel,
timestamp
});
}
}

module.exports = {
RandomReactionHandler
};


const f = () => {
return new RandomReactionHandler(options);



const addReaction = (emoji, channel, timestamp) => {
web.reactions.add(emoji, {
Expand All @@ -49,16 +75,6 @@ module.exports = function(robot) {
}
};

robot.respond(/.*(opinion|should (i|we)|do you like|you feel).*/i, (msg) => {
reaction(robot, msg, 0.3, 1.0);
});

robot.hear(/.*/, function(msg) {
robot.logger.info("Testing react");
robot.logger.info(`Message${JSON.stringify(msg.message)}`);
return reaction(robot, msg, CHANCE_TO_REACT_NONSENSE, CHANCE_TO_REACT);
});

// TODO
return {
setCurrentMood,
Expand Down
87 changes: 48 additions & 39 deletions packages/prathu-reaction/src/random-reaction.test.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,62 @@
const { assert } = require('chai');
const { expect } = require('chai');

const sinon = require('sinon');

const randomReaction = require('./random-reaction');

xdescribe('random-reaction', () => {
it('should add hear/respond', () => {
const robot = {
respond: sinon.spy(),
hear: sinon.spy(),
adapter: {
client: {
web: {}
}
}
const { RandomReactionHandler } = require('./random-reaction');

describe('random-reaction', () => {
it('should update store and add reaction', () => {
const slackWebClient = { };
slackWebClient.reactions = { };
slackWebClient.reactions.add = sinon.stub();

const store = {
set: sinon.stub()
};

randomReaction(robot);
const r = new RandomReactionHandler({
store,
slackWebClient
});

assert.isOk(robot.respond.called);
assert.isOk(robot.hear.called);
});
r.addReaction("foo", "bar", "123");

xit('should react', () => {
const robot = {
respond: sinon.spy(),
hear: sinon.spy(),
adapter: {
client: {
web: {
emoji: {
list: sinon.stub().yields(null, {
emoji: {
foo: "bar"
}
})
}
}
}
}
};
expect(store.set.called).to.be.true;
expect(store.set.calledWith("prathu-reaction.addReaction.emoji", "foo")).to.be.true;

const ex = randomReaction(robot);
expect(slackWebClient.reactions.add.called).to.be.true;
expect(slackWebClient.reactions.add.calledWith("foo", {
channel: "bar",
timestamp: "123"
})).to.be.true;
});

it('should do random', () => {
const r = new RandomReactionHandler({
});

sinon.spy(ex.addReaction);
const boolean = r.random(1);

ex.reaction(robot, {}, 0, 1);
expect(typeof boolean).to.eql('number');
expect(boolean === 0 || boolean === 1).to.eql(true);

assert.isOk(robot.adapter.client.web.emoji.list.called);
});

it('reaction should call random', () => {
const r = new RandomReactionHandler({});

sinon.spy(r, "random");

r.reaction(null, 0, 0);

expect(r.random.calledOnce).to.be.true;
expect(r.random.calledWith(0)).to.be.true;

r.random.resetHistory();

r.reaction(null, 0, 0);

expect(r.random.calledTwice).to.be.true;
expect(r.random.calledWith(0)).to.be.true;
});
});

0 comments on commit 56778d5

Please sign in to comment.