Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(tests): align memory tests with two-file memory architecture

Bingxi Zhao (Frank)3ec4bc9bf03e
diff --git a/tests/api/test_memory_router.py b/tests/api/test_memory_router.py
index 4edb29ef1..a0dc40982 100644
--- a/tests/api/test_memory_router.py
+++ b/tests/api/test_memory_router.py
@@ -17,18 +17,31 @@ def _build_app() -> FastAPI:
return app
+def _make_snapshot(
+ summary: str = "",
+ profile: str = "",
+ summary_updated_at: str | None = None,
+ profile_updated_at: str | None = None,
+):
+ return type(
+ "Snapshot",
+ (),
+ {
+ "summary": summary,
+ "profile": profile,
+ "summary_updated_at": summary_updated_at,
+ "profile_updated_at": profile_updated_at,
+ },
+ )()
+
+
def test_memory_router_returns_single_document(monkeypatch) -> None:
class FakeMemoryService:
def read_snapshot(self):
- return type(
- "Snapshot",
- (),
- {
- "content": "## Preferences\n- Prefer concise answers.",
- "exists": True,
- "updated_at": "2026-03-13T12:00:00+08:00",
- },
- )()
+ return _make_snapshot(
+ profile="## Preferences\n- Prefer concise answers.",
+ profile_updated_at="2026-03-13T12:00:00+08:00",
+ )
monkeypatch.setattr("deeptutor.api.routers.memory.get_memory_service", lambda: FakeMemoryService())
@@ -36,11 +49,11 @@ def test_memory_router_returns_single_document(monkeypatch) -> None:
response = client.get("/api/v1/memory")
assert response.status_code == 200
- assert response.json() == {
- "content": "## Preferences\n- Prefer concise answers.",
- "exists": True,
- "updated_at": "2026-03-13T12:00:00+08:00",
- }
+ body = response.json()
+ assert body["profile"] == "## Preferences\n- Prefer concise answers."
+ assert body["profile_updated_at"] == "2026-03-13T12:00:00+08:00"
+ assert body["summary"] == ""
+ assert body["summary_updated_at"] is None
def test_memory_router_refreshes_from_session(monkeypatch) -> None:
@@ -50,17 +63,19 @@ def test_memory_router_refreshes_from_session(monkeypatch) -> None:
return None
return {"session_id": session_id}
+ _snapshot = _make_snapshot(
+ profile="## Preferences\n- Prefer concise answers.",
+ summary="## Current Focus\n- Working on memory.",
+ profile_updated_at="2026-03-13T12:10:00+08:00",
+ summary_updated_at="2026-03-13T12:10:00+08:00",
+ )
+
class FakeMemoryService:
async def refresh_from_session(self, session_id, language="en"):
- return type(
- "Result",
- (),
- {
- "content": "## Preferences\n- Prefer concise answers.\n\n## Context\n- Working on memory.",
- "changed": True,
- "updated_at": "2026-03-13T12:10:00+08:00",
- },
- )()
+ return type("Result", (), {"changed": True})()
+
+ def read_snapshot(self):
+ return _snapshot
monkeypatch.setattr("deeptutor.api.routers.memory.get_sqlite_session_store", lambda: FakeStore())
monkeypatch.setattr("deeptutor.api.routers.memory.get_memory_service", lambda: FakeMemoryService())
@@ -72,32 +87,29 @@ def test_memory_router_refreshes_from_session(monkeypatch) -> None:
)
assert response.status_code == 200
- assert response.json()["changed"] is True
- assert response.json()["exists"] is True
- assert "## Context" in response.json()["content"]
+ body = response.json()
+ assert body["changed"] is True
+ assert "Working on memory" in body["summary"]
+ assert "Prefer concise answers" in body["profile"]
def test_memory_router_updates_document(monkeypatch) -> None:
class FakeMemoryService:
- def write_memory(self, content: str):
- return type(
- "Snapshot",
- (),
- {
- "content": content,
- "exists": bool(content),
- "updated_at": "2026-03-13T12:20:00+08:00",
- },
- )()
+ def write_file(self, which, content: str):
+ return _make_snapshot(
+ profile=content if which == "profile" else "",
+ profile_updated_at="2026-03-13T12:20:00+08:00",
+ )
monkeypatch.setattr("deeptutor.api.routers.memory.get_memory_service", lambda: FakeMemoryService())
with TestClient(_build_app()) as client:
response = client.put(
"/api/v1/memory",
- json={"content": "## Preferences\n- Prefer concise answers."},
+ json={"file": "profile", "content": "## Preferences\n- Prefer concise answers."},
)
assert response.status_code == 200
- assert response.json()["saved"] is True
- assert response.json()["content"] == "## Preferences\n- Prefer concise answers."
+ body = response.json()
+ assert body["saved"] is True
+ assert body["profile"] == "## Preferences\n- Prefer concise answers."
diff --git a/tests/services/memory/test_memory_service.py b/tests/services/memory/test_memory_service.py
index 4ef056846..9121dc517 100644
--- a/tests/services/memory/test_memory_service.py
+++ b/tests/services/memory/test_memory_service.py
@@ -4,9 +4,9 @@ from deeptutor.services.memory.service import MemoryService
from deeptutor.services.session.sqlite_store import SQLiteSessionStore
-def test_memory_service_snapshot_is_empty_without_file(tmp_path) -> None:
+def _make_service(tmp_path):
store = SQLiteSessionStore(tmp_path / "chat_history.db")
- service = MemoryService(
+ return MemoryService(
path_service=type(
"PathServiceStub",
(),
@@ -15,32 +15,28 @@ def test_memory_service_snapshot_is_empty_without_file(tmp_path) -> None:
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.content == ""
- assert snapshot.exists is False
- assert snapshot.updated_at is None
+ assert snapshot.summary == ""
+ assert snapshot.profile == ""
+ assert snapshot.summary_updated_at is None
+ assert snapshot.profile_updated_at is None
-async def _no_change_llm(**_kwargs) -> str:
- return "NO_CHANGE"
+async def _no_change_stream(**_kwargs):
+ yield "NO_CHANGE"
-async def _rewrite_llm(**_kwargs) -> str:
- return "## Preferences\n- Prefer concise answers.\n\n## Context\n- Working on DeepTutor memory."
+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:
- store = SQLiteSessionStore(tmp_path / "chat_history.db")
- service = MemoryService(
- path_service=type(
- "PathServiceStub",
- (),
- {"get_memory_dir": lambda self: tmp_path / "memory"},
- )(),
- store=store,
- )
- monkeypatch.setattr("deeptutor.services.memory.service.llm_complete", _rewrite_llm)
+ service = _make_service(tmp_path)
+ monkeypatch.setattr("deeptutor.services.memory.service.llm_stream", _rewrite_stream)
import asyncio
@@ -55,25 +51,16 @@ def test_memory_service_refresh_turn_writes_rewritten_document(monkeypatch, tmp_
)
assert result.changed is True
- assert "## Preferences" in result.content
assert "concise answers" in result.content
- assert service.memory_path.exists()
+ 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:
- store = SQLiteSessionStore(tmp_path / "chat_history.db")
- service = MemoryService(
- path_service=type(
- "PathServiceStub",
- (),
- {"get_memory_dir": lambda self: tmp_path / "memory"},
- )(),
- store=store,
- )
- monkeypatch.setattr("deeptutor.services.memory.service.llm_complete", _no_change_llm)
+ service = _make_service(tmp_path)
+ monkeypatch.setattr("deeptutor.services.memory.service.llm_stream", _no_change_stream)
import asyncio
@@ -89,4 +76,5 @@ def test_memory_service_refresh_turn_skips_when_model_returns_no_change(
assert result.changed is False
assert result.content == ""
- assert service.memory_path.exists() is False
+ assert not service._path("profile").exists()
+ assert not service._path("summary").exists()