-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sentinel_backends.py
More file actions
250 lines (200 loc) · 9.19 KB
/
Copy pathtest_sentinel_backends.py
File metadata and controls
250 lines (200 loc) · 9.19 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
"""Tests for sentinel_backends module.
Tests follow TDD: written before implementation passes.
"""
import io
import json
import subprocess
import threading
import unittest
from unittest.mock import MagicMock, patch
import pytest
import sentinel_backends
from sentinel_backends import backend_reachable, call_llm, resolve_backend
class TestResolveBackend(unittest.TestCase):
"""Tests for resolve_backend()."""
def test_global_default_claude(self):
"""Global default: config has backend=claude with backends.claude.model=haiku."""
config = {
"backend": "claude",
"backends": {
"claude": {"model": "haiku"},
},
}
backend, model = resolve_backend(config)
self.assertEqual(backend, "claude")
self.assertEqual(model, "haiku")
def test_per_rule_override(self):
"""Per-rule override: config has backend=ollama but override_backend/model passed."""
config = {
"backend": "ollama",
"backends": {
"ollama": {"model": "gemma3:4b"},
},
}
backend, model = resolve_backend(config, override_backend="claude", override_model="opus")
self.assertEqual(backend, "claude")
self.assertEqual(model, "opus")
def test_backward_compat_model_only(self):
"""Backward compat: config has only model key (no backends key)."""
config = {"model": "gemma3:4b"}
backend, model = resolve_backend(config)
self.assertEqual(backend, "ollama")
self.assertEqual(model, "gemma3:4b")
def test_model_only_override(self):
"""Model-only override: config has ollama backend, but override_model is passed."""
config = {
"backend": "ollama",
"backends": {
"ollama": {"model": "gemma3:4b"},
},
}
backend, model = resolve_backend(config, override_model="gemma3:12b")
self.assertEqual(backend, "ollama")
self.assertEqual(model, "gemma3:12b")
class TestCallLlmOllama(unittest.TestCase):
"""Tests for call_llm dispatching to _call_ollama."""
def test_call_llm_ollama_returns_content(self):
"""call_llm with ollama backend calls Ollama and returns content."""
fake_response_body = json.dumps({
"message": {"content": '{"violation": false}'}
}).encode()
mock_resp = MagicMock()
mock_resp.read.return_value = fake_response_body
mock_resp.__enter__ = lambda s: s
mock_resp.__exit__ = MagicMock(return_value=False)
config = {
"backends": {
"ollama": {"url": "http://localhost:11434"},
},
"timeout_ms": 5000,
}
with patch("urllib.request.urlopen", return_value=mock_resp):
result = call_llm(
"test prompt",
"system prompt",
"gemma3:4b",
"ollama",
config,
)
self.assertIn("violation", result)
class TestCallLlmUnknownBackend(unittest.TestCase):
"""Tests for call_llm raising on unknown backend."""
def test_unknown_backend_raises_value_error(self):
"""call_llm with an unknown backend raises ValueError."""
with self.assertRaises(ValueError):
call_llm("prompt", "system", "model", "unknown_backend", {})
class TestInitOllamaSemaphore(unittest.TestCase):
"""Tests for init_ollama_semaphore."""
def test_sets_module_semaphore(self):
"""init_ollama_semaphore sets module-level _ollama_semaphore."""
sentinel_backends.init_ollama_semaphore(3)
self.assertIsNotNone(sentinel_backends._ollama_semaphore)
# Semaphore with concurrency 3: should allow 3 acquires without blocking
sem = sentinel_backends._ollama_semaphore
sem.acquire()
sem.acquire()
sem.acquire()
# 4th acquire should block — check it's not immediately available
acquired = sem.acquire(blocking=False)
self.assertFalse(acquired)
# Clean up
sem.release()
sem.release()
sem.release()
class TestCallLlmClaude(unittest.TestCase):
"""Tests for call_llm dispatching to _call_claude."""
def test_call_llm_claude(self):
"""Claude backend runs claude CLI with correct args."""
mock_result = MagicMock()
mock_result.stdout = '{"violation": false, "confidence": 0.8, "reason": "ok"}'
mock_result.returncode = 0
config = {
"timeout_ms": 10000,
"backends": {"claude": {"model": "haiku"}},
}
with patch("sentinel_backends.subprocess.run", return_value=mock_result) as mock_run:
result = call_llm("test prompt", "system prompt", "haiku", "claude", config)
self.assertIn("violation", result)
args = mock_run.call_args
cmd = args[0][0]
self.assertIn("claude", cmd)
self.assertIn("-p", cmd)
self.assertIn("--print", cmd)
self.assertIn("--model", cmd)
self.assertIn("haiku", cmd)
self.assertIn("--system-prompt", cmd)
self.assertIn("--no-session-persistence", cmd)
def test_call_llm_claude_timeout(self):
"""Claude backend raises on subprocess timeout."""
config = {"timeout_ms": 5000, "backends": {}}
with patch("sentinel_backends.subprocess.run",
side_effect=subprocess.TimeoutExpired(cmd="claude", timeout=5)):
with self.assertRaises(subprocess.TimeoutExpired):
call_llm("prompt", "system", "haiku", "claude", config)
def test_call_llm_claude_not_found(self):
"""Claude backend raises when binary not on PATH."""
config = {"timeout_ms": 5000, "backends": {}}
with patch("sentinel_backends.subprocess.run",
side_effect=FileNotFoundError("claude not found")):
with self.assertRaises(FileNotFoundError):
call_llm("prompt", "system", "haiku", "claude", config)
class TestCallLlmCopilot(unittest.TestCase):
def test_call_llm_copilot(self):
"""Copilot backend runs copilot CLI with correct args."""
mock_result = MagicMock()
mock_result.stdout = '{"violation": false, "confidence": 0.7, "reason": "ok"}'
mock_result.returncode = 0
config = {"timeout_ms": 10000, "backends": {"copilot": {"model": "gpt-5-mini"}}}
with patch("sentinel_backends.subprocess.run", return_value=mock_result) as mock_run:
result = call_llm("test prompt", "system prompt", "gpt-5-mini", "copilot", config)
self.assertIn("violation", result)
cmd = mock_run.call_args[0][0]
self.assertIn("copilot", cmd)
self.assertIn("-p", cmd)
self.assertIn("--model", cmd)
self.assertIn("gpt-5-mini", cmd)
self.assertIn("--output-format", cmd)
def test_call_llm_copilot_prepends_system_prompt(self):
"""Copilot has no --system-prompt flag, so system prompt is prepended to user prompt."""
mock_result = MagicMock()
mock_result.stdout = '{"violation": false}'
mock_result.returncode = 0
config = {"timeout_ms": 5000, "backends": {}}
with patch("sentinel_backends.subprocess.run", return_value=mock_result) as mock_run:
call_llm("user prompt", "system instructions", "gpt-5-mini", "copilot", config)
cmd = mock_run.call_args[0][0]
p_idx = cmd.index("-p")
combined_prompt = cmd[p_idx + 1]
self.assertIn("system instructions", combined_prompt)
self.assertIn("user prompt", combined_prompt)
class TestBackendReachable(unittest.TestCase):
def test_ollama_up(self):
"""Ollama reachable when HTTP endpoint responds."""
mock_resp = MagicMock()
mock_resp.__enter__ = lambda s: s
mock_resp.__exit__ = MagicMock(return_value=False)
config = {"backends": {"ollama": {"url": "http://localhost:11434"}}}
with patch("sentinel_backends.urllib.request.urlopen", return_value=mock_resp):
self.assertTrue(backend_reachable("ollama", config))
def test_ollama_down(self):
"""Ollama unreachable when HTTP fails."""
config = {"backends": {"ollama": {"url": "http://localhost:11434"}}}
with patch("sentinel_backends.urllib.request.urlopen", side_effect=ConnectionError):
self.assertFalse(backend_reachable("ollama", config))
def test_claude_found(self):
"""Claude reachable when binary exists on PATH."""
config = {"backends": {}}
with patch("sentinel_backends.shutil.which", return_value="/usr/local/bin/claude"):
self.assertTrue(backend_reachable("claude", config))
def test_claude_missing(self):
"""Claude unreachable when binary not on PATH."""
config = {"backends": {}}
with patch("sentinel_backends.shutil.which", return_value=None):
self.assertFalse(backend_reachable("claude", config))
def test_copilot_found(self):
"""Copilot reachable when binary exists on PATH."""
config = {"backends": {}}
with patch("sentinel_backends.shutil.which", return_value="/usr/local/bin/copilot"):
self.assertTrue(backend_reachable("copilot", config))
if __name__ == "__main__":
unittest.main()