//! Checks the Rust bindings against reference values produced by the upstream //! `swetest` program (built from the same vendored sources), e.g. //! `swetest -b14.7.1985 -ut12:30 -p0123456789tAD -fPls -house19.0402,47.4979,P`. //! //! Requires the ephemeris files from `scripts/fetch-deps.sh` (data/ephe). use std::path::PathBuf; use std::sync::OnceLock; use astro_engine::chart::{AspectKind, PointId, Sect}; use astro_engine::swiss::SwissEphemeris; use astro_engine::{Body, ChartInput, ChartOptions, Ephemeris, Frame, GeoPosition, HouseSystem, calculate_chart}; /// 1985-07-14 12:30 UT const JD: f64 = 2_446_261.020_833_333_5; const BUDAPEST: GeoPosition = GeoPosition { latitude: 47.4979, longitude: 19.0402, elevation: 0.0 }; fn engine() -> &'static SwissEphemeris { static ENGINE: OnceLock = OnceLock::new(); ENGINE.get_or_init(|| { let dir = std::env::var_os("ASTRO_EPHE_DIR") .map(PathBuf::from) .unwrap_or_else(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../data/ephe")); SwissEphemeris::new(dir).expect("ephemeris files (run scripts/fetch-deps.sh)") }) } fn close(actual: f64, expected: f64, tol: f64) -> bool { (actual - expected).abs() <= tol } #[test] fn body_positions_match_swetest() { let expected = [ (Body::Sun, 112.0007251, 0.9541281), (Body::Moon, 71.1646551, 12.2200994), (Body::Mercury, 138.5356993, 0.9265284), (Body::Venus, 68.8767351, 1.0967944), (Body::Mars, 113.0940242, 0.6506060), (Body::Jupiter, 314.6149088, -0.1098597), (Body::Saturn, 231.5708687, -0.0181844), (Body::Uranus, 254.5844730, -0.0296575), (Body::Neptune, 271.6955376, -0.0248706), (Body::Pluto, 211.9256159, 0.0012526), (Body::TrueNode, 46.1491385, -0.0664496), (Body::MeanLilith, 34.7361561, 0.1108111), (Body::Chiron, 72.1761740, 0.0654425), ]; for (body, lon, speed) in expected { let s = engine().position(body, JD, &Frame::TROPICAL_GEOCENTRIC).unwrap(); assert!(close(s.longitude, lon, 1e-6), "{body:?} longitude {} != {lon}", s.longitude); assert!(close(s.speed_longitude, speed, 1e-6), "{body:?} speed {} != {speed}", s.speed_longitude); assert!(!s.degraded_precision, "{body:?} should come from the data files"); } let sun = engine().position(Body::Sun, JD, &Frame::TROPICAL_GEOCENTRIC).unwrap(); assert!(close(sun.distance, 1.016507864, 1e-8)); assert!(close(sun.declination, 21.6455828, 1e-6)); } #[test] fn sun_at_j2000() { // Textbook value: apparent solar longitude at 2000-01-01 12:00 UT = 280.3689° let s = engine().position(Body::Sun, 2_451_545.0, &Frame::TROPICAL_GEOCENTRIC).unwrap(); assert!(close(s.longitude, 280.3689187, 1e-6), "{}", s.longitude); } #[test] fn placidus_houses_match_swetest() { let h = engine().houses(JD, &BUDAPEST, HouseSystem::Placidus, &Frame::TROPICAL_GEOCENTRIC).unwrap(); let cusps = [ 215.9678137, 244.3181228, 278.8086642, 316.4009907, 349.5231422, 15.5954622, 35.9678137, 64.3181228, 98.8086642, 136.4009907, 169.5231422, 195.5954622, ]; for (i, (a, e)) in h.cusps.iter().zip(cusps).enumerate() { assert!(close(*a, e, 1e-6), "cusp {} {a} != {e}", i + 1); } assert_eq!(h.used, HouseSystem::Placidus); assert!(close(h.ascendant, 215.9678137, 1e-6)); assert!(close(h.midheaven, 136.4009907, 1e-6)); assert!(close(h.armc, 138.8578266, 1e-6)); assert!(close(h.vertex, 72.3891351, 1e-6)); assert!(close(h.equatorial_ascendant, 231.2864612, 1e-6)); assert!(close(h.ascendant_speed, 264.1614525, 1e-4)); } #[test] fn placidus_falls_back_to_porphyry_in_polar_regions() { let arctic = GeoPosition { latitude: 75.0, ..BUDAPEST }; let h = engine().houses(JD, &arctic, HouseSystem::Placidus, &Frame::TROPICAL_GEOCENTRIC).unwrap(); assert_eq!(h.used, HouseSystem::Porphyry); assert!(h.fallback_reason.is_some()); // swetest: house 1 = 199°49'46.9801" assert!(close(h.cusps[0], 199.0 + 49.0 / 60.0 + 46.9801 / 3600.0, 1e-6)); } #[test] fn delta_t_1985() { // swetest: Delta T 54.6303394 s assert!(close(engine().delta_t(JD) * 86_400.0, 54.6303394, 1e-4)); } #[test] fn full_chart() { let input = ChartInput { jd_ut: JD, observer: BUDAPEST, time_known: true, options: ChartOptions::default() }; let chart = calculate_chart(engine(), &input).unwrap(); assert!(chart.warnings.is_empty(), "{:?}", chart.warnings); // 10 planets + north/south node + Lilith + Chiron + 4 asteroids assert_eq!(chart.points.len(), 18); let get = |id| chart.points.iter().find(|p| p.id == id).unwrap(); let sun = get(PointId::Sun); assert_eq!(sun.position.sign, astro_engine::ZodiacSign::Cancer); assert_eq!(sun.house, Some(9)); // 112.0° lies between cusp 9 (98.8°) and 10 (136.4°) let north = get(PointId::NorthNode); let south = get(PointId::SouthNode); assert!(close(south.position.longitude, north.position.longitude + 180.0, 1e-9)); // Sun 112.0007 and Mars 113.0940: conjunction with orb ~1.09° let conj = chart .aspects .iter() .find(|a| a.point1 == PointId::Sun && a.point2 == PointId::Mars) .expect("sun-mars conjunction"); assert_eq!(conj.aspect, AspectKind::Conjunction); assert!(close(conj.orb, 1.0932991, 1e-6)); // Sun in house 9 is above the horizon -> day chart assert_eq!(chart.summary.sect, Some(Sect::Day)); let lots = chart.lots.as_ref().unwrap(); let expected_fortune = (215.9678137 + 71.1646551 - 112.0007251f64).rem_euclid(360.0); assert!(close(lots.fortune.position.longitude, expected_fortune, 1e-6)); let json = serde_json::to_value(&chart).unwrap(); assert_eq!(json["points"][0]["id"], "sun"); assert_eq!(json["houses"]["system"], "placidus"); } #[test] fn unknown_time_omits_houses() { let input = ChartInput { jd_ut: JD, observer: BUDAPEST, time_known: false, options: ChartOptions::default() }; let chart = calculate_chart(engine(), &input).unwrap(); assert!(chart.houses.is_none() && chart.angles.is_none() && chart.lots.is_none()); assert!(chart.points.iter().all(|p| p.house.is_none())); assert!(!chart.warnings.is_empty()); } #[test] fn sidereal_zodiac_subtracts_ayanamsa() { let tropical = engine().position(Body::Sun, JD, &Frame::TROPICAL_GEOCENTRIC).unwrap(); let frame = Frame { sidereal: Some(astro_engine::Ayanamsa::Lahiri), topocentric: None }; let sidereal = engine().position(Body::Sun, JD, &frame).unwrap(); let ayanamsa = engine().ayanamsa(JD, astro_engine::Ayanamsa::Lahiri).unwrap(); assert!((23.0..24.5).contains(&ayanamsa), "lahiri ayanamsa {ayanamsa}"); assert!(close(tropical.longitude - sidereal.longitude, ayanamsa, 1e-3)); } #[test] fn eclipses_of_2026() { use astro_engine::{EclipseKind, EclipseType}; // 2026-01-01 .. 2027-01-01 (UT): annular 17 Feb, total lunar 3 Mar, total solar 12 Aug, partial lunar 28 Aug. let found = engine().eclipses(2_461_041.5, 2_461_406.5).unwrap(); let list: Vec<(EclipseKind, EclipseType, String)> = found.iter().map(|e| (e.kind, e.eclipse_type, astro_engine::forecast::date_of(e.jd_ut, 0.0))).collect(); assert_eq!( list, vec![ (EclipseKind::Solar, EclipseType::Annular, "2026-02-17".into()), (EclipseKind::Lunar, EclipseType::Total, "2026-03-03".into()), (EclipseKind::Solar, EclipseType::Total, "2026-08-12".into()), (EclipseKind::Lunar, EclipseType::Partial, "2026-08-28".into()), ] ); } #[test] fn solar_return_and_forecast() { use astro_engine::chart::PointId; use astro_engine::{calculate_forecast, solar_return}; let input = ChartInput { jd_ut: JD, observer: BUDAPEST, time_known: true, options: ChartOptions::default() }; let natal = calculate_chart(engine(), &input).unwrap(); let sr = solar_return(engine(), &natal, 2026).unwrap(); // The Sun is back at 112.0007251° (Cancer 22°) on 14 July 2026. let sun = engine().position(Body::Sun, sr, &Frame::TROPICAL_GEOCENTRIC).unwrap(); assert!(close(sun.longitude, 112.0007251, 1e-6), "{}", sun.longitude); assert_eq!(astro_engine::forecast::date_of(sr, 2.0), "2026-07-14"); let next = solar_return(engine(), &natal, 2027).unwrap(); assert!((next - sr - 365.25).abs() < 0.1); let f = calculate_forecast(engine(), &natal, sr, next, 2.0).unwrap(); assert_eq!(f.age, 41); assert_eq!((f.start.as_str(), f.end.as_str()), ("2026-07-14", "2027-07-14")); // Age 41: the 6th house from a Scorpio Ascendant (Aries), lord Mars. let p = f.profection.as_ref().unwrap(); assert_eq!(p.house, 6); assert_eq!(p.sign, astro_engine::ZodiacSign::Aries); assert_eq!(p.lord, PointId::Mars); // Every transit touches the year, has a peak inside it and exact hits inside it. assert!(!f.transits.is_empty()); for t in &f.transits { assert!(t.peak.as_str() >= f.start.as_str() && t.peak.as_str() <= f.end.as_str(), "{t:?}"); assert!(t.min_orb <= 1.0 && !t.periods.is_empty()); assert!(t.exact.iter().all(|e| e.date >= f.start && e.date <= f.end)); } assert_eq!(f.transit_houses.len(), 5); assert_eq!(f.progressions.bodies.len(), 5); // The partial lunar eclipse of 28 Aug 2026 and the two in early 2027 fall in this year. assert!(f.eclipses.iter().any(|e| e.date == "2026-08-28")); serde_json::to_string(&f).unwrap(); }