diff --git a/app/modules/wechat/wechat.py b/app/modules/wechat/wechat.py index edaaad833..4653cbb02 100644 --- a/app/modules/wechat/wechat.py +++ b/app/modules/wechat/wechat.py @@ -216,7 +216,7 @@ class WeChat: return False if text: formatted_text = text.replace("\n\n", "\n") - content = f"{title}\n{formatted_text}" + content = f"{title}\n{formatted_text}" if title else formatted_text else: assert title is not None content = title diff --git a/tests/test_notification_optional_payloads.py b/tests/test_notification_optional_payloads.py index 83ebf927a..d8f77d351 100644 --- a/tests/test_notification_optional_payloads.py +++ b/tests/test_notification_optional_payloads.py @@ -20,6 +20,7 @@ from app.modules.synologychat.synologychat import SynologyChat from app.modules.vocechat import VoceChatModule from app.modules.vocechat.vocechat import VoceChat from app.modules.wechat import WechatModule +from app.modules.wechat.wechat import WeChat from app.modules.wechat.wechatbot import WeChatBot from app.modules.wechatclawbot import WechatClawBotModule from app.schemas.message import Message, MessageResponse @@ -67,6 +68,25 @@ def test_dingtalk_missing_torrent_does_not_hide_later_candidate(monkeypatch): sent.assert_called_once_with(message, "2. [Demo](https://site.test/details/1)") +@pytest.mark.parametrize(("link", "expected"), [ + (None, "下载完成"), + ("https://mp.test/detail", "下载完成\n点击查看:https://mp.test/detail"), +]) +def test_wechat_text_only_notification_does_not_render_none_title(link, expected, monkeypatch): + """ + 企业微信仅有正文的通知只发送正文,不把缺失的标题渲染为 None。 + """ + client = object.__new__(WeChat) + client._appid = "1" + client._send_msg_url = "https://wechat.test/send" + sent = Mock(return_value=True) + monkeypatch.setattr(client, "_WeChat__get_access_token", Mock(return_value=True)) + monkeypatch.setattr(client, "_WeChat__post_request", sent) + assert client.send_msg(title=None, text="下载完成", link=link) is True + sent.assert_called_once() + assert sent.call_args.args[1]["text"]["content"] == expected + + @pytest.mark.parametrize(("module_class", "scheme", "method", "module_path"), [ (DiscordModule, "discord", "download_discord_file_bytes", "app.modules.discord"), (QQBotModule, "qq", "download_qq_file_bytes", "app.modules.qqbot.module"),