-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtest-config.py
More file actions
333 lines (285 loc) · 11.2 KB
/
test-config.py
File metadata and controls
333 lines (285 loc) · 11.2 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
#!/usr/bin/env python3
"""Test configuration cascade functionality."""
import subprocess
import os
import json
import shutil
import tempfile
import threading
import time
from http.server import HTTPServer, BaseHTTPRequestHandler
MOCK_PORT = 18820
RUNPROMPT = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "runprompt")
passed = 0
failed = 0
class MockHandler(BaseHTTPRequestHandler):
received_requests = []
def do_POST(self):
content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length).decode('utf-8')
MockHandler.received_requests.append({
'path': self.path,
'headers': dict(self.headers),
'body': json.loads(body) if body else None
})
response = {
"choices": [{
"message": {"content": "OK"}
}]
}
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(response).encode('utf-8'))
def log_message(self, format, *args):
pass
def run_server(server):
server.serve_forever()
def start_server(port):
MockHandler.received_requests = []
server = HTTPServer(('127.0.0.1', port), MockHandler)
thread = threading.Thread(target=run_server, args=(server,))
thread.daemon = True
thread.start()
time.sleep(0.1)
return server
def test(name, func):
global passed, failed
try:
func()
print("✅ %s" % name)
passed += 1
except AssertionError as e:
print("❌ %s" % name)
print(" %s" % e)
failed += 1
def clean_env():
"""Return a copy of environ with RUNPROMPT_* vars removed."""
env = os.environ.copy()
for key in list(env.keys()):
if key.startswith('RUNPROMPT_'):
del env[key]
return env
def test_config_file_model():
"""Test that model can be set from config file."""
server = start_server(MOCK_PORT)
config_dir = tempfile.mkdtemp()
try:
# Create config file
runprompt_dir = os.path.join(config_dir, ".runprompt")
os.makedirs(runprompt_dir)
config_file = os.path.join(runprompt_dir, "config.yml")
with open(config_file, "w") as f:
f.write("model: openai/gpt-4o-from-config\n")
# Create a prompt file without model
prompt_file = os.path.join(config_dir, "test.prompt")
with open(prompt_file, "w") as f:
f.write("---\n---\nHello {{name}}!")
env = clean_env()
env['OPENAI_BASE_URL'] = 'http://127.0.0.1:%d' % MOCK_PORT
env['OPENAI_API_KEY'] = 'test-key'
env['HOME'] = config_dir # So ~/.runprompt is found
result = subprocess.run(
[RUNPROMPT, prompt_file],
capture_output=True,
text=True,
env=env,
input='{"name": "World"}',
cwd=config_dir
)
assert result.returncode == 0, "Expected success, got: %s" % result.stderr
assert len(MockHandler.received_requests) == 1, "Expected 1 request"
req = MockHandler.received_requests[0]
assert req['body']['model'] == 'gpt-4o-from-config', \
"Expected model from config, got: %s" % req['body'].get('model')
finally:
server.shutdown()
shutil.rmtree(config_dir)
def test_env_overrides_config_file():
"""Test that env var overrides config file."""
server = start_server(MOCK_PORT + 1)
config_dir = tempfile.mkdtemp()
try:
# Create config file with one model
runprompt_dir = os.path.join(config_dir, ".runprompt")
os.makedirs(runprompt_dir)
config_file = os.path.join(runprompt_dir, "config.yml")
with open(config_file, "w") as f:
f.write("model: openai/from-config\n")
# Create a prompt file without model
prompt_file = os.path.join(config_dir, "test.prompt")
with open(prompt_file, "w") as f:
f.write("---\n---\nHello {{name}}!")
env = clean_env()
env['OPENAI_BASE_URL'] = 'http://127.0.0.1:%d' % (MOCK_PORT + 1)
env['OPENAI_API_KEY'] = 'test-key'
env['HOME'] = config_dir
env['RUNPROMPT_MODEL'] = 'openai/from-env' # Should override config
result = subprocess.run(
[RUNPROMPT, prompt_file],
capture_output=True,
text=True,
env=env,
input='{"name": "World"}',
cwd=config_dir
)
assert result.returncode == 0, "Expected success, got: %s" % result.stderr
req = MockHandler.received_requests[0]
assert req['body']['model'] == 'from-env', \
"Expected model from env, got: %s" % req['body'].get('model')
finally:
server.shutdown()
shutil.rmtree(config_dir)
def test_cli_overrides_env():
"""Test that CLI flag overrides env var."""
server = start_server(MOCK_PORT + 2)
config_dir = tempfile.mkdtemp()
try:
# Create a prompt file without model
prompt_file = os.path.join(config_dir, "test.prompt")
with open(prompt_file, "w") as f:
f.write("---\n---\nHello {{name}}!")
env = clean_env()
env['OPENAI_BASE_URL'] = 'http://127.0.0.1:%d' % (MOCK_PORT + 2)
env['OPENAI_API_KEY'] = 'test-key'
env['RUNPROMPT_MODEL'] = 'openai/from-env'
result = subprocess.run(
[RUNPROMPT, '--model=openai/from-cli', prompt_file],
capture_output=True,
text=True,
env=env,
input='{"name": "World"}',
cwd=config_dir
)
assert result.returncode == 0, "Expected success, got: %s" % result.stderr
req = MockHandler.received_requests[0]
assert req['body']['model'] == 'from-cli', \
"Expected model from CLI, got: %s" % req['body'].get('model')
finally:
server.shutdown()
shutil.rmtree(config_dir)
def test_local_config_overrides_home():
"""Test that ./.runprompt overrides ~/.runprompt."""
server = start_server(MOCK_PORT + 3)
home_dir = tempfile.mkdtemp()
work_dir = tempfile.mkdtemp()
try:
# Create home config
home_runprompt = os.path.join(home_dir, ".runprompt")
os.makedirs(home_runprompt)
with open(os.path.join(home_runprompt, "config.yml"), "w") as f:
f.write("model: openai/from-home\n")
# Create local config
local_runprompt = os.path.join(work_dir, ".runprompt")
os.makedirs(local_runprompt)
with open(os.path.join(local_runprompt, "config.yml"), "w") as f:
f.write("model: openai/from-local\n")
# Create prompt file
prompt_file = os.path.join(work_dir, "test.prompt")
with open(prompt_file, "w") as f:
f.write("---\n---\nHello!")
env = clean_env()
env['OPENAI_BASE_URL'] = 'http://127.0.0.1:%d' % (MOCK_PORT + 3)
env['OPENAI_API_KEY'] = 'test-key'
env['HOME'] = home_dir
# Clear XDG to avoid interference
env.pop('XDG_CONFIG_HOME', None)
result = subprocess.run(
[RUNPROMPT, prompt_file],
capture_output=True,
text=True,
env=env,
input='{}',
cwd=work_dir
)
assert result.returncode == 0, "Expected success, got: %s" % result.stderr
req = MockHandler.received_requests[0]
assert req['body']['model'] == 'from-local', \
"Expected model from local config, got: %s" % req['body'].get('model')
finally:
server.shutdown()
shutil.rmtree(home_dir)
shutil.rmtree(work_dir)
def test_api_key_from_config():
"""Test that API key can be set from config file."""
server = start_server(MOCK_PORT + 4)
config_dir = tempfile.mkdtemp()
try:
# Create config file with API key
runprompt_dir = os.path.join(config_dir, ".runprompt")
os.makedirs(runprompt_dir)
with open(os.path.join(runprompt_dir, "config.yml"), "w") as f:
f.write("openai_api_key: key-from-config\n")
prompt_file = os.path.join(config_dir, "test.prompt")
with open(prompt_file, "w") as f:
f.write("---\nmodel: openai/gpt-4o\n---\nHello!")
env = clean_env()
env['OPENAI_BASE_URL'] = 'http://127.0.0.1:%d' % (MOCK_PORT + 4)
env['HOME'] = config_dir
# Don't set OPENAI_API_KEY - should come from config
env.pop('OPENAI_API_KEY', None)
result = subprocess.run(
[RUNPROMPT, prompt_file],
capture_output=True,
text=True,
env=env,
input='{}',
cwd=config_dir
)
assert result.returncode == 0, "Expected success, got: %s" % result.stderr
req = MockHandler.received_requests[0]
assert 'Bearer key-from-config' in req['headers'].get('Authorization', ''), \
"Expected API key from config in auth header"
finally:
server.shutdown()
shutil.rmtree(config_dir)
def test_tool_path_from_config():
"""Test that tool_path can be set from config file."""
server = start_server(MOCK_PORT + 5)
config_dir = tempfile.mkdtemp()
try:
# Create tools directory with a tool
tools_dir = os.path.join(config_dir, "my_tools")
os.makedirs(tools_dir)
with open(os.path.join(tools_dir, "test_tool.py"), "w") as f:
f.write('def my_tool(x: str):\n """A test tool."""\n return x\n')
# Create config file with tool_path
runprompt_dir = os.path.join(config_dir, ".runprompt")
os.makedirs(runprompt_dir)
with open(os.path.join(runprompt_dir, "config.yml"), "w") as f:
f.write("tool_path:\n - %s\n" % tools_dir)
prompt_file = os.path.join(config_dir, "test.prompt")
with open(prompt_file, "w") as f:
f.write("---\nmodel: openai/gpt-4o\ntools:\n - test_tool.*\n---\nHello!")
env = clean_env()
env['OPENAI_BASE_URL'] = 'http://127.0.0.1:%d' % (MOCK_PORT + 5)
env['OPENAI_API_KEY'] = 'test-key'
env['HOME'] = config_dir
result = subprocess.run(
[RUNPROMPT, prompt_file],
capture_output=True,
text=True,
env=env,
input='{}',
cwd=config_dir
)
assert result.returncode == 0, "Expected success, got: %s" % result.stderr
req = MockHandler.received_requests[0]
tools = req['body'].get('tools', [])
tool_names = [t['function']['name'] for t in tools]
assert 'my_tool' in tool_names, \
"Expected my_tool from config tool_path, got: %s" % tool_names
finally:
server.shutdown()
shutil.rmtree(config_dir)
if __name__ == '__main__':
test("config file model", test_config_file_model)
test("env overrides config file", test_env_overrides_config_file)
test("CLI overrides env", test_cli_overrides_env)
test("local config overrides home", test_local_config_overrides_home)
test("API key from config", test_api_key_from_config)
test("tool_path from config", test_tool_path_from_config)
print("")
print("Passed: %d, Failed: %d" % (passed, failed))
if failed > 0:
exit(1)