|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import sys |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from interpreter.cli import _parse_list_arg, load_interpreter, parse_args |
| 9 | +from interpreter.profiles import Profile |
| 10 | + |
| 11 | + |
| 12 | +def test_version_flag(): |
| 13 | + # Test --version flag |
| 14 | + result = subprocess.run( |
| 15 | + ["interpreter", "--version"], capture_output=True, text=True |
| 16 | + ) |
| 17 | + assert result.returncode == 0 |
| 18 | + assert "Open Interpreter" in result.stdout |
| 19 | + |
| 20 | + |
| 21 | +def test_help_flag(): |
| 22 | + # Test --help flag |
| 23 | + result = subprocess.run(["interpreter", "--help"], capture_output=True, text=True) |
| 24 | + assert result.returncode == 0 |
| 25 | + assert "usage:" in result.stdout.lower() or "help" in result.stdout.lower() |
| 26 | + |
| 27 | + |
| 28 | +def test_parse_list_arg(): |
| 29 | + # Test parsing JSON list |
| 30 | + assert _parse_list_arg('["a", "b", "c"]') == ["a", "b", "c"] |
| 31 | + |
| 32 | + # Test parsing comma-separated list |
| 33 | + assert _parse_list_arg("a,b,c") == ["a", "b", "c"] |
| 34 | + |
| 35 | + # Test empty input |
| 36 | + assert _parse_list_arg("") == [] |
| 37 | + |
| 38 | + # Test whitespace handling |
| 39 | + assert _parse_list_arg("a, b, c ") == ["a", "b", "c"] |
| 40 | + |
| 41 | + |
| 42 | +def test_model_flag(): |
| 43 | + # Test --model flag |
| 44 | + with patch.object(sys, "argv", ["interpreter", "--model", "gpt-4"]): |
| 45 | + args = parse_args() |
| 46 | + assert args["model"] == "gpt-4" |
| 47 | + |
| 48 | + |
| 49 | +def test_api_key_flag(): |
| 50 | + # Test --api-key flag |
| 51 | + test_key = "test-key-123" |
| 52 | + with patch.object(sys, "argv", ["interpreter", "--api-key", test_key]): |
| 53 | + args = parse_args() |
| 54 | + assert args["api_key"] == test_key |
| 55 | + |
| 56 | + |
| 57 | +def test_temperature_flag(): |
| 58 | + # Test --temperature flag |
| 59 | + with patch.object(sys, "argv", ["interpreter", "--temperature", "0.7"]): |
| 60 | + args = parse_args() |
| 61 | + assert args["temperature"] == "0.7" |
| 62 | + |
| 63 | + |
| 64 | +def test_auto_run_flag(): |
| 65 | + # Test --auto-run flag |
| 66 | + with patch.object(sys, "argv", ["interpreter", "--auto-run"]): |
| 67 | + args = parse_args() |
| 68 | + assert args["auto_run"] is True |
| 69 | + |
| 70 | + |
| 71 | +def test_debug_flag(): |
| 72 | + # Test --debug flag |
| 73 | + with patch.object(sys, "argv", ["interpreter", "--debug"]): |
| 74 | + args = parse_args() |
| 75 | + assert args["debug"] is True |
| 76 | + |
| 77 | + |
| 78 | +def test_tool_calling_flags(): |
| 79 | + # Test --no-tool-calling flag |
| 80 | + with patch.object(sys, "argv", ["interpreter", "--no-tool-calling"]): |
| 81 | + args = parse_args() |
| 82 | + assert args["tool_calling"] is False |
| 83 | + |
| 84 | + |
| 85 | +def test_interactive_flags(): |
| 86 | + # Test --interactive flag |
| 87 | + with patch.object(sys, "argv", ["interpreter", "--interactive"]): |
| 88 | + args = parse_args() |
| 89 | + assert args["interactive"] is True |
| 90 | + |
| 91 | + # Test --no-interactive flag |
| 92 | + with patch.object(sys, "argv", ["interpreter", "--no-interactive"]): |
| 93 | + args = parse_args() |
| 94 | + assert args["interactive"] is False |
| 95 | + |
| 96 | + |
| 97 | +def test_direct_input(): |
| 98 | + # Test direct input without flags |
| 99 | + test_input = "Hello interpreter" |
| 100 | + with patch.object(sys, "argv", ["interpreter", test_input]): |
| 101 | + args = parse_args() |
| 102 | + assert args["input"] == f"i {test_input}" |
| 103 | + |
| 104 | + |
| 105 | +@pytest.mark.asyncio |
| 106 | +async def test_load_interpreter(): |
| 107 | + # Test interpreter loading with custom settings |
| 108 | + args = {"model": "gpt-4", "temperature": 0.7, "auto_run": True, "debug": True} |
| 109 | + interpreter = load_interpreter(args) |
| 110 | + |
| 111 | + assert interpreter.model == "gpt-4" |
| 112 | + assert interpreter.temperature == 0.7 |
| 113 | + assert interpreter.auto_run is True |
| 114 | + assert interpreter.debug is True |
0 commit comments