OffGridSchedula / tests /test_impact.py
ParetoOptimal's picture
Initial Commit
0366d65
Raw
History Blame Contribute Delete
1.95 kB
"""Durable weekly impact metrics (server/impact.py)."""
import importlib
import pytest
from server import impact
@pytest.fixture(autouse=True)
def _clean():
impact.reset()
yield
impact.reset()
def test_record_and_read_roundtrip():
impact.record_capture(3, conflicts_caught=1)
w = impact.this_week()
assert w["events_captured"] == 3
assert w["conflicts_caught"] == 1
# default estimate: 3*8 + 1*15
assert w["minutes_saved"] == 3 * 8 + 1 * 15
def test_accumulates_across_calls():
impact.record_capture(2)
impact.record_capture(1, conflicts_caught=2)
w = impact.this_week()
assert w["events_captured"] == 3
assert w["conflicts_caught"] == 2
def test_persists_across_restart():
"""this_week() reads from disk each call, so a fresh import sees prior data."""
impact.record_capture(5)
reloaded = importlib.reload(impact)
assert reloaded.this_week()["events_captured"] == 5
def test_week_rollover_keeps_weeks_separate(monkeypatch):
# one _week_key() call per record_capture / this_week
weeks = iter(["2026-W23", "2026-W24"])
monkeypatch.setattr(impact, "_week_key", lambda when=None: next(weeks))
impact.record_capture(2) # writes W23
this = impact.this_week() # reads W24 -> empty
assert this["events_captured"] == 0
# both weeks coexist in the file
data = impact._load()
assert data["2026-W23"]["events_captured"] == 2
def test_minutes_saved_env_override(monkeypatch):
monkeypatch.setenv("IMPACT_MIN_PER_EVENT", "12")
monkeypatch.setenv("IMPACT_MIN_PER_CONFLICT", "20")
impact.record_capture(2, conflicts_caught=1)
assert impact.this_week()["minutes_saved"] == 2 * 12 + 1 * 20
def test_this_week_defaults_to_zero():
w = impact.this_week()
assert w == {"events_captured": 0, "conflicts_caught": 0, "minutes_saved": 0}