Files
Jyotisha/tests/test_personal_calendar.py
ClaudeandClaude Opus 5.5 228df0f7fb
Independent Staging Quality Gate / validate-frontend (push) Successful in 5m12s
Independent Staging Quality Gate / validate-python (push) Successful in 5m48s
Independent Staging Quality Gate / publish (push) Successful in 3m27s
feat(ephemeris): personal month calendar on 星历 (BUG-1666, BUG-1667)
The 星历 page gets a month calendar for the chosen person: every day is
吉 / 平 / 凶 with two or three life themes from transits against their own
chart, the selected day opens to its themes and "为什么", and below it
「接下来要留心」, 「这几天」 and 「回看」 (owner, 2026-11-07: A's grid plus
C's lower half).

Engine: scripts/personal_calendar.py joins existing calculators on one
date axis (one call, <0.5 s for 45 days): Panchanga in the subject's own
ayanamsa, Tara / Chandra bala, Moon house from Lagna and Moon,
Ashtakavarga SAV + Moon BAV, transit conjunctions with natal points,
functional benefic / malefic, activity verdicts with native inputs,
ingress / station / combustion / eclipse / Vimshottari AD+PD boundaries,
and a month backdrop (Vimshottari MD/AD/PD, Narayana, Sade Sati / Kantaka,
slow-graha contacts, Jupiter+Saturn double transit). New endpoint
/api/personal_calendar.

BUG-1666: check_activity_muhurta ignored Vishti karana, Vyatipata /
Vaidhriti and an avoided Chandra bala, so a Vishti day could be 大吉.
BUG-1667 (closes BUG-703 / BUG-707): /api/panchanga_range now honours the
requested ayanamsa; the day view asks for the subject's own and drops the
Lahiri note.

Frontend: /api/ephemeris/month (own rate-limit bucket, cached),
/api/ephemeris/checks + migration 20261107010000_ephemeris_day_checks
(answers kept with the birth declaration they were made under). Copy is
life-first with the sky events folded under 为什么; draft wording pending
the astrologer. TASK-ephemeris-page decisions 4/6 and hard lines 3/7/9
superseded per the owner; DESIGN.md §16 updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXozbkyFNqbu8pVEtZ7W
2026-11-07 00:44:37 +08:00

130 lines
5.8 KiB
Python
Raw Permalink 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.
"""Personal transit calendar (星历 month view) and the two engine fixes it needs.
BUG-1666: an activity verdict ignored Vishti karana, the rejected yogas and
an avoided Chandrabala. BUG-1667 (with BUG-703): the five limbs ignored the
requested ayanamsa.
"""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
ROOT = Path(__file__).resolve().parents[1]
SCRIPTS = ROOT / "scripts"
if str(SCRIPTS) not in sys.path:
sys.path.insert(0, str(SCRIPTS))
pytest.importorskip("swisseph")
import muhurta # noqa: E402
import personal_calendar as pc # noqa: E402
SAMPLE = {
"year": 1994, "month": 8, "day": 15, "hour": 9, "minute": 30,
"lat": 31.23, "lon": 121.47, "tz": 8, "ayanamsa": "raman", "node_mode": "mean",
}
@pytest.fixture(scope="module")
def november():
return pc.build_personal_calendar({**SAMPLE, "start_date": "2026-11-01", "end_date": "2026-11-30"})
def _panchanga(**overrides):
base = muhurta.panchanga_range_report("2026-11-06", "2026-11-06", lat=31.23, lon=121.47, tz=8)["days"][0]["panchanga"]
for key, value in overrides.items():
base[key] = {**base[key], **value}
return base
def test_vishti_day_is_never_excellent():
# 2026-11-06 has a good tithi, nakshatra and weekday for marriage; make the karana Vishti.
clean = muhurta.check_activity_muhurta(_panchanga(), "marriage")
assert clean["verdict"].startswith("大吉")
vishti = muhurta.check_activity_muhurta(_panchanga(karana={"karana": "Vishti", "is_vishti": True}), "marriage")
assert "vishti_karana" in vishti["blocking_doshas"]
assert not vishti["verdict"].startswith(("大吉", "吉"))
def test_rejected_yoga_and_avoided_chandrabala_count_against_the_day():
yoga = muhurta.check_activity_muhurta(_panchanga(yoga={"yoga": "Vyatipata"}), "business")
assert "rejected_yoga" in yoga["blocking_doshas"]
assert not yoga["verdict"].startswith(("大吉", "吉"))
moon_sign = int(_panchanga()["nakshatra"]["moon_lon"] // 30)
eighth_back = (moon_sign - 7) % 12 # the day's Moon is 8th from this birth Moon
chandra = muhurta.check_activity_muhurta(_panchanga(), "travel", birth_moon_sign=eighth_back)
assert "chandrabala_avoided" in chandra["blocking_doshas"]
def test_activity_verdict_codes():
assert muhurta.activity_verdict_code("大吉(Excellent)") == "excellent"
assert muhurta.activity_verdict_code("不宜(Avoid)") == "avoid"
assert muhurta.activity_verdict_code(None) == "neutral"
def test_panchanga_range_follows_requested_ayanamsa():
lahiri = muhurta.panchanga_range_report("2026-11-06", "2026-11-06", lat=31.23, lon=121.47, tz=8, ayanamsa_name="lahiri")
raman = muhurta.panchanga_range_report("2026-11-06", "2026-11-06", lat=31.23, lon=121.47, tz=8, ayanamsa_name="raman")
lahiri_moon = lahiri["days"][0]["panchanga"]["nakshatra"]["moon_lon"]
raman_moon = raman["days"][0]["panchanga"]["nakshatra"]["moon_lon"]
assert 1.0 < (raman_moon - lahiri_moon) % 360 < 2.0 # Raman's ayanamsa is ~1.45° smaller
assert "raman" in str(raman["calculation_policy"]).lower()
def test_month_has_one_row_per_day_with_every_layer(november):
days = november["days"]
assert [d["date"] for d in days][0] == "2026-11-01"
assert len(days) == 30
for day in days:
assert day["level"] in {"good", "mid", "bad"}
assert 1 <= len(day["themes"]) <= 3
assert day["themes"][0]["sources"][0]["kind"] == "moon_house"
assert set(day["panchanga"]) == {"vara", "tithi", "nakshatra", "yoga", "karana"}
assert 1 <= day["tara"]["distance"] <= 27
assert 1 <= day["chandrabala"]["distance"] <= 12
assert day["ashtakavarga"] and 0 <= day["ashtakavarga"]["moon_bav"] <= 8
assert set(day["activities"]) == set(pc.ACTIVITIES)
assert november["natal"]["functional"]["status"]
assert november["calculation"]["ayanamsa"] == "raman"
def test_chandrashtama_days_are_bad(november):
eighth = [d for d in november["days"] if d["chandrabala"]["distance"] == 8]
assert eighth, "the Moon is 8th from the birth Moon for about two days a month"
assert all(d["level"] == "bad" for d in eighth)
assert all(any(r["code"] == "chandrashtama" for r in d["reasons"]) for d in eighth)
def test_known_sky_events_of_november_2026(november):
by_date = {d["date"]: d for d in november["days"]}
stations = {(e["body"], e["motion"]) for e in by_date["2026-11-14"]["events"] if e["kind"] == "station"}
assert ("Mercury", "retrograde_to_direct") in stations
assert ("Venus", "retrograde_to_direct") in stations
# The transit Sun reaches this chart's natal Moon (Scorpio ~10°) around 25 November.
sun_on_moon = [d["date"] for d in november["days"] if any(c["transit"] == "Sun" and c["natal"] == "Moon" for c in d["contacts"])]
assert sun_on_moon and "2026-11-23" <= sun_on_moon[0] <= "2026-11-27"
def test_moon_crosses_each_natal_point_about_once(november):
crossings = [c["natal"] for d in november["days"] for c in d["contacts"] if c["transit"] == "Moon"]
for point in ("Lagna", "Sun", "Venus", "Saturn"):
assert 1 <= crossings.count(point) <= 2
def test_backdrop_layers(november):
vim = november["backdrop"]["vimshottari"]
assert vim["md"] and vim["ad"] and vim["pd"]
assert vim["pd_end"] <= vim["ad_end"] <= vim["md_end"]
assert november["backdrop"]["narayana"]["status"] in {"used", "blocked"}
assert november["backdrop"]["sade_sati"]["status"] == "used"
assert november["backdrop"]["double_transit"]["status"] in {"used", "blocked"}
def test_bad_input_is_rejected():
with pytest.raises(pc.PersonalCalendarError):
pc.build_personal_calendar({**SAMPLE, "start_date": "2026-11-01", "end_date": "2027-01-30"})
with pytest.raises(pc.PersonalCalendarError):
pc.build_personal_calendar({**SAMPLE, "start_date": "2026-11-02", "end_date": "2026-11-01"})