Spaces:
Sleeping
Sleeping
File size: 10,217 Bytes
097eaf0 | 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 | """
Integration tests for agent method with mocked requests.
"""
import unittest
from unittest.mock import patch, MagicMock
from pydantic import BaseModel, Field
from typing import List, Optional
from firecrawl import FirecrawlApp
class Founder(BaseModel):
name: str = Field(description="Full name of the founder")
role: Optional[str] = Field(None, description="Role or position")
background: Optional[str] = Field(None, description="Professional background")
class FoundersSchema(BaseModel):
founders: List[Founder] = Field(description="List of founders")
class TestAgent(unittest.TestCase):
"""Integration tests for agent method."""
@patch('firecrawl.v2.utils.http_client.requests.post')
@patch('firecrawl.v2.utils.http_client.requests.get')
def test_agent_basic(self, mock_get, mock_post):
"""Test basic agent call."""
# Mock start agent response
mock_start_response = MagicMock()
mock_start_response.ok = True
mock_start_response.status_code = 200
mock_start_response.json.return_value = {
"success": True,
"id": "test-agent-123",
"status": "processing"
}
mock_post.return_value = mock_start_response
# Mock get status response (completed)
mock_status_response = MagicMock()
mock_status_response.ok = True
mock_status_response.status_code = 200
mock_status_response.json.return_value = {
"success": True,
"id": "test-agent-123",
"status": "completed",
"data": {
"founders": [
{"name": "John Doe", "role": "CEO", "background": "Tech entrepreneur"},
{"name": "Jane Smith", "role": "CTO", "background": "Software engineer"}
]
},
"creditsUsed": 10,
"expiresAt": "2024-01-01T00:00:00Z"
}
mock_get.return_value = mock_status_response
app = FirecrawlApp(api_key="test-api-key")
result = app.agent(
prompt="Find the founders of Firecrawl",
schema=FoundersSchema
)
# Verify post was called with correct URL and data
mock_post.assert_called_once()
post_call_args = mock_post.call_args
post_url = post_call_args[1]["url"] if "url" in post_call_args[1] else post_call_args[0][0]
assert "/v2/agent" in str(post_url)
# Check request body
request_body = post_call_args[1]["json"]
assert request_body["prompt"] == "Find the founders of Firecrawl"
assert "schema" in request_body
assert request_body["schema"]["type"] == "object"
assert "founders" in request_body["schema"]["properties"]
# Verify get was called to check status
mock_get.assert_called()
# Check result
assert result.status == "completed"
assert result.data is not None
@patch('firecrawl.v2.utils.http_client.requests.post')
def test_agent_with_urls(self, mock_post):
"""Test agent call with URLs."""
mock_response = MagicMock()
mock_response.ok = True
mock_response.status_code = 200
mock_response.json.return_value = {
"success": True,
"status": "completed",
"data": {"result": "done"}
}
mock_post.return_value = mock_response
app = FirecrawlApp(api_key="test-api-key")
result = app.agent(
urls=["https://example.com", "https://test.com"],
prompt="Extract information",
schema={"type": "object", "properties": {"info": {"type": "string"}}}
)
# Check request body includes URLs
post_call_args = mock_post.call_args
request_body = post_call_args[1]["json"]
assert request_body["urls"] == ["https://example.com", "https://test.com"]
assert request_body["prompt"] == "Extract information"
@patch('firecrawl.v2.utils.http_client.requests.post')
def test_agent_with_dict_schema(self, mock_post):
"""Test agent call with dict schema."""
mock_response = MagicMock()
mock_response.ok = True
mock_response.status_code = 200
mock_response.json.return_value = {
"success": True,
"status": "completed",
"data": {"result": "done"}
}
mock_post.return_value = mock_response
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
}
}
app = FirecrawlApp(api_key="test-api-key")
result = app.agent(
prompt="Extract person data",
schema=schema
)
# Check request body includes schema
post_call_args = mock_post.call_args
request_body = post_call_args[1]["json"]
assert request_body["schema"] == schema
@patch('firecrawl.v2.utils.http_client.requests.post')
def test_agent_with_all_params(self, mock_post):
"""Test agent call with all parameters."""
mock_response = MagicMock()
mock_response.ok = True
mock_response.status_code = 200
mock_response.json.return_value = {
"success": True,
"status": "completed",
"data": {"result": "done"}
}
mock_post.return_value = mock_response
schema = {"type": "object"}
urls = ["https://example.com"]
app = FirecrawlApp(api_key="test-api-key")
result = app.agent(
urls=urls,
prompt="Complete test",
schema=schema,
integration="test-integration",
max_credits=50,
strict_constrain_to_urls=True,
poll_interval=1,
timeout=30
)
# Check all parameters are in request body
post_call_args = mock_post.call_args
request_body = post_call_args[1]["json"]
assert request_body["prompt"] == "Complete test"
assert request_body["urls"] == urls
assert request_body["schema"] == schema
assert request_body["integration"] == "test-integration"
assert request_body["maxCredits"] == 50
assert request_body["strictConstrainToURLs"] is True
@patch('firecrawl.v2.utils.http_client.requests.post')
def test_agent_pydantic_schema_normalization(self, mock_post):
"""Test that Pydantic schemas are properly normalized."""
mock_response = MagicMock()
mock_response.ok = True
mock_response.status_code = 200
mock_response.json.return_value = {
"success": True,
"status": "completed",
"data": {"result": "done"}
}
mock_post.return_value = mock_response
app = FirecrawlApp(api_key="test-api-key")
result = app.agent(
prompt="Find founders",
schema=FoundersSchema
)
# Check that schema was normalized to JSON schema format
post_call_args = mock_post.call_args
request_body = post_call_args[1]["json"]
assert "schema" in request_body
schema = request_body["schema"]
assert schema["type"] == "object"
assert "properties" in schema
assert "founders" in schema["properties"]
assert schema["properties"]["founders"]["type"] == "array"
@patch('firecrawl.v2.utils.http_client.requests.post')
@patch('firecrawl.v2.utils.http_client.requests.get')
def test_agent_url_construction(self, mock_get, mock_post):
"""Test that agent requests are sent to correct URL."""
# Mock start agent response
mock_start_response = MagicMock()
mock_start_response.ok = True
mock_start_response.status_code = 200
mock_start_response.json.return_value = {
"success": True,
"id": "test-agent-123",
"status": "processing"
}
mock_post.return_value = mock_start_response
# Mock get status response
mock_status_response = MagicMock()
mock_status_response.ok = True
mock_status_response.status_code = 200
mock_status_response.json.return_value = {
"success": True,
"id": "test-agent-123",
"status": "completed",
"data": {"result": "done"}
}
mock_get.return_value = mock_status_response
app = FirecrawlApp(api_key="test-api-key", api_url="https://api.firecrawl.dev")
result = app.agent(prompt="Test prompt")
# Check POST URL - requests.post is called with url as keyword arg
post_call_args = mock_post.call_args
post_url = post_call_args[1].get("url") if "url" in post_call_args[1] else post_call_args[0][0]
assert "/v2/agent" in str(post_url)
# Check GET URL
get_call_args = mock_get.call_args
get_url = get_call_args[1].get("url") if "url" in get_call_args[1] else get_call_args[0][0]
assert "/v2/agent/test-agent-123" in str(get_url)
@patch('firecrawl.v2.utils.http_client.requests.post')
def test_agent_headers(self, mock_post):
"""Test that agent requests include correct headers."""
mock_response = MagicMock()
mock_response.ok = True
mock_response.status_code = 200
mock_response.json.return_value = {
"success": True,
"status": "completed",
"data": {"result": "done"}
}
mock_post.return_value = mock_response
app = FirecrawlApp(api_key="test-api-key")
result = app.agent(prompt="Test prompt")
# Check headers
post_call_args = mock_post.call_args
headers = post_call_args[1]["headers"]
assert "Authorization" in headers
assert headers["Authorization"] == "Bearer test-api-key"
assert headers["Content-Type"] == "application/json"
if __name__ == '__main__':
unittest.main()
|