Spaces:
Sleeping
Sleeping
File size: 3,157 Bytes
c9c2d7e 46b394e c9c2d7e 46b394e c9c2d7e bd0cdc2 46b394e | 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 | from fastapi.testclient import TestClient
from server.main import app
import os
client = TestClient(app)
def test_health_check():
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_healthz_check():
response = client.get("/healthz")
assert response.status_code == 200
assert response.json() == {"status": "ok"}
def test_generate_agent():
payload = {
"name": "TestApiAgent",
"prompt": "You are a test agent.",
"task": "Do nothing.",
"model": "claude-3-5-sonnet-20241022",
"provider": "anthropic"
}
response = client.post("/api/generate", json=payload)
assert response.status_code == 200
data = response.json()
assert data["status"] == "success"
assert "TestApiAgent" in data["code"]
assert "filename" in data
assert data["filename"] == "testapiagent.py"
def test_generate_agent_huggingface():
payload = {
"name": "TestHfAgent",
"prompt": "You are a test agent.",
"task": "Do nothing.",
"model": "meta-llama/Meta-Llama-3-8B-Instruct",
"provider": "huggingface"
}
response = client.post("/api/generate", json=payload)
assert response.status_code == 200
data = response.json()
assert data["status"] == "success"
assert "TestHfAgent" in data["code"]
def test_generate_agent_invalid_model():
payload = {
"name": "TestAgent",
"prompt": "Test",
"task": "Test",
"model": "invalid-model",
"provider": "anthropic"
}
response = client.post("/api/generate", json=payload)
assert response.status_code == 422 # Validation error
def test_generate_agent_long_prompt():
payload = {
"name": "TestAgent",
"prompt": "A" * 10001, # Exceeds 10000 char limit
"task": "Test",
"model": "claude-3-5-sonnet-20241022",
"provider": "anthropic"
}
response = client.post("/api/generate", json=payload)
assert response.status_code == 400
assert "exceeds maximum length" in response.json()["detail"].lower()
def test_execute_agent():
test_code = '''
class TestAgent:
def __init__(self, api_key):
self.api_key = api_key
def run(self, task):
return f"Processed: {task}"
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--task", default="test")
args = parser.parse_args()
agent = TestAgent("test")
print(agent.run(args.task))
'''
payload = {
"code": test_code,
"task": "Hello World"
}
response = client.post("/api/execute", json=payload)
assert response.status_code == 200
data = response.json()
assert data["status"] == "success"
assert "Processed: Hello World" in data["output"]
def test_execute_agent_large_code():
payload = {
"code": "A" * 100001, # Exceeds 100KB limit
"task": "Test"
}
response = client.post("/api/execute", json=payload)
assert response.status_code == 400
assert "exceeds maximum size" in response.json()["detail"].lower()
|