fix:正常获取Bot自身发送的消息id

This commit is contained in:
SengokuCola
2026-04-05 13:05:18 +08:00
parent ead90cbdf3
commit 18d48e0145
8 changed files with 1147 additions and 46 deletions

View File

@@ -0,0 +1,47 @@
from datetime import datetime
from types import SimpleNamespace
from src.chat.message_receive.message import SessionMessage
from src.common.data_models.mai_message_data_model import MessageInfo, UserInfo
from src.common.data_models.message_component_data_model import MessageSequence, ReplyComponent, TextComponent
from src.maisaka.builtin_tool.context import BuiltinToolRuntimeContext
def _build_sent_message() -> SessionMessage:
message = SessionMessage(
message_id="real-message-id",
timestamp=datetime(2026, 4, 5, 12, 0, 0),
platform="qq",
)
message.message_info = MessageInfo(
user_info=UserInfo(
user_id="bot-qq",
user_nickname="MaiSaka",
user_cardname=None,
),
group_info=None,
additional_config={},
)
message.raw_message = MessageSequence(
[
ReplyComponent(target_message_id="m123"),
TextComponent(text="你好"),
]
)
message.session_id = "test-session"
message.initialized = True
return message
def test_append_sent_message_to_chat_history_keeps_message_id() -> None:
runtime = SimpleNamespace(_chat_history=[])
engine = SimpleNamespace(_get_runtime_manager=lambda: None)
tool_ctx = BuiltinToolRuntimeContext(engine=engine, runtime=runtime)
tool_ctx.append_sent_message_to_chat_history(_build_sent_message())
assert len(runtime._chat_history) == 1
history_message = runtime._chat_history[0]
assert history_message.message_id == "real-message-id"
assert "[msg_id]real-message-id\n" in history_message.raw_message.components[0].text
assert "[msg_id:real-message-id]" in history_message.visible_text

View File

@@ -142,6 +142,46 @@ async def test_text_to_stream_delegates_to_platform_io(monkeypatch: pytest.Monke
]
@pytest.mark.asyncio
async def test_text_to_stream_with_message_returns_sent_message(monkeypatch: pytest.MonkeyPatch) -> None:
fake_manager = _FakePlatformIOManager(
delivery_batch=SimpleNamespace(
has_success=True,
sent_receipts=[
SimpleNamespace(
driver_id="plugin.qq.sender",
external_message_id="real-message-id",
metadata={},
)
],
failed_receipts=[],
route_key=SimpleNamespace(platform="qq"),
)
)
stored_messages: List[Any] = []
monkeypatch.setattr(send_service, "get_platform_io_manager", lambda: fake_manager)
monkeypatch.setattr(send_service, "get_bot_account", lambda platform: "bot-qq")
monkeypatch.setattr(
send_service._chat_manager,
"get_session_by_session_id",
lambda stream_id: _build_private_stream() if stream_id == "test-session" else None,
)
monkeypatch.setattr(
send_service.MessageUtils,
"store_message_to_db",
lambda message: stored_messages.append(message),
)
sent_message = await send_service.text_to_stream_with_message(text="你好", stream_id="test-session")
assert sent_message is not None
assert sent_message.message_id == "real-message-id"
assert fake_manager.ensure_calls == 1
assert len(stored_messages) == 1
assert stored_messages[0].message_id == "real-message-id"
@pytest.mark.asyncio
async def test_text_to_stream_returns_false_when_platform_io_fails(monkeypatch: pytest.MonkeyPatch) -> None:
fake_manager = _FakePlatformIOManager(