-- constellation.sql -- Tests for v0.14.0: IAU constellation identification
--
-- Verifies the Roman (1987) boundary lookup against well-known
-- stellar positions and solar system objects.

CREATE EXTENSION IF NOT EXISTS pg_orrery;

-- ============================================================
-- Known stars via J2000 RA/Dec overload
-- ============================================================

-- Polaris (Alpha UMi): RA 2.5303h, Dec +89.264
SELECT constellation(2.5303, 89.264) AS polaris_constellation;

-- Sirius (Alpha CMa): RA 6.7525h, Dec -16.716
SELECT constellation(6.7525, -16.716) AS sirius_constellation;

-- Betelgeuse (Alpha Ori): RA 5.9195h, Dec +7.407
SELECT constellation(5.9195, 7.407) AS betelgeuse_constellation;

-- Vega (Alpha Lyr): RA 18.6156h, Dec +38.784
SELECT constellation(18.6156, 38.784) AS vega_constellation;

-- Antares (Alpha Sco): RA 16.4901h, Dec -26.432
SELECT constellation(16.4901, -26.432) AS antares_constellation;

-- Deneb (Alpha Cyg): RA 20.6905h, Dec +45.280
SELECT constellation(20.6905, 45.280) AS deneb_constellation;

-- Rigel (Beta Ori): RA 5.2423h, Dec -8.202
SELECT constellation(5.2423, -8.202) AS rigel_constellation;

-- ============================================================
-- Celestial poles
-- ============================================================

-- South celestial pole -> Octans
SELECT constellation(0.0, -90.0) AS south_pole_constellation;

-- Near north celestial pole -> Ursa Minor
SELECT constellation(0.0, 89.0) AS north_pole_constellation;

-- ============================================================
-- Solar system objects via equatorial overload
-- ============================================================

-- Sun at 2024 summer solstice should be in Gemini (not Cancer --
-- precession has shifted the solstice point)
SELECT constellation(sun_equatorial('2024-06-21 12:00:00+00'::timestamptz))
    AS sun_solstice_constellation;

-- Jupiter in Jan 2024 should be in Aries
SELECT constellation(planet_equatorial(5, '2024-01-15 12:00:00+00'::timestamptz))
    AS jupiter_jan2024_constellation;

-- ============================================================
-- Both overloads should agree for the same position
-- ============================================================

SELECT constellation(18.6156, 38.784)
     = constellation(make_equatorial(18.6156, 38.784, 0.0))
    AS overloads_agree;

-- ============================================================
-- RA boundary edge case near 0h/24h wrap
-- ============================================================

-- RA just above 0h at various declinations
SELECT constellation(0.01, 45.0) IS NOT NULL AS ra_near_zero_valid;
SELECT constellation(23.99, -30.0) IS NOT NULL AS ra_near_24_valid;

-- ============================================================
-- Error cases
-- ============================================================

-- RA out of range
DO $$ BEGIN PERFORM constellation(24.1, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'RA=24.1: %', SQLERRM; END $$;
DO $$ BEGIN PERFORM constellation(-0.1, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'RA=-0.1: %', SQLERRM; END $$;

-- Dec out of range
DO $$ BEGIN PERFORM constellation(12.0, 91.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'Dec=91: %', SQLERRM; END $$;
