Files
DeepTutor/tests/api/test_memory_resolver.py
Bingxi Zhao (Frank) 6153d8c092 release: v1.4.0 — release notes, version bump, repo hygiene
- Add assets/releases/ver1-4-0.md covering deltas since v1.4.0-beta
  (reasoning_effort normalization, tool-schema fallback, restart-safe
  turn runtime, frontend placeholder hardening, CLI chat REPL config).
- Bump deeptutor/__version__ to 1.4.0.
- Update README Releases section: v1.4.0 at top, install opt-in note
  updated for stable.
- Fix pre-existing pre-commit failures so the v1.4.0 cut runs clean:
  bandit B324 (mark _sha1 as non-security), mypy no-redef in
  document.py / line_doc.py, mypy arg-type/assignment in
  snapshot/adapters.py, mypy assignment + arg-type in
  services/memory/settings.py, mypy func-returns-value on
  set.add idiom in deeptutor_cli/init_wizard.py.
- Apply ruff format / prettier sweep across the repo.
- Remove stale workspace + i18n env example + playwright captures
  already cleaned locally; ignore .playwright-cli/ going forward.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 21:09:22 +08:00

83 lines
2.7 KiB
Python

"""GET /api/v1/memory/resolve_entry/{id} → (layer, key).
L3 docs cite L2 entries by their ``m_<ULID>`` entry id. The resolver
turns an L3 footnote click into "which L2 surface do I navigate to".
"""
from __future__ import annotations
import importlib
from pathlib import Path
import pytest
pytest.importorskip("fastapi")
FastAPI = pytest.importorskip("fastapi").FastAPI
TestClient = pytest.importorskip("fastapi.testclient").TestClient
memory_router = importlib.import_module("deeptutor.api.routers.memory").router
paths_mod = importlib.import_module("deeptutor.services.memory.paths")
document_mod = importlib.import_module("deeptutor.services.memory.document")
@pytest.fixture
def client(tmp_path: Path, monkeypatch) -> TestClient:
monkeypatch.setattr(paths_mod, "memory_root", lambda: tmp_path)
(tmp_path / "L2").mkdir()
(tmp_path / "L3").mkdir()
app = FastAPI()
app.include_router(memory_router, prefix="/api/v1/memory")
return TestClient(app)
def _seed_l2(tmp_path: Path, surface: str, entry_id: str) -> None:
doc = document_mod.Document(
title=f"{surface} memory",
sections=[
(
"Themes",
[
document_mod.Entry(
id=entry_id,
section="Themes",
text="some fact",
refs=[f"{surface}:r1"],
),
],
),
],
)
(tmp_path / "L2" / f"{surface}.md").write_text(document_mod.serialize(doc), encoding="utf-8")
def test_resolve_entry_returns_owning_surface(client: TestClient, tmp_path: Path) -> None:
entry_id = "m_01HZK1ABCDEFGHJKMNPQRSTVWX"
_seed_l2(tmp_path, "notebook", entry_id)
res = client.get(f"/api/v1/memory/resolve_entry/{entry_id}")
assert res.status_code == 200
body = res.json()
assert body == {"layer": "L2", "key": "notebook", "entry_id": entry_id}
def test_resolve_entry_404_when_missing(client: TestClient) -> None:
res = client.get("/api/v1/memory/resolve_entry/m_01HZK1ABCDEFGHJKMNPQRSTVWX")
assert res.status_code == 404
def test_resolve_entry_400_on_bad_id(client: TestClient) -> None:
res = client.get("/api/v1/memory/resolve_entry/not-an-entry-id")
assert res.status_code == 400
def test_resolve_entry_first_hit_wins(client: TestClient, tmp_path: Path) -> None:
"""Iteration order is the SURFACES tuple — chat before notebook."""
entry_id = "m_01HZK1ABCDEFGHJKMNPQRSTVWX"
_seed_l2(tmp_path, "chat", entry_id)
_seed_l2(tmp_path, "notebook", entry_id) # duplicate (shouldn't happen IRL)
res = client.get(f"/api/v1/memory/resolve_entry/{entry_id}")
assert res.status_code == 200
assert res.json()["key"] == "chat"