BUG-2392 writes formation_note_zh / formation_note_en on formed Raja rows whose lords also rule 6, 8 or 12. The report now prints that note after the status in the body yoga table, after the formation line on the yoga page, and in the appendix table's formation cell. Also updates a stale assertion from BUG-2367 (the PD-only cell in the house-lord column). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DbBXp3RzgZVEpT1PXxkwHj
239 lines
13 KiB
Python
239 lines
13 KiB
Python
"""BUG-2065 (list b): report fields that printed the wrong value or the wrong label.
|
||
|
||
Every chart here is fictional (tests.test_report_english_edition.CASES) and every injected
|
||
value is synthetic.
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import copy
|
||
import re
|
||
|
||
import pytest
|
||
|
||
from scripts.jyotish_engine import _nakshatra_fields
|
||
from scripts.pl9_full_data_export import _sanitize_pl9_ai_density_markdown_en
|
||
from scripts.pl9_interpretation_packs import build_yoga_interpretation_pack
|
||
from scripts.pl9_reader_export import _pl9_export_markdown_for_edition
|
||
from scripts.report_render.upstream_sync_chapters import _rect_section
|
||
from scripts.report_render.yoga_status_words import _report_yoga_rows, report_yoga_table
|
||
from tests.test_report_english_edition import CASES, _packet
|
||
|
||
HOUSE_LORDS = ("Sun", "Moon", "Mars", "Mercury", "Jupiter", "Venus", "Saturn")
|
||
PLACE = "虚构市测试医院"
|
||
|
||
|
||
def _current_levels(timing: dict) -> tuple[dict, dict, dict]:
|
||
current = timing["dasha"]["current_dasha"]
|
||
ad = next(row for row in current["antardasha_timeline"] if row.get("is_current"))
|
||
pd = next(row for row in ad["pratyantar_dasha_timeline"] if row.get("is_current"))
|
||
return current, ad, pd
|
||
|
||
|
||
@pytest.fixture(scope="module")
|
||
def editions() -> tuple[str, str, dict, str]:
|
||
packet = _packet(CASES["day"])
|
||
# The current PD lord is set to a planet that runs neither the MD nor the AD
|
||
# (the reader body reads the worksheet copy, the appendix the packet-level copy).
|
||
current, ad, _pd = _current_levels(packet["worksheets"]["timing_and_predictive_systems"])
|
||
pd_lord = next(name for name in HOUSE_LORDS if name not in (current["lord"], ad["lord"]))
|
||
for timing in (packet["worksheets"]["timing_and_predictive_systems"], packet["timing_and_predictive_systems"]):
|
||
_current_levels(timing)[2]["lord"] = pd_lord
|
||
packet["birth_provenance"] = {**(packet.get("birth_provenance") or {}), "birthplace_label": PLACE}
|
||
zh = _pl9_export_markdown_for_edition(copy.deepcopy(packet), "full_data")
|
||
english = copy.deepcopy(packet)
|
||
english["report_language"] = "en"
|
||
return zh, _pl9_export_markdown_for_edition(english, "full_data"), packet, pd_lord
|
||
|
||
|
||
def _cells(line: str) -> list[str]:
|
||
return [cell.strip() for cell in line.strip().strip("|").split("|")]
|
||
|
||
|
||
def _tables(markdown: str, header_start: str) -> list[list[list[str]]]:
|
||
"""Every table whose header line starts with `header_start`: header cells, then body rows."""
|
||
lines = markdown.splitlines()
|
||
found = []
|
||
for index, line in enumerate(lines):
|
||
if not line.startswith(header_start):
|
||
continue
|
||
rows = [_cells(line)]
|
||
for body in lines[index + 2:]:
|
||
if not body.startswith("|"):
|
||
break
|
||
rows.append(_cells(body))
|
||
found.append(rows)
|
||
return found
|
||
|
||
|
||
def test_two_neecha_bhanga_rows_stay_two_rows_with_their_own_combination() -> None:
|
||
def row(planet: str, **extra) -> dict:
|
||
return {"name": "Neecha Bhanga", "planets": [planet], "status": "formed", **extra}
|
||
|
||
rows = [
|
||
row("Mars", condition="cond-mars"),
|
||
row("Venus", condition="cond-venus"),
|
||
row("Mars", rule_id="second_rule_same_planet"),
|
||
{"name": "Amala Yoga", "planets": [], "rule_id": "a"},
|
||
{"name": "Amala Yoga", "planets": [], "rule_id": "b"},
|
||
]
|
||
kept = _report_yoga_rows(rows)
|
||
assert [(item["name"], item["planets"]) for item in kept] == [
|
||
("Neecha Bhanga", ["Mars"]), ("Neecha Bhanga", ["Venus"]), ("Amala Yoga", []),
|
||
]
|
||
packet = {
|
||
"report_yoga_conditions": {"status": "executed", "rows": kept, "not_evaluated": []},
|
||
"worksheets": {"advanced_systems": {"yoga": {"detected_yogas": [
|
||
row("Mars", combination="火星落陷于巨蟹座", combination_en="Mars debilitated in Cancer"),
|
||
row("Venus", combination="金星落陷于处女座", combination_en="Venus debilitated in Virgo"),
|
||
]}}},
|
||
}
|
||
_layer, named, _observations = report_yoga_table(packet)
|
||
assert [(display["planets"], display["combination_en"]) for _row, display in named[:2]] == [
|
||
(["Mars"], "Mars debilitated in Cancer"), (["Venus"], "Venus debilitated in Virgo"),
|
||
]
|
||
assert [display["combination"] for _row, display in named[:2]] == ["火星落陷于巨蟹座", "金星落陷于处女座"]
|
||
|
||
|
||
def test_house_lord_table_counts_the_current_pd_lord_and_names_the_levels(editions) -> None:
|
||
zh, en, packet, pd_lord = editions
|
||
houses = packet["worksheets"]["d1_rasi_bhava"].get("houses") or packet["core_chart"]["houses"]
|
||
pd_houses = [str(index) for index in range(1, 13)
|
||
if (houses.get(f"house_{index}") or houses.get(str(index)) or {}).get("lord") == pd_lord]
|
||
assert pd_houses
|
||
for markdown, header, active in (
|
||
# 原值: {"active", "active Dasha layer"}
|
||
# 新值: 加上读者用语 "Current"(英文版不再印原始状态词 active)
|
||
# 原因: BUG-2104 英文版表格单元格不印原始状态键
|
||
# 原值: (en, "Dasha activation (MD/AD/PD)", {...}), (zh, "大运激活(大运/小运/细运)", {"当前运行", "当前大运层"})
|
||
# 新值: 表头问三级主星,单元格为 Yes / 是
|
||
# 原因: BUG-2110 占星师 2026-11-21:「是否为当前大运/小运/细运主星」
|
||
# 原值: {"Yes"} / {"是"}
|
||
# 新值: 另允许「仅为细运主星(按填写的出生时刻)」
|
||
# 原因: BUG-2367 起 PD 主星只在出生时刻范围稳定时算作当前主星,否则单独写明(staging 上自 BUG-2367 起失败,BUG-2394 补)
|
||
(en, "Current MD / AD / PD lord?", {"Yes", "PD only, at the entered birth minute"}),
|
||
(zh, "是否为当前大运/小运/细运主星", {"是", "仅为细运主星(按填写的出生时刻)"}),
|
||
):
|
||
tables = [table for table in _tables(markdown, "| ") if table[0][-1] == header]
|
||
assert tables, header
|
||
for table in tables:
|
||
cells = {row[0]: row[-1] for row in table[1:]}
|
||
assert all(cells[house] in active for house in pd_houses), (header, cells)
|
||
|
||
|
||
def test_flattened_sub_periods_name_their_parent_period(editions) -> None:
|
||
zh, en, _packet_value, _pd_lord = editions
|
||
assert "| AD/child | Main period | Sub-period | Start | End |" in en
|
||
assert "main level" not in en and "sub level" not in en
|
||
for markdown, ad_header, pd_header, missing in (
|
||
(en, "| AD/child | Main period |", "| PD | Maha |", {"not listed", "-", ""}),
|
||
(zh, "| AD/child | 主层 |", "| PD | 主层 |", {"未列", "-", ""}),
|
||
):
|
||
ad_tables = _tables(markdown, ad_header)
|
||
pd_tables = _tables(markdown, pd_header)
|
||
assert len(ad_tables) >= 4 and len(pd_tables) >= 4
|
||
for table in ad_tables + pd_tables:
|
||
assert not [row for row in table[1:] if row[1] in missing], table[0]
|
||
|
||
|
||
def test_moon_chart_degree_is_the_degree_within_the_sign(editions) -> None:
|
||
zh, en, packet, _pd_lord = editions
|
||
moon = packet["worksheets"]["d1_rasi_bhava"]["moon_chart"]
|
||
raw = moon.get("raw") if isinstance(moon.get("raw"), dict) else moon
|
||
longitudes = [row["longitude"] for row in raw["planets"].values() if isinstance(row, dict)]
|
||
assert max(longitudes) >= 30 # the source rows carry 0–360 longitudes
|
||
for markdown, header in ((en, "| Moon Chart | Sign | Degree |"), (zh, "| 月亮盘 | 星座 | 度数 |")):
|
||
table = _tables(markdown, header)[0]
|
||
degrees = [float(row[2].rstrip("°")) for row in table[1:]]
|
||
assert len(degrees) == 9 and all(0 <= value < 30 for value in degrees), degrees
|
||
sun = raw["planets"]["Sun"]["longitude"]
|
||
assert f"{sun % 30:.2f}°" == _tables(en, "| Moon Chart | Sign | Degree |")[0][1][2]
|
||
|
||
|
||
def test_shadbala_tables_name_their_units_and_the_ayana_angle(editions) -> None:
|
||
zh, en, _packet_value, _pd_lord = editions
|
||
assert "| Drik | Total (Virupa) | Required (Rupa) | BPHS minimum |" in en
|
||
# BUG-2266: 原值中文表头末列「BPHS minimum」。
|
||
assert "| Drik | 合计(Virupa) | 所需(Rupa) | 最低线 |" in zh
|
||
assert "| Total | Required |" not in en and "| Total | Required |" not in zh
|
||
# The appendix component table (seven components and the total, all in Virupa).
|
||
assert "| Planet | Sthana | Dig | Kala | Chesta | Naisargika | Drik | Total (Virupa) |\n" in en
|
||
assert "| Drik | 合计(Virupa) |\n" in zh
|
||
assert "| Planet | Degree | Ayana intermediate angle | Speed |" in en
|
||
assert "| 行星 | 度数 | Ayana 中间角 | 速度 |" in zh
|
||
assert "| Planet | Degree | Declination |" not in en and "| 行星 | 度数 | 赤纬 |" not in zh
|
||
|
||
|
||
def test_appendix_lagna_row_carries_nakshatra_and_pada(editions) -> None:
|
||
zh, en, packet, _pd_lord = editions
|
||
fields = _nakshatra_fields(float(packet["core_chart"]["ascendant"]["lon"]))
|
||
table = _tables(en, "| Object | Sign | Degree in sign | Longitude | House | Nakshatra |")[0]
|
||
lagna = next(row for row in table[1:] if row[0] == "Lagna")
|
||
header = table[0]
|
||
assert lagna[header.index("Nakshatra")] == fields["nakshatra"]
|
||
assert lagna[header.index("Pada")] == str(fields["nakshatra_pada"])
|
||
zh_table = _tables(zh, "| 天体 | 星座 | 落座度数 | 黄经 | 宫位 | 星宿 |")[0]
|
||
zh_header = zh_table[0]
|
||
assert zh_table[1][zh_header.index("星宿四分")] == str(fields["nakshatra_pada"])
|
||
assert zh_table[1][zh_header.index("星宿")] not in {"未列", "-", ""}
|
||
|
||
|
||
def test_place_label_is_kept_in_the_english_edition(editions) -> None:
|
||
zh, en, _packet_value, _pd_lord = editions
|
||
# BUG-2204 原值:英文版原样保留中文地点标签;新值:纯中文地点在英文版写成坐标、不含汉字;
|
||
# 原因:英文版含汉字会被 han_leak 检查整份拦下(BUG-2204)。中文版照旧。
|
||
place_line = next(line for line in en.splitlines() if line.startswith("- Place label:"))
|
||
assert PLACE not in place_line
|
||
assert not re.search(r"[\u4e00-\u9fff]", place_line), place_line
|
||
assert f"- 地点标签:{PLACE}" in zh
|
||
# Fixed words still translate; an empty label still reads as before.
|
||
assert "- Place label: not recorded" in _rect_section({"birth_provenance": {}}, "en")
|
||
assert "- 地点标签:未记录" in _rect_section({"birth_provenance": {}}, "zh")
|
||
|
||
|
||
def test_neecha_bhanga_sentence_only_on_neecha_bhanga_pages() -> None:
|
||
snapshot = {"detected_yogas": [
|
||
{"name": "Kahala Yoga", "name_cn": "卡哈拉瑜伽", "planets": ["Jupiter"], "combination": "虚构组合", "combination_en": "synthetic"},
|
||
{"name": "Neecha Bhanga", "name_cn": "落陷取消", "planets": ["Mars"], "combination": "虚构组合", "combination_en": "synthetic"},
|
||
]}
|
||
for language, sentence in (("en", "Neecha Bhanga is not a Raja Yoga."), ("zh", "落陷取消不是王者格。")):
|
||
pages = build_yoga_interpretation_pack(snapshot, {}, set(), language=language)["pages"]
|
||
assert len(pages) == 2
|
||
themes = [page["sections"]["theme"] for page in pages]
|
||
assert sentence not in themes[0]
|
||
assert sentence in themes[1]
|
||
assert all(" " not in theme for theme in themes)
|
||
|
||
|
||
def test_appendix_dignity_cells_carry_one_name(editions) -> None:
|
||
zh, en, _packet_value, _pd_lord = editions
|
||
en_table = _tables(en, "| Object | Sign | Degree in sign | Longitude | House | Nakshatra |")[0]
|
||
status = en_table[0].index("Status")
|
||
en_cells = [row[status] for row in en_table[1:] if row[0] != "Lagna"]
|
||
assert len(en_cells) == 9
|
||
assert not [cell for cell in en_cells if "(" in cell or "enters" in cell], en_cells
|
||
zh_table = _tables(zh, "| 天体 | 星座 | 落座度数 | 黄经 | 宫位 | 星宿 |")[0]
|
||
zh_cells = [row[zh_table[0].index("状态")] for row in zh_table[2:]]
|
||
assert len(zh_cells) == 9
|
||
assert not [cell for cell in zh_cells if "(" in cell or "(" in cell], zh_cells
|
||
# 原值: (en, "Dasha activation (MD/AD/PD)"), (zh, "大运激活(大运/小运/细运)")
|
||
# 新值: BUG-2110 的新表头
|
||
# 原因: BUG-2110 占星师 2026-11-21:表头问到大运、小运、细运三级主星
|
||
for markdown, header in ((en, "Current MD / AD / PD lord?"), (zh, "是否为当前大运/小运/细运主星")):
|
||
for table in [table for table in _tables(markdown, "| ") if table[0][-1] == header]:
|
||
assert not [row for row in table[1:] if "(" in row[-2] or "enters" in row[-2]], table[1:3]
|
||
|
||
|
||
def test_number_lists_in_names_are_not_rewritten_as_house_conditions(editions) -> None:
|
||
source = (
|
||
"# T\n\n| Yoga | Status |\n|---|---|\n| Mangal Dosha (Mars in 1/4/7/8/12) | computed |\n\n"
|
||
"#### Mangal Dosha (Mars in 1/4/7/8/12)\n"
|
||
)
|
||
cleaned = _sanitize_pl9_ai_density_markdown_en(source, "T")
|
||
assert cleaned.count("Mangal Dosha (Mars in 1/4/7/8/12)") == 2
|
||
assert "condition" not in cleaned
|
||
_zh, en, _packet_value, _pd_lord = editions
|
||
assert "- Shadbala method: Shadbala six-fold strength, in Virupas, combining Sthana, Dig, Kala, Drik and BPHS Chesta" in en
|
||
assert "Shadbalasix-fold" not in en
|
||
|