-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cli.py
More file actions
306 lines (255 loc) · 10.8 KB
/
test_cli.py
File metadata and controls
306 lines (255 loc) · 10.8 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""Unit tests for the CLI.
Note: Tests which run async code synchronously from CLI entrypoints must be
marked async so that they do not interfere with pytest-asyncio's event loop.
"""
import json
from pathlib import Path
import tempfile
import textwrap
import typing as _t
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
import respx
from typer.testing import CliRunner
from plugboard.cli import app
from plugboard.cli.ai import _AGENTS_MD
runner = CliRunner()
def _create_test_project(
base_path: Path,
*,
as_package: bool = True,
include_hidden_dir: bool = False,
) -> Path:
"""Create a minimal Python project for CLI discovery tests."""
project_dir = base_path / "test_project"
project_dir.mkdir()
if as_package:
(project_dir / "__init__.py").write_text("")
(project_dir / "test_file.py").write_text("")
else:
(project_dir / "test_file.py").write_text(
textwrap.dedent("""
from plugboard.component import Component, IOController as IO
class VisibleComponent(Component):
io = IO(outputs=["out"])
async def step(self) -> None:
self.out = 1
""").strip()
)
if include_hidden_dir:
hidden_dir = project_dir / ".venv"
hidden_dir.mkdir()
(hidden_dir / "bad_module.py").write_text('raise RuntimeError("should not import")')
return project_dir
def test_cli_version() -> None:
"""Tests the version command."""
result = runner.invoke(app, ["version"])
# CLI must run without error
assert result.exit_code == 0
# Must output version information
assert "Plugboard version:" in result.stdout
assert "Platform:" in result.stdout
assert "Python version:" in result.stdout
@pytest.fixture
def test_project_dir() -> _t.Iterator[Path]:
"""Create a minimal Python package for testing."""
with tempfile.TemporaryDirectory() as tmpdir:
yield _create_test_project(Path(tmpdir))
@pytest.mark.asyncio
async def test_cli_process_run() -> None:
"""Tests the process run command."""
with patch("plugboard.cli.process.ProcessBuilder") as mock_process_builder:
mock_process = AsyncMock()
mock_process_builder.build.return_value = mock_process
result = runner.invoke(app, ["process", "run", "tests/data/minimal-process.yaml"])
# CLI must run without error
assert result.exit_code == 0
assert "Process complete" in result.stdout
# Process must be built
mock_process_builder.build.assert_called_once()
# Process must be initialised
mock_process.__aenter__.assert_called_once()
# Process must be run
mock_process.run.assert_called_once()
# Process must be destroyed
mock_process.__aexit__.assert_called_once()
def test_cli_process_tune() -> None:
"""Tests the process tune command."""
with patch("plugboard.cli.process.Tuner") as mock_tuner_cls:
mock_tuner = MagicMock()
mock_tuner_cls.return_value = mock_tuner
result = runner.invoke(
app, ["process", "tune", "tests/data/minimal-process-with-tune.yaml"]
)
# CLI must run without error
assert result.exit_code == 0
assert "Tune job complete" in result.stdout
# Tuner must be instantiated
mock_tuner_cls.assert_called_once()
# Tuner must be run
mock_tuner.run.assert_called_once()
def test_cli_process_diagram() -> None:
"""Tests the process diagram command."""
result = runner.invoke(app, ["process", "diagram", "tests/data/minimal-process.yaml"])
# CLI must run without error
assert result.exit_code == 0
# Must output a Mermaid flowchart
assert "flowchart" in result.stdout
@pytest.mark.asyncio
async def test_cli_process_run_with_local_override() -> None:
"""Tests the process run command with --process-type local."""
with patch("plugboard.cli.process.ProcessBuilder") as mock_process_builder:
mock_process = AsyncMock()
mock_process_builder.build.return_value = mock_process
result = runner.invoke(
app,
["process", "run", "tests/data/minimal-process.yaml", "--process-type", "local"],
)
# CLI must run without error
assert result.exit_code == 0
assert "Process complete" in result.stdout
# Process must be built with LocalProcess type
mock_process_builder.build.assert_called_once()
call_args = mock_process_builder.build.call_args
process_spec = call_args[0][0]
assert process_spec.type == "plugboard.process.LocalProcess"
assert process_spec.connector_builder.type == "plugboard.connector.AsyncioConnector"
@pytest.mark.asyncio
async def test_cli_process_run_with_ray_override() -> None:
"""Tests the process run command with --process-type ray."""
with patch("plugboard.cli.process.ProcessBuilder") as mock_process_builder:
mock_process = AsyncMock()
mock_process_builder.build.return_value = mock_process
result = runner.invoke(
app,
["process", "run", "tests/data/minimal-process.yaml", "--process-type", "ray"],
)
# CLI must run without error
assert result.exit_code == 0
assert "Process complete" in result.stdout
# Process must be built with RayProcess type
mock_process_builder.build.assert_called_once()
call_args = mock_process_builder.build.call_args
process_spec = call_args[0][0]
assert process_spec.type == "plugboard.process.RayProcess"
assert process_spec.connector_builder.type == "plugboard.connector.RayConnector"
assert process_spec.args.state.type == "plugboard.state.RayStateBackend"
def test_cli_process_validate() -> None:
"""Tests the process validate command."""
result = runner.invoke(app, ["process", "validate", "tests/data/minimal-process.yaml"])
# CLI must run without error for a valid config
assert result.exit_code == 0
assert "Validation passed" in result.stdout
def test_cli_process_validate_invalid() -> None:
"""Tests the process validate command with an invalid process."""
with patch("plugboard.cli.process.validate_process") as mock_validate:
mock_validate.return_value = ["Component 'x' has unconnected inputs: ['in_1']"]
result = runner.invoke(app, ["process", "validate", "tests/data/minimal-process.yaml"])
assert result.exit_code == 1
assert "Validation failed" in result.stderr
def test_cli_ai_init(tmp_path: Path) -> None:
"""Tests the ai init command creates AGENTS.md."""
result = runner.invoke(app, ["ai", "init", str(tmp_path)])
assert result.exit_code == 0
assert "Created" in result.stdout
# File must exist with expected content
agents_md = tmp_path / "AGENTS.md"
assert agents_md.exists()
content = agents_md.read_text()
assert "Plugboard" in content
def test_cli_ai_init_already_exists(tmp_path: Path) -> None:
"""Tests the ai init command fails when AGENTS.md already exists."""
(tmp_path / "AGENTS.md").write_text("existing content")
result = runner.invoke(app, ["ai", "init", str(tmp_path)])
assert result.exit_code == 1
# Error is printed to stderr which typer captures in output
assert "already exists" in result.output
def test_cli_ai_init_default_directory() -> None:
"""Tests the ai init command uses current directory by default."""
with tempfile.TemporaryDirectory() as tmpdir:
original_cwd = Path.cwd()
try:
import os
os.chdir(tmpdir)
result = runner.invoke(app, ["ai", "init"])
assert result.exit_code == 0
assert (Path(tmpdir) / "AGENTS.md").exists()
finally:
os.chdir(original_cwd)
def test_cli_ai_agents_template_is_packaged_file() -> None:
"""Tests the AI template is a real package file rather than a symlink."""
assert _AGENTS_MD.exists()
assert _AGENTS_MD.is_file()
assert not _AGENTS_MD.is_symlink()
@pytest.mark.parametrize(
("as_package", "include_hidden_dir", "expected_component_name"),
[
(True, False, None),
(True, True, None),
(False, False, "VisibleComponent"),
(False, True, "VisibleComponent"),
],
)
def test_cli_server_discover(
tmp_path: Path,
as_package: bool,
include_hidden_dir: bool,
expected_component_name: str | None,
) -> None:
"""Tests the server discover command."""
project_dir = _create_test_project(
tmp_path,
as_package=as_package,
include_hidden_dir=include_hidden_dir,
)
with respx.mock:
# Mock all the API endpoints
component_route = respx.post("http://test:8000/types/component").respond(
json={"status": "ok"}
)
connector_route = respx.post("http://test:8000/types/connector").respond(
json={"status": "ok"}
)
event_route = respx.post("http://test:8000/types/event").respond(json={"status": "ok"})
process_route = respx.post("http://test:8000/types/process").respond(json={"status": "ok"})
result = runner.invoke(
app,
[
"server",
"discover",
str(project_dir),
"--api-url",
"http://test:8000",
],
)
# CLI must run without error
assert result.exit_code == 0
assert result.exception is None
assert "Discovery complete" in result.stdout
# At minimum, should have discovered plugboard's built-in types
# The exact number may vary, but we expect some calls to each endpoint
assert component_route.called
assert connector_route.called
assert event_route.called
assert process_route.called
if expected_component_name is not None:
assert any(
json.loads(call.request.content)["name"] == expected_component_name
for call in component_route.calls
)
def test_cli_server_discover_with_env_var(test_project_dir: Path) -> None:
"""Tests the server discover command with environment variable."""
with respx.mock:
# Mock all the API endpoints with the env var URL
respx.post("http://env-test:9000/types/component").respond(json={"status": "ok"})
respx.post("http://env-test:9000/types/connector").respond(json={"status": "ok"})
respx.post("http://env-test:9000/types/event").respond(json={"status": "ok"})
respx.post("http://env-test:9000/types/process").respond(json={"status": "ok"})
result = runner.invoke(
app,
["server", "discover", str(test_project_dir)],
env={"PLUGBOARD_API_URL": "http://env-test:9000"},
)
# CLI must run without error
assert result.exit_code == 0
assert "Discovery complete" in result.stdout