summary refs log tree commit diff homepage
path: root/tests/DiscordWebhooks.test.ts
blob: 0965c3cb067e288f8e3dbd8e61dbe42c8446146d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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',
        }
      )
    })
  })
})