- agent: 新增 base.py 执行基类、execution.py 执行器、web.py 网络工具, middlewares/steering.py 中间件;移除旧的 agent.py - channels: 新增 ChannelManager、registry、models 与 IM 渠道(企业微信), 扩展 base.py - flows: 新增 agent_task / bus / cron / scheduler / service / stream / rpc / processor / tasks / model_settings / builtin;移除旧的 task.py - db: 新增 sqlite schema 与 repo 层 - docs: 新增架构说明、Channel × EventBus 设计、任务编写教程 - 依赖新增 aiosqlite / httpx / langgraph / primp / pydantic / ripgrep / wecom-aibot-sdk;新增 bot 命令入口 - 测试:232 passed
94 lines
2.7 KiB
Python
94 lines
2.7 KiB
Python
"""冒烟测试:配置、工具与通道(不需要网络和 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())
|