-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.py
More file actions
428 lines (340 loc) · 16.1 KB
/
test_server.py
File metadata and controls
428 lines (340 loc) · 16.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
"""
Tests for the CloseClaw REST API server.
The server is started on a random OS-assigned port (port=0) for each test
class so tests are fully isolated and never conflict with existing services.
All Ollama and hardware calls are mocked so tests run without a live backend.
"""
from __future__ import annotations
import json
import threading
import http.client
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from closeclaw.config import Config
from closeclaw.agent import Agent
from closeclaw.server import CloseClawServer, create_server, VERSION
# ─── Helpers ──────────────────────────────────────────────────────────────────
def _make_config(tmp_path: Path) -> Config:
config = Config(config_path=tmp_path / "config.toml")
config._base = tmp_path / ".closeclaw"
config._data["skills"]["dir"] = str(config._base / "skills")
config._data["memory"]["db_path"] = str(config._base / "memory.db")
return config
def _make_agent(tmp_path: Path, model: str = "test-model") -> Agent:
config = _make_config(tmp_path)
return Agent(config, model=model, skills_dirs=[])
def _get(port: int, path: str) -> tuple:
"""GET request → (status_code, parsed_json_body)."""
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
conn.request("GET", path)
resp = conn.getresponse()
body = json.loads(resp.read())
headers = {k.lower(): v for k, v in resp.getheaders()}
conn.close()
return resp.status, body, headers
def _post(port: int, path: str, body: dict) -> tuple:
"""POST JSON request → (status_code, parsed_json_body)."""
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
data = json.dumps(body).encode()
conn.request(
"POST", path, body=data, headers={"Content-Type": "application/json"}
)
resp = conn.getresponse()
result = json.loads(resp.read())
conn.close()
return resp.status, result
def _post_raw(port: int, path: str, raw: bytes, content_type: str = "application/json") -> tuple:
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
conn.request("POST", path, body=raw, headers={"Content-Type": content_type})
resp = conn.getresponse()
body = resp.read()
status = resp.status
conn.close()
return status, body
def _post_sse(port: int, path: str, body: dict) -> list:
"""POST and collect SSE events; returns list of parsed event dicts."""
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
data = json.dumps(body).encode()
conn.request(
"POST", path, body=data, headers={"Content-Type": "application/json"}
)
resp = conn.getresponse()
raw = resp.read()
conn.close()
events = []
for block in raw.decode("utf-8").split("\n\n"):
block = block.strip()
if block.startswith("data: "):
try:
events.append(json.loads(block[6:]))
except json.JSONDecodeError:
pass
return events
# ─── Fixtures ─────────────────────────────────────────────────────────────────
@pytest.fixture
def server(tmp_path):
"""Start a server on a random port, yield (server, port), then shut down."""
agent = _make_agent(tmp_path)
config = _make_config(tmp_path)
srv = CloseClawServer(("127.0.0.1", 0), agent=agent, config=config)
port = srv.server_address[1]
t = threading.Thread(target=srv.serve_forever, daemon=True)
t.start()
yield srv, port
srv.shutdown()
srv.server_close()
# ─── /health ──────────────────────────────────────────────────────────────────
class TestHealthEndpoint:
def test_returns_200_ok(self, server):
_, port = server
status, body, _ = _get(port, "/health")
assert status == 200
assert body["status"] == "ok"
def test_includes_version(self, server):
_, port = server
_, body, _ = _get(port, "/health")
assert body["version"] == VERSION
def test_cors_header_present(self, server):
_, port = server
_, _, headers = _get(port, "/health")
assert headers.get("access-control-allow-origin") == "*"
# ─── /models ──────────────────────────────────────────────────────────────────
class TestModelsEndpoint:
def test_returns_model_list(self, server):
_, port = server
with patch("closeclaw.server.ModelManager") as MockMM:
inst = MockMM.return_value
inst.list_models.return_value = ["llama3.2:3b", "phi3:mini"]
inst.is_available.return_value = True
status, body, _ = _get(port, "/models")
assert status == 200
assert isinstance(body["models"], list)
def test_ollama_unavailable_flag(self, server):
_, port = server
with patch("closeclaw.server.ModelManager") as MockMM:
inst = MockMM.return_value
inst.list_models.return_value = []
inst.is_available.return_value = False
_, body, _ = _get(port, "/models")
assert body["models"] == []
assert body["ollama_available"] is False
def test_ollama_available_flag(self, server):
_, port = server
with patch("closeclaw.server.ModelManager") as MockMM:
inst = MockMM.return_value
inst.list_models.return_value = ["llama3.2:3b"]
inst.is_available.return_value = True
_, body, _ = _get(port, "/models")
assert body["ollama_available"] is True
# ─── /skills ──────────────────────────────────────────────────────────────────
class TestSkillsEndpoint:
def test_returns_skills_list(self, server):
_, port = server
status, body, _ = _get(port, "/skills")
assert status == 200
assert "skills" in body
assert isinstance(body["skills"], list)
def test_empty_when_no_skills(self, server):
# Default test config has no skills dir, so list is empty.
_, port = server
_, body, _ = _get(port, "/skills")
# Should not error; may be empty list.
assert isinstance(body["skills"], list)
def test_skill_fields(self, tmp_path):
"""Skills returned include required fields."""
skill_dir = tmp_path / "myskill"
skill_dir.mkdir()
(skill_dir / "skill.toml").write_text("""
[skill]
id = "myskill"
name = "My Skill"
version = "2.0.0"
description = "Does stuff"
""")
config = _make_config(tmp_path)
config._data["skills"]["dir"] = str(tmp_path)
agent = Agent(config, model="test", skills_dirs=[])
srv = CloseClawServer(("127.0.0.1", 0), agent=agent, config=config)
port = srv.server_address[1]
t = threading.Thread(target=srv.serve_forever, daemon=True)
t.start()
try:
_, body, _ = _get(port, "/skills")
skills = body["skills"]
assert any(s["id"] == "myskill" for s in skills)
skill = next(s for s in skills if s["id"] == "myskill")
for field in ("id", "name", "version", "description", "keywords"):
assert field in skill
finally:
srv.shutdown()
srv.server_close()
# ─── /hardware ────────────────────────────────────────────────────────────────
class TestHardwareEndpoint:
def test_returns_hardware_fields(self, server):
_, port = server
status, body, _ = _get(port, "/hardware")
assert status == 200
for field in (
"cpu_arch", "cpu_cores", "total_ram_mb", "free_ram_mb",
"has_cuda", "has_metal", "os_name", "recommended_model", "profile_name",
):
assert field in body, f"missing field: {field}"
def test_has_gpu_fields(self, server):
_, port = server
_, body, _ = _get(port, "/hardware")
assert "gpu_name" in body
assert "gpu_vram_mb" in body
assert "is_raspberry_pi" in body
# ─── POST /chat (non-streaming) ───────────────────────────────────────────────
class TestChatJsonEndpoint:
def test_missing_message_returns_400(self, server):
_, port = server
status, body = _post(port, "/chat", {"stream": False})
assert status == 400
assert "error" in body
def test_empty_message_returns_400(self, server):
_, port = server
status, body = _post(port, "/chat", {"message": " ", "stream": False})
assert status == 400
def test_invalid_json_returns_400(self, server):
_, port = server
status, _ = _post_raw(port, "/chat", b"not json at all")
assert status == 400
def test_successful_response_shape(self, server):
srv, port = server
def mock_chat(model, messages, temperature, top_p, stream):
yield "Hello from mock!"
with patch.object(srv.agent.model_manager, "chat", side_effect=mock_chat):
status, body = _post(port, "/chat", {"message": "hi", "stream": False})
assert status == 200
assert "response" in body
assert "Hello from mock!" in body["response"]
assert "model" in body
assert "iterations" in body
assert "tool_calls" in body
assert "skill_calls" in body
assert "tokens_approx" in body
def test_model_override_applied(self, server):
srv, port = server
def mock_chat(model, messages, temperature, top_p, stream):
yield "ok"
with patch.object(srv.agent.model_manager, "chat", side_effect=mock_chat):
_post(port, "/chat", {
"message": "hi", "stream": False, "model": "phi3:mini"
})
assert srv.agent.get_active_model() == "phi3:mini"
def test_tool_calls_in_response(self, server):
srv, port = server
call_count = [0]
def mock_chat(model, messages, temperature, top_p, stream):
if call_count[0] == 0:
call_count[0] += 1
yield '[TOOL:shell command="echo hello"]'
else:
yield "Done."
with patch.object(srv.agent.model_manager, "chat", side_effect=mock_chat):
status, body = _post(port, "/chat", {"message": "run echo", "stream": False})
assert status == 200
assert len(body["tool_calls"]) >= 1
assert body["tool_calls"][0]["tool"] == "shell"
# ─── POST /chat (SSE streaming) ───────────────────────────────────────────────
class TestChatSSEEndpoint:
def test_streams_token_events(self, server):
srv, port = server
def mock_chat(model, messages, temperature, top_p, stream):
yield "Hello"
yield " world"
with patch.object(srv.agent.model_manager, "chat", side_effect=mock_chat):
events = _post_sse(port, "/chat", {"message": "hi"})
token_events = [e for e in events if e.get("type") == "token"]
assert len(token_events) >= 1
combined = "".join(e["content"] for e in token_events)
assert "Hello" in combined
def test_final_done_event(self, server):
srv, port = server
def mock_chat(model, messages, temperature, top_p, stream):
yield "Final"
with patch.object(srv.agent.model_manager, "chat", side_effect=mock_chat):
events = _post_sse(port, "/chat", {"message": "test"})
done_events = [e for e in events if e.get("type") == "done"]
assert len(done_events) == 1
assert done_events[0]["response"] == "Final"
assert "model" in done_events[0]
def test_done_event_assembles_full_response(self, server):
srv, port = server
def mock_chat(model, messages, temperature, top_p, stream):
for word in ["The", " quick", " brown", " fox"]:
yield word
with patch.object(srv.agent.model_manager, "chat", side_effect=mock_chat):
events = _post_sse(port, "/chat", {"message": "story"})
done = next(e for e in events if e.get("type") == "done")
assert done["response"] == "The quick brown fox"
def test_content_type_is_sse(self, server):
srv, port = server
def mock_chat(model, messages, temperature, top_p, stream):
yield "hi"
with patch.object(srv.agent.model_manager, "chat", side_effect=mock_chat):
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
data = json.dumps({"message": "hi"}).encode()
conn.request("POST", "/chat", body=data,
headers={"Content-Type": "application/json"})
resp = conn.getresponse()
resp.read()
ct = resp.getheader("Content-Type")
conn.close()
assert ct is not None
assert "text/event-stream" in ct
def test_missing_message_still_400_not_sse(self, server):
_, port = server
# stream=True (default) but no message → should get JSON 400
status, body = _post(port, "/chat", {})
assert status == 400
# ─── Routing / 404 ────────────────────────────────────────────────────────────
class TestRouting:
def test_get_unknown_path_404(self, server):
_, port = server
status, body, _ = _get(port, "/nonexistent")
assert status == 404
assert "error" in body
def test_post_unknown_path_404(self, server):
_, port = server
status, body = _post(port, "/unknown", {})
assert status == 404
def test_options_preflight_200(self, server):
_, port = server
conn = http.client.HTTPConnection("127.0.0.1", port, timeout=10)
conn.request("OPTIONS", "/chat")
resp = conn.getresponse()
resp.read()
conn.close()
assert resp.status == 200
assert resp.getheader("Access-Control-Allow-Methods") is not None
# ─── create_server factory ────────────────────────────────────────────────────
class TestCreateServer:
def test_returns_closeclaw_server(self, tmp_path):
config = _make_config(tmp_path)
with patch("closeclaw.server.detect_hardware") as mock_hw:
mock_hw.return_value = MagicMock(recommended_model="llama3.2:3b")
srv = create_server(port=0, config=config)
assert isinstance(srv, CloseClawServer)
assert srv.agent is not None
srv.server_close()
def test_uses_provided_model(self, tmp_path):
config = _make_config(tmp_path)
srv = create_server(port=0, model="custom-model", config=config)
assert srv.agent.get_active_model() == "custom-model"
srv.server_close()
def test_auto_selects_model_when_none(self, tmp_path):
config = _make_config(tmp_path)
config._data["model"]["selection"] = "auto"
with patch("closeclaw.server.detect_hardware") as mock_hw:
mock_hw.return_value = MagicMock(recommended_model="phi3:mini")
srv = create_server(port=0, config=config)
assert srv.agent.get_active_model() == "phi3:mini"
srv.server_close()
def test_binds_to_requested_port(self, tmp_path):
config = _make_config(tmp_path)
srv = create_server(port=0, model="m", config=config)
assert srv.server_address[1] > 0
srv.server_close()