import { describe, it, expect } from 'bun:test';
import { toHex, fromHex, toBase64, fromBase64 } from './encoding';

describe('hex encoding', () => {
  it('round-trips bytes via hex', () => {
    const original = new Uint8Array([0, 255, 128, 64, 32, 16, 8, 4, 2, 1]);
    const hex = toHex(original);
    const decoded = fromHex(hex);
    expect(decoded).toEqual(original);
  });

  it('handles empty buffer', () => {
    const empty = new Uint8Array(0);
    expect(toHex(empty)).toBe('');
    expect(fromHex('')).toEqual(empty);
  });

  it('handles single byte', () => {
    expect(toHex(new Uint8Array([0]))).toBe('00');
    expect(toHex(new Uint8Array([255]))).toBe('ff');
  });
});

describe('base64 encoding', () => {
  it('round-trips bytes via base64', () => {
    const original = new Uint8Array([0, 255, 128, 64, 32, 16, 8, 4, 2, 1]);
    const b64 = toBase64(original);
    const decoded = fromBase64(b64);
    expect(decoded).toEqual(original);
  });

  it('handles empty buffer', () => {
    const empty = new Uint8Array(0);
    expect(toBase64(empty)).toBe('');
    expect(fromBase64('')).toEqual(empty);
  });

  it('produces URL-safe output (no + or /)', () => {
    // Some implementations produce + and /, verify we don't
    // or if we do, verify round-trip still works
    const data = new Uint8Array([255, 255, 255]);
    const b64 = toBase64(data);
    expect(fromBase64(b64)).toEqual(data);
  });
});
