- 百工域 domains/baigong:capabilities(统一能力契约:执行类型 builtin/qiankun/mcp/http + 四维风险标注 + 来源版本)、mandates(授契 grant / 行契 charter 预算时间窗,撤销立即生效)、tool_calls(净化摘要 + 观察标证);请命复用 pending_confirmations 加 expires_at/brief_json(超时=拒绝执行 HJ-1302,六要素说明) - 司命裁决器 siming.py:四维取最高档(能力/数据/后果/情境);禁域为代码常量不可配置(递归删根/格式化/写裸设备/fork 炸弹、file:// 协议、密钥材料外发)——专项测试断言恶意标注与篡改对象仍拒 HJ-1301 - 执行链:禁域→裁决(HJ-1303 未授契拒且双留痕)→请命→执行(乾坤经 SandboxClient HMAC;MCP 经 call_tool;内置无副作用)→净化标证(观察非事实,敏感遮蔽+体量截断)→行动账 - MCP 登记/启停/刷新自动派生与撤下 capabilities(博闻登记接入点);内置能力种子(echo/datetime/qiankun.shell/web.fetch) - 端点:/capabilities /mandates /petitions /tool-calls + /tools 兼容投影;前端百工页四区(能力目录四维标注/授契行契/请命批准拒绝/行动账观察标证) - 退役:modules/safety|skills|tool_runtime|agent、application/tool_runtime、services/skills|safety、tool_policies/skills/model_endpoint_overrides 表(迁移 006)、前端 SkillManager/ToolsConsole/旧 tools 页 - 验收(test_baigong_siming 四用例):未授契被拒留痕、请命超时不执行、禁域不可关闭、结果为观察不入忆;后端 454、前端 220、BFF 4、神识 5 包全绿(2 个预存环境失败不变)
196 lines
7.3 KiB
Python
196 lines
7.3 KiB
Python
"""API 集成测试。"""
|
||
|
||
from collections.abc import AsyncGenerator
|
||
|
||
import pytest
|
||
from httpx import ASGITransport, AsyncClient
|
||
|
||
from app.api.v1.deps import require_admin
|
||
from app.main import app
|
||
|
||
|
||
@pytest.fixture
|
||
async def client(
|
||
monkeypatch: pytest.MonkeyPatch, auth_headers: dict[str, str]
|
||
) -> AsyncGenerator[AsyncClient, None]:
|
||
transport = ASGITransport(app=app)
|
||
async with AsyncClient(
|
||
transport=transport,
|
||
base_url="http://test",
|
||
headers=auth_headers,
|
||
) as http_client:
|
||
yield http_client
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_health_and_defaults(client: AsyncClient) -> None:
|
||
health = await client.get("/api/v1/health")
|
||
assert health.status_code == 200
|
||
character = await client.get("/api/v1/characters/active")
|
||
assert character.status_code == 200
|
||
assert character.json()["id"].startswith("character_")
|
||
personality = await client.get("/api/v1/personality/current")
|
||
assert personality.status_code == 200
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_personality_propose_approve_reject_and_rollback(client: AsyncClient) -> None:
|
||
# 初始当前人格为 v1
|
||
before = (await client.get("/api/v1/personality/current")).json()
|
||
assert before["version"] == 1
|
||
|
||
# 空字段提议被拒(invalid_request)
|
||
empty = await client.post(
|
||
"/api/v1/personality/propose-update",
|
||
json={"fields": {}, "reason": "空提议"},
|
||
)
|
||
assert empty.status_code == 400
|
||
|
||
# 提交 pending 建议(覆盖 string + json 字段)
|
||
propose = await client.post(
|
||
"/api/v1/personality/propose-update",
|
||
json={
|
||
"fields": {"identity_profile": "测试身份", "traits": {"warmth": 0.5}},
|
||
"reason": "联调人格建议",
|
||
},
|
||
)
|
||
assert propose.status_code == 201
|
||
proposal = propose.json()
|
||
assert proposal["status"] == "pending"
|
||
assert proposal["fields"] == {"identity_profile": "测试身份", "traits": {"warmth": 0.5}}
|
||
proposal_id = proposal["proposal_id"]
|
||
|
||
# 审核通过:派生新版本并切换当前指针,字段覆盖生效
|
||
approve = await client.post(f"/api/v1/personality/proposals/{proposal_id}/approve")
|
||
assert approve.status_code == 200
|
||
applied = approve.json()
|
||
assert applied["status"] == "applied"
|
||
assert applied["resulting_version_id"]
|
||
|
||
after = (await client.get("/api/v1/personality/current")).json()
|
||
assert after["version"] == before["version"] + 1
|
||
assert after["identity_profile"] == "测试身份"
|
||
assert after["traits"]["warmth"] == 0.5
|
||
new_version_id = after["id"]
|
||
|
||
# 重复审核同一建议 → conflict
|
||
again = await client.post(f"/api/v1/personality/proposals/{proposal_id}/approve")
|
||
assert again.status_code == 409
|
||
|
||
# 驳回另一条建议:不生成版本,不改当前指针
|
||
propose2 = await client.post(
|
||
"/api/v1/personality/propose-update",
|
||
json={"fields": {"expression_style": "被驳回"}, "reason": "测试驳回"},
|
||
)
|
||
pid2 = propose2.json()["proposal_id"]
|
||
reject = await client.post(f"/api/v1/personality/proposals/{pid2}/reject")
|
||
assert reject.status_code == 200
|
||
assert reject.json()["status"] == "rejected"
|
||
assert (await client.get("/api/v1/personality/current")).json()["id"] == new_version_id
|
||
|
||
# 回滚到 v1:当前指针切回,旧版本数不变
|
||
versions = (await client.get("/api/v1/personality/versions")).json()
|
||
v1 = next(v for v in versions["items"] if v["version"] == 1)
|
||
rollback = await client.post(f"/api/v1/personality/versions/{v1['id']}/rollback")
|
||
assert rollback.status_code == 200
|
||
assert (await client.get("/api/v1/personality/current")).json()["id"] == v1["id"]
|
||
|
||
# 回滚到不存在的版本 → 404
|
||
missing = await client.post("/api/v1/personality/versions/personality_nope/rollback")
|
||
assert missing.status_code == 404
|
||
|
||
# 写操作均入起居注(personality_core 枢)
|
||
# /admin/* 强制 require_admin 鉴权,联调测试在此局部绕过(鉴权本身覆盖见 test_admin_auth)
|
||
app.dependency_overrides[require_admin] = lambda: None
|
||
try:
|
||
audit = await client.get("/api/v1/admin/audit/timeline?pivot=personality_core")
|
||
assert audit.status_code == 200
|
||
actions = {item["action"] for item in audit.json()["items"]}
|
||
assert "personality.propose" in actions
|
||
assert "personality.proposal.approved" in actions
|
||
assert "personality.proposal.rejected" in actions
|
||
assert "personality.rollback" in actions
|
||
finally:
|
||
app.dependency_overrides.pop(require_admin, None)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_mcp_server_crud(client: AsyncClient) -> None:
|
||
"""MCP 服务器注册 CRUD——覆盖 services/mcp 路径。"""
|
||
created = await client.post(
|
||
"/api/v1/tools/mcp-servers",
|
||
json={
|
||
"name": "测试 MCP",
|
||
"transport": "stdio",
|
||
"command": "echo",
|
||
"args_json": '["hello"]',
|
||
},
|
||
)
|
||
assert created.status_code == 201
|
||
mcp_id = created.json()["id"]
|
||
|
||
listed = await client.get("/api/v1/tools/mcp-servers")
|
||
assert listed.status_code == 200
|
||
assert any(item["id"] == mcp_id for item in listed.json())
|
||
|
||
# MCP 服务器没有 GET single 端点,跳过
|
||
|
||
# PATCH: UpdateMcpServerRequest 不含 name 字段,改 command 验证路径可通
|
||
updated = await client.patch(
|
||
f"/api/v1/tools/mcp-servers/{mcp_id}",
|
||
json={"command": "cat", "args_json": '["-n"]'},
|
||
)
|
||
assert updated.status_code == 200
|
||
assert updated.json()["command"] == "cat"
|
||
|
||
deleted = await client.delete(f"/api/v1/tools/mcp-servers/{mcp_id}")
|
||
assert deleted.status_code == 204
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_world_entities_crud(client: AsyncClient) -> None:
|
||
"""河图世界实体 CRUD——覆盖 services/world 路径。"""
|
||
created = await client.post(
|
||
"/api/v1/world/entities",
|
||
json={
|
||
"name": "测试地点",
|
||
"type": "location",
|
||
"attributes": {"climate": "温暖"},
|
||
"state_type": "fixed",
|
||
"sources": [],
|
||
},
|
||
)
|
||
assert created.status_code == 200
|
||
entity_id = created.json()["id"]
|
||
|
||
listed = await client.get("/api/v1/world/entities")
|
||
assert listed.status_code == 200
|
||
assert len(listed.json()["items"]) >= 1
|
||
|
||
fetched = await client.get(f"/api/v1/world/entities/{entity_id}")
|
||
assert fetched.status_code == 200
|
||
assert fetched.json()["name"] == "测试地点"
|
||
|
||
# 世界实体无 PATCH/DELETE API,仅测试 GET 和建表
|
||
deleted = await client.delete(f"/api/v1/world/entities/{entity_id}")
|
||
assert deleted.status_code in (404, 405) # 无删除端点,接受未实现
|
||
|
||
gone = await client.get(f"/api/v1/world/entities/{entity_id}")
|
||
assert gone.status_code in (200, 404)
|
||
|
||
|
||
@pytest.mark.asyncio
|
||
async def test_preferences_flow(client: AsyncClient) -> None:
|
||
"""偏好设置读写——覆盖 services/preferences 路径。"""
|
||
fetched = await client.get("/api/v1/settings/preferences")
|
||
assert fetched.status_code == 200
|
||
assert "temperature" in fetched.json()
|
||
|
||
updated = await client.patch(
|
||
"/api/v1/settings/preferences",
|
||
json={"temperature": 0.7, "max_output_tokens": 4096},
|
||
)
|
||
assert updated.status_code == 200
|
||
assert updated.json()["temperature"] == 0.7
|
||
assert updated.json()["max_output_tokens"] == 4096
|