2026-02-17 02:31:39 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
"""
|
2026-02-26 13:43:44 +08:00
|
|
|
LangChain + LangGraph 脚手架基础测试
|
2026-02-17 02:31:39 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
import sys
|
|
|
|
|
import os
|
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
config_path = os.path.join(os.path.dirname(__file__), '..', 'config', 'config.ini')
|
|
|
|
|
if not os.path.exists(config_path):
|
|
|
|
|
with open(config_path, 'w') as f:
|
|
|
|
|
f.write("""
|
|
|
|
|
[General]
|
|
|
|
|
DEFAULT_MODEL_SECTION = gpt-4o
|
|
|
|
|
MAX_RETRIES = 1
|
|
|
|
|
TIMEOUT = 10
|
|
|
|
|
|
|
|
|
|
[gpt-4o]
|
|
|
|
|
MODEL_NAME = gpt-4o
|
|
|
|
|
OPENAI_API_KEY = your_openai_api_key_here
|
|
|
|
|
|
|
|
|
|
[gpt-3.5-turbo]
|
|
|
|
|
MODEL_NAME = gpt-3.5-turbo
|
|
|
|
|
OPENAI_API_KEY = your_openai_api_key_here
|
|
|
|
|
""")
|
|
|
|
|
|
2026-02-17 02:31:39 +08:00
|
|
|
from workflows.workflow_manager import WorkflowManager, WorkflowType
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestWorkflowManager(unittest.TestCase):
|
2026-02-26 13:43:44 +08:00
|
|
|
"""测试 WorkflowManager 功能"""
|
2026-02-17 02:31:39 +08:00
|
|
|
|
|
|
|
|
def setUp(self):
|
2026-02-26 13:43:44 +08:00
|
|
|
"""设置测试夹具"""
|
2026-02-17 02:31:39 +08:00
|
|
|
self.manager = WorkflowManager()
|
|
|
|
|
|
|
|
|
|
def test_get_available_workflows(self):
|
2026-02-26 13:43:44 +08:00
|
|
|
"""测试可用工作流返回"""
|
2026-02-17 02:31:39 +08:00
|
|
|
workflows = self.manager.get_available_workflows()
|
|
|
|
|
self.assertIsInstance(workflows, list)
|
|
|
|
|
self.assertGreater(len(workflows), 0)
|
|
|
|
|
self.assertIn("conversation", workflows)
|
|
|
|
|
self.assertIn("tool_using", workflows)
|
|
|
|
|
|
|
|
|
|
def test_get_workflow(self):
|
2026-02-26 13:43:44 +08:00
|
|
|
"""测试获取工作流实例"""
|
2026-02-17 02:31:39 +08:00
|
|
|
conversation_workflow = self.manager.get_workflow(WorkflowType.CONVERSATION)
|
|
|
|
|
self.assertIsNotNone(conversation_workflow)
|
|
|
|
|
|
|
|
|
|
tool_workflow = self.manager.get_workflow(WorkflowType.TOOL_USING)
|
|
|
|
|
self.assertIsNotNone(tool_workflow)
|
|
|
|
|
|
|
|
|
|
def test_session_management(self):
|
2026-02-26 13:43:44 +08:00
|
|
|
"""测试会话创建与获取"""
|
|
|
|
|
# 执行工作流以创建会话
|
2026-02-17 02:31:39 +08:00
|
|
|
result = self.manager.execute_workflow(
|
|
|
|
|
WorkflowType.CONVERSATION,
|
|
|
|
|
"Hello, test session"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
session_id = result["session_id"]
|
|
|
|
|
self.assertIsNotNone(session_id)
|
|
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
# 测试会话信息获取
|
2026-02-17 02:31:39 +08:00
|
|
|
session_info = self.manager.get_session_info(session_id)
|
|
|
|
|
self.assertIsNotNone(session_info)
|
|
|
|
|
self.assertEqual(session_info["workflow_type"], WorkflowType.CONVERSATION)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestConfiguration(unittest.TestCase):
|
2026-02-26 13:43:44 +08:00
|
|
|
"""测试配置校验"""
|
2026-02-17 02:31:39 +08:00
|
|
|
|
2026-02-26 13:43:44 +08:00
|
|
|
def test_config_loading(self):
|
|
|
|
|
"""测试能从 config.ini 加载配置"""
|
|
|
|
|
from config import Config
|
|
|
|
|
model_config = Config.get_model_config()
|
|
|
|
|
self.assertIn('model', model_config)
|
|
|
|
|
self.assertIn('api_key', model_config)
|
2026-02-17 02:31:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|