summary refs log tree commit diff homepage
path: root/tests/DiscordWebhooks.test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'tests/DiscordWebhooks.test.ts')
-rw-r--r--tests/DiscordWebhooks.test.ts95
1 files changed, 95 insertions, 0 deletions
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