Spaces:
Sleeping

studiq-backend / tests /test_memory.py
StudIQ Deploy Bot
Deploy from GitHub dfb73d1b6bbef010d2e746b3cd8523d7a82a4fc2
70b5b0f
Raw History Blame Contribute Delete
5.45 kB
"""Tests for the daily memory generation service."""
import uuid
from unittest.mock import AsyncMock, patch
import pytest
from httpx import AsyncClient
from sqlalchemy import select
from app.models.memory import DailyMemory
from app.models.message import Message
# ── Helpers ──────────────────────────────────────────────────────────
async def _create_user_with_messages(client: AsyncClient, msg_count: int = 6):
"""Create a user and insert fake chat messages."""
from tests.conftest import create_authenticated_user
user_id_str, token = await create_authenticated_user(client)
user_id = uuid.UUID(user_id_str)
# Insert messages directly into DB
from tests.conftest import TestSessionLocal
async with TestSessionLocal() as db:
for i in range(msg_count):
role = "user" if i % 2 == 0 else "assistant"
content = (
f"What is normalization? (msg {i})"
if role == "user"
else f"Normalization is the process of organizing data... (msg {i})"
)
db.add(
Message(
user_id=user_id,
role=role,
content=content,
)
)
await db.commit()
return user_id, token
def _auth(token: str) -> dict:
return {"Authorization": f"Bearer {token}"}
# ── Tests ───────────────────────────────────────────────────────────
@pytest.mark.asyncio
@pytest.mark.usefixtures("allow_rate_limit")
async def test_memory_generated_after_threshold(client: AsyncClient):
"""Memory should be generated when message count >= 4."""
user_id, token = await _create_user_with_messages(client, msg_count=6)
mock_summary = "Student asked about normalization in DBMS. They understood 1NF but struggled with 3NF."
with patch(
"app.services.memory_service.call_llm",
new_callable=AsyncMock,
return_value=mock_summary,
):
from app.services.memory_service import maybe_update_daily_memory
from tests.conftest import TestSessionLocal
async with TestSessionLocal() as db:
await maybe_update_daily_memory(user_id, db)
# Check memory was created
async with TestSessionLocal() as db:
result = await db.execute(
select(DailyMemory).where(DailyMemory.user_id == user_id)
)
memory = result.scalar_one_or_none()
assert memory is not None
assert "normalization" in memory.summary.lower()
assert memory.message_count == 6
@pytest.mark.asyncio
@pytest.mark.usefixtures("allow_rate_limit")
async def test_memory_skipped_below_threshold(client: AsyncClient):
"""Memory should NOT be generated when message count < 4."""
user_id, token = await _create_user_with_messages(client, msg_count=2)
from app.services.memory_service import maybe_update_daily_memory
from tests.conftest import TestSessionLocal
async with TestSessionLocal() as db:
await maybe_update_daily_memory(user_id, db)
# Check no memory was created
async with TestSessionLocal() as db:
result = await db.execute(
select(DailyMemory).where(DailyMemory.user_id == user_id)
)
memory = result.scalar_one_or_none()
assert memory is None
@pytest.mark.asyncio
@pytest.mark.usefixtures("allow_rate_limit")
async def test_memory_updates_on_new_messages(client: AsyncClient):
"""Memory should update when 4+ new messages arrive after initial summary."""
user_id, token = await _create_user_with_messages(client, msg_count=6)
mock_summary_1 = "First summary about normalization."
mock_summary_2 = "Updated summary with linked lists added."
from app.services.memory_service import maybe_update_daily_memory
from tests.conftest import TestSessionLocal
# First memory generation
with patch(
"app.services.memory_service.call_llm",
new_callable=AsyncMock,
return_value=mock_summary_1,
):
async with TestSessionLocal() as db:
await maybe_update_daily_memory(user_id, db)
# Add 4 more messages
async with TestSessionLocal() as db:
for i in range(4):
role = "user" if i % 2 == 0 else "assistant"
db.add(
Message(
user_id=user_id,
role=role,
content=f"Tell me about linked lists (extra msg {i})",
)
)
await db.commit()
# Second memory generation
with patch(
"app.services.memory_service.call_llm",
new_callable=AsyncMock,
return_value=mock_summary_2,
):
async with TestSessionLocal() as db:
await maybe_update_daily_memory(user_id, db)
# Check memory was updated
async with TestSessionLocal() as db:
result = await db.execute(
select(DailyMemory).where(DailyMemory.user_id == user_id)
)
memory = result.scalar_one_or_none()
assert memory is not None
assert "linked lists" in memory.summary.lower()
assert memory.message_count == 10