|
| 1 | +import { expect, it, describe } from 'bun:test' |
| 2 | +import { base, ALPHABETS, base64, base58, base32, base16, base2 } from '../src' |
| 3 | + |
| 4 | +describe('ts-base-x', () => { |
| 5 | + describe('base function configuration', () => { |
| 6 | + it('should create a base converter with default BASE58 alphabet', () => { |
| 7 | + const converter = base() |
| 8 | + const input = new Uint8Array([72, 101, 108, 108, 111]) // "Hello" |
| 9 | + const encoded = converter.encode(input) |
| 10 | + const decoded = converter.decode(encoded) |
| 11 | + expect(decoded).toEqual(input) |
| 12 | + }) |
| 13 | + |
| 14 | + it('should throw error for alphabet too long', () => { |
| 15 | + const longAlphabet = 'x'.repeat(255) |
| 16 | + expect(() => base(longAlphabet)).toThrow('Alphabet too long') |
| 17 | + }) |
| 18 | + |
| 19 | + it('should throw error for ambiguous alphabet', () => { |
| 20 | + expect(() => base('00123')).toThrow('0 is ambiguous') |
| 21 | + }) |
| 22 | + }) |
| 23 | + |
| 24 | + describe('encoding', () => { |
| 25 | + it('should encode Uint8Array input', () => { |
| 26 | + const input = new Uint8Array([1, 2, 3]) |
| 27 | + const encoded = base64.encode(input) |
| 28 | + expect(typeof encoded).toBe('string') |
| 29 | + expect(encoded).toBe('QID') |
| 30 | + }) |
| 31 | + |
| 32 | + it('should handle empty input', () => { |
| 33 | + const input = new Uint8Array(0) |
| 34 | + const encoded = base64.encode(input) |
| 35 | + expect(encoded).toBe('') |
| 36 | + }) |
| 37 | + |
| 38 | + it('should handle leading zeros', () => { |
| 39 | + const input = new Uint8Array([0, 0, 1]) |
| 40 | + const encoded = base58.encode(input) |
| 41 | + expect(encoded.startsWith('11')).toBe(true) |
| 42 | + }) |
| 43 | + }) |
| 44 | + |
| 45 | + describe('decoding', () => { |
| 46 | + it('should decode valid base64 string', () => { |
| 47 | + const input = 'QID' |
| 48 | + const decoded = base64.decode(input) |
| 49 | + expect(decoded).toEqual(new Uint8Array([1, 2, 3])) |
| 50 | + }) |
| 51 | + |
| 52 | + it('should handle empty string', () => { |
| 53 | + const input = '' |
| 54 | + const decoded = base64.decode(input) |
| 55 | + expect(decoded).toEqual(new Uint8Array(0)) |
| 56 | + }) |
| 57 | + |
| 58 | + it('should throw error for invalid characters', () => { |
| 59 | + expect(() => base58.decode('invalid!')).toThrow('Non-base58 character') |
| 60 | + }) |
| 61 | + }) |
| 62 | + |
| 63 | + describe('different bases', () => { |
| 64 | + it('should work with base16 (hex)', () => { |
| 65 | + const input = new Uint8Array([255, 0, 128]) |
| 66 | + const encoded = base16.encode(input) |
| 67 | + expect(encoded).toBe('ff0080') |
| 68 | + expect(base16.decode(encoded)).toEqual(input) |
| 69 | + }) |
| 70 | + |
| 71 | + it('should work with base2 (binary)', () => { |
| 72 | + const input = new Uint8Array([5]) // 00000101 in binary |
| 73 | + const encoded = base2.encode(input) |
| 74 | + expect(encoded).toBe('101') |
| 75 | + expect(base2.decode(encoded)).toEqual(input) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should work with base32', () => { |
| 79 | + const input = new Uint8Array([72, 101, 108, 108, 111]) // "Hello" |
| 80 | + const encoded = base32.encode(input) |
| 81 | + const decoded = base32.decode(encoded) |
| 82 | + expect(decoded).toEqual(input) |
| 83 | + }) |
| 84 | + }) |
| 85 | + |
| 86 | + describe('error handling', () => { |
| 87 | + it('should throw for invalid input types in encode', () => { |
| 88 | + expect(() => base64.encode('not a Uint8Array' as any)).toThrow('Expected Uint8Array') |
| 89 | + }) |
| 90 | + |
| 91 | + it('should throw for invalid input types in decode', () => { |
| 92 | + expect(() => base64.decode(123 as any)).toThrow('Expected String') |
| 93 | + }) |
| 94 | + |
| 95 | + it('should throw for invalid characters', () => { |
| 96 | + // The implementation uses 'Non-base58 character' for all bases |
| 97 | + expect(() => base58.decode('invalid!')).toThrow('Non-base58 character') |
| 98 | + expect(() => base16.decode('0123456789abcdefg')).toThrow('Non-base58 character') |
| 99 | + expect(() => base2.decode('1012')).toThrow('Non-base58 character') |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('roundtrip tests', () => { |
| 104 | + const testCases = [ |
| 105 | + new Uint8Array([]), |
| 106 | + new Uint8Array([0]), |
| 107 | + new Uint8Array([1, 2, 3, 4, 5]), |
| 108 | + new Uint8Array([0, 0, 0, 0, 1]), |
| 109 | + new Uint8Array([255, 255, 255]), |
| 110 | + new Uint8Array(Array(100).fill(65)), // Longer input |
| 111 | + ] |
| 112 | + |
| 113 | + for (const testCase of testCases) { |
| 114 | + it(`should correctly roundtrip ${testCase.length} bytes`, () => { |
| 115 | + const encoded = base64.encode(testCase) |
| 116 | + const decoded = base64.decode(encoded) |
| 117 | + expect(decoded).toEqual(testCase) |
| 118 | + }) |
| 119 | + } |
| 120 | + }) |
| 121 | +}) |
0 commit comments