Files
DeepTutor/tests/api/test_sessions_recycle.py
evan188199-tech 9b066b5f98 feat(sessions): add recycle bin for deleted chats (#1398)
* feat(sessions): add recycle bin for deleted chats

Implements soft-delete for chat sessions so users can restore
accidentally deleted conversations from a recycle bin.

Backend:
- Schema migration adds deleted_at REAL column to sessions
- delete_session becomes a soft-delete (sets deleted_at timestamp)
- New store methods: restore_session, purge_session, list_deleted_sessions
- Session list queries exclude soft-deleted sessions
- API endpoints: GET /sessions/recycle-bin, POST /{id}/restore,
  DELETE /{id}/purge (purge triggers attachment/mastery cleanup)

Frontend:
- RecycleBinSection: collapsible sidebar section listing deleted
  sessions with Restore and Delete permanently actions
- Wired into SidebarShell via recycleBinSlot prop
- API client functions for list, restore, and purge
- en/zh translations for all recycle-bin UI strings

Tests: 6 backend tests covering the soft-delete lifecycle

* style: apply ruff format to sqlite_store

* fix(sessions): preserve recycled chat artifacts

* feat(sessions): fold #1444's backend into this recycle bin

#1444 built the same feature from the other end. This PR had the SQLite
column, the routes and the sidebar UI; #1444 had the protocol declarations,
the PocketBase implementation and the CLI wording. Neither shipped alone —
this one left multi-user deployments answering AttributeError on
/recycle-bin, and #1444 built a bin nobody could see. Closes #1444; its
backend is here.

Kept from here: one `deleted_at` column. #1444 carried `is_deleted` beside it
in both backends, and `deleted_at IS NOT NULL` already says everything the
boolean did — PocketBase now derives the outward `is_deleted` field from the
timestamp instead of storing it twice.

Taken from #1444: the naming, which turns out to fix the residual both PRs
shipped with. `delete_session` keeps meaning what every existing caller
already meant by it — remove the row — so `reading.py`'s two internal
cleanups need no patch: a deleted reading workspace still takes its sessions
with it instead of depositing them in a learner's bin, where restoring one
would resurrect a session pointing at a workspace that no longer exists. The
chat surface's delete is a separate, explicitly named `soft_delete_session`,
and `hard_delete_session` empties the bin. Both backends agree, and the
protocol declares all five so a backend cannot silently lack one again.

`test_entries_cascade_on_session_purge` becomes two tests, because there are
now two deletions worth pinning: one the bin can undo, and one that skips it
and cascades immediately.

Co-Authored-By: sjagadee <srini.jagadeesh@gmail.com>
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: XZH <cto@xzh.ai>
Co-authored-by: Bingxi Zhao (Frank) <13051345080@163.com>
Co-authored-by: sjagadee <srini.jagadeesh@gmail.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-09-14 01:43:35 +08:00

102 lines
3.0 KiB
Python

from __future__ import annotations
from typing import Any
import pytest
class _RecycleBinStore:
"""Minimal in-memory stand-in covering the recycle-bin lifecycle."""
def __init__(self) -> None:
self.sessions: dict[str, dict[str, Any]] = {}
def seed(self, session_id: str) -> None:
self.sessions[session_id] = {
"session_id": session_id,
"title": f"Session {session_id}",
"deleted_at": None,
}
async def delete_session(self, session_id: str) -> bool:
row = self.sessions.get(session_id)
if row is None or row.get("deleted_at") is not None:
return False
row["deleted_at"] = 1.0
return True
async def restore_session(self, session_id: str) -> bool:
row = self.sessions.get(session_id)
if row is None or row.get("deleted_at") is None:
return False
row["deleted_at"] = None
return True
async def purge_session(self, session_id: str) -> bool:
row = self.sessions.get(session_id)
if row is None or row.get("deleted_at") is None:
return False
del self.sessions[session_id]
return True
async def list_deleted_sessions(self, *, limit: int, offset: int):
deleted = [s for s in self.sessions.values() if s.get("deleted_at") is not None]
return deleted[offset : offset + limit]
async def get_session(self, session_id: str):
return self.sessions.get(session_id)
def _make_store() -> _RecycleBinStore:
store = _RecycleBinStore()
store.seed("s-active")
store.seed("s-doomed")
return store
@pytest.mark.asyncio
async def test_delete_marks_recycled():
store = _make_store()
assert await store.delete_session("s-doomed")
assert store.sessions["s-doomed"]["deleted_at"] is not None
assert store.sessions["s-active"]["deleted_at"] is None
@pytest.mark.asyncio
async def test_restore_clears_deleted_at():
store = _make_store()
await store.delete_session("s-doomed")
assert await store.restore_session("s-doomed")
assert store.sessions["s-doomed"]["deleted_at"] is None
@pytest.mark.asyncio
async def test_restore_fails_when_not_deleted():
store = _make_store()
assert not await store.restore_session("s-active")
@pytest.mark.asyncio
async def test_purge_removes_only_deleted():
store = _make_store()
await store.delete_session("s-doomed")
assert await store.purge_session("s-doomed")
assert "s-doomed" not in store.sessions
assert not await store.purge_session("s-active")
@pytest.mark.asyncio
async def test_list_deleted_excludes_active():
store = _make_store()
await store.delete_session("s-doomed")
deleted = await store.list_deleted_sessions(limit=50, offset=0)
assert len(deleted) == 1
assert deleted[0]["session_id"] == "s-doomed"
@pytest.mark.asyncio
async def test_delete_fails_when_already_deleted():
store = _make_store()
await store.delete_session("s-doomed")
assert not await store.delete_session("s-doomed")