import { describe, it, expect, vi } from 'vitest'
import { findUserOrder } from '../find-user-order.js'

describe('findUserOrder', () => {
  it('returns order when found', async () => {
    const mockOrder = { id: '1', userId: 'user1' }
    const prisma = { order: { findFirst: vi.fn().mockResolvedValue(mockOrder) } }
    const result = await findUserOrder(prisma, '1', 'user1')
    expect(result).toEqual(mockOrder)
    expect(prisma.order.findFirst).toHaveBeenCalledWith(
      expect.objectContaining({ where: { id: '1', userId: 'user1' } }),
    )
  })

  it('throws 404 when order not found', async () => {
    const prisma = { order: { findFirst: vi.fn().mockResolvedValue(null) } }
    await expect(findUserOrder(prisma, '999', 'user1')).rejects.toMatchObject({ statusCode: 404 })
  })

  it('passes include option', async () => {
    const mockOrder = { id: '1', userId: 'user1', items: [] }
    const prisma = { order: { findFirst: vi.fn().mockResolvedValue(mockOrder) } }
    const result = await findUserOrder(prisma, '1', 'user1', { items: true })
    expect(result).toEqual(mockOrder)
    expect(prisma.order.findFirst).toHaveBeenCalledWith(expect.objectContaining({ include: { items: true } }))
  })
})
