"""BUG-1321: the API fallback dasha year matches dasha_analyzer's 365.25 days. Upstream 4d543209. The two 365.25636 constants in the API server made the fallback timeline and the years-into-mahadasha figure drift from the analyzer. """ from __future__ import annotations import importlib.util import os import sys from datetime import datetime, timedelta SCRIPTS = os.path.join(os.path.dirname(__file__), "..", "scripts") if SCRIPTS not in sys.path: sys.path.insert(0, SCRIPTS) from dasha_analyzer import build_dasha_timeline, lon_to_nakshatra # noqa: E402 def _api_handler(): """Reuse the existing unbound handler helper. Do not add another forgery site.""" path = os.path.join(os.path.dirname(__file__), "test_api_server_security.py") spec = importlib.util.spec_from_file_location("_api_security_helpers_dasha", path) module = importlib.util.module_from_spec(spec) spec.loader.exec_module(module) return module._handler() def test_fallback_timeline_matches_analyzer_to_the_day() -> None: birth = datetime(2000, 1, 1) nak_info, progress, _pada = lon_to_nakshatra(0.0) timeline, _elapsed, _remaining, _lord = build_dasha_timeline("2000-01-01", nak_info, progress) handler = _api_handler() years = timeline[0]["years"] periods = handler._fallback_dasha_periods(birth, "generic", {"years": years * 9}) assert periods[0]["lord"] == timeline[0]["lord"] assert periods[0]["start"] == timeline[0]["start"].strftime("%Y-%m-%d") assert periods[0]["end"] == timeline[0]["end"].strftime("%Y-%m-%d") fallback_days = ( datetime.strptime(periods[0]["end"], "%Y-%m-%d") - datetime.strptime(periods[0]["start"], "%Y-%m-%d") ).days analyzer_days = (timeline[0]["end"].date() - timeline[0]["start"].date()).days assert fallback_days - analyzer_days == 0 long_periods = handler._fallback_dasha_periods(birth, "generic", {"years": 1422}) expected = (birth + timedelta(days=158 * 365.25)).strftime("%Y-%m-%d") stale = (birth + timedelta(days=158 * 365.25636)).strftime("%Y-%m-%d") assert expected != stale assert long_periods[0]["end"] == expected def test_years_into_mahadasha_uses_the_analyzer_year() -> None: birth = datetime(2000, 1, 1) today = datetime(2003, 1, 1) handler = _api_handler() layer = handler._compute_vimshottari_analysis_layer(birth, 0.0, current_date="2003-01-01") days = (today - birth).days years_into = layer["five_levels"]["mahadasha"]["years_into_period"] assert years_into == days / 365.25 assert years_into != days / 365.25636