Files
dbx-main/tests/test_project_alerts_api.py
T
king 84113d1fd7 feat(project): silence web alert popup while PC notifier is running
If the user has a non-revoked notifier token seen in the last 3 minutes,
/api/notifications/alerts returns only the baseline with desktop=true;
the web marks it seen so alerts resume on the web after the app stops.
A failing check keeps the web popup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 11:35:14 +09:00

72 lines
3.1 KiB
Python

"""알림 팝업 API — 기준점·태그/배정만·읽지 않은 것·after 이후·오래된 것부터. DB 없이 가짜 저장소로."""
import importlib
import unittest
from unittest.mock import patch
from fastapi import FastAPI
from fastapi.testclient import TestClient
api = importlib.import_module("app.modules.project.router")
ROWS = [ # 최신순(list_notifications 와 같게)
{"id": 9, "type": "completed", "title": "완료", "is_read": False, "task_id": 1, "project_id": 1},
{"id": 8, "type": "mention", "title": "나를 태그함: A", "is_read": False, "task_id": 1, "project_id": 1},
{"id": 7, "type": "assigned", "title": "작업 배정: B", "is_read": True, "task_id": 2, "project_id": 1},
{"id": 6, "type": "assigned", "title": "작업 배정: C", "is_read": False, "task_id": 3, "project_id": 1},
{"id": 2, "type": "assigned", "title": "작업 배정: 옛것", "is_read": False, "task_id": 4, "project_id": 1},
]
class FakeStore:
desktop = False
def notifier_active(self, email):
return self.desktop
def list_notifications(self, recipient_email, unread_only=False, limit=100):
rows = [r for r in ROWS if not unread_only or not r["is_read"]]
return rows[:limit]
class AlertsApiTests(unittest.TestCase):
def setUp(self):
app = FastAPI()
app.include_router(api.router)
self.client = TestClient(app)
def get(self, qs="", desktop=False):
st = FakeStore()
st.desktop = desktop
with patch.object(api, "_require_user", return_value={"email": "Me@Test.local"}), \
patch.object(api, "_db_or_503", return_value=st), \
patch.object(api, "_visible_notifications", side_effect=lambda st, user, items: items):
return self.client.get("/project/api/notifications/alerts" + qs).json()
def test_without_after_returns_only_baseline(self):
out = self.get()
self.assertEqual((out["me"], out["latest_id"], out["alerts"]), ("me@test.local", 9, []))
def test_after_returns_unread_mentions_and_assignments_oldest_first(self):
out = self.get("?after=5")
self.assertEqual([a["id"] for a in out["alerts"]], [6, 8]) # 완료 알림(9)·읽은 것(7)·옛것(2) 제외
self.assertEqual(out["alerts"][1]["type"], "mention")
self.assertNotIn("desktop", out)
def test_pc_notifier_running_silences_web_popup(self):
out = self.get("?after=5", desktop=True)
self.assertEqual((out["alerts"], out["latest_id"], out["desktop"]), ([], 9, True))
def test_notifier_check_failure_keeps_web_popup(self):
class Broken(FakeStore):
def notifier_active(self, email):
raise RuntimeError("db down")
with patch.object(api, "_require_user", return_value={"email": "me@test.local"}), \
patch.object(api, "_db_or_503", return_value=Broken()), \
patch.object(api, "_visible_notifications", side_effect=lambda st, user, items: items):
out = self.client.get("/project/api/notifications/alerts?after=5").json()
self.assertEqual([a["id"] for a in out["alerts"]], [6, 8])
if __name__ == "__main__":
unittest.main()