test_openapi_schema_agent.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import json
  2. from pathlib import Path
  3. import httpx
  4. import pytest
  5. from agency_swarm import Agent
  6. SCHEMA_PATH = Path(__file__).resolve().parents[3] / "tests" / "data" / "schemas" / "pastebin.json"
  7. @pytest.mark.asyncio
  8. async def test_agent_calls_pastebin_tool(monkeypatch):
  9. """Agent should load schema from folder, apply headers, and send JSON-serializable payload."""
  10. captured = {}
  11. async def fake_request(self, method, url, **kwargs):
  12. json_payload = kwargs.get("json")
  13. if json_payload is not None:
  14. json.dumps(json_payload) # raises TypeError when payload contains non-serializable values
  15. captured.update({"method": method, "url": url, **kwargs})
  16. class Response:
  17. def json(self):
  18. return {"ok": True, "payload": kwargs.get("json")}
  19. return Response()
  20. monkeypatch.setattr(httpx.AsyncClient, "request", fake_request, raising=False)
  21. agent = Agent(
  22. name="PasteAgent",
  23. instructions="Use the pastebin tool.",
  24. model="gpt-5.4-mini",
  25. schemas_folder=str(SCHEMA_PATH.parent),
  26. api_headers={"pastebin.json": {"Authorization": "Bearer test-token"}},
  27. )
  28. paste_tool = next(tool for tool in agent.tools if tool.name == "createPaste")
  29. payload = {
  30. "requestBody": {
  31. "title": "Test Note",
  32. "content": "This is a paste.",
  33. "visibility": "public",
  34. }
  35. }
  36. result = await paste_tool.on_invoke_tool(None, json.dumps(payload))
  37. assert result["ok"] is True
  38. assert captured["headers"]["Authorization"] == "Bearer test-token"
  39. assert captured["json"]["visibility"] == "public"
  40. assert isinstance(captured["json"]["visibility"], str)