Merge pull request #177 from openqj/feat-project-todos
项目待办 + LLM 标记工具:todos 接口、控制台待办页、todo/task-list/cost/files 标记与首轮注入
This commit is contained in:
15 files changed
+2997
-35
No files matched your search
@@ -0,0 +1,269 @@
|
||||
"""成本/配额/文件标记单测:task-cost / quota-status / task-files
|
||||
解析、sandbox 提取、worker 投递与 worktree 解析。
|
||||
|
||||
不跑真实模型、不联网:解析是纯函数;worker 投递用 init_db
|
||||
建的临时 SQLite 库,直接调 _deliver_marker_tools,不走
|
||||
run_one 全量流程;文件读取用临时目录冒充 worktree(测
|
||||
试里直接给 Worker 换 tenants_root 与 host_py_path)。
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
try:
|
||||
from cloud.cost import CostStore
|
||||
from cloud.marker_tools import (
|
||||
extract_quota_status,
|
||||
extract_task_costs,
|
||||
extract_task_files,
|
||||
format_cost,
|
||||
safe_worktree_path,
|
||||
)
|
||||
from cloud.sandbox import LocalExecutor, _parse_host_result
|
||||
from cloud.server import init_db
|
||||
from cloud.worker import Worker
|
||||
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
from cloud.cost import CostStore
|
||||
from cloud.marker_tools import (
|
||||
extract_quota_status,
|
||||
extract_task_costs,
|
||||
extract_task_files,
|
||||
format_cost,
|
||||
safe_worktree_path,
|
||||
)
|
||||
from cloud.sandbox import LocalExecutor, _parse_host_result
|
||||
from cloud.server import init_db
|
||||
from cloud.worker import Worker
|
||||
|
||||
FAKE_ENV = {"XCM_API_KEY": "test-placeholder"}
|
||||
|
||||
|
||||
class ExtractTestCase(unittest.TestCase):
|
||||
def test_cost_and_files(self):
|
||||
self.assertEqual(extract_task_costs(
|
||||
"```task-cost\n{\"task_id\": \"t1\"}\n```"),
|
||||
[{"task_id": "t1"}])
|
||||
self.assertEqual(extract_task_costs(
|
||||
"```task-cost\n{\"to\": \"t1\"}\n```"),
|
||||
[{"task_id": "t1"}])
|
||||
self.assertEqual(extract_task_costs(
|
||||
"```task-cost\n{}\n```"), [])
|
||||
self.assertEqual(extract_task_files(
|
||||
"```task-files\n{\"task_id\": \"t1\"}\n```"),
|
||||
[{"task_id": "t1", "path": ""}])
|
||||
self.assertEqual(extract_task_files(
|
||||
"```task-files\n{\"task_id\": \"t1\", \"path\": \"a.txt\"}\n```"),
|
||||
[{"task_id": "t1", "path": "a.txt"}])
|
||||
self.assertEqual(extract_task_files(
|
||||
"```task-files\n{\"path\": \"a.txt\"}\n```"), [])
|
||||
|
||||
def test_quota_only_object(self):
|
||||
self.assertEqual(extract_quota_status(
|
||||
"```quota-status\n{}\n```"), [{}])
|
||||
self.assertEqual(extract_quota_status(
|
||||
"```quota-status\n{\"owner\": \"someone\"}\n```"), [{}])
|
||||
self.assertEqual(extract_quota_status(
|
||||
"```quota-status\n[]\n```"), [])
|
||||
|
||||
def test_safe_worktree_path(self):
|
||||
self.assertIsNone(safe_worktree_path("/tmp/w", "../etc/passwd"))
|
||||
self.assertIsNone(safe_worktree_path("/tmp/w", "/etc/passwd"))
|
||||
self.assertEqual(safe_worktree_path("/tmp/w", ""),
|
||||
"/tmp/w")
|
||||
self.assertEqual(safe_worktree_path("/tmp/w", "a/b.txt"),
|
||||
"/tmp/w/a/b.txt")
|
||||
|
||||
def test_format_cost_unpriced_separate(self):
|
||||
line = format_cost("t1", {"usd_micros": 0, "total_tokens": 10,
|
||||
"actual_tokens": 10,
|
||||
"estimated_tokens": 0, "turns": 1,
|
||||
"unpriced_actual_tokens": 10})
|
||||
self.assertIn("$0.0000", line)
|
||||
self.assertIn("无价 actual token 10 未计入美元", line)
|
||||
|
||||
def test_sandbox_extracts_both_executors(self):
|
||||
text = ("```task-cost\n{\"task_id\": \"t1\"}\n```\n"
|
||||
"```quota-status\n{}\n```\n"
|
||||
"```task-files\n{\"task_id\": \"t1\"}\n```")
|
||||
fake = SimpleNamespace(
|
||||
returncode=0,
|
||||
stdout=json.dumps({"status": "completed", "text": text}),
|
||||
stderr="")
|
||||
local = LocalExecutor("/fake/host.py")._result_from_runner("t1", fake)
|
||||
self.assertEqual(len(local["task_costs"]), 1)
|
||||
self.assertEqual(len(local["quota_status"]), 1)
|
||||
self.assertEqual(len(local["task_files"]), 1)
|
||||
docker = _parse_host_result("t1", fake)
|
||||
self.assertEqual(len(docker["task_costs"]), 1)
|
||||
self.assertEqual(len(docker["quota_status"]), 1)
|
||||
self.assertEqual(len(docker["task_files"]), 1)
|
||||
|
||||
|
||||
class WorkerCostFilesTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.db_path = os.path.join(self.tmp.name, "control.db")
|
||||
init_db(self.db_path)
|
||||
self._insert("PM", status="running", created_by="tester")
|
||||
self._member("tester", "maintainer")
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def _insert(self, task_id, status="queued", created_by="tester",
|
||||
project_id="p1", prompt="do work", parent=None,
|
||||
**cols):
|
||||
now = time.time()
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"INSERT INTO tasks(id, project_id, prompt, status,"
|
||||
" idempotency_key, depends_on, priority, created_by,"
|
||||
" created_at, updated_at, last_heartbeat)"
|
||||
" VALUES(?, ?, ?, ?, NULL, '[]', 0, ?, ?, ?, NULL)",
|
||||
(task_id, project_id, prompt, status, created_by,
|
||||
now, now))
|
||||
if parent is not None:
|
||||
conn.execute(
|
||||
"UPDATE tasks SET parent_task_id = ? WHERE id = ?",
|
||||
(parent, task_id))
|
||||
for key, value in cols.items():
|
||||
conn.execute(
|
||||
"UPDATE tasks SET %s = ? WHERE id = ?" % key,
|
||||
(value, task_id))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _member(self, user_id, role):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS users(id TEXT PRIMARY KEY,"
|
||||
" username TEXT UNIQUE, api_key_hash TEXT,"
|
||||
" created_at REAL)")
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS project_members("
|
||||
"project_id TEXT, user_id TEXT, role TEXT,"
|
||||
" PRIMARY KEY(project_id, user_id))")
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO project_members"
|
||||
"(project_id, user_id, role) VALUES('p1', ?, ?)",
|
||||
(user_id, role))
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO users(id, username, api_key_hash,"
|
||||
" created_at) VALUES(?, ?, '', 0)", (user_id, user_id))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _worker(self):
|
||||
return Worker(self.db_path, "/fake/host.py", env=FAKE_ENV)
|
||||
|
||||
def _followup_bodies(self, task_id="PM"):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT body FROM task_followups"
|
||||
" WHERE task_id = ?", (task_id,)).fetchall()
|
||||
return [row[0] for row in rows]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_deliver_cost_and_quota(self):
|
||||
self._insert("E1", status="completed", parent="PM")
|
||||
store = CostStore(self.db_path)
|
||||
try:
|
||||
store.record("E1", "p1", "gpt-test", 100, 50,
|
||||
usd_micros=12345, source="actual")
|
||||
finally:
|
||||
try:
|
||||
store._conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
worker = self._worker()
|
||||
worker._deliver_marker_tools(worker.get_task("PM"), {
|
||||
"status": "completed",
|
||||
"task_costs": [{"task_id": "E1"}],
|
||||
"quota_status": [{}],
|
||||
"task_files": []})
|
||||
bodies = self._followup_bodies()
|
||||
self.assertEqual(len(bodies), 1)
|
||||
self.assertIn("$0.0123", bodies[0])
|
||||
self.assertIn("配额", bodies[0])
|
||||
|
||||
def test_deliver_cost_other_creator_rejected(self):
|
||||
self._insert("E1", status="completed", created_by="someone-else")
|
||||
worker = self._worker()
|
||||
worker._deliver_marker_tools(worker.get_task("PM"), {
|
||||
"status": "completed",
|
||||
"task_costs": [{"task_id": "E1"}],
|
||||
"quota_status": [],
|
||||
"task_files": []})
|
||||
bodies = self._followup_bodies()
|
||||
self.assertIn("不在派生链内且创建人不同", bodies[0])
|
||||
|
||||
def test_deliver_files_list_and_read(self):
|
||||
self._insert("E1", status="completed", parent="PM")
|
||||
worker = self._worker()
|
||||
# 冒充 worktree:租户路径下给 E1 建目录与文件。
|
||||
worktree = os.path.join(self.tmp.name, "tenants", "p1",
|
||||
"work", "host-worktrees", "E1")
|
||||
os.makedirs(worktree)
|
||||
with open(os.path.join(worktree, "out.txt"), "w") as fh:
|
||||
fh.write("hello-files")
|
||||
with open(os.path.join(worktree, "bin.dat"), "wb") as fh:
|
||||
fh.write(b"\x00\x01\x02")
|
||||
worker.tenants_root = os.path.join(self.tmp.name, "tenants")
|
||||
worker._deliver_marker_tools(worker.get_task("PM"), {
|
||||
"status": "completed",
|
||||
"task_costs": [],
|
||||
"quota_status": [],
|
||||
"task_files": [{"task_id": "E1", "path": ""},
|
||||
{"task_id": "E1", "path": "out.txt"},
|
||||
{"task_id": "E1", "path": "bin.dat"},
|
||||
{"task_id": "E1", "path": "../escape"}]})
|
||||
bodies = self._followup_bodies()
|
||||
self.assertEqual(len(bodies), 1)
|
||||
self.assertIn("out.txt", bodies[0])
|
||||
self.assertIn("hello-files", bodies[0])
|
||||
self.assertIn("二进制文件", bodies[0])
|
||||
self.assertIn("路径逃逸", bodies[0])
|
||||
|
||||
def test_deliver_files_cloud_and_no_worktree_rejected(self):
|
||||
self._insert("C1", status="completed", parent="PM",
|
||||
executor="cloud")
|
||||
self._insert("N1", status="completed", parent="PM")
|
||||
worker = self._worker()
|
||||
worker.tenants_root = os.path.join(self.tmp.name, "tenants")
|
||||
worker._deliver_marker_tools(worker.get_task("PM"), {
|
||||
"status": "completed",
|
||||
"task_costs": [],
|
||||
"quota_status": [],
|
||||
"task_files": [{"task_id": "C1", "path": ""},
|
||||
{"task_id": "N1", "path": ""}]})
|
||||
bodies = self._followup_bodies()
|
||||
self.assertIn("不在本机", bodies[0])
|
||||
self.assertIn("没有本地 worktree", bodies[0])
|
||||
|
||||
def test_deliver_goal_sender_ignored(self):
|
||||
self._insert("G1", status="running", goal_mode=1)
|
||||
worker = self._worker()
|
||||
worker._deliver_marker_tools(worker.get_task("G1"), {
|
||||
"status": "completed",
|
||||
"task_costs": [{"task_id": "G1"}],
|
||||
"quota_status": [{}],
|
||||
"task_files": []})
|
||||
self.assertEqual(self._followup_bodies("G1"), [])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,138 @@
|
||||
"""项目待办模块单测(cloud/todos.py)。
|
||||
|
||||
不跑真实模型、不联网:临时 SQLite 文件 + 模块函数直调。
|
||||
覆盖:建表幂等、创建校验与默认值、状态五态与完成时间、
|
||||
PATCH 只改给定字段、过滤(状态/q)、排序追加、硬删除、
|
||||
跨项目隔离(模块层按 project_id 查,服务端层再做 404)。
|
||||
"""
|
||||
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
from cloud.todos import (
|
||||
create_todo,
|
||||
delete_todo,
|
||||
ensure_todos_table,
|
||||
get_todo,
|
||||
list_todos,
|
||||
update_todo,
|
||||
)
|
||||
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
from cloud.todos import (
|
||||
create_todo,
|
||||
delete_todo,
|
||||
ensure_todos_table,
|
||||
get_todo,
|
||||
list_todos,
|
||||
update_todo,
|
||||
)
|
||||
|
||||
|
||||
class TodosTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.db_path = os.path.join(self.tmp.name, "control.db")
|
||||
self.conn = sqlite3.connect(self.db_path)
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
ensure_todos_table(self.conn)
|
||||
self.conn.commit()
|
||||
self.now = 1700000000.0
|
||||
self.project = "proj-1"
|
||||
|
||||
def tearDown(self):
|
||||
self.conn.close()
|
||||
self.tmp.cleanup()
|
||||
|
||||
def _make(self, title="写文档", **kw):
|
||||
kw.setdefault("project_id", self.project)
|
||||
return create_todo(
|
||||
self.conn, kw.pop("project_id"), "user-1", title,
|
||||
now=self.now, **kw)
|
||||
|
||||
def test_ensure_is_idempotent(self):
|
||||
ensure_todos_table(self.conn)
|
||||
ensure_todos_table(self.conn)
|
||||
self.conn.commit()
|
||||
rows = self.conn.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table'"
|
||||
" AND name = 'todos'").fetchall()
|
||||
self.assertEqual(len(rows), 1)
|
||||
|
||||
def test_create_defaults_and_validation(self):
|
||||
t = self._make()
|
||||
self.assertEqual(t["status"], "pending")
|
||||
self.assertEqual(t["priority"], "medium")
|
||||
self.assertEqual(t["description"], "")
|
||||
self.assertIsNone(t["task_id"])
|
||||
self.assertIsNone(t["due_at"])
|
||||
self.assertIsNone(t["completed_at"])
|
||||
self.assertEqual(t["sort_order"], 0)
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(title=" ")
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(status="done")
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(priority="urgent")
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(title="x" * 501)
|
||||
|
||||
def test_sort_order_appends(self):
|
||||
a = self._make("第一")
|
||||
b = self._make("第二")
|
||||
c = self._make("第三", sort_order=10)
|
||||
self.assertEqual((a["sort_order"], b["sort_order"],
|
||||
c["sort_order"]), (0, 1, 10))
|
||||
titles = [t["title"] for t in list_todos(self.conn, self.project)]
|
||||
self.assertEqual(titles, ["第一", "第二", "第三"])
|
||||
|
||||
def test_status_completed_sets_and_clears_completed_at(self):
|
||||
t = self._make()
|
||||
done = update_todo(self.conn, t["id"], {"status": "completed"})
|
||||
self.assertIsNotNone(done["completed_at"])
|
||||
back = update_todo(self.conn, t["id"], {"status": "in_progress"})
|
||||
self.assertIsNone(back["completed_at"])
|
||||
self.assertEqual(back["status"], "in_progress")
|
||||
|
||||
def test_update_only_given_fields(self):
|
||||
t = self._make("原标题", description="原说明",
|
||||
priority="low", task_id="task-1")
|
||||
u = update_todo(self.conn, t["id"], {"title": "新标题"})
|
||||
self.assertEqual(u["title"], "新标题")
|
||||
self.assertEqual(u["description"], "原说明")
|
||||
self.assertEqual(u["priority"], "low")
|
||||
self.assertEqual(u["task_id"], "task-1")
|
||||
self.assertIsNone(update_todo(self.conn, "nope", {"title": "x"}))
|
||||
|
||||
def test_due_at_ms_tolerance(self):
|
||||
t = self._make(due_at=1700000000000)
|
||||
self.assertEqual(t["due_at"], 1700000000.0)
|
||||
with self.assertRaises(ValueError):
|
||||
self._make(due_at="明天")
|
||||
|
||||
def test_list_filters(self):
|
||||
self._make("写文档", status="completed")
|
||||
self._make("修 bug", status="in_progress")
|
||||
self._make("写测试", status="pending")
|
||||
done = list_todos(self.conn, self.project, status="completed")
|
||||
self.assertEqual([t["title"] for t in done], ["写文档"])
|
||||
q = list_todos(self.conn, self.project, q="写")
|
||||
self.assertEqual(sorted(t["title"] for t in q), ["写文档", "写测试"])
|
||||
other = list_todos(self.conn, "proj-2")
|
||||
self.assertEqual(other, [])
|
||||
|
||||
def test_delete(self):
|
||||
t = self._make()
|
||||
deleted = delete_todo(self.conn, t["id"])
|
||||
self.assertEqual(deleted["id"], t["id"])
|
||||
self.assertIsNone(get_todo(self.conn, t["id"]))
|
||||
self.assertIsNone(delete_todo(self.conn, t["id"]))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,162 @@
|
||||
"""项目待办服务端单测:POST/GET /projects/{id}/todos、
|
||||
GET/PATCH/DELETE /projects/{id}/todos/{tid}。
|
||||
|
||||
不跑真实模型、不联网:本地临时端口 + 临时 SQLite,与
|
||||
test_automations_server.py 同款骨架。覆盖鉴权、权限、
|
||||
校验、跨项目 404、状态完成时间、删除语义。
|
||||
"""
|
||||
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
try:
|
||||
from cloud.auth import AuthStore
|
||||
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.server import create_server
|
||||
|
||||
|
||||
class TodoServerTestCase(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 _url(self, project_id=None):
|
||||
return "/projects/%s/todos" % (project_id or self.project["id"])
|
||||
|
||||
def _create(self, project_id=None, **extra):
|
||||
body = {"title": "写文档"}
|
||||
body.update(extra)
|
||||
return self._req("POST", self._url(project_id), body,
|
||||
self.owner_key)
|
||||
|
||||
def test_create_list_get(self):
|
||||
status, todo = self._create()
|
||||
self.assertEqual(status, 201)
|
||||
self.assertEqual(todo["status"], "pending")
|
||||
self.assertEqual(todo["priority"], "medium")
|
||||
status, body = self._req(
|
||||
"GET", self._url(), key=self.owner_key)
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual(body["total"], 1)
|
||||
self.assertEqual(body["counts"]["pending"], 1)
|
||||
status, got = self._req(
|
||||
"GET", self._url() + "/" + todo["id"], key=self.owner_key)
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual(got["id"], todo["id"])
|
||||
|
||||
def test_auth_and_permission(self):
|
||||
status, _ = self._req("GET", self._url())
|
||||
self.assertEqual(status, 401)
|
||||
status, _ = self._req("POST", self._url(),
|
||||
{"title": "x"}, self.outsider_key)
|
||||
self.assertEqual(status, 403)
|
||||
status, _ = self._req("GET", self._url(), key=self.outsider_key)
|
||||
self.assertEqual(status, 403)
|
||||
|
||||
def test_validation_and_cross_project_404(self):
|
||||
status, _ = self._create(title=" ")
|
||||
self.assertEqual(status, 400)
|
||||
status, _ = self._create(status="done")
|
||||
self.assertEqual(status, 400)
|
||||
status, todo = self._create()
|
||||
self.assertEqual(status, 201)
|
||||
# 单条属于别的项目时一律 404,不泄露存在性
|
||||
status, _ = self._req(
|
||||
"GET", self._url(self.project2["id"]) + "/" + todo["id"],
|
||||
key=self.owner_key)
|
||||
self.assertEqual(status, 404)
|
||||
status, _ = self._req(
|
||||
"GET", self._url() + "/nope", key=self.owner_key)
|
||||
self.assertEqual(status, 404)
|
||||
|
||||
def test_patch_completed_and_delete(self):
|
||||
_, todo = self._create()
|
||||
status, done = self._req(
|
||||
"PATCH", self._url() + "/" + todo["id"],
|
||||
{"status": "completed"}, self.owner_key)
|
||||
self.assertEqual(status, 200)
|
||||
self.assertIsNotNone(done["completed_at"])
|
||||
status, back = self._req(
|
||||
"PATCH", self._url() + "/" + todo["id"],
|
||||
{"status": "blocked"}, self.owner_key)
|
||||
self.assertEqual(status, 200)
|
||||
self.assertIsNone(back["completed_at"])
|
||||
status, _ = self._req(
|
||||
"DELETE", self._url() + "/" + todo["id"],
|
||||
key=self.owner_key)
|
||||
self.assertEqual(status, 200)
|
||||
status, _ = self._req(
|
||||
"GET", self._url() + "/" + todo["id"], key=self.owner_key)
|
||||
self.assertEqual(status, 404)
|
||||
|
||||
def test_list_filter(self):
|
||||
self._create()
|
||||
self._create(title="修 bug", status="in_progress")
|
||||
status, body = self._req(
|
||||
"GET", self._url() + "?status=in_progress",
|
||||
key=self.owner_key)
|
||||
self.assertEqual(status, 200)
|
||||
self.assertEqual(len(body["todos"]), 1)
|
||||
# 计数与 total 是全项目口径,不受过滤影响
|
||||
self.assertEqual(body["total"], 2)
|
||||
self.assertEqual(body["counts"]["pending"], 1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,256 @@
|
||||
"""任务列表标记与项目注入单测:task-list 解析、sandbox 提取、
|
||||
worker 投递与首轮项目注入。
|
||||
|
||||
不跑真实模型、不联网:解析是纯函数;worker 投递与注入用
|
||||
init_db 建的临时 SQLite 库,直接调 _deliver_marker_tools 与
|
||||
_apply_project_injection,不走 run_one 全量流程。
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
try:
|
||||
from cloud.marker_tools import extract_task_lists, format_task
|
||||
from cloud.sandbox import LocalExecutor, _parse_host_result
|
||||
from cloud.server import init_db
|
||||
from cloud.worker import Worker
|
||||
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
from cloud.marker_tools import extract_task_lists, format_task
|
||||
from cloud.sandbox import LocalExecutor, _parse_host_result
|
||||
from cloud.server import init_db
|
||||
from cloud.worker import Worker
|
||||
|
||||
FAKE_ENV = {"XCM_API_KEY": "test-placeholder"}
|
||||
|
||||
|
||||
class ExtractTestCase(unittest.TestCase):
|
||||
def test_list_default_and_alias(self):
|
||||
out = extract_task_lists("```task-list\n{}\n```")
|
||||
self.assertEqual(out, [{"status": None, "q": None, "limit": 20}])
|
||||
out = extract_task_lists(
|
||||
"```task_list\n{\"status\": \"failed\", \"limit\": 5}\n```")
|
||||
self.assertEqual(out, [{"status": "failed", "q": None,
|
||||
"limit": 5}])
|
||||
|
||||
def test_list_limit_clamped_and_invalid_skipped(self):
|
||||
out = extract_task_lists(
|
||||
"```task-list\n{\"limit\": 999}\n```")
|
||||
self.assertEqual(out[0]["limit"], 50)
|
||||
out = extract_task_lists(
|
||||
"```task-list\n{\"limit\": 0}\n```")
|
||||
self.assertEqual(out[0]["limit"], 1)
|
||||
for body in ("{\"status\": \"doing\"}",
|
||||
"{\"limit\": \"many\"}",
|
||||
"{\"limit\": true}",
|
||||
"{\"q\": 123}"):
|
||||
with self.subTest(body=body):
|
||||
self.assertEqual(extract_task_lists(
|
||||
"```task-list\n%s\n```" % body), [])
|
||||
|
||||
def test_list_cap_five(self):
|
||||
text = "\n".join("```task-list\n{}\n```" for _ in range(6))
|
||||
self.assertEqual(len(extract_task_lists(text)), 5)
|
||||
|
||||
def test_format_task_truncates(self):
|
||||
line = format_task({"id": "t1", "status": "failed",
|
||||
"prompt": "x" * 100, "created_by": "u1",
|
||||
"created_at": 1700000000})
|
||||
self.assertIn("…", line)
|
||||
self.assertIn("2023-11-14T22:13:20Z", line)
|
||||
|
||||
def test_sandbox_extracts_both_executors(self):
|
||||
text = "```task-list\n{\"status\": \"failed\"}\n```"
|
||||
fake = SimpleNamespace(
|
||||
returncode=0,
|
||||
stdout=json.dumps({"status": "completed", "text": text}),
|
||||
stderr="")
|
||||
local = LocalExecutor("/fake/host.py")._result_from_runner("t1", fake)
|
||||
self.assertEqual(len(local["task_lists"]), 1)
|
||||
docker = _parse_host_result("t1", fake)
|
||||
self.assertEqual(len(docker["task_lists"]), 1)
|
||||
|
||||
|
||||
class WorkerTaskListTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.db_path = os.path.join(self.tmp.name, "control.db")
|
||||
init_db(self.db_path)
|
||||
self._insert("PM", status="running", created_by="tester")
|
||||
self._member("tester", "maintainer")
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def _insert(self, task_id, status="queued", created_by="tester",
|
||||
project_id="p1", prompt="do work", **cols):
|
||||
now = time.time()
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"INSERT INTO tasks(id, project_id, prompt, status,"
|
||||
" idempotency_key, depends_on, priority, created_by,"
|
||||
" created_at, updated_at, last_heartbeat)"
|
||||
" VALUES(?, ?, ?, ?, NULL, '[]', 0, ?, ?, ?, NULL)",
|
||||
(task_id, project_id, prompt, status, created_by,
|
||||
now, now))
|
||||
for key, value in cols.items():
|
||||
conn.execute(
|
||||
"UPDATE tasks SET %s = ? WHERE id = ?" % key,
|
||||
(value, task_id))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _member(self, user_id, role):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS users(id TEXT PRIMARY KEY,"
|
||||
" username TEXT UNIQUE, api_key_hash TEXT,"
|
||||
" created_at REAL)")
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS project_members("
|
||||
"project_id TEXT, user_id TEXT, role TEXT,"
|
||||
" PRIMARY KEY(project_id, user_id))")
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO project_members"
|
||||
"(project_id, user_id, role) VALUES('p1', ?, ?)",
|
||||
(user_id, role))
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO users(id, username, api_key_hash,"
|
||||
" created_at) VALUES(?, ?, '', 0)", (user_id, user_id))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _project(self, project_id="p1", name="演示项目",
|
||||
repo_url="git@github.com:openqj/demo.git"):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO projects(id, name, owner_id,"
|
||||
" created_at, repo_url, auto_pr)"
|
||||
" VALUES(?, ?, 'tester', ?, ?, 0)",
|
||||
(project_id, name, time.time(), repo_url))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _worker(self):
|
||||
return Worker(self.db_path, "/fake/host.py", env=FAKE_ENV)
|
||||
|
||||
def _followup_bodies(self, task_id="PM"):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT body FROM task_followups"
|
||||
" WHERE task_id = ?", (task_id,)).fetchall()
|
||||
return [row[0] for row in rows]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_deliver_lists_all_with_counts(self):
|
||||
self._insert("E1", status="failed", prompt="修 bug")
|
||||
self._insert("E2", status="running", prompt="做功能")
|
||||
self._insert("X1", status="failed", project_id="p2",
|
||||
prompt="别项目")
|
||||
worker = self._worker()
|
||||
worker._deliver_marker_tools(worker.get_task("PM"), {
|
||||
"status": "completed",
|
||||
"task_lists": [{"status": None, "q": None, "limit": 20}]})
|
||||
bodies = self._followup_bodies()
|
||||
self.assertEqual(len(bodies), 1)
|
||||
# 全部状态 + 计数在,跨项目任务不在,PM 自己在(本项目)。
|
||||
self.assertIn("failed=1", bodies[0])
|
||||
self.assertIn("修 bug", bodies[0])
|
||||
self.assertIn("做功能", bodies[0])
|
||||
self.assertNotIn("别项目", bodies[0])
|
||||
|
||||
def test_deliver_status_filter_and_limit(self):
|
||||
self._insert("E1", status="failed", prompt="修 bug")
|
||||
self._insert("E2", status="failed", prompt="修另一 bug")
|
||||
self._insert("E3", status="completed", prompt="已做完")
|
||||
worker = self._worker()
|
||||
worker._deliver_marker_tools(worker.get_task("PM"), {
|
||||
"status": "completed",
|
||||
"task_lists": [{"status": "failed", "q": None, "limit": 1}]})
|
||||
bodies = self._followup_bodies()
|
||||
self.assertIn("共 2 条", bodies[0])
|
||||
self.assertIn("还有 1 条未列出", bodies[0])
|
||||
self.assertNotIn("已做完", bodies[0])
|
||||
|
||||
def test_deliver_denied_without_read_permission(self):
|
||||
self._insert("PM2", status="running", created_by="outsider")
|
||||
worker = self._worker()
|
||||
worker._deliver_marker_tools(worker.get_task("PM2"), {
|
||||
"status": "completed",
|
||||
"task_lists": [{"status": None, "q": None, "limit": 20}]})
|
||||
bodies = self._followup_bodies("PM2")
|
||||
self.assertIn("无 read 权限", bodies[0])
|
||||
|
||||
def test_deliver_goal_sender_ignored(self):
|
||||
self._insert("G1", status="running", goal_mode=1)
|
||||
worker = self._worker()
|
||||
worker._deliver_marker_tools(worker.get_task("G1"), {
|
||||
"status": "completed",
|
||||
"task_lists": [{"status": None, "q": None, "limit": 20}]})
|
||||
self.assertEqual(self._followup_bodies("G1"), [])
|
||||
|
||||
def test_injection_project_and_env(self):
|
||||
self._project()
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"INSERT INTO environments(id, project_id, name, branch,"
|
||||
" executor, status, error_text, repo_url, created_by,"
|
||||
" created_at, updated_at)"
|
||||
" VALUES('env1', 'p1', '主环境', 'main', 'docker',"
|
||||
" 'ready', NULL, 'git@github.com:openqj/demo.git',"
|
||||
" 'tester', ?, ?)", (time.time(), time.time()))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
worker = self._worker()
|
||||
task = {"id": "PM", "project_id": "p1", "prompt": "做事",
|
||||
"created_by": "tester", "environment_id": "env1"}
|
||||
out = worker._apply_project_injection(task)
|
||||
self.assertIn("演示项目", out["prompt"])
|
||||
self.assertIn("git@github.com:openqj/demo.git", out["prompt"])
|
||||
self.assertIn("主环境", out["prompt"])
|
||||
self.assertIn("task-list", out["prompt"])
|
||||
self.assertEqual(task["prompt"], "做事")
|
||||
|
||||
def test_injection_unbound_repo_and_goal(self):
|
||||
self._project(repo_url=None)
|
||||
worker = self._worker()
|
||||
goal = {"id": "G1", "project_id": "p1", "prompt": "做事",
|
||||
"goal": "做目标", "created_by": "tester",
|
||||
"goal_mode": 1}
|
||||
out = worker._apply_project_injection(goal)
|
||||
self.assertIn("未绑定", out["prompt"])
|
||||
# goal 任务同时改 goal(执行器取 goal 优先于 prompt)。
|
||||
self.assertIn("未绑定", out["goal"])
|
||||
self.assertIn("演示项目", out["goal"])
|
||||
|
||||
def test_injection_skips_coordinator_resume_missing(self):
|
||||
worker = self._worker()
|
||||
coord = {"id": "C1", "project_id": "p1", "prompt": "x",
|
||||
"coordinator_parts": "a,b"}
|
||||
self.assertIs(worker._apply_project_injection(coord), coord)
|
||||
resume = {"id": "PM", "project_id": "p1", "prompt": "x",
|
||||
"_resume": True}
|
||||
self.assertIs(worker._apply_project_injection(resume), resume)
|
||||
missing = {"id": "PM", "project_id": "nope", "prompt": "x"}
|
||||
self.assertIs(worker._apply_project_injection(missing), missing)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,330 @@
|
||||
"""项目待办标记工具单测:todo-list / todo-create / todo-update
|
||||
解析、sandbox 提取、worker 投递与首轮注入。
|
||||
|
||||
不跑真实模型、不联网:解析是纯函数;worker 投递与注入用
|
||||
init_db 建的临时 SQLite 库,直接调 _deliver_marker_tools 与
|
||||
_apply_todo_injection,不走 run_one 全量流程。
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
try:
|
||||
from cloud.db import connect as db_connect
|
||||
from cloud.marker_tools import (
|
||||
extract_todo_creates,
|
||||
extract_todo_lists,
|
||||
extract_todo_updates,
|
||||
format_todo,
|
||||
)
|
||||
from cloud.sandbox import LocalExecutor, _parse_host_result
|
||||
from cloud.server import init_db
|
||||
from cloud.todos import create_todo, list_todos
|
||||
from cloud.worker import Worker
|
||||
except ImportError: # discover -s cloud/tests 时把项目根目录补进 sys.path
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
||||
from cloud.db import connect as db_connect
|
||||
from cloud.marker_tools import (
|
||||
extract_todo_creates,
|
||||
extract_todo_lists,
|
||||
extract_todo_updates,
|
||||
format_todo,
|
||||
)
|
||||
from cloud.sandbox import LocalExecutor, _parse_host_result
|
||||
from cloud.server import init_db
|
||||
from cloud.todos import create_todo, list_todos
|
||||
from cloud.worker import Worker
|
||||
|
||||
FAKE_ENV = {"XCM_API_KEY": "test-placeholder"}
|
||||
|
||||
|
||||
class ExtractTestCase(unittest.TestCase):
|
||||
def test_list_default_and_alias(self):
|
||||
out = extract_todo_lists("```todo-list\n{}\n```")
|
||||
self.assertEqual(out, [{"status": None, "q": None}])
|
||||
out = extract_todo_lists("```todo_list\n{\"status\": \"blocked\"}\n```")
|
||||
self.assertEqual(out, [{"status": "blocked", "q": None}])
|
||||
|
||||
def test_list_invalid_status_skipped(self):
|
||||
out = extract_todo_lists(
|
||||
"```todo-list\n{\"status\": \"doing\"}\n```\n"
|
||||
"```todo-list\n{\"status\": \"pending\"}\n```")
|
||||
self.assertEqual(len(out), 1)
|
||||
self.assertEqual(out[0]["status"], "pending")
|
||||
|
||||
def test_list_cap_five(self):
|
||||
text = "\n".join("```todo-list\n{}\n```" for _ in range(6))
|
||||
self.assertEqual(len(extract_todo_lists(text)), 5)
|
||||
|
||||
def test_create_valid_and_defaults(self):
|
||||
out = extract_todo_creates(
|
||||
"```todo-create\n{\"title\": \"做 X\"}\n```")
|
||||
self.assertEqual(len(out), 1)
|
||||
self.assertEqual(out[0]["title"], "做 X")
|
||||
self.assertIsNone(out[0]["status"])
|
||||
self.assertIsNone(out[0]["priority"])
|
||||
|
||||
def test_create_invalid_skipped(self):
|
||||
cases = [
|
||||
"{\"title\": \"\"}",
|
||||
"{\"title\": \"x\", \"status\": \"doing\"}",
|
||||
"{\"title\": \"x\", \"priority\": \"urgent\"}",
|
||||
"{\"title\": \"x\", \"due_at\": \"明天\"}",
|
||||
"{\"title\": \"x\", \"sort_order\": 1.5}",
|
||||
"{\"description\": \"no title\"}",
|
||||
]
|
||||
for body in cases:
|
||||
with self.subTest(body=body):
|
||||
self.assertEqual(
|
||||
extract_todo_creates("```todo-create\n%s\n```" % body),
|
||||
[])
|
||||
|
||||
def test_create_due_ms_tolerance(self):
|
||||
out = extract_todo_creates(
|
||||
"```todo-create\n{\"title\": \"x\", \"due_at\": 1700000000000}\n```")
|
||||
self.assertEqual(out[0]["due_at"], 1700000000.0)
|
||||
|
||||
def test_update_valid_and_empty_skipped(self):
|
||||
out = extract_todo_updates(
|
||||
"```todo-update\n{\"id\": \"abc\", \"status\": \"completed\"}\n```")
|
||||
self.assertEqual(out, [{"id": "abc",
|
||||
"fields": {"status": "completed"}}])
|
||||
self.assertEqual(extract_todo_updates(
|
||||
"```todo-update\n{\"id\": \"abc\"}\n```"), [])
|
||||
self.assertEqual(extract_todo_updates(
|
||||
"```todo-update\n{\"id\": \"abc\", \"status\": \"doing\"}\n```"), [])
|
||||
self.assertEqual(extract_todo_updates(
|
||||
"```todo-update\n{\"status\": \"completed\"}\n```"), [])
|
||||
|
||||
def test_update_unknown_field_ignored(self):
|
||||
out = extract_todo_updates(
|
||||
"```todo-update\n{\"id\": \"abc\", \"status\": \"blocked\","
|
||||
" \"note\": \"x\"}\n```")
|
||||
self.assertEqual(out[0]["fields"], {"status": "blocked"})
|
||||
|
||||
def test_format_todo_truncates_and_due_utc(self):
|
||||
line = format_todo({"title": "x" * 120, "status": "pending",
|
||||
"id": "t1", "priority": "high",
|
||||
"due_at": 1700000000, "task_id": None})
|
||||
self.assertIn("…", line)
|
||||
self.assertIn("due=2023-11-14T22:13:20Z", line)
|
||||
|
||||
def test_sandbox_extracts_both_executors(self):
|
||||
text = ("```todo-list\n{}\n```\n"
|
||||
"```todo-create\n{\"title\": \"做 X\"}\n```\n"
|
||||
"```todo-update\n{\"id\": \"t1\", \"status\": \"completed\"}"
|
||||
"\n```")
|
||||
fake = SimpleNamespace(
|
||||
returncode=0,
|
||||
stdout=json.dumps({"status": "completed", "text": text}),
|
||||
stderr="")
|
||||
local = LocalExecutor("/fake/host.py")._result_from_runner("t1", fake)
|
||||
self.assertEqual(len(local["todo_lists"]), 1)
|
||||
self.assertEqual(len(local["todo_creates"]), 1)
|
||||
self.assertEqual(len(local["todo_updates"]), 1)
|
||||
docker = _parse_host_result("t1", fake)
|
||||
self.assertEqual(len(docker["todo_lists"]), 1)
|
||||
self.assertEqual(len(docker["todo_creates"]), 1)
|
||||
self.assertEqual(len(docker["todo_updates"]), 1)
|
||||
|
||||
|
||||
class WorkerTodoTestCase(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
self.db_path = os.path.join(self.tmp.name, "control.db")
|
||||
init_db(self.db_path)
|
||||
self._insert("PM", status="running", created_by="tester")
|
||||
self._member("tester", "maintainer")
|
||||
|
||||
def tearDown(self):
|
||||
self.tmp.cleanup()
|
||||
|
||||
def _insert(self, task_id, status="queued", created_by="tester",
|
||||
project_id="p1", **cols):
|
||||
now = time.time()
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"INSERT INTO tasks(id, project_id, prompt, status,"
|
||||
" idempotency_key, depends_on, priority, created_by,"
|
||||
" created_at, updated_at, last_heartbeat)"
|
||||
" VALUES(?, ?, ?, ?, NULL, '[]', 0, ?, ?, ?, NULL)",
|
||||
(task_id, project_id, "do work", status, created_by,
|
||||
now, now))
|
||||
for key, value in cols.items():
|
||||
conn.execute(
|
||||
"UPDATE tasks SET %s = ? WHERE id = ?" % key,
|
||||
(value, task_id))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _member(self, user_id, role):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS users(id TEXT PRIMARY KEY,"
|
||||
" username TEXT UNIQUE, api_key_hash TEXT,"
|
||||
" created_at REAL)")
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS project_members("
|
||||
"project_id TEXT, user_id TEXT, role TEXT,"
|
||||
" PRIMARY KEY(project_id, user_id))")
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO project_members"
|
||||
"(project_id, user_id, role) VALUES('p1', ?, ?)",
|
||||
(user_id, role))
|
||||
conn.execute(
|
||||
"INSERT OR IGNORE INTO users(id, username, api_key_hash,"
|
||||
" created_at) VALUES(?, ?, '', 0)", (user_id, user_id))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _worker(self):
|
||||
return Worker(self.db_path, "/fake/host.py", env=FAKE_ENV)
|
||||
|
||||
def _seed_todo(self, title, **kw):
|
||||
conn = db_connect(self.db_path)
|
||||
try:
|
||||
return create_todo(conn, "p1", "tester", title, **kw)
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _followup_bodies(self):
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT body FROM task_followups"
|
||||
" WHERE task_id = 'PM'").fetchall()
|
||||
return [row[0] for row in rows]
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _todos(self):
|
||||
conn = db_connect(self.db_path)
|
||||
try:
|
||||
return list_todos(conn, "p1")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def test_deliver_create_update_list(self):
|
||||
worker = self._worker()
|
||||
sender = worker.get_task("PM")
|
||||
todo = self._seed_todo("旧项")
|
||||
worker._deliver_marker_tools(sender, {
|
||||
"status": "completed",
|
||||
"todo_creates": [{"title": "新项", "description": None,
|
||||
"status": None, "priority": "high",
|
||||
"task_id": None, "due_at": None,
|
||||
"sort_order": None}],
|
||||
"todo_updates": [{"id": todo["id"],
|
||||
"fields": {"status": "completed"}}],
|
||||
"todo_lists": [{"status": None, "q": None}]})
|
||||
titles = [t["title"] for t in self._todos()]
|
||||
self.assertIn("新项", titles)
|
||||
updated = [t for t in self._todos() if t["id"] == todo["id"]][0]
|
||||
self.assertEqual(updated["status"], "completed")
|
||||
self.assertIsNotNone(updated["completed_at"])
|
||||
bodies = self._followup_bodies()
|
||||
self.assertEqual(len(bodies), 1)
|
||||
# 列表默认只返回未完成项:已完成的旧项不在列表段里,
|
||||
# 新项在(更新报告行里提到旧项是正常的,不算列表)。
|
||||
self.assertIn("新项", bodies[0])
|
||||
list_section = bodies[0].split("待办列表", 1)[1]
|
||||
self.assertNotIn("旧项", list_section)
|
||||
|
||||
def test_deliver_cross_project_update_rejected(self):
|
||||
worker = self._worker()
|
||||
sender = worker.get_task("PM")
|
||||
conn = db_connect(self.db_path)
|
||||
try:
|
||||
other = create_todo(conn, "p2", "tester", "别项目")
|
||||
finally:
|
||||
conn.close()
|
||||
worker._deliver_marker_tools(sender, {
|
||||
"status": "completed",
|
||||
"todo_creates": [], "todo_lists": [],
|
||||
"todo_updates": [{"id": other["id"],
|
||||
"fields": {"status": "completed"}}]})
|
||||
bodies = self._followup_bodies()
|
||||
self.assertEqual(len(bodies), 1)
|
||||
self.assertIn("不存在", bodies[0])
|
||||
conn = db_connect(self.db_path)
|
||||
try:
|
||||
kept = [t for t in list_todos(conn, "p2")
|
||||
if t["id"] == other["id"]][0]
|
||||
finally:
|
||||
conn.close()
|
||||
self.assertEqual(kept["status"], "pending")
|
||||
|
||||
def test_deliver_denied_without_write_permission(self):
|
||||
self._member("viewer1", "viewer")
|
||||
self._insert("PM2", status="running", created_by="viewer1")
|
||||
worker = self._worker()
|
||||
sender = worker.get_task("PM2")
|
||||
worker._deliver_marker_tools(sender, {
|
||||
"status": "completed",
|
||||
"todo_creates": [{"title": "不应建", "description": None,
|
||||
"status": None, "priority": None,
|
||||
"task_id": None, "due_at": None,
|
||||
"sort_order": None}],
|
||||
"todo_lists": [], "todo_updates": []})
|
||||
self.assertEqual(
|
||||
[t["title"] for t in self._todos()], [])
|
||||
conn = sqlite3.connect(self.db_path)
|
||||
try:
|
||||
bodies = [row[0] for row in conn.execute(
|
||||
"SELECT body FROM task_followups"
|
||||
" WHERE task_id = 'PM2'").fetchall()]
|
||||
finally:
|
||||
conn.close()
|
||||
self.assertIn("无 create_task 权限", bodies[0])
|
||||
|
||||
def test_deliver_goal_sender_ignored(self):
|
||||
self._insert("G1", status="running", goal_mode=1)
|
||||
worker = self._worker()
|
||||
sender = worker.get_task("G1")
|
||||
worker._deliver_marker_tools(sender, {
|
||||
"status": "completed",
|
||||
"todo_creates": [{"title": "不应建", "description": None,
|
||||
"status": None, "priority": None,
|
||||
"task_id": None, "due_at": None,
|
||||
"sort_order": None}],
|
||||
"todo_lists": [], "todo_updates": []})
|
||||
self.assertEqual([t["title"] for t in self._todos()], [])
|
||||
|
||||
def test_injection_lists_open_only(self):
|
||||
self._seed_todo("待做", status="pending")
|
||||
self._seed_todo("进行中", status="in_progress")
|
||||
self._seed_todo("已做完", status="completed")
|
||||
worker = self._worker()
|
||||
task = {"id": "PM", "project_id": "p1", "prompt": "做事",
|
||||
"created_by": "tester"}
|
||||
out = worker._apply_todo_injection(task)
|
||||
self.assertIn("待做", out["prompt"])
|
||||
self.assertIn("进行中", out["prompt"])
|
||||
self.assertNotIn("已做完", out["prompt"])
|
||||
self.assertIn("todo-update", out["prompt"])
|
||||
# 原 prompt 不落库:内存副本改了,调用方传的 dict 不动。
|
||||
self.assertEqual(task["prompt"], "做事")
|
||||
|
||||
def test_injection_skips_goal_and_empty(self):
|
||||
worker = self._worker()
|
||||
goal = {"id": "G1", "project_id": "p1", "prompt": "目标",
|
||||
"created_by": "tester", "goal_mode": 1}
|
||||
self.assertIs(worker._apply_todo_injection(goal), goal)
|
||||
empty = {"id": "PM", "project_id": "p1", "prompt": "做事",
|
||||
"created_by": "tester"}
|
||||
self.assertIs(worker._apply_todo_injection(empty), empty)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user