"""ChannelManager-lite 运行时测试。""" from __future__ import annotations import asyncio from typing import Any import pytest from bot042.channels.base import BaseChannel from bot042.channels.manager import ChannelManager from bot042.channels.models import ( ChannelMessage, ChannelSubject, InboundMessage, MessageEvent, OutboundContent, OutboundText, TextContent, ) from bot042.flows.bus import EventBus from bot042.flows.rpc import RpcHub class CollectChannel(BaseChannel): channel_type = "collect" def __init__(self, **kwargs: Any) -> None: super().__init__(**kwargs) self.processed: list[str] = [] async def start(self) -> None: pass async def stop(self) -> None: pass def parse_inbound(self, raw_payload: Any) -> InboundMessage: if isinstance(raw_payload, InboundMessage): return raw_payload return InboundMessage( channel_id=self.channel_id, content=[TextContent(text=str(raw_payload))], channel_subject=ChannelSubject(subject_id="u1"), ) async def _send_text(self, subject: ChannelSubject, text: str) -> None: pass async def _send_content(self, subject: ChannelSubject, parts: list) -> None: pass async def _send_media(self, subject: ChannelSubject, media: Any) -> None: pass async def _echo(msg: InboundMessage): yield MessageEvent.text("echo:" + msg.text) def _msg(text: str, session: str = "u1") -> InboundMessage: return InboundMessage( channel_id="c1", content=[TextContent(text=text)], channel_subject=ChannelSubject(subject_id=session), ) async def _drain(manager: ChannelManager, timeout: float = 2.0) -> None: await manager.wait_idle(timeout=timeout) def test_enqueue_processes_messages() -> None: async def _run() -> tuple[list[str], list[str]]: received: list[str] = [] sent: list[str] = [] async def processor(msg: InboundMessage): received.append(msg.text) yield MessageEvent.text("echo:" + msg.text) async def fake_send(subject: ChannelSubject, text: str) -> None: sent.append(text) manager = ChannelManager(processor=processor) channel = CollectChannel(channel_id="c1") channel._send_text = fake_send # type: ignore[method-assign] await manager.add_channel(channel) await manager.start() manager.enqueue("c1", "ping") await manager.wait_idle(timeout=2) await manager.stop() return received, sent received, sent = asyncio.run(_run()) assert received == ["ping"] assert sent == ["echo:ping"] def test_publish_inbound_records_latest_and_emits() -> None: async def _run() -> ChannelMessage: envelopes: list[ChannelMessage] = [] manager = ChannelManager(publisher=envelopes.append) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) await manager.start() manager.enqueue("c1", "hello") await _drain(manager) latest = manager.get_latest_message("c1") assert latest is not None first = latest.content[0] assert isinstance(first, TextContent) first.text = "被改掉" # 深拷贝:外部改动不影响管理器内部状态 copied = manager.get_latest_message("c1") assert copied is not None and copied.text == "hello" by_subject = manager.get_latest_message("c1", subject_id="u1") assert by_subject is not None and by_subject.text == "hello" assert manager.get_latest_message("nope") is None await manager.stop() return envelopes[0] envelope = asyncio.run(_run()) assert envelope.channel_id == "c1" assert envelope.subject_id == "u1" assert envelope.session_key == "u1" assert envelope.message.text == "hello" def test_send_text_routes_and_rejects_unknown() -> None: async def _run() -> list[str]: sent: list[str] = [] async def fake_send(subject: ChannelSubject, text: str) -> None: sent.append(f"{subject.subject_id}:{text}") manager = ChannelManager() channel = CollectChannel(channel_id="c1") channel._send_text = fake_send # type: ignore[method-assign] await manager.add_channel(channel) await manager.start() manager.enqueue("c1", "seed") # 先登记 subject await _drain(manager) await manager.send_text("c1", "u1", "主动推送") assert [s.subject_id for s in manager.list_subjects("c1")] == ["u1"] with pytest.raises(LookupError, match="未登记主体"): await manager.send_text("c1", "ghost", "x") with pytest.raises(ValueError, match="渠道不存在"): await manager.send_text("nope", "u1", "x") await manager.stop() return sent assert asyncio.run(_run()) == ["u1:主动推送"] def test_rpc_send_dispatches_text_and_content() -> None: async def _run() -> tuple[object, object, list[str]]: sent: list[str] = [] rich: list[int] = [] async def fake_send(subject: ChannelSubject, text: str) -> None: sent.append(text) async def fake_content(subject: ChannelSubject, parts: list) -> None: rich.append(len(parts)) manager = ChannelManager() channel = CollectChannel(channel_id="c1") channel._send_text = fake_send # type: ignore[method-assign] channel._send_content = fake_content # type: ignore[method-assign] await manager.add_channel(channel) await manager.start() manager.enqueue("c1", "seed") await _drain(manager) text_result = await manager.rpc_send( OutboundText(channel_id="c1", subject_id="u1", text="hi") ) content_result = await manager.rpc_send( OutboundContent( channel_id="c1", subject_id="u1", parts=[TextContent(text="rich")], ) ) with pytest.raises(TypeError, match="不支持的出站载荷"): await manager.rpc_send("bad") await manager.stop() return text_result, content_result, sent text_result, content_result, sent = asyncio.run(_run()) assert text_result == {"sent": True} assert content_result == {"sent": True} assert sent == ["hi"] def test_rpc_channel_send_over_bus() -> None: """端到端:任务侧通过 RPC 发消息,registry 作为 server 收到并发送。""" async def _run() -> tuple[object, list[str]]: bus = EventBus() hub = RpcHub(bus) sent: list[str] = [] async def fake_send(subject: ChannelSubject, text: str) -> None: sent.append(text) manager = ChannelManager() channel = CollectChannel(channel_id="c1") channel._send_text = fake_send # type: ignore[method-assign] await manager.add_channel(channel) await manager.start() manager.enqueue("c1", "seed") await _drain(manager) hub.serve("channel.send", manager.rpc_send) result = await hub.call( "channel.send", OutboundText(channel_id="c1", subject_id="u1", text="来自 RPC"), timeout=2, ) hub.close() await manager.stop() return result, sent result, sent = asyncio.run(_run()) assert result == {"sent": True} assert sent == ["来自 RPC"] def test_same_session_batch_merged() -> None: async def _run() -> list[str]: texts: list[str] = [] async def collect(msg: InboundMessage): texts.append(msg.text) yield MessageEvent.text("ok") manager = ChannelManager(processor=collect) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) await manager.start() manager.enqueue("c1", "a") manager.enqueue("c1", "b") manager.enqueue("c1", "c") await _drain(manager) await manager.stop() return texts texts = asyncio.run(_run()) # 同会话连发合批为一回合(内容拼接) assert texts == ["a\nb\nc"] def test_session_lock_serializes_run_in_session() -> None: async def _run() -> list[str]: order: list[str] = [] manager = ChannelManager(processor=_echo) channel = CollectChannel(channel_id="c1") async def slow(msg: InboundMessage): order.append("msg-start") await asyncio.sleep(0.05) order.append("msg-end") yield MessageEvent.text("ok") await manager.add_channel(channel, processor=slow) await manager.start() async def operation() -> None: order.append("op-start") await asyncio.sleep(0.08) order.append("op-end") task = asyncio.create_task(manager.run_in_session("c1", "u1", operation)) await asyncio.sleep(0.01) manager.enqueue("c1", "x") await task await _drain(manager) await manager.stop() return order order = asyncio.run(_run()) # 操作与消息不会交错:要么全 op 先,要么全 msg 先(同一会话锁) assert order in ( ["op-start", "op-end", "msg-start", "msg-end"], ["msg-start", "msg-end", "op-start", "op-end"], ) def test_wait_idle_flushes_pending() -> None: async def _run() -> int: count = 0 async def slow(msg: InboundMessage): nonlocal count await asyncio.sleep(0.02) count += 1 yield MessageEvent.text("ok") manager = ChannelManager(processor=slow) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) await manager.start() for i in range(3): manager.enqueue("c1", _msg(f"m{i}", session=f"u{i}")) await manager.wait_idle(timeout=2) await manager.stop() return count assert asyncio.run(_run()) == 3 def test_status_transitions() -> None: async def _run() -> None: manager = ChannelManager(processor=_echo) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) assert manager.status("c1").reason is None assert manager.status("ghost").reason == "unregistered" await manager.start() await manager.disable_channel("c1") st = manager.status("c1") assert st.connected is False assert st.reason == "disabled" await manager.enable_channel("c1") assert manager.status("c1").reason is None await manager.remove_channel("c1") assert manager.status("c1").reason == "unregistered" await manager.stop() asyncio.run(_run()) def test_pre_lock_handler_called() -> None: async def _run() -> list[str]: seen: list[str] = [] async def hook(channel_id: str, message: InboundMessage) -> None: seen.append(message.text) manager = ChannelManager(processor=_echo) manager.set_pre_lock_handler(hook) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) await manager.start() manager.enqueue("c1", "hello") await _drain(manager) await manager.stop() return seen assert asyncio.run(_run()) == ["hello"] def test_add_channel_duplicate_rejected() -> None: async def _run() -> None: manager = ChannelManager(processor=_echo) await manager.add_channel(CollectChannel(channel_id="c1")) try: await manager.add_channel(CollectChannel(channel_id="c1")) except ValueError as exc: assert "已注册" in str(exc) else: # pragma: no cover raise AssertionError("重复注册应当报错") await manager.stop() asyncio.run(_run()) def test_push_text_routes_to_channel() -> None: async def _run() -> list[str]: sent: list[str] = [] manager = ChannelManager(processor=_echo) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) async def fake_send(subject: ChannelSubject, text: str) -> None: sent.append(text) channel._send_text = fake_send # type: ignore[method-assign] await manager.push_text("c1", ChannelSubject(subject_id="u1"), "pushed") await manager.stop() return sent assert asyncio.run(_run()) == ["pushed"] def test_session_lock_reclaimed_when_idle() -> None: """会话结束后锁条目立即回收,锁表规模不随历史会话数增长。""" async def _run() -> tuple[int, int, int]: manager = ChannelManager(processor=_echo) await manager.add_channel(CollectChannel(channel_id="c1")) async def noop() -> None: return None for i in range(200): await manager.run_in_session("c1", f"u{i}", noop) after_idle = len(manager._session_locks) gate = asyncio.Event() async def holder_op() -> None: await gate.wait() holder = asyncio.create_task(manager.run_in_session("c1", "busy", holder_op)) await asyncio.sleep(0.01) while_busy = len(manager._session_locks) gate.set() await holder return after_idle, while_busy, len(manager._session_locks) # 200 个已结束会话不留残留;在途会话只占 1 个条目,结束后归零 assert asyncio.run(_run()) == (0, 1, 0) def test_session_lock_kept_while_waiter_pending() -> None: """有条目被等待时不能回收,否则新协程会拿到新锁破坏互斥。""" async def _run() -> tuple[list[str], int]: manager = ChannelManager(processor=_echo) await manager.add_channel(CollectChannel(channel_id="c1")) order: list[str] = [] gate = asyncio.Event() async def holder_op() -> None: order.append("holder-enter") await gate.wait() order.append("holder-exit") async def waiter_op() -> None: order.append("waiter-enter") holder = asyncio.create_task(manager.run_in_session("c1", "u1", holder_op)) await asyncio.sleep(0.01) waiter = asyncio.create_task(manager.run_in_session("c1", "u1", waiter_op)) await asyncio.sleep(0.01) # 等待者阻塞期间条目必须仍在,且两者严格串行 assert len(manager._session_locks) == 1 gate.set() await asyncio.gather(holder, waiter) return order, len(manager._session_locks) order, remaining = asyncio.run(_run()) assert order == ["holder-enter", "holder-exit", "waiter-enter"] assert remaining == 0 def test_batching_keeps_channel_fifo_across_sessions() -> None: """合批探测到的跨会话消息不能被推回队尾(渠道内保持 FIFO)。""" async def _run() -> list[str]: processed: list[str] = [] async def processor(msg: InboundMessage): processed.append(msg.text) yield MessageEvent.text("ok") manager = ChannelManager(processor=processor) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) await manager.start() for text, session in (("a", "u1"), ("b", "u2"), ("c", "u1"), ("d", "u2")): manager.enqueue("c1", _msg(text, session)) await manager.wait_idle(timeout=2) await manager.stop() return processed assert asyncio.run(_run()) == ["a", "b", "c", "d"] def test_deferred_counter_released_when_channel_stops() -> None: """禁用渠道取消 worker 时,暂存计数必须归还(否则 wait_idle 永不返回)。""" async def _run() -> tuple[int, int]: gate = asyncio.Event() async def processor(msg: InboundMessage): if msg.text == "slow": await gate.wait() yield MessageEvent.text("ok") manager = ChannelManager(processor=processor) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) await manager.start() manager.enqueue("c1", _msg("slow", "u1")) manager.enqueue("c1", _msg("carried", "u2")) # 等 worker 取走 u1(u2 被合批探测暂存)并卡在 slow 上 for _ in range(200): if manager._deferred: break await asyncio.sleep(0.01) assert manager._deferred == 1 await manager.disable_channel("c1") leaked = manager._deferred await manager.wait_idle(timeout=2) await manager.stop() return leaked, manager._deferred assert asyncio.run(_run()) == (0, 0) def test_enable_channel_is_idempotent() -> None: """重复启用不能叠加 worker 组(否则消息会被重复处理)。""" async def _run() -> tuple[int, list[str]]: processed: list[str] = [] async def processor(msg: InboundMessage): processed.append(msg.text) yield MessageEvent.text("ok") manager = ChannelManager(processor=processor) channel = CollectChannel(channel_id="c1") await manager.add_channel(channel) await manager.start() await manager.enable_channel("c1") await manager.enable_channel("c1") workers = len(manager._workers["c1"]) manager.enqueue("c1", _msg("hi", "u1")) await manager.wait_idle(timeout=2) await manager.stop() return workers, processed workers, processed = asyncio.run(_run()) assert workers == 1 assert processed == ["hi"]