feat: 添加任务历史记录、持久化会话、WebSocket支持和数据持久化功能

This commit is contained in:
2026-03-10 18:58:03 +08:00
parent f56ba5559d
commit 7fdd31b07b
22 changed files with 2006 additions and 4 deletions

View File

@@ -0,0 +1,67 @@
import pytest
from unittest.mock import patch
from datetime import datetime
from app.services.persistent_session import PersistentSessionManager
class TestPersistentSessionManager:
@pytest.fixture(autouse=True)
def setup(self, mock_settings, mock_opencode_client):
self.manager = PersistentSessionManager()
self.manager._sessions = {}
self.mock_client = mock_opencode_client
@pytest.mark.asyncio
async def test_create_session(self):
with patch("app.services.persistent_session.opencode_client", self.mock_client):
session = await self.manager.create_session("Test Session")
assert session.id is not None
assert session.name == "Test Session"
assert session.created_at is not None
@pytest.mark.asyncio
async def test_delete_session(self):
with patch("app.services.persistent_session.opencode_client", self.mock_client):
session = await self.manager.create_session("Temp Session")
session_id = session.id
result = await self.manager.delete_session(session_id)
assert result is True
assert session_id not in self.manager._sessions
@pytest.mark.asyncio
async def test_send_message(self):
with patch("app.services.persistent_session.opencode_client", self.mock_client):
session = await self.manager.create_session("Chat Session")
result = await self.manager.send_message(session.id, "Hello")
assert result is not None
@pytest.mark.asyncio
async def test_send_message_to_nonexistent(self):
with patch("app.services.persistent_session.opencode_client", self.mock_client):
with pytest.raises(ValueError):
await self.manager.send_message("nonexistent", "Hello")
@pytest.mark.asyncio
async def test_get_session(self):
with patch("app.services.persistent_session.opencode_client", self.mock_client):
session = await self.manager.create_session("Get Test")
retrieved = self.manager.get_session(session.id)
assert retrieved is not None
assert retrieved.id == session.id
@pytest.mark.asyncio
async def test_list_sessions(self):
with patch("app.services.persistent_session.opencode_client", self.mock_client):
await self.manager.create_session("Session 1")
await self.manager.create_session("Session 2")
sessions = self.manager.list_sessions()
assert len(sessions) == 2