|
| 1 | +/* eslint no-implicit-dependencies: "off" */ |
| 2 | +/* eslint no-magic-numbers: "off" */ |
| 3 | +/* tslint:disable:no-implicit-dependencies no-magic-numbers */ |
| 4 | +import { expect } from 'chai'; |
| 5 | +import 'mocha'; |
| 6 | + |
| 7 | +import { getMessageMock } from '../../test/mocks'; |
| 8 | +import * as Discord from 'discord.js'; |
| 9 | + |
| 10 | +import roll, { parseDice, rollDices, instruction } from './roll'; |
| 11 | + |
| 12 | +describe('roll', () => { |
| 13 | + describe('parser', () => { |
| 14 | + it('parses valid simple roll', () => { |
| 15 | + const res = parseDice('4d6'); |
| 16 | + expect(res.count).to.be.equal(4); |
| 17 | + expect(res.dice).to.be.equal(6); |
| 18 | + }); |
| 19 | + it('throws on invalid input', () => { |
| 20 | + expect(() => parseDice('4ds6')).to.throw(); |
| 21 | + }); |
| 22 | + it('parses valid roll with modifier', () => { |
| 23 | + const res = parseDice('4d6+5'); |
| 24 | + expect(res.count).to.be.equal(4); |
| 25 | + expect(res.dice).to.be.equal(6); |
| 26 | + expect(res.modifier).to.be.equal(5); |
| 27 | + }); |
| 28 | + it('parses valid roll with negative modifier', () => { |
| 29 | + const res = parseDice('4d6-3'); |
| 30 | + expect(res.count).to.be.equal(4); |
| 31 | + expect(res.dice).to.be.equal(6); |
| 32 | + expect(res.modifier).to.be.equal(-3); |
| 33 | + }); |
| 34 | + }); |
| 35 | + describe('roll', () => { |
| 36 | + it('allows valid response', () => { |
| 37 | + const count = 4; |
| 38 | + const dice = 6; |
| 39 | + const modifier = 1; |
| 40 | + const notation = `${count}d${dice}+${modifier}`; |
| 41 | + const res = rollDices(notation); |
| 42 | + expect(res.notation).to.be.equal(notation); |
| 43 | + expect(res.value).to.be.lessThan(count * dice * modifier + 1); |
| 44 | + }); |
| 45 | + it('does not allow invalid response', () => { |
| 46 | + const count = 400; |
| 47 | + const dice = 6; |
| 48 | + const modifier = 1; |
| 49 | + const notation = `${count}d${dice}+${modifier}`; |
| 50 | + expect(() => rollDices(notation)).to.throw(); |
| 51 | + }); |
| 52 | + }); |
| 53 | + describe('handleCommand', () => { |
| 54 | + it('should reply', async () => { |
| 55 | + const msg = getMessageMock('msg'); |
| 56 | + await roll.execute((msg as unknown) as Discord.Message, ['4d6']); |
| 57 | + await expect(msg.channel.send).to.have.been.calledOnce; |
| 58 | + }); |
| 59 | + it('reply instruction on fail', async () => { |
| 60 | + const msg = getMessageMock('msg'); |
| 61 | + await roll.execute((msg as unknown) as Discord.Message, ['3k5']); |
| 62 | + await expect(msg.channel.send).to.have.been.calledOnceWith(instruction); |
| 63 | + }); |
| 64 | + }); |
| 65 | +}); |
0 commit comments