Files
discordeno/packages/utils/tests/base64.spec.ts
Fleny 919474069d chore: Migrate ESLint and prettier to Biome (#3634)
* Migrate eslint and prettier to biomejs

This does NOT include examples/bigbot as it has its own formatter

* Update to biome 1.8.0

* Readd dotenv dev dependency to rest

During a merge it got lost
2024-07-13 13:05:02 -05:00

58 lines
2.5 KiB
TypeScript

import { Buffer } from 'node:buffer'
import { expect } from 'chai'
import { describe, it } from 'mocha'
import { decode, encode } from '../src/base64.js'
describe('base64.ts', () => {
describe('encode', () => {
it('can encode string to base64', () => {
expect(encode('Man Ё𤭢')).to.be.equal('TWFuINCB8KStog==')
})
it('can encode Uint8Array to base64', () => {
expect(encode(new Uint8Array([77, 97, 110, 32, 208, 129, 240, 164, 173, 162]))).to.be.equal('TWFuINCB8KStog==')
expect(encode(new Uint8Array([77, 97, 110, 32, 208, 129, 240, 164, 173]))).to.be.equal('TWFuINCB8KSt')
expect(encode(new Uint8Array([77, 97, 110, 32, 208, 129, 240, 164, 173, 162, 63]))).to.be.equal('TWFuINCB8KStoj8=')
})
it('can encode Buffer to base64', () => {
expect(encode(Buffer.from([77, 97, 110, 32, 208, 129, 240, 164, 173, 162]))).to.be.equal('TWFuINCB8KStog==')
expect(encode(Buffer.from([77, 97, 110, 32, 208, 129, 240, 164, 173]))).to.be.equal('TWFuINCB8KSt')
expect(encode(Buffer.from([77, 97, 110, 32, 208, 129, 240, 164, 173, 162, 63]))).to.be.equal('TWFuINCB8KStoj8=')
})
})
describe('decode', () => {
it('can dencode string to Uint8Array', () => {
expect(new TextDecoder().decode(decode('TWFuINCB8KStog=='))).to.be.equal('Man Ё𤭢')
expect(new TextDecoder().decode(decode('TWFuINCB8KSt'))).to.be.equal('Man Ё\ufffd')
expect(new TextDecoder().decode(decode('TWFuINCB8KStoj8='))).to.be.equal('Man Ё𤭢?')
})
it('will throw an error with invalid string', () => {
expect(() => decode('=adw')).to.throw('Unable to parse base64 string.')
expect(() => decode('a')).to.throw('Unable to parse base64 string.')
expect(() => decode('$avs')).to.throw('Unable to parse base64 string.')
expect(() => decode('~daw')).to.throw('Unable to parse base64 string.')
})
})
/** 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: number[] = []
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)
}
})
})