Files
deerflow-code/offline-backend-20260512/backend/tests/test_roundtable_chain_seat_enrichment.py
T

135 lines
5.4 KiB
Python

"""业务链条「席位富化」(available / live_name)测试。
覆盖(对应 roundtable_chains 路由的读时计算富化,前端选链不再实时查 /api/agents):
- 纯函数 ``_apply_seat_enrichment``:存活标记、改名刷新、功能型内置排除、
id 大小写归一、多条链批量;
- ``_is_functional_agent``:与前端 filterRecommendCandidates 排除集对齐;
- schema 安全:ChainSeatResponseSchema 序列化带富化字段;create 请求的
ChainSeatSchema 即使收到富化字段也不持久化(model_dump 剥掉)。
"""
from __future__ import annotations
import pytest
from app.gateway.routers.roundtable_chains import (
ChainSeatResponseSchema,
ChainSeatSchema,
_apply_seat_enrichment,
_is_functional_agent,
)
def _chain(cid: str, seats: list[dict]) -> dict:
return {"id": cid, "title": cid, "seats": [dict(s) for s in seats]}
def _seat(agent_id: str, name: str = "") -> dict:
return {"agent_id": agent_id, "name": name, "description": ""}
def test_functional_agent_exclusion_set():
"""与前端 filterRecommendCandidates 的排除集逐字对齐(含 coordinator- 前缀)。"""
for aid in (
"roundtable-intent",
"roundtable-recommender",
"roundtable-coordinator",
"roundtable-report",
"Roundtable-Coordinator",
"roundtable-coordinator-abc123",
):
assert _is_functional_agent(aid), aid
# 普通席位(含默认席位 id roundtable-summary)不排除。
for aid in ("agent-abc", "roundtable-intelligence", "roundtable-summary", "my-agent"):
assert not _is_functional_agent(aid), aid
def test_apply_enrichment_marks_available_and_live_name():
chain = _chain("c1", [_seat("a", "旧名"), _seat("b", "环境")])
_apply_seat_enrichment([chain], [{"id": "a", "name": "新名"}, {"id": "b", "name": "环境"}])
a, b = chain["seats"]
assert a["available"] is True
assert a["live_name"] == "新名" # 改名后前端显示实时名而非快照
assert b["available"] is True
assert b["live_name"] == "环境"
def test_apply_enrichment_missing_agent_marked_unavailable():
chain = _chain("c1", [_seat("a"), _seat("gone", "已删除的席位")])
_apply_seat_enrichment([chain], [{"id": "a", "name": "A"}])
a, gone = chain["seats"]
assert a["available"] is True
assert gone["available"] is False
assert gone["live_name"] is None # 前端回退 seat.name 快照
def test_apply_enrichment_functional_agents_unavailable_even_if_alive():
"""功能型内置智能体在 agents 表里存在(seeder 落盘),但不可作席位。"""
chain = _chain(
"c1",
[_seat("a"), _seat("roundtable-intent", "意图理解")],
)
_apply_seat_enrichment(
[chain],
[{"id": "a", "name": "A"}, {"id": "roundtable-intent", "name": "任务理解"}],
)
_, intent = chain["seats"]
assert intent["available"] is False
assert intent["live_name"] is None
def test_apply_enrichment_case_insensitive_and_multi_chain():
"""seat.agent_id 大小写与 agents 表 id 归一(小写)后匹配;多条链一次算完。"""
c1 = _chain("c1", [_seat("Agent-ABC", "快照名")])
c2 = _chain("c2", [_seat("agent-abc"), _seat("zzz")])
_apply_seat_enrichment([c1, c2], [{"id": "agent-abc", "name": "实时名"}])
assert c1["seats"][0]["available"] is True
assert c1["seats"][0]["live_name"] == "实时名"
assert c2["seats"][0]["available"] is True
assert c2["seats"][1]["available"] is False
def test_response_schema_serializes_enrichment_fields():
seat = ChainSeatResponseSchema(
agent_id="a", name="快照", available=True, live_name="实时"
)
dumped = seat.model_dump()
assert dumped["available"] is True
assert dumped["live_name"] == "实时"
def test_create_request_schema_drops_enrichment_fields():
"""create/update 请求收到富化字段也不落库(ChainSeatSchema 未声明 → 剥掉)。"""
seat = ChainSeatSchema.model_validate(
{"agent_id": "a", "name": "x", "available": True, "live_name": "y"}
)
dumped = seat.model_dump()
assert "available" not in dumped
assert "live_name" not in dumped
@pytest.mark.asyncio
async def test_store_roundtrip_keeps_seats_json_clean():
"""富化只改响应 dict;落库 / 回读的 seats JSON 不含富化字段。"""
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from deerflow.persistence.base import Base
from deerflow.persistence.roundtable_chains.model import RoundtableChainRow # noqa: F401 注册表
from deerflow.persistence.roundtable_chains.sql import RoundtableChainRepository
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
try:
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
repo = RoundtableChainRepository(async_sessionmaker(engine, expire_on_commit=False))
row = await repo.create_chain(
"u1", {"id": "c1", "title": "链", "seats": [_seat("a", "A"), _seat("b", "B")]}
)
# 模拟路由层富化(只动响应 dict)。
_apply_seat_enrichment([row], [{"id": "a", "name": "A"}])
# 回读:持久化层不带 available / live_name。
got = await repo.get_chain("c1", "u1")
assert got is not None
assert all("available" not in s and "live_name" not in s for s in got["seats"])
finally:
await engine.dispose()