File size: 4,557 Bytes
ef30000
 
 
 
 
 
 
38cfebe
ef30000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38cfebe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Tests for core/executor.py — CadQuery code execution and export.

These tests require CadQuery to be installed.
"""

import pytest
from pathlib import Path
from core.executor import sanitize_code, execute_cadquery, export_step, export_stl, export_3mf, export_all

pytestmark = pytest.mark.requires_cadquery


class TestSanitizeCode:
    def test_strips_markdown_fences(self):
        code = "```python\nresult = 1\n```"
        assert "```" not in sanitize_code(code)

    def test_strips_plain_fences(self):
        code = "```\nresult = 1\n```"
        assert "```" not in sanitize_code(code)

    def test_removes_cadquery_imports(self):
        code = "import cadquery as cq\nresult = cq.Workplane('XY').box(10,10,10)"
        cleaned = sanitize_code(code)
        assert "import cadquery" not in cleaned
        assert "result" in cleaned

    def test_removes_math_import(self):
        code = "import math\nresult = cq.Workplane('XY').box(10,10,10)"
        cleaned = sanitize_code(code)
        assert "import math" not in cleaned

    def test_preserves_valid_code(self):
        code = "result = cq.Workplane('XY').box(10, 20, 30)"
        assert sanitize_code(code) == code


class TestExecuteCadquery:
    def test_simple_box(self):
        result = execute_cadquery("result = cq.Workplane('XY').box(10, 20, 30)")
        assert result.success is True
        assert result.volume > 0
        assert result.face_count == 6
        assert result.edge_count == 12
        assert len(result.bounding_box) == 3

    def test_cylinder(self):
        result = execute_cadquery("result = cq.Workplane('XY').cylinder(20, 10)")
        assert result.success is True
        assert result.volume > 0

    def test_missing_result_variable(self):
        result = execute_cadquery("x = cq.Workplane('XY').box(10,10,10)")
        assert result.success is False
        assert "result" in result.error

    def test_syntax_error(self):
        result = execute_cadquery("result = cq.Workplane('XY').box(10, 10,")
        assert result.success is False
        assert result.error is not None

    def test_wrong_type(self):
        result = execute_cadquery("result = 42")
        assert result.success is False
        assert "Workplane" in result.error

    def test_code_with_markdown_fences(self):
        code = "```python\nimport cadquery as cq\nresult = cq.Workplane('XY').box(5,5,5)\n```"
        result = execute_cadquery(code)
        assert result.success is True

    def test_summary_on_success(self):
        result = execute_cadquery("result = cq.Workplane('XY').box(10, 20, 30)")
        summary = result.summary()
        assert "OK" in summary
        assert "Volume" in summary

    def test_summary_on_failure(self):
        result = execute_cadquery("result = bad_code")
        summary = result.summary()
        assert "FAILED" in summary


class TestExport:
    def test_export_step(self, tmp_path):
        exec_result = execute_cadquery("result = cq.Workplane('XY').box(10,10,10)")
        assert exec_result.success
        path = export_step(exec_result.result, tmp_path / "test.step")
        assert path.exists()
        assert path.suffix == ".step"

    def test_export_stl(self, tmp_path):
        exec_result = execute_cadquery("result = cq.Workplane('XY').box(10,10,10)")
        assert exec_result.success
        path = export_stl(exec_result.result, tmp_path / "test.stl")
        assert path.exists()
        assert path.suffix == ".stl"

    def test_export_all(self, tmp_path):
        exec_result = execute_cadquery("result = cq.Workplane('XY').box(10,10,10)")
        assert exec_result.success
        files = export_all(exec_result.result, tmp_path / "part")
        assert files["step"].exists()
        assert files["stl"].exists()


class TestExport3MF:
    def test_export_3mf(self, tmp_path):
        import cadquery as cq
        from core.executor import export_3mf
        shape = cq.Workplane("XY").box(10, 10, 10)
        path = tmp_path / "test.3mf"
        result = export_3mf(shape, path)
        assert result.exists()
        assert result.suffix == ".3mf"
        assert result.stat().st_size > 0

    def test_export_all_includes_3mf(self, tmp_path):
        import cadquery as cq
        from core.executor import export_all
        shape = cq.Workplane("XY").box(10, 10, 10)
        base = tmp_path / "part"
        files = export_all(shape, base)
        assert "3mf" in files
        assert files["3mf"].exists()
        assert files["step"].exists()
        assert files["stl"].exists()