feat: unify live dashboard snapshot (#131)
CI / lint (pull_request) Successful in 9s
CI / build-frontend (pull_request) Successful in 4s

This commit is contained in:
timmy committed 2026-08-06 19:29:21 +00:00
1 parent e2a4b60143
commit 30f92d18a2
10 files changed
+421 -128

No files matched your search

+2 -2
View File
@@ -12,7 +12,7 @@ async def test_live_gitea_api_responses_cannot_be_stored_by_shared_caches(monkey
async def empty_collection():
return []
async def empty_events():
async def empty_events(user_data=None):
return []
monkeypatch.setattr(main, "current_user", user)
@@ -23,7 +23,7 @@ async def test_live_gitea_api_responses_cannot_be_stored_by_shared_caches(monkey
transport = httpx.ASGITransport(app=main.app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
for path in ("/api/v1/context", "/api/v1/events"):
for path in ("/api/v1/context", "/api/v1/events", "/api/v1/live"):
response = await client.get(path)
assert response.status_code == 200
+1 -2
View File
@@ -15,6 +15,5 @@ def test_api_requests_resolve_inside_dashboard_subpath():
urljoin("https://forge.alexanderwhitestone.com/dashboard/", path)
for path in api_paths
} == {
"https://forge.alexanderwhitestone.com/dashboard/api/v1/context",
"https://forge.alexanderwhitestone.com/dashboard/api/v1/events",
"https://forge.alexanderwhitestone.com/dashboard/api/v1/live",
}
+18 -3
View File
@@ -17,8 +17,9 @@ def test_dashboard_has_realtime_gitea_event_stream_widget():
assert "Gitea event stream" in html
assert "id=\"gitea-events\"" in html
assert "function paintEventStream" in html
assert "setInterval(() => { if (!document.hidden) loadEventStream(); }, 5000)" in html
assert "if (!document.hidden) loadEventStream();" in html
assert "fetch('api/v1/live'" in html
assert "paintEventStream(snapshot.events)" in html
assert "loadEventStream" not in html
assert "event.actor?.login" in html
assert "event.repo?.full_name" in html
@@ -27,7 +28,6 @@ def test_event_stream_reports_when_activity_was_refreshed():
html = DASHBOARD.read_text()
assert 'id="gitea-events-status"' in html
assert "setEventStreamStatus('Updating…')" in html
assert "setEventStreamStatus('Updated ' + fmt(new Date()))" in html
@@ -121,6 +121,21 @@ async def test_activity_events_fetches_feed_for_authenticated_user(monkeypatch):
assert paths == ["users/timmy/activities/feeds?limit=20"]
@pytest.mark.anyio
async def test_activity_events_reuses_supplied_authenticated_user(monkeypatch):
async def unexpected_current_user():
raise AssertionError("current user must not be fetched twice")
async def fake_fetch(path):
assert path == "users/timmy/activities/feeds?limit=20"
return []
monkeypatch.setattr(gitea_proxy, "current_user", unexpected_current_user)
monkeypatch.setattr(gitea_proxy, "fetch", fake_fetch)
assert await gitea_proxy.activity_events({"login": "timmy"}) == []
@pytest.mark.anyio
async def test_activity_events_skips_malformed_feed_entries(monkeypatch):
async def fake_current_user():
+43
View File
@@ -0,0 +1,43 @@
import httpx
import pytest
from src import gitea_proxy
from src import main
@pytest.mark.anyio
async def test_gitea_transport_is_reused_across_requests_and_closed():
client_ids = []
async def handler(request):
client_ids.append(id(gitea_proxy._client))
return httpx.Response(200, json={"path": request.url.path})
client = gitea_proxy.start_client(transport=httpx.MockTransport(handler))
try:
first = await gitea_proxy.fetch("user")
second = await gitea_proxy.fetch("user/repos")
finally:
await gitea_proxy.stop_client()
assert first == {"path": "/api/v1/user"}
assert second == {"path": "/api/v1/user/repos"}
assert client_ids == [id(client), id(client)]
assert client.is_closed
@pytest.mark.anyio
async def test_application_lifespan_opens_and_closes_gitea_transport(monkeypatch):
calls = []
monkeypatch.setattr(gitea_proxy, "start_client", lambda: calls.append("start"))
async def stop_client():
calls.append("stop")
monkeypatch.setattr(gitea_proxy, "stop_client", stop_client)
async with main.app.router.lifespan_context(main.app):
assert calls == ["start"]
assert calls == ["start", "stop"]
+23
View File
@@ -55,6 +55,29 @@ async def test_pull_requests_merge_assignment_and_review_responsibilities(monkey
assert pulls[1]["work_reasons"] == ["review_requested"]
@pytest.mark.anyio
async def test_pull_request_searches_start_concurrently(monkeypatch):
started = [asyncio.Event(), asyncio.Event()]
release = asyncio.Event()
async def fake_fetch(path):
index = 0 if "assigned=true" in path else 1
started[index].set()
await release.wait()
return []
monkeypatch.setattr(gitea_proxy, "fetch", fake_fetch)
task = asyncio.create_task(gitea_proxy.pull_requests())
try:
await asyncio.wait_for(
asyncio.gather(*(event.wait() for event in started)), timeout=1
)
finally:
release.set()
assert await task == []
@pytest.mark.anyio
async def test_requested_review_guard_uses_only_dedicated_review_search(monkeypatch):
requested_paths = []
+144
View File
@@ -0,0 +1,144 @@
import asyncio
import json
import pytest
from src import main
@pytest.fixture(autouse=True)
def reset_live_snapshot_task():
main._live_snapshot_task = None
yield
main._live_snapshot_task = None
def payload(response):
return json.loads(response.body)
@pytest.mark.anyio
async def test_live_snapshot_fetches_user_once_and_updates_work_and_activity(monkeypatch):
calls = {"user": 0}
async def user():
calls["user"] += 1
return {"id": 1, "login": "timmy"}
async def empty():
return []
async def events(authenticated_user):
assert authenticated_user["login"] == "timmy"
return [{"type": "push"}]
monkeypatch.setattr(main, "current_user", user)
monkeypatch.setattr(main, "repos", empty)
monkeypatch.setattr(main, "issues", empty)
monkeypatch.setattr(main, "pull_requests", empty)
monkeypatch.setattr(main, "activity_events", events)
response = await main.live_snapshot()
result = payload(response)
assert calls["user"] == 1
assert result["context"]["user"]["login"] == "timmy"
assert result["events"] == [{"type": "push"}]
assert result["sections"] == {"context": "fresh", "events": "fresh"}
@pytest.mark.anyio
async def test_live_snapshot_keeps_fresh_context_when_activity_fails(monkeypatch):
async def user():
return {"id": 1, "login": "timmy"}
async def empty():
return []
async def failing_events(authenticated_user):
raise ConnectionError("secret upstream detail")
monkeypatch.setattr(main, "current_user", user)
monkeypatch.setattr(main, "repos", empty)
monkeypatch.setattr(main, "issues", empty)
monkeypatch.setattr(main, "pull_requests", empty)
monkeypatch.setattr(main, "activity_events", failing_events)
result = payload(await main.live_snapshot())
assert result["context"]["user"]["login"] == "timmy"
assert result["events"] is None
assert result["sections"] == {
"context": "fresh",
"events": "temporarily unavailable",
}
assert "secret" not in json.dumps(result)
@pytest.mark.anyio
async def test_live_snapshot_keeps_fresh_activity_when_work_fails(monkeypatch):
async def user():
return {"id": 1, "login": "timmy"}
async def failing_repos():
raise ConnectionError("work unavailable")
async def empty():
return []
async def events(authenticated_user):
return [{"type": "push"}]
monkeypatch.setattr(main, "current_user", user)
monkeypatch.setattr(main, "repos", failing_repos)
monkeypatch.setattr(main, "issues", empty)
monkeypatch.setattr(main, "pull_requests", empty)
monkeypatch.setattr(main, "activity_events", events)
result = payload(await main.live_snapshot())
assert result["context"] is None
assert result["events"] == [{"type": "push"}]
assert result["sections"] == {
"context": "temporarily unavailable",
"events": "fresh",
}
@pytest.mark.anyio
async def test_live_snapshot_coalesces_only_simultaneous_requests(monkeypatch):
user_calls = 0
release = asyncio.Event()
async def user():
nonlocal user_calls
user_calls += 1
return {"id": 1, "login": "timmy"}
async def blocked_repos():
await release.wait()
return []
async def empty():
return []
async def events(authenticated_user):
return []
monkeypatch.setattr(main, "current_user", user)
monkeypatch.setattr(main, "repos", blocked_repos)
monkeypatch.setattr(main, "issues", empty)
monkeypatch.setattr(main, "pull_requests", empty)
monkeypatch.setattr(main, "activity_events", events)
first = asyncio.create_task(main.live_snapshot())
await asyncio.sleep(0)
second = asyncio.create_task(main.live_snapshot())
await asyncio.sleep(0)
assert user_calls == 1
release.set()
await asyncio.gather(first, second)
await main.live_snapshot()
assert user_calls == 2
+13 -1
View File
@@ -14,4 +14,16 @@ async def test_one_context_snapshot_updates_every_context_backed_panel():
assert "setInterval(tickWidgets, 2000)" not in html
assert "updateRepoMix(qs('#repo-mix'), fetch)" not in html
assert "setInterval(load, 8000)" not in html
assert "document.addEventListener('visibilitychange'" in html
assert "document.addEventListener('visibilitychange'" in html
@pytest.mark.anyio
async def test_one_live_snapshot_updates_work_and_activity_on_one_timer():
html = await dashboard()
assert "fetch('api/v1/live'" in html
assert "renderLiveSnapshot(snapshot)" in html
assert "renderContextSnapshot(snapshot.context)" in html
assert "paintEventStream(snapshot.events)" in html
assert "loadEventStream" not in html
assert "5000" not in html