Files
DeepTutor/tests/api/test_notebook_router.py
Bingxi Zhao (Frank) 28c11e2ae8 feat: update notes, chat interactions and workspace UI on preview
Remove course pages, APIs and the course_study capability. Add anchored note
threads and reviewed learning-block generation, queued and withdrawable chat
messages, quote attachments, side chats, and the calendar/favorites/navigation
updates.

Keep preview's existing backend architecture. Wire the new features through
its Python application and existing chat engine, retain legacy selected-text
requests, persist mid-turn messages in replay, and restore note context when
regenerating. Leave stored user data intact.

Validate frontend contracts, dependency boundaries, types, 1290 node tests,
840 rendered tests, lint, localization and the production build. Exercise
session/notes/API regressions, 53 chat-engine tests and 20 Python/native
integration tests with isolated runtime data.
2026-10-26 22:13:34 +08:00

602 lines
21 KiB
Python

from __future__ import annotations
import asyncio
import base64
from hashlib import sha256
import importlib
from pathlib import Path
import pytest
pytest.importorskip("fastapi")
FastAPI = pytest.importorskip("fastapi").FastAPI
TestClient = pytest.importorskip("fastapi.testclient").TestClient
notebook_router = importlib.import_module("deeptutor.api.routers.question_notebook").router
sessions_router = importlib.import_module("deeptutor.api.routers.sessions").router
from deeptutor.services.session.sqlite_store import SQLiteSessionStore
from deeptutor.services.storage.attachment_store import LocalDiskAttachmentStore
def _build_app(store: SQLiteSessionStore) -> FastAPI:
app = FastAPI()
app.include_router(notebook_router, prefix="/api/question-notebook")
app.include_router(sessions_router, prefix="/api/sessions")
return app
@pytest.fixture
def store(tmp_path: Path, monkeypatch) -> SQLiteSessionStore:
instance = SQLiteSessionStore(db_path=tmp_path / "router-test.db")
monkeypatch.setattr(
"deeptutor.api.routers.question_notebook.get_sqlite_session_store",
lambda: instance,
)
monkeypatch.setattr(
"deeptutor.api.routers.sessions.get_sqlite_session_store",
lambda: instance,
)
return instance
def _quiz_answers():
return [
{
"question_id": "q1",
"question": "Capital of France?",
"question_type": "choice",
"options": {"A": "Berlin", "B": "Paris"},
"user_answer": "A",
"correct_answer": "B",
"explanation": "Paris is the capital.",
"difficulty": "easy",
"is_correct": False,
},
{
"question_id": "q2",
"question": "2+2?",
"question_type": "choice",
"options": {"A": "3", "B": "4"},
"user_answer": "B",
"correct_answer": "B",
"is_correct": True,
},
]
def _seed_session(
store: SQLiteSessionStore,
session_id: str,
*questions: tuple[str, str, bool],
) -> str:
session = asyncio.run(
store.create_session(title=f"Session {session_id}", session_id=session_id)
)
asyncio.run(store.upsert_notebook_entries(session["id"], _make_items(*questions)))
return session["id"]
def _make_items(*questions: tuple[str, str, bool]) -> list[dict]:
return [
{"question_id": question_id, "question": question, "is_correct": is_correct}
for question_id, question, is_correct in questions
]
def test_list_entries_empty(store: SQLiteSessionStore) -> None:
with TestClient(_build_app(store)) as client:
resp = client.get("/api/question-notebook/entries")
assert resp.status_code == 200
assert resp.json() == {"items": [], "total": 0}
def test_upsert_entry_persists_base64_answer_image(
store: SQLiteSessionStore, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
session = asyncio.run(store.create_session(title="Image answer"))
attachment_store = LocalDiskAttachmentStore(root=tmp_path / "attachments")
monkeypatch.setattr(
"deeptutor.api.routers.question_notebook.get_attachment_store",
lambda: attachment_store,
)
with TestClient(_build_app(store)) as client:
response = client.post(
"/api/question-notebook/entries/upsert",
json={
"session_id": session["id"],
"question_id": "image-question",
"question": "Identify the diagram.",
"user_answer_images": [
{
"id": "answer-image-1",
"base64": base64.b64encode(b"image-bytes").decode("ascii"),
"filename": "answer.png",
"mime_type": "image/png",
}
],
},
)
assert response.status_code == 200
assert response.json()["user_answer_images"] == [
{
"id": "answer-image-1",
# Pinned to the originating data scope; default workspace = empty id.
"url": (f"/files/attachments/{session['id']}/answer-image-1/answer.png?dt_workspace="),
"filename": "answer.png",
"mime_type": "image/png",
}
]
stored_path = attachment_store.resolve_path(
session_id=session["id"],
attachment_id="answer-image-1",
filename="answer.png",
)
assert stored_path is not None
assert stored_path.read_bytes() == b"image-bytes"
def test_independent_entry_uses_stable_answer_image_owner_and_cleans_up(
store: SQLiteSessionStore, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
attachment_store = LocalDiskAttachmentStore(root=tmp_path / "attachments")
monkeypatch.setattr(
"deeptutor.api.routers.question_notebook.get_attachment_store",
lambda: attachment_store,
)
origin_ref = "document:guide.pdf:page-4"
owner = (
"question-notebook-" + sha256(f"document_analysis:{origin_ref}".encode()).hexdigest()[:24]
)
with TestClient(_build_app(store)) as client:
response = client.post(
"/api/question-notebook/entries/upsert",
json={
"origin_type": "document_analysis",
"origin_ref": origin_ref,
"question_id": "image-question",
"question": "Identify the diagram.",
"user_answer_images": [
{
"id": "answer-image-1",
"base64": base64.b64encode(b"image-bytes").decode("ascii"),
"filename": "answer.png",
"mime_type": "image/png",
}
],
},
)
assert response.status_code == 200
entry_id = response.json()["id"]
assert response.json()["user_answer_images"][0]["url"].startswith(
f"/files/attachments/{owner}/answer-image-1/"
)
stored_path = attachment_store.resolve_path(
session_id=owner,
attachment_id="answer-image-1",
filename="answer.png",
)
assert stored_path is not None and stored_path.read_bytes() == b"image-bytes"
deleted = client.delete(f"/api/question-notebook/entries/{entry_id}")
assert deleted.status_code == 200
assert not stored_path.exists()
def test_list_entries_includes_all_sessions(store: SQLiteSessionStore) -> None:
_seed_session(
store,
"session-a",
("a1", "A one?", False),
)
_seed_session(
store,
"session-b",
("b1", "B one?", True),
)
with TestClient(_build_app(store)) as client:
response = client.get("/api/question-notebook/entries")
assert response.status_code == 200
body = response.json()
assert body["total"] == 2
assert {item["question_id"] for item in body["items"]} == {"a1", "b1"}
def test_quiz_results_populates_notebook(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session(title="Quiz Session"))
sid = session["id"]
with TestClient(_build_app(store)) as client:
resp = client.post(
f"/api/sessions/{sid}/quiz-results",
json={"answers": _quiz_answers()},
)
assert resp.status_code == 200
body = resp.json()
assert body["recorded"] is True
assert body["notebook_count"] == 2
assert "[Quiz Performance]" in body["content"]
listing = client.get("/api/question-notebook/entries")
assert listing.status_code == 200
items = listing.json()["items"]
assert len(items) == 2
def test_quiz_results_upserts_on_retry(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session())
sid = session["id"]
with TestClient(_build_app(store)) as client:
client.post(f"/api/sessions/{sid}/quiz-results", json={"answers": _quiz_answers()})
updated = _quiz_answers()
updated[0]["user_answer"] = "B"
updated[0]["is_correct"] = True
client.post(f"/api/sessions/{sid}/quiz-results", json={"answers": updated})
listing = client.get("/api/question-notebook/entries").json()
assert listing["total"] == 2
q1 = next(e for e in listing["items"] if e["question_id"] == "q1")
assert q1["is_correct"] is True
assert q1["user_answer"] == "B"
def test_bookmark_toggle(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session())
asyncio.run(
store.upsert_notebook_entries(
session["id"],
[
{
"question_id": "q1",
"question": "Q?",
"is_correct": False,
}
],
)
)
eid = asyncio.run(store.list_notebook_entries())["items"][0]["id"]
with TestClient(_build_app(store)) as client:
resp = client.patch(
f"/api/question-notebook/entries/{eid}",
json={"bookmarked": True},
)
assert resp.status_code == 200
bm = client.get("/api/question-notebook/entries?bookmarked=true").json()
assert bm["total"] == 1
client.patch(f"/api/question-notebook/entries/{eid}", json={"bookmarked": False})
bm2 = client.get("/api/question-notebook/entries?bookmarked=true").json()
assert bm2["total"] == 0
def test_delete_entry(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session())
asyncio.run(
store.upsert_notebook_entries(
session["id"],
[
{
"question_id": "q1",
"question": "Q?",
"is_correct": False,
}
],
)
)
eid = asyncio.run(store.list_notebook_entries())["items"][0]["id"]
with TestClient(_build_app(store)) as client:
assert client.delete(f"/api/question-notebook/entries/{eid}").status_code == 200
assert client.delete(f"/api/question-notebook/entries/{eid}").status_code == 404
def test_category_crud_and_association(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session())
asyncio.run(
store.upsert_notebook_entries(
session["id"],
[
{
"question_id": "q1",
"question": "Q?",
"is_correct": False,
}
],
)
)
eid = asyncio.run(store.list_notebook_entries())["items"][0]["id"]
with TestClient(_build_app(store)) as client:
cat_resp = client.post(
"/api/question-notebook/categories",
json={"name": "Math"},
)
assert cat_resp.status_code == 201
cat_id = cat_resp.json()["id"]
cats = client.get("/api/question-notebook/categories").json()
assert len(cats) == 1
assert cats[0]["name"] == "Math"
add_resp = client.post(
f"/api/question-notebook/entries/{eid}/categories",
json={"category_id": cat_id},
)
assert add_resp.status_code == 200
by_cat = client.get(f"/api/question-notebook/entries?category_id={cat_id}").json()
assert by_cat["total"] == 1
rm_resp = client.delete(f"/api/question-notebook/entries/{eid}/categories/{cat_id}")
assert rm_resp.status_code == 200
by_cat2 = client.get(f"/api/question-notebook/entries?category_id={cat_id}").json()
assert by_cat2["total"] == 0
client.patch(f"/api/question-notebook/categories/{cat_id}", json={"name": "Algebra"})
cats2 = client.get("/api/question-notebook/categories").json()
assert cats2[0]["name"] == "Algebra"
client.delete(f"/api/question-notebook/categories/{cat_id}")
assert client.get("/api/question-notebook/categories").json() == []
def test_lookup_entry_by_question(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session())
asyncio.run(
store.upsert_notebook_entries(
session["id"],
[
{
"question_id": "q1",
"question": "Q?",
"is_correct": False,
}
],
)
)
with TestClient(_build_app(store)) as client:
resp = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": session["id"], "question_id": "q1"},
)
assert resp.status_code == 200
assert resp.json()["question_id"] == "q1"
resp404 = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": session["id"], "question_id": "nope"},
)
assert resp404.status_code == 404
def test_quiz_state_isolated_per_turn(store: SQLiteSessionStore) -> None:
"""Regression test for #487 — two quizzes in the same chat session must
not share answer state, even when the positional ``question_id`` (e.g.
``q_1``) collides. The producing turn_id scopes notebook entries.
"""
session = asyncio.run(store.create_session())
sid = session["id"]
with TestClient(_build_app(store)) as client:
first = _quiz_answers()
resp1 = client.post(
f"/api/sessions/{sid}/quiz-results",
json={"answers": first, "turn_id": "turn_A"},
)
assert resp1.status_code == 200
assert resp1.json()["notebook_count"] == 2
second = _quiz_answers()
second[0]["user_answer"] = ""
second[0]["is_correct"] = False
resp2 = client.post(
f"/api/sessions/{sid}/quiz-results",
json={"answers": second, "turn_id": "turn_B"},
)
assert resp2.status_code == 200
assert resp2.json()["notebook_count"] == 2
listing = client.get("/api/question-notebook/entries").json()
assert listing["total"] == 4
# Looking up q1 scoped to the first turn returns the first quiz's
# answer, not the second.
scoped_a = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": sid, "question_id": "q1", "turn_id": "turn_A"},
)
assert scoped_a.status_code == 200
assert scoped_a.json()["user_answer"] == "A"
assert scoped_a.json()["turn_id"] == "turn_A"
# The second turn has no recorded answer for q1.
scoped_b = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": sid, "question_id": "q1", "turn_id": "turn_B"},
)
assert scoped_b.status_code == 200
assert scoped_b.json()["user_answer"] == ""
assert scoped_b.json()["turn_id"] == "turn_B"
def test_lookup_without_turn_id_only_matches_legacy_namespace(
store: SQLiteSessionStore,
) -> None:
"""Regression test for #677 — a lookup that doesn't pass turn_id must
never see turn-scoped rows (positional ids like ``q_1`` repeat across
quizzes, so a cross-turn fallback leaks the previous quiz's answers into
a new quiz). It only matches the legacy namespace (turn_id='')."""
session = asyncio.run(store.create_session())
sid = session["id"]
asyncio.run(
store.upsert_notebook_entries(
sid,
[
{
"turn_id": "turn_A",
"question_id": "q1",
"question": "Q?",
"user_answer": "A",
"is_correct": False,
}
],
)
)
with TestClient(_build_app(store)) as client:
# Turn-scoped rows are invisible without their turn_id.
resp = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": sid, "question_id": "q1"},
)
assert resp.status_code == 404
# Pre-turn-scoping rows (migrated with turn_id='') stay reachable.
asyncio.run(
store.upsert_notebook_entries(
sid,
[
{
"turn_id": "",
"question_id": "q1",
"question": "Q?",
"user_answer": "B",
"is_correct": True,
}
],
)
)
legacy = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": sid, "question_id": "q1"},
)
assert legacy.status_code == 200
assert legacy.json()["turn_id"] == ""
assert legacy.json()["user_answer"] == "B"
def test_lookup_missing_entry_returns_404_by_default(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session())
sid = session["id"]
with TestClient(_build_app(store)) as client:
resp = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": sid, "question_id": "absent"},
)
assert resp.status_code == 404
def test_lookup_missing_entry_returns_204_when_missing_ok(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session())
sid = session["id"]
with TestClient(_build_app(store)) as client:
resp = client.get(
"/api/question-notebook/entries/lookup/by-question",
params={"session_id": sid, "question_id": "absent", "missing_ok": "true"},
)
assert resp.status_code == 204
assert resp.content == b""
def test_assessment_v2_fields_round_trip_and_filters(store: SQLiteSessionStore) -> None:
session = asyncio.run(store.create_session(title="Review"))
asyncio.run(
store.upsert_notebook_entries(
session["id"],
[
{
"question_id": "graded-wrong",
"question": "Wrong?",
"is_correct": False,
"result": "incorrect",
"assessment_type": "quiz",
"source": "mastery_path",
"mastery_path_id": "path-1",
"knowledge_point_id": "kp-1",
},
{
"question_id": "ungraded",
"question": "Pending?",
"is_correct": False,
"result": "ungraded",
"assessment_type": "review",
},
{
"question_id": "book-old",
"question": "Chapter?",
"is_correct": False,
"source": "book",
"material_id": "book-1",
},
],
)
)
with TestClient(_build_app(store)) as client:
all_rows = client.get("/api/question-notebook/entries").json()
assert all_rows["total"] == 3
ungraded = next(item for item in all_rows["items"] if item["question_id"] == "ungraded")
assert ungraded["result"] == "ungraded"
old_book = next(item for item in all_rows["items"] if item["question_id"] == "book-old")
assert old_book["source"] == "book"
assert old_book["result"] == "incorrect"
wrong = client.get("/api/question-notebook/entries", params={"is_correct": "false"}).json()
assert {item["question_id"] for item in wrong["items"]} == {"graded-wrong", "book-old"}
by_type = client.get(
"/api/question-notebook/entries", params={"assessment_type": "quiz"}
).json()
assert {item["question_id"] for item in by_type["items"]} == {"graded-wrong"}
by_result = client.get(
"/api/question-notebook/entries", params={"result": "ungraded"}
).json()
assert {item["question_id"] for item in by_result["items"]} == {"ungraded"}
by_link = client.get(
"/api/question-notebook/entries",
params={"mastery_path_id": "path-1", "knowledge_point_id": "kp-1"},
).json()
assert {item["question_id"] for item in by_link["items"]} == {"graded-wrong"}
eid = next(
item["id"] for item in all_rows["items"] if item["question_id"] == "graded-wrong"
)
client.patch(f"/api/question-notebook/entries/{eid}", json={"resolved": True})
resolved = client.get("/api/question-notebook/entries", params={"resolved": "true"}).json()
assert eid in {item["id"] for item in resolved["items"]}
client.patch(f"/api/question-notebook/entries/{eid}", json={"resolved": False})
unresolved = client.get(
"/api/question-notebook/entries", params={"resolved": "false", "is_correct": "false"}
).json()
assert eid in {item["id"] for item in unresolved["items"]}
client.post(
"/api/question-notebook/entries/upsert",
json={
"session_id": session["id"],
"question_id": "graded-wrong",
"question": "Wrong?",
"is_correct": False,
"result": "incorrect",
"assessment_type": "quiz",
"source": "mastery_path",
"mastery_path_id": "path-1",
"knowledge_point_id": "kp-1",
},
)
after = client.get("/api/question-notebook/entries").json()
assert after["total"] == 3