"""冒烟测试:配置、工具与通道(不需要网络和 API key)。""" from __future__ import annotations import asyncio import pytest from bot042.agent import get_tools as agent_get_tools from bot042.agent import reset_robot from bot042.agent.middlewares import get_tools, move, perceive, robot, speak from bot042.channels import CliChannel from bot042.config import Settings def test_settings_from_env(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setenv("BOT042_MODEL", "openai:test-model") monkeypatch.setenv("BOT042_MAX_STEPS", "12") s = Settings.from_env() assert s.model == "openai:test-model" assert s.max_steps == 12 def test_tools_registered() -> None: names = {t.name for t in get_tools()} assert {"perceive", "move", "speak"} <= names assert agent_get_tools() == get_tools() def test_move_updates_state() -> None: reset_robot() result = asyncio.run(move.ainvoke({"direction": "北", "distance": 2})) assert robot.y == pytest.approx(2.0) assert robot.facing == "north" assert "[移动]" in result bad = asyncio.run(move.ainvoke({"direction": "up", "distance": 1})) assert "[移动失败]" in bad def test_perceive_and_speak() -> None: reset_robot() assert "[感知]" in asyncio.run(perceive.ainvoke({})) assert "[播报]" in asyncio.run(speak.ainvoke({"text": "hello"})) assert robot.log == ["speak: hello"] def test_cli_channel(capsys: pytest.CaptureFixture[str]) -> None: channel = CliChannel() msg = channel.parse_inbound("hello") assert msg.text == "hello" assert msg.channel_subject is not None assert msg.channel_subject.subject_id == "local" async def _send() -> None: await channel.reply_text(msg.channel_subject, "hi") asyncio.run(_send()) assert capsys.readouterr().out.strip() == "hi" def test_cli_channel_input_pump(monkeypatch: pytest.MonkeyPatch) -> None: channel = CliChannel() enqueued: list[str] = [] channel.attach(lambda payload: enqueued.append(payload)) inputs = iter(["ping", "exit"]) monkeypatch.setattr("builtins.input", lambda _prompt="": next(inputs)) async def _run() -> None: await channel.start() await channel.wait_closed() await channel.stop() asyncio.run(_run()) assert enqueued == ["ping"] def test_cli_channel_eof_closes(monkeypatch: pytest.MonkeyPatch) -> None: channel = CliChannel() def raise_eof(_prompt: str = "") -> str: raise EOFError monkeypatch.setattr("builtins.input", raise_eof) async def _run() -> None: await channel.start() await channel.wait_closed() await channel.stop() asyncio.run(_run())