From 3b0306b0f09d79426c117784092701e1a4d8133c Mon Sep 17 00:00:00 2001 From: destruc7i0n Date: Mon, 3 Jan 2022 16:08:32 -0500 Subject: added tests --- tests/Discord.test.ts | 42 +++++++++++++++++++ tests/DiscordWebhooks.test.ts | 95 ++++++++++++++++++++++++++++++++++++++++++ tests/MinecraftHandler.test.ts | 47 +++++++++++++++++++++ tests/constants.ts | 8 ++++ tests/setup.js | 3 ++ 5 files changed, 195 insertions(+) create mode 100644 tests/Discord.test.ts create mode 100644 tests/DiscordWebhooks.test.ts create mode 100644 tests/MinecraftHandler.test.ts create mode 100644 tests/constants.ts create mode 100644 tests/setup.js (limited to 'tests') diff --git a/tests/Discord.test.ts b/tests/Discord.test.ts new file mode 100644 index 0000000..400aa98 --- /dev/null +++ b/tests/Discord.test.ts @@ -0,0 +1,42 @@ +import Discord from '../src/Discord' + +import { defaultConfig } from './constants' + +describe('Discord', () => { + describe('replace mentions', () => { + test('does not replace mentions if config is disabled', async () => { + const discord = new Discord(defaultConfig) + + const replacedMessage = await discord['replaceDiscordMentions']('hey @destruc7i0n#7070') + expect(replacedMessage).toBe('hey @destruc7i0n#7070') + }) + + // test('does replace mentions if config is enabled', async () => { + // const discord = new Discord({...defaultConfig, ALLOW_USER_MENTIONS: true}) + // await discord.init() + + // const replacedMessage = await discord['replaceDiscordMentions']('hey @destruc7i0n#7070') + // expect(replacedMessage).toBe('hey <@129277271843274752>') + // }) + + test('removes @everyone and @here if disabled', async () => { + const discord = new Discord({...defaultConfig, ALLOW_HERE_EVERYONE_MENTIONS: false}) + + const everyone = await discord['replaceDiscordMentions']('hey @everyone') + const here = await discord['replaceDiscordMentions']('hey @here') + + expect(everyone).toBe('hey @ everyone') + expect(here).toBe('hey @ here') + }) + + test('keeps @everyone and @here if enabled', async () => { + const discord = new Discord({...defaultConfig, ALLOW_HERE_EVERYONE_MENTIONS: true}) + + const everyone = await discord['replaceDiscordMentions']('hey @everyone') + const here = await discord['replaceDiscordMentions']('hey @here') + + expect(everyone).toBe('hey @everyone') + expect(here).toBe('hey @here') + }) + }) +}) \ No newline at end of file diff --git a/tests/DiscordWebhooks.test.ts b/tests/DiscordWebhooks.test.ts new file mode 100644 index 0000000..0965c3c --- /dev/null +++ b/tests/DiscordWebhooks.test.ts @@ -0,0 +1,95 @@ +import DiscordWebhooks from '../src/DiscordWebhooks' + +import { defaultConfig } from './constants' + +describe('DiscordWebhooks', () => { + test('parses discord webhooks', () => { + const dw = new DiscordWebhooks(defaultConfig) + + expect(dw['parseDiscordWebhook']('https://discordapp.com/api/webhooks/id/token')).toStrictEqual({ id: 'id', token: 'token' }) + expect(dw['parseDiscordWebhook']('https://ptb.discordapp.com/api/webhooks/id/token')).toStrictEqual({ id: 'id', token: 'token' }) + expect(dw['parseDiscordWebhook']('https://discord.com/api/webhooks/id/token')).toStrictEqual({ id: 'id', token: 'token' }) + expect(dw['parseDiscordWebhook']('https://canary.discord.com/api/webhooks/id/token')).toStrictEqual({ id: 'id', token: 'token' }) + + expect(dw['parseDiscordWebhook']('https://diskrod.com/api/webhooks/id/token')).toStrictEqual({ id: null, token: null }) + }) + + test('gets uuid from username and caches', async () => { + const dw = new DiscordWebhooks(defaultConfig) + + expect(await dw['getUUIDFromUsername']('destruc7i0n')).toBe('2d8cf844fa3441c38d4e597b32697909') + expect(await dw['getUUIDFromUsername']('hypixel')).toBe('f7c77d999f154a66a87dc4a51ef30d19') + + expect(dw['uuidCache'].get('destruc7i0n')).toBe('2d8cf844fa3441c38d4e597b32697909') + // this should be cached + expect(await dw['getUUIDFromUsername']('destruc7i0n')).toBe('2d8cf844fa3441c38d4e597b32697909') + }) + + describe('webhook generation', () => { + test('creates for valid players', async () => { + const dw = new DiscordWebhooks(defaultConfig) + + expect(await dw['makeDiscordWebhook']('destruc7i0n', 'hey')).toStrictEqual( + { + username: 'destruc7i0n', + content: 'hey', + avatarURL: 'https://mc-heads.net/avatar/2d8cf844fa3441c38d4e597b32697909/256', + } + ) + + expect(await dw['makeDiscordWebhook']('hypixel', 'hey')).toStrictEqual( + { + username: 'hypixel', + content: 'hey', + avatarURL: 'https://mc-heads.net/avatar/f7c77d999f154a66a87dc4a51ef30d19/256', + } + ) + }) + + test('creates with default player head for invalid players', async () => { + const dw = new DiscordWebhooks(defaultConfig) + + // inb4 someone makes `fakedestruc7i0n` + expect(await dw['makeDiscordWebhook']('fakedestruc7i0n', 'hey')).toStrictEqual( + { + username: 'fakedestruc7i0n', + content: 'hey', + // default player head + avatarURL: 'https://mc-heads.net/avatar/c06f89064c8a49119c29ea1dbd1aab82/256', + } + ) + + const dw2 = new DiscordWebhooks({...defaultConfig, DEFAULT_PLAYER_HEAD: '2d8cf844fa3441c38d4e597b32697909'}) + + // inb4 someone makes `fakedestruc7i0n` + expect(await dw2['makeDiscordWebhook']('fakedestruc7i0n', 'hey')).toStrictEqual( + { + username: 'fakedestruc7i0n', + content: 'hey', + avatarURL: 'https://mc-heads.net/avatar/2d8cf844fa3441c38d4e597b32697909/256', + } + ) + }) + + test('creates for server message', async () => { + const dw = new DiscordWebhooks(defaultConfig) + + expect(await dw['makeDiscordWebhook']('Shulker - Server', 'hey')).toStrictEqual( + { + username: 'Shulker - Server', + content: 'hey', + } + ) + + const dw2 = new DiscordWebhooks({...defaultConfig, SERVER_IMAGE: 'https://thedestruc7i0n.ca'}) + + expect(await dw2['makeDiscordWebhook']('Shulker - Server', 'hey')).toStrictEqual( + { + username: 'Shulker - Server', + content: 'hey', + avatarURL: 'https://thedestruc7i0n.ca', + } + ) + }) + }) +}) \ No newline at end of file diff --git a/tests/MinecraftHandler.test.ts b/tests/MinecraftHandler.test.ts new file mode 100644 index 0000000..4bb10f4 --- /dev/null +++ b/tests/MinecraftHandler.test.ts @@ -0,0 +1,47 @@ +import MinecraftHandler from '../src/MinecraftHandler' + +import { defaultConfig } from './constants' + +describe('MinecraftHandler', () => { + test('parses player join connection stat', () => { + const handler = new MinecraftHandler(defaultConfig) + + const { message } = handler['parseLogLine']('[Server thread/INFO]: destruc7i0n joined the game')! + + expect(message).toBe('destruc7i0n joined the game') + }) + + test('parses player leave connection stat', () => { + const handler = new MinecraftHandler(defaultConfig) + + const { message } = handler['parseLogLine']('[Server thread/INFO]: destruc7i0n left the game')! + + expect(message).toBe('destruc7i0n left the game') + }) + + test('parses death messages', () => { + const handler = new MinecraftHandler(defaultConfig) + + const tests = [ + '[Server thread/INFO]: destruc7i0n drowned', + '[Server thread/INFO]: destruc7i0n died', + '[Server thread/INFO]: destruc7i0n experienced kinetic energy', + '[Server thread/INFO]: destruc7i0n blew up', + '[Server thread/INFO]: destruc7i0n hit the ground too hard', + '[Server thread/INFO]: destruc7i0n fell off a ladder', + '[Server thread/INFO]: destruc7i0n was squashed by a falling anvil', + '[Server thread/INFO]: destruc7i0n went off with a bang', + '[Server thread/INFO]: destruc7i0n tried to swim in lava', + '[Server thread/INFO]: destruc7i0n was slain by mcfunction', + '[Server thread/INFO]: destruc7i0n suffocated in a wall', + '[Server thread/INFO]: destruc7i0n fell out of the world', + '[Server thread/INFO]: destruc7i0n withered away', + ] + + for (const test of tests) { + const { message } = handler['parseLogLine'](test)! + + expect(message).toBe(test.replace('[Server thread/INFO]: ', '')) + } + }) +}) \ No newline at end of file diff --git a/tests/constants.ts b/tests/constants.ts new file mode 100644 index 0000000..f60aa10 --- /dev/null +++ b/tests/constants.ts @@ -0,0 +1,8 @@ +import type { Config } from '../src/Config' + +export const defaultConfig: Config = { + ...require('../config.example.json'), + DISCORD_TOKEN: process.env.DISCORD_TOKEN, + WEBHOOK_URL: process.env.WEBHOOK_URL, + DISCORD_CHANNEL_ID: process.env.DISCORD_CHANNEL_ID, +} diff --git a/tests/setup.js b/tests/setup.js new file mode 100644 index 0000000..dada92d --- /dev/null +++ b/tests/setup.js @@ -0,0 +1,3 @@ +const path = require('path') + +require('dotenv').config({ path: path.resolve('./.env.test') }) -- cgit 1.4.1