Skip to content

Commit

Permalink
Boggers bad (#38)
Browse files Browse the repository at this point in the history
* First pass

* OK this probably works

* It is finished
  • Loading branch information
zippy1981 committed Apr 1, 2024
1 parent ae0eab1 commit 6cff519
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 23 deletions.
18 changes: 9 additions & 9 deletions __tests__/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@ describe('Tests the config module', () => {

const OLD_ENV = process.env;

beforeAll(() => {

});

beforeEach(() => {
// If we mock this, then it just won't do anything, which is what we want to do.
jest.mock('dotenv');
});
// If we mock this, then it just won't do anything, which is what we want to do.
jest.mock('dotenv');

afterEach(() => {
jest.clearAllMocks();
Expand All @@ -29,7 +23,8 @@ describe('Tests the config module', () => {
'RealestOneBlockedPercent': 5,
'TheRealests': [
'kerouac5',
]
],
'SearchPhrasesToBlock': [],
});
});

Expand Down Expand Up @@ -68,4 +63,9 @@ describe('Tests the config module', () => {
expect(config.MessageFetchCount).toEqual(Number.parseInt(element));
});

it.each([' ', '\t\n, ,', ',,,'])('Empty whitespace only value %s are ignored for SEARCH_PHRASES_TO_BLOCK', (element) => {
process.env.SEARCH_PHRASES_TO_BLOCK = element;
const config = require('../config').getConfig();
expect(config.SearchPhrasesToBlock).toEqual([]);
});
});
2 changes: 1 addition & 1 deletion __tests__/one-blocked-message.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

jest.mock('../config', () => ({
getConfig: () => ({TheRealests: [ 'testRealest' ],
OneBlockedPercent: 1,
Expand Down
29 changes: 27 additions & 2 deletions __tests__/replacer.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const { replaceFirstMessage, splitReplaceCommand, extractUrls } = require('../replacer');
jest.mock('../config');
const config = require('../config');
config.getConfig.mockReturnValue({
SearchPhrasesToBlock: ['boogers', 'dong']
});
const { replaceFirstMessage, splitReplaceCommand, extractUrls } = require('../replacer');


describe('Tests the replacer module', () => {

const messages = [
'No I used this for my demo so I could justify going hog wild',
'lol is this why you went so hog wild getting the environment set up like a real project instead of brans script kiddy level hackery?',
Expand All @@ -9,7 +17,8 @@ describe('Tests the replacer module', () => {
'I guess I could get back to working on the bot but honestly I haven’t written code for a living since 2006, zippy would run circles around me',
'This is a sample string with a URL https://www.example.com and another URL http://www.example.org',
'This is not the only version, this is just an example',
'oo https://www.google.com/search?q=aaron+burr&sca_esv=575309331&sxsrf=AM9HkKngs20KZwsuZ8WffUtq81ntoB-7ww%3A1697847658021&source=hp&ei=aRkzZeaKOciGptQPoJy78Ag&iflsig=AO6bgOgAAAAAZTMnet89R6hwn_gqlPxJYlrXn89wh42m&ved=0ahUKEwim46K074WCAxVIg4kEHSDODo4Q4dUDCA0&uact=5&oq=aaron+burr&gs_lp=Egdnd3Mtd2l6IgphYXJvbiBidXJyMgsQLhiABBixAxiDATIFEAAYgAQyCxAAGIAEGLEDGIMBMhEQLhiABBjHARivARiYBRibBTIIEC4YgAQYsQMyBRAAGIAEMgUQLhiABDIFEAAYgAQyBRAAGIAEMgsQLhivARjHARiABEiJGVCoBFi8EXABeACQAQCYAVagAZQFqgECMTC4AQPIAQD4AQGoAgrCAg0QLhjHARjRAxjqAhgnwgIHECMY6gIYJ8ICDRAuGMcBGK8BGOoCGCfCAhAQABgDGI8BGOUCGOoCGIwDwgIREC4YgAQYsQMYgwEYxwEY0QPCAgsQLhiKBRixAxiDAcICDhAuGIAEGLEDGMcBGNEDwgILEAAYigUYsQMYgwHCAgsQLhiABBjHARjRA8ICCBAAGIAEGLEDwgIIEC4YsQMYgAQ&sclient=gws-wiz oo'
'oo https://www.google.com/search?q=aaron+burr&sca_esv=575309331&sxsrf=AM9HkKngs20KZwsuZ8WffUtq81ntoB-7ww%3A1697847658021&source=hp&ei=aRkzZeaKOciGptQPoJy78Ag&iflsig=AO6bgOgAAAAAZTMnet89R6hwn_gqlPxJYlrXn89wh42m&ved=0ahUKEwim46K074WCAxVIg4kEHSDODo4Q4dUDCA0&uact=5&oq=aaron+burr&gs_lp=Egdnd3Mtd2l6IgphYXJvbiBidXJyMgsQLhiABBixAxiDATIFEAAYgAQyCxAAGIAEGLEDGIMBMhEQLhiABBjHARivARiYBRibBTIIEC4YgAQYsQMyBRAAGIAEMgUQLhiABDIFEAAYgAQyBRAAGIAEMgsQLhivARjHARiABEiJGVCoBFi8EXABeACQAQCYAVagAZQFqgECMTC4AQPIAQD4AQGoAgrCAg0QLhjHARjRAxjqAhgnwgIHECMY6gIYJ8ICDRAuGMcBGK8BGOoCGCfCAhAQABgDGI8BGOUCGOoCGIwDwgIREC4YgAQYsQMYgwEYxwEY0QPCAgsQLhiKBRixAxiDAcICDhAuGIAEGLEDGMcBGNEDwgILEAAYigUYsQMYgwHCAgsQLhiABBjHARjRA8ICCBAAGIAEGLEDwgIIEC4YsQMYgAQ&sclient=gws-wiz oo',
'I have boogers',
].map(content => ({
content,
author: 'author'
Expand Down Expand Up @@ -78,6 +87,7 @@ describe('Tests the replacer module', () => {
const actual = replaceFirstMessage(messages, sut.search, sut.replacement, channel);

expect(actual).toBe(false);
expect(sut.isBlockedPhrase).toBe(false);
expect(channel.send).toBeCalledWith(expected);
});

Expand All @@ -87,7 +97,22 @@ describe('Tests the replacer module', () => {
const actual = replaceFirstMessage(messages, sut.search, sut.replacement, channel);

expect(actual).toBe(false);
expect(sut.isBlockedPhrase).toBe(false);
expect(channel.send).toBeCalledWith(expected);
});

it('respects config.SearchPhrasesToBlock for the search', () => {
const sut = splitReplaceCommand('!s green boogers/ancedote');

expect(sut.isBlockedPhrase).toBe(true);
expect(channel.send).not.toBeCalled();
});

it('respects config.SearchPhrasesToBlock for the replace', () => {
const sut = splitReplaceCommand('!s ancedote/dong');

expect(sut.isBlockedPhrase).toBe(true);
expect(channel.send).not.toBeCalled();
});

});
3 changes: 2 additions & 1 deletion __tests__/scoring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe('Tests for the scoring module', () => {
'urch++',
'poly--',
'urch++',
'urch--'
'urch--',
'exercise one uncovered line',
].forEach(async (phrase) => {
await processScores({ content: phrase });
});
Expand Down
6 changes: 6 additions & 0 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const getConfig = () => {
const messesageFetchCount = Number.parseInt(process.env.MESSAGE_FETCH_COUNT);
const oneBlockedPercent = Number.parseFloat(process.env.ONE_BLOCKED_PERCENT);
const realestOneBlockedPercentneBlockedPercent = Number.parseFloat(process.env.REALEST_ONE_BLOCKED_PERCENT);
const searchPhrasesToBlock = (process.env.SEARCH_PHRASES_TO_BLOCK ?? '').split(',').filter(phrase => phrase.trim() !== '');

return {
/**
Expand Down Expand Up @@ -41,6 +42,11 @@ const getConfig = () => {
* Percentage change of getting "who is one blocked message" for the realest.
*/
RealestOneBlockedPercent: (realestOneBlockedPercentneBlockedPercent >= 0) ? realestOneBlockedPercentneBlockedPercent : 5,

/**
* Phrases to ignore in a search and replace.
*/
SearchPhrasesToBlock : searchPhrasesToBlock,

/**
* The discord token.
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ client.on('messageCreate', async (initialQuery) => {
}

let channel = initialQuery.channel;

// TODO: we need to encapsulate all thse calls to replacer functions in another module because SOLID
const messages = await channel.messages.fetch({ limit: config.MessageFetchCount});
const splitMessage = splitReplaceCommand(initialQuery.content);
const failedToFind = replaceFirstMessage(messages, splitMessage.search, splitMessage.replacement, channel);
if(failedToFind) {
initialQuery.channel.send(initialQuery.author.toString() + ' nobody said that, dumb ass');
if(!splitMessage.isBlockedPhrase) {
const failedToFind = replaceFirstMessage(messages, splitMessage.search, splitMessage.replacement, channel);
if(failedToFind) {
initialQuery.channel.send(initialQuery.author.toString() + ' nobody said that, dumb ass');
}
}
}
else if (initialQuery.content.indexOf('!score ') == 0)
Expand Down
10 changes: 6 additions & 4 deletions jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"!./coverage/**"
],
"clearMocks": true,
"coverageThreshold": {
"resetMocks": true,
"resetModules": true,
"coverageThreshold": {
"global": {
"branches": 56,
"functions": 75,
"branches": 59,
"functions": 77,
"lines": 66,
"statements": 65
"statements": 66
}
}
}
19 changes: 17 additions & 2 deletions replacer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const config = require('./config').getConfig();

/**
* Function that takes a string and then returns a "dumbed down" version
* of it. Without smart quotes, etc.
Expand Down Expand Up @@ -88,18 +90,31 @@ function replaceFirstMessage(messages, regex, replacement, channel) {
* Takes the replace message and turn it into a searh regex and a replace message
*
* @param {string} replaceCommand
* @returns {{search:RegExp, replacement:string}}
* @returns {{search:RegExp, isBlockedPhrase:boolean, replacement:string}}
*/
function splitReplaceCommand(replaceCommand) {
var response = replaceCommand.replace(/!s /, '').split('/');
const search = new RegExp(response[0].unicodeToMerica(), 'gi');
const replacement = response[1];

return {
search,
replacement: response[1]
isBlockedPhrase: isBlockedSearchPhrase(response[0]) || isBlockedSearchPhrase(replacement),
replacement
};
}

/**
* Indicates if a phrase is blocekd from search and replace.
* @param {string} phrase The phrase to check to see if its blocked.
* @returns true if the phrase is blocked. False otherwise.
*/
function isBlockedSearchPhrase(phrase) {
return config
.SearchPhrasesToBlock
.findIndex(blockedPhrase => phrase.match(new RegExp(blockedPhrase.normalize('NFD').replace(/[\u0300-\u036f]/g, ''), 'iu'))) > -1;
}

module.exports = {
extractUrls,
replaceFirstMessage,
Expand Down

0 comments on commit 6cff519

Please sign in to comment.