Include utility modules, shared channels, prompts, read-only skills, wardrobe memory and regression tests. Externalize legacy credentials and exclude private runtime state.
52 lines
2.1 KiB
Python
52 lines
2.1 KiB
Python
"""Notification delivery must survive chat-model outages with an explicit marker."""
|
|
|
|
from pathlib import Path
|
|
import sys
|
|
import unittest
|
|
from unittest.mock import AsyncMock, patch
|
|
|
|
BASE = Path(__file__).resolve().parents[1]
|
|
sys.path[:0] = [str(BASE / "bot-utility"), str(BASE)]
|
|
|
|
import notification_render
|
|
import notifications
|
|
from chat import engine
|
|
|
|
|
|
class NotificationApiFallbackTests(unittest.IsolatedAsyncioTestCase):
|
|
def event(self):
|
|
return notifications.Event(
|
|
kind="komari",
|
|
dedupe_key="komari:api-test",
|
|
legacy_text="[Komari] 服务器「Ama-10」当前处于离线状态。",
|
|
uid=1,
|
|
payload={"server": "Ama-10", "status": "offline"},
|
|
)
|
|
|
|
async def test_chat_api_failure_returns_raw_notice_with_marker(self):
|
|
with patch.object(engine, "call_deepseek", new=AsyncMock(return_value=None)):
|
|
rendered = await notification_render.render_event(self.event(), "chat", 10001)
|
|
self.assertEqual(
|
|
rendered,
|
|
"[Komari] 服务器「Ama-10」当前处于离线状态。\n\n【聊天模式 API 暂时不可用】",
|
|
)
|
|
|
|
async def test_command_mode_never_gets_chat_api_marker(self):
|
|
with patch.object(engine, "call_deepseek", new=AsyncMock(return_value=None)):
|
|
rendered = await notification_render.render_event(self.event(), "command", 10001)
|
|
self.assertEqual(rendered, self.event().legacy_text)
|
|
|
|
async def test_successful_render_has_no_api_marker(self):
|
|
with patch.object(engine, "call_deepseek", new=AsyncMock(return_value="Ama-10 掉线了。")):
|
|
rendered = await notification_render.render_event(self.event(), "chat", 10001)
|
|
self.assertEqual(rendered, "Ama-10 掉线了。")
|
|
self.assertNotIn(notification_render.API_UNAVAILABLE_NOTICE, rendered)
|
|
|
|
def test_marker_is_not_duplicated(self):
|
|
text = "原文\n\n" + notification_render.API_UNAVAILABLE_NOTICE
|
|
self.assertEqual(notification_render._with_api_notice(text, "chat"), text)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|