-- v013_features.sql -- Tests for v0.13.0: make_equatorial constructor
--
-- Verifies that make_equatorial() produces the same result as text
-- literal casting, validates input bounds, and round-trips correctly.

-- Load the extension
CREATE EXTENSION IF NOT EXISTS pg_orrery;

-- ============================================================
-- make_equatorial() constructor
-- ============================================================

-- Basic construction and accessor round-trip
SELECT eq_ra(make_equatorial(6.75, 45.0, 1000.0)) AS ra_hours;
SELECT eq_dec(make_equatorial(6.75, 45.0, 1000.0)) AS dec_deg;
SELECT eq_distance(make_equatorial(6.75, 45.0, 1000.0)) AS dist_km;

-- Compare with text literal cast (must match)
SELECT make_equatorial(6.75, 45.0, 1000.0)::text = '(6.75000000,45.00000000,1000.000)'::equatorial::text
    AS constructor_matches_literal;

-- Edge cases: RA boundaries
SELECT make_equatorial(0.0, 0.0, 0.0) IS NOT NULL AS ra_zero;
SELECT make_equatorial(23.99999999, 0.0, 0.0) IS NOT NULL AS ra_max;

-- Edge cases: Dec boundaries
SELECT make_equatorial(12.0, -90.0, 0.0) IS NOT NULL AS dec_south_pole;
SELECT make_equatorial(12.0, 90.0, 0.0) IS NOT NULL AS dec_north_pole;

-- Edge cases: zero distance (stars)
SELECT eq_distance(make_equatorial(12.0, 45.0, 0.0)) AS zero_distance;

-- Negative distance (allowed -- could represent parallax distance in km)
SELECT eq_distance(make_equatorial(12.0, 45.0, -1000.0)) AS negative_distance;

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

-- RA out of range (must fail)
\set ON_ERROR_ROLLBACK on
DO $$ BEGIN PERFORM make_equatorial(24.0, 0.0, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'ra=24: %', SQLERRM; END $$;
DO $$ BEGIN PERFORM make_equatorial(-0.1, 0.0, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'ra=-0.1: %', SQLERRM; END $$;

-- Dec out of range (must fail)
DO $$ BEGIN PERFORM make_equatorial(12.0, 90.1, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'dec=90.1: %', SQLERRM; END $$;
DO $$ BEGIN PERFORM make_equatorial(12.0, -90.1, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'dec=-90.1: %', SQLERRM; END $$;

-- NaN and Inf (must fail)
DO $$ BEGIN PERFORM make_equatorial('NaN'::float8, 0.0, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'ra=NaN: %', SQLERRM; END $$;
DO $$ BEGIN PERFORM make_equatorial(12.0, 'Infinity'::float8, 0.0); EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'dec=Inf: %', SQLERRM; END $$;
