Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.1 KB
Raw Blame
import { describe, expect, it, vi } from 'vitest';

import { createBasicAuthCredentials, decodeFromBase64, encodeToBase64 } from './base64';

describe('base64 utilities', () => {
  describe('encodeToBase64', () => {
    it('should encode string to base64 in browser environment', () => {
      // Mock browser environment
      global.btoa = vi
        .fn()
        .mockImplementation((input) => Buffer.from(input, 'utf8').toString('base64'));

      const result = encodeToBase64('test');

      expect(global.btoa).toHaveBeenCalledWith('test');
      expect(result).toBe('dGVzdA==');
    });

    it('should encode string to base64 in Node.js environment', () => {
      // Mock Node.js environment by removing btoa
      const originalBtoa = global.btoa;
      // @ts-ignore
      delete global.btoa;

      const result = encodeToBase64('test');

      expect(result).toBe('dGVzdA==');

      // Restore btoa
      global.btoa = originalBtoa;
    });
  });

  describe('decodeFromBase64', () => {
    it('should decode base64 string in browser environment', () => {
      // Mock browser environment
      global.atob = vi
        .fn()
        .mockImplementation((input) => Buffer.from(input, 'base64').toString('utf8'));

      const result = decodeFromBase64('dGVzdA==');

      expect(global.atob).toHaveBeenCalledWith('dGVzdA==');
      expect(result).toBe('test');
    });

    it('should decode base64 string in Node.js environment', () => {
      // Mock Node.js environment by removing atob
      const originalAtob = global.atob;
      // @ts-ignore
      delete global.atob;

      const result = decodeFromBase64('dGVzdA==');

      expect(result).toBe('test');

      // Restore atob
      global.atob = originalAtob;
    });
  });

  describe('createBasicAuthCredentials', () => {
    it('should create basic auth credentials', () => {
      const username = 'testuser';
      const password = 'testpass';

      const result = createBasicAuthCredentials(username, password);

      // Expected base64 for 'testuser:testpass' is 'dGVzdHVzZXI6dGVzdHBhc3M='
      expect(result).toBe('dGVzdHVzZXI6dGVzdHBhc3M=');
    });

    it('should handle special characters in credentials', () => {
      const username = '[email protected]';
      const password = 'p@ss:w0rd!';

      const result = createBasicAuthCredentials(username, password);
      const decoded = decodeFromBase64(result);

      expect(decoded).toBe('[email protected]:p@ss:w0rd!');
    });

    it('should handle empty credentials', () => {
      const result = createBasicAuthCredentials('', '');
      const decoded = decodeFromBase64(result);

      expect(decoded).toBe(':');
    });
  });

  describe('round-trip encoding/decoding', () => {
    it('should preserve data through encode/decode cycle', () => {
      const testStrings = [
        'simple text',
        'test@123:password',
        '中文测试',
        'user:pass',
        'special!@#$%^&*()chars',
        '',
      ];

      testStrings.forEach((input) => {
        const encoded = encodeToBase64(input);
        const decoded = decodeFromBase64(encoded);
        expect(decoded).toBe(input);
      });
    });
  });

  describe('non-ASCII in the browser environment (native btoa/atob)', () => {
    it('should round-trip UTF-8 text that native btoa would reject as raw input', () => {
      const originalBtoa = global.btoa;
      const originalAtob = global.atob;
      // Faithful to real browsers: btoa throws on any code point > U+00FF,
      // and atob returns a Latin1 binary string (not a UTF-8-decoded string).
      global.btoa = (s: string) => {
        if (/[\u{0100}-\u{10FFFF}]/u.test(s)) throw new Error('InvalidCharacterError');
        return Buffer.from(s, 'latin1').toString('base64');
      };
      global.atob = (b: string) => Buffer.from(b, 'base64').toString('latin1');

      try {
        const input = '中文测试 😀 café';
        // Before the fix this threw InvalidCharacterError on encode.
        expect(decodeFromBase64(encodeToBase64(input))).toBe(input);
      } finally {
        global.btoa = originalBtoa;
        global.atob = originalAtob;
      }
    });
  });
});