"""八字编译测试。 守的是:**命是数值,不是散文。** 编译结果必须稳定(同一个四柱永远得到同一个向量), 而且强弱必须真的影响敏感度。 """ from __future__ import annotations import pytest from cybergril.core.bazi import ( ELEMENTS, Pillars, compile_pillars, day_master_strength, element_scores, ten_god, ten_god_distribution, ) PILLARS = Pillars(year="甲子", month="丙寅", day="戊午", time="壬子") def test_pillars_day_master() -> None: assert PILLARS.day_master == "戊" def test_element_scores_sum_positive() -> None: scores = element_scores(PILLARS) assert set(scores) == set(ELEMENTS) assert sum(scores.values()) > 0 assert all(v >= 0 for v in scores.values()) def test_element_scores_rejects_bad_format() -> None: with pytest.raises(ValueError, match="格式错误"): element_scores(Pillars(year="甲", month="丙寅", day="戊午", time="壬子")) def test_day_master_strength_in_range() -> None: scores = element_scores(PILLARS) s = day_master_strength(PILLARS, scores) assert 0.0 <= s <= 1.0 def test_ten_god_known_relations() -> None: # 木克土(克我),同阳 → 七杀 assert ten_god("戊", "甲") == "七杀" # 火生土(生我),同阳 → 偏印 assert ten_god("戊", "丙") == "偏印" # 土克水(我克),同阳 → 偏财 assert ten_god("戊", "壬") == "偏财" # 土同土,同阳 → 比肩 assert ten_god("戊", "戊") == "比肩" def test_ten_god_distribution_normalized() -> None: dist = ten_god_distribution(PILLARS) assert dist assert abs(sum(dist.values()) - 1.0) < 1e-6 assert all(0 <= v <= 1 for v in dist.values()) def test_compile_is_deterministic() -> None: a = compile_pillars(PILLARS) b = compile_pillars(PILLARS) assert a.as_dict() == b.as_dict() def test_compile_produces_bounded_params() -> None: d = compile_pillars(PILLARS) for key in ( "autonomy", "compliance", "expressiveness", "warmth", "anxiety", "aggression", "discipline", "correcting", "solitude_need", "impulsivity", "sharing", "realism", ): val = getattr(d, key) assert 0.0 <= val <= 1.0, f"{key} 越界: {val}" def test_strength_drives_sensitivity() -> None: """身弱必须更敏感、恢复更慢。这是强弱进入动力学的唯一入口。""" weak = compile_pillars(Pillars(year="甲子", month="乙丑", day="丙子", time="丁亥")) # 用编译结果自身的 strength 反推关系 for d in (weak, compile_pillars(PILLARS)): expected_sens = 1.35 - 0.70 * d.strength assert abs(d.sensitivity - expected_sens) < 1e-9 expected_rec = 1.60 - 1.20 * d.strength assert abs(d.recovery_scale - expected_rec) < 1e-9 def test_traits_text_non_empty() -> None: assert compile_pillars(PILLARS).traits_text()