From b21ee66d589766573a57462d4f97f252d35f1e4e Mon Sep 17 00:00:00 2001 From: H01001000 Date: Sun, 4 Dec 2022 21:29:58 +0800 Subject: [PATCH] test: add test --- packages/bot/tests/empty.spec.ts | 1 - packages/bot/tests/utils.test.ts | 80 ++++++++++ packages/bot/tests/utils/utils.spec.ts | 27 ++++ .../oldTest/webhook/deleteWebhook.test.ts | 10 +- packages/gateway/package.json | 2 +- packages/utils/tests/base64.spec.ts | 24 +++ packages/utils/tests/bigint.spec.ts | 20 +++ packages/utils/tests/collection.spec.ts | 145 ++++++++++++++++++ packages/utils/tests/hash.spec.ts | 24 +++ packages/utils/tests/token.spec.ts | 14 ++ packages/utils/tests/validateLength.spec.ts | 26 ++++ 11 files changed, 368 insertions(+), 5 deletions(-) delete mode 100644 packages/bot/tests/empty.spec.ts create mode 100644 packages/bot/tests/utils.test.ts create mode 100644 packages/bot/tests/utils/utils.spec.ts create mode 100644 packages/utils/tests/bigint.spec.ts create mode 100644 packages/utils/tests/collection.spec.ts create mode 100644 packages/utils/tests/hash.spec.ts create mode 100644 packages/utils/tests/token.spec.ts create mode 100644 packages/utils/tests/validateLength.spec.ts diff --git a/packages/bot/tests/empty.spec.ts b/packages/bot/tests/empty.spec.ts deleted file mode 100644 index f0a766d36..000000000 --- a/packages/bot/tests/empty.spec.ts +++ /dev/null @@ -1 +0,0 @@ -export { } diff --git a/packages/bot/tests/utils.test.ts b/packages/bot/tests/utils.test.ts new file mode 100644 index 000000000..d9657a830 --- /dev/null +++ b/packages/bot/tests/utils.test.ts @@ -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' + ) + } +}) + +*/ diff --git a/packages/bot/tests/utils/utils.spec.ts b/packages/bot/tests/utils/utils.spec.ts new file mode 100644 index 000000000..193c4c19e --- /dev/null +++ b/packages/bot/tests/utils/utils.spec.ts @@ -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' + ) +}) diff --git a/packages/discordeno/oldTest/webhook/deleteWebhook.test.ts b/packages/discordeno/oldTest/webhook/deleteWebhook.test.ts index 24c016718..85ce8ab12 100644 --- a/packages/discordeno/oldTest/webhook/deleteWebhook.test.ts +++ b/packages/discordeno/oldTest/webhook/deleteWebhook.test.ts @@ -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) diff --git a/packages/gateway/package.json b/packages/gateway/package.json index e79856a2e..a9738c0c8 100644 --- a/packages/gateway/package.json +++ b/packages/gateway/package.json @@ -42,4 +42,4 @@ "dependencies": { "ws": "^8.11.0" } -} \ No newline at end of file +} diff --git a/packages/utils/tests/base64.spec.ts b/packages/utils/tests/base64.spec.ts index 002cf652e..5f7a3592f 100644 --- a/packages/utils/tests/base64.spec.ts +++ b/packages/utils/tests/base64.spec.ts @@ -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) + } +}) diff --git a/packages/utils/tests/bigint.spec.ts b/packages/utils/tests/bigint.spec.ts new file mode 100644 index 000000000..e631f15b5 --- /dev/null +++ b/packages/utils/tests/bigint.spec.ts @@ -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) +}) diff --git a/packages/utils/tests/collection.spec.ts b/packages/utils/tests/collection.spec.ts new file mode 100644 index 000000000..374554776 --- /dev/null +++ b/packages/utils/tests/collection.spec.ts @@ -0,0 +1,145 @@ +import { expect } from 'chai' +import sinon from 'sinon' +import { Collection } from '../src/collection.js' + +describe('[collection]', () => { + let collection: Collection + + 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() + }) +}) diff --git a/packages/utils/tests/hash.spec.ts b/packages/utils/tests/hash.spec.ts new file mode 100644 index 000000000..87e8c6330 --- /dev/null +++ b/packages/utils/tests/hash.spec.ts @@ -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) +}) diff --git a/packages/utils/tests/token.spec.ts b/packages/utils/tests/token.spec.ts new file mode 100644 index 000000000..0e4a075b1 --- /dev/null +++ b/packages/utils/tests/token.spec.ts @@ -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' + ) +}) diff --git a/packages/utils/tests/validateLength.spec.ts b/packages/utils/tests/validateLength.spec.ts new file mode 100644 index 000000000..f7eb0ad72 --- /dev/null +++ b/packages/utils/tests/validateLength.spec.ts @@ -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) +})