import { describe, it, expect, vi } from 'vitest'
import { validateGalleryImages } from '../validate-gallery-images.js'

describe('validateGalleryImages', () => {
  it('returns null when urls is empty', async () => {
    const prisma = { galleryImage: { findMany: vi.fn() } }
    const result = await validateGalleryImages(prisma, [])
    expect(result).toBeNull()
  })

  it('throws 400 when image not found', async () => {
    const prisma = { galleryImage: { findMany: vi.fn().mockResolvedValue([]) } }
    await expect(validateGalleryImages(prisma, ['/uploads/missing.jpg'])).rejects.toMatchObject({ statusCode: 400 })
  })

  it('throws 400 when image not yet resized', async () => {
    const prisma = {
      galleryImage: { findMany: vi.fn().mockResolvedValue([{ url: '/uploads/img.jpg', isResized: false }]) },
    }
    await expect(validateGalleryImages(prisma, ['/uploads/img.jpg'])).rejects.toMatchObject({ statusCode: 400 })
  })

  it('returns existing images when all valid and resized', async () => {
    const images = [
      { url: '/uploads/img1.jpg', isResized: true },
      { url: '/uploads/img2.jpg', isResized: true },
    ]
    const prisma = { galleryImage: { findMany: vi.fn().mockResolvedValue(images) } }
    const result = await validateGalleryImages(prisma, ['/uploads/img1.jpg', '/uploads/img2.jpg'])
    expect(result).toEqual(images)
  })
})
