80 lines
2.7 KiB
Python
80 lines
2.7 KiB
Python
"""运行时限制测试。"""
|
|
|
|
import pytest
|
|
|
|
from app.core.config import settings
|
|
from app.core.errors import AppError
|
|
from app.db.session import AsyncSessionLocal
|
|
from app.repositories import characters as character_repo
|
|
from app.services.runtime import append_event, cancel_run, create_run
|
|
|
|
|
|
async def _admin_character_id() -> str:
|
|
"""从种子数据中获取管理员用户的活跃角色 ID。"""
|
|
from sqlalchemy import select
|
|
|
|
from app.models.user import User
|
|
|
|
async with AsyncSessionLocal() as session:
|
|
result = await session.execute(select(User).where(User.is_admin.is_(True)).limit(1))
|
|
admin = result.scalar_one_or_none()
|
|
assert admin is not None
|
|
char = await character_repo.get_active_character(session, admin.id)
|
|
assert char is not None
|
|
return char.id
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_event_payload_limit(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(settings, "event_payload_max_bytes", 20)
|
|
character_id = await _admin_character_id()
|
|
async with AsyncSessionLocal() as db:
|
|
run = await create_run(
|
|
db, character_id=character_id, session_id=None, model_hint="default-chat"
|
|
)
|
|
with pytest.raises(AppError) as exc:
|
|
await append_event(
|
|
db,
|
|
run_id=run.id,
|
|
character_id=character_id,
|
|
event_type="message.delta",
|
|
payload={"delta": "x" * 100},
|
|
)
|
|
assert exc.value.code == "payload_too_large"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_run_max_events(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(settings, "run_max_events", 1)
|
|
character_id = await _admin_character_id()
|
|
async with AsyncSessionLocal() as db:
|
|
run = await create_run(
|
|
db, character_id=character_id, session_id=None, model_hint="default-chat"
|
|
)
|
|
await append_event(
|
|
db, run_id=run.id, character_id=character_id, event_type="message.delta", payload={}
|
|
)
|
|
with pytest.raises(AppError) as exc:
|
|
await append_event(
|
|
db,
|
|
run_id=run.id,
|
|
character_id=character_id,
|
|
event_type="message.delta",
|
|
payload={},
|
|
)
|
|
assert exc.value.code == "context_overflow"
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_cancel_run() -> None:
|
|
character_id = await _admin_character_id()
|
|
async with AsyncSessionLocal() as db:
|
|
run = await create_run(
|
|
db, character_id=character_id, session_id=None, model_hint="default-chat"
|
|
)
|
|
await db.commit()
|
|
response = await cancel_run(run.id)
|
|
assert response.status == "cancelled"
|
|
same = await cancel_run(run.id)
|
|
assert same.status == "cancelled"
|