stackchain-dashboard/tests/test_gitea_notifications.py
timmy a86de5cbf4
All checks were successful
CI / lint (pull_request) Successful in 11s
CI / build-frontend (pull_request) Successful in 5s
feat: paginate unread updates inbox (#147)
2026-08-06 23:30:32 +00:00

113 lines
3.5 KiB
Python

import httpx
import pytest
from src import gitea_proxy
@pytest.mark.anyio
async def test_notification_page_preserves_upstream_total_without_loading_other_pages():
requests = []
def upstream(request):
requests.append(str(request.url))
return httpx.Response(
200,
headers={"X-Total-Count": "125"},
json=[{
"id": 42,
"unread": True,
"repository": {"full_name": "stackchain/api"},
"subject": {
"title": "Retry failed deploy",
"type": "Issue",
"html_url": "https://forge.example/stackchain/api/issues/7",
},
}],
)
gitea_proxy.start_client(transport=httpx.MockTransport(upstream))
try:
result = await gitea_proxy.notification_page(page=2)
finally:
await gitea_proxy.stop_client()
assert requests == [
"http://127.0.0.1:3000/api/v1/notifications?status-types=unread&limit=50&page=2"
]
assert result["page"] == 2
assert result["total"] == 125
assert result["has_more"] is True
assert [item["id"] for item in result["items"]] == [42]
@pytest.mark.anyio
async def test_unread_notifications_are_normalized_for_mobile_handoff():
result = gitea_proxy._normalize_notifications(
[
{
"id": 42,
"unread": True,
"updated_at": "2026-08-06T12:30:00Z",
"repository": {"full_name": "stackchain/api"},
"subject": {
"title": "Retry failed deploy",
"type": "Issue",
"state": "open",
"html_url": "https://forge.example/stackchain/api/issues/7",
"latest_comment_html_url": "https://forge.example/stackchain/api/issues/7#issuecomment-9",
},
},
{"id": 43, "repository": None, "subject": None},
{
"id": 44,
"repository": {"full_name": "stackchain/web"},
"subject": {"title": "Unsafe", "html_url": "javascript:alert(1)"},
},
"malformed",
]
)
assert result == [
{
"id": 42,
"unread": True,
"updated_at": "2026-08-06T12:30:00Z",
"repository": "stackchain/api",
"number": 7,
"title": "Retry failed deploy",
"subject_type": "Issue",
"state": "open",
"url": "https://forge.example/stackchain/api/issues/7#issuecomment-9",
"subject_url": "https://forge.example/stackchain/api/issues/7",
},
{
"id": 43,
"unread": False,
"updated_at": "",
"repository": "",
"number": None,
"title": "Untitled update",
"subject_type": "Update",
"state": "",
"url": "",
"subject_url": "",
},
{
"id": 44,
"unread": False,
"updated_at": "",
"repository": "stackchain/web",
"number": None,
"title": "Unsafe",
"subject_type": "Update",
"state": "",
"url": "",
"subject_url": "",
},
]
@pytest.mark.anyio
async def test_notification_collection_rejects_non_list_payload():
with pytest.raises(ValueError, match="notification response was not a list"):
gitea_proxy._normalize_notifications({"message": "unexpected"})