import { render, screen, waitFor } from '@testing-library/react'
import { afterEach, describe, expect, it, vi } from 'vitest'
import '../i18n'
import { VeilConditions } from './VeilConditions'
const sky = {
moon_phase: 0.5,
moon_name: 'full moon',
moon_illumination: 0.98,
witching_proximity: null,
thinness: 0.98,
}
function stubFetch(body: unknown, ok = true) {
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({ ok, json: () => Promise.resolve(body) }),
)
}
describe('VeilConditions', () => {
afterEach(() => {
vi.unstubAllGlobals()
})
it('renders moon, thinness phrase and storm line from the endpoint', async () => {
stubFetch({ sky, geomagnetic: { kp: 6.3, label: 'moderate geomagnetic storm', storm: true } })
render()
expect(await screen.findByText('full moon')).toBeInTheDocument()
expect(screen.getByText('98% lit')).toBeInTheDocument()
expect(screen.getByText('the veil runs thin tonight')).toBeInTheDocument()
expect(screen.getByText('geomagnetic storm')).toBeInTheDocument()
expect(screen.getByText('Kp 6')).toBeInTheDocument()
})
it('omits the field line when geomagnetic is null', async () => {
stubFetch({ sky, geomagnetic: null })
render()
expect(await screen.findByText('full moon')).toBeInTheDocument()
expect(screen.queryByText(/field:/)).toBeNull()
expect(screen.queryByText(/Kp/)).toBeNull()
})
it('renders nothing when the fetch fails', async () => {
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new Error('down')))
const { container } = render()
await waitFor(() => expect(fetch).toHaveBeenCalled())
expect(container.firstChild).toBeNull()
})
})