File size: 2,691 Bytes
9304064
 
 
 
 
 
 
 
1db1a69
9304064
 
 
 
 
 
1db1a69
 
9304064
 
 
 
 
1db1a69
 
9304064
 
 
 
1db1a69
 
9304064
 
 
 
 
1db1a69
 
 
 
 
 
9304064
 
 
1db1a69
 
 
 
9304064
 
1db1a69
 
 
 
 
9304064
1db1a69
7ac569f
 
 
 
 
1db1a69
 
 
7ac569f
 
 
 
 
 
 
 
 
1db1a69
 
 
7ac569f
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
"""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)