Files
sorsfonalak/crates/astro-engine/tests/sky.rs
T
fecoandClaude Opus 5.5 699ce2abbd
CI / test (push) Successful in 2m6s
CI / build (push) Skipped
CI / deploy (push) Skipped
Moon and retrograde calendars, "today in the sky" on the own chart
New public pages: /holdnaptar (new and full moons, quarters, eclipses and
the Moon's sign changes, month by month, with the Moon of today) and
/retrograd (the retrograde periods of Mercury to Pluto with their shadows,
and which planets are retrograde now), each with a page per year around
the current one. The own chart gets a "Ma az égen" card: the Moon's sign,
phase and natal house, the tightest transits to the chart in fixed
sentences, and the retrograde planets. No AI, nothing stored.

The events come from astro-engine (sampling and bisection, checked against
swetest and the 2026 eclipses) through horoscope-api GET /v1/sky and
/v1/sky/events; the backend serves them at /v1/public/sky… and keeps them
in memory (the sky now for five minutes, a year's events for good). Links
in the mobile menu, the footer, the sitemap and the free tools on the home
page; the desktop header is unchanged (it is full at 1280 px).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-23 17:21:36 +00:00

153 lines
7.1 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! 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<SwissEphemeris> = 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<String> = all.iter().filter(|l| l.kind == LunationKind::FullMoon).map(|l| day(l.jd_ut)).collect();
let new: Vec<String> = 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");
}