M8-R06: refresh Tibber prices after startup and configuration
This commit is contained in:
@@ -27,7 +27,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from app.integrations.tibber.client import PricePoint, TibberError
|
||||
from app.models.energy import EnergyContract, EnergyContractVersion, TibberPrice
|
||||
from app.services.tibber_prices import refresh_prices
|
||||
from app.services.tibber_prices import refresh_prices, run_tibber_refresh_best_effort, trigger_tibber_refresh
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
@@ -392,3 +392,55 @@ def test_refresh_prices_propagates_client_error(energy_db, monkeypatch):
|
||||
|
||||
# No rows should have been written.
|
||||
assert _count_tibber_price_rows(session) == 0
|
||||
|
||||
|
||||
def test_best_effort_refresh_uses_isolated_session_and_sanitises_failure(monkeypatch, caplog):
|
||||
"""Background failures close their own session and never log supplied secrets."""
|
||||
import app.db as db_module
|
||||
import app.services.config_page as config_page
|
||||
import app.services.tibber_prices as tibber_prices
|
||||
|
||||
token = "secret-token-must-not-appear"
|
||||
home_id = "secret-home-must-not-appear"
|
||||
settings = _FakeSettings(token=token, home_id=home_id)
|
||||
|
||||
class _Session:
|
||||
rolled_back = False
|
||||
closed = False
|
||||
|
||||
def rollback(self):
|
||||
self.rolled_back = True
|
||||
|
||||
def close(self):
|
||||
self.closed = True
|
||||
|
||||
session = _Session()
|
||||
monkeypatch.setattr(db_module, "get_session_local", lambda: lambda: session)
|
||||
monkeypatch.setattr(config_page, "build_runtime_settings", lambda *_args: settings)
|
||||
monkeypatch.setattr(tibber_prices, "refresh_prices", lambda *_args: (_ for _ in ()).throw(RuntimeError(token)))
|
||||
|
||||
assert run_tibber_refresh_best_effort() is True
|
||||
assert session.rolled_back is True
|
||||
assert session.closed is True
|
||||
assert token not in caplog.text
|
||||
assert home_id not in caplog.text
|
||||
|
||||
|
||||
def test_trigger_tibber_refresh_starts_daemon_without_running_inline(monkeypatch):
|
||||
"""API triggers are non-blocking; the worker owns the eventual refresh."""
|
||||
import app.services.tibber_prices as tibber_prices
|
||||
|
||||
captured = {}
|
||||
|
||||
class _Thread:
|
||||
def __init__(self, **kwargs):
|
||||
captured.update(kwargs)
|
||||
|
||||
def start(self):
|
||||
captured["started"] = True
|
||||
|
||||
monkeypatch.setattr(tibber_prices, "Thread", _Thread)
|
||||
trigger_tibber_refresh()
|
||||
assert captured["started"] is True
|
||||
assert captured["daemon"] is True
|
||||
assert captured["target"] is run_tibber_refresh_best_effort
|
||||
|
||||
Reference in New Issue
Block a user