From 4ce80d0af8f53e93aa3a936b1ad4c5b6c065c881 Mon Sep 17 00:00:00 2001
From: mat <27899617+mat-1@users.noreply.github.com>
Date: Tue, 27 Apr 2021 14:29:07 -0500
Subject: Add unit tests (#12)

* start adding unit tests

* add more to test/data/mojang.json

* fix sending http requests in tests when it shouldn't

* add a few more tests

* try to add a github action to run tests

* Update test.yml
---
 src/database.ts      | 20 +++++++++++---------
 src/hypixelApi.ts    |  2 +-
 src/hypixelCached.ts | 15 ++++++++-------
 src/index.ts         |  5 +++--
 src/util.ts          | 10 ----------
 5 files changed, 23 insertions(+), 29 deletions(-)

(limited to 'src')

diff --git a/src/database.ts b/src/database.ts
index 5633ce4..a974f6c 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -424,12 +424,14 @@ async function fetchAllLeaderboards(fast?: boolean): Promise<void> {
 	if (debug) console.log('Finished caching leaderboards!')
 }
 
-
-connect().then(() => {
-	// when it connects, cache the leaderboards and remove bad members
-	removeBadMemberLeaderboardAttributes()
-	// cache leaderboards on startup so its faster later on
-	fetchAllLeaderboards(true)
-	// cache leaderboard players again every 4 hours
-	setInterval(fetchAllLeaderboards, 4 * 60 * 60 * 1000)
-})
+// make sure it's not in a test
+if (typeof global.it !== 'function') {
+	connect().then(() => {
+		// when it connects, cache the leaderboards and remove bad members
+		removeBadMemberLeaderboardAttributes()
+		// cache leaderboards on startup so its faster later on
+		fetchAllLeaderboards(true)
+		// cache leaderboard players again every 4 hours
+		setInterval(fetchAllLeaderboards, 4 * 60 * 60 * 1000)
+	})
+}
\ No newline at end of file
diff --git a/src/hypixelApi.ts b/src/hypixelApi.ts
index f4c9817..fdb4535 100644
--- a/src/hypixelApi.ts
+++ b/src/hypixelApi.ts
@@ -17,7 +17,7 @@ const httpsAgent = new Agent({
 
 
 /** This array should only ever contain one item because using multiple hypixel api keys isn't allowed :) */ 
-const apiKeys = process.env.hypixel_keys.split(' ')
+const apiKeys = process.env?.hypixel_keys?.split(' ') ?? []
 
 interface KeyUsage {
 	remaining: number
diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts
index 3dc89a3..aa3100b 100644
--- a/src/hypixelCached.ts
+++ b/src/hypixelCached.ts
@@ -12,49 +12,50 @@ import { debug } from '.'
 
 // cache usernames for 4 hours
 /** uuid: username */
-const usernameCache = new NodeCache({
+export const usernameCache = new NodeCache({
 	stdTTL: 60 * 60 * 4,
 	checkperiod: 60,
 	useClones: false,
 })
 
-const basicProfilesCache = new NodeCache({
+export const basicProfilesCache = new NodeCache({
 	stdTTL: 60 * 10,
 	checkperiod: 60,
 	useClones: true,
 })
 
-const playerCache = new NodeCache({
+export const playerCache = new NodeCache({
 	stdTTL: 60,
 	checkperiod: 10,
 	useClones: true,
 })
 
 // cache "basic players" (players without profiles) for 4 hours
-const basicPlayerCache = new NodeCache({
+export const basicPlayerCache = new NodeCache({
 	stdTTL: 60 * 60 * 4,
 	checkperiod: 60 * 10,
 	useClones: true
 })
 
-const profileCache = new NodeCache({
+export const profileCache = new NodeCache({
 	stdTTL: 30,
 	checkperiod: 10,
 	useClones: true,
 })
 
-const profilesCache = new NodeCache({
+export const profilesCache = new NodeCache({
 	stdTTL: 60 * 3,
 	checkperiod: 10,
 	useClones: false,
 })
 
-const profileNameCache = new NodeCache({
+export const profileNameCache = new NodeCache({
 	stdTTL: 60 * 60,
 	checkperiod: 60,
 	useClones: false,
 })
 
+
 interface KeyValue {
 	key: any
 	value: any
diff --git a/src/index.ts b/src/index.ts
index 6976882..2c0f1ee 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -63,5 +63,6 @@ app.get('/leaderboards', async(req, res) => {
 })
 
 
-
-app.listen(8080, () => console.log('App started :)'))
+// only run the server if it's not doing tests
+if (typeof global.it !== 'function')
+	app.listen(8080, () => console.log('App started :)'))
diff --git a/src/util.ts b/src/util.ts
index facc425..43bb2d8 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -6,16 +6,6 @@ export function undashUuid(uuid: string): string {
 	return uuid.replace(/-/g, '').toLowerCase()
 }
 
-export function queryToJson(queryString): { [ key: string ]: string } {
-    const query = {}
-    const pairs = (queryString[0] === '?' ? queryString.substr(1) : queryString).split('&')
-    for (let i = 0; i < pairs.length; i++) {
-        const pair = pairs[i].split('=')
-        query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '')
-    }
-    return query
-}
-
 
 export function jsonToQuery(data): string {
     return Object.entries(data || {}).map(e => e.join('=')).join('&')
-- 
cgit