初始化提交
This commit is contained in:
commit
887c1f449e
963 files changed
+166869
No files matched your search
@@ -0,0 +1,126 @@
|
||||
"""测试 governance service 中的 goal 事件写入与会话目标自动持久化。"""
|
||||
|
||||
import pytest
|
||||
|
||||
from app.core.errors import AppError
|
||||
from app.models.governance import Goal
|
||||
|
||||
# ── auto_persist_session_goal 测试 ──
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_auto_persist_session_goal_creates_new():
|
||||
"""auto_persist_session_goal: 当没有活跃的 session 目标时,创建一个新的。"""
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from app.services.goal_persist import auto_persist_session_goal
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
goal = await auto_persist_session_goal(session, "char_c", "帮助用户解决编程问题")
|
||||
assert goal is not None
|
||||
assert goal.level == "session"
|
||||
assert goal.character_id == "char_c"
|
||||
assert "编程" in goal.title
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_auto_persist_session_goal_reuses_active():
|
||||
"""auto_persist_session_goal: 已有活跃 session 目标时,不创建新的。"""
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from app.services.goal_persist import auto_persist_session_goal
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
existing = Goal(
|
||||
id="goal_session_existing",
|
||||
character_id="char_d",
|
||||
level="session",
|
||||
status="active",
|
||||
title="已有目标",
|
||||
priority=50,
|
||||
)
|
||||
session.add(existing)
|
||||
await session.flush()
|
||||
goal = await auto_persist_session_goal(session, "char_d", "新对话内容要足够长才能通过验证")
|
||||
assert goal is not None
|
||||
assert goal.id == "goal_session_existing" # 复用了已有目标
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_auto_persist_session_goal_short_content():
|
||||
"""auto_persist_session_goal: 内容过短时返回 None。"""
|
||||
from app.db.session import AsyncSessionLocal
|
||||
from app.services.goal_persist import auto_persist_session_goal
|
||||
|
||||
async with AsyncSessionLocal() as session:
|
||||
goal = await auto_persist_session_goal(session, "char_c", "你好")
|
||||
assert goal is None
|
||||
|
||||
|
||||
# ── governance service CRUD 测试(事件写入通过 emit_goal_event 静默跳过)──
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_goal_success():
|
||||
"""create_goal: 创建目标成功,返回 GoalNode。"""
|
||||
from app.schemas.admin import CreateGoalRequest
|
||||
from app.services.governance import create_goal
|
||||
|
||||
payload = CreateGoalRequest(
|
||||
title="测试目标",
|
||||
description="这是一个测试目标",
|
||||
level="session",
|
||||
priority=50,
|
||||
)
|
||||
result = await create_goal(payload)
|
||||
assert result.title == "测试目标"
|
||||
assert result.level == "session"
|
||||
assert result.status == "pending"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_goal_success():
|
||||
"""update_goal: 更新目标成功。"""
|
||||
from app.schemas.admin import CreateGoalRequest, UpdateGoalRequest
|
||||
from app.services.governance import create_goal, update_goal
|
||||
|
||||
payload = CreateGoalRequest(
|
||||
title="待更新目标",
|
||||
description="稍后更新",
|
||||
level="task",
|
||||
priority=50,
|
||||
)
|
||||
created = await create_goal(payload)
|
||||
|
||||
update = UpdateGoalRequest(title="已更新目标", status="active")
|
||||
updated = await update_goal(created.id, update)
|
||||
assert updated.title == "已更新目标"
|
||||
assert updated.status == "active"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_goal_not_found():
|
||||
"""update_goal: 目标不存在时抛出 AppError。"""
|
||||
from app.schemas.admin import UpdateGoalRequest
|
||||
from app.services.governance import update_goal
|
||||
|
||||
update = UpdateGoalRequest(title="不存在")
|
||||
with pytest.raises(AppError) as exc:
|
||||
await update_goal("goal_nonexistent", update)
|
||||
assert "not_found" in str(exc.value)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_goal_success():
|
||||
"""delete_goal: 删除目标成功。"""
|
||||
from app.schemas.admin import CreateGoalRequest
|
||||
from app.services.governance import create_goal, delete_goal, list_goals
|
||||
|
||||
payload = CreateGoalRequest(
|
||||
title="待删除目标",
|
||||
description="稍后删除",
|
||||
level="task",
|
||||
priority=50,
|
||||
)
|
||||
created = await create_goal(payload)
|
||||
await delete_goal(created.id)
|
||||
all_goals = await list_goals()
|
||||
assert not any(g.id == created.id for g in all_goals)
|
||||
Reference in new issue
Block a user