Files
DeepTutor/tests/tools/test_list_notebook.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

148 lines
5.0 KiB
Python

"""Unit tests for the ``list_notebook`` tool's pure rendering logic."""
from __future__ import annotations
from deeptutor.tools.list_notebook import (
MAX_NOTEBOOKS_RENDERED,
MAX_RECORDS_RENDERED,
list_notebooks_or_records,
)
class _FakeManager:
"""Minimal NotebookManager stand-in for unit tests."""
def __init__(self, notebooks=(), records_by_nb=None):
self._notebooks = list(notebooks)
self._records = dict(records_by_nb or {})
def list_notebooks(self):
return list(self._notebooks)
def get_records(self, notebook_id):
return list(self._records.get(notebook_id, []))
# ---------------------------------------------------------------------------
# Index mode (no notebook_id)
# ---------------------------------------------------------------------------
def test_index_returns_empty_message_when_user_has_no_notebooks() -> None:
outcome = list_notebooks_or_records(notebook_manager=_FakeManager())
assert outcome.ok is True
assert "no notebooks" in outcome.text.lower()
assert outcome.summary == {"mode": "index", "count": 0}
def test_index_renders_id_name_count_for_each_notebook() -> None:
manager = _FakeManager(
notebooks=[
{
"id": "nb-1",
"name": "Math",
"description": "Calculus + linear algebra",
"record_count": 12,
"updated_at": 1_700_000_000.0,
},
{
"id": "nb-2",
"name": "Physics",
"record_count": 3,
"updated_at": 1_700_000_010.0,
},
],
)
outcome = list_notebooks_or_records(notebook_manager=manager)
assert outcome.ok is True
assert "`nb-1`" in outcome.text
assert "**Math**" in outcome.text
assert "12 records" in outcome.text
assert "Calculus + linear algebra" in outcome.text
assert "`nb-2`" in outcome.text
assert "Physics" in outcome.text
assert outcome.summary == {"mode": "index", "count": 2}
def test_index_caps_huge_notebook_lists() -> None:
manager = _FakeManager(
notebooks=[
{"id": f"nb-{i}", "name": f"NB {i}", "record_count": i}
for i in range(MAX_NOTEBOOKS_RENDERED + 10)
],
)
outcome = list_notebooks_or_records(notebook_manager=manager)
assert outcome.ok is True
assert "showing" in outcome.text.lower()
assert outcome.summary["count"] == MAX_NOTEBOOKS_RENDERED + 10
# ---------------------------------------------------------------------------
# Drill-down mode (notebook_id given)
# ---------------------------------------------------------------------------
def test_drilldown_rejects_unknown_notebook_id() -> None:
manager = _FakeManager(
notebooks=[{"id": "nb-1", "name": "Math"}],
)
outcome = list_notebooks_or_records(notebook_id="bogus", notebook_manager=manager)
assert outcome.ok is False
assert "Unknown notebook_id" in outcome.error
assert "`nb-1`" in outcome.error # tells the LLM the right id to use
def test_drilldown_empty_records() -> None:
manager = _FakeManager(
notebooks=[{"id": "nb-1", "name": "Math"}],
records_by_nb={"nb-1": []},
)
outcome = list_notebooks_or_records(notebook_id="nb-1", notebook_manager=manager)
assert outcome.ok is True
assert "no records" in outcome.text.lower()
assert outcome.summary == {"mode": "records", "notebook_id": "nb-1", "count": 0}
def test_drilldown_renders_records_newest_first() -> None:
manager = _FakeManager(
notebooks=[{"id": "nb-1", "name": "Math"}],
records_by_nb={
"nb-1": [
{
"id": "r-old",
"title": "Old",
"summary": "old summary",
"type": "chat",
"created_at": 1_700_000_000.0,
},
{
"id": "r-new",
"title": "New",
"summary": "new summary",
"type": "chat",
"created_at": 1_700_001_000.0,
},
]
},
)
outcome = list_notebooks_or_records(notebook_id="nb-1", notebook_manager=manager)
assert outcome.ok is True
# Newest first — "r-new" appears before "r-old" in the rendered text.
assert outcome.text.index("`r-new`") < outcome.text.index("`r-old`")
assert "new summary" in outcome.text
def test_drilldown_caps_huge_record_lists() -> None:
big_records = [
{"id": f"r-{i}", "title": f"R{i}", "summary": "", "created_at": float(i)}
for i in range(MAX_RECORDS_RENDERED + 20)
]
manager = _FakeManager(
notebooks=[{"id": "nb-1", "name": "Math"}],
records_by_nb={"nb-1": big_records},
)
outcome = list_notebooks_or_records(notebook_id="nb-1", notebook_manager=manager)
assert outcome.ok is True
assert "showing" in outcome.text.lower()
assert outcome.summary["count"] == MAX_RECORDS_RENDERED + 20