FUE-T09: grace-shift *_today daily reset past local midnight + dedicated 00:00:10 publish
This commit is contained in:
+236
-19
@@ -1729,10 +1729,13 @@ def test_import_cost_today_getter_uses_local_day_window(energy_db) -> None:
|
||||
"""FU11: import_cost_today getter returns value for today's local window.
|
||||
|
||||
Setup: period at local 'today' (within today's UTC window), active contract.
|
||||
Timezone pinned to UTC. Verifies: value = metered_import + fixed_costs for 1 day
|
||||
Timezone pinned to UTC, local_now pinned to 2026-06-26 12:00:00 UTC (deterministic midday).
|
||||
Verifies: value = metered_import + fixed_costs for 1 day
|
||||
(Principle C: today counts as elapsed if we're past local midnight — which UTC is).
|
||||
|
||||
FUE-T05: provider requires an active electricity meter.
|
||||
FUE-T09: local_now pinned (not real time) to eliminate the ~5s flaky window introduced
|
||||
by the grace period: (local_now()-5s).date() is unpredictable near UTC 00:00:00–00:00:04.
|
||||
"""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
@@ -1740,17 +1743,16 @@ def test_import_cost_today_getter_uses_local_day_window(energy_db) -> None:
|
||||
from app.integrations.expose import build_catalog
|
||||
from app.services import timezone as _tz_mod
|
||||
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
# Place a period in today's window (1 hour ago, well within today UTC midnight→tomorrow)
|
||||
# When TZ=UTC, local today = UTC today, so a period 1h ago is in today's window.
|
||||
t0 = now_utc.replace(minute=0, second=0, microsecond=0) - timedelta(hours=1)
|
||||
# Ensure t0 is still today UTC (handle edge case where now is < 1h past UTC midnight)
|
||||
if t0.date() < now_utc.date():
|
||||
t0 = now_utc.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
# Pin to a deterministic midday moment — (fake_now - 5s).date() == fake_now.date() always.
|
||||
fake_now = datetime(2026, 6, 26, 12, 0, 0, tzinfo=timezone.utc)
|
||||
today_midnight = fake_now.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
# Place a period 1 hour before fake_now — well within today's [midnight, tomorrow midnight).
|
||||
t0 = fake_now.replace(hour=11, minute=0, second=0, microsecond=0)
|
||||
|
||||
# Contract starting well before today
|
||||
effective_from = now_utc.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=30)
|
||||
meter_start = now_utc.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=30)
|
||||
effective_from = today_midnight - timedelta(days=30)
|
||||
meter_start = today_midnight - timedelta(days=30)
|
||||
|
||||
import_cost_today = 1.23
|
||||
|
||||
@@ -1766,8 +1768,12 @@ def test_import_cost_today_getter_uses_local_day_window(energy_db) -> None:
|
||||
session.commit()
|
||||
|
||||
with Session(energy_db) as session:
|
||||
# Pin timezone to UTC so today_local = UTC date (deterministic on any CI host).
|
||||
with patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")):
|
||||
# Pin both local_tz (UTC) and local_now (deterministic midday) so the today window
|
||||
# is always [2026-06-26 00:00:00 UTC, 2026-06-27 00:00:00 UTC), regardless of CI clock.
|
||||
with (
|
||||
patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")),
|
||||
patch.object(_tz_mod, "local_now", return_value=fake_now),
|
||||
):
|
||||
catalog = build_catalog(session)
|
||||
today_entry = next(
|
||||
e for e in catalog if e.entity.key == "energy.import_cost_today"
|
||||
@@ -1789,6 +1795,8 @@ def test_export_revenue_today_getter_uses_local_day_window(energy_db) -> None:
|
||||
"""FU11: export_revenue_today getter returns value for today's local window.
|
||||
|
||||
FUE-T05: provider requires an active electricity meter.
|
||||
FUE-T09: local_now pinned (not real time) to eliminate the ~5s flaky window introduced
|
||||
by the grace period: (local_now()-5s).date() is unpredictable near UTC 00:00:00–00:00:04.
|
||||
"""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
@@ -1796,13 +1804,15 @@ def test_export_revenue_today_getter_uses_local_day_window(energy_db) -> None:
|
||||
from app.integrations.expose import build_catalog
|
||||
from app.services import timezone as _tz_mod
|
||||
|
||||
now_utc = datetime.now(timezone.utc)
|
||||
t0 = now_utc.replace(minute=0, second=0, microsecond=0) - timedelta(hours=1)
|
||||
if t0.date() < now_utc.date():
|
||||
t0 = now_utc.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
# Pin to a deterministic midday moment — (fake_now - 5s).date() == fake_now.date() always.
|
||||
fake_now = datetime(2026, 6, 26, 12, 0, 0, tzinfo=timezone.utc)
|
||||
today_midnight = fake_now.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||
|
||||
effective_from = now_utc.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=30)
|
||||
meter_start = now_utc.replace(hour=0, minute=0, second=0, microsecond=0) - timedelta(days=30)
|
||||
# Place a period 1 hour before fake_now — well within today's [midnight, tomorrow midnight).
|
||||
t0 = fake_now.replace(hour=11, minute=0, second=0, microsecond=0)
|
||||
|
||||
effective_from = today_midnight - timedelta(days=30)
|
||||
meter_start = today_midnight - timedelta(days=30)
|
||||
export_today = 0.75
|
||||
|
||||
with Session(energy_db) as session:
|
||||
@@ -1817,7 +1827,12 @@ def test_export_revenue_today_getter_uses_local_day_window(energy_db) -> None:
|
||||
session.commit()
|
||||
|
||||
with Session(energy_db) as session:
|
||||
with patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")):
|
||||
# Pin both local_tz (UTC) and local_now (deterministic midday) so the today window
|
||||
# is always [2026-06-26 00:00:00 UTC, 2026-06-27 00:00:00 UTC), regardless of CI clock.
|
||||
with (
|
||||
patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")),
|
||||
patch.object(_tz_mod, "local_now", return_value=fake_now),
|
||||
):
|
||||
catalog = build_catalog(session)
|
||||
today_entry = next(
|
||||
e for e in catalog if e.entity.key == "energy.export_revenue_today"
|
||||
@@ -2468,3 +2483,205 @@ def test_energy_cost_identifiers_change_after_meter_swap(energy_db) -> None:
|
||||
assert entity.device.identifiers[1] != old_uuid, (
|
||||
f"After swap, old uuid {old_uuid!r} must not appear in identifiers"
|
||||
)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# FUE-T09: *_today grace — window selection at / around local midnight
|
||||
# ---------------------------------------------------------------------------
|
||||
#
|
||||
# Strategy: patch local_now + local_tz (to UTC) and spy on summarize to capture
|
||||
# the start/end arguments. Verifies that the 5-second grace means:
|
||||
# - 00:00:03 → today_local = yesterday (grace not yet elapsed)
|
||||
# - 00:00:12 → today_local = today (grace elapsed)
|
||||
# - 12:00:00 → today_local = today (midday, unaffected)
|
||||
|
||||
|
||||
def _spy_summarize_factory(captured: dict):
|
||||
"""Return a summarize spy that records start/end and returns a zero-value dict."""
|
||||
def _spy(sess, start, end):
|
||||
captured["start"] = start
|
||||
captured["end"] = end
|
||||
return {"metered_import": 0.0, "fixed_costs": 0.0, "metered_export": 0.0, "credits": 0.0}
|
||||
return _spy
|
||||
|
||||
|
||||
def _setup_grace_db(energy_db) -> None:
|
||||
"""Insert active meter + active contract + one non-degraded period for grace tests."""
|
||||
anchor = datetime(2026, 6, 25, 0, 0, 0, tzinfo=timezone.utc)
|
||||
with Session(energy_db) as session:
|
||||
_make_active_meter(session, started_at=anchor, label="Grace Test Meter")
|
||||
_make_contract_with_version(
|
||||
session,
|
||||
values=_STANDING_VALUES,
|
||||
effective_from=anchor,
|
||||
active=True,
|
||||
)
|
||||
_make_period(session, period_start=anchor, import_cost=1.0, export_revenue=0.5, degraded=False)
|
||||
session.commit()
|
||||
|
||||
|
||||
def test_today_getter_grace_uses_yesterday_window_at_00_00_03(energy_db) -> None:
|
||||
"""FUE-T09 AC1a: at local 00:00:03 (< grace=5s), both today getters use YESTERDAY's window.
|
||||
|
||||
With grace=5s, (local_now() - 5s) = 2026-06-25 23:59:58, so today_local = 2026-06-25
|
||||
(yesterday). summarize must be called with start = 2026-06-25 00:00:00 UTC.
|
||||
Verifies both import and export today getters.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from zoneinfo import ZoneInfo
|
||||
from app.integrations.expose import build_catalog
|
||||
from app.services import timezone as _tz_mod
|
||||
|
||||
_setup_grace_db(energy_db)
|
||||
|
||||
# 2026-06-26 00:00:03 UTC — 3 seconds past midnight, still within 5-second grace.
|
||||
fake_now = datetime(2026, 6, 26, 0, 0, 3, tzinfo=timezone.utc)
|
||||
|
||||
captured_import: dict = {}
|
||||
captured_export: dict = {}
|
||||
|
||||
with Session(energy_db) as session:
|
||||
with (
|
||||
patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")),
|
||||
patch.object(_tz_mod, "local_now", return_value=fake_now),
|
||||
patch("app.services.energy_cost.summarize", side_effect=_spy_summarize_factory(captured_import)),
|
||||
):
|
||||
catalog = build_catalog(session)
|
||||
import_entry = next(e for e in catalog if e.entity.key == "energy.import_cost_today")
|
||||
import_entry.entity.value_getter(session)
|
||||
|
||||
# Reset and test export getter (separate patch context so we get a fresh spy dict)
|
||||
captured_export = {}
|
||||
with Session(energy_db) as session:
|
||||
with (
|
||||
patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")),
|
||||
patch.object(_tz_mod, "local_now", return_value=fake_now),
|
||||
patch("app.services.energy_cost.summarize", side_effect=_spy_summarize_factory(captured_export)),
|
||||
):
|
||||
catalog = build_catalog(session)
|
||||
export_entry = next(e for e in catalog if e.entity.key == "energy.export_revenue_today")
|
||||
export_entry.entity.value_getter(session)
|
||||
|
||||
# Grace not elapsed: today_local = 2026-06-25 (yesterday), so window starts there.
|
||||
expected_start = datetime(2026, 6, 25, 0, 0, 0, tzinfo=timezone.utc)
|
||||
assert captured_import.get("start") == expected_start, (
|
||||
f"import_cost_today at 00:00:03: window start must be yesterday ({expected_start}), "
|
||||
f"got {captured_import.get('start')}"
|
||||
)
|
||||
assert captured_export.get("start") == expected_start, (
|
||||
f"export_revenue_today at 00:00:03: window start must be yesterday ({expected_start}), "
|
||||
f"got {captured_export.get('start')}"
|
||||
)
|
||||
|
||||
|
||||
def test_today_getter_grace_uses_today_window_at_00_00_12(energy_db) -> None:
|
||||
"""FUE-T09 AC1b: at local 00:00:12 (> grace=5s), both today getters use TODAY's window.
|
||||
|
||||
With grace=5s, (local_now() - 5s) = 2026-06-26 00:00:07, so today_local = 2026-06-26
|
||||
(today). summarize must be called with start = 2026-06-26 00:00:00 UTC.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from zoneinfo import ZoneInfo
|
||||
from app.integrations.expose import build_catalog
|
||||
from app.services import timezone as _tz_mod
|
||||
|
||||
_setup_grace_db(energy_db)
|
||||
|
||||
# 2026-06-26 00:00:12 UTC — 12 seconds past midnight, beyond the 5-second grace.
|
||||
fake_now = datetime(2026, 6, 26, 0, 0, 12, tzinfo=timezone.utc)
|
||||
|
||||
captured_import: dict = {}
|
||||
captured_export: dict = {}
|
||||
|
||||
with Session(energy_db) as session:
|
||||
with (
|
||||
patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")),
|
||||
patch.object(_tz_mod, "local_now", return_value=fake_now),
|
||||
patch("app.services.energy_cost.summarize", side_effect=_spy_summarize_factory(captured_import)),
|
||||
):
|
||||
catalog = build_catalog(session)
|
||||
import_entry = next(e for e in catalog if e.entity.key == "energy.import_cost_today")
|
||||
import_entry.entity.value_getter(session)
|
||||
|
||||
captured_export = {}
|
||||
with Session(energy_db) as session:
|
||||
with (
|
||||
patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")),
|
||||
patch.object(_tz_mod, "local_now", return_value=fake_now),
|
||||
patch("app.services.energy_cost.summarize", side_effect=_spy_summarize_factory(captured_export)),
|
||||
):
|
||||
catalog = build_catalog(session)
|
||||
export_entry = next(e for e in catalog if e.entity.key == "energy.export_revenue_today")
|
||||
export_entry.entity.value_getter(session)
|
||||
|
||||
# Grace elapsed: today_local = 2026-06-26 (today), window starts there.
|
||||
expected_start = datetime(2026, 6, 26, 0, 0, 0, tzinfo=timezone.utc)
|
||||
assert captured_import.get("start") == expected_start, (
|
||||
f"import_cost_today at 00:00:12: window start must be today ({expected_start}), "
|
||||
f"got {captured_import.get('start')}"
|
||||
)
|
||||
assert captured_export.get("start") == expected_start, (
|
||||
f"export_revenue_today at 00:00:12: window start must be today ({expected_start}), "
|
||||
f"got {captured_export.get('start')}"
|
||||
)
|
||||
|
||||
|
||||
def test_today_getter_grace_unaffected_at_midday(energy_db) -> None:
|
||||
"""FUE-T09 AC2: at 12:00, grace doesn't shift the window — today_local still = today.
|
||||
|
||||
Well past midnight, (local_now() - 5s).date() == local_now().date() always.
|
||||
"""
|
||||
from unittest.mock import patch
|
||||
from zoneinfo import ZoneInfo
|
||||
from app.integrations.expose import build_catalog
|
||||
from app.services import timezone as _tz_mod
|
||||
|
||||
_setup_grace_db(energy_db)
|
||||
|
||||
# 2026-06-26 12:00:00 UTC — midday.
|
||||
fake_now = datetime(2026, 6, 26, 12, 0, 0, tzinfo=timezone.utc)
|
||||
|
||||
captured: dict = {}
|
||||
|
||||
with Session(energy_db) as session:
|
||||
with (
|
||||
patch.object(_tz_mod, "local_tz", return_value=ZoneInfo("UTC")),
|
||||
patch.object(_tz_mod, "local_now", return_value=fake_now),
|
||||
patch("app.services.energy_cost.summarize", side_effect=_spy_summarize_factory(captured)),
|
||||
):
|
||||
catalog = build_catalog(session)
|
||||
import_entry = next(e for e in catalog if e.entity.key == "energy.import_cost_today")
|
||||
import_entry.entity.value_getter(session)
|
||||
|
||||
# At 12:00 the grace (5s) does not push the date back: today_local = 2026-06-26.
|
||||
expected_start = datetime(2026, 6, 26, 0, 0, 0, tzinfo=timezone.utc)
|
||||
assert captured.get("start") == expected_start, (
|
||||
f"import_cost_today at 12:00: window start must be today ({expected_start}), "
|
||||
f"got {captured.get('start')}"
|
||||
)
|
||||
|
||||
|
||||
def test_midnight_state_publish_no_raise_when_mqtt_disabled() -> None:
|
||||
"""FUE-T09 AC3: _run_midnight_state_publish is safe when MQTT is disabled.
|
||||
|
||||
_run_midnight_state_publish delegates to publish_states, which is internally
|
||||
guarded (_should_publish returns False when MQTT is disabled or not connected).
|
||||
This test verifies publish_states itself doesn't raise under those conditions —
|
||||
the job's try/except swallows any other exception as well.
|
||||
"""
|
||||
settings = _make_settings(mqtt_enabled=False)
|
||||
mock_mgr = _make_mock_manager(is_connected=False)
|
||||
|
||||
with (
|
||||
patch("app.services.ha_discovery.build_runtime_settings", return_value=settings),
|
||||
patch("app.services.ha_discovery.mqtt_manager", mock_mgr),
|
||||
):
|
||||
from app.services.ha_discovery import publish_states
|
||||
from sqlalchemy import create_engine as _ce
|
||||
from sqlalchemy.orm import Session as _S
|
||||
|
||||
eng = _ce("sqlite:///:memory:")
|
||||
with _S(eng) as sess:
|
||||
publish_states(sess) # must not raise
|
||||
|
||||
mock_mgr.publish.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user