190 lines
5.8 KiB
Python
190 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
dev-workflow 单元测试
|
|
|
|
测试状态管理和编排器的基本功能。
|
|
"""
|
|
|
|
import json
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
# 添加模块路径
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
from state_manager import (
|
|
init_state,
|
|
load_state,
|
|
save_state,
|
|
set_phase,
|
|
complete_phase,
|
|
update_phase_data,
|
|
increment_retry,
|
|
reset_retry
|
|
)
|
|
|
|
|
|
class TestStateManager(unittest.TestCase):
|
|
"""测试状态管理器"""
|
|
|
|
def setUp(self):
|
|
"""设置测试环境"""
|
|
self.temp_dir = tempfile.mkdtemp()
|
|
self.state_file = Path(self.temp_dir) / ".planning" / "dev-workflow" / "state.json"
|
|
self.state_file.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
# 切换到临时目录
|
|
self.original_cwd = os.getcwd()
|
|
os.chdir(self.temp_dir)
|
|
|
|
def tearDown(self):
|
|
"""清理测试环境"""
|
|
os.chdir(self.original_cwd)
|
|
import shutil
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
def test_init_state(self):
|
|
"""测试初始化状态"""
|
|
state = init_state(
|
|
project_name="测试项目",
|
|
trigger_message="开始开发测试功能"
|
|
)
|
|
|
|
self.assertEqual(state["project_name"], "测试项目")
|
|
self.assertEqual(state["current_phase"], 1)
|
|
self.assertEqual(state["current_stage"], "头脑风暴")
|
|
self.assertEqual(state["phases"]["1"]["status"], "in_progress")
|
|
self.assertIn("trigger_message", state["context"])
|
|
|
|
def test_set_phase(self):
|
|
"""测试设置阶段"""
|
|
init_state("测试项目", "测试")
|
|
|
|
set_phase(3)
|
|
state = load_state()
|
|
|
|
self.assertEqual(state["current_phase"], 3)
|
|
self.assertEqual(state["current_stage"], "方案审核")
|
|
self.assertEqual(state["phases"]["3"]["status"], "in_progress")
|
|
self.assertEqual(state["phases"]["1"]["status"], "completed")
|
|
|
|
def test_complete_phase(self):
|
|
"""测试完成阶段"""
|
|
init_state("测试项目", "测试")
|
|
|
|
complete_phase(1)
|
|
state = load_state()
|
|
|
|
self.assertEqual(state["phases"]["1"]["status"], "completed")
|
|
self.assertIn("completed_at", state["phases"]["1"])
|
|
self.assertEqual(state["current_phase"], 2)
|
|
|
|
def test_update_phase_data(self):
|
|
"""测试更新阶段数据"""
|
|
init_state("测试项目", "测试")
|
|
|
|
update_phase_data(1, {"questions_asked": 5, "collected": True})
|
|
state = load_state()
|
|
|
|
self.assertEqual(state["phases"]["1"]["data"]["questions_asked"], 5)
|
|
self.assertEqual(state["phases"]["1"]["data"]["collected"], True)
|
|
|
|
def test_retry_logic(self):
|
|
"""测试重试逻辑"""
|
|
init_state("测试项目", "测试")
|
|
|
|
# 第一次重试
|
|
can_retry = increment_retry()
|
|
self.assertTrue(can_retry)
|
|
state = load_state()
|
|
self.assertEqual(state["retry_count"], 1)
|
|
|
|
# 重置
|
|
reset_retry()
|
|
state = load_state()
|
|
self.assertEqual(state["retry_count"], 0)
|
|
|
|
def test_max_retries(self):
|
|
"""测试最大重试次数"""
|
|
init_state("测试项目", "测试")
|
|
|
|
# 连续重试
|
|
for i in range(3):
|
|
can_retry = increment_retry()
|
|
if i < 2:
|
|
self.assertTrue(can_retry)
|
|
else:
|
|
self.assertFalse(can_retry)
|
|
|
|
|
|
class TestOrchestrator(unittest.TestCase):
|
|
"""测试编排器"""
|
|
|
|
def setUp(self):
|
|
"""设置测试环境"""
|
|
self.temp_dir = tempfile.mkdtemp()
|
|
self.original_cwd = os.getcwd()
|
|
os.chdir(self.temp_dir)
|
|
|
|
def tearDown(self):
|
|
"""清理测试环境"""
|
|
os.chdir(self.original_cwd)
|
|
import shutil
|
|
shutil.rmtree(self.temp_dir)
|
|
|
|
def test_orchestrator_init(self):
|
|
"""测试编排器初始化"""
|
|
from orchestrator import DevWorkflowOrchestrator
|
|
|
|
orchestrator = DevWorkflowOrchestrator()
|
|
orchestrator.initialize("开始开发测试功能", "测试项目")
|
|
|
|
# 检查目录结构
|
|
planning_dir = Path(".planning/dev-workflow")
|
|
self.assertTrue(planning_dir.exists())
|
|
self.assertTrue((planning_dir / "solutions").exists())
|
|
self.assertTrue((planning_dir / "reviews").exists())
|
|
self.assertTrue((planning_dir / "development").exists())
|
|
self.assertTrue((planning_dir / "code-review").exists())
|
|
|
|
def test_phase_transitions(self):
|
|
"""测试阶段转换"""
|
|
from orchestrator import DevWorkflowOrchestrator
|
|
|
|
orchestrator = DevWorkflowOrchestrator()
|
|
orchestrator.initialize("开始开发测试功能", "测试项目")
|
|
|
|
# 测试各阶段方法存在
|
|
self.assertTrue(hasattr(orchestrator, 'run_phase_1'))
|
|
self.assertTrue(hasattr(orchestrator, 'run_phase_2'))
|
|
self.assertTrue(hasattr(orchestrator, 'run_phase_9'))
|
|
|
|
|
|
class TestSKILLDocumentation(unittest.TestCase):
|
|
"""测试SKILL.md文档"""
|
|
|
|
def test_skill_md_exists(self):
|
|
"""测试SKILL.md存在"""
|
|
skill_md = Path(__file__).parent / "SKILL.md"
|
|
self.assertTrue(skill_md.exists(), "SKILL.md should exist")
|
|
|
|
def test_skill_md_content(self):
|
|
"""测试SKILL.md内容"""
|
|
skill_md = Path(__file__).parent / "SKILL.md"
|
|
content = skill_md.read_text()
|
|
|
|
# 检查必要内容
|
|
self.assertIn("name: dev-workflow", content)
|
|
self.assertIn("开始开发", content)
|
|
self.assertIn("9阶段", content)
|
|
self.assertIn("头脑风暴", content)
|
|
self.assertIn("方案设计", content)
|
|
self.assertIn("代码审查", content)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main(verbosity=2)
|