File size: 2,158 Bytes
8dcf472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Tests for the local CodeInterpreterTool."""
from __future__ import annotations

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))


class TestCodeInterpreterTool:
    def test_basic_print(self):
        from src.tools.code_interpreter import CodeInterpreterTool
        tool = CodeInterpreterTool()
        result = tool._run("print('hello world')")
        assert "hello world" in result

    def test_pandas_calculation(self):
        from src.tools.code_interpreter import CodeInterpreterTool
        tool = CodeInterpreterTool()
        code = """
revenues = [100_000, 250_000, 600_000, 1_500_000, 3_750_000]
cagr = (revenues[-1] / revenues[0]) ** (1 / (len(revenues) - 1)) - 1
print(f"CAGR: {cagr:.1%}")
"""
        result = tool._run(code)
        assert "CAGR:" in result
        assert "%" in result

    def test_numpy_calculation(self):
        from src.tools.code_interpreter import CodeInterpreterTool
        tool = CodeInterpreterTool()
        code = """
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(f"Mean: {np.mean(arr)}, Std: {np.std(arr):.2f}")
"""
        result = tool._run(code)
        assert "Mean: 30.0" in result

    def test_blocks_os_import(self):
        from src.tools.code_interpreter import CodeInterpreterTool
        tool = CodeInterpreterTool()
        result = tool._run("import os; os.system('ls')")
        assert "Error" in result or "blocked" in result.lower()

    def test_blocks_open_call(self):
        from src.tools.code_interpreter import CodeInterpreterTool
        tool = CodeInterpreterTool()
        result = tool._run("open('/etc/passwd').read()")
        assert "Error" in result

    def test_empty_code_returns_error(self):
        from src.tools.code_interpreter import CodeInterpreterTool
        tool = CodeInterpreterTool()
        result = tool._run("")
        assert "Error" in result

    def test_syntax_error_handled(self):
        from src.tools.code_interpreter import CodeInterpreterTool
        tool = CodeInterpreterTool()
        result = tool._run("def broken(:")
        assert "Error" in result or "SyntaxError" in result