199 lines
7.2 KiB
Python
199 lines
7.2 KiB
Python
"""
|
|
tests/services/admin/test_agent_service.py
|
|
|
|
用途:agent_service CRUD 与权限绑定单元测试(Registry 结构化列)。
|
|
"""
|
|
import pytest
|
|
|
|
from app import db
|
|
from app.core.errors import NotFoundError, ValidationError
|
|
from app.models import Agent, AgentRuntimeBinding, User
|
|
from app.services.admin import agent_service
|
|
|
|
|
|
def test_agent_crud(app):
|
|
with app.app_context():
|
|
created = agent_service.create_agent({
|
|
'name': 'Test Agent',
|
|
'runtime_provider': 'iframe',
|
|
'conversation_mode': 'embedded',
|
|
'iframe_url': 'https://example.com',
|
|
'dify_app_id': 'unused',
|
|
})
|
|
agent_id = created['id']
|
|
|
|
items = agent_service.list_agents()
|
|
row = next(item for item in items if item['id'] == agent_id)
|
|
assert row['runtime_provider'] == 'iframe'
|
|
assert row['iframe_url'] == 'https://example.com'
|
|
|
|
agent_service.update_agent(agent_id, {'description': 'updated'})
|
|
refreshed = db.session.get(Agent, agent_id)
|
|
assert refreshed.description == 'updated'
|
|
|
|
agent_service.delete_agent(agent_id)
|
|
assert db.session.get(Agent, agent_id) is None
|
|
|
|
|
|
def test_create_agent_requires_dify_app_id_for_dify_provider(app):
|
|
with app.app_context():
|
|
with pytest.raises(ValidationError, match='dify_app_id'):
|
|
agent_service.create_agent({
|
|
'name': 'API Agent',
|
|
'runtime_provider': 'dify',
|
|
'dify_api_key': 'sk-test',
|
|
})
|
|
|
|
|
|
def test_create_file_check_agent_requires_schedule_task(app):
|
|
with app.app_context():
|
|
with pytest.raises(ValidationError, match='bound_schedule_task_id'):
|
|
agent_service.create_agent({
|
|
'name': 'File Check',
|
|
'agent_code': 'file_check',
|
|
'runtime_provider': 'dify',
|
|
'dify_app_id': 'app-fc',
|
|
'dify_api_key': 'sk-test',
|
|
})
|
|
|
|
|
|
def test_update_agent_clears_handler_config_when_explicit_null(app):
|
|
"""切换到空 schema 类型并传 handler_config=null 时应清空列。"""
|
|
with app.app_context():
|
|
created = agent_service.create_agent({
|
|
'name': 'Switch Target',
|
|
'agent_code': 'generic',
|
|
'runtime_provider': 'dify',
|
|
'conversation_mode': 'chat',
|
|
'dify_app_id': 'app-switch',
|
|
'dify_api_key': 'sk-test',
|
|
'handler_config': {'intent_routes': ['text2sql'], 'content_profile': 'generic_entrance'},
|
|
})
|
|
agent_id = created['id']
|
|
|
|
agent_service.update_agent(agent_id, {
|
|
'name': 'Switch Target',
|
|
'agent_code': 'shipping_notice',
|
|
'runtime_provider': 'hybrid',
|
|
'conversation_mode': 'task_chat',
|
|
'dify_app_id': 'app-switch',
|
|
'handler_config': None,
|
|
})
|
|
|
|
refreshed = db.session.get(Agent, agent_id)
|
|
assert refreshed.agent_code == 'shipping_notice'
|
|
assert refreshed.handler_config is None
|
|
|
|
|
|
def test_create_agent_and_runtime_binding_are_atomic(app):
|
|
"""创建货代 Agent 时绑定校验失败必须回滚 Agent,不能留下半配置入口。"""
|
|
with app.app_context():
|
|
provider = Agent(
|
|
name='Invalid Chatflow Provider',
|
|
agent_code='generic',
|
|
runtime_provider='dify',
|
|
dify_app_type='chatflow',
|
|
exposure_mode='workspace',
|
|
dify_app_id='invalid-create-provider',
|
|
dify_api_key='sk-test',
|
|
api_base_url='http://dify.example/v1',
|
|
is_active=True,
|
|
)
|
|
db.session.add(provider)
|
|
db.session.commit()
|
|
|
|
with pytest.raises(ValidationError):
|
|
agent_service.create_agent({
|
|
'name': 'Atomic Shipping Create',
|
|
'agent_code': 'shipping_notice',
|
|
'runtime_provider': 'hybrid',
|
|
'conversation_mode': 'task_chat',
|
|
'dify_app_id': 'atomic-shipping-create',
|
|
'dify_api_key': 'sk-test',
|
|
'api_base_url': 'http://dify.example/v1',
|
|
'order_extraction_runtime_binding': {
|
|
'provider_agent_id': provider.id,
|
|
'enabled': True,
|
|
'config': {'timeout_seconds': 900, 'max_attempts': 3},
|
|
},
|
|
})
|
|
db.session.rollback()
|
|
|
|
assert Agent.query.filter_by(name='Atomic Shipping Create').count() == 0
|
|
assert AgentRuntimeBinding.query.count() == 0
|
|
|
|
|
|
def test_update_agent_and_runtime_binding_are_atomic(app):
|
|
"""更新 Agent 时绑定校验失败必须回滚 Agent 字段,避免配置只保存一半。"""
|
|
with app.app_context():
|
|
owner = Agent(
|
|
name='Atomic Shipping Update',
|
|
description='before',
|
|
agent_code='shipping_notice',
|
|
runtime_provider='hybrid',
|
|
conversation_mode='task_chat',
|
|
dify_app_id='atomic-shipping-update',
|
|
dify_api_key='sk-test',
|
|
api_base_url='http://dify.example/v1',
|
|
is_active=True,
|
|
)
|
|
provider = Agent(
|
|
name='Invalid Update Provider',
|
|
agent_code='generic',
|
|
runtime_provider='dify',
|
|
dify_app_type='chatflow',
|
|
exposure_mode='workspace',
|
|
dify_app_id='invalid-update-provider',
|
|
dify_api_key='sk-test',
|
|
api_base_url='http://dify.example/v1',
|
|
is_active=True,
|
|
)
|
|
db.session.add_all([owner, provider])
|
|
db.session.commit()
|
|
|
|
with pytest.raises(ValidationError):
|
|
agent_service.update_agent(owner.id, {
|
|
'description': 'after',
|
|
'order_extraction_runtime_binding': {
|
|
'provider_agent_id': provider.id,
|
|
'enabled': True,
|
|
'config': {'timeout_seconds': 900, 'max_attempts': 3},
|
|
},
|
|
})
|
|
db.session.rollback()
|
|
|
|
assert db.session.get(Agent, owner.id).description == 'before'
|
|
assert AgentRuntimeBinding.query.filter_by(owner_agent_id=owner.id).count() == 0
|
|
|
|
|
|
def test_update_agent_permissions(app):
|
|
with app.app_context():
|
|
agent = Agent(
|
|
name='perm-agent',
|
|
type='iframe',
|
|
runtime_provider='iframe',
|
|
conversation_mode='embedded',
|
|
iframe_url='https://example.com',
|
|
is_active=True,
|
|
)
|
|
user_a = User(username='u-a', email='a@test.com', status='active', role='user')
|
|
user_b = User(username='u-b', email='b@test.com', status='active', role='user')
|
|
db.session.add_all([agent, user_a, user_b])
|
|
db.session.commit()
|
|
|
|
agent_service.update_agent_permissions(agent.id, [user_a.id])
|
|
db.session.refresh(user_a)
|
|
assert any(a.id == agent.id for a in user_a.agents)
|
|
|
|
agent_service.update_agent_permissions(agent.id, [user_b.id])
|
|
db.session.refresh(user_a)
|
|
db.session.refresh(user_b)
|
|
assert user_a.agents == []
|
|
assert any(a.id == agent.id for a in user_b.agents)
|
|
|
|
|
|
def test_test_agent_connectivity_not_found(app):
|
|
with app.app_context():
|
|
with pytest.raises(NotFoundError):
|
|
agent_service.test_agent_connectivity(99999)
|