Files
sanguo_moziplus_v2/tests/test_e2e_v27.py
T
2026-05-18 19:28:55 +08:00

924 lines
34 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""v2.7 端到端测试 — 全链路真实环境
覆盖:项目管理 → Task CRUD → SubTask → Stage进度 → 状态聚合 → 依赖链 → 超时 → Mail → 真实Agent调度
"""
import asyncio
import json
import os
import sys
import time
import uuid
from datetime import datetime
from pathlib import Path
from typing import Any, Dict, List, Optional
import pytest
from fastapi.testclient import TestClient
# 指向部署目录
DEPLOY_DIR = Path.home() / ".sanguo_projects" / "sanguo_moziplus_v2"
sys.path.insert(0, str(DEPLOY_DIR))
from src.main import app
from src.blackboard.db import init_db, get_connection, VALID_TRANSITIONS, VALID_STATUSES
from src.blackboard.models import Task
from src.blackboard.operations import Blackboard
from src.blackboard.queries import Queries
from src.blackboard.registry import ProjectRegistry
from src.daemon.ticker import Ticker
from src.utils import get_data_root
# ── Fixtures ──
@pytest.fixture(scope="module")
def client():
return TestClient(app)
@pytest.fixture(scope="module")
def data_root():
return get_data_root()
def _pid() -> str:
"""生成唯一测试项目ID"""
return f"e2e-v27-{uuid.uuid4().hex[:8]}"
def _tid() -> str:
"""生成唯一任务ID"""
return f"e2e-task-{uuid.uuid4().hex[:8]}"
# ===================================================================
# E1: 项目管理
# ===================================================================
class TestE1ProjectManagement:
"""E1: 项目创建、列表、归档"""
def test_e11_create_and_get_project(self, client):
pid = _pid()
resp = client.post("/api/projects", json={
"id": pid,
"name": f"E2E测试-{pid}",
"description": "自动测试项目",
})
assert resp.status_code == 200
data = resp.json()
assert data.get("project_id") == pid or data.get("id") == pid
# 获取
resp = client.get(f"/api/projects/{pid}")
assert resp.status_code == 200
assert resp.json().get("id") == pid
def test_e12_list_projects(self, client):
resp = client.get("/api/projects")
assert resp.status_code == 200
data = resp.json()
assert "projects" in data
assert isinstance(data["projects"], dict)
def test_e13_archive_project(self, client):
pid = _pid()
client.post("/api/projects", json={"id": pid, "name": f"Archive-{pid}"})
resp = client.post(f"/api/projects/{pid}/archive")
assert resp.status_code == 200
# 验证状态
resp = client.get(f"/api/projects/{pid}")
assert resp.json()["status"] == "archived"
def test_e14_create_project_auto_discover(self, data_root):
"""创建含 blackboard.db 的目录,验证自动发现"""
pid = _pid()
project_dir = data_root / pid
project_dir.mkdir(parents=True, exist_ok=True)
init_db(project_dir / "blackboard.db")
# 注册
registry = ProjectRegistry(data_root)
registry.create_project(pid, f"Discover-{pid}", source="auto_discovered")
projects = registry.list_projects()
assert pid in projects
# 清理
import shutil
shutil.rmtree(project_dir, ignore_errors=True)
# ===================================================================
# E2: Task CRUD + 状态机
# ===================================================================
class TestE2TaskCRUD:
"""E2: Task 创建、查询、状态转换"""
@pytest.fixture(autouse=True)
def setup_project(self, client):
self.pid = _pid()
client.post("/api/projects", json={
"id": self.pid, "name": f"E2-{self.pid}",
})
def test_e21_create_task(self, client):
tid = _tid()
resp = client.post(f"/api/projects/{self.pid}/tasks", json={
"id": tid,
"title": "测试任务",
"description": "E2E测试",
"status": "pending",
})
assert resp.status_code == 200
assert resp.json().get("task_id") == tid or resp.json().get("id") == tid
self._tid = tid
def test_e22_get_task_expand(self, client):
tid = _tid()
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": tid, "title": "Expand测试", "status": "pending",
})
resp = client.get(f"/api/projects/{self.pid}/tasks/{tid}?expand=all")
assert resp.status_code == 200
data = resp.json()
assert data.get("id") == tid
assert "comments" in data or "outputs" in data
def test_e23_valid_transitions(self, client):
"""pending → claimed → working → review → done"""
tid = _tid()
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": tid, "title": "状态机测试", "status": "pending",
})
transitions = [
("claimed", {"agent": "zhangfei-dev"}),
("working", {"agent": "zhangfei-dev"}),
("review", {"agent": "zhangfei-dev"}),
("done", {"agent": "zhangfei-dev"}),
]
for new_status, body in transitions:
resp = client.post(
f"/api/projects/{self.pid}/tasks/{tid}/status",
json={"status": new_status, **body},
)
assert resp.status_code == 200, f"Failed at → {new_status}: {resp.text}"
def test_e24_invalid_transition_rejected(self, client):
"""pending → done 应被拒绝"""
tid = _tid()
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": tid, "title": "非法转换", "status": "pending",
})
resp = client.post(
f"/api/projects/{self.pid}/tasks/{tid}/status",
json={"status": "done", "agent": "test"},
)
assert resp.status_code == 409
assert "invalid_transition" in resp.text or "Cannot transition" in resp.text
def test_e25_list_tasks_filter(self, client):
"""按状态筛选"""
for st in ["pending", "pending"]:
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": _tid(), "title": "Filter", "status": st,
})
resp = client.get(f"/api/projects/{self.pid}/tasks?status=pending")
assert resp.status_code == 200
tasks = resp.json().get("tasks", resp.json())
assert len(tasks) >= 2
# ===================================================================
# E3: SubTask 父子关系
# ===================================================================
class TestE3SubTask:
"""E3: 父子 Task 关系"""
@pytest.fixture(autouse=True)
def setup(self, client):
self.pid = _pid()
client.post("/api/projects", json={
"id": self.pid, "name": f"E3-{self.pid}",
})
self.parent_id = "e3-parent-001"
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": self.parent_id,
"title": "父任务",
"status": "pending",
"stages_json": json.dumps([{"id": "setup", "label": "Setup"}, {"id": "run", "label": "Run"}, {"id": "verify", "label": "Verify"}]),
})
# 创建 3 个子 Task
self.child_ids = []
for i, stage in enumerate(["setup", "run", "verify"]):
cid = f"e3-child-{i}"
self.child_ids.append(cid)
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": cid,
"title": f"子任务-{stage}",
"status": "pending",
"parent_task": self.parent_id,
"stage": stage,
})
def test_e31_list_subtasks(self, client):
resp = client.get(f"/api/projects/{self.pid}/tasks?parent_task={self.parent_id}")
assert resp.status_code == 200
tasks = resp.json().get("tasks", resp.json())
assert len(tasks) == 3
ids = {t["id"] for t in tasks}
assert set(self.child_ids) == ids
def test_e32_top_level_excludes_children(self, data_root):
db_path = data_root / self.pid / "blackboard.db"
q = Queries(db_path)
top = q.top_level_tasks()
top_ids = {t.id for t in top}
assert self.parent_id in top_ids
for cid in self.child_ids:
assert cid not in top_ids
def test_e33_child_stage_field(self, data_root):
db_path = data_root / self.pid / "blackboard.db"
bb = Blackboard(db_path)
for i, stage in enumerate(["setup", "run", "verify"]):
t = bb.get_task(f"e3-child-{i}")
assert t is not None
assert t.stage == stage
# ===================================================================
# E4: Stage 进度
# ===================================================================
class TestE4StageProgress:
"""E4: stages_json + stage 分组统计"""
@pytest.fixture(autouse=True)
def setup(self, client):
self.pid = _pid()
client.post("/api/projects", json={
"id": self.pid, "name": f"E4-{self.pid}",
})
self.parent_id = "e4-parent-001"
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": self.parent_id,
"title": "Stage父任务",
"status": "pending",
"stages_json": json.dumps([{"id": "data", "label": "Data"}, {"id": "code", "label": "Code"}, {"id": "test", "label": "Test"}]),
})
# 每个stage一个子任务
for i, stage in enumerate(["data", "code", "test"]):
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": f"e4-child-{i}",
"title": f"Stage-{stage}",
"status": "pending",
"parent_task": self.parent_id,
"stage": stage,
})
def test_e41_progress_endpoint(self, client):
resp = client.get(f"/api/projects/{self.pid}/tasks/{self.parent_id}/progress")
assert resp.status_code == 200
data = resp.json()
assert "stages" in data
assert len(data["stages"]) == 3
def test_e42_progress_counts(self, data_root):
"""把 data stage 标记 done,验证进度"""
db_path = data_root / self.pid / "blackboard.db"
bb = Blackboard(db_path)
# data child → done
bb.update_task_status("e4-child-0", "claimed", agent="test")
bb.update_task_status("e4-child-0", "working", agent="test")
bb.update_task_status("e4-child-0", "review", agent="test")
bb.update_task_status("e4-child-0", "done", agent="test")
q = Queries(db_path)
progress = q.parent_task_progress(self.parent_id)
stages = {s["id"]: s for s in progress["stages"]}
assert stages["data"]["total"] == 1
assert stages["data"]["done"] == 1
assert stages["code"]["total"] == 1
assert stages["code"]["done"] == 0
def test_e43_empty_stages_progress(self, client):
"""无 stages_json 的 Task 进度"""
tid = _tid()
client.post(f"/api/projects/{self.pid}/tasks", json={
"id": tid, "title": "无Stage", "status": "pending",
})
db_path = get_data_root() / self.pid / "blackboard.db"
q = Queries(db_path)
progress = q.parent_task_progress(tid)
# 无子任务,应返回空或基本结构
assert progress is not None
# ===================================================================
# E5: 父 Task 状态聚合
# ===================================================================
class TestE5ParentAggregation:
"""E5: compute_parent_status 聚合逻辑"""
@pytest.fixture(autouse=True)
def setup(self, data_root):
self.pid = _pid()
db_path = data_root / self.pid / "blackboard.db"
db_path.parent.mkdir(parents=True, exist_ok=True)
init_db(db_path)
self.db_path = db_path
self.bb = Blackboard(db_path)
self.q = Queries(db_path)
# 创建父任务
self.parent_id = "e5-parent"
self.bb.create_task(Task(
id=self.parent_id, title="聚合父任务", status="pending",
))
def _create_child(self, cid: str, status: str = "pending", stage: str = None):
self.bb.create_task(Task(
id=cid, title=f"子-{cid}", status="pending",
parent_task=self.parent_id, stage=stage,
))
if status == "pending":
return
# Walk state machine to target status
path_map = {
"claimed": ["claimed"],
"working": ["claimed", "working"],
"review": ["claimed", "working", "review"],
"done": ["claimed", "working", "review", "done"],
"failed": ["claimed", "working", "failed"],
"blocked": ["claimed", "working", "blocked"],
"cancelled": ["cancelled"],
}
for s in path_map.get(status, []):
self.bb.update_task_status(cid, s, agent="test")
def test_e51_all_done_parent_done(self):
self._create_child("c1", "done")
self._create_child("c2", "done")
result = self.q.compute_parent_status(self.parent_id)
assert result == "done"
def test_e52_has_review_parent_review(self):
self._create_child("c3", "done")
self._create_child("c4", "review")
result = self.q.compute_parent_status(self.parent_id)
assert result == "review"
def test_e53_has_working_parent_working(self):
self._create_child("c5", "done")
self._create_child("c6", "working")
result = self.q.compute_parent_status(self.parent_id)
assert result == "working"
def test_e54_all_pending_parent_pending(self):
self._create_child("c7", "pending")
self._create_child("c8", "pending")
result = self.q.compute_parent_status(self.parent_id)
assert result == "pending"
def test_e55_cancelled_excluded(self):
"""cancelled 子 Task 不参与聚合"""
self._create_child("c9", "done")
self._create_child("c10", "cancelled")
# 只有 c9 有效 → done
result = self.q.compute_parent_status(self.parent_id)
assert result == "done"
def test_e56_cancelled_parent_not_overridden(self):
"""cancelled 的父 Task 不被聚合覆盖"""
# 先让所有子任务 done
self._create_child("c11", "done")
# 手动把父任务设为 cancelled
conn = get_connection(self.db_path)
try:
conn.execute("UPDATE tasks SET status='cancelled' WHERE id=?", (self.parent_id,))
conn.commit()
finally:
conn.close()
# _refresh_parent_statuses 应跳过 cancelled 父任务
ticker = Ticker.__new__(Ticker)
ticker._refresh_parent_statuses(self.db_path)
# 验证父任务仍是 cancelled
t = self.bb.get_task(self.parent_id)
assert t.status == "cancelled"
# ===================================================================
# E6: 依赖链
# ===================================================================
class TestE6DependencyChain:
"""E6: depends_on 依赖推进"""
@pytest.fixture(autouse=True)
def setup(self, data_root):
self.pid = _pid()
db_path = data_root / self.pid / "blackboard.db"
db_path.parent.mkdir(parents=True, exist_ok=True)
init_db(db_path)
self.db_path = db_path
self.bb = Blackboard(db_path)
def test_e61_dep_advance(self):
"""A done → B 从 blocked → pending"""
self.bb.create_task(Task(id="dep-a", title="A", status="pending"))
self.bb.create_task(Task(id="dep-b", title="B", status="blocked", depends_on=json.dumps(["dep-a"])))
# 完成 A
for s in ["claimed", "working", "review", "done"]:
self.bb.update_task_status("dep-a", s, agent="test")
# 手动 tick 依赖推进
ticker = Ticker.__new__(Ticker)
advanced = ticker._advance_dependencies(self.db_path)
assert "dep-b" in advanced
t = self.bb.get_task("dep-b")
assert t.status == "pending"
def test_e62_dep_not_done_stays_blocked(self):
"""A 未完成 → B 保持 blocked"""
self.bb.create_task(Task(id="dep-c", title="C", status="pending"))
self.bb.create_task(Task(id="dep-d", title="D", status="blocked", depends_on=json.dumps(["dep-c"])))
ticker = Ticker.__new__(Ticker)
advanced = ticker._advance_dependencies(self.db_path)
assert "dep-d" not in advanced
assert self.bb.get_task("dep-d").status == "blocked"
def test_e63_chain_a_b_c(self):
"""A → B → C 多层依赖"""
self.bb.create_task(Task(id="chain-a", title="A", status="pending"))
self.bb.create_task(Task(id="chain-b", title="B", status="blocked", depends_on=json.dumps(["chain-a"])))
self.bb.create_task(Task(id="chain-c", title="C", status="blocked", depends_on=json.dumps(["chain-b"])))
# 完成 A
for s in ["claimed", "working", "review", "done"]:
self.bb.update_task_status("chain-a", s, agent="test")
ticker = Ticker.__new__(Ticker)
advanced1 = ticker._advance_dependencies(self.db_path)
assert "chain-b" in advanced1
# B 现在 pending,完成 B
for s in ["claimed", "working", "review", "done"]:
self.bb.update_task_status("chain-b", s, agent="test")
advanced2 = ticker._advance_dependencies(self.db_path)
assert "chain-c" in advanced2
# ===================================================================
# E7: 超时回收
# ===================================================================
class TestE7Timeout:
"""E7: claimed/working 超时回收"""
@pytest.fixture(autouse=True)
def setup(self, data_root):
self.pid = _pid()
db_path = data_root / self.pid / "blackboard.db"
db_path.parent.mkdir(parents=True, exist_ok=True)
init_db(db_path)
self.db_path = db_path
self.bb = Blackboard(db_path)
def test_e71_claimed_timeout_to_pending(self):
"""claimed 超过 claim_timeout → pending"""
self.bb.create_task(Task(id="to-001", title="超时测试", status="pending"))
self.bb.update_task_status("to-001", "claimed", agent="test-agent")
# 手动把 claimed_at 设为 10 分钟前
conn = get_connection(self.db_path)
try:
conn.execute(
"UPDATE tasks SET claimed_at=datetime('now','-10 minutes') WHERE id=?",
("to-001",)
)
conn.commit()
finally:
conn.close()
ticker = Ticker.__new__(Ticker)
ticker.claim_timeout_minutes = 5.0
ticker.default_task_timeout_minutes = 30.0
reclaimed = ticker._check_timeouts(self.db_path)
assert "to-001" in reclaimed
assert self.bb.get_task("to-001").status == "pending"
def test_e72_working_timeout_to_failed(self):
"""working 超过 task_timeout → failed"""
self.bb.create_task(Task(id="to-002", title="工作超时", status="pending"))
self.bb.update_task_status("to-002", "claimed", agent="test-agent")
self.bb.update_task_status("to-002", "working", agent="test-agent")
# 手动把 started_at 设为 60 分钟前
conn = get_connection(self.db_path)
try:
conn.execute(
"UPDATE tasks SET started_at=datetime('now','-60 minutes') WHERE id=?",
("to-002",)
)
conn.commit()
finally:
conn.close()
ticker = Ticker.__new__(Ticker)
ticker.claim_timeout_minutes = 5.0
ticker.default_task_timeout_minutes = 30.0
reclaimed = ticker._check_timeouts(self.db_path)
assert "to-002" in reclaimed
assert self.bb.get_task("to-002").status == "failed"
# ===================================================================
# E8: Mail Tab 6 端点
# ===================================================================
class TestE8MailTab:
"""E8: Mail 端到端"""
@pytest.fixture(autouse=True)
def setup(self, client):
self.mail_ids = []
def _send_mail(self, client, **kwargs):
# Fix: 'from' is a Python keyword, callers use 'from_'
if 'from_' in kwargs:
kwargs['from'] = kwargs.pop('from_')
resp = client.post("/api/mail", json=kwargs)
assert resp.status_code == 200
data = resp.json()
assert data["ok"] is True
mid = data["mail_id"]
self.mail_ids.append(mid)
return mid
def test_e81_send_inform_auto_done(self, client):
"""inform 类型自动 done"""
mid = self._send_mail(client,
title="通知测试",
text="这是一条通知",
from_="pangtong-fujunshi",
to="simayi-challenger",
type="inform",
)
resp = client.get(f"/api/mail/{mid}")
data = resp.json()
assert data["status"] == "done"
def test_e82_send_task_assign_pending(self, client):
"""task-assign 类型保持 pending"""
mid = self._send_mail(client,
title="任务分配",
text="请完成此任务",
from_="pangtong-fujunshi",
to="zhangfei-dev",
type="task-assign",
)
resp = client.get(f"/api/mail/{mid}")
data = resp.json()
assert data["status"] == "pending"
def test_e83_list_with_filters(self, client):
"""列表 + 筛选"""
self._send_mail(client,
title="筛选测试1",
text="body",
from_="pangtong-fujunshi",
to="simayi-challenger",
type="text",
)
self._send_mail(client,
title="筛选测试2",
text="body",
from_="zhangfei-dev",
to="simayi-challenger",
type="text",
)
# 按 from 筛选
resp = client.get("/api/mail?from_agent=pangtong-fujunshi")
mails = resp.json()["mails"]
assert any(m["title"] == "筛选测试1" for m in mails)
# 按 to 筛选
resp = client.get("/api/mail?to_agent=simayi-challenger")
mails = resp.json()["mails"]
assert len(mails) >= 2
def test_e84_mail_detail_with_comments(self, client):
"""详情 + 评论"""
mid = self._send_mail(client,
title="详情测试",
text="正文内容",
from_="pangtong-fujunshi",
to="simayi-challenger",
type="text",
)
# 添加评论(通过 blackboard 直接写)
from src.api.mail_routes import _db_path
conn = get_connection(_db_path())
try:
conn.execute(
"INSERT INTO comments (task_id, author, comment_type, body) VALUES (?, ?, ?, ?)",
(mid, "simayi-challenger", "general", "收到,正在处理"),
)
conn.commit()
finally:
conn.close()
resp = client.get(f"/api/mail/{mid}")
data = resp.json()
assert data["title"] == "详情测试"
# 验证评论已写入(通过直接查 DB,绕过 Comment.from_row 的 card_id 兼容问题)
from src.api.mail_routes import _db_path as mail_db
conn = get_connection(mail_db())
try:
row = conn.execute("SELECT COUNT(*) as cnt FROM comments WHERE task_id=?", (mid,)).fetchone()
assert row["cnt"] == 1
finally:
conn.close()
def test_e85_mark_read(self, client):
"""标记已读"""
mid = self._send_mail(client,
title="已读测试",
text="body",
from_="pangtong-fujunshi",
to="simayi-challenger",
type="text",
)
# 确认初始未读
resp = client.get(f"/api/mail/{mid}")
assert resp.json()["is_read"] is False
# 标记已读
resp = client.patch(f"/api/mail/{mid}", json={"is_read": True})
assert resp.status_code == 200
# 验证
resp = client.get(f"/api/mail/{mid}")
assert resp.json()["is_read"] is True
def test_e86_mark_executed(self, client):
"""标记已执行(走完整状态链)"""
mid = self._send_mail(client,
title="执行测试",
text="body",
from_="pangtong-fujunshi",
to="zhangfei-dev",
type="task-assign",
)
# 先走状态链到 review,再标记 executed
from src.api.mail_routes import _db_path as mail_db
bb = Blackboard(mail_db())
for s in ["claimed", "working", "review"]:
bb.update_task_status(mid, s, agent="zhangfei-dev")
resp = client.patch(f"/api/mail/{mid}", json={"mark_executed": True})
assert resp.status_code == 200
resp = client.get(f"/api/mail/{mid}")
data = resp.json()
assert data["is_read"] is True
assert data["status"] == "done"
def test_e87_summary_and_agents(self, client):
"""统计 + Agent 列表"""
self._send_mail(client,
title="统计测试",
text="body",
from_="zhaoyun-data",
to="guanyu-dev",
type="inform",
)
resp = client.get("/api/mail/summary")
data = resp.json()
assert "total" in data
assert "unread" in data
assert data["total"] > 0
resp = client.get("/api/mail/agents/list")
data = resp.json()
assert "agents" in data
assert isinstance(data["agents"], list)
# ===================================================================
# E9: 真实 Agent 调度
# ===================================================================
class TestE9RealAgentDispatch:
"""E9: 创建真实任务 → Ticker 调度 → Agent spawn + 回写
注意:依赖真实 openclaw agent 进程,可能受环境影响。
"""
@pytest.fixture(autouse=True)
def setup(self, client, data_root):
self.pid = f"e2e-v27-agent-{uuid.uuid4().hex[:6]}"
client.post("/api/projects", json={
"id": self.pid,
"name": f"E9-Agent-{self.pid}",
"config": {"agents": ["zhangfei-dev"]},
})
self.data_root = data_root
def _make_ticker(self):
"""创建含 Dispatcher + Spawner 的 Ticker(真实 spawn"""
from src.blackboard.registry import ProjectRegistry
from src.daemon.dispatcher import Dispatcher
from src.daemon.spawner import AgentSpawner
registry = ProjectRegistry(self.data_root)
self._spawner = AgentSpawner(dry_run=False)
dispatcher = Dispatcher(
registered_agents=["zhangfei-dev", "jiangwei-infra", "simayi-challenger", "pangtong-fujunshi"],
spawner=self._spawner,
)
return Ticker(
registry=registry,
tick_interval=30,
max_dispatch_per_tick=3,
dispatcher=dispatcher,
spawner=self._spawner,
)
def test_e91_simple_task_agent_execute(self, client):
"""简单任务 → 真实 Agent 执行 → 等待完成"""
tid = _tid()
resp = client.post(f"/api/projects/{self.pid}/tasks", json={
"id": tid,
"title": "E2E简单任务:echo hello",
"description": "请执行:echo hello,然后把输出写入黑板。这是E2E测试,完成后标记done。",
"status": "pending",
"assignee": "zhangfei-dev",
"task_type": "coding",
"priority": 5,
})
assert resp.status_code == 200
# 手动触发 tick(走调度 + 真实 spawn
ticker = self._make_ticker()
result = asyncio.run(ticker.tick())
# 验证 dispatch 成功
proj = result.get("projects", {}).get(self.pid, {})
assert proj.get("status") == "ok"
dispatched = proj.get("dispatched", [])
assert tid in dispatched, f"Task {tid} not dispatched, dispatched={dispatched}"
# 等待 Agent 完成(最多 180 秒)
db_path = self.data_root / self.pid / "blackboard.db"
bb = Blackboard(db_path)
for _ in range(36):
time.sleep(5)
t = bb.get_task(tid)
if t and t.status in ("done", "failed", "review"):
break
t = bb.get_task(tid)
assert t is not None
assert t.status != "pending", f"Agent 未被调度,状态仍为 pending"
def test_e92_review_task_dispatch(self, client):
"""review 任务 → 真实 Agent 执行 → 等待完成"""
tid = _tid()
resp = client.post(f"/api/projects/{self.pid}/tasks", json={
"id": tid,
"title": "E2E Review:检查代码",
"description": "请查看任务描述并回复你的评审意见。这是E2E测试,简单回复即可。",
"status": "pending",
"assignee": "jiangwei-infra",
"task_type": "review",
"priority": 5,
})
assert resp.status_code == 200
ticker = self._make_ticker()
result = asyncio.run(ticker.tick())
proj = result.get("projects", {}).get(self.pid, {})
dispatched = proj.get("dispatched", [])
assert tid in dispatched, f"Review task {tid} not dispatched, dispatched={dispatched}"
# 等待 Agent 完成(最多 180 秒)
db_path = self.data_root / self.pid / "blackboard.db"
bb = Blackboard(db_path)
for _ in range(36):
time.sleep(5)
t = bb.get_task(tid)
if t and t.status in ("done", "failed", "review", "working", "claimed"):
break
t = bb.get_task(tid)
assert t is not None
assert t.status != "pending", f"Review Agent 未被调度,状态仍为 pending"
# ===================================================================
# E10: 全链路集成
# ===================================================================
class TestE10FullChain:
"""E10: 项目 → 父子Task → Ticker → 聚合 → 依赖 → Mail → 验证完整链"""
def test_e10_full_chain(self, client, data_root):
pid = f"e2e-v27-full-{uuid.uuid4().hex[:6]}"
# 1. 创建项目
resp = client.post("/api/projects", json={
"id": pid, "name": f"E10全链路-{pid}",
"config": {"agents": ["zhangfei-dev"]},
})
assert resp.status_code == 200
# 2. 创建父 Task(带 stages
parent_id = f"{pid}-parent"
resp = client.post(f"/api/projects/{pid}/tasks", json={
"id": parent_id,
"title": "全链路父任务",
"status": "pending",
"stages_json": json.dumps([{"id": "setup", "label": "Setup"}, {"id": "execute", "label": "Execute"}, {"id": "verify", "label": "Verify"}]),
})
assert resp.status_code == 200
# 3. 创建 3 个子 Task + 1 个依赖 Task
child_ids = []
for i, stage in enumerate(["setup", "execute", "verify"]):
cid = f"{pid}-child-{i}"
child_ids.append(cid)
resp = client.post(f"/api/projects/{pid}/tasks", json={
"id": cid,
"title": f"子任务-{stage}",
"status": "pending",
"parent_task": parent_id,
"stage": stage,
})
assert resp.status_code == 200
# 依赖 Taskblocked
dep_id = f"{pid}-dep"
resp = client.post(f"/api/projects/{pid}/tasks", json={
"id": dep_id,
"title": "依赖任务",
"depends_on": [child_ids[0]],
})
assert resp.status_code == 200
# 4. 手动把依赖任务设为 blockedAPI create 默认 pending
db_path = data_root / pid / "blackboard.db"
bb = Blackboard(db_path)
bb.update_task_status(dep_id, "claimed", agent="test")
bb.update_task_status(dep_id, "working", agent="test")
bb.update_task_status(dep_id, "blocked", agent="test")
# 5. 完成 setup 子任务
for s in ["claimed", "working", "review", "done"]:
bb.update_task_status(child_ids[0], s, agent="test")
# 5. 触发 Ticker(依赖推进 + 父状态聚合)
from src.blackboard.registry import ProjectRegistry
registry = ProjectRegistry(data_root)
ticker = Ticker(registry=registry, tick_interval=30)
# 依赖推进
advanced = ticker._advance_dependencies(db_path)
assert dep_id in advanced, "依赖任务应被推进为 pending"
# 父状态聚合
ticker._refresh_parent_statuses(db_path)
parent = bb.get_task(parent_id)
# 1 done + 2 pending → working (有活跃子任务) 或 pending
assert parent.status in ("pending", "working", "review"), f"父任务状态异常: {parent.status}"
# 6. 发送 Mail 通知
resp = client.post("/api/mail", json={
"title": f"E10全链路完成通知-{pid}",
"text": f"项目 {pid} 的 setup 阶段已完成",
"from": "simayi-challenger",
"to": "pangtong-fujunshi",
"type": "inform",
})
assert resp.status_code == 200
# 7. 验证 Mail 已创建
resp = client.get("/api/mail?from_agent=simayi-challenger")
mails = resp.json()["mails"]
assert any(m["title"].startswith("E10全链路") for m in mails)
# 8. 验证 Stage 进度
q = Queries(db_path)
progress = q.parent_task_progress(parent_id)
stages = {s["id"]: s for s in progress["stages"]}
assert stages["setup"]["done"] == 1
assert stages["execute"]["done"] == 0
# 9. 完成 execute 子任务
for s in ["claimed", "working", "review", "done"]:
bb.update_task_status(child_ids[1], s, agent="test")
# 再次聚合
ticker._refresh_parent_statuses(db_path)
progress2 = q.parent_task_progress(parent_id)
stages2 = {s["id"]: s for s in progress2["stages"]}
assert stages2["setup"]["done"] == 1
assert stages2["execute"]["done"] == 1
assert stages2["verify"]["done"] == 0
print(f"\n✅ E10 全链路测试通过!项目: {pid}")
print(f" 父任务: {parent_id}, 状态: {bb.get_task(parent_id).status}")
print(f" Stage进度: setup=done, execute=done, verify=pending")
print(f" 依赖推进: {dep_id} → pending")
print(f" Mail通知: 已发送")