Files
Jyotisha/tests/test_yoga_selection_1975.py

107 lines
5.7 KiB
Python

"""BUG-1975(占星师 2026-11-18 瑜伽卡片选取规则回复):规则级主题映射、按规则 id 去重、实例键、规则定义供查询。"""
from __future__ import annotations
import json
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path[:0] = [str(ROOT / "scripts"), str(ROOT), str(ROOT / "scripts" / "research")]
import build_yoga_rule_topics as builder # noqa: E402
import consultation_native_layers as layers # noqa: E402
TOPICS = json.loads((ROOT / "references" / "yoga_rule_topics.v1.json").read_text(encoding="utf-8"))
RULES = json.loads((ROOT / "references" / "yoga_rules.json").read_text(encoding="utf-8"))["rules"]
def test_every_rule_has_an_entry_and_the_map_is_pending_her_review() -> None:
assert TOPICS["status"] == "pending_astrologer_review"
assert set(TOPICS["rules"]) == {rule["id"] for rule in RULES}
for rule_id, entry in TOPICS["rules"].items():
# BUG-2218: topics come from the frozen snapshot; keywords are only a proposal beside them.
assert entry["mapping"] in {"frozen", "unmapped"}, rule_id
assert bool(entry["topics"]) == (entry["mapping"] == "frozen"), rule_id
def test_the_file_is_what_the_builder_writes_from_the_rule_table() -> None:
by_id = {rule["id"]: rule for rule in RULES}
for rule_id, entry in TOPICS["rules"].items():
assert builder.frozen_classify(by_id[rule_id], builder._frozen())["topics"] == entry["topics"], rule_id
def test_durbhaga_and_special_split_per_rule_her_examples() -> None:
rules = TOPICS["rules"]
# durbhaga is not a wealth class: the mother and relatives rules carry their own topics.
assert rules["bvr_matrunasa_precise"]["topics"] == ["parents"]
assert rules["bvr_matrunasa_precise"]["subtopics"] == {"parents": ["mother"]}
assert "wealth" not in rules["bvr_matrunasa_precise"]["topics"]
assert rules["bvr_bandhubhisthyaktha_precise"]["topics"] == ["family"]
# special rules about the mother and relatives are reachable by topic, not by category.
assert "parents" in rules["bvr_matru_sneha_yoga"]["topics"]
assert "parents" in rules["bvr_matrudeerghayur_yoga"]["topics"]
assert "family" in rules["bvr_bandhu_pujya_yoga"]["topics"]
# The mother's health is about the mother, not the native's health; a disowning clause is not read.
assert "health" not in rules["bvr_matrudeerghayur_yoga"]["topics"]
assert "health" not in rules["bvr_matrunasa_precise"]["topics"]
def test_condition_text_is_not_read() -> None:
rule = {"id": "x", "effects_en": ["Good fortune"], "combination_en": "lord of the 4th (mother) in the 2nd (wealth)"}
assert builder.classify(rule)["topics"] == []
def _layer(hits: list[dict], misses: list[dict] | None = None) -> dict:
modules = {"raman_support": {"yogas": {"status": "executed", "hits": hits, "misses": misses or [],
"ungoverned_detected": []}}}
return layers.build_yoga_conditions({}, modules, {})
def test_the_same_rule_matched_twice_is_one_row_with_a_hit_count() -> None:
out = _layer([
{"name": "Bandhu Pujya Yoga", "rule_id": "bvr_bandhu_pujya_yoga", "hit": True, "planets": ["Jupiter"]},
{"name": "Bandhu Pujya", "rule_id": "bvr_bandhu_pujya_yoga", "hit": True, "planets": ["Jupiter"]},
])
assert len(out["rows"]) == 1
row = out["rows"][0]
assert row["hit_count"] == 2
assert row["alternate_names"] == ["Bandhu Pujya"]
assert row["instance_key"] == f"bvr_bandhu_pujya_yoga|{row['method_version']}|D1"
assert "family" in row["rule_topics"] and row["topic_mapping"] == "keyword_proposal"
def test_same_name_different_rules_stay_two_rows() -> None:
out = _layer([
{"name": "Vesi Yoga", "rule_id": "bvr_016_vesi_precise", "hit": True, "planets": ["Mars"]},
{"name": "Vesi Yoga", "rule_id": "bvr_017_vosi_precise", "hit": True, "planets": ["Mars"]},
])
assert sorted(row["rule_id"] for row in out["rows"]) == ["bvr_016_vesi_precise", "bvr_017_vosi_precise"]
def test_list_rows_and_definitions_for_the_lookup() -> None:
out = _layer([{"name": "Bandhu Pujya Yoga", "rule_id": "bvr_bandhu_pujya_yoga", "hit": True, "planets": ["Jupiter"]}],
[{"name": "Matrunasa", "rule_id": "bvr_matrunasa_precise", "hit": False}])
miss = out["not_formed"][0]
assert miss["rule_topics"] == ["parents"] and miss["instance_key"].startswith("bvr_matrunasa_precise|")
assert out["topic_map"]["status"] == "pending_astrologer_review"
assert out["topic_map"]["version"].startswith("yoga_rule_topics:")
definitions = out["rule_definitions"]
# Evaluated rules carry the structured condition; listed-only rules carry source labels only.
assert "logic" in definitions["bvr_bandhu_pujya_yoga"]
assert "logic" in definitions["bvr_matrunasa_precise"]
unevaluated = [rid for rid in definitions if rid not in {"bvr_bandhu_pujya_yoga", "bvr_matrunasa_precise"}]
assert unevaluated and all("logic" not in definitions[rid] for rid in unevaluated)
def test_a_rule_without_a_map_entry_is_marked_as_such() -> None:
assert layers.yoga_rule_topic_fields("main_detector:neecha_bhanga") == {"rule_topics": [], "topic_mapping": "no_rule_entry"}
def test_rewriting_a_meaning_does_not_move_a_rule_to_another_topic():
"""BUG-2218 (占星师 2026-11-22): Malavya's new 「健康」 wording keeps it out of health; Sasa's 「游历」 out of migration."""
rules = json.loads((ROOT / "references" / "yoga_rule_topics.v1.json").read_text("utf8"))["rules"]
assert rules["bvr_maalavya_yoga"]["topics"] == ["wealth"]
assert "health" in rules["bvr_maalavya_yoga"].get("keyword_proposal", [])
assert rules["mahapurusha_saturn"]["topics"] == []
assert "marriage" not in rules["saubhagya_yoga"]["topics"]