import { describe, it, expect, vi, beforeEach } from 'vitest'
import { renderHook, act } from '@testing-library/react'
import { useCountdown } from '../hooks/useCountdown'

vi.mock('../lib/api/listings', () => ({
  fetchCountdownSync: vi.fn().mockResolvedValue([
    { id: 1, time_left_mins: 30 },
    { id: 2, time_left_mins: 5 },
  ]),
}))

describe('useCountdown', () => {
  beforeEach(() => {
    vi.clearAllMocks()
  })

  it('returns null for unknown id before sync', () => {
    const { result } = renderHook(() => useCountdown())
    expect(result.current(999)).toBeNull()
  })

  it('returns mins for known id after sync', async () => {
    const { result } = renderHook(() => useCountdown())

    await act(async () => {
      // Wait for the initial fetch to complete
      await new Promise(resolve => setTimeout(resolve, 50))
    })

    const mins = result.current(1)
    expect(mins).not.toBeNull()
    expect(mins).toBeCloseTo(30, 0)
  })
})
