from datetime import datetime from pathlib import Path import pytest def stamp(value): return datetime.fromisoformat(value).timestamp() def test_closed_batch_window_survives_restart_and_clock_rollback(tmp_path): from app.services.orchestration.admission_clock import AdmissionClock path = tmp_path / "admission-clock.json" clock = AdmissionClock(path) assert clock.observe(stamp("2026-11-11T07:59:59+08:00")) assert clock.observe(stamp("2026-11-11T08:00:00+08:00")) restarted = AdmissionClock(path) assert not restarted.observe(stamp("2026-11-11T07:59:59+08:00")) assert restarted.reason == "window_already_closed" assert not restarted.observe(stamp("2026-11-11T07:55:00+08:00")) assert restarted.reason == "clock_rollback" assert restarted.observe(stamp("2026-11-11T21:00:00+08:00")) def test_corrupt_clock_state_fails_closed_without_affecting_interactive(tmp_path): from app.services.orchestration.admission_clock import AdmissionClock path = tmp_path / "clock.json" path.write_text("{incomplete") clock = AdmissionClock(path) assert not clock.observe(stamp("2026-11-11T21:00:00+08:00")) assert clock.reason == "clock_state_invalid" @pytest.mark.parametrize("value", ["NaN", "Infinity", "-Infinity", "1e300"]) def test_nonfinite_or_impossible_watermark_only_closes_batch_gate(tmp_path, value): from app.services.orchestration.admission_clock import AdmissionClock path = tmp_path / "clock.json" path.write_text('{"max_seen":' + value + ',"closed_through":"2026-11-11"}') clock = AdmissionClock(path) assert not clock.observe(stamp("2026-11-11T21:00:00+08:00")) assert clock.reason == "clock_state_invalid"