"""Real chart parity, civil midnight and bounded/cache endpoint contracts.""" from datetime import timedelta import pytest from scripts.rectification_varga_segments_api import scan_varga_segments from scripts.research import varga_resolution_lib as vr @pytest.mark.parametrize("radius", [10, 30, 60]) def test_scan_matches_research_every_minute(radius, tmp_path): case = vr.load_cases()[0] # Public AA calibration fixture, never a user profile. origin = vr.birth_datetime(case) body = {"start_at": (origin - timedelta(minutes=radius)).strftime("%Y-%m-%dT%H:%M"), "end_at": (origin + timedelta(minutes=radius)).strftime("%Y-%m-%dT%H:%M"), "latitude": case["birth"]["latitude"], "longitude": case["birth"]["longitude"], "timezone_offset": case["birth"]["timezone_offset"], "target_charts": ["D1", "D9", "D10"]} actual = scan_varga_segments(body, cache_dir=tmp_path) signs = vr.signs_per_minute(case, radius, vargas=("D1", "D9", "D10")) assert actual["window_minutes"] == 2 * radius + 1 assert [minute["signs"] for minute in actual["minutes"]] == [signs[offset] for offset in range(-radius, radius + 1)] for chart in body["target_charts"]: expected = vr.segments_for(signs, (chart,)) assert [(row["start"] - radius, row["end"] - radius, row["sign"]) for row in actual["segments"][chart]] == [(row["start"], row["end"], row["key"][0]) for row in expected] assert scan_varga_segments(body, cache_dir=tmp_path) == actual assert len(list(tmp_path.glob("*.json"))) == 1 def test_midnight_keeps_dates_and_offsets(tmp_path): body = {"start_at": "2000-01-01T23:55", "end_at": "2000-01-02T00:05", "latitude": 0, "longitude": 0, "timezone_offset": 0, "target_charts": ["D1"]} result = scan_varga_segments(body, cache_dir=tmp_path) assert result["minutes"][0]["date"] == "2000-01-01" assert result["minutes"][-1]["date"] == "2000-01-02" assert result["minutes"][-1]["offset"] == 10 assert all(result["full_window_unique"].values()) @pytest.mark.parametrize("patch", [ {"target_charts": ["D12"]}, {"target_charts": ["D1", {}]}, {"target_charts": [["D1"]]}, {"target_charts": [None]}, {"target_charts": ["D1", "D1"]}, {"target_charts": "D1"}, {"end_at": "2000-01-03T00:00"}, {"latitude": float("nan")}, {"timezone_offset": True}, ]) def test_invalid_scan_rejected(patch, tmp_path): body = {"start_at": "2000-01-01T00:00", "end_at": "2000-01-01T00:10", "latitude": 0, "longitude": 0, "timezone_offset": 0, **patch} with pytest.raises(ValueError): scan_varga_segments(body, cache_dir=tmp_path) @pytest.mark.parametrize("damaged", ["[]", "null", "42", "{broken"]) def test_nonobject_or_broken_cache_recomputed(damaged, tmp_path): body = {"start_at": "2000-01-01T00:00", "end_at": "2000-01-01T00:02", "latitude": 0, "longitude": 0, "timezone_offset": 0, "target_charts": ["D1"]} expected = scan_varga_segments(body, cache_dir=tmp_path) path = next(tmp_path.glob("*.json")) path.write_text(damaged, encoding="utf-8") assert scan_varga_segments(body, cache_dir=tmp_path) == expected