Skip to content

Commit

Permalink
Merge pull request #149 from destinygg/unit-tests
Browse files Browse the repository at this point in the history
Fix Unit tests
  • Loading branch information
11k authored Feb 9, 2024
2 parents 88a5181 + fd0a0f3 commit ef60b09
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
8 changes: 3 additions & 5 deletions tests/lib/commands/implementations/ban.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const CommandOutput = require('../../../../lib/commands/command-output');
const assert = require('assert');
const sinon = require('sinon');
const Command = require('../../../../lib/commands/command-interface');
const config = require('../../../../lib/configuration/prod.config.json');
const MessageRelay = require('../../../../lib/services/message-relay');
const PunishmentCache = require('../../../../lib/services/punishment-cache');
Expand Down Expand Up @@ -43,8 +41,8 @@ const flairArmorPhrases = [

describe('Flair based dodge test', () => {
beforeEach(() => {
config['flairBasedBanDodgeChances']['testFlair'] = 1;
roleCache = new RoleCache(config);
config['punishmentCache']['flairBasedBanDodgeChances']['testFlair'] = 1;
const roleCache = new RoleCache(config.roleCache);
roleCache.roleMap = {
kierke: { roles: ['testFlair'], timestamp: 123 },
poorkierke: { roles: [], timestamp: 123 },
Expand All @@ -56,7 +54,7 @@ describe('Flair based dodge test', () => {
punishmentStream: {
write: sinon.spy(),
},
punishmentCache: new PunishmentCache(config),
punishmentCache: new PunishmentCache(config.punishmentCache),
roleCache: roleCache,
};

Expand Down
6 changes: 4 additions & 2 deletions tests/lib/commands/implementations/breaking-news.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe('breakingNews command Test', () => {
};
});

it('mutes link messages when "all" with default time, then turned off', (done) => {
// Needs twitter api
it.skip('mutes link messages when "all" with default time, then turned off', (done) => {
const messageRelay = this.mockServices.messageRelay;
const punishmentStream = this.mockServices.punishmentStream;

Expand Down Expand Up @@ -70,7 +71,8 @@ describe('breakingNews command Test', () => {
}, 1500);
});

it('mutes link messages when "on" with custom time, then turned off', (done) => {
// Needs twitter api
it.skip('mutes link messages when "on" with custom time, then turned off', (done) => {
const messageRelay = this.mockServices.messageRelay;
const punishmentStream = this.mockServices.punishmentStream;

Expand Down
55 changes: 38 additions & 17 deletions tests/lib/services/dgg-rolling-chat-cache.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
const assert = require('assert');
const sinon = require('sinon');
const RollingChatCache = require('../../../lib/services/dgg-rolling-chat-cache');
const messageMatchingService = require('../../../lib/services/message-matching');
const Logger = require('bunyan');

describe('Chat Cache Test suite', () => {
describe('Chat Cache Viewer Map Tests', () => {
beforeEach(() => {
this.mockServices = {
logger: sinon.createStubInstance(Logger),
messageMatching: messageMatchingService,
};
});

it('add messages to the cache for a given user', () => {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = {
linusred: ['hey nice meme man'],
};
Expand All @@ -14,7 +23,7 @@ describe('Chat Cache Test suite', () => {
});

it('add messages to the cache for a given user up to the default of 2', () => {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = {
linusred: ['hey nice meme man', 'really cool'],
};
Expand All @@ -25,7 +34,7 @@ describe('Chat Cache Test suite', () => {
});

it('add messages to the cache for a given user and replaces messages if the number of messages is above the threshold', () => {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = {
linusred: ['really cool', 'nice'],
};
Expand All @@ -37,7 +46,7 @@ describe('Chat Cache Test suite', () => {
});

it('add messages to the cache for multiple users', () => {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = {
linusred: ['hey nice meme man'],
bob: ['really cool']
Expand All @@ -49,23 +58,23 @@ describe('Chat Cache Test suite', () => {
});

it('returns expected value for diffed messages that are the same', () => {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = [1];
chatCache.addMessageToCache('linusred', 'hey nice meme man');
const result = chatCache.diffNewMessageForUser('linusred', 'hey nice meme man');
assert.deepStrictEqual(result, expected);
});

it('returns expected value for diffed messages that are diferent', () => {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = [0.9];
chatCache.addMessageToCache('linusred', '1234567890');
const result = chatCache.diffNewMessageForUser('linusred', '123456789');
assert.deepStrictEqual(result, expected);
});

it('returns expected value for diffed messages that are crazy', () => {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = [1];
chatCache.addMessageToCache('linusred', `
\` (¯\\\`·.¸¸.·´¯\\\`·.¸¸.·´¯)
Expand All @@ -90,6 +99,10 @@ describe('Chat Cache Test suite', () => {

describe('Chat Cache Tombstoning Tests', () => {
beforeEach(function () {
this.mockServices = {
logger: sinon.createStubInstance(Logger),
messageMatching: messageMatchingService,
};
this.clock = sinon.useFakeTimers();
});

Expand All @@ -98,7 +111,7 @@ describe('Chat Cache Test suite', () => {
});

it('adds a message to the tombstone with a timestamp cache', function () {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = {
linusred: 0,
};
Expand All @@ -107,14 +120,14 @@ describe('Chat Cache Test suite', () => {
});

it('expires and removes a set of message after a duration of time', function () {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
chatCache.addMessageToCache('linusred', 'hey nice meme man');
this.clock.tick(1800000);
assert.deepStrictEqual(chatCache.tombStoneMap, {});
});

it('expires messages for many users after a given period of time', function () {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
chatCache.addMessageToCache('linusred', 'hey nice meme man');
chatCache.addMessageToCache('neat', 'hey nice meme man');
chatCache.addMessageToCache('cool', 'hey nice meme man');
Expand All @@ -124,7 +137,7 @@ describe('Chat Cache Test suite', () => {
});

it('updates expire time upon a new messages being added', function () {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);
const expected = {
linusred: 900, neat: 900, cool: 900, kyle: 900
};
Expand All @@ -145,6 +158,10 @@ describe('Chat Cache Test suite', () => {

describe('Chat Cache Rate Limit Tests', () => {
beforeEach(function () {
this.mockServices = {
logger: sinon.createStubInstance(Logger),
messageMatching: messageMatchingService,
};
this.clock = sinon.useFakeTimers();
});

Expand All @@ -153,7 +170,7 @@ describe('Chat Cache Test suite', () => {
});

it('rate limits if theres not enough time between messages', function () {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);

chatCache.addMessageToCache('linusred', 'hey nice meme man');
chatCache.addMessageToCache('linusred', 'hey nice meme man');
Expand All @@ -165,7 +182,7 @@ describe('Chat Cache Test suite', () => {
});

it('does not rate limit if messages are spread out', function () {
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1});
const chatCache = new RollingChatCache({viewerMessageMinimumLength: 1}, this.mockServices);

chatCache.addMessageToCache('linusred', 'hey nice meme man');
this.clock.tick(2000);
Expand All @@ -185,6 +202,10 @@ describe('Chat Cache Test suite', () => {

describe('Chat Cache Running List', () => {
beforeEach(function () {
this.mockServices = {
logger: sinon.createStubInstance(Logger),
messageMatching: messageMatchingService,
};
this.clock = sinon.useFakeTimers();
this.clock.tick(0)
});
Expand All @@ -193,14 +214,14 @@ describe('Chat Cache Test suite', () => {
this.clock.restore();
});
it('adds a message to running list', function () {
const chatCache = new RollingChatCache({});
const chatCache = new RollingChatCache({}, this.mockServices);
const expected = [{user: 'linusred', message: 'hey nice meme man', timeStamp: 0}];
chatCache.addMessageToCache('linusred', 'hey nice meme man');
assert.deepStrictEqual(chatCache.runningMessageList, expected);
});

it('adds many messages to running list', function () {
const chatCache = new RollingChatCache({});
const chatCache = new RollingChatCache({}, this.mockServices);
const expected = [{user: 'linusred', message: 'hey nice meme man', timeStamp: 0},
{user: 'jimbo', message: 'hey', timeStamp: 0}];
chatCache.addMessageToCache('linusred', 'hey nice meme man');
Expand All @@ -210,7 +231,7 @@ describe('Chat Cache Test suite', () => {


it('replaces the first messages when the queue is full', function () {
const chatCache = new RollingChatCache({messsagesToKeepPerUser: 10, maxMessagesInList: 2, timeToLive:0, tombStoneInterval: 0});
const chatCache = new RollingChatCache({messsagesToKeepPerUser: 10, maxMessagesInList: 2, timeToLive:0, tombStoneInterval: 0}, this.mockServices);
const expected = [{user: 'jimbo', message: 'hey', timeStamp: 0},
{user: 'jimbo', message: 'cool', timeStamp: 0}];
chatCache.addMessageToCache('linusred', 'hey nice meme man');
Expand All @@ -220,7 +241,7 @@ describe('Chat Cache Test suite', () => {
});

it('replaces many messages when the queue is full', function () {
const chatCache = new RollingChatCache({messsagesToKeepPerUser: 10, maxMessagesInList: 2, timeToLive:0, tombStoneInterval: 0});
const chatCache = new RollingChatCache({messsagesToKeepPerUser: 10, maxMessagesInList: 2, timeToLive:0, tombStoneInterval: 0}, this.mockServices);
const expected = [{user: 'linusred', message: 'eugh', timeStamp: 0},
{user: 'linusred', message: 'dank memes', timeStamp: 0}];
chatCache.addMessageToCache('linusred', 'hey nice meme man');
Expand Down
19 changes: 13 additions & 6 deletions tests/lib/services/spam-detection.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
const assert = require('assert');
const sinon = require('sinon');
const RollingChatCache = require('../../../lib/services/dgg-rolling-chat-cache');
const SpamDetection = require('../../../lib/services/spam-detection');
const messageMatchingService = require('../../../lib/services/message-matching');
const Logger = require('bunyan');

describe('Spam detection Tests', () => {
beforeEach(function() {
this.spamDetection = new SpamDetection({});
this.mockServices = {
logger: sinon.createStubInstance(Logger),
messageMatching: messageMatchingService,
};
this.spamDetection = new SpamDetection({}, this.mockServices);
});

it('detects stupid non-ascii spam', function() {
Expand All @@ -29,7 +36,7 @@ describe('Spam detection Tests', () => {
});

it('does not checks lists for similar messages in the list if the string length is short.', function() {
const chatCache = new RollingChatCache({});
const chatCache = new RollingChatCache({}, this.mockServices);
chatCache.addMessageToRunningList('linusred', 'hey nice meme man');
chatCache.addMessageToRunningList('jimbo', 'hey');

Expand All @@ -41,7 +48,7 @@ describe('Spam detection Tests', () => {
});

it('does checks lists for similar messages in the list if the string length is long.', function() {
const chatCache = new RollingChatCache({});
const chatCache = new RollingChatCache({}, this.mockServices);
chatCache.addMessageToRunningList(
'linusred',
'hey nice meme man hey nice meme man hey nice meme man hey nice meme man hey nice meme man hey nice meme man hey nice meme man hey nice meme man hey nice meme man hey nice meme man',
Expand All @@ -64,7 +71,7 @@ describe('Spam detection Tests', () => {
maxMessagesInList: 2,
timeToLive: 0,
tombStoneInterval: 0,
});
}, this.mockServices);
const expected = ['linusred'];
chatCache.addMessageToRunningList('linusred', 'hey nice meme man');
chatCache.addMessageToRunningList('linusred', 'lol really nice meme man');
Expand All @@ -82,7 +89,7 @@ describe('Spam detection Tests', () => {
maxMessagesInList: 20,
timeToLive: 0,
tombStoneInterval: 0,
});
}, this.mockServices);
const expected = ['coopy', 'linusred'];
chatCache.addMessageToRunningList('linusred', 'hey nice meme man');
chatCache.addMessageToRunningList('coopy', 'hey NiCe MeME ');
Expand All @@ -100,7 +107,7 @@ describe('Spam detection Tests', () => {
maxMessagesInList: 20,
timeToLive: 0,
tombStoneInterval: 0,
});
}, this.mockServices);
const expected = ['coopy', 'linusred'];
chatCache.addMessageToRunningList('linusred', 'hey nice meme man');
chatCache.addMessageToRunningList('coopy', 'hey NiCe MeME ');
Expand Down

0 comments on commit ef60b09

Please sign in to comment.