fix: detect missed recipient names and expose QQ mention diagnostics

This commit is contained in:
itxiaohui committed 2026-10-06 17:49:20 +08:00
1 parent f89aadb5da
commit 9c5b87772f
9 files changed
+277 -9

No files matched your search

+89
View File
@@ -0,0 +1,89 @@
import json
from pathlib import Path
from types import SimpleNamespace
from test_reminders import plugin as plugin
from test_reminders import record
from services.diagnostics import VERSION, diagnose, identity
async def test_diagnostic_uses_actual_payload_and_does_not_create_task(plugin, event_factory):
instance, _ = plugin
event = event_factory(
"/tx diagnose 1分钟 测试 <@other_generic>",
raw=SimpleNamespace(
group_openid="group_1",
mentions=[SimpleNamespace(member_openid=None)],
raw_data={
"content": "/tx diagnose 1分钟 测试 <@other_generic>",
"mentions": [
{"id": "other_generic", "member_openid": "other_member", "username": "小明"}
],
"token": "must_not_be_printed",
"author": {"member_openid": "user_1"},
},
),
)
await instance.tx(event)
result, _ = json.JSONDecoder().raw_decode(event.replies[0].split("\n", 1)[1])
assert result["插件版本"] == VERSION
assert result["解析结果"]["对象"] == identity("other_member")
assert not result["解析结果"]["提醒自己"]
assert result["QQ原始mentions"][0]["id"] == identity("other_generic")
assert result["SDK mentions"] == [{}]
assert "other_generic" not in event.replies[0] and "other_member" not in event.replies[0]
assert "must_not_be_printed" not in event.replies[0]
assert "<@" not in event.replies[0]
assert not await instance.store.list(record()["scope"])
async def test_diagnostic_explains_missing_member_id(plugin, event_factory):
instance, _ = plugin
event = event_factory(
"/tx diagnose 1分钟 测试 @小明",
raw={"group_openid": "group_1", "content": "1分钟 测试 @小明"},
)
result = json.loads(
await diagnose(event, "1分钟 测试 @小明", instance.store, record()["scope"])
)
assert result["QQ原始mentions"] == [] and result["At消息段"] == []
assert "还未识别" in result["解析失败"]
async def test_status_reports_installed_plugin_version(plugin, event_factory):
instance, _ = plugin
event = event_factory("/tx status")
await instance.tx(event)
assert f"插件版本:{VERSION}" in event.replies[0]
assert f"version: {VERSION}" in (
Path(__file__).resolve().parents[1] / "metadata.yaml"
).read_text(encoding="utf-8")
async def test_saved_reminder_diagnostic_compares_creator_and_target_without_sending(
plugin, event_factory
):
instance, platform = plugin
number = await instance.store.add(record(target_id="other_member", target_name="xh"), 10)
event = event_factory(f"/tx diagnose {number}")
await instance.tx(event)
assert f"保存对象:xh / {identity('other_member')}" in event.replies[0]
assert f"到期 @ 使用:{identity('other_member')}" in event.replies[0]
assert "发起人与对象 ID 相同:否" in event.replies[0]
assert "other_member" not in event.replies[0] and "user_1" not in event.replies[0]
assert len(await instance.store.list(record()["scope"])) == 1
platform.get_client().api.post_group_message.assert_not_called()
async def test_reminder_diagnostic_respects_creator_and_group_access(plugin, event_factory):
instance, _ = plugin
number = await instance.store.add(record(creator_id="another_creator"), 10)
event = event_factory(f"/tx diagnose {number}")
await instance.tx(event)
assert "没有查看权限" in event.replies[0]
assert "保存对象" not in event.replies[0]
event = event_factory(f"/tx diagnose {number}", group="another_group")
event.admin = True
await instance.tx(event)
assert "找不到该提醒" in event.replies[0]
+44
View File
@@ -165,3 +165,47 @@ async def test_at_component_name_is_learned(plugin, event_factory):
event.messages = [SimpleNamespace(qq="target_member", name="小明", type="At")]
await observe_members(event, instance.store, record()["scope"])
assert (await instance.store.member(record()["scope"], "小明"))["user_id"] == "target_member"
@pytest.mark.parametrize(
"text",
[
"/tx 1分钟 开会@小明",
"/tx 1分钟 开会@小明",
"@小明 /tx 1分钟 开会",
"@小明 /提醒 1分钟 开会",
"@小明 /remind 1分钟 开会",
],
)
async def test_adjacent_and_before_command_nicknames_are_recipients(plugin, event_factory, text):
instance, platform = plugin
await instance.store.observe(record()["scope"], "target_member", "小明")
event = event_factory(text)
await instance.tx(event)
rem = (await instance.store.list(record()["scope"]))[0]
assert rem["target_id"] == "target_member" and rem["content"] == "开会"
await instance.store._run(
lambda db: db.execute(
"UPDATE reminders SET remind_at=? WHERE id=?", (time.time() - 1, rem["id"])
)
)
await instance._check_due()
body = platform.get_client().api.post_group_message.call_args.kwargs["markdown"]["content"]
assert body.startswith('<qqbot-at-user id="target_member" />') and 'id="user_1"' not in body
async def test_adjacent_unknown_nickname_is_not_silently_self(plugin, event_factory):
instance, _ = plugin
event = event_factory("/tx 1分钟 开会@陌生人")
await instance.tx(event)
assert "未创建提醒" in event.replies[0]
assert not await instance.store.list(record()["scope"])
async def test_email_in_reminder_content_is_not_a_recipient(plugin, event_factory):
instance, _ = plugin
event = event_factory("/tx 1分钟 给 test@example.com 发邮件")
await instance.tx(event)
rem = (await instance.store.list(record()["scope"]))[0]
assert rem["target_id"] == rem["creator_id"]
assert rem["content"] == "给 test@example.com 发邮件"
+10 -3
View File
@@ -57,7 +57,7 @@ async def test_self_reminder_has_one_native_mention_without_visible_id(platform_
platform = platform_factory()
await deliver(platform, rem)
sent = platform.get_client().api.post_group_message.call_args.kwargs["markdown"]["content"]
assert sent == '<qqbot-at-user id="MEMBER_OPENID" />\n\n⏰ 吃饭\n\n(提醒 \\#1)'
assert sent == '<qqbot-at-user id="MEMBER_OPENID" />\n\n⏰ 吃饭\n\n(提醒 #1)'
@pytest.mark.parametrize("sdk_object", [False, True])
@@ -132,7 +132,7 @@ async def test_approved_template_keeps_native_mention_and_reply_context(platform
"params": [
{
"key": "body",
"values": ['<qqbot-at-user id="user_1" />\n\n⏰ 吃饭\n\n(提醒 \\#1)'],
"values": ['<qqbot-at-user id="user_1" />\n\n⏰ 吃饭\n\n(提醒 #1)'],
}
],
}
@@ -146,7 +146,14 @@ def test_markdown_preserves_literal_content_and_keeps_mention_outside_code():
assert body.startswith('<qqbot-at-user id="user_1" />\n\n')
assert "\\`\\`\\`" in body and "\\*\\*测试\\*\\*" in body
assert "<qqbot-at-everyone" not in body
assert "&lt;qqbot\\-at\\-everyone /&gt;" in body
assert "&lt;qqbot-at-everyone /&gt;" in body
def test_reported_display_name_and_reminder_number_have_no_added_backslashes():
body = reminder_markdown({**record(creator_name="小慧(代码版)", target_id="xh"), "id": 14})
assert body.startswith('<qqbot-at-user id="xh" />')
assert "来自 小慧(代码版) 的提醒" in body and "(提醒 #14)" in body
assert "\\(" not in body and "\\#" not in body
async def test_md_rejection_never_falls_back_to_literal_tag(platform_factory):