Files
syops/backend/tests/test_agent_registry_intent_routes.py

76 lines
2.5 KiB
Python

"""
backend/tests/test_agent_registry_intent_routes.py
用途:验证 Intent route 注册表与 normalize/validate 契约。
依赖:agent_registry.intent_routes、chat.intent_route_registry、pytest。
"""
from __future__ import annotations
import pytest
from app.core.errors import ValidationError
from app.services.agent_registry.intent_routes import (
IntentRouteDefinition,
get_intent_route_keys,
list_intent_routes,
normalize_intent_routes_list,
register_intent_route,
validate_intent_routes,
)
from app.services.chat.intent_route_registry import ensure_intent_routes_loaded
@pytest.fixture(autouse=True)
def _load_builtin_routes():
ensure_intent_routes_loaded()
def test_builtin_intent_routes_registered():
"""内置四个 route 应在 chat route 加载后注册。"""
keys = get_intent_route_keys()
assert 'hr_leave_sql' in keys
assert 'plm_oracle' in keys
assert len(keys) >= 4
def test_list_intent_routes_returns_admin_catalog_shape():
"""Admin catalog 应返回 key/label/description 等字段。"""
rows = list_intent_routes()
assert rows
hr = next(item for item in rows if item['key'] == 'hr_leave_sql')
assert hr['label']
assert hr['description']
assert hr['enabled'] is True
assert isinstance(hr['sort_order'], int)
def test_validate_intent_routes_rejects_unknown():
"""Admin 写入应拒绝未注册 route。"""
with pytest.raises(ValidationError, match='无效的 intent_route'):
validate_intent_routes(['not_a_route'])
def test_normalize_intent_routes_list_skips_unknown():
"""回填场景应静默跳过未知 route。"""
normalized = normalize_intent_routes_list(['hr_leave_sql', 'unknown', 'plm_oracle'])
assert normalized == ['hr_leave_sql', 'plm_oracle']
def test_register_intent_route_rejects_empty_key():
"""空 key 注册应失败。"""
with pytest.raises(ValidationError, match='不能为空'):
register_intent_route(IntentRouteDefinition(key='', label='x', description='y'))
def test_register_intent_route_rejects_duplicate():
"""重复注册同一 key 应失败。"""
from app.services.agent_registry import intent_routes as module
key = '__pytest_intent_route_dup__'
module._INTENT_ROUTE_REGISTRY.pop(key, None)
register_intent_route(IntentRouteDefinition(key=key, label='t', description='d'))
with pytest.raises(ValidationError, match='重复'):
register_intent_route(IntentRouteDefinition(key=key, label='t2', description='d2'))
module._INTENT_ROUTE_REGISTRY.pop(key, None)