|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | """Test configuration cascade functionality.""" |
| 3 | +import importlib.machinery |
| 4 | +import importlib.util |
3 | 5 | import subprocess |
4 | 6 | import os |
5 | 7 | import json |
6 | 8 | import shutil |
7 | 9 | import tempfile |
8 | 10 | import threading |
9 | 11 | import time |
| 12 | +import urllib.request |
10 | 13 | from http.server import HTTPServer, BaseHTTPRequestHandler |
11 | 14 |
|
12 | 15 | MOCK_PORT = 18820 |
@@ -41,6 +44,30 @@ def log_message(self, format, *args): |
41 | 44 | pass |
42 | 45 |
|
43 | 46 |
|
| 47 | +class DummyResponse: |
| 48 | + def __enter__(self): |
| 49 | + return self |
| 50 | + |
| 51 | + def __exit__(self, exc_type, exc, tb): |
| 52 | + pass |
| 53 | + |
| 54 | + def read(self): |
| 55 | + response = { |
| 56 | + "choices": [{ |
| 57 | + "message": {"content": "OK"} |
| 58 | + }] |
| 59 | + } |
| 60 | + return json.dumps(response).encode("utf-8") |
| 61 | + |
| 62 | + |
| 63 | +def load_runprompt(): |
| 64 | + loader = importlib.machinery.SourceFileLoader("runprompt_module", RUNPROMPT) |
| 65 | + spec = importlib.util.spec_from_loader(loader.name, loader) |
| 66 | + module = importlib.util.module_from_spec(spec) |
| 67 | + loader.exec_module(module) |
| 68 | + return module |
| 69 | + |
| 70 | + |
44 | 71 | def run_server(server): |
45 | 72 | server.serve_forever() |
46 | 73 |
|
@@ -76,6 +103,48 @@ def clean_env(): |
76 | 103 | return env |
77 | 104 |
|
78 | 105 |
|
| 106 | +def test_timeout_cascade_for_provider_request(): |
| 107 | + """Test that timeout config reaches the provider request.""" |
| 108 | + module = load_runprompt() |
| 109 | + captured = [] |
| 110 | + original_urlopen = urllib.request.urlopen |
| 111 | + |
| 112 | + def fake_urlopen(req, **kwargs): |
| 113 | + captured.append(kwargs.get("timeout")) |
| 114 | + return DummyResponse() |
| 115 | + |
| 116 | + try: |
| 117 | + urllib.request.urlopen = fake_urlopen |
| 118 | + cases = [ |
| 119 | + ({}, {}, {}, 120.0, "default"), |
| 120 | + ({"timeout": 30}, {}, {}, 30.0, "config file"), |
| 121 | + ({"timeout": 30}, {"timeout": 45}, {}, 45.0, "env"), |
| 122 | + ({"timeout": 30}, {"timeout": 45}, {"timeout": 60}, 60.0, |
| 123 | + "CLI args"), |
| 124 | + ] |
| 125 | + for files, env, args, expected, source in cases: |
| 126 | + module.CONFIG["files"] = files |
| 127 | + module.CONFIG["env"] = env |
| 128 | + module.CONFIG["args"] = args |
| 129 | + module.make_request("https://example.test/chat/completions", |
| 130 | + "test-key", "gpt-4o", |
| 131 | + [{"role": "user", "content": "hi"}], |
| 132 | + {}, "openai") |
| 133 | + assert captured[-1] == expected, \ |
| 134 | + "Expected timeout from %s, got: %s" % (source, captured[-1]) |
| 135 | + finally: |
| 136 | + urllib.request.urlopen = original_urlopen |
| 137 | + |
| 138 | + |
| 139 | +def test_timeout_cli_flag(): |
| 140 | + """Test that --timeout is parsed as a config value.""" |
| 141 | + module = load_runprompt() |
| 142 | + parsed = module.parse_args(["--timeout", "12.5", "test.prompt"]) |
| 143 | + module.init_config(parsed) |
| 144 | + assert module.get_conf("timeout") == 12.5, \ |
| 145 | + "Expected parsed timeout, got: %s" % module.get_conf("timeout") |
| 146 | + |
| 147 | + |
79 | 148 | def test_config_file_model(): |
80 | 149 | """Test that model can be set from config file.""" |
81 | 150 | server = start_server(MOCK_PORT) |
@@ -320,6 +389,8 @@ def test_tool_path_from_config(): |
320 | 389 |
|
321 | 390 |
|
322 | 391 | if __name__ == '__main__': |
| 392 | + test("timeout cascade for provider request", test_timeout_cascade_for_provider_request) |
| 393 | + test("timeout CLI flag", test_timeout_cli_flag) |
323 | 394 | test("config file model", test_config_file_model) |
324 | 395 | test("env overrides config file", test_env_overrides_config_file) |
325 | 396 | test("CLI overrides env", test_cli_overrides_env) |
|
0 commit comments