Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp2U3BH1SxwAGyhhbe1
247 lines
15 KiB
Python
247 lines
15 KiB
Python
"""BUG-2105: fixes found while building the astrologer return package (2026-11-20).
|
||
|
||
Earlier fixes were checked only on synthetic rows and missed the real engine data shape, so every
|
||
test here also asserts on a rendered report of the repository's public fixture charts (both
|
||
editions; rendered once per process by tests.public_chart_reports). Other charts are synthetic.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import copy
|
||
import re
|
||
|
||
import pytest
|
||
|
||
from scripts.pl9_interpretation_packs import build_yoga_interpretation_pack
|
||
from scripts.report_render import parity_chapters as pc
|
||
from tests.public_chart_reports import PUBLIC_REPORT_CASES, public_chart_reports
|
||
|
||
JOBS, OBAMA = "steve_jobs_1955_aa", "barack_obama_1961_aa"
|
||
|
||
|
||
def _cells(line: str) -> list[str]:
|
||
return [cell.strip() for cell in line.strip().strip("|").split("|")]
|
||
|
||
|
||
def _tables(markdown: str):
|
||
lines = markdown.splitlines()
|
||
for index in range(len(lines) - 1):
|
||
if lines[index].startswith("|") and re.match(r"^\|\s*:?-{3,}", lines[index + 1]):
|
||
body = []
|
||
for line in lines[index + 2:]:
|
||
if not line.startswith("|"):
|
||
break
|
||
body.append(_cells(line))
|
||
yield _cells(lines[index]), body
|
||
|
||
|
||
def _section_pages(markdown: str, heading: str) -> list[str]:
|
||
start = markdown.index(heading + "\n") + len(heading)
|
||
rest = markdown[start:]
|
||
end = re.search(r"^#{1,3} ", rest, re.M)
|
||
return re.findall(r"^#### (.*)$", rest[:end.start()] if end else rest, re.M)
|
||
|
||
|
||
# 1. Argala equal count ---------------------------------------------------------------------------
|
||
|
||
def _unresolved_argala_sources(packet: dict) -> int:
|
||
houses = packet["worksheets"]["advanced_systems"]["argala"]["houses"]
|
||
count = 0
|
||
for item in houses.values():
|
||
for group in ("primary", "secondary"):
|
||
for entry in (item.get(group) or {}).values():
|
||
if entry.get("role") == "virodhargala" or not (entry.get("planets") or entry.get("blockers")):
|
||
continue
|
||
if entry.get("comparison_status") == "strength_comparison_required":
|
||
assert "effective" not in entry # the real shape: no `effective` key at all
|
||
count += 1
|
||
return count
|
||
|
||
|
||
@pytest.mark.parametrize("case", PUBLIC_REPORT_CASES)
|
||
def test_argala_equal_count_without_effective_key_is_undecided(case):
|
||
packet, chinese, english = public_chart_reports(case)
|
||
expected = _unresolved_argala_sources(packet)
|
||
assert expected == {JOBS: 17, OBAMA: 4}[case]
|
||
zh = [row[-1] for header, body in _tables(chinese) if header[0] == "参照宫" and header[-1] == "成立" for row in body]
|
||
en = [row[-1] for header, body in _tables(english) if header[0] == "Reference house" and header[-1] == "Effective" for row in body]
|
||
assert zh.count("同数未决") == expected and en.count("Undecided (equal count)") == expected
|
||
assert len(zh) == len(en) and "未判定" not in zh
|
||
# A row with no verdict and no other evidence is "not determined", never "no".
|
||
bare = {"house_from_reference": 2, "planets": ["Sun"], "blockers": []}
|
||
assert pc._argala_effective(bare, "zh") == "未判定" and pc._argala_effective(bare, "en") == "Not determined"
|
||
assert pc._argala_effective({**bare, "comparison_status": "obstructed"}, "zh") == "否"
|
||
assert pc._argala_effective({**bare, "strength_comparison": {"status": "unresolved"}}, "en") == "Undecided (equal count)"
|
||
|
||
|
||
# 2. Neecha Bhanga keeps the original dignity --------------------------------------------------------
|
||
|
||
_DIGNITY_HEADERS_ZH = {"尊贵状态", "宫主 尊贵状态", "宫主尊贵", "状态", "木星"}
|
||
_DIGNITY_HEADERS_EN = {"Dignity", "Lord dignity", "Status", "Jupiter"}
|
||
|
||
|
||
def _dignity_cells(markdown: str, headers: set[str]) -> list[str]:
|
||
cells = []
|
||
for header, body in _tables(markdown):
|
||
for index, name in enumerate(header):
|
||
if name in headers:
|
||
cells.extend(row[index] for row in body if index < len(row))
|
||
return cells
|
||
|
||
|
||
def test_neecha_bhanga_dignity_cells_keep_the_debilitation():
|
||
_packet, chinese, english = public_chart_reports(OBAMA)
|
||
zh, en = _dignity_cells(chinese, _DIGNITY_HEADERS_ZH), _dignity_cells(english, _DIGNITY_HEADERS_EN)
|
||
assert "落陷取消" not in zh and "Neecha Bhanga" not in en
|
||
# Planet table, both house-lord tables (2 rows each), raw positions, Shodashvarga matrix.
|
||
# 原值:七格都印 BUG-2030 长句;新值:短句「落陷;另有缓解条件¹」,长句只在格局页写一次
|
||
# 原因:BUG-2155(占星师 2026-11-22 §4)
|
||
# 原值:「落陷;另有缓解条件¹」;新值:「落陷;缓解条件待核」(格局表这一行是「检测器检出,待核实」)
|
||
# 原因:BUG-2165(占星师 2026-11-22 §4:已核只在所选来源条件核实后)
|
||
# 原值:「落陷;缓解条件待核」×7;新值:「落陷;有缓解」×7;原因:BUG-2280(负责人 11-23,有缓解就写有缓解)
|
||
assert zh.count("落陷;有缓解") == 7
|
||
assert en.count("Debilitated; mitigated") == 7
|
||
assert "缓解条件待核" not in chinese and "to be checked" not in english
|
||
assert "| 木星 | 土星 | 太阳 | 金星 | 木星 | 落陷;有缓解 | 0.96 |" in chinese
|
||
assert "| Jupiter | Saturn | Sun | Venus | Jupiter | Debilitated; mitigated | 0.96 |" in english
|
||
_packet, jobs_zh, _jobs_en = public_chart_reports(JOBS)
|
||
assert "有缓解" not in jobs_zh and "缓解条件待核" not in jobs_zh # no cancellation on this chart
|
||
|
||
|
||
# 3. Yoga pages keyed by rule ----------------------------------------------------------------------
|
||
|
||
def test_yoga_pages_are_keyed_by_rule_in_both_editions():
|
||
_packet, chinese, english = public_chart_reports(JOBS)
|
||
zh, en = _section_pages(chinese, "### 瑜伽解释"), _section_pages(english, "### Yoga interpretation")
|
||
# 原值:54 页;新值:53 页;原因:BUG-2154,kalatra_dosha_mars_7 归档为别名,不再单独一页
|
||
# BUG-2222 原值 53;新值 54:天界花树(Parijatha)按占星师上游的 Raman 47 定义(D1 或 D9 定位链末端任一分支)在这张盘成立
|
||
# BUG-2295 原值 54;新值 57:经典王者格、财富瑜伽的行在计算处记下参与星(占星师上游 f41efc51,负责人 2026-11-24 同意),
|
||
# 同一规则不同参与星分页(「王者格(水星)」「王者格(木星、金星)」),各页自己判断与当前大运的关系。
|
||
assert len(zh) == len(en) == 57
|
||
assert len(set(en)) == len(en) and len(set(zh)) == len(zh)
|
||
for title in ("Amala Yoga (Benefics in the 10th/11th houses)", "Amala Yoga (Only benefics in the 10th house)",
|
||
"Durmukha Yoga (Malefic in the 2nd house)",
|
||
# 原值: "Mangal Dosha (Mars in 1/4/7/8/12) (Mars in house 8)" 与 "Mangal Dosha (Mars in 1/4/7/8/12)" 两页
|
||
# 新值: 规范规则一页,英文名改为规范名
|
||
# 原因: BUG-2154(占星师 2026-11-22 §3):同一条件的两条规则只留规范入口
|
||
"Mars Marriage-Related Placement (1/4/7/8/12 House Screen)"):
|
||
assert title in en
|
||
assert not any("rule from" in title for title in en)
|
||
assert "Combination: ." not in english and "Combination: ;" not in english
|
||
_packet, obama_zh, obama_en = public_chart_reports(OBAMA)
|
||
assert len(_section_pages(obama_zh, "### 瑜伽解释")) == len(_section_pages(obama_en, "### Yoga interpretation"))
|
||
# Synthetic: two rules sharing a name stay two pages; one rule on two planets stays two pages.
|
||
rows = [
|
||
{"rule_id": "rule_a", "name": "Same Yoga", "combination_en": "Condition A"},
|
||
{"rule_id": "rule_b", "name": "Same Yoga", "combination_en": "Condition B"},
|
||
{"rule_id": "rule_c", "name": "Per Planet", "planets": ["Mars"], "combination_en": "Mars"},
|
||
{"rule_id": "rule_c", "name": "Per Planet", "planets": ["Venus"], "combination_en": "Venus"},
|
||
]
|
||
pages = [page["name"] for page in build_yoga_interpretation_pack({"detected_yogas": rows}, language="en")["pages"]]
|
||
assert pages == ["Same Yoga (Condition A)", "Same Yoga (Condition B)", "Per Planet (Mars)", "Per Planet (Venus)"]
|
||
|
||
|
||
# 4. Candidate yoga table matches engine rows by rule_id --------------------------------------------
|
||
|
||
def test_candidate_table_rows_keep_their_own_rule_names():
|
||
packet, chinese, english = public_chart_reports(JOBS)
|
||
layer_ids = [row.get("rule_id") for row in packet["report_yoga_conditions"]["rows"]]
|
||
# 原值:kalatra_dosha_mars_7 在层里;新值:规范规则在、别名不在;原因:BUG-2154
|
||
assert "amala_yoga" in layer_ids and "kalatra_dosha_mars_1_4_7_8_12" in layer_ids and "kalatra_dosha_mars_7" not in layer_ids
|
||
zh_rows = [row for header, body in _tables(chinese) if header[:2] == ["瑜伽", "状态"] for row in body]
|
||
en_rows = [row for header, body in _tables(english) if header[:2] == ["Yoga candidate", "Status"] for row in body]
|
||
zh_names = {row[0]: row for row in zh_rows}
|
||
assert "无瑕格局" in zh_names and "纯净瑜伽" not in zh_names
|
||
assert zh_names["无瑕格局"][4] == "吉星在10/11宫"
|
||
# 原值:「火星障碍(火星在关键宫)」在表里;新值:规范中文名一行;原因:BUG-2154
|
||
assert "火星婚姻相关配置(1/4/7/8/12宫筛查)" in zh_names and "火星障碍(火星在关键宫)" not in zh_names
|
||
amala_en = [row for row in en_rows if row[0] == "Amala Yoga"]
|
||
assert amala_en and all(row[4] == "Benefics in the 10th/11th houses" for row in amala_en)
|
||
# BUG-2089 same-fact merge still joins the two names.
|
||
_packet, obama_zh, obama_en = public_chart_reports(OBAMA)
|
||
assert "| 太阳瑜伽(太阳入角宫)/太阳瑜伽(太阳入7宫) |" in obama_zh
|
||
assert any(" / " in row[0] for header, body in _tables(obama_en) if header[:2] == ["Yoga candidate", "Status"] for row in body)
|
||
|
||
|
||
# 5. Narayana direction readings stored in the packet ------------------------------------------------
|
||
|
||
@pytest.mark.parametrize("case", PUBLIC_REPORT_CASES)
|
||
def test_narayana_direction_readings_are_stored_and_rendered_from_the_packet(case):
|
||
packet, chinese, english = public_chart_reports(case)
|
||
stored = packet["worksheets"]["timing_and_predictive_systems"]["narayana_dasha_report_extras"]
|
||
assert set(stored) == {"seed_comparison", "ad_direction_readings"}
|
||
assert stored == pc._narayana_extras(packet, pc._narayana_module(packet))
|
||
readings = stored["ad_direction_readings"]
|
||
assert [row["default"] for row in readings] == [True, False]
|
||
assert {"profile", "status", "md", "ad", "start_sign", "direction"} <= set(readings[0])
|
||
assert "narayana_dasha_report_extras" not in packet["worksheets"]["timing_and_predictive_systems"]["narayana_dasha"]
|
||
# The chapter reads the stored value; a packet without it computes the same text.
|
||
without = copy.deepcopy(packet)
|
||
del without["worksheets"]["timing_and_predictive_systems"]["narayana_dasha_report_extras"]
|
||
for language in ("zh", "en"):
|
||
assert pc._narayana(packet, language) == pc._narayana(without, language)
|
||
sentinel = copy.deepcopy(packet)
|
||
sentinel["worksheets"]["timing_and_predictive_systems"]["narayana_dasha_report_extras"]["ad_direction_readings"] = []
|
||
assert pc._narayana(sentinel, "en") != pc._narayana(packet, "en")
|
||
assert "两种读法在这一段大运里给出相同的方向和相同的当前小运。" in chinese
|
||
assert "The two readings give the same direction and the same current sub-period in this mahadasha." in english
|
||
|
||
|
||
# 6. Conditional dasha: no claimed result without inputs ---------------------------------------------
|
||
|
||
def test_conditional_dasha_row_shows_inputs_or_names_the_engine_verdict():
|
||
_packet, chinese, english = public_chart_reports(JOBS)
|
||
zh = [row for header, body in _tables(chinese) for row in body if row and row[0] == "Shattrimsha Sama"]
|
||
en = [row for header, body in _tables(english) for row in body if row and row[0] == "Shattrimsha Sama"]
|
||
assert zh and en
|
||
assert zh[0][2] == "夜间出生;上升的 Hora 主:月亮" and zh[0][3] == "成立"
|
||
assert en[0][2] == "Night birth; Hora lord of the Lagna: Moon" and en[0][3] == "Met"
|
||
for markdown in (chinese, english):
|
||
assert not [row for header, body in _tables(markdown) for row in body
|
||
if len(row) > 3 and row[2] in ("未随报告返回", "Not carried in the report data") and row[3] in ("成立", "Met")]
|
||
# Inputs not carried: the reason names the source instead of describing what it cannot show.
|
||
row = {"rule": "bphs_shattrimshatsama_day_sun_hora_or_night_moon_hora", "condition": True, "inputs": {}}
|
||
_text, inputs, reason = pc._applicability_words(row, "zh")
|
||
assert inputs == "未随报告返回" and reason == "按引擎判定成立;判定所用输入没有随报告返回"
|
||
_text, inputs, reason = pc._applicability_words({**row, "condition": False}, "en")
|
||
assert reason == "Not met per the engine's verdict; the inputs it used are not carried in the report data"
|
||
|
||
|
||
# 7. Arudha table: house from the Lagna, not the pada's serial number ----------------------------------
|
||
|
||
def test_arudha_table_prints_house_from_the_lagna():
|
||
_packet, chinese, english = public_chart_reports(JOBS)
|
||
assert "| 点 | 星座 | 座内度数 | 从上升数第几宫 |" in chinese
|
||
assert "| Point | Sign | Degree in sign | House from the Lagna |" in english
|
||
assert "| Karma Pada (A10) | 狮子座 | — | 12 |" in chinese # Leo from a Virgo Lagna
|
||
assert "| Karma Pada (A10) | Leo | — | 12 |" in english
|
||
assert "| Hora 上升 | 水瓶座 | 24.02° | 6 |" in chinese
|
||
assert "度数 / 宫位" not in chinese and "Degree / House" not in english
|
||
_packet, obama_zh, obama_en = public_chart_reports(OBAMA)
|
||
assert "| Arudha 上升 (AL) | 天秤座 | — | 10 |" in obama_zh # Libra from a Capricorn Lagna
|
||
assert "| Arudha Lagna (AL) | Libra | — | 10 |" in obama_en
|
||
|
||
|
||
# 8. Darakaraka reader does not mutate its module tables ---------------------------------------------
|
||
|
||
def test_darakaraka_reader_is_repeatable_and_leaves_module_tables_alone():
|
||
from scripts import darakaraka_reader as dk
|
||
|
||
packet, chinese, english = public_chart_reports(JOBS)
|
||
chart = {"ascendant": packet["core_chart"]["ascendant"], "planets": packet["core_chart"]["planets"]}
|
||
tables = copy.deepcopy((dk.PLANET_SPOUSE_TRAITS, dk.SIGN_REFINEMENTS, dk.HOUSE_INTERPRETATIONS))
|
||
results = [dk.analyze_darakaraka(copy.deepcopy(chart), use_8_karaka=True) for _ in range(3)]
|
||
assert results[0] == results[1] == results[2]
|
||
assert (dk.PLANET_SPOUSE_TRAITS, dk.SIGN_REFINEMENTS, dk.HOUSE_INTERPRETATIONS) == tables
|
||
# The sign refinement ran on this chart (Mars in Aries), so the bug would have shown here.
|
||
assert results[0]["dk_planet"] == "Mars" and results[0]["dk_sign"] == "Aries"
|
||
assert "**行动力**" in results[0]["personality_traits"]
|
||
assert "| 配偶星(DK) | 火星 |" in chinese and "| Darakaraka | Mars |" in english
|
||
# Synthetic: a sign that softens a challenge (Taurus removes 急躁 from Mars) leaves the table intact.
|
||
synthetic = {"planets": {name: {"degree_raw": 40.0 + index, "sign": "Taurus", "house": 2}
|
||
for index, name in enumerate(("Sun", "Moon", "Mercury", "Jupiter", "Venus", "Saturn", "Rahu"))}}
|
||
synthetic["planets"]["Mars"] = {"degree_raw": 31.0, "sign": "Taurus", "house": 2}
|
||
first = dk.analyze_darakaraka(copy.deepcopy(synthetic))
|
||
assert all(dk.analyze_darakaraka(copy.deepcopy(synthetic)) == first for _ in range(2))
|
||
assert (dk.PLANET_SPOUSE_TRAITS, dk.SIGN_REFINEMENTS, dk.HOUSE_INTERPRETATIONS) == tables
|