aboutsummaryrefslogtreecommitdiff
path: root/test/test.js
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-04-27 14:29:07 -0500
committerGitHub <noreply@github.com>2021-04-27 14:29:07 -0500
commit4ce80d0af8f53e93aa3a936b1ad4c5b6c065c881 (patch)
treef5ffb4d7a23d7c6edee01fb605ff81348feea40d /test/test.js
parent562cd341f75bfb2701cc844cf30f1191e4170ca7 (diff)
downloadskyblock-api-4ce80d0af8f53e93aa3a936b1ad4c5b6c065c881.tar.gz
skyblock-api-4ce80d0af8f53e93aa3a936b1ad4c5b6c065c881.tar.bz2
skyblock-api-4ce80d0af8f53e93aa3a936b1ad4c5b6c065c881.zip
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
Diffstat (limited to 'test/test.js')
-rw-r--r--test/test.js146
1 files changed, 146 insertions, 0 deletions
diff --git a/test/test.js b/test/test.js
new file mode 100644
index 0000000..32a6321
--- /dev/null
+++ b/test/test.js
@@ -0,0 +1,146 @@
+const assert = require('assert')
+const database = require('../build/database')
+const hypixelApi = require('../build/hypixelApi')
+const hypixelCached = require('../build/hypixelCached')
+const hypixel = require('../build/hypixel')
+const util = require('../build/util')
+const mojang = require('../build/mojang')
+const fs = require('fs')
+const path = require('path')
+
+
+const cachedJsonData = {}
+
+let requestsSent = 0
+
+async function readJsonData(dir) {
+ if (cachedJsonData[dir])
+ return cachedJsonData[dir]
+
+ const data = await fs.promises.readFile(path.join('test', 'data', dir + '.json'))
+ const parsedData = JSON.parse(data)
+ cachedJsonData[dir] = parsedData
+ return parsedData
+}
+
+hypixelApi.sendApiRequest = async ({ path, key, args }) => {
+ requestsSent ++
+ switch (path) {
+ case 'player': {
+ return await readJsonData(`player/${args.uuid}`)
+ }
+ case 'skyblock/profiles': {
+ return await readJsonData(`skyblock/profiles/${args.uuid}`)
+ }
+ }
+ console.log(path, args)
+}
+
+mojang.profileFromUuid = async (uuid) => {
+ requestsSent ++
+ const uuidToUsername = await readJsonData('mojang')
+ const undashedUuid = undashUuid(uuid)
+ const username = uuidToUsername[undashUuid(undashedUuid)]
+ return { username, uuid: undashedUuid }
+}
+mojang.profileFromUsername = async (username) => {
+ requestsSent ++
+ const uuidToUsername = await readJsonData('mojang')
+ const uuid = Object.keys(uuidToUsername).find(uuid => uuidToUsername[uuid] === username)
+ return { username, uuid }
+}
+mojang.profileFromUser = async (user) => {
+ if (util.isUuid(user))
+ return await mojang.profileFromUuid(user)
+ else
+ return await mojang.profileFromUsername(user)
+}
+
+
+/** Clear all the current caches and stuff */
+function resetState() {
+ hypixelCached.usernameCache.flushAll()
+ hypixelCached.basicProfilesCache.flushAll()
+ hypixelCached.playerCache.flushAll()
+ hypixelCached.basicPlayerCache.flushAll()
+ hypixelCached.profileCache.flushAll()
+ hypixelCached.profileNameCache.flushAll()
+ requestsSent = 0
+}
+
+describe('util', () => {
+ describe('#undashUuid()', () => {
+ it('Undashes correctly', () => {
+ assert.strictEqual(util.undashUuid('6536bfed-8695-48fd-83a1-ecd24cf2a0fd'), '6536bfed869548fd83a1ecd24cf2a0fd')
+ })
+ it('Lowercases correctly', () => {
+ assert.strictEqual(util.undashUuid('6536BFED-8695-48FD-83A1-ECD24CF2A0FD'), '6536bfed869548fd83a1ecd24cf2a0fd')
+ })
+ })
+ describe('#jsonToQuery()', () => {
+ it('Creates correct query', () => {
+ assert.strictEqual(util.jsonToQuery({ 'hello': 'world' }), 'hello=world')
+ })
+ it('Creates correct query for multiple', () => {
+ assert.strictEqual(util.jsonToQuery({ 'hello': 'world', 'asdf': 'fdsa' }), 'hello=world&asdf=fdsa')
+ })
+ })
+ describe('#isUuid()', () => {
+ it('Detects correct undashed uuid', () => {
+ assert.ok(util.isUuid('6536bfed869548fd83a1ecd24cf2a0fd'))
+ })
+ it('Detects correct dashed uuid', () => {
+ assert.ok(util.isUuid('6536bfed-8695-48fd-83a1-ecd24cf2a0fd'))
+ })
+ it('Detects correct dashed and capitalized uuid', () => {
+ assert.ok(util.isUuid('6536BFED-8695-48FD-83A1-ECD24CF2A0FD'))
+ })
+ it('Detects bad uuid that\'s too long', () => {
+ assert.ok(!util.isUuid('6536bfed869548fd83a1ecd24cf2a0fda'))
+ })
+ it('Detects bad uuid that\'s too short', () => {
+ assert.ok(!util.isUuid('6536bfed869548fd83a1ecd24cf2a0f'))
+ })
+ })
+})
+
+describe('hypixel', () => {
+ describe('#fetchBasicPlayer()', () => {
+ it('Checks user uuid and username', async() => {
+ resetState()
+ const user = await hypixelCached.fetchBasicPlayer('py5')
+ assert.strictEqual(user.uuid, '6536bfed869548fd83a1ecd24cf2a0fd')
+ assert.strictEqual(user.username, 'py5')
+ })
+ it('Checks the player\'s rank', async() => {
+ resetState()
+ const user = await hypixelCached.fetchBasicPlayer('py5')
+ assert.strictEqual(user.rank.name, 'MVP+')
+ assert.strictEqual(user.rank.color, '#3ffefe')
+ assert.strictEqual(user.rank.colored, '§b[MVP§2+§b]')
+ })
+ it('Makes sure caching works properly', async() => {
+ resetState()
+ await hypixelCached.fetchBasicPlayer('py5')
+ // 1 request to mojang, 1 request to hypixel
+ assert.strictEqual(requestsSent, 2)
+ await hypixelCached.fetchBasicPlayer('py5')
+ // since it's caching, it should still be 2 requests
+ assert.strictEqual(requestsSent, 2)
+ })
+ })
+
+ describe('#fetchUser()', () => {
+ it('Makes sure user.player exists', async() => {
+ resetState()
+ const user = await hypixel.fetchUser(
+ { user: 'py5' },
+ ['profiles', 'player']
+ )
+ assert.ok(user.player)
+ })
+ })
+})
+
+
+