#!/usr/bin/env python3
"""
简单集成测试 - 直接测试工具模块
"""
import unittest
import asyncio
import sys
import os
# 添加项目根目录到路径
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, project_root)
from src.fusion360_mcp import tools
class TestSimpleIntegration(unittest.TestCase):
"""简单集成测试 - 直接测试 tools 模块"""
def setUp(self):
"""测试初始化"""
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def tearDown(self):
"""测试清理"""
self.loop.close()
def async_test(self, coro):
"""运行异步测试"""
return self.loop.run_until_complete(coro)
async def check_fusion360_plugin(self) -> bool:
"""检查 Fusion 360 插件是否运行在端口 9000"""
try:
api = tools.get_api()
response = await api._request("GET", "/api/health")
return response.get("status") == "healthy"
except Exception as e:
print(f"❌ Fusion 360 插件未运行: {e}")
return False
def test_fusion360_plugin_connection(self):
"""测试 Fusion 360 插件连接"""
async def test():
print("🔍 检查 Fusion 360 插件连接...")
is_connected = await self.check_fusion360_plugin()
if is_connected:
print("✅ Fusion 360 插件连接正常 (端口 9000)")
else:
print("❌ Fusion 360 插件连接失败")
print("请确保:")
print("1. Fusion 360 已启动")
print("2. 插件已加载: addin/fusion360_mcp_addin/")
print("3. 插件在端口 9000 运行")
# 即使连接失败也不让测试失败,只是记录状态
return is_connected
result = self.async_test(test())
# 这里可以根据需要决定是否强制要求连接成功
# self.assertTrue(result, "Fusion 360 插件连接失败")
def test_create_document_tool(self):
"""测试创建文档工具"""
async def test():
# 先检查连接
if not await self.check_fusion360_plugin():
print("⏭️ 跳过文档创建测试,Fusion 360 插件未连接")
return
print("📄 测试创建文档...")
try:
result = await tools.create_document(
name="单元测试文档",
units="mm"
)
if result:
print(f"✅ 创建文档成功: {result}")
return True
else:
print("❌ 创建文档失败: 无返回结果")
return False
except Exception as e:
print(f"❌ 创建文档异常: {e}")
return False
self.async_test(test())
def test_create_object_tool(self):
"""测试创建对象工具"""
async def test():
# 先检查连接
if not await self.check_fusion360_plugin():
print("⏭️ 跳过对象创建测试,Fusion 360 插件未连接")
return
print("🔵 测试创建圆柱体...")
try:
result = await tools.create_object(
object_type="extrude",
parameters={
"base_feature": "circle",
"radius": 25.0,
"height": 50.0
}
)
if result:
print(f"✅ 创建圆柱体成功: {result}")
return True
else:
print("❌ 创建圆柱体失败: 无返回结果")
return False
except Exception as e:
print(f"❌ 创建圆柱体异常: {e}")
return False
self.async_test(test())
def test_get_objects_tool(self):
"""测试获取对象列表工具"""
async def test():
# 先检查连接
if not await self.check_fusion360_plugin():
print("⏭️ 跳过获取对象测试,Fusion 360 插件未连接")
return
print("📋 测试获取对象列表...")
try:
result = await tools.get_objects()
if result:
objects = result.get("objects", []) if isinstance(result, dict) else []
print(f"✅ 获取对象列表成功,共 {len(objects)} 个对象")
for obj in objects[:3]: # 只显示前3个
print(f" - {obj}")
return True
else:
print("❌ 获取对象列表失败: 无返回结果")
return False
except Exception as e:
print(f"❌ 获取对象列表异常: {e}")
return False
self.async_test(test())
def test_get_parts_list_tool(self):
"""测试获取零件列表工具"""
async def test():
# 先检查连接
if not await self.check_fusion360_plugin():
print("⏭️ 跳过获取零件列表测试,Fusion 360 插件未连接")
return
print("🔧 测试获取零件列表...")
try:
result = await tools.get_parts_list()
if result:
parts = result.get("parts", []) if isinstance(result, dict) else []
print(f"✅ 获取零件列表成功,共 {len(parts)} 个零件")
for part in parts[:3]: # 只显示前3个
print(f" - {part}")
return True
else:
print("❌ 获取零件列表失败: 无返回结果")
return False
except Exception as e:
print(f"❌ 获取零件列表异常: {e}")
return False
self.async_test(test())
if __name__ == '__main__':
print("🧪 Fusion360 MCP 简单集成测试")
print("=" * 50)
print("测试目标: 直接测试 src/tools.py 模块")
print("要求: Fusion 360 插件运行在端口 9000")
print("=" * 50)
unittest.main(verbosity=2)