import { describe, expect, it } from 'vitest'
import { parseImageDimensions } from './image-dimensions'

const PNG_2X3 = new Uint8Array([
  0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
  0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03,
])

const JPEG_4X5 = new Uint8Array([
  0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01,
  0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0xff, 0xc0, 0x00, 0x0b,
  0x08, 0x00, 0x05, 0x00, 0x04, 0x01, 0x01, 0x11, 0x00,
])

const WEBP_VP8X_6X7 = new Uint8Array([
  0x52, 0x49, 0x46, 0x46, 0x20, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50,
  0x56, 0x50, 0x38, 0x58, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x05, 0x00, 0x00, 0x06, 0x00, 0x00,
])

describe('@platform-modules/uploads/image-dimensions', () => {
  it('parses PNG dimensions', () => {
    expect(parseImageDimensions(PNG_2X3)).toEqual({ width: 2, height: 3 })
  })

  it('parses JPEG dimensions', () => {
    expect(parseImageDimensions(JPEG_4X5)).toEqual({ width: 4, height: 5 })
  })

  it('parses WebP VP8X dimensions', () => {
    expect(parseImageDimensions(WEBP_VP8X_6X7)).toEqual({ width: 6, height: 7 })
  })

  it('returns null for truncated headers', () => {
    expect(parseImageDimensions(PNG_2X3.subarray(0, 8))).toBeNull()
  })
})
