"""Tests for agents/tools.py — CrewAI CadQuery tools. These tests require CadQuery to be installed. """ import json import pytest from agents.tools import ExecuteCadTool, ValidateCadTool, GenerateGcodeTool, set_last_shape, get_last_shape pytestmark = pytest.mark.requires_cadquery class TestExecuteCadTool: def test_valid_code(self): tool = ExecuteCadTool() result = json.loads(tool._run(code="result = cq.Workplane('XY').box(10,10,10)")) assert result["success"] is True assert result["volume_mm3"] > 0 assert result["face_count"] == 6 def test_invalid_code(self): tool = ExecuteCadTool() result = json.loads(tool._run(code="result = bad_code")) assert result["success"] is False assert result["error"] is not None def test_missing_result(self): tool = ExecuteCadTool() result = json.loads(tool._run(code="x = 42")) assert result["success"] is False class TestValidateCadTool: def test_valid_machinable(self): # First execute code to set the last shape exec_tool = ExecuteCadTool() exec_tool._run(code="result = cq.Workplane('XY').box(50, 30, 10)") # Now validate the last shape tool = ValidateCadTool() result = json.loads(tool._run()) assert "validation" in result assert result["validation"]["machinable"] is True def test_no_shape_available(self): set_last_shape(None) tool = ValidateCadTool() result = json.loads(tool._run()) assert result["success"] is False def test_returns_validation(self): exec_tool = ExecuteCadTool() exec_tool._run(code="result = cq.Workplane('XY').box(20, 20, 20)") tool = ValidateCadTool() result = json.loads(tool._run()) assert "validation" in result assert result["success"] is True class TestGenerateGcodeTool: def test_returns_error_when_no_shape(self): set_last_shape(None) tool = GenerateGcodeTool() result = json.loads(tool._run( operations=["pocket", "profile"], tool_diameter=6.0, post_processor="grbl", )) assert result["success"] is False assert "no shape" in result["error"].lower() def test_returns_error_when_ocp_not_installed(self): from unittest.mock import MagicMock set_last_shape(MagicMock()) tool = GenerateGcodeTool() result = json.loads(tool._run( operations=["pocket"], tool_diameter=6.0, post_processor="grbl", )) assert result["success"] is False set_last_shape(None)