-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ok I did things wrong * SHIP IT!! * coverage is weird here * jest tet
- Loading branch information
Showing
7 changed files
with
101 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
describe('Tests the config module', () => { | ||
|
||
let OLD_ENV; | ||
const OLD_ENV = process.env; | ||
|
||
beforeAll(() => { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
const { oneBlockedMessage } = require('../one-blocked-message'); | ||
jest.mock('dotenv'); | ||
jest.mock('../config', () => ({ | ||
getConfig: () => ({TheRealests: [ 'testRealest' ], | ||
OneBlockedPercent: 1, | ||
RealestOneBlockedPercent: 5 | ||
}) | ||
})); | ||
|
||
describe('Tests the one blocked message module', () => { | ||
beforeAll(() => { | ||
// If we mock this, then it just won't do anything, which is what we want to do. | ||
|
||
}); | ||
|
||
beforeEach(() => { | ||
jest.spyOn(global.Math, 'random').mockReturnValue(0.96); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test('oneBlockedMessage sends a message to a realest user and returns true when random value is greater than trigger percentage for a realest user but less than for a regular user', () => { | ||
const initialQuery = { | ||
author: { | ||
toString: () => '@testRealest', | ||
username: 'testRealest' | ||
}, | ||
channel: { send: jest.fn() } | ||
}; | ||
|
||
const result = oneBlockedMessage(initialQuery); | ||
|
||
expect(result).toBe(true); | ||
expect(initialQuery.channel.send).toHaveBeenCalledWith('@testRealest who is one blocked message'); | ||
}); | ||
|
||
test('oneBlockedMessage does not send a message and returns false when random value is less than or equal to trigger percentage', () => { | ||
// Change the mock return value for this test | ||
|
||
const initialQuery = { | ||
author: { username: 'testUser' }, | ||
channel: { send: jest.fn() } | ||
}; | ||
|
||
const result = oneBlockedMessage(initialQuery); | ||
|
||
expect(result).toBe(false); | ||
expect(initialQuery.channel.send).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const config = require('./config').getConfig(); | ||
|
||
/** | ||
* Determines if the user is one of the realest mother fuckers there is. | ||
* @param {string } username the query | ||
* @returns {boolean} true if user is one of the realest; | ||
*/ | ||
function isRealest(username) { | ||
return config.TheRealests.some((k) => k.localeCompare(username, undefined, { sensitivity: 'base' }) === 0); | ||
} | ||
|
||
/** | ||
* Randomly sends the message "who is one blocked message" | ||
* | ||
* @param {{author: { username: string}, channel: {send: function(string)} }} initialQuery the message to send. | ||
* @returns true if one blocked message gets sent, false otherwise. | ||
*/ | ||
function oneBlockedMessage(initialQuery) { | ||
const isARealOne = isRealest(initialQuery.author.username); | ||
const randomVal = Math.random() * 100; | ||
const triggerPecentage = 100 - (isARealOne ? config.RealestOneBlockedPercent : config.OneBlockedPercent); | ||
console.debug(`Random Value: ${randomVal} - Trigger: ${triggerPecentage}. User ${initialQuery.author.username} - Realest: ${isARealOne}.`); | ||
if(randomVal > triggerPecentage) | ||
{ | ||
initialQuery.channel.send(initialQuery.author.toString() + ' who is one blocked message'); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
module.exports = { | ||
oneBlockedMessage | ||
}; |