feat(agent): add redis short-term user context cache and align tests

This commit is contained in:
qzl
2026-03-06 12:02:10 +08:00
parent fb8f21bcf3
commit c5ccfc4b88
34 changed files with 2073 additions and 263 deletions
@@ -0,0 +1,43 @@
from __future__ import annotations
from fastapi import HTTPException
from v1.agent.repository import AgentRepository
class _FakeSession:
def __init__(self) -> None:
self.added: list[object] = []
def add(self, obj: object) -> None:
self.added.append(obj)
async def flush(self) -> None:
return None
async def refresh(self, _obj: object) -> None:
return None
async def test_create_session_for_user_creates_session_row() -> None:
session = _FakeSession()
repository = AgentRepository(session=session) # type: ignore[arg-type]
await repository.create_session_for_user(
user_id="00000000-0000-0000-0000-000000000001"
)
session_row = session.added[0]
assert str(getattr(session_row, "user_id")) == "00000000-0000-0000-0000-000000000001"
async def test_create_session_for_user_rejects_invalid_uuid() -> None:
session = _FakeSession()
repository = AgentRepository(session=session) # type: ignore[arg-type]
try:
await repository.create_session_for_user(user_id="invalid-uuid")
raise AssertionError("expected invalid user_id")
except HTTPException as exc:
assert exc.status_code == 422
assert exc.detail == "Invalid user_id"