feat(notification): 通知标题和正文支持多语言
- 通知静态配置支持 title/body i18n - 前端通知列表和详情页展示本地化内容 - 新增数据库迁移脚本 - 更新通知协议文档
This commit is contained in:
@@ -5,7 +5,12 @@ from uuid import UUID, uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from v1.notifications.service import NotificationService, _parse_payload
|
||||
from v1.notifications.service import (
|
||||
NotificationService,
|
||||
_parse_payload,
|
||||
resolve_i18n_text,
|
||||
normalize_locale,
|
||||
)
|
||||
from v1.notifications.schemas import (
|
||||
NotificationPayloadNone,
|
||||
NotificationPayloadRoute,
|
||||
@@ -39,8 +44,8 @@ class _FakeNotification:
|
||||
*,
|
||||
id: UUID,
|
||||
type: str = "system",
|
||||
title: str = "Test",
|
||||
body: str = "Test body",
|
||||
title: dict[str, str] | None = None,
|
||||
body: dict[str, str] | None = None,
|
||||
payload: dict | None = None,
|
||||
status: str = "published",
|
||||
deleted_at: datetime | None = None,
|
||||
@@ -48,8 +53,8 @@ class _FakeNotification:
|
||||
):
|
||||
self.id = id
|
||||
self.type = type
|
||||
self.title = title
|
||||
self.body = body
|
||||
self.title = title or {"zh": "Test"}
|
||||
self.body = body or {"zh": "Test body"}
|
||||
self.payload = payload or {"action": "none"}
|
||||
self.status = status
|
||||
self.deleted_at = deleted_at
|
||||
@@ -154,8 +159,8 @@ def _make_notification(
|
||||
notification_id: UUID | None = None,
|
||||
is_read: bool = False,
|
||||
read_at: datetime | None = None,
|
||||
title: str = "Test",
|
||||
body: str = "Test body",
|
||||
title: dict[str, str] | None = None,
|
||||
body: dict[str, str] | None = None,
|
||||
payload: dict | None = None,
|
||||
status: str = "published",
|
||||
deleted_at: datetime | None = None,
|
||||
@@ -185,8 +190,12 @@ class TestListNotifications:
|
||||
async def test_returns_only_user_a_notifications(
|
||||
self, service: NotificationService, fake_repo: _FakeNotificationRepository
|
||||
):
|
||||
un_a, n_a = _make_notification(user_id=USER_A, title="A1")
|
||||
un_b, n_b = _make_notification(user_id=USER_B, title="B1")
|
||||
un_a, n_a = _make_notification(
|
||||
user_id=USER_A, title={"zh": "A1"}, body={"zh": "A1 body"},
|
||||
)
|
||||
un_b, n_b = _make_notification(
|
||||
user_id=USER_B, title={"zh": "B1"}, body={"zh": "B1 body"},
|
||||
)
|
||||
fake_repo.add_item(un_a, n_a)
|
||||
fake_repo.add_item(un_b, n_b)
|
||||
|
||||
@@ -219,7 +228,9 @@ class TestListNotifications:
|
||||
self, service: NotificationService, fake_repo: _FakeNotificationRepository
|
||||
):
|
||||
for i in range(3):
|
||||
un, n = _make_notification(user_id=USER_A, title=f"N{i}")
|
||||
un, n = _make_notification(
|
||||
user_id=USER_A, title={"zh": f"N{i}"}, body={"zh": f"N{i} body"},
|
||||
)
|
||||
fake_repo.add_item(un, n)
|
||||
|
||||
result = await service.list_notifications(user_id=USER_A, limit=2)
|
||||
@@ -383,3 +394,75 @@ class TestParsePayload:
|
||||
assert payload.route == "/settings"
|
||||
assert payload.entity_id is None
|
||||
assert payload.tab is None
|
||||
|
||||
|
||||
class TestResolveI18nText:
|
||||
def test_exact_locale_match(self):
|
||||
text = resolve_i18n_text({"zh": "你好", "en": "Hello"}, "en")
|
||||
assert text == "Hello"
|
||||
|
||||
def test_falls_back_to_default(self):
|
||||
text = resolve_i18n_text({"zh": "你好", "en": "Hello"}, "zh_Hant")
|
||||
assert text == "你好"
|
||||
|
||||
def test_returns_empty_when_default_missing(self):
|
||||
text = resolve_i18n_text({"en": "Hello"}, "zh_Hant")
|
||||
assert text == ""
|
||||
|
||||
def test_empty_dict(self):
|
||||
text = resolve_i18n_text({}, "en")
|
||||
assert text == ""
|
||||
|
||||
|
||||
class TestNormalizeLocale:
|
||||
def test_known_locale_passthrough(self):
|
||||
assert normalize_locale("zh") == "zh"
|
||||
assert normalize_locale("zh_Hant") == "zh_Hant"
|
||||
assert normalize_locale("en") == "en"
|
||||
|
||||
def test_none_returns_default(self):
|
||||
assert normalize_locale(None) == "zh"
|
||||
|
||||
def test_zh_cn_maps_to_zh(self):
|
||||
assert normalize_locale("zh_CN") == "zh"
|
||||
assert normalize_locale("zh_Hans") == "zh"
|
||||
|
||||
def test_zh_tw_maps_to_hant(self):
|
||||
assert normalize_locale("zh_TW") == "zh_Hant"
|
||||
assert normalize_locale("zh-Hant") == "zh_Hant"
|
||||
|
||||
def test_unknown_returns_default(self):
|
||||
assert normalize_locale("fr") == "zh"
|
||||
assert normalize_locale("ja") == "zh"
|
||||
|
||||
|
||||
class TestListNotificationsI18n:
|
||||
@pytest.mark.asyncio
|
||||
async def test_locale_en_returns_english(
|
||||
self, service: NotificationService, fake_repo: _FakeNotificationRepository
|
||||
):
|
||||
un, n = _make_notification(
|
||||
user_id=USER_A,
|
||||
title={"zh": "你好", "en": "Hello"},
|
||||
body={"zh": "正文", "en": "Body"},
|
||||
)
|
||||
fake_repo.add_item(un, n)
|
||||
|
||||
result = await service.list_notifications(user_id=USER_A, locale="en")
|
||||
assert result.items[0].title == "Hello"
|
||||
assert result.items[0].body == "Body"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_locale_zh_hant_falls_back_to_zh(
|
||||
self, service: NotificationService, fake_repo: _FakeNotificationRepository
|
||||
):
|
||||
un, n = _make_notification(
|
||||
user_id=USER_A,
|
||||
title={"zh": "你好", "en": "Hello"},
|
||||
body={"zh": "正文", "en": "Body"},
|
||||
)
|
||||
fake_repo.add_item(un, n)
|
||||
|
||||
result = await service.list_notifications(user_id=USER_A, locale="zh_Hant")
|
||||
assert result.items[0].title == "你好"
|
||||
assert result.items[0].body == "正文"
|
||||
|
||||
Reference in New Issue
Block a user