//! The sky calendar against known 2026 events (stations checked with the //! upstream `swetest`, eclipses from the published eclipse tables). //! //! Requires the ephemeris files from `scripts/fetch-deps.sh` (data/ephe). use std::path::PathBuf; use std::sync::OnceLock; use astro_engine::sky::{LunationKind, lunations, moon_ingresses, next_station, retrograde_periods, stations}; use astro_engine::swiss::SwissEphemeris; use astro_engine::time::gregorian_from_jdn; use astro_engine::{Body, Ephemeris, Frame, ZodiacSign}; 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)") }) } /// 0h UT of a calendar day. fn jd(y: i32, m: i32, d: i32) -> f64 { // Fliegel–Van Flandern, valid for Gregorian dates. let a = (14 - m) / 12; let (y2, m2) = (y + 4800 - a, m + 12 * a - 3); let jdn = d + (153 * m2 + 2) / 5 + 365 * y2 + y2 / 4 - y2 / 100 + y2 / 400 - 32045; f64::from(jdn) - 0.5 } /// `YYYY-MM-DD` of a moment, UT. fn day(jd_ut: f64) -> String { let (y, m, d) = gregorian_from_jdn((jd_ut + 0.5).floor() as i64); format!("{y:04}-{m:02}-{d:02}") } #[test] fn lunations_of_2026_include_the_eclipses() { let all = lunations(engine(), jd(2026, 1, 1), jd(2027, 1, 1)).unwrap(); // About 12.4 lunations a year, four phases each. assert!((48..=51).contains(&all.len()), "{}", all.len()); // The phases follow each other in order, a week or so apart. for w in all.windows(2) { let next = |k| match k { LunationKind::NewMoon => LunationKind::FirstQuarter, LunationKind::FirstQuarter => LunationKind::FullMoon, LunationKind::FullMoon => LunationKind::LastQuarter, LunationKind::LastQuarter => LunationKind::NewMoon, }; assert_eq!(w[1].kind, next(w[0].kind)); let gap = w[1].jd_ut - w[0].jd_ut; assert!((6.0..9.0).contains(&gap), "{gap}"); } let full: Vec = all.iter().filter(|l| l.kind == LunationKind::FullMoon).map(|l| day(l.jd_ut)).collect(); let new: Vec = all.iter().filter(|l| l.kind == LunationKind::NewMoon).map(|l| day(l.jd_ut)).collect(); // Total lunar eclipse on 3 March, total solar eclipse on 12 August, and // the "blue moon" (second full moon of May) on 31 May. assert!(full.contains(&"2026-03-03".to_string()), "{full:?}"); assert!(full.contains(&"2026-05-31".to_string()), "{full:?}"); assert!(new.contains(&"2026-08-12".to_string()), "{new:?}"); // At the exact moment the Moon and the Sun are in line. let e = engine(); for l in &all { let sun = e.position(Body::Sun, l.jd_ut, &Frame::TROPICAL_GEOCENTRIC).unwrap().longitude; let target = match l.kind { LunationKind::NewMoon => 0.0, LunationKind::FirstQuarter => 90.0, LunationKind::FullMoon => 180.0, LunationKind::LastQuarter => 270.0, }; let off = ((l.moon_longitude - sun - target) + 540.0).rem_euclid(360.0) - 180.0; assert!(off.abs() < 1e-4, "{off}"); } // The eclipses fall on these lunations. for ecl in e.eclipses(jd(2026, 1, 1), jd(2027, 1, 1)).unwrap() { assert!(all.iter().any(|l| (l.jd_ut - ecl.jd_ut).abs() < 0.5), "{ecl:?}"); } } #[test] fn the_moon_changes_sign_every_two_or_three_days() { let all = moon_ingresses(engine(), jd(2026, 3, 1), jd(2026, 4, 1)).unwrap(); assert!((13..=15).contains(&all.len()), "{}", all.len()); for w in all.windows(2) { assert_eq!(w[1].1.index(), (w[0].1.index() + 1) % 12); let gap = w[1].0 - w[0].0; assert!((1.9..3.0).contains(&gap), "{gap}"); } let e = engine(); for &(t, sign) in &all { let lon = e.position(Body::Moon, t, &Frame::TROPICAL_GEOCENTRIC).unwrap().longitude; assert_eq!(ZodiacSign::from_longitude(lon + 1e-4), sign); assert!(((lon - 30.0 * sign.index() as f64 + 540.0).rem_euclid(360.0) - 180.0).abs() < 1e-4); } } #[test] fn mercury_turns_retrograde_three_times_in_2026() { let periods = retrograde_periods(engine(), Body::Mercury, jd(2026, 1, 1), jd(2027, 1, 1)).unwrap(); let days: Vec<(String, String)> = periods.iter().map(|p| (day(p.station_retrograde.jd_ut), day(p.station_direct.jd_ut))).collect(); assert_eq!( days, [ ("2026-02-26".into(), "2026-03-20".into()), ("2026-06-29".into(), "2026-07-23".into()), ("2026-10-24".into(), "2026-11-13".into()), ] ); for p in &periods { // Retrograde between the stations, direct before and after. let e = engine(); let v = |t| e.position(Body::Mercury, t, &Frame::TROPICAL_GEOCENTRIC).unwrap().speed_longitude; let mid = (p.station_retrograde.jd_ut + p.station_direct.jd_ut) / 2.0; assert!(v(mid) < 0.0 && v(p.station_retrograde.jd_ut - 1.0) > 0.0 && v(p.station_direct.jd_ut + 1.0) > 0.0); // The shadow: about two weeks on each side, at the stations' degrees. let before = p.station_retrograde.jd_ut - p.shadow_start; let after = p.shadow_end - p.station_direct.jd_ut; assert!((8.0..25.0).contains(&before) && (8.0..25.0).contains(&after), "{before} {after}"); let lon = |t| e.position(Body::Mercury, t, &Frame::TROPICAL_GEOCENTRIC).unwrap().longitude; assert!((lon(p.shadow_start) - p.station_direct.longitude).abs() < 1e-4); assert!((lon(p.shadow_end) - p.station_retrograde.longitude).abs() < 1e-4); } } #[test] fn slow_planets_and_overlapping_periods() { // Every planet turns retrograde at most once or so in a year; Saturn and // the outer planets once, with a period of four to six months. for body in [Body::Saturn, Body::Uranus, Body::Neptune, Body::Pluto] { let periods = retrograde_periods(engine(), body, jd(2026, 1, 1), jd(2027, 1, 1)).unwrap(); assert!(!periods.is_empty(), "{body:?}"); for p in &periods { let len = p.station_direct.jd_ut - p.station_retrograde.jd_ut; assert!((120.0..170.0).contains(&len), "{body:?} {len}"); assert!(p.shadow_start < p.station_retrograde.jd_ut && p.shadow_end > p.station_direct.jd_ut); } } // A period that began before the window still counts. let mercury = retrograde_periods(engine(), Body::Mercury, jd(2026, 3, 10), jd(2026, 3, 11)).unwrap(); assert_eq!(mercury.len(), 1); assert_eq!(day(mercury[0].station_retrograde.jd_ut), "2026-02-26"); // Stations alternate, and the next one is found from any moment. let all = stations(engine(), Body::Mercury, jd(2026, 1, 1), jd(2027, 1, 1)).unwrap(); assert_eq!(all.len(), 6); assert!(all.windows(2).all(|w| w[0].retrograde != w[1].retrograde)); let next = next_station(engine(), Body::Mercury, jd(2026, 3, 1), 200.0).unwrap().unwrap(); assert!(!next.retrograde); assert_eq!(day(next.jd_ut), "2026-03-20"); }