feat: add ordered agent runtime
This commit is contained in:
76
backend/apps/agents/tests/test_agents_api.py
Normal file
76
backend/apps/agents/tests/test_agents_api.py
Normal file
@@ -0,0 +1,76 @@
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.core.management import call_command
|
||||
|
||||
from apps.modelhub.services import CompletionResult
|
||||
|
||||
from .helpers import create_agent, create_deployment
|
||||
|
||||
|
||||
def login_admin(client):
|
||||
call_command("bootstrap_governance")
|
||||
user = get_user_model().objects.get(username="admin")
|
||||
client.force_login(user)
|
||||
return user
|
||||
|
||||
|
||||
def test_admin_publishes_and_runs_agent(client, db, monkeypatch):
|
||||
login_admin(client)
|
||||
definition, version = create_agent()
|
||||
monkeypatch.setattr(
|
||||
"apps.modelhub.services._request_completion",
|
||||
lambda *args, **kwargs: CompletionResult('{"summary":"ok","reply":"ok"}', 3, 2),
|
||||
)
|
||||
|
||||
published = client.post(f"/api/agents/{definition.id}/publish")
|
||||
assert published.status_code == 200
|
||||
assert published.json()["status"] == "published"
|
||||
|
||||
run = client.post(
|
||||
f"/api/agents/{definition.id}/runs",
|
||||
data={"version": 1, "input": {"problem": "无法登录"}},
|
||||
content_type="application/json",
|
||||
)
|
||||
assert run.status_code == 200
|
||||
assert run.json()["status"] == "succeeded"
|
||||
public_id = run.json()["public_id"]
|
||||
detail = client.get(f"/api/agent-runs/{public_id}")
|
||||
assert detail.status_code == 200
|
||||
assert detail.json()["config_snapshot"]["agent"]["version"] == 1
|
||||
|
||||
|
||||
def test_publishing_rejects_draft_skill(client, db):
|
||||
login_admin(client)
|
||||
definition, version = create_agent()
|
||||
binding = version.steps.first()
|
||||
binding.skill_version.status = "draft"
|
||||
binding.skill_version.save(update_fields=["status"])
|
||||
|
||||
response = client.post(f"/api/agents/{definition.id}/publish")
|
||||
|
||||
assert response.status_code == 400
|
||||
assert "已发布 Skill" in response.json()["detail"]
|
||||
|
||||
|
||||
def test_agent_run_permission_is_required(client, db):
|
||||
definition, _ = create_agent(published=True)
|
||||
user = get_user_model().objects.create_user(username="requester", password="password")
|
||||
client.force_login(user)
|
||||
|
||||
response = client.post(
|
||||
f"/api/agents/{definition.id}/runs",
|
||||
data={"version": 1, "input": {"problem": "无法登录"}},
|
||||
content_type="application/json",
|
||||
)
|
||||
|
||||
assert response.status_code == 403
|
||||
|
||||
|
||||
def test_seed_command_creates_support_agent(client, db):
|
||||
deployment = create_deployment()
|
||||
|
||||
call_command("seed_ai_demo", deployment_id=deployment.id)
|
||||
|
||||
from apps.agents.models import AgentDefinition
|
||||
|
||||
agent = AgentDefinition.objects.get(code="support-analysis")
|
||||
assert agent.versions.get(status="draft").steps.count() == 3
|
||||
Reference in New Issue
Block a user