Files
DeepTutor/tests/services/memory/test_memory_service.py
Bingxi Zhao (Frank) 3ec4bc9bf0 fix(tests): align memory tests with two-file memory architecture
Memory service was refactored from single-file (content/exists/updated_at)
to two-file (summary/profile) architecture, but tests were not updated.

Made-with: Cursor
2026-04-07 23:44:35 +08:00

81 lines
2.3 KiB
Python

from __future__ import annotations
from deeptutor.services.memory.service import MemoryService
from deeptutor.services.session.sqlite_store import SQLiteSessionStore
def _make_service(tmp_path):
store = SQLiteSessionStore(tmp_path / "chat_history.db")
return MemoryService(
path_service=type(
"PathServiceStub",
(),
{"get_memory_dir": lambda self: tmp_path / "memory"},
)(),
store=store,
)
def test_memory_service_snapshot_is_empty_without_file(tmp_path) -> None:
service = _make_service(tmp_path)
snapshot = service.read_snapshot()
assert snapshot.summary == ""
assert snapshot.profile == ""
assert snapshot.summary_updated_at is None
assert snapshot.profile_updated_at is None
async def _no_change_stream(**_kwargs):
yield "NO_CHANGE"
async def _rewrite_stream(**_kwargs):
yield "## Preferences\n- Prefer concise answers.\n\n## Context\n- Working on DeepTutor memory."
def test_memory_service_refresh_turn_writes_rewritten_document(monkeypatch, tmp_path) -> None:
service = _make_service(tmp_path)
monkeypatch.setattr("deeptutor.services.memory.service.llm_stream", _rewrite_stream)
import asyncio
result = asyncio.run(
service.refresh_from_turn(
user_message="Please remember that I like concise answers.",
assistant_message="Sure, I'll keep answers concise.",
session_id="s1",
capability="chat",
language="en",
)
)
assert result.changed is True
assert "concise answers" in result.content
assert service._path("profile").exists() or service._path("summary").exists()
def test_memory_service_refresh_turn_skips_when_model_returns_no_change(
monkeypatch,
tmp_path,
) -> None:
service = _make_service(tmp_path)
monkeypatch.setattr("deeptutor.services.memory.service.llm_stream", _no_change_stream)
import asyncio
result = asyncio.run(
service.refresh_from_turn(
user_message="What is 2+2?",
assistant_message="4",
session_id="s1",
capability="chat",
language="en",
)
)
assert result.changed is False
assert result.content == ""
assert not service._path("profile").exists()
assert not service._path("summary").exists()