Files
Jyotisha/tests/test_report_range_scan_2123.py
T
ClaudeandClaude Opus 5.5 24caa338af
Independent Staging Quality Gate / validate-python (push) Successful in 10m49s
Independent Staging Quality Gate / validate-frontend (push) Successful in 14m45s
Independent Staging Quality Gate / publish (push) Successful in 3m18s
fix(report): a range-scan layer that was not computed never reads as unchanged (BUG-2123 follow-up)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXp2Vp62Ur3C7yRWQdSq
2026-11-21 17:29:16 +08:00

204 lines
11 KiB
Python

"""BUG-2123 (占星师 2026-11-20 第 9 问): boundary scan across the declared birth-time range.
Public fixtures (steve_jobs_1955_aa, barack_obama_1961_aa) declare no range; a declared ±2 minute range
is added to a copy of their packets. Cross-midnight, seconds and the bisection helper use synthetic
times on a synthetic place (neither fixture has those cases).
"""
from copy import deepcopy
from datetime import datetime, timedelta
import pytest
from scripts.chart_choice import boundaries
from scripts.report_render import range_boundary_scan as scan
from scripts.report_render.upstream_sync_chapters import (
SENSITIVITY_KEYS,
render_upstream_sync_chapters,
report_birth_range_scan,
)
from tests.public_chart_reports import public_chart_reports
TWO_MINUTES = {"before_minutes": 2, "after_minutes": 2, "source": "user_stated"}
def _with_window(case_id: str, window: dict | None) -> dict:
packet = deepcopy(public_chart_reports(case_id)[0])
packet.pop("birth_range_boundary_scan", None)
if window is not None:
packet["report_birth_window"] = window
return packet
def _synthetic_packet(time: str, window: dict | None, **extra) -> dict:
hour, minute, second = (int(part) for part in time.split(":"))
packet = {
"birth_info": {"date": "1990-06-30", "time": time[:5], "hour": hour, "minute": minute, "second": second,
"lat": 30.0, "lon": 120.0, "tz": "UTC+8.0", "ayanamsa_name": "lahiri", "node_mode": "mean"},
"calculation_profile": {"effective_settings": {"ayanamsa": "lahiri", "node_mode": "mean", "timezone_offset": "+08:00"}},
**extra,
}
if window is not None:
packet["report_birth_window"] = window
return packet
def test_declared_two_minutes_finds_a_change_only_the_scan_sees_on_both_fixtures() -> None:
# Jobs: D40 changes at +1 min 13 s, outside the ±1 minute sample (which shows D40 unchanged).
# Obama: D9 changes at +1 min 40 s.
expected = {
"steve_jobs_1955_aa": ("D40.ascendant", "1955-02-24T19:16:13", "Libra", "Scorpio",
"| D40 上升 | 天秤座 | 19:16:13 | 天秤座 | 天蝎座 |",
"| D40 ascendant | Libra | 19:16:13 | Libra | Scorpio |"),
"barack_obama_1961_aa": ("D9.ascendant", "1961-08-04T19:25:40", "Leo", "Virgo",
"| D9 上升 | 狮子座 | 19:25:40 | 狮子座 | 处女座 |",
"| D9 ascendant | Leo | 19:25:40 | Leo | Virgo |"),
}
for case_id, (key, at, before, after, zh_row, en_row) in expected.items():
packet = _with_window(case_id, TWO_MINUTES)
result = report_birth_range_scan(packet)
assert result["status"] == "scanned"
assert {"at": at, "before": before, "after": after} in result["intervals"][0]["layers"][key]["changes"]
assert set(result["intervals"][0]["layers"]) == set(SENSITIVITY_KEYS)
zh, en = render_upstream_sync_chapters(packet, "zh"), render_upstream_sync_chapters(packet, "en")
assert "### 出生时间范围换界扫描" in zh and zh_row in zh
assert "### Boundary scan across the declared birth-time range" in en and en_row in en
# 原值: the method line explained minute steps and what bisection cannot see
# 新值: only the reader-facing facts (times to the second; same sign does not fix finer dasha dates)
# 原因: rectification window review 2026-11-21: scan mechanics stay backstage (astrologer 11-19 layering)
assert "换界时刻精确到秒" in zh and "Change times are given to the second" in en
# The ±1 minute sample of Jobs keeps D40 unchanged: only the range scan finds the change.
jobs_zh = render_upstream_sync_chapters(_with_window("steve_jobs_1955_aa", TWO_MINUTES), "zh")
assert "| D40 上升 | 天秤座 | 三分钟对照中没有变化 |" in jobs_zh
def test_no_declared_range_is_not_scanned_and_says_so_on_both_fixtures() -> None:
for case_id in ("steve_jobs_1955_aa", "barack_obama_1961_aa"):
_, chinese, english = public_chart_reports(case_id)
assert "- 没有做这项扫描:出生资料里没有给出出生时间的范围。" in chinese
assert "- Not run: the birth data gives no birth-time range." in english
for window, zh_line in (
({"before_minutes": 1, "after_minutes": 1, "source": "confirmed_minute"},
"- 没有做这项扫描:填写的是确定的出生时间,没有给出范围,报告不自行扩大范围。"),
({"before_minutes": 30, "after_minutes": 30, "source": "default_unknown_30"},
"- 没有做这项扫描:出生资料里没有给出出生时间的范围。"),
):
packet = _with_window(case_id, window)
assert report_birth_range_scan(packet)["status"] == "not_run"
zh = render_upstream_sync_chapters(packet, "zh")
assert zh_line in zh and "换界时刻" not in zh
def test_one_minute_sample_title_says_auxiliary_on_both_fixtures() -> None:
for case_id in ("steve_jobs_1955_aa", "barack_obama_1961_aa"):
_, chinese, english = public_chart_reports(case_id)
assert "### 生时敏感性矩阵(辅助:只抽查前后各 1 分钟)" in chinese
assert "### Birth-time sensitivity matrix (auxiliary: a one-minute sample either side)" in english
assert "三个时刻结果相同,也不能证明整个出生时间范围都不变。" in chinese
assert "Three equal samples do not show that the whole birth-time range is stable." in english
def test_scan_matches_a_per_second_check_across_midnight_with_seconds() -> None:
# Synthetic: reported 23:59:30 with ±2 minutes runs from 23:57:30 to the next day 00:01:30.
packet = _synthetic_packet("23:59:30", TWO_MINUTES)
result = report_birth_range_scan(packet)
part = result["intervals"][0]
assert (part["start"], part["end"]) == ("1990-06-30T23:57:30", "1990-07-01T00:01:30")
req = boundaries.ChartRequest("1990-06-30", 30.0, 120.0, 8.0, "lahiri", "mean")
start = datetime(1990, 6, 30, 23, 57, 30)
charts = [boundaries.chart_at(req, start + timedelta(seconds=s)) for s in range(241)]
def value(key: str, chart: dict):
if key.endswith(".ascendant"):
division = int(key.split(".")[0][1:])
return boundaries.varga.SIGNS[boundaries.varga_sign(float(chart["ascendant"]["lon"]), division)]
if key.startswith("arudha."):
return scan._arudha_signs(chart)[key.split(".")[1]]
return {house: scan._cusp_dict(scan._kp_cusp(chart, house)) for house in scan.KP_CUSPS}
found_any = False
for key in SENSITIVITY_KEYS:
values = [value(key, chart) for chart in charts]
expected_times = [(start + timedelta(seconds=s)).isoformat() for s in range(1, 241) if values[s] != values[s - 1]]
got = sorted({change["at"] for change in part["layers"][key]["changes"]})
assert got == expected_times, key
assert part["layers"][key]["value_at_start"] == values[0], key
found_any = found_any or bool(got)
assert found_any
zh = render_upstream_sync_chapters(packet, "zh")
assert "- 扫描范围:23:57:30 至 1990-07-01 00:01:30" in zh
def test_two_changes_inside_one_step_are_both_found_to_the_second() -> None:
start = datetime(2000, 1, 1, 12, 0, 0)
marks = (start + timedelta(seconds=17), start + timedelta(seconds=43))
def value_at(at: datetime) -> str:
return "A" if at < marks[0] else "B" if at < marks[1] else "C"
changes = scan.find_value_changes(value_at, start, start + timedelta(minutes=2))
assert changes == [{"at": marks[0], "before": "A", "after": "B"}, {"at": marks[1], "before": "B", "after": "C"}]
def test_life_events_and_failures_never_print_as_no_change() -> None:
plain = report_birth_range_scan(_synthetic_packet("10:00:00", TWO_MINUTES))
with_feedback = report_birth_range_scan(_synthetic_packet(
"10:00:00", TWO_MINUTES, life_events=[{"year": 2010, "domain": "career"}], user_feedback={"accurate": False}))
assert plain == with_feedback
broken = _synthetic_packet("10:00:00", TWO_MINUTES)
broken["birth_info"]["lat"] = "not a number"
result = report_birth_range_scan(broken)
assert result == {"schema": scan.SCHEMA, "status": "not_run", "layers_scanned": list(SENSITIVITY_KEYS),
"reason": "calculation_failed"}
lines = "\n".join(render_upstream_sync_chapters(broken, "zh").splitlines())
assert "这次没有算出扫描结果;这不表示范围内没有换界。" in lines
assert "范围内各层都没有查到换界" not in lines
@pytest.mark.parametrize("language", ["zh", "en"])
def test_rendered_scan_has_no_internal_keys(language: str) -> None:
text = render_upstream_sync_chapters(_with_window("barack_obama_1961_aa", TWO_MINUTES), language)
section = text.split("### 出生时间范围换界扫描" if language == "zh" else "### Boundary scan across")[1].split("\n### ")[0]
for leaked in ("house_", "value_at_start", "user_stated", "find_lines", "arudha.", "KP.cusp", "nakshatra_lord"):
assert leaked not in section, leaked
def test_ascendant_changes_equal_rectification_find_lines_to_the_second() -> None:
# Rectification (chart-choice) and the report must give the same change seconds for the same settings.
req = boundaries.ChartRequest("1990-06-30", 30.0, 120.0, 8.0, "lahiri", "mean")
start, end = datetime(1990, 6, 30, 5, 0, 0), datetime(1990, 6, 30, 7, 0, 0)
layers = ("D1.ascendant", "D9.ascendant", "D10.ascendant")
ours = scan.scan_birth_range(req, start, end, layers)
direct = boundaries.find_lines(req, start, end, (1, 9, 10))
for division in (1, 9, 10):
expected = [line.at.isoformat(timespec="seconds") for line in direct if f"D{division}" in line.flips]
got = [change["at"] for change in ours[f"D{division}.ascendant"]["changes"]]
assert got == expected, division
assert any(ours[key]["changes"] for key in layers)
def test_reader_text_does_not_describe_scan_mechanics() -> None:
from scripts.report_render.upstream_sync_chapters import _range_scan_section # noqa: F401
import inspect
import scripts.report_render.upstream_sync_chapters as module
source = inspect.getsource(module)
assert "二分法" not in source and "变过去又变回来" not in source
def test_a_layer_that_was_not_computed_is_never_shown_as_unchanged() -> None:
from scripts.report_render.upstream_sync_chapters import _range_scan_section
scan_result = {
"schema": scan.SCHEMA, "status": "scanned", "layers_scanned": list(scan.RANGE_SCAN_LAYERS),
"window": {"center": "1990-06-30T06:00:00", "source": "user_stated", "before_minutes": 2, "after_minutes": 2},
"intervals": [{"start": "1990-06-30T05:58:00", "end": "1990-06-30T06:02:00",
"layers": {"D1.ascendant": {"value_at_start": "Cancer", "changes": []},
"D9.ascendant": {"value_at_start": None, "changes": []}}}],
}
for language, unchanged, note in (("zh", "范围内各层都没有查到换界", "这不表示它们在范围内不变"),
("en", "No change was found in any layer", "does not mean they stay the same")):
text = "\n".join(_range_scan_section(scan_result, language))
assert unchanged not in text
assert note in text