This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
Expand file tree
/
Copy pathtest_cli.py
More file actions
221 lines (187 loc) · 6.76 KB
/
test_cli.py
File metadata and controls
221 lines (187 loc) · 6.76 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"""Tests for the CLI module."""
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
from click.testing import CliRunner
from codegate.cli import cli
from codegate.codegate_logging import LogFormat, LogLevel
from codegate.config import DEFAULT_PROVIDER_URLS
@pytest.fixture
def cli_runner() -> CliRunner:
"""Create a Click CLI test runner."""
return CliRunner()
@pytest.fixture
def mock_logging(monkeypatch: Any) -> MagicMock:
"""Mock the logging function."""
mock = MagicMock()
monkeypatch.setattr("codegate.cli.structlog.get_logger", mock)
return mock
@pytest.fixture
def mock_setup_logging(monkeypatch: Any) -> MagicMock:
"""Mock the setup_logging function."""
mock = MagicMock()
monkeypatch.setattr("codegate.cli.setup_logging", mock)
return mock
@pytest.fixture
def temp_config_file(tmp_path: Path) -> Path:
"""Create a temporary config file."""
config_file = tmp_path / "config.yaml"
config_file.write_text(
"""
port: 8989
host: localhost
log_level: DEBUG
log_format: JSON
"""
)
return config_file
def test_cli_version(cli_runner: CliRunner) -> None:
"""Test CLI version command."""
result = cli_runner.invoke(cli, ["--version"])
assert result.exit_code == 0
def test_serve_default_options(
cli_runner: CliRunner, mock_logging: Any, mock_setup_logging: Any
) -> None:
"""Test serve command with default options."""
with patch("uvicorn.run") as mock_run:
logger_instance = MagicMock()
mock_logging.return_value = logger_instance
result = cli_runner.invoke(cli, ["serve"])
assert result.exit_code == 0
mock_setup_logging.assert_called_once_with(LogLevel.INFO, LogFormat.JSON)
mock_logging.assert_called_with("codegate")
logger_instance.info.assert_any_call(
"Starting server",
extra={
"host": "localhost",
"port": 8989,
"log_level": "INFO",
"log_format": "JSON",
"prompts_loaded": 7, # Default prompts are loaded
"provider_urls": DEFAULT_PROVIDER_URLS,
},
)
mock_run.assert_called_once()
def test_serve_custom_options(
cli_runner: CliRunner, mock_logging: Any, mock_setup_logging: Any
) -> None:
"""Test serve command with custom options."""
with patch("uvicorn.run") as mock_run:
logger_instance = MagicMock()
mock_logging.return_value = logger_instance
result = cli_runner.invoke(
cli,
[
"serve",
"--port",
"8989",
"--host",
"localhost",
"--log-level",
"DEBUG",
"--log-format",
"TEXT",
],
)
assert result.exit_code == 0
mock_setup_logging.assert_called_once_with(LogLevel.DEBUG, LogFormat.TEXT)
mock_logging.assert_called_with("codegate")
logger_instance.info.assert_any_call(
"Starting server",
extra={
"host": "localhost",
"port": 8989,
"log_level": "DEBUG",
"log_format": "TEXT",
"prompts_loaded": 7, # Default prompts are loaded
"provider_urls": DEFAULT_PROVIDER_URLS,
},
)
mock_run.assert_called_once()
def test_serve_invalid_port(cli_runner: CliRunner) -> None:
"""Test serve command with invalid port."""
result = cli_runner.invoke(cli, ["serve", "--port", "999999"])
assert result.exit_code == 2
assert "Port must be between 1 and 65535" in result.output
def test_serve_invalid_log_level(cli_runner: CliRunner) -> None:
"""Test serve command with invalid log level."""
result = cli_runner.invoke(cli, ["serve", "--log-level", "INVALID"])
assert result.exit_code == 2
assert "Invalid value for '--log-level'" in result.output
def test_serve_with_config_file(
cli_runner: CliRunner, mock_logging: Any, temp_config_file: Path, mock_setup_logging: Any
) -> None:
"""Test serve command with config file."""
with patch("uvicorn.run") as mock_run:
logger_instance = MagicMock()
mock_logging.return_value = logger_instance
result = cli_runner.invoke(cli, ["serve", "--config", str(temp_config_file)])
assert result.exit_code == 0
mock_setup_logging.assert_called_once_with(LogLevel.DEBUG, LogFormat.JSON)
mock_logging.assert_called_with("codegate")
logger_instance.info.assert_any_call(
"Starting server",
extra={
"host": "localhost",
"port": 8989,
"log_level": "DEBUG",
"log_format": "JSON",
"prompts_loaded": 7, # Default prompts are loaded
"provider_urls": DEFAULT_PROVIDER_URLS,
},
)
mock_run.assert_called_once()
def test_serve_with_nonexistent_config_file(cli_runner: CliRunner) -> None:
"""Test serve command with nonexistent config file."""
result = cli_runner.invoke(cli, ["serve", "--config", "nonexistent.yaml"])
assert result.exit_code == 2
assert "does not exist" in result.output
def test_serve_priority_resolution(
cli_runner: CliRunner,
mock_logging: Any,
temp_config_file: Path,
env_vars: Any,
mock_setup_logging: Any,
) -> None:
"""Test serve command respects configuration priority."""
with patch("uvicorn.run") as mock_run:
logger_instance = MagicMock()
mock_logging.return_value = logger_instance
result = cli_runner.invoke(
cli,
[
"serve",
"--config",
str(temp_config_file),
"--port",
"8080",
"--host",
"example.com",
"--log-level",
"ERROR",
"--log-format",
"TEXT",
],
)
assert result.exit_code == 0
mock_setup_logging.assert_called_once_with(LogLevel.ERROR, LogFormat.TEXT)
mock_logging.assert_called_with("codegate")
logger_instance.info.assert_any_call(
"Starting server",
extra={
"host": "example.com",
"port": 8080,
"log_level": "ERROR",
"log_format": "TEXT",
"prompts_loaded": 7, # Default prompts are loaded
"provider_urls": DEFAULT_PROVIDER_URLS,
},
)
mock_run.assert_called_once()
def test_main_function() -> None:
"""Test main function."""
with patch("codegate.cli.cli") as mock_cli:
from codegate.cli import main
main()
mock_cli.assert_called_once()