feat: P1 补齐(自动评审队列、自动化+分诊、并发 caps、运行时审批队列)
This commit is contained in:
1 parent
29efc6c61f
commit
e0cecd70aa
18 files changed
+3474
-27
No files matched your search
@@ -0,0 +1,238 @@
|
||||
"""自动化与分诊收件箱服务端单测:POST/GET /projects/{id}/
|
||||
automations、POST /projects/{id}/automations/{aid}/cancel、
|
||||
GET /projects/{id}/triage。
|
||||
|
||||
不跑真实模型、不联网:本地临时端口 + 临时 SQLite,与
|
||||
test_peer_messages_server.py 同款骨架。覆盖鉴权、权限、
|
||||
校验、取消语义(404/409)、分诊聚合与审计。
|
||||
"""
|
||||
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
from cloud.auth import AuthStore
|
||||
from cloud.automations import record_automation_run
|
||||
from cloud.server import create_server
|
||||
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
from cloud.auth import AuthStore
|
||||
from cloud.automations import record_automation_run
|
||||
from cloud.server import create_server
|
||||
|
||||
|
||||
class AutomationServerTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.db_path = os.path.join(self.tmp.name, "control.db")
|
||||
self.auth = AuthStore(self.db_path)
|
||||
self.owner_id, self.owner_key = self.auth.create_user("owner")
|
||||
self.outsider_id, self.outsider_key = self.auth.create_user(
|
||||
"outsider")
|
||||
self.server = create_server(self.db_path, "127.0.0.1", 0)
|
||||
self.port = self.server.server_address[1]
|
||||
self.thread = threading.Thread(
|
||||
target=self.server.serve_forever, daemon=True)
|
||||
self.thread.start()
|
||||
status, self.project = self._req(
|
||||
"POST", "/projects", {"name": "demo"}, self.owner_key)
|
||||
self.assertEqual(status, 201)
|
||||
status, self.project2 = self._req(
|
||||
"POST", "/projects", {"name": "other"}, self.owner_key)
|
||||
self.assertEqual(status, 201)
|
||||
|
||||
def tearDown(self):
|
||||
self.server.shutdown()
|
||||
self.server.server_close()
|
||||
self.thread.join(timeout=5)
|
||||
try:
|
||||
self.auth.conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
self.tmp.cleanup()
|
||||
|
||||
def _req(self, method, path, body=None, key=None):
|
||||
conn = http.client.HTTPConnection(
|
||||
"127.0.0.1", self.port, timeout=10)
|
||||
hdrs = {}
|
||||
payload = None
|
||||
if body is not None:
|
||||
payload = json.dumps(body).encode("utf-8")
|
||||
hdrs["Content-Type"] = "application/json"
|
||||
if key is not None:
|
||||
hdrs["X-API-Key"] = key
|
||||
try:
|
||||
conn.request(method, path, body=payload, headers=hdrs)
|
||||
resp = conn.getresponse()
|
||||
data = resp.read()
|
||||
finally:
|
||||
conn.close()
|
||||
try:
|
||||
parsed = json.loads(data.decode("utf-8")) if data else None
|
||||
except ValueError:
|
||||
parsed = None
|
||||
return resp.status, parsed
|
||||
|
||||
def _autos_url(self, project_id=None):
|
||||
return "/projects/%s/automations" % (
|
||||
project_id or self.project["id"])
|
||||
|
||||
def _create_auto(self, project_id=None, **extra):
|
||||
body = {"name": "daily", "prompt": "做每日检查"}
|
||||
body.update(extra)
|
||||
return self._req(
|
||||
"POST", self._autos_url(project_id), body, self.owner_key)
|
||||
|
||||
def _audit_actions(self, project_id):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT action FROM audit_log WHERE project_id = ?",
|
||||
(project_id,)).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return [r[0] for r in rows]
|
||||
|
||||
def _create_task(self, project_id):
|
||||
status, task = self._req(
|
||||
"POST", "/tasks",
|
||||
{"project_id": project_id, "prompt": "做事"}, self.owner_key)
|
||||
self.assertEqual(status, 201, task)
|
||||
return task
|
||||
|
||||
def _set_status(self, task_id, status):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute("UPDATE tasks SET status = ? WHERE id = ?",
|
||||
(status, task_id))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_create_and_list(self):
|
||||
status, auto = self._create_auto(interval_seconds=3600)
|
||||
self.assertEqual(status, 201, auto)
|
||||
self.assertEqual(auto["status"], "active")
|
||||
self.assertEqual(auto["interval_seconds"], 3600)
|
||||
self.assertTrue(auto["due"])
|
||||
self.assertIn("automation_created",
|
||||
self._audit_actions(self.project["id"]))
|
||||
status, body = self._req(
|
||||
"GET", self._autos_url(), None, self.owner_key)
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual(len(body["automations"]), 1)
|
||||
self.assertEqual(body["due_count"], 1)
|
||||
self.assertEqual(body["project_id"], self.project["id"])
|
||||
|
||||
def test_create_defaults_and_validation(self):
|
||||
status, auto = self._create_auto()
|
||||
self.assertEqual(status, 201, auto)
|
||||
self.assertEqual(auto["interval_seconds"], 86400)
|
||||
for body in (
|
||||
{"prompt": "x"},
|
||||
{"name": "n"},
|
||||
{"name": "n", "prompt": "x", "interval_seconds": 30},
|
||||
{"name": "n", "prompt": "x",
|
||||
"interval_seconds": "3600"}):
|
||||
status, _ = self._req(
|
||||
"POST", self._autos_url(), body, self.owner_key)
|
||||
self.assertEqual(status, 400, body)
|
||||
status, _ = self._create_auto()
|
||||
self.assertEqual(status, 400)
|
||||
|
||||
def test_auth_and_permission(self):
|
||||
status, _ = self._req(
|
||||
"POST", self._autos_url(),
|
||||
{"name": "n", "prompt": "x"}, None)
|
||||
self.assertEqual(status, 401)
|
||||
status, _ = self._req(
|
||||
"GET", self._autos_url(), None, None)
|
||||
self.assertEqual(status, 401)
|
||||
status, _ = self._req(
|
||||
"POST", self._autos_url(),
|
||||
{"name": "n", "prompt": "x"}, self.outsider_key)
|
||||
self.assertEqual(status, 403)
|
||||
status, _ = self._req(
|
||||
"GET", self._autos_url(), None, self.outsider_key)
|
||||
self.assertEqual(status, 403)
|
||||
status, _ = self._req(
|
||||
"POST", "/projects/nope/automations",
|
||||
{"name": "n", "prompt": "x"}, self.owner_key)
|
||||
self.assertEqual(status, 404)
|
||||
|
||||
def test_cancel_semantics(self):
|
||||
_, auto = self._create_auto()
|
||||
url = "%s/%s/cancel" % (self._autos_url(), auto["id"])
|
||||
status, body = self._req("POST", url, {}, self.owner_key)
|
||||
self.assertEqual(status, 200, body)
|
||||
self.assertEqual(body["status"], "cancelled")
|
||||
self.assertFalse(body["due"])
|
||||
self.assertIn("automation_cancelled",
|
||||
self._audit_actions(self.project["id"]))
|
||||
status, _ = self._req("POST", url, {}, self.owner_key)
|
||||
self.assertEqual(status, 409)
|
||||
status, _ = self._req(
|
||||
"POST", "%s/missing/cancel" % self._autos_url(), {},
|
||||
self.owner_key)
|
||||
self.assertEqual(status, 404)
|
||||
# 别的项目的自动化 id 在本项目下取消也一律 404。
|
||||
_, other = self._create_auto(project_id=self.project2["id"])
|
||||
status, _ = self._req(
|
||||
"POST", "%s/%s/cancel" % (self._autos_url(), other["id"]),
|
||||
{}, self.owner_key)
|
||||
self.assertEqual(status, 404)
|
||||
status, _ = self._req(
|
||||
"POST", url, {}, self.outsider_key)
|
||||
self.assertEqual(status, 403)
|
||||
|
||||
def test_triage_inbox(self):
|
||||
_, auto = self._create_auto()
|
||||
task = self._create_task(self.project["id"])
|
||||
self._set_status(task["id"], "failed")
|
||||
runner = self._create_task(self.project["id"])
|
||||
self._set_status(runner["id"], "running")
|
||||
status, _ = self._req(
|
||||
"POST", "/tasks/%s/followups" % runner["id"],
|
||||
{"body": "请看一下", "mode": "queued"}, self.owner_key)
|
||||
self.assertEqual(status, 201)
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
record_automation_run(
|
||||
conn, auto["id"], self.project["id"],
|
||||
task_id=task["id"], status="created", detail="")
|
||||
finally:
|
||||
conn.close()
|
||||
status, body = self._req(
|
||||
"GET", "/projects/%s/triage" % self.project["id"], None,
|
||||
self.owner_key)
|
||||
self.assertEqual(status, 200, body)
|
||||
self.assertEqual(
|
||||
[d["id"] for d in body["automations_due"]], [auto["id"]])
|
||||
self.assertEqual(
|
||||
[t["id"] for t in body["failed_tasks"]], [task["id"]])
|
||||
self.assertEqual(len(body["pending_followups"]), 1)
|
||||
self.assertEqual(
|
||||
body["pending_followups"][0]["task_id"], runner["id"])
|
||||
self.assertEqual(len(body["recent_runs"]), 1)
|
||||
self.assertEqual(body["recent_runs"][0]["task_status"],
|
||||
"failed")
|
||||
self.assertEqual(body["counts"]["automations_due"], 1)
|
||||
status, _ = self._req(
|
||||
"GET", "/projects/%s/triage" % self.project["id"], None,
|
||||
self.outsider_key)
|
||||
self.assertEqual(status, 403)
|
||||
status, _ = self._req(
|
||||
"GET", "/projects/%s/triage" % self.project["id"], None,
|
||||
None)
|
||||
self.assertEqual(status, 401)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user