aboutsummaryrefslogtreecommitdiff
path: root/test-data-generator/index.ts
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-data-generator/index.ts
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-data-generator/index.ts')
-rw-r--r--test-data-generator/index.ts44
1 files changed, 44 insertions, 0 deletions
diff --git a/test-data-generator/index.ts b/test-data-generator/index.ts
new file mode 100644
index 0000000..c2b1361
--- /dev/null
+++ b/test-data-generator/index.ts
@@ -0,0 +1,44 @@
+/**
+ * Automatically generate Hypixel API responses for the unit tests
+ */
+
+import * as hypixelApi from '../src/hypixelApi'
+import * as mojang from '../src/mojang'
+import fs from 'fs/promises'
+import path from 'path'
+
+const playerUuids = [
+ '6536bfed869548fd83a1ecd24cf2a0fd',
+ '4133cab5a7534f3f9bb636fc06a1f0fd',
+ 'ef3bb867eec048a1a9b92b451f0ffc66',
+ 'e403573808ad45ddb5c48ec7c4db0144',
+]
+
+async function writeTestData(requestPath: string, name: string, contents: any) {
+ const dir = path.join(__dirname, '..', 'test', 'data', requestPath)
+ await fs.mkdir(dir, { recursive: true })
+ await fs.writeFile(path.join(dir, `${name}.json`), JSON.stringify(contents, null, 2))
+}
+
+async function addResponse(requestPath: string, args: { [ key: string ]: string }, name: string) {
+ const response = await hypixelApi.sendApiRequest({
+ path: requestPath,
+ args: args,
+ key: hypixelApi.chooseApiKey()
+ })
+ await writeTestData(requestPath, name, response)
+}
+
+async function main() {
+ const uuidsToUsername = {}
+ for (const playerUuid of playerUuids) {
+ await addResponse('player', { uuid: playerUuid }, playerUuid)
+ await addResponse('skyblock/profiles', { uuid: playerUuid }, playerUuid)
+ const { username: playerUsername } = await mojang.profileFromUuid(playerUuid)
+ uuidsToUsername[playerUuid] = playerUsername
+ }
+
+ await writeTestData('', 'mojang', uuidsToUsername)
+}
+
+main() \ No newline at end of file