import { describe, it, expect, vi } from 'vitest'
import { usePagination } from '../usePagination'

describe('usePagination', () => {
  it('starts at page 1 with default pageSize', () => {
    const { page, pageSize, total } = usePagination({ fetchFn: vi.fn() })
    expect(page.value).toBe(1)
    expect(pageSize.value).toBe(20)
    expect(total.value).toBe(0)
  })

  it('accepts custom pageSize', () => {
    const { pageSize } = usePagination({ fetchFn: vi.fn(), pageSize: 50 })
    expect(pageSize.value).toBe(50)
  })

  it('totalPages is computed correctly', () => {
    const { total, totalPages } = usePagination({ fetchFn: vi.fn(), pageSize: 10 })
    total.value = 25
    expect(totalPages.value).toBe(3)
  })

  it('hasMore is true when not on last page', () => {
    const { total, totalPages, hasMore } = usePagination({ fetchFn: vi.fn(), pageSize: 10 })
    total.value = 25
    expect(totalPages.value).toBe(3)
    expect(hasMore.value).toBe(true)
  })

  it('hasMore is false on last page', () => {
    const { total, page, hasMore } = usePagination({ fetchFn: vi.fn(), pageSize: 10 })
    total.value = 10
    page.value = 1
    expect(hasMore.value).toBe(false)
  })

  it('goToPage calls fetchFn and updates page', async () => {
    const fetchFn = vi.fn()
    const { page, goToPage } = usePagination({ fetchFn })
    await goToPage(3)
    expect(page.value).toBe(3)
    expect(fetchFn).toHaveBeenCalledWith(3)
  })

  it('totalPages is at least 1 for zero items', () => {
    const { totalPages } = usePagination({ fetchFn: vi.fn() })
    expect(totalPages.value).toBe(1)
  })
})
