import { describe, expect, it } from 'vitest' import { encrypt, decrypt, isBase64, addBase64, decodeBase64 } from '../../../script/aes' describe('aes - encrypt / decrypt', () => { it('对普通字符串进行加密后以 a$ 开头、~ 结尾', () => { const result = encrypt('hello') expect(typeof result).toBe('string') expect((result as string).startsWith('a$')).toBe(true) expect((result as string).endsWith('~')).toBe(true) }) it('对已加密字符串(a$...~)不二次加密,直接返回原值', () => { const first = encrypt('hello') as string const second = encrypt(first) as string expect(second).toBe(first) }) it('空字符串不加密,直接返回空字符串', () => { expect(encrypt('')).toBe('') }) it('boolean 值原样返回,不做任何处理', () => { expect(encrypt(true)).toBe(true) expect(encrypt(false)).toBe(false) }) it('加密后能正确解密还原原始值', () => { const plaintext = 'hello world 123!@#中文' const encrypted = encrypt(plaintext) as string const decrypted = decrypt(encrypted) expect(decrypted).toBe(plaintext) }) it('对多个不同字符串加密后,结果各不相同', () => { const e1 = encrypt('abc') as string const e2 = encrypt('xyz') as string expect(e1).not.toBe(e2) }) it('decrypt 对不以 a$ 开头的字符串抛出异常', () => { expect(() => decrypt('不是加密字符串')).toThrow('不是AES加密的字符串') expect(() => decrypt('b$somethingelse~')).toThrow('不是AES加密的字符串') }) it('解密解密后的值保持幂等(decrypt 正确还原 encrypt 结果)', () => { const testCases = ['simple', '中文测试', '123456', 'UPPER_CASE', 'mix123中Eng'] for (const tc of testCases) { expect(decrypt(encrypt(tc) as string)).toBe(tc) } }) }) describe('aes - addBase64 / decodeBase64 / isBase64', () => { it('addBase64 对普通字符串添加 b$ 前缀和 ~ 后缀', () => { const result = addBase64('hello') expect(result.startsWith('b$')).toBe(true) expect(result.endsWith('~')).toBe(true) }) it('addBase64 对已编码字符串(b$...)不重复编码', () => { const first = addBase64('hello') const second = addBase64(first) expect(second).toBe(first) }) it('addBase64 对空字符串直接返回空字符串', () => { expect(addBase64('')).toBe('') }) it('addBase64 传 uri=true 时能被 decodeBase64 正确还原', () => { const encoded = addBase64('hello world', true) const decoded = decodeBase64(encoded) expect(decoded).toBe('hello world') }) it('decodeBase64 能还原 addBase64 编码的字符串', () => { const encoded = addBase64('hello') const decoded = decodeBase64(encoded) expect(decoded).toBe('hello') }) it('decodeBase64 对不以 b$ 开头的字符串原样返回', () => { expect(decodeBase64('普通字符串')).toBe('普通字符串') expect(decodeBase64('a$xxx~')).toBe('a$xxx~') }) it('decodeBase64 对空字符串和非字符串原样返回', () => { expect(decodeBase64('')).toBe('') expect(decodeBase64(null as any)).toBe(null) expect(decodeBase64(undefined as any)).toBe(undefined) }) it('decodeBase64 对不以 ~ 结尾的 b$ 字符串原样返回', () => { const noSuffix = 'b$SGVsbG8=' expect(decodeBase64(noSuffix)).toBe(noSuffix) }) it('isBase64 在字符串包含 b$ 时返回 true', () => { expect(isBase64('b$SGVsbG8=~')).toBe(true) expect(isBase64('prefixb$data~suffix')).toBe(true) }) it('isBase64 在字符串不含 b$ 时返回 false', () => { expect(isBase64('普通字符串')).toBe(false) expect(isBase64('a$encrypteddata~')).toBe(false) expect(isBase64('')).toBe(false) }) })