stackchain-dashboard/tests/test_following_api.py
timmy d3ba5975a0
All checks were successful
CI / lint (pull_request) Successful in 4m13s
CI / build-release (pull_request) Successful in 9s
CI / browser-journey (pull_request) Successful in 7m9s
CI / release-candidate (pull_request) Has been skipped
refactor: make Following sync outcome explicit
2026-08-23 06:23:57 +00:00

183 lines
6.9 KiB
Python

import httpx
import pytest
from src import main
from src.following_store import FollowingStore
@pytest.mark.anyio
async def test_confirmed_watch_updates_account_following_collection(monkeypatch, tmp_path):
store = FollowingStore(tmp_path / "following.sqlite3", encryption_key=b"a" * 32)
detail = {
"repository": "stackchain/api",
"kind": "issue",
"number": 42,
"title": "Make mobile review useful",
"state": "open",
"updated_at": "2026-08-23T03:00:00Z",
"url": "https://forge.example/stackchain/api/issues/42",
"claimable": True,
}
async def preview(repository, kind, number):
assert (repository, kind, number) == ("stackchain/api", "issue", 42)
return detail
async def set_subscription(repository, number, watching):
return {"watching": watching}
async def user():
return {"login": "Timmy"}
monkeypatch.setattr(main, "_following_store", lambda: store, raising=False)
monkeypatch.setattr(main.gitea_proxy, "work_preview", preview)
monkeypatch.setattr(main.gitea_proxy, "set_issue_subscription", set_subscription)
monkeypatch.setattr(main, "current_user", user)
transport = httpx.ASGITransport(app=main.app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
watched = await client.put(
"/api/v1/repos/stackchain/api/issues/42/preview/subscription?kind=issue"
)
following = await client.get("/api/v1/following")
assert watched.status_code == 200
assert watched.json() == {
"watching": True,
"following_synced": True,
"following_revision": 1,
"following_count": 1,
}
assert following.status_code == 200
assert following.headers["cache-control"] == "no-store"
assert following.json() == {"revision": 1, "items": [{
"repository": "stackchain/api",
"number": 42,
"title": "Make mobile review useful",
"state": "open",
"updated_at": "2026-08-23T03:00:00Z",
"url": "https://forge.example/stackchain/api/issues/42",
}]}
@pytest.mark.anyio
async def test_full_following_collection_rejects_watch_before_gitea_mutation(monkeypatch, tmp_path):
store = FollowingStore(tmp_path / "following.sqlite3", encryption_key=b"b" * 32, limit=1)
store.set_watching("timmy", {
"repository": "stackchain/api", "number": 7, "title": "Already followed",
"state": "open", "updated_at": "2026-08-23T02:00:00Z",
"url": "https://forge.example/stackchain/api/issues/7",
}, True)
calls = []
async def preview(repository, kind, number):
return {
"repository": repository, "kind": kind, "number": number,
"title": "Another issue", "state": "open",
"updated_at": "2026-08-23T03:00:00Z",
"url": "https://forge.example/stackchain/api/issues/42", "claimable": True,
}
async def set_subscription(*args):
calls.append(args)
return {"watching": True}
async def user():
return {"login": "timmy"}
monkeypatch.setattr(main, "_following_store", lambda: store)
monkeypatch.setattr(main.gitea_proxy, "work_preview", preview)
monkeypatch.setattr(main.gitea_proxy, "set_issue_subscription", set_subscription)
monkeypatch.setattr(main, "current_user", user)
transport = httpx.ASGITransport(app=main.app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
response = await client.put(
"/api/v1/repos/stackchain/api/issues/42/preview/subscription?kind=issue"
)
assert response.status_code == 503
assert calls == []
assert store.get("timmy")["items"][0]["number"] == 7
@pytest.mark.anyio
async def test_following_write_failure_compensates_confirmed_gitea_watch(monkeypatch, tmp_path):
store = FollowingStore(tmp_path / "following.sqlite3", encryption_key=b"c" * 32)
calls = []
async def preview(repository, kind, number):
return {
"repository": repository, "kind": kind, "number": number,
"title": "Recoverable watch", "state": "open",
"updated_at": "2026-08-23T03:00:00Z",
"url": "https://forge.example/stackchain/api/issues/42", "claimable": True,
}
async def set_subscription(repository, number, watching):
calls.append(watching)
return {"watching": watching}
async def user():
return {"login": "timmy"}
def fail_write(*args):
raise OSError("disk unavailable")
monkeypatch.setattr(main, "_following_store", lambda: store)
monkeypatch.setattr(main.gitea_proxy, "work_preview", preview)
monkeypatch.setattr(main.gitea_proxy, "set_issue_subscription", set_subscription)
monkeypatch.setattr(main, "current_user", user)
monkeypatch.setattr(store, "set_watching", fail_write)
transport = httpx.ASGITransport(app=main.app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
response = await client.put(
"/api/v1/repos/stackchain/api/issues/42/preview/subscription?kind=issue"
)
assert response.status_code == 503
assert response.json() == {"error": "Watch status was not changed. Please retry."}
assert calls == [True, False]
@pytest.mark.anyio
async def test_uncompensated_following_write_reports_authoritative_partial_outcome(monkeypatch, tmp_path):
store = FollowingStore(tmp_path / "following.sqlite3", encryption_key=b"d" * 32)
calls = []
async def preview(repository, kind, number):
return {
"repository": repository, "kind": kind, "number": number,
"title": "Split watch", "state": "open",
"updated_at": "2026-08-23T03:00:00Z",
"url": "https://forge.example/stackchain/api/issues/42", "claimable": True,
}
async def set_subscription(repository, number, watching):
calls.append(watching)
return {"watching": True}
async def user():
return {"login": "timmy"}
monkeypatch.setattr(main, "_following_store", lambda: store)
monkeypatch.setattr(main.gitea_proxy, "work_preview", preview)
monkeypatch.setattr(main.gitea_proxy, "set_issue_subscription", set_subscription)
monkeypatch.setattr(main, "current_user", user)
monkeypatch.setattr(store, "set_watching", lambda *args: (_ for _ in ()).throw(OSError()))
transport = httpx.ASGITransport(app=main.app)
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
response = await client.put(
"/api/v1/repos/stackchain/api/issues/42/preview/subscription?kind=issue"
)
assert response.status_code == 200
assert response.json() == {
"watching": True,
"following_synced": False,
"error": "Watching in Gitea, but Following could not sync. Retry this action.",
}
assert calls == [True, False]