1
0

base64.test.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import { describe, it, expect } from 'vitest';
  2. import {
  3. bytesToBase64,
  4. base64ToBytes,
  5. bytesToBase64Url,
  6. base64UrlToBytes,
  7. textToBase64,
  8. base64ToText,
  9. } from './base64';
  10. describe('base64', () => {
  11. it('round-trips raw bytes through standard base64', () => {
  12. const bytes = new Uint8Array([0, 1, 2, 250, 251, 252, 253, 254, 255]);
  13. expect(base64ToBytes(bytesToBase64(bytes))).toEqual(bytes);
  14. });
  15. it('encodes URL-safe base64 without padding or +//', () => {
  16. const bytes = new Uint8Array([251, 255, 191, 0]);
  17. const url = bytesToBase64Url(bytes);
  18. expect(url).not.toMatch(/[+/=]/);
  19. expect(base64UrlToBytes(url)).toEqual(bytes);
  20. });
  21. it('tolerates missing padding when decoding', () => {
  22. // "M" => 0x33; "TQ" decodes to one byte 0x4d.
  23. expect(base64ToBytes('TQ')).toEqual(new Uint8Array([0x4d]));
  24. });
  25. it('round-trips UTF-8 text (including non-ASCII) for vmess payloads', () => {
  26. const text = JSON.stringify({ ps: 'سرور تهران', net: 'ws' });
  27. expect(base64ToText(textToBase64(text))).toBe(text);
  28. });
  29. });