|
| 1 | +import { expect } from 'chai'; |
| 2 | +import type * as Discord from 'discord.js'; |
| 3 | +import Sinon from 'sinon'; |
| 4 | +import * as Db from '../db'; |
| 5 | + |
| 6 | +import { getMessageMock, getMemberMock } from '../../test/mocks'; |
| 7 | + |
| 8 | +import { addKarma, KARMA_REGEX } from './karma'; |
| 9 | + |
| 10 | +type MemberMock = ReturnType<typeof getMemberMock>; |
| 11 | + |
| 12 | +const getAddKarmaMsgMock = (from: MemberMock, membersToReward: ReadonlyArray<MemberMock>) => { |
| 13 | + const msg = getMessageMock('msg', { |
| 14 | + mentions: { |
| 15 | + members: { |
| 16 | + size: membersToReward.length, |
| 17 | + values: () => [ |
| 18 | + ...[from, ...membersToReward].map(({ id, mention }) => ({ |
| 19 | + fetch: () => ({ id, toString: () => mention }), |
| 20 | + })), |
| 21 | + ], |
| 22 | + }, |
| 23 | + }, |
| 24 | + author: { id: from.id, toString: () => from.mention }, |
| 25 | + }); |
| 26 | + |
| 27 | + return msg; |
| 28 | +}; |
| 29 | + |
| 30 | +type DB = ReturnType<typeof Db.initDb>; |
| 31 | + |
| 32 | +describe('add karma', () => { |
| 33 | + const AggregateMock = { |
| 34 | + sort() { |
| 35 | + return this; |
| 36 | + }, |
| 37 | + limit() { |
| 38 | + return this; |
| 39 | + }, |
| 40 | + toArray() { |
| 41 | + return []; |
| 42 | + }, |
| 43 | + }; |
| 44 | + |
| 45 | + const CollectionMock = { |
| 46 | + insertMany: Sinon.stub(), |
| 47 | + aggregate: Sinon.stub().returns(AggregateMock), |
| 48 | + }; |
| 49 | + |
| 50 | + const DbMock = { |
| 51 | + collection: Sinon.stub().returns(CollectionMock), |
| 52 | + } as unknown as DB; |
| 53 | + |
| 54 | + beforeEach(() => { |
| 55 | + Sinon.stub(Db, 'initDb').returns(Promise.resolve(DbMock)); |
| 56 | + }); |
| 57 | + afterEach(() => { |
| 58 | + Sinon.reset(); |
| 59 | + Sinon.restore(); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('checks regex', () => { |
| 63 | + it('checks if one user should be given a karma', () => { |
| 64 | + const memberToReward = getMemberMock(); |
| 65 | + |
| 66 | + const msg = getMessageMock('msg', { content: `${memberToReward.mention} ++` }); |
| 67 | + expect(KARMA_REGEX.test(msg.content)).to.have.be.true; |
| 68 | + }); |
| 69 | + |
| 70 | + it('checks if two users should be given a karma', () => { |
| 71 | + const firstMemberToReward = getMemberMock(); |
| 72 | + const secondMemberToReward = getMemberMock(); |
| 73 | + |
| 74 | + const firstMsg = getMessageMock('msg', { |
| 75 | + content: `${firstMemberToReward.mention} ${secondMemberToReward.mention} ++`, |
| 76 | + }); |
| 77 | + expect(KARMA_REGEX.test(firstMsg.content)).to.have.be.true; |
| 78 | + |
| 79 | + const secondMsg = getMessageMock('msg', { |
| 80 | + content: `${firstMemberToReward.mention} ++ ${secondMemberToReward.mention}`, |
| 81 | + }); |
| 82 | + expect(KARMA_REGEX.test(secondMsg.content)).to.have.be.true; |
| 83 | + |
| 84 | + const thirdMsg = getMessageMock('msg', { |
| 85 | + content: `${firstMemberToReward.mention} ++ ${secondMemberToReward.mention} ++`, |
| 86 | + }); |
| 87 | + expect(KARMA_REGEX.test(thirdMsg.content)).to.have.be.true; |
| 88 | + }); |
| 89 | + |
| 90 | + it('checks if one user should not be given a karma', () => { |
| 91 | + const memberToReward = getMemberMock(); |
| 92 | + |
| 93 | + const msg = getMessageMock('msg', { content: `random message ${memberToReward.mention} ++` }); |
| 94 | + expect(KARMA_REGEX.test(msg.content)).to.have.be.false; |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should add karma for one user', async () => { |
| 99 | + const from = getMemberMock(); |
| 100 | + const memberToReward = getMemberMock(); |
| 101 | + |
| 102 | + const msg = getAddKarmaMsgMock(from, [memberToReward]); |
| 103 | + |
| 104 | + await addKarma.execute(msg as unknown as Discord.Message, []); |
| 105 | + |
| 106 | + const arg = CollectionMock.insertMany.lastCall.firstArg[0]; |
| 107 | + expect(arg.from).to.eql(from.id); |
| 108 | + expect(arg.to).to.eql(memberToReward.id); |
| 109 | + expect(arg.value).to.eql(1); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should add karma for two users', async () => { |
| 113 | + const from = getMemberMock(); |
| 114 | + |
| 115 | + const firstMemberToReward = getMemberMock(); |
| 116 | + const secondMemberToReward = getMemberMock(); |
| 117 | + |
| 118 | + const msg = getAddKarmaMsgMock(from, [firstMemberToReward, secondMemberToReward]); |
| 119 | + |
| 120 | + await addKarma.execute(msg as unknown as Discord.Message, []); |
| 121 | + |
| 122 | + { |
| 123 | + const arg = CollectionMock.insertMany.lastCall.firstArg[0]; |
| 124 | + expect(arg.from).to.eql(from.id); |
| 125 | + expect(arg.to).to.eql(firstMemberToReward.id); |
| 126 | + expect(arg.value).to.eql(1); |
| 127 | + } |
| 128 | + { |
| 129 | + const arg = CollectionMock.insertMany.lastCall.firstArg[1]; |
| 130 | + expect(arg.from).to.eql(from.id); |
| 131 | + expect(arg.to).to.eql(secondMemberToReward.id); |
| 132 | + expect(arg.value).to.eql(1); |
| 133 | + } |
| 134 | + }); |
| 135 | +}); |
0 commit comments