153 lines
5.3 KiB
Python
153 lines
5.3 KiB
Python
"""
|
||
tests/test_agent_registry_admin_payload.py
|
||
|
||
用途:Admin Agent Registry 载荷校验与 extra 清理单元测试。
|
||
"""
|
||
import pytest
|
||
|
||
from app.core.errors import ValidationError
|
||
from app.models import Agent
|
||
from app.services.agent_registry.admin_payload import (
|
||
MIGRATED_EXTRA_REGISTRY_KEYS,
|
||
apply_agent_payload,
|
||
strip_migrated_extra_registry_keys,
|
||
validate_agent_payload,
|
||
)
|
||
|
||
|
||
def test_validate_generic_dify_agent_payload():
|
||
normalized = validate_agent_payload({
|
||
'name': 'Generic Bot',
|
||
'agent_code': 'generic',
|
||
'runtime_provider': 'dify',
|
||
'conversation_mode': 'chat',
|
||
'dify_app_id': 'app-123',
|
||
'dify_api_key': 'sk-test',
|
||
})
|
||
assert normalized['agent_code'] == 'generic'
|
||
assert normalized['runtime_provider'] == 'dify'
|
||
assert normalized['type'] == 'api'
|
||
|
||
|
||
def test_validate_rejects_unknown_agent_code():
|
||
with pytest.raises(ValidationError, match='agent_code'):
|
||
validate_agent_payload({'name': 'Bad', 'agent_code': 'unknown_type'})
|
||
|
||
|
||
def test_validate_file_check_requires_bound_schedule_task_id():
|
||
with pytest.raises(ValidationError, match='bound_schedule_task_id'):
|
||
validate_agent_payload({
|
||
'name': 'File Check',
|
||
'agent_code': 'file_check',
|
||
'runtime_provider': 'dify',
|
||
'dify_app_id': 'app-fc',
|
||
'bound_schedule_task_id': None,
|
||
})
|
||
|
||
|
||
def test_validate_iframe_requires_url():
|
||
with pytest.raises(ValidationError, match='iframe_url'):
|
||
validate_agent_payload({
|
||
'name': 'Embed',
|
||
'runtime_provider': 'iframe',
|
||
'conversation_mode': 'embedded',
|
||
})
|
||
|
||
|
||
def test_apply_file_check_writes_columns_and_strips_migrated_extra(app):
|
||
"""双写已关闭:结构化列写入 ORM,extra 不再 mirror Registry 键。"""
|
||
with app.app_context():
|
||
normalized = validate_agent_payload({
|
||
'name': 'RoHS Check',
|
||
'agent_code': 'file_check',
|
||
'runtime_provider': 'dify',
|
||
'dify_app_id': 'app-fc',
|
||
'bound_schedule_task_id': 42,
|
||
'handler_config': {
|
||
'poll_interval_seconds': 3,
|
||
'lock_input_when_running': False,
|
||
},
|
||
'extra_config': {
|
||
'agent_code': 'legacy-code',
|
||
'tool_run_progress': {'schedule_task_id': 1},
|
||
'training_workspace_enabled': True,
|
||
},
|
||
})
|
||
agent = Agent(name=normalized['name'], is_active=True)
|
||
apply_agent_payload(agent, normalized)
|
||
|
||
assert agent.agent_code == 'file_check'
|
||
assert agent.bound_schedule_task_id == 42
|
||
assert agent.capabilities['task_progress'] is True
|
||
extra = agent.extra_config or {}
|
||
for key in MIGRATED_EXTRA_REGISTRY_KEYS:
|
||
assert key not in extra
|
||
assert extra.get('training_workspace_enabled') is True
|
||
|
||
|
||
def test_validate_explicit_null_handler_config_clears_existing(app):
|
||
"""显式 handler_config=null 应清空旧配置,而非保留 existing。"""
|
||
with app.app_context():
|
||
agent = Agent(
|
||
name='Generic Bot',
|
||
is_active=True,
|
||
agent_code='generic',
|
||
runtime_provider='dify',
|
||
conversation_mode='chat',
|
||
dify_app_id='app-1',
|
||
handler_config={'intent_routes': ['text2sql'], 'content_profile': 'generic_entrance'},
|
||
)
|
||
normalized = validate_agent_payload({
|
||
'name': agent.name,
|
||
'agent_code': 'shipping_notice',
|
||
'runtime_provider': 'hybrid',
|
||
'conversation_mode': 'task_chat',
|
||
'dify_app_id': agent.dify_app_id,
|
||
'handler_config': None,
|
||
}, agent=agent, provided_fields=frozenset({
|
||
'name', 'agent_code', 'runtime_provider', 'conversation_mode', 'dify_app_id', 'handler_config',
|
||
}))
|
||
assert normalized['handler_config'] is None
|
||
|
||
|
||
def test_validate_omitted_handler_config_preserves_existing(app):
|
||
"""未传 handler_config 时应保留 ORM 现有值。"""
|
||
with app.app_context():
|
||
existing = {'intent_routes': ['text2sql']}
|
||
agent = Agent(
|
||
name='Generic Bot',
|
||
is_active=True,
|
||
agent_code='generic',
|
||
runtime_provider='dify',
|
||
conversation_mode='chat',
|
||
dify_app_id='app-1',
|
||
handler_config=existing,
|
||
)
|
||
normalized = validate_agent_payload({
|
||
'name': 'Renamed',
|
||
'agent_code': 'generic',
|
||
'runtime_provider': 'dify',
|
||
'conversation_mode': 'chat',
|
||
'dify_app_id': 'app-1',
|
||
}, agent=agent, provided_fields=frozenset({'name'}))
|
||
assert normalized['handler_config']['intent_routes'] == ['text2sql']
|
||
|
||
|
||
def test_strip_migrated_extra_registry_keys_preserves_training(app):
|
||
with app.app_context():
|
||
agent = Agent(
|
||
name='Training Bot',
|
||
is_active=True,
|
||
agent_code='generic',
|
||
extra_config={
|
||
'agent_code': 'generic',
|
||
'welcome_message': 'old',
|
||
'training_pass_score': 80,
|
||
},
|
||
)
|
||
strip_migrated_extra_registry_keys(agent)
|
||
extra = agent.extra_config or {}
|
||
assert 'agent_code' not in extra
|
||
assert 'welcome_message' not in extra
|
||
assert extra.get('training_pass_score') == 80
|