Files
sorsfonalak/crates/astro-engine/tests/swiss_reference.rs
T
fecoandClaude Opus 5.5 0358e137f1
CI / test (push) Successful in 3m17s
CI / build (push) Skipped
CI / deploy (push) Skipped
Weekly horoscope: its own page, a Monday e-mail, and one page for every e-mail setting
- The week's sky on the member's own chart (horoscope-api POST /v1/week,
  astro_engine::week): the planets' aspects to the natal points with the day
  each is exact, positions, the Moon's signs, lunar phases, stations and sign
  changes; stored once per chart and week (weekly_skies, migration 0047).
- Its text is a 'weekly' analysis written by the summary's (cheap) model when
  the member opens /heti-horoszkop: one month per page, the newest week
  first, the waiting animation while it is written. In the menu, and linked
  from the chart page.
- Monday 7:00 e-mail (worker task 'weekly'): only that it is there; on by
  default for everyone with a chart, members of today too. Its footer opens
  /ertesitesek?token=…, where the weekly e-mail and every forum e-mail can
  be set without logging in; the mail client's one-click unsubscribe turns
  off the weekly one. Also a switch on the page and on the Profile page.
- Privacy notice (HU/EN) describes it; ratings, admin labels and the model
  page know the new kind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-27 09:40:09 +00:00

254 lines
11 KiB
Rust

//! 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<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)")
})
}
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();
}
#[test]
fn one_week() {
use astro_engine::calculate_week;
let input = ChartInput { jd_ut: JD, observer: BUDAPEST, time_known: true, options: ChartOptions::default() };
let natal = calculate_chart(engine(), &input).unwrap();
// Monday 30 November 2026, 0:00 in Budapest (UTC+1), to the next Monday.
let start = 2_461_374.5 - 1.0 / 24.0;
assert_eq!(astro_engine::forecast::date_of(start, 1.0), "2026-11-30");
let w = calculate_week(engine(), &natal, start, start + 7.0, 1.0).unwrap();
assert_eq!((w.start.as_str(), w.end.as_str()), ("2026-11-30", "2026-12-06"));
assert_eq!(w.positions.len(), 9);
assert!(w.positions.iter().all(|p| p.house.is_some()));
// Every contact is within orb during the week, with its peak and exact hits inside it.
assert!(!w.contacts.is_empty());
for c in &w.contacts {
assert!(c.min_orb <= 1.0, "{c:?}");
assert!(c.peak.as_str() >= w.start.as_str() && c.peak.as_str() <= w.end.as_str(), "{c:?}");
assert!(c.exact.iter().all(|e| e.date >= w.start && e.date <= w.end), "{c:?}");
assert_ne!(c.planet, astro_engine::chart::PointId::Moon);
}
// The Moon changes sign every two or three days, each time into the next sign.
assert!((2..=4).contains(&w.moon_ingresses.len()), "{:?}", w.moon_ingresses);
let mut sign = w.moon_sign;
for i in &w.moon_ingresses {
assert_eq!(i.sign.index(), (sign.index() + 1) % 12);
sign = i.sign;
}
// About one main lunar phase a week.
assert!((1..=2).contains(&w.lunations.len()), "{:?}", w.lunations);
let json = serde_json::to_value(&w).unwrap();
assert!(json["contacts"][0]["planet"].is_string());
assert!(json["positions"][0]["sign"].is_string());
}