import { describe, test, expect } from 'vitest';
import { toAvatarDataUrl } from './avatar.js';

describe('toAvatarDataUrl', () => {
  test('encodes bytes as a base64 data URL with the given content type', () => {
    const bytes = new Uint8Array([0xff, 0xd8, 0xff, 0xe0]); // JPEG magic
    expect(toAvatarDataUrl(bytes, 'image/jpeg')).toBe('data:image/jpeg;base64,/9j/4A==');
  });

  test('strips charset/params from the content type', () => {
    const bytes = new Uint8Array([0x89, 0x50, 0x4e, 0x47]); // PNG magic
    expect(toAvatarDataUrl(bytes, 'image/png; charset=binary')).toBe('data:image/png;base64,iVBORw==');
  });

  test('defaults to image/jpeg when the content type is missing or not an image', () => {
    const bytes = new Uint8Array([0x01, 0x02]);
    expect(toAvatarDataUrl(bytes, null)).toBe('data:image/jpeg;base64,AQI=');
    expect(toAvatarDataUrl(bytes, 'text/html')).toBe('data:image/jpeg;base64,AQI=');
  });

  test('returns null for empty bytes (nothing to embed)', () => {
    expect(toAvatarDataUrl(new Uint8Array([]), 'image/jpeg')).toBeNull();
  });
});
