// extension/src/shared/__tests__/base32.test.ts
import { describe, expect, it } from 'vitest';
import { base32Decode, base32Encode } from '../base32';

describe('base32', () => {
  // RFC 4648 § 10 test vectors
  it('encodes empty', () => expect(base32Encode(new Uint8Array())).toBe(''));
  it('encodes "f"',   () => expect(base32Encode(new TextEncoder().encode('f'))).toBe('MY'));
  it('encodes "fo"',  () => expect(base32Encode(new TextEncoder().encode('fo'))).toBe('MZXQ'));
  it('encodes "foo"', () => expect(base32Encode(new TextEncoder().encode('foo'))).toBe('MZXW6'));
  it('encodes "foob"',  () => expect(base32Encode(new TextEncoder().encode('foob'))).toBe('MZXW6YQ'));
  it('encodes "fooba"', () => expect(base32Encode(new TextEncoder().encode('fooba'))).toBe('MZXW6YTB'));
  it('encodes "foobar"',() => expect(base32Encode(new TextEncoder().encode('foobar'))).toBe('MZXW6YTBOI'));

  it('decodes round-trip', () => {
    const bytes = new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x9a]);
    expect(base32Decode(base32Encode(bytes))).toEqual(bytes);
  });

  it('decodes case-insensitively', () => {
    expect(base32Decode('mzxw6')).toEqual(new TextEncoder().encode('foo'));
  });

  it('decodes ignoring whitespace and padding', () => {
    expect(base32Decode('JBSW Y3DP EHPK 3PXP==')).toEqual(
      base32Decode('JBSWY3DPEHPK3PXP'),
    );
  });

  it('throws on invalid characters', () => {
    expect(() => base32Decode('MZ!W6')).toThrow();
  });
});
