import {describe, it, expect} from 'vitest'
import {formatKuveti} from '../kuveti'

const kuveti = {
  url: '/uploads/original.jpg',
  formats: {
    thumbnail: {url: '/uploads/thumbnail.jpg', width: 156, height: 156},
    small: {url: '/uploads/small.jpg', width: 500, height: 500},
    medium: {url: '/uploads/medium.jpg', width: 750, height: 750},
    large: {url: '/uploads/large.jpg', width: 1000, height: 1000},
  }
}

describe('formatKuveti', () => {
  it('returns null when there is no image', () => {
    expect(formatKuveti(null)).toBeNull()
    expect(formatKuveti(undefined)).toBeNull()
  })

  it('defaults to the large format', () => {
    expect(formatKuveti(kuveti)).toBe(kuveti.formats.large)
  })

  it('prefers the thumbnail format when asked', () => {
    expect(formatKuveti(kuveti, 'thumbnail')).toBe(kuveti.formats.thumbnail)
  })

  it('falls back to small then medium then large when thumbnail is missing', () => {
    const {thumbnail, ...rest} = kuveti.formats
    const withoutThumbnail = {...kuveti, formats: rest}
    expect(formatKuveti(withoutThumbnail, 'thumbnail')).toBe(rest.small)
  })

  it('falls back to the original file when no format matches', () => {
    const noFormats = {url: '/uploads/original.jpg', formats: {}}
    expect(formatKuveti(noFormats, 'thumbnail')).toBe(noFormats)
  })
})
