"""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