test: add test

This commit is contained in:
H01001000
2022-12-04 21:29:58 +08:00
parent 20396fab53
commit b21ee66d58
11 changed files with 368 additions and 5 deletions

View File

@@ -1 +0,0 @@
export { }

View File

@@ -0,0 +1,80 @@
export {}
/*
import { formatImageURL } from '../src/index.js'
import { decode, encode } from '../src/util/base64.js'
import { bigintToSnowflake, snowflakeToBigint } from '../src/util/bigint.js'
import { assertEquals } from './deps.js'
/*
it(
'[emoji] Create an emoji url',
ignore: process.env.TEST_ENV === 'UNIT',
() => {
const bot = loadBot()
assertEquals(
bot.helpers.getEmojiURL(785403373817823272n, false),
'https://cdn.discordapp.com/emojis/785403373817823272.png'
)
assertEquals(
bot.helpers.getEmojiURL(785403373817823272n, true),
'https://cdn.discordapp.com/emojis/785403373817823272.gif'
)
}
)
it(
"[guild] format a guild's icon url",
ignore: process.env.TEST_ENV === 'UNIT',
() => {
const bot = loadBot()
assertEquals(
bot.helpers.getGuildIconURL(785384884197392384n, 3837424427068676005442449262648382018748n),
'https://cdn.discordapp.com/icons/785384884197392384/46f50fb412eab14ec455d5cf777154bc.jpg?size=128'
)
}
)
it(
"[guild] format a guild's banner url",
ignore: process.env.TEST_ENV === 'UNIT',
() => {
const bot = loadBot()
assertEquals(
bot.helpers.getGuildBannerURL(613425648685547541n, {
banner: 3919584870146358272366452115178209474142n
}),
'https://cdn.discordapp.com/banners/613425648685547541/84c4964c115c128fb9100952c3b4f65e.jpg?size=128'
)
}
)
it(
"[guild] format a guild's splash url",
ignore: process.env.TEST_ENV === 'UNIT',
() => {
const bot = loadBot()
assertEquals(
bot.helpers.getGuildSplashURL(785384884197392384n, 3837424427068676005442449262648382018748n),
'https://cdn.discordapp.com/splashes/785384884197392384/46f50fb412eab14ec455d5cf777154bc.jpg?size=128'
)
}
)
it(
'[member] format a members avatar url',
ignore: process.env.TEST_ENV === 'UNIT',
() => {
const bot = loadBot()
assertEquals(
bot.helpers.getAvatarURL(130136895395987456n, '8840', {
avatar: 4055337350987360625717955448021200177333n
}),
'https://cdn.discordapp.com/avatars/130136895395987456/eae5905ad2d18d7c8deca20478b088b5.jpg?size=128'
)
}
})
*/

View File

@@ -0,0 +1,27 @@
import { expect } from 'chai'
import { formatImageURL, hasProperty } from '../../src/utils/utils.js'
const obj = { prop: 'lts372005' }
it('[utils] hasProperty does HAVE property', () => {
expect(hasProperty(obj, 'prop')).to.be.equal(true)
})
it('[utils] hasProperty does NOT HAVE property', () => {
expect(hasProperty(obj, 'lts372005')).to.be.equal(false)
})
it('[utils] format image url', () => {
expect(formatImageURL('https://skillz.is.pro')).to.be.equal(
'https://skillz.is.pro.jpg?size=128'
)
expect(formatImageURL('https://skillz.is.pro', 1024)).to.be.equal(
'https://skillz.is.pro.jpg?size=1024'
)
expect(formatImageURL('https://skillz.is.pro', 1024, 'gif')).to.be.equal(
'https://skillz.is.pro.gif?size=1024'
)
expect(formatImageURL('https://skillz.is.pro', undefined, 'gif')).to.be.equal(
'https://skillz.is.pro.gif?size=128'
)
})

View File

@@ -5,13 +5,17 @@ import { CACHED_COMMUNITY_GUILD_ID } from '../utils.js'
Deno.test({
name: '[webhook] delete a webhook',
ignore: process.env.TEST_ENV === 'UNIT',
async fn(t) {
async fn (t) {
const bot = loadBot()
const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, { name: 'deleteWebhook' })
const channel = await bot.helpers.createChannel(CACHED_COMMUNITY_GUILD_ID, {
name: 'deleteWebhook'
})
assertExists(channel?.id)
const webhook = await bot.helpers.createWebhook(channel.id, { name: 'delete' })
const webhook = await bot.helpers.createWebhook(channel.id, {
name: 'delete'
})
assertExists(webhook?.id)
await bot.helpers.deleteWebhook(webhook.id)

View File

@@ -42,4 +42,4 @@
"dependencies": {
"ws": "^8.11.0"
}
}
}

View File

@@ -26,3 +26,27 @@ describe('base64.ts', () => {
})
})
})
/** Old test */
it('[utils] encode some bytes to base64', () => {
expect(
encode(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
).to.be.deep.equal('AQIDBAUGBwgJCg==')
})
it('[utils] decode some base64 to bytes', () => {
expect(decode('AQIDBAUGBwgJCg==')).to.be.deep.equal(
new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
)
})
it('[utils] encode/decode base64 roundtrip should work', () => {
for (let i = 0; i < 10; i++) {
const bytes = []
for (let i = 0; i < 10000; i++) {
bytes.push(Math.floor(Math.random() * 256))
}
const data = new Uint8Array(bytes)
expect(decode(encode(data))).to.be.deep.equal(data)
}
})

View File

@@ -0,0 +1,20 @@
import { expect } from 'chai'
import { bigintToSnowflake, snowflakeToBigint } from '../src/bigint.js'
it('[bigint] - Transform a snowflake string to bigint', () => {
const text = '130136895395987456'
const big = 130136895395987456n
const result = snowflakeToBigint(text)
expect(big).to.be.equal(result)
expect(text).to.be.not.equal(result)
})
it('[bigint] - Transform a bigint to a string', () => {
const text = '130136895395987456'
const big = 130136895395987456n
const result = bigintToSnowflake(big)
expect(text).to.be.equal(result)
expect(big).to.be.not.equal(result)
})

View File

@@ -0,0 +1,145 @@
import { expect } from 'chai'
import sinon from 'sinon'
import { Collection } from '../src/collection.js'
describe('[collection]', () => {
let collection: Collection<any, any>
beforeEach(() => {
collection = new Collection()
})
it('[collection] collection values to array', () => {
const testCollection = new Collection([
['best', 'tri'],
['proficient', 'yui']
])
expect(testCollection.array()).to.be.deep.equal(['tri', 'yui'])
})
it('[collection] get a random value', () => {
const testCollection = new Collection([['best', 'tri']])
expect(testCollection.random() ?? '').to.be.oneOf(['best', 'tri'])
expect(collection.random()).to.be.undefined
})
describe('', () => {
beforeEach(() => {
collection.set('best developer', 'triformine')
})
it('[collection] Set a value without maxSize', () => {
expect(collection.size).to.be.equal(1)
expect(collection.get('best developer')).to.be.equal('triformine')
})
describe('', () => {
beforeEach(() => {
collection.set('deno', 'yes')
})
it('[collection] get the value of the first element', () => {
expect(collection.first()).to.be.equal('triformine')
})
it('[collection] get the value of the last element', () => {
expect(collection.last()).to.be.equal('yes')
})
})
})
describe('[collection] Create a collection with maxSize', () => {
const maxSize = 2
const maxCollection = new Collection([], {
maxSize
})
expect(maxCollection).to.exist
expect(maxCollection.maxSize).to.exist
expect(maxCollection.maxSize).to.be.equal(maxSize)
describe('[collection] Test if maxSize works properly', () => {
maxCollection.set('foo', 'bar')
maxCollection.set('me', 'you')
expect(maxCollection.size).to.be.equal(2)
maxCollection.set('this', 'not')
expect(maxCollection.size).to.be.equal(2)
it('[collection] Test if forceSet ignore maxSize', () => {
maxCollection.forceSet('this', 'not')
expect(maxCollection.size).to.be.equal(3)
})
})
})
const testCollection = new Collection([
['a', 1],
['b', 2],
['c', 3]
])
it('[collection] find by key or value', () => {
expect(testCollection.find((v, k) => v === 2)).to.be.equal(2)
expect(testCollection.find((v, k) => k === 'b')).to.be.equal(2)
})
it('[collection] filter by key or value', () => {
expect(testCollection.filter((v, k) => v === 3).size).to.be.equal(1)
expect(testCollection.filter((v, k) => k === 'd').size).to.be.equal(0)
})
it('[collection] map', () => {
expect(testCollection.map((k, v) => `${v}${k}`)).to.be.deep.equal([
'a1',
'b2',
'c3'
])
})
it('[collection] some', () => {
expect(testCollection.some((v, _) => v === 1)).to.be.equal(true)
expect(testCollection.some((v, _) => v === 4)).to.be.equal(false)
})
it('[collection] every', () => {
expect(testCollection.every((v, _) => v !== 0)).to.be.equal(true)
expect(testCollection.every((v, _) => v === 1)).to.be.equal(false)
})
it('[collection] reduce', () => {
expect(testCollection.reduce((acc, val) => acc + val, 0)).to.be.equal(6)
})
it('[collection] start sweeper', async () => {
const clock = sinon.useFakeTimers()
const sweeperCollection = new Collection(
[
['a', 1],
['b', 2]
],
{
sweeper: {
filter: (v, _) => v === 1,
interval: 50
}
}
)
try {
await clock.tickAsync(49)
expect(sweeperCollection.size).to.be.equal(2)
await clock.tickAsync(1)
expect(sweeperCollection.size).to.be.equal(1)
} catch (err) {
sweeperCollection.stopSweeper()
throw err
}
sweeperCollection.stopSweeper()
clock.restore()
})
})

View File

@@ -0,0 +1,24 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { expect } from 'chai'
import { iconBigintToHash, iconHashToBigInt } from '../src/hash.js'
const iconHash = '4bbb271a13f7195031adcc06a2d867ce'
const iconBigInt = 3843769888406823508519992434416504301518n
const a_iconHash = 'a_4bbb271a13f7195031adcc06a2d867ce'
const a_iconBigInt = 3503487521485885045056617826984736090062n
it('[utils] icon hash to bigint', () => {
expect(iconHashToBigInt(iconHash)).to.be.equal(iconBigInt)
})
it('[utils] icon bigint to hash', () => {
expect(iconBigintToHash(iconBigInt)).to.be.equal(iconHash)
})
it('[utils] icon hash to bigint a_ (animated)', () => {
expect(iconHashToBigInt(a_iconHash)).to.be.equal(a_iconBigInt)
})
it('[utils] icon bigint to hash a_ (animated)', () => {
expect(iconBigintToHash(a_iconBigInt)).to.be.equal(a_iconHash)
})

View File

@@ -0,0 +1,14 @@
import { expect } from 'chai'
import { removeTokenPrefix } from '../src/token.js'
it('[token] Remove token prefix when Bot is prefixed.', () => {
expect(removeTokenPrefix('Bot discordeno is best lib')).to.be.equal(
'discordeno is best lib'
)
})
it('[token] Remove token prefix when Bot is NOT prefixed.', () => {
expect(removeTokenPrefix('discordeno is best lib')).to.be.equal(
'discordeno is best lib'
)
})

View File

@@ -0,0 +1,26 @@
import { expect } from 'chai'
import { validateLength } from '../src/validateLength.js'
it('[utils] Validate length is too low', () => {
expect(validateLength('test', { min: 5 })).to.be.equal(false)
})
it('[utils] Validate length is too high', () => {
expect(validateLength('test', { max: 3 })).to.be.equal(false)
})
it('[utils] Validate length is NOT just right in between.', () => {
expect(validateLength('test', { min: 5, max: 3 })).to.be.equal(false)
})
it('[utils] Validate length is NOT too low', () => {
expect(validateLength('test', { min: 3 })).to.be.equal(true)
})
it('[utils] Validate length is NOT too high', () => {
expect(validateLength('test', { max: 5 })).to.be.equal(true)
})
it('[utils] Validate length is just right in between.', () => {
expect(validateLength('test', { min: 3, max: 6 })).to.be.equal(true)
})