eval harness: API keys from a file, mock-server tests for both API providers
--key-file / key_file: first line is the key, never recorded (only its source). Self-tests run the openai and anthropic providers against a local mock server: request translation, extras, token parameter, auth, tool loop, token accounting, and no key in the record.
This commit is contained in:
1 parent
d7bb9273f3
commit
2c1fdf1cc6
3 files changed
+100
-11
No files matched your search
@@ -128,3 +128,80 @@ class MatrixTest(unittest.TestCase):
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
class MockAPITest(unittest.TestCase):
|
||||
"""The API providers against a local mock server: request translation
|
||||
(tools, extras, token parameter, auth from a key file) and the tool loop,
|
||||
without any external call."""
|
||||
|
||||
def serve(self, reply_fn):
|
||||
import http.server
|
||||
import threading
|
||||
|
||||
seen = []
|
||||
|
||||
class H(http.server.BaseHTTPRequestHandler):
|
||||
def do_POST(self):
|
||||
body = json.loads(self.rfile.read(int(self.headers["content-length"])))
|
||||
seen.append({"path": self.path, "headers": {k.lower(): v for k, v in self.headers.items()}, "body": body})
|
||||
data = json.dumps(reply_fn(len(seen), body)).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("content-type", "application/json")
|
||||
self.send_header("content-length", str(len(data)))
|
||||
self.end_headers()
|
||||
self.wfile.write(data)
|
||||
|
||||
def log_message(self, *a):
|
||||
pass
|
||||
|
||||
srv = http.server.HTTPServer(("127.0.0.1", 0), H)
|
||||
threading.Thread(target=srv.serve_forever, daemon=True).start()
|
||||
self.addCleanup(srv.shutdown)
|
||||
return f"http://127.0.0.1:{srv.server_address[1]}", seen
|
||||
|
||||
def key_file(self):
|
||||
f = tempfile.NamedTemporaryFile("w", delete=False, suffix=".key")
|
||||
f.write("test-secret-123\n")
|
||||
f.close()
|
||||
return f.name
|
||||
|
||||
def test_openai_compatible(self):
|
||||
steps = [("write_file", {"path": "src/main.rs", "content": open(os.path.join(HERE, "scripts", "rust.json")).read() and json.load(open(os.path.join(HERE, "scripts", "rust.json")))[4][0]["input"]["content"]}),
|
||||
("run", {"command": "sh test.sh"}), ("finish", {})]
|
||||
|
||||
def reply(n, body):
|
||||
name, args = steps[min(n, len(steps)) - 1]
|
||||
return {"model": "mock", "usage": {"prompt_tokens": 100, "completion_tokens": 10, "prompt_tokens_details": {"cached_tokens": 40}},
|
||||
"choices": [{"finish_reason": "tool_calls", "message": {"role": "assistant", "content": None,
|
||||
"tool_calls": [{"id": f"c{n}", "type": "function", "function": {"name": name, "arguments": json.dumps(args)}}]}}]}
|
||||
|
||||
url, seen = self.serve(reply)
|
||||
out = tempfile.mkdtemp()
|
||||
_, rec = record.run_one(TASK, "rust", "openai", "mock-model", out, sandbox_kind=SANDBOX, evaluator=EVALUATOR, base_url=url + "/v1",
|
||||
provider_options={"extra": {"reasoning_effort": "medium"}, "key_file": self.key_file()})
|
||||
self.assertTrue(rec["accepted"], rec["verdict"])
|
||||
self.assertEqual(rec["metrics"]["cached_input_tokens"], 120)
|
||||
first = seen[0]
|
||||
self.assertEqual(first["path"], "/v1/chat/completions")
|
||||
self.assertEqual(first["headers"]["authorization"], "Bearer test-secret-123")
|
||||
self.assertEqual(first["body"]["reasoning_effort"], "medium")
|
||||
self.assertIn("max_tokens", first["body"])
|
||||
self.assertEqual({t["function"]["name"] for t in first["body"]["tools"]}, {"list_files", "read_file", "write_file", "edit_file", "run", "finish"})
|
||||
self.assertEqual(seen[1]["body"]["messages"][-1]["role"], "tool")
|
||||
self.assertNotIn("test-secret-123", json.dumps(rec))
|
||||
|
||||
def test_anthropic(self):
|
||||
def reply(n, body):
|
||||
blocks = [{"type": "tool_use", "id": f"t{n}", "name": "list_files", "input": {}}] if n == 1 else [{"type": "tool_use", "id": f"t{n}", "name": "finish", "input": {}}]
|
||||
return {"model": "mock", "stop_reason": "tool_use", "content": blocks, "usage": {"input_tokens": 50, "output_tokens": 5, "cache_read_input_tokens": 20}}
|
||||
|
||||
url, seen = self.serve(reply)
|
||||
_, rec = record.run_one(TASK, "zig", "anthropic", "mock-model", tempfile.mkdtemp(), sandbox_kind=SANDBOX, base_url=url,
|
||||
provider_options={"key_file": self.key_file(), "extra": {"thinking": {"type": "enabled", "budget_tokens": 1024}}})
|
||||
self.assertEqual(rec["metrics"]["termination"], "finished")
|
||||
self.assertEqual(seen[0]["path"], "/v1/messages")
|
||||
self.assertEqual(seen[0]["headers"]["x-api-key"], "test-secret-123")
|
||||
self.assertEqual(seen[0]["body"]["thinking"]["budget_tokens"], 1024)
|
||||
self.assertEqual(seen[1]["body"]["messages"][-1]["content"][0]["type"], "tool_result")
|
||||
self.assertEqual(rec["metrics"]["input_tokens"], 140)
|
||||
Reference in new issue
Block a user