Files
nanobot/tests/channels/test_qq_compaction_notices.py
T
AlfredChaos 24c41a6af0 fix(channels): drop compaction notices on channels without an in-place affordance (#5799)
* fix(channels): drop compaction notices on channels without an in-place affordance

QQ's C2C/group message API has no edit or recall endpoint, so the
compaction lifecycle reached the chat as two permanent standalone
messages. Add a show_compaction_notices capability that defaults to
true (Telegram/Discord/WebSocket keep their in-place lifecycle) and
opt QQ out, with a per-channel showCompactionNotices config override.

Fixes #5784

* fix(qq): keep compaction-notice suppression inside the QQ channel

Per review on #5799: drop ContextCompactionEvent in QQChannel.send()
based on a QQ-local QQConfig.show_compaction_notices (default false,
camel alias showCompactionNotices) instead of a cross-channel
BaseChannel capability plus manager-level configuration and dispatch
policy. BaseChannel, ChannelManager and the manager compaction test
return to their upstream shape.

Also adds the missing zh-TW locale copy (plus es/id/ko/vi so all ten
QQ locales carry the new setup field) and QQ-level tests covering the
default drop, the enabled path, ordinary-message passthrough, and the
camelCase config override.
2026-09-18 00:45:58 +08:00

74 lines
2.5 KiB
Python

"""QQ drops compaction notices it cannot present as one message.
QQ's C2C/group message API has no edit or recall endpoint, so the
compaction lifecycle would land as two separate permanent messages. The
channel drops ``ContextCompactionEvent`` by default; ``showCompactionNotices:
true`` restores the notices for anyone who wants them (#5784).
"""
from __future__ import annotations
from unittest.mock import AsyncMock
import pytest
pytest.importorskip("botpy")
from nanobot.bus.events import OutboundMessage
from nanobot.bus.outbound_events import ContextCompactionEvent, outbound_message_for_event
from nanobot.bus.queue import MessageBus
from nanobot.channels.qq.runtime import QQChannel, QQConfig
def _make_channel(**config_kwargs) -> QQChannel:
config = QQConfig(app_id="test_app", secret="test_secret", **config_kwargs)
channel = QQChannel(config, MessageBus())
channel._client = object() # truthy: pass the initialized check
return channel
@pytest.mark.asyncio
async def test_compaction_notices_dropped_by_default(monkeypatch) -> None:
channel = _make_channel()
send_text = AsyncMock()
monkeypatch.setattr(channel, "_send_text_only", send_text)
for phase in ("started", "succeeded"):
await channel.send(outbound_message_for_event(
channel="qq", chat_id="chat",
event=ContextCompactionEvent(compaction_id="c1", phase=phase),
))
send_text.assert_not_awaited()
@pytest.mark.asyncio
async def test_compaction_notices_sent_when_enabled(monkeypatch) -> None:
channel = _make_channel(show_compaction_notices=True)
send_text = AsyncMock()
monkeypatch.setattr(channel, "_send_text_only", send_text)
await channel.send(outbound_message_for_event(
channel="qq", chat_id="chat",
event=ContextCompactionEvent(compaction_id="c1", phase="started"),
))
send_text.assert_awaited_once()
assert send_text.await_args.kwargs["content"] == "Compressing context…"
@pytest.mark.asyncio
async def test_ordinary_messages_unaffected(monkeypatch) -> None:
channel = _make_channel()
send_text = AsyncMock()
monkeypatch.setattr(channel, "_send_text_only", send_text)
await channel.send(OutboundMessage(channel="qq", chat_id="chat", content="hello"))
send_text.assert_awaited_once()
def test_config_accepts_camel_case_override() -> None:
config = QQConfig.model_validate({"appId": "a", "secret": "s", "showCompactionNotices": True})
assert config.show_compaction_notices is True