This commit is contained in:
“xHuPo” 2025-06-09 13:35:15 +08:00
commit 2b8870a40e
51 changed files with 5845 additions and 0 deletions

139
utils/base32.test.js Normal file
View file

@ -0,0 +1,139 @@
// Base32 Test Suite
const base32 = require('./base32.js');
// Test cases
const tests = [
// 1. Basic encoding/decoding
{
name: '基础编码/解码测试',
input: 'Hello, World!',
expected: 'JBSWY3DPFQQFO33SNRSCC==='
},
// 2. Empty string
{
name: '空字符串测试',
input: '',
expected: ''
},
// 3. Unicode characters
{
name: 'Unicode字符测试',
input: '你好,世界!',
expected: '4S4K3DZNEOZTG5TF'
},
// 4. Special characters
{
name: '特殊字符测试',
input: '!@#$%^&*()',
expected: 'IVLF4UKEJRGUU==='
},
// 5. Long string
{
name: '长字符串测试',
input: 'A'.repeat(100),
expected: 'IFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQKBIFAUCQ======'
}
];
// Custom alphabet test
const customAlphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
// Run tests
console.log('=== Base32 Test Suite ===\n');
// 1. Standard Tests
console.log('1. Standard Tests:');
tests.forEach(test => {
try {
const encoded = base32.encode(test.input);
const decoded = new TextDecoder().decode(base32.decode(encoded));
const passed = encoded === test.expected && decoded === test.input;
console.log(`${test.name}:`);
console.log(` Input: ${test.input}`);
console.log(` Encoded: ${encoded}`);
console.log(` Decoded: ${decoded}`);
console.log(` Result: ${passed ? '✓ 通过' : '✗ 失败'}\n`);
} catch (e) {
console.log(` Error: ${e.message}\n`);
}
});
// 2. Custom Alphabet Tests
console.log('2. Custom Alphabet Tests:');
try {
base32.setCustomAlphabet(customAlphabet);
const input = 'Hello';
const encoded = base32.encode(input);
const decoded = new TextDecoder().decode(base32.decode(encoded));
console.log(`Custom alphabet encoding:`);
console.log(` Input: ${input}`);
console.log(` Encoded: ${encoded}`);
console.log(` Decoded: ${decoded}`);
console.log(` Result: ${decoded === input ? '✓ 通过' : '✗ 失败'}\n`);
} catch (e) {
console.log(` Error: ${e.message}\n`);
}
// 3. Error Handling Tests
console.log('3. Error Handling Tests:');
// Invalid input type
try {
base32.encode(null);
console.log('Invalid input type test: ✗ 失败 (应该抛出错误)');
} catch (e) {
console.log(`Invalid input type test: ✓ 通过 (${e.type})`);
}
// Invalid Base32 string
try {
base32.decode('!@#$');
console.log('Invalid Base32 string test: ✗ 失败 (应该抛出错误)');
} catch (e) {
console.log(`Invalid Base32 string test: ✓ 通过 (${e.type})`);
}
// Invalid padding
try {
base32.decode('JBSWY3DP=', { strict: true });
console.log('Invalid padding test: ✗ 失败 (应该抛出错误)');
} catch (e) {
console.log(`Invalid padding test: ✓ 通过 (${e.type})`);
}
// 4. Utility Function Tests
console.log('\n4. Utility Function Tests:');
// isValid
console.log('isValid tests:');
[
'JBSWY3DP',
'JBSWY3DP======',
'invalid!',
''
].forEach(input => {
console.log(` "${input}": ${base32.isValid(input)}`);
});
// normalize
console.log('\nnormalize tests:');
[
'jbswy3dp',
'JBSWY3DP',
'JBSWY3DP======'
].forEach(input => {
try {
console.log(` "${input}" -> "${base32.normalize(input)}"`);
} catch (e) {
console.log(` "${input}" Error: ${e.message}`);
}
});
// encodedLength
console.log('\nencodedLength tests:');
[5, 10, 15].forEach(len => {
console.log(` Input length ${len}:`);
console.log(` With padding: ${base32.encodedLength(len)}`);
console.log(` Without padding: ${base32.encodedLength(len, { noPadding: true })}`);
});