mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 00:29:37 +08:00
* fix(codex): log a warning when token revocation fails during logout Logout previously swallowed revocation failures silently, leaving the remote token possibly valid with no operator trace. Log only the error kind and HTTP status (never credential material) and keep clearing the local credentials as before. * fix(llm): warn when CodeBuddy tool-call interrupt or drain fails A silent except-pass after receiving tool_calls let the SDK keep generating in the background when interrupt() failed, quietly draining quota. Log a warning (exception type + short reason) for interrupt and drain failures; return value semantics unchanged. * fix(memory): log corrupt snapshot files instead of skipping silently read_notebook_entities / read_cowriter_entities / read_book_entities swallowed (OSError, json.JSONDecodeError) with a bare continue, so a corrupt notebook file or manifest silently dropped content from the memory snapshot. Each skip now logs a warning with the file path and reason (no file content); snapshot results are otherwise unchanged. * fix(notebook): log unreadable files instead of skipping them silently during index rebuild * fix(question): log failed error-event delivery in mimic websocket The mimic generation error handler swallowed secondary failures of the error send_json with a bare 'except Exception: pass', so a delivery failure left the frontend without any reason and nothing in the logs. Split the handler: closed/disconnecting clients (RuntimeError, WebSocketDisconnect) stay debug-level, any other send failure is logged as a warning with traceback. Cleanup paths keep their best-effort narrow handling. Focused tests cover both branches. * test(codebuddy): make credential fixture expiry relative to now test_load_credentials_parses_session wrote a hard-coded expiresAt of 1791055241000 (2026-10-03 15:40:41 UTC) and asserted is_expired() is False. Once that instant passed the assertion flipped, so the test now fails on every Python version for every branch. Derive expiresAt / refreshExpiresAt from time.time() so the fixture always describes a live session, and assert against the value written. --------- Co-authored-by: XZH <cto@xzh.ai> Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
285 lines
9.9 KiB
Python
285 lines
9.9 KiB
Python
"""Notebook service regression tests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
from deeptutor.services.notebook.service import NotebookManager, RecordType
|
|
|
|
|
|
def test_add_record_accepts_enum_record_type(tmp_path) -> None:
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook = manager.create_notebook("CLI test notebook")
|
|
|
|
result = manager.add_record(
|
|
notebook_ids=[notebook["id"]],
|
|
record_type=RecordType.CHAT,
|
|
title="Sample",
|
|
user_query="Sample",
|
|
output="# Sample",
|
|
)
|
|
|
|
assert result["record"]["type"] == RecordType.CHAT
|
|
|
|
stored = manager.get_notebook(notebook["id"])
|
|
assert stored is not None
|
|
assert stored["records"][0]["type"] == "chat"
|
|
|
|
|
|
def test_add_record_strips_thinking_tags_from_summary(tmp_path) -> None:
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook = manager.create_notebook("Sanitized notebook")
|
|
|
|
result = manager.add_record(
|
|
notebook_ids=[notebook["id"]],
|
|
record_type="chat",
|
|
title="Sample",
|
|
summary="<think>private reasoning</think>\nReusable summary.",
|
|
user_query="Sample",
|
|
output="# Sample",
|
|
)
|
|
|
|
assert result["record"]["summary"] == "Reusable summary."
|
|
|
|
stored = manager.get_notebook(notebook["id"])
|
|
assert stored is not None
|
|
assert stored["records"][0]["summary"] == "Reusable summary."
|
|
|
|
|
|
def test_get_notebook_repairs_existing_thinking_tags_in_summary(tmp_path) -> None:
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook = manager.create_notebook("Legacy notebook")
|
|
manager.add_record(
|
|
notebook_ids=[notebook["id"]],
|
|
record_type="chat",
|
|
title="Sample",
|
|
summary="Reusable summary.",
|
|
user_query="Sample",
|
|
output="# Sample",
|
|
)
|
|
|
|
path = manager._get_notebook_file(notebook["id"])
|
|
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
raw["records"][0]["summary"] = "<think>old reasoning</think>\nReusable summary."
|
|
path.write_text(json.dumps(raw, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
|
|
repaired = manager.get_notebook(notebook["id"])
|
|
assert repaired is not None
|
|
assert repaired["records"][0]["summary"] == "Reusable summary."
|
|
assert "old reasoning" not in path.read_text(encoding="utf-8")
|
|
|
|
|
|
def test_concurrent_add_record_keeps_every_record(tmp_path) -> None:
|
|
"""Forty parallel saves must all land, and must not corrupt the file.
|
|
|
|
Before per-notebook locking + atomic writes, this lost records at two
|
|
writers and left unparseable JSON at forty — which surfaced to users as
|
|
a notebook that had silently vanished.
|
|
"""
|
|
import threading
|
|
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook_id = manager.create_notebook("Race")["id"]
|
|
|
|
def save(index: int) -> None:
|
|
manager.add_record(
|
|
notebook_ids=[notebook_id],
|
|
record_type=RecordType.CHAT,
|
|
title=f"record-{index}",
|
|
user_query="q",
|
|
output="body" * 500,
|
|
)
|
|
|
|
threads = [threading.Thread(target=save, args=(i,)) for i in range(40)]
|
|
for thread in threads:
|
|
thread.start()
|
|
for thread in threads:
|
|
thread.join()
|
|
|
|
stored = manager.get_notebook(notebook_id)
|
|
assert stored is not None
|
|
assert len(stored["records"]) == 40
|
|
assert {row["record_count"] for row in manager.list_notebooks()} == {40}
|
|
|
|
|
|
def test_update_record_leaves_unmentioned_kb_name_alone(tmp_path) -> None:
|
|
"""Renaming a record must not clear its knowledge-base link."""
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook_id = manager.create_notebook("KB")["id"]
|
|
record = manager.add_record(
|
|
notebook_ids=[notebook_id],
|
|
record_type=RecordType.CHAT,
|
|
title="Original",
|
|
user_query="q",
|
|
output="o",
|
|
kb_name="physics",
|
|
)["record"]
|
|
|
|
updated = manager.update_record(notebook_id, record["id"], title="Renamed")
|
|
|
|
assert updated is not None
|
|
assert updated["title"] == "Renamed"
|
|
assert updated["kb_name"] == "physics"
|
|
|
|
cleared = manager.update_record(notebook_id, record["id"], kb_name=None)
|
|
assert cleared is not None
|
|
assert cleared["kb_name"] is None
|
|
|
|
|
|
def test_damaged_notebook_is_reported_not_hidden(tmp_path) -> None:
|
|
"""A file that exists but does not parse must never look like a deletion."""
|
|
import pytest
|
|
|
|
from deeptutor.services.notebook.service import NotebookCorruptedError
|
|
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
(tmp_path / "broken01.json").write_text("{ not json", encoding="utf-8")
|
|
|
|
listed = {row["id"]: row for row in manager.list_notebooks()}
|
|
assert listed["broken01"]["unreadable"] is True
|
|
|
|
with pytest.raises(NotebookCorruptedError):
|
|
manager.get_notebook("broken01")
|
|
|
|
|
|
def test_index_rebuild_reports_unreadable_files_instead_of_silence(tmp_path, caplog) -> None:
|
|
"""Rebuilding the index must name each damaged file, never skip it silently.
|
|
|
|
A notebook file that exists but does not parse used to vanish from the
|
|
rebuilt index with no per-file log, so the damage was invisible until a
|
|
user noticed the entry missing.
|
|
"""
|
|
import logging
|
|
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
good_id = manager.create_notebook("Good")["id"]
|
|
(tmp_path / "broken01.json").write_text("{ not json", encoding="utf-8")
|
|
manager.index_file.write_text("not json at all", encoding="utf-8")
|
|
|
|
with caplog.at_level(logging.WARNING, logger="deeptutor.services.notebook.service"):
|
|
entries = manager._rebuild_index_entries()
|
|
|
|
assert good_id in {row["id"] for row in entries}
|
|
assert any("broken01.json" in record.getMessage() for record in caplog.records)
|
|
|
|
# The damaged file stays visible through the normal listing path too.
|
|
listed = {row["id"]: row for row in manager.list_notebooks()}
|
|
assert listed["broken01"]["unreadable"] is True
|
|
assert good_id in listed
|
|
|
|
|
|
def test_list_notebooks_adopts_files_missing_from_the_index(tmp_path) -> None:
|
|
"""A notebook whose index row was lost is still listed."""
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook_id = manager.create_notebook("Orphaned")["id"]
|
|
|
|
manager.index_file.write_text('{"notebooks": []}', encoding="utf-8")
|
|
|
|
listed = {row["id"] for row in manager.list_notebooks()}
|
|
assert notebook_id in listed
|
|
|
|
|
|
def test_copy_record_is_independent_of_its_source(tmp_path) -> None:
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
source = manager.create_notebook("Source")["id"]
|
|
target = manager.create_notebook("Target")["id"]
|
|
record = manager.add_record(
|
|
notebook_ids=[source],
|
|
record_type=RecordType.CHAT,
|
|
title="Shared",
|
|
user_query="q",
|
|
output="o",
|
|
)["record"]
|
|
|
|
copied = manager.copy_record(source, record["id"], target)
|
|
assert copied is not None
|
|
assert copied["id"] != record["id"]
|
|
|
|
manager.update_record(target, copied["id"], title="Edited in target")
|
|
|
|
assert manager.get_record(source, record["id"])["title"] == "Shared"
|
|
assert manager.get_record(target, copied["id"])["title"] == "Edited in target"
|
|
|
|
|
|
def test_move_record_leaves_the_source_notebook(tmp_path) -> None:
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
source = manager.create_notebook("Source")["id"]
|
|
target = manager.create_notebook("Target")["id"]
|
|
record = manager.add_record(
|
|
notebook_ids=[source],
|
|
record_type=RecordType.CHAT,
|
|
title="Travelling",
|
|
user_query="q",
|
|
output="o",
|
|
)["record"]
|
|
|
|
moved = manager.move_record(source, record["id"], target)
|
|
|
|
assert moved is not None
|
|
assert manager.get_record(source, record["id"]) is None
|
|
assert manager.get_records(target)[0]["title"] == "Travelling"
|
|
|
|
|
|
def test_statistics_counts_every_record_type(tmp_path) -> None:
|
|
"""The per-type breakdown must add up to the total, tutorbot included."""
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook_id = manager.create_notebook("Stats")["id"]
|
|
for record_type in (RecordType.CHAT, RecordType.TUTORBOT, RecordType.RESEARCH):
|
|
manager.add_record(
|
|
notebook_ids=[notebook_id],
|
|
record_type=record_type,
|
|
title=str(record_type),
|
|
user_query="q",
|
|
output="o",
|
|
)
|
|
|
|
stats = manager.get_statistics()
|
|
|
|
assert stats["records_by_type"]["tutorbot"] == 1
|
|
assert sum(stats["records_by_type"].values()) == stats["total_records"] == 3
|
|
|
|
|
|
def test_export_markdown_renders_titles_and_bodies(tmp_path) -> None:
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
notebook_id = manager.create_notebook("Exported", description="A short blurb")["id"]
|
|
manager.add_record(
|
|
notebook_ids=[notebook_id],
|
|
record_type=RecordType.CHAT,
|
|
title="First entry",
|
|
user_query="q",
|
|
output="Body text.",
|
|
summary="One-line summary.",
|
|
)
|
|
|
|
markdown = manager.export_markdown(notebook_id)
|
|
|
|
assert markdown is not None
|
|
assert "# Exported" in markdown
|
|
assert "> A short blurb" in markdown
|
|
assert "## First entry" in markdown
|
|
assert "Body text." in markdown
|
|
|
|
|
|
def test_notebook_id_cannot_escape_base_dir(tmp_path) -> None:
|
|
"""A traversal id must be rejected, never resolved outside ``base_dir``."""
|
|
import pytest
|
|
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
|
|
for evil in ("../../x", "/etc/passwd", "../../../../../system/auth/users"):
|
|
with pytest.raises(ValueError):
|
|
manager.get_notebook(evil)
|
|
|
|
|
|
def test_save_notebook_uses_managed_id_not_content_id(tmp_path) -> None:
|
|
"""A notebook's on-disk ``id`` must not redirect where it is written."""
|
|
manager = NotebookManager(base_dir=str(tmp_path))
|
|
|
|
payload = {"id": "../../../../../system/auth/users", "records": []}
|
|
manager._save_notebook("real-notebook-id", payload)
|
|
|
|
target = manager._get_notebook_file("real-notebook-id")
|
|
assert target.is_relative_to(tmp_path.resolve())
|
|
persisted = json.loads(target.read_text(encoding="utf-8"))
|
|
assert persisted["id"] == "../../../../../system/auth/users"
|