"""Each metric's normal, by time slot (#5739): the slot arithmetic and the registry. Learning itself runs in SQL; tests/integration/test_norms.py covers it against Postgres.""" from __future__ import annotations from datetime import datetime, timedelta, timezone from steward.core import norms def test_a_day_slot_is_the_utc_hour(): at = datetime(2026, 10, 14, 2, 30, tzinfo=timezone(timedelta(hours=-5))) # 07:30 UTC assert norms.slot_of(at, "day") == 7 def test_a_week_slot_counts_hours_from_monday_utc(): monday = datetime(2026, 10, 12, 0, 10, tzinfo=timezone.utc) assert norms.slot_of(monday, "week") == 0 assert norms.slot_of(monday + timedelta(days=2, hours=2), "week") == 50 # Wed 02:10 assert norms.slot_of(monday + timedelta(days=6, hours=23), "week") == 167 def test_a_learned_metric_is_registered_once_with_its_floor(): norms.learn_normal("test", "x_pct", 2) norms.learn_normal("test", "x_pct", 3) assert norms.LEARNED[("test", "x_pct")] == norms.Learned("test", "x_pct", 3.0) del norms.LEARNED[("test", "x_pct")]