From cf8249ae88136e15df497698ce9a18ce549b2b12 Mon Sep 17 00:00:00 2001 From: sascha Date: Sun, 16 Aug 2026 22:04:21 +0200 Subject: [PATCH] Fix long Emby history ranges (tests/test_app.py) --- tests/test_app.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/tests/test_app.py b/tests/test_app.py index 5c6b2d7..fe4a29b 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -216,6 +216,60 @@ def test_emby_account_sharing_endpoint_is_read_only_and_filterable(monkeypatch): assert payload["users"][0]["username"] == "Alice" +def test_emby_history_step_stays_below_prometheus_resolution_limit(): + assert app._emby_history_step(7) == 60 + for days in (7, 30, 90): + step = app._emby_history_step(days) + assert step % 60 == 0 + assert (days * 86400) / step <= 10_500 + + +def test_emby_history_fetch_chunks_90_days_and_merges_equal_series(monkeypatch): + calls = [] + + class FakeResponse: + def __init__(self, params): + self.params = params + + def raise_for_status(self): + return None + + def json(self): + start = self.params["start"] + end = self.params["end"] + return {"status": "success", "data": {"result": [{ + "metric": {"job": "emby-sascha", "username": "Alice", "remoteEndPoint": "8.8.8.8"}, + "values": [[start, "1"], [end, "1"]], + }]}} + + class FakeClient: + def __init__(self, **_kwargs): + pass + + async def __aenter__(self): + return self + + async def __aexit__(self, *_args): + return None + + async def get(self, _url, params, headers, cookies): + calls.append(params) + return FakeResponse(params) + + monkeypatch.setattr(app, "SERVICES", {"grafana": {"url": "http://grafana", "auth": "none"}}) + monkeypatch.setattr(app.httpx, "AsyncClient", FakeClient) + monkeypatch.setattr(app.time, "time", lambda: 10_000_000) + + series, step = asyncio.run(app._fetch_emby_session_history(90, "all")) + + assert len(calls) == 3 + assert all(call["end"] - call["start"] <= 30 * 86400 for call in calls) + assert step == 780 + assert len(series) == 1 + timestamps = [value[0] for value in series[0]["values"]] + assert timestamps == sorted(set(timestamps)) + + def test_capabilities_is_live_machine_readable_safety_map(): with TestClient(app.app) as client: response = client.get(