import { describe, it } from 'node:test'
import assert from 'node:assert/strict'
import {
  avatarUrlToVCardPhotoLine,
  gravatarUrl,
  parseVCardPhoto,
} from './contact-avatar.ts'

describe('contact-avatar', () => {
  it('builds stable Gravatar URL for normalized email', () => {
    assert.equal(
      gravatarUrl('Alice@Example.COM', 80),
      'https://www.gravatar.com/avatar/7206d8c4dd027ffeae12fb6540cbed2ba?s=80&d=404',
    )
  })

  it('parses embedded vCard PHOTO as data URI', () => {
    const photo = parseVCardPhoto('PHOTO;ENCODING=b;TYPE=PNG', 'aGVsbG8=')
    assert.equal(photo, 'data:image/png;base64,aGVsbG8=')
  })

  it('parses vCard PHOTO URI', () => {
    assert.equal(
      parseVCardPhoto('PHOTO;VALUE=URI', 'https://cdn.example/face.jpg'),
      'https://cdn.example/face.jpg',
    )
  })

  it('serializes data URI to vCard PHOTO line', () => {
    const line = avatarUrlToVCardPhotoLine(
      'data:image/jpeg;base64,/9j/abc',
      (v) => v,
    )
    assert.equal(line, 'PHOTO;ENCODING=b;TYPE=JPEG:/9j/abc')
  })

  it('skips Gravatar URLs when exporting vCard', () => {
    assert.equal(
      avatarUrlToVCardPhotoLine(
        'https://www.gravatar.com/avatar/abc?s=80&d=404',
        (v) => v,
      ),
      null,
    )
  })
})
