mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-06 05:51:24 +08:00
221 lines
8.2 KiB
Python
221 lines
8.2 KiB
Python
"""API surface tests for /api/v1/partners (create / config / soul / assets)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
try:
|
|
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
except Exception: # pragma: no cover
|
|
FastAPI = None
|
|
TestClient = None
|
|
|
|
pytestmark = pytest.mark.skipif(
|
|
FastAPI is None or TestClient is None, reason="fastapi not installed"
|
|
)
|
|
|
|
|
|
@pytest.fixture
|
|
def isolated_root(tmp_path, monkeypatch) -> Path:
|
|
from deeptutor.multi_user import paths
|
|
|
|
project_root = tmp_path
|
|
admin_root = (project_root / "data").resolve()
|
|
monkeypatch.setattr(paths, "PROJECT_ROOT", project_root)
|
|
monkeypatch.setattr(paths, "ADMIN_WORKSPACE_ROOT", admin_root)
|
|
monkeypatch.setattr(paths, "USERS_ROOT", admin_root / "users")
|
|
monkeypatch.setattr(paths, "SYSTEM_ROOT", admin_root / "system")
|
|
monkeypatch.setattr(paths, "_path_services", {})
|
|
admin_root.mkdir(parents=True, exist_ok=True)
|
|
return admin_root
|
|
|
|
|
|
@pytest.fixture
|
|
def client(isolated_root, monkeypatch) -> TestClient:
|
|
import deeptutor.api.routers.partners as partners_router_mod
|
|
from deeptutor.services.partners.manager import PartnerManager
|
|
|
|
# Fresh manager per test so the module-level singleton can't leak
|
|
# tmp-path state across tests.
|
|
mgr = PartnerManager()
|
|
monkeypatch.setattr(partners_router_mod, "get_partner_manager", lambda: mgr)
|
|
partners_router_mod._start_locks.clear()
|
|
|
|
app = FastAPI()
|
|
app.include_router(partners_router_mod.router, prefix="/api/v1/partners")
|
|
return TestClient(app)
|
|
|
|
|
|
def _create(client: TestClient, **overrides):
|
|
payload = {
|
|
"name": "Ada",
|
|
"description": "study partner",
|
|
"soul": {"source": "custom", "content": "# Soul\nBe rigorous."},
|
|
"start": False,
|
|
**overrides,
|
|
}
|
|
return client.post("/api/v1/partners", json=payload)
|
|
|
|
|
|
class TestCreate:
|
|
def test_create_returns_masked_config(self, client):
|
|
res = _create(
|
|
client,
|
|
channels={"telegram": {"enabled": True, "token": "123:ABC"}},
|
|
enabled_tools=["web_search"],
|
|
mcp_tools=[],
|
|
)
|
|
assert res.status_code == 200
|
|
body = res.json()
|
|
assert body["partner_id"] == "ada"
|
|
assert body["channels"]["telegram"]["token"] == "***"
|
|
assert body["enabled_tools"] == ["web_search"]
|
|
assert body["mcp_tools"] == []
|
|
assert body["soul_origin"] == {"type": "custom", "id": ""}
|
|
assert body["provisioning"]["errors"] == []
|
|
|
|
def test_duplicate_id_conflicts(self, client):
|
|
assert _create(client).status_code == 200
|
|
assert _create(client).status_code == 409
|
|
|
|
def test_top_level_delivery_flags_rejected(self, client):
|
|
res = _create(client, channels={"send_progress": False})
|
|
assert res.status_code == 422
|
|
|
|
def test_create_from_library_soul(self, client):
|
|
res = _create(
|
|
client,
|
|
partner_id="mathy",
|
|
soul={"source": "library", "id": "math-tutor"},
|
|
)
|
|
assert res.status_code == 200
|
|
soul = client.get("/api/v1/partners/mathy/soul").json()
|
|
assert "math tutor" in soul["content"].lower()
|
|
|
|
def test_create_with_unknown_library_soul_404(self, client):
|
|
res = _create(client, soul={"source": "library", "id": "ghost"})
|
|
assert res.status_code == 404
|
|
|
|
|
|
class TestConfigAndSoul:
|
|
def test_get_masks_secrets_by_default(self, client):
|
|
_create(client, channels={"telegram": {"enabled": True, "token": "raw"}})
|
|
body = client.get("/api/v1/partners/ada").json()
|
|
assert body["channels"]["telegram"]["token"] == "***"
|
|
body = client.get("/api/v1/partners/ada?include_secrets=true").json()
|
|
assert body["channels"]["telegram"]["token"] == "raw"
|
|
|
|
def test_patch_updates_tools_and_clears(self, client):
|
|
_create(client, enabled_tools=["web_search", "paper_search"])
|
|
res = client.patch(
|
|
"/api/v1/partners/ada",
|
|
json={"enabled_tools": [], "mcp_tools": ["mcp_x_y"]},
|
|
)
|
|
assert res.status_code == 200
|
|
body = client.get("/api/v1/partners/ada").json()
|
|
assert body["enabled_tools"] == []
|
|
assert body["mcp_tools"] == ["mcp_x_y"]
|
|
|
|
def test_avatar_roundtrip_and_validation(self, client):
|
|
_create(client)
|
|
avatar = "data:image/png;base64,iVBORw0KGgo="
|
|
res = client.patch("/api/v1/partners/ada", json={"avatar": avatar})
|
|
assert res.status_code == 200
|
|
assert client.get("/api/v1/partners/ada").json()["avatar"] == avatar
|
|
|
|
# Clearing works; junk and oversized payloads are rejected.
|
|
assert client.patch("/api/v1/partners/ada", json={"avatar": ""}).status_code == 200
|
|
assert client.get("/api/v1/partners/ada").json()["avatar"] == ""
|
|
res = client.patch(
|
|
"/api/v1/partners/ada", json={"avatar": "https://evil.example/x.png"}
|
|
)
|
|
assert res.status_code == 422
|
|
res = client.patch(
|
|
"/api/v1/partners/ada",
|
|
json={"avatar": "data:image/png;base64," + "A" * 200_001},
|
|
)
|
|
assert res.status_code == 422
|
|
|
|
def test_soul_roundtrip(self, client):
|
|
_create(client)
|
|
res = client.put("/api/v1/partners/ada/soul", json={"content": "# Soul\nUpdated."})
|
|
assert res.status_code == 200
|
|
assert client.get("/api/v1/partners/ada/soul").json()["content"] == "# Soul\nUpdated."
|
|
|
|
def test_404_for_unknown_partner(self, client):
|
|
assert client.get("/api/v1/partners/ghost").status_code == 404
|
|
assert client.get("/api/v1/partners/ghost/soul").status_code == 404
|
|
|
|
|
|
class TestAssets:
|
|
def _seed_skill(self, admin_root: Path, name="focus"):
|
|
skill = admin_root / "user" / "workspace" / "skills" / name
|
|
skill.mkdir(parents=True)
|
|
(skill / "SKILL.md").write_text(
|
|
f"---\nname: {name}\ndescription: d\n---\nBody", encoding="utf-8"
|
|
)
|
|
|
|
def test_add_list_remove_assets(self, client, isolated_root):
|
|
self._seed_skill(isolated_root)
|
|
_create(client)
|
|
|
|
res = client.post("/api/v1/partners/ada/assets", json={"skills": ["focus"]})
|
|
assert res.status_code == 200
|
|
assert res.json()["copied"]["skills"] == ["focus"]
|
|
assert [s["name"] for s in res.json()["assets"]["skills"]] == ["focus"]
|
|
|
|
res = client.delete("/api/v1/partners/ada/assets/skill/focus")
|
|
assert res.status_code == 200
|
|
assert res.json()["assets"]["skills"] == []
|
|
|
|
def test_unknown_asset_reported_in_errors(self, client):
|
|
_create(client)
|
|
res = client.post("/api/v1/partners/ada/assets", json={"skills": ["ghost"]})
|
|
assert res.status_code == 200
|
|
assert res.json()["errors"][0]["type"] == "skill"
|
|
|
|
|
|
class TestSoulLibraryEndpoints:
|
|
def test_souls_crud(self, client):
|
|
res = client.get("/api/v1/partners/souls")
|
|
assert res.status_code == 200
|
|
assert any(s["id"] == "math-tutor" for s in res.json())
|
|
|
|
res = client.post(
|
|
"/api/v1/partners/souls",
|
|
json={"id": "custom-soul", "name": "Custom", "content": "# Soul"},
|
|
)
|
|
assert res.status_code == 200
|
|
assert client.get("/api/v1/partners/souls/custom-soul").status_code == 200
|
|
assert (
|
|
client.put("/api/v1/partners/souls/custom-soul", json={"name": "Renamed"}).json()[
|
|
"name"
|
|
]
|
|
== "Renamed"
|
|
)
|
|
assert client.delete("/api/v1/partners/souls/custom-soul").status_code == 200
|
|
assert client.get("/api/v1/partners/souls/custom-soul").status_code == 404
|
|
|
|
def test_soul_sources_shape(self, client):
|
|
body = client.get("/api/v1/partners/soul-sources").json()
|
|
assert "library" in body and "personas" in body
|
|
|
|
|
|
class TestHistory:
|
|
def test_history_reads_session_store(self, client, isolated_root):
|
|
_create(client)
|
|
sessions = isolated_root / "partners" / "ada" / "sessions"
|
|
sessions.mkdir(parents=True, exist_ok=True)
|
|
(sessions / "telegram_42.jsonl").write_text(
|
|
json.dumps({"role": "user", "content": "hi", "timestamp": "2026-01-01T00:00:00"})
|
|
+ "\n",
|
|
encoding="utf-8",
|
|
)
|
|
res = client.get("/api/v1/partners/ada/history")
|
|
assert res.status_code == 200
|
|
assert res.json()[0]["content"] == "hi"
|