Skip to content
This repository has been archived by the owner on Aug 5, 2019. It is now read-only.

Commit

Permalink
Test emojiPrompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Throne3d committed Apr 28, 2018
1 parent 8b4f92d commit cee1a4c
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 16 deletions.
18 changes: 10 additions & 8 deletions lib/harmony.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,19 @@ class Harmony {
return formattedMessage;
}

emojiPrompt(message, options, target) {
async emojiPrompt(message, options, target) {
// TODO: work on messages when can't react
const filter = (reaction, user) => ((!target && user !== this.client.user) || user === target) && options.indexOf(reaction.emoji.name) >= 0;
let prompt = Promise.all([
const filter = (reaction, user) => {
if (options.indexOf(reaction.emoji.name) < 0) return false;
if (target) return user === target;
return user !== this.client.user;
};
let [reactions, response] = await Promise.all([
this.performReactions(message, options),
message.awaitReactions(filter, { max: 1, time: 15000 }),
])
.then(([reactions, response]) => {
return Promise.all(reactions.map(x => x.remove())).then(_ => response);
});
return prompt;
]);
await Promise.all(reactions.map(x => x.remove()));
return response;
}

// returns a promised [addressesMe, optional text]: respectively, a boolean for if the text seems to address the bot, and the string of the relevant text
Expand Down
8 changes: 8 additions & 0 deletions test/stubs/discord-stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ class MessageStub extends Discord.Message {
this.embeds.push(embed);
return embed;
}

newReaction(name, count, me) {
const emoji = new Discord.ReactionEmoji(null, name);
const reaction = new Discord.MessageReaction(this, emoji, count, me);
emoji.reaction = reaction;
this.reactions.set(reaction.id, reaction);
return reaction;
}
}

class DataManagerStub {
Expand Down
164 changes: 156 additions & 8 deletions test/test-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('Harmony', function() {
});
const messageReactStub = sinon.stub();
message.react = messageReactStub;
const reaction = new Discord.MessageReaction(message, '👍', 1, true);
const reaction = message.newReaction('👍', 1, true);
messageReactStub.resolves(reaction);
return bot.performReactions(message, ['👍']).then(reacts => {
expect(reacts).to.deep.equal([reaction]);
Expand All @@ -47,9 +47,9 @@ describe('Harmony', function() {
});
const messageReactStub = sinon.stub();
message.react = messageReactStub;
const reaction1 = new Discord.MessageReaction(message, '👍', 1, true);
const reaction2 = new Discord.MessageReaction(message, '👎', 1, true);
const reaction3 = new Discord.MessageReaction(message, '❗', 1, true);
const reaction1 = message.newReaction('👍', 1, true);
const reaction2 = message.newReaction('👎', 1, true);
const reaction3 = message.newReaction('❗', 1, true);

function quickPromise(count, reaction) {
return new Promise(function(resolve) {
Expand Down Expand Up @@ -82,10 +82,10 @@ describe('Harmony', function() {
});
const messageReactStub = sinon.stub();
message.react = messageReactStub;
const reaction1 = new Discord.MessageReaction(message, '👍', 1, true);
const reaction2 = new Discord.MessageReaction(message, '👎', 1, true);
const removeStub1 = sinon.stub(reaction1, 'remove');
const removeStub2 = sinon.stub(reaction2, 'remove');
const reaction1 = message.newReaction('👍', 1, true);
const reaction2 = message.newReaction('👎', 1, true);
const removeStub1 = reaction1.remove = sinon.stub();
const removeStub2 = reaction2.remove = sinon.stub();
removeStub1.resolves(reaction1);
removeStub2.resolves(reaction2);

Expand Down Expand Up @@ -178,5 +178,153 @@ describe('Harmony', function() {
});
});
});

describe('emojiPrompt', function() {
beforeEach(function() {
this.message = this.bot.client.channel.newMessage({
content: 'Message',
mentions: new Discord.Collection()
});
});

it('offers reactions to press on message', async function() {
const { bot, message } = this;
const reactionList = ['✅', '❎'];

bot.performReactions = sinon.stub().withArgs(message, reactionList).resolves([]);
message.awaitReactions = sinon.stub().resolves();

await bot.emojiPrompt(message, reactionList);

expect(bot.performReactions.callCount).to.equal(1);
});

it('returns selection', async function() {
const { bot, message } = this;
const reactionList = ['✅', '❎'];

bot.performReactions = sinon.stub().withArgs(message, reactionList).resolves([]);
message.awaitReactions = sinon.stub().resolves('test');

const resp = await bot.emojiPrompt(message, reactionList);

expect(bot.performReactions.callCount).to.equal(1);
expect(message.awaitReactions.callCount).to.equal(1);
expect(resp).to.equal('test');
});

it('removes own reactions on selection', async function() {
const { bot, message } = this;
const reactionList = ['✅', '❎'];

const reaction1 = message.newReaction('✅', 1, true);
reaction1.remove = sinon.stub();

const reaction2 = message.newReaction('❎', 1, true);
reaction2.remove = sinon.stub();

bot.performReactions = sinon.stub().withArgs(message, reactionList).resolves([reaction1, reaction2]);
message.awaitReactions = sinon.stub().resolves();

await bot.emojiPrompt(message, reactionList);

expect(bot.performReactions.callCount).to.equal(1);
expect(message.awaitReactions.callCount).to.equal(1);
expect(reaction1.remove.args).to.deep.equal([[]]);
expect(reaction2.remove.args).to.deep.equal([[]]);
});

it('awaits reactions correctly with no target', async function() {
const { bot, message } = this;
const reactionList = ['✅', '❎'];

const reaction1 = message.newReaction('✅', 1, true);
const reaction2 = message.newReaction('❎', 1, true);
reaction1.remove = reaction2.remove = sinon.stub();

const botUser = bot.client.user;
const otherUser = bot.client.newUser();
const otherUser2 = bot.client.newUser();

bot.performReactions = sinon.stub().withArgs(message, reactionList).resolves([reaction1, reaction2]);
message.awaitReactions = sinon.stub().callsFake(function(filter, opts) {
expect(opts).to.deep.equal({ max: 1, time: 15000 });
expect(filter(reaction1, botUser)).to.equal(false);
expect(filter(reaction2, botUser)).to.equal(false);

reaction1.count = 2;
expect(filter(reaction1, otherUser)).to.equal(true);
reaction1.count = 3;
expect(filter(reaction1, otherUser2)).to.equal(true);

const lateBotReaction = message.newReaction('❓', 1, true);
const lateOtherReaction = message.newReaction('❗', 1, true);
expect(filter(lateBotReaction, botUser)).to.equal(false);
expect(filter(lateOtherReaction, otherUser)).to.equal(false);
return Promise.resolve();
});

await bot.emojiPrompt(message, reactionList);

expect(bot.performReactions.callCount).to.equal(1);
expect(message.awaitReactions.callCount).to.equal(1);
});

it('awaits reactions correctly with target', async function() {
const { bot, message } = this;
const reactionList = ['✅', '❎'];

const reaction1 = message.newReaction('✅', 1, true);
const reaction2 = message.newReaction('❎', 1, true);
reaction1.remove = reaction2.remove = sinon.stub();

const botUser = bot.client.user;
const otherUser = bot.client.newUser();
const otherUser2 = bot.client.newUser();

bot.performReactions = sinon.stub().withArgs(message, reactionList).resolves([reaction1, reaction2]);
message.awaitReactions = sinon.stub().callsFake(function(filter, opts) {
expect(opts).to.deep.equal({ max: 1, time: 15000 });
expect(filter(reaction1, botUser)).to.equal(false);
expect(filter(reaction2, botUser)).to.equal(false);

reaction1.count = 2;
expect(filter(reaction1, otherUser)).to.equal(true);
reaction1.count = 3;
expect(filter(reaction1, otherUser2)).to.equal(false);
return Promise.resolve();
});

await bot.emojiPrompt(message, reactionList, otherUser);

expect(bot.performReactions.callCount).to.equal(1);
expect(message.awaitReactions.callCount).to.equal(1);
});

it('does not perform extraneous actions', async function() {
const { bot, message } = this;
const reactionList = ['✅', '❎'];

const reaction1 = message.newReaction('✅', 1, true);
reaction1.remove = sinon.stub();

const reaction2 = message.newReaction('❎', 1, true);
reaction2.remove = sinon.stub();

bot.performReactions = sinon.stub().withArgs(message, reactionList).resolves([reaction1, reaction2]);
message.awaitReactions = sinon.stub().resolves('test');

const resp = await bot.emojiPrompt(message, reactionList);

expect(bot.performReactions.callCount).to.equal(1);
expect(message.awaitReactions.callCount).to.equal(1);
expect(resp).to.equal('test');
expect(reaction1.remove.args).to.deep.equal([[]]);
expect(reaction2.remove.args).to.deep.equal([[]]);

expect(bot.client.sendMessageStub.callCount).to.equal(0);
expect(bot.client.sendReactionStub.callCount).to.equal(0);
});
});
});
});

0 comments on commit cee1a4c

Please sign in to comment.