Skip to content

Commit

Permalink
Adding helper for asking for hubot day
Browse files Browse the repository at this point in the history
  • Loading branch information
O-Mutt committed Oct 15, 2020
1 parent 62a386e commit 7c8d854
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 6 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubot-plusplus-expanded",
"version": "1.1.8",
"version": "1.2.0",
"description": "A hubot script for micro praise",
"main": "index.js",
"engines": {
Expand Down Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"clark": "^0.0.6",
"lodash": "^4.17.20",
"moment": "^2.29.1",
"mongodb": "^3.6.2",
"request": "^2.88.2",
"snyk": "^1.413.2"
Expand Down
8 changes: 8 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ function createEraseUserScoreRegExp() {
return new RegExp(`${eraseClause}${allowSpaceAfterObject}${votedObject}${allowSpaceAfterObject}${reasonForVote}${eol}`, 'i');
}

/**
*
*/
function createBotDayRegExp(botName) {
return new RegExp(`(what day|when|which day|which) is (my|\\w+\\.\\w+)( )?('s)? ${botName}( )?day(\\?)?`, 'i');
}

/**
* { user1, user2 }++
* { user1, user2 }--
Expand Down Expand Up @@ -190,6 +197,7 @@ const helpers = {
negativeOperators,
isCakeDay,
getYearsAsString,
createBotDayRegExp,
};

module.exports = helpers;
15 changes: 15 additions & 0 deletions src/plusplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
const clark = require('clark');
const request = require('request');
const _ = require('lodash');
const moment = require('moment');
const ScoreKeeper = require('./scorekeeper');
const helper = require('./helpers');

Expand All @@ -48,6 +49,7 @@ module.exports = function plusPlus(robot) {
const multiUserVoteRegExp = helper.createMultiUserVoteRegExp();
const topOrBottomRegExp = helper.createTopBottomRegExp();
const eraseScoreRegExp = helper.createEraseUserScoreRegExp();
const botDayRegexp = helper.createBotDayRegExp(robot.name);

/* eslint-disable */
// listen to everything
Expand All @@ -58,6 +60,7 @@ module.exports = function plusPlus(robot) {
// listen for bot tag/ping
robot.respond(askForScoreRegexp, respondWithScore);
robot.respond(topOrBottomRegExp, respondWithLeaderLoserBoard);
robot.respond(botDayRegexp, respondWithUsersBotDay);

// admin
robot.respond(eraseScoreRegExp, eraseUserScore);
Expand Down Expand Up @@ -224,6 +227,18 @@ module.exports = function plusPlus(robot) {
return msg.send(message.join('\n'));
}

async function respondWithUsersBotDay(msg) {
let userToLookup = msg.message.user.name;
let messageName = 'Your';
if (msg.match[2].toLowerCase() !== 'my') {
userToLookup = helper.cleanName(msg.match[2]);
messageName = `${userToLookup}'s`;
}
const user = await scoreKeeper.getUser(userToLookup);
const dateObj = new Date(user[`${robot.name}Day`]);
msg.send(`${messageName} ${robot.name}day is ${moment(dateObj).format('MM-DD-yyyy')}`);
}

async function eraseUserScore(msg) {
let erased;
// eslint-disable-next-line
Expand Down
2 changes: 1 addition & 1 deletion test/mock_full_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@
"cnViZW4ucmVzdHJlcG8=" : 1,
"bWF0dC5jb3JsZXk=" : 2
},
"qraftyDay" : "2020-08-09T16:55:04.374Z"
"hubotDay" : "2020-07-09T16:55:04.374Z"
}
2 changes: 1 addition & 1 deletion test/mock_minimal_user.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
"c2ViYQ==" : -4,
"cHNhYXMtYXBwbGljYXRpb25z" : 1
},
"qraftyDay" : "2020-07-09T16:55:04.374Z"
"hubotDay" : "2020-07-09T16:55:04.374Z"
}
34 changes: 32 additions & 2 deletions test/plusplus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,45 @@ describe.only('PlusPlus', function plusPlusTest() {
it('should respond with 5 reasons if the user has 5', async function respondWithScore() {
room.user.say('matt.erickson', '@hubot score for matt.erickson');
// eslint-disable-next-line new-cap
await (new Promise.delay(1000)); // wait for the db call in hubot
await (new Promise.delay(20)); // wait for the db call in hubot
expect(room.messages[1][1]).to.match(/matt\.erickson has 227 points\.\n\n:star: Here are some reasons :star:(\n.*:.*){5}/);
});

it('should respond with 3 reasons if the user has 3', async function respondWithScore() {
room.user.say('matt.erickson.min', '@hubot score for matt.erickson.min');
// eslint-disable-next-line new-cap
await (new Promise.delay(1000)); // wait for the db call in hubot
await (new Promise.delay(20)); // wait for the db call in hubot
expect(room.messages[1][1]).to.match(/matt\.erickson\.min has 8 points\.\n\n:star: Here are some reasons :star:(\n.*:.*){3}/);
});
});

describe('respondWithUsersBotDay', function respondWithUsersBotDay() {
it('should respond with the hubot day when asked', async function respondWithDay() {
room.user.say('matt.erickson', 'hubot when is my hubotday?');
// eslint-disable-next-line new-cap
await (new Promise.delay(30)); // wait for the db call in hubot
expect(room.messages[1][1]).to.equal('Your hubotday is 07-09-2020');
});

it('should respond with the hubot day when asked about a different persons hubot day', async function respondWithDay() {
room.user.say('phil.bob', 'hubot what day is matt.erickson hubot day?');
// eslint-disable-next-line new-cap
await (new Promise.delay(30)); // wait for the db call in hubot
expect(room.messages[1][1]).to.equal('matt.erickson\'s hubotday is 07-09-2020');
});

it('should respond with the hubot day when asked about a different persons (with \') hubot day', async function respondWithDay() {
room.user.say('phil.bob', 'hubot what day is matt.erickson\'s hubot day?');
// eslint-disable-next-line new-cap
await (new Promise.delay(30)); // wait for the db call in hubot
expect(room.messages[1][1]).to.equal('matt.erickson\'s hubotday is 07-09-2020');
});

it('should respond with the hubot day when asked about a different persons (with space \') hubot day', async function respondWithDay() {
room.user.say('phil.bob', 'hubot what day is matt.erickson \'s hubot day?');
// eslint-disable-next-line new-cap
await (new Promise.delay(30)); // wait for the db call in hubot
expect(room.messages[1][1]).to.equal('matt.erickson\'s hubotday is 07-09-2020');
});
});
});

0 comments on commit 7c8d854

Please sign in to comment.