Skip to content

Commit

Permalink
ok I did things wrong (#34)
Browse files Browse the repository at this point in the history
* ok I did things wrong

* SHIP IT!!

* coverage is weird here

* jest tet
  • Loading branch information
zippy1981 committed Oct 30, 2023
1 parent c2837cb commit c118812
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-requests.yml
Expand Up @@ -26,7 +26,7 @@ jobs:
run: |
npm ci # This will make sure that package.json and package-lock.json are in sync
npm run lint
npx jest --coverage --coverageReporters=cobertura
npx jest --coverage
npm test # This will be unit tests
# Won't work because of permissions and I will fight later
# - uses: 5monkeys/cobertura-action@master
Expand Down
2 changes: 1 addition & 1 deletion __tests__/config.test.js
@@ -1,6 +1,6 @@
describe('Tests the config module', () => {

let OLD_ENV;
const OLD_ENV = process.env;

beforeAll(() => {

Expand Down
54 changes: 54 additions & 0 deletions __tests__/one-blocked-message.test.js
@@ -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();
});
});
1 change: 0 additions & 1 deletion config.js
@@ -1,6 +1,5 @@
require('dotenv').config();


const getConfig = () => {
const messesageFetchCount = Number.parseInt(process.env.MESSAGE_FETCH_COUNT);
const oneBlockedPercent = Number.parseFloat(process.env.ONE_BLOCKED_PERCENT);
Expand Down
27 changes: 7 additions & 20 deletions index.js
Expand Up @@ -2,21 +2,13 @@ const { Client, GatewayIntentBits } = require('discord.js');
const config = require('./config').getConfig();
const { replaceFirstMessage, splitReplaceCommand } = require('./replacer');
const { processScores, getScore } = require('./scoring');

const { oneBlockedMessage } = require('./one-blocked-message');

/**
* Gets the config but cleans out any values that should be secret.
*/
const getCleansedConfig = () => ({ ... config, Token: undefined });

/**
* Determines if the user is one of the realest mother fuckers there is.
* @param {string } username the query
* @returns true if user is one of the realest;
*/
function isRealest(username) {
return config.TheRealests.filter((k) => k.toLocaleLowerCase() === username);
}

const client = new Client({
intents: [
Expand All @@ -37,20 +29,11 @@ client.on('messageCreate', async (initialQuery) => {
initialQuery.channel.send(`Config: \`\`\`json\n${JSON.stringify(getCleansedConfig(), null, 2)}}\n\`\`\``);
}
else if (initialQuery.content.indexOf('!s ') == 0) {
// Substitution query identified

console.log('Quoting user ' + initialQuery.author.username);

const isARealOne = isRealest(initialQuery.author.username);
if (isARealOne)
{
console.debug('One of the realest', initialQuery.author);
if (oneBlockedMessage(initialQuery)) {
return;
}
if(Math.random() * 100 > (100 - (isARealOne ? config.RealestOneBlockedPercent : config.OneBlockedPercent)))
{
initialQuery.channel.send(initialQuery.author.toString() + ' who is one blocked message');
return;
}

let channel = initialQuery.channel;

Expand All @@ -63,6 +46,10 @@ client.on('messageCreate', async (initialQuery) => {
}
else if (initialQuery.content.indexOf('!score ') == 0)
{
if (oneBlockedMessage(initialQuery)) {
return;
}

const phrase = initialQuery.content.replace(/^!score/, '').trim();
getScore(phrase, score => {
initialQuery.channel.send(`Score *${phrase}*: ${score}`);
Expand Down
8 changes: 4 additions & 4 deletions jest.config.json
Expand Up @@ -8,10 +8,10 @@
"clearMocks": true,
"coverageThreshold": {
"global": {
"branches": 50,
"functions": 63,
"lines": 60,
"statements": 59
"branches": 56,
"functions": 75,
"lines": 66,
"statements": 65
}
}
}
34 changes: 34 additions & 0 deletions one-blocked-message.js
@@ -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
};

0 comments on commit c118812

Please sign in to comment.