完成大量美化

This commit is contained in:
2026-05-01 15:44:14 +08:00
parent 6b65b24b0f
commit f0e7e75ffb
36 changed files with 4393 additions and 1506 deletions

131
test_chat_service.py Normal file
View File

@@ -0,0 +1,131 @@
"""
测试聊天服务
"""
import sys
import importlib.util
from pathlib import Path
# 直接加载chat_service模块不通过__init__.py
spec = importlib.util.spec_from_file_location(
"chat_service",
Path(__file__).parent / "backend" / "services" / "chat_service.py"
)
chat_service_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(chat_service_module)
ChatService = chat_service_module.ChatService
import json
# 初始化聊天服务
data_path = Path("data")
chat_service = ChatService(data_path)
print("=" * 60)
print("测试1: 获取所有聊天列表")
print("=" * 60)
all_chats = chat_service.list_all_chats()
print(json.dumps(all_chats, ensure_ascii=False, indent=2))
print("\n" + "=" * 60)
print("测试2: 获取指定聊天的完整内容")
print("=" * 60)
try:
chat_data = chat_service.get_chat("testRole1", "111")
print(f"Metadata: {json.dumps(chat_data['metadata'], ensure_ascii=False, indent=2)}")
print(f"\nMessages count: {len(chat_data['messages'])}")
for msg in chat_data['messages']:
print(f" Floor {msg['floor']}: {msg['name']} - {msg['mes'][:50]}...")
except Exception as e:
print(f"Error: {e}")
print("\n" + "=" * 60)
print("测试3: 获取聊天的所有消息")
print("=" * 60)
try:
messages_data = chat_service.get_chat("testRole2", "222")
print(f"Messages count: {len(messages_data['messages'])}")
for msg in messages_data['messages']:
print(f" Floor {msg['floor']}: {msg['name']} ({'User' if msg['is_user'] else 'AI'})")
print(f" Content: {msg['mes'][:80]}...")
if msg.get('swipes') and len(msg['swipes']) > 0:
print(f" Swipes: {len(msg['swipes'])} versions")
except Exception as e:
print(f"Error: {e}")
print("\n" + "=" * 60)
print("测试4: 创建新聊天")
print("=" * 60)
try:
new_chat = chat_service.create_chat("testRole4", "test_chat", {
"user_name": "TestUser",
"character_name": "TestAI"
})
print(f"Created: {json.dumps(new_chat, ensure_ascii=False, indent=2)}")
# 添加测试消息
msg1 = chat_service.add_message("testRole4", "test_chat", {
"name": "TestUser",
"is_user": True,
"mes": "这是一条测试消息"
})
print(f"Added message 1: Floor {msg1['floor']}")
msg2 = chat_service.add_message("testRole4", "test_chat", {
"name": "TestAI",
"is_user": False,
"mes": "这是AI的回复"
})
print(f"Added message 2: Floor {msg2['floor']}")
# 读取验证
verify_chat = chat_service.get_chat("testRole4", "test_chat")
print(f"\nVerification - Messages count: {len(verify_chat['messages'])}")
for msg in verify_chat['messages']:
print(f" Floor {msg['floor']}: {msg['name']} - {msg['mes']}")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
print("\n" + "=" * 60)
print("测试5: 更新消息")
print("=" * 60)
try:
updated = chat_service.update_message("testRole4", "test_chat", 1, {
"mes": "这是更新后的测试消息"
})
print(f"Updated message at floor 1: {updated['mes']}")
# 验证更新
verify_chat = chat_service.get_chat("testRole4", "test_chat")
for msg in verify_chat['messages']:
if msg['floor'] == 1:
print(f"Verified - Floor 1: {msg['mes']}")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
print("\n" + "=" * 60)
print("测试6: 删除消息")
print("=" * 60)
try:
deleted = chat_service.delete_message("testRole4", "test_chat", 1)
print(f"Deleted message from floor 1: {deleted['mes']}")
# 验证删除并检查floor重新编号
verify_chat = chat_service.get_chat("testRole4", "test_chat")
print(f"\nAfter deletion - Messages count: {len(verify_chat['messages'])}")
for msg in verify_chat['messages']:
print(f" Floor {msg['floor']}: {msg['name']} - {msg['mes']}")
except Exception as e:
print(f"Error: {e}")
import traceback
traceback.print_exc()
print("\n" + "=" * 60)
print("所有测试完成!")
print("=" * 60)