-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfirecrawl_test.go_V0
304 lines (241 loc) · 9.68 KB
/
firecrawl_test.go_V0
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package firecrawl
import (
"log"
"os"
"testing"
"time"
"github.com/google/uuid"
"github.com/joho/godotenv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var API_URL_V0 string
var TEST_API_KEY_V0 string
func init() {
err := godotenv.Load("../.env")
if err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
API_URL_V0 = os.Getenv("API_URL")
TEST_API_KEY_V0 = os.Getenv("TEST_API_KEY")
}
func TestNoAPIKeyV0(t *testing.T) {
_, err := NewFirecrawlApp("", API_URL_V0, "v0")
assert.Error(t, err)
assert.Contains(t, err.Error(), "no API key provided")
}
func TestScrapeURLInvalidAPIKeyV0(t *testing.T) {
app, err := NewFirecrawlApp("invalid_api_key", API_URL_V0, "v0")
require.NoError(t, err)
_, err = app.ScrapeURL("https://firecrawl.dev", nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unexpected error during scrape URL: Status code 401. Unauthorized: Invalid token")
}
func TestBlocklistedURLV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
_, err = app.ScrapeURL("https://facebook.com/fake-test", nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unexpected error during scrape URL: Status code 403. Firecrawl currently does not support social media scraping due to policy restrictions.")
}
func TestSuccessfulResponseWithValidPreviewTokenV0(t *testing.T) {
app, err := NewFirecrawlApp("this_is_just_a_preview_token", API_URL_V0, "v0")
require.NoError(t, err)
response, err := app.ScrapeURL("https://roastmywebsite.ai", nil)
require.NoError(t, err)
assert.NotNil(t, response)
scrapeResponse := response.(*FirecrawlDocumentV0)
assert.Contains(t, scrapeResponse.Content, "_Roast_")
}
func TestScrapeURLE2EV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
response, err := app.ScrapeURL("https://roastmywebsite.ai", nil)
require.NoError(t, err)
assert.NotNil(t, response)
scrapeResponse := response.(*FirecrawlDocumentV0)
assert.Contains(t, scrapeResponse.Content, "_Roast_")
assert.NotEqual(t, scrapeResponse.Markdown, "")
assert.NotNil(t, scrapeResponse.Metadata)
assert.Equal(t, scrapeResponse.HTML, "")
}
func TestSuccessfulResponseWithValidAPIKeyAndIncludeHTMLV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
params := map[string]any{
"pageOptions": map[string]any{
"includeHtml": true,
},
}
response, err := app.ScrapeURL("https://roastmywebsite.ai", params)
require.NoError(t, err)
assert.NotNil(t, response)
scrapeResponse := response.(*FirecrawlDocumentV0)
assert.Contains(t, scrapeResponse.Content, "_Roast_")
assert.Contains(t, scrapeResponse.Markdown, "_Roast_")
assert.Contains(t, scrapeResponse.HTML, "<h1")
assert.NotNil(t, scrapeResponse.Metadata)
}
func TestSuccessfulResponseForValidScrapeWithPDFFileV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
response, err := app.ScrapeURL("https://arxiv.org/pdf/astro-ph/9301001.pdf", nil)
require.NoError(t, err)
assert.NotNil(t, response)
scrapeResponse := response.(*FirecrawlDocumentV0)
assert.Contains(t, scrapeResponse.Content, "We present spectrophotometric observations of the Broad Line Radio Galaxy")
assert.NotNil(t, scrapeResponse.Metadata)
}
func TestSuccessfulResponseForValidScrapeWithPDFFileWithoutExplicitExtensionV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
response, err := app.ScrapeURL("https://arxiv.org/pdf/astro-ph/9301001", nil)
time.Sleep(6 * time.Second) // wait for 6 seconds
require.NoError(t, err)
assert.NotNil(t, response)
scrapeResponse := response.(*FirecrawlDocumentV0)
assert.Contains(t, scrapeResponse.Content, "We present spectrophotometric observations of the Broad Line Radio Galaxy")
assert.NotNil(t, scrapeResponse.Metadata)
}
func TestCrawlURLInvalidAPIKeyV0(t *testing.T) {
app, err := NewFirecrawlApp("invalid_api_key", API_URL_V0, "v0")
require.NoError(t, err)
_, err = app.CrawlURL("https://firecrawl.dev", nil, false, 2, "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unexpected error during start crawl job: Status code 401. Unauthorized: Invalid token")
}
func TestShouldReturnErrorForBlocklistedURLV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
_, err = app.CrawlURL("https://twitter.com/fake-test", nil, false, 2, "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unexpected error during start crawl job: Status code 403. Firecrawl currently does not support social media scraping due to policy restrictions.")
}
func TestCrawlURLWaitForCompletionE2EV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
params := map[string]any{
"crawlerOptions": map[string]any{
"excludes": []string{"blog/*"},
},
}
response, err := app.CrawlURL("https://roastmywebsite.ai", params, true, 2, "")
require.NoError(t, err)
assert.NotNil(t, response)
crawlResponse := response.([]*FirecrawlDocumentV0)
require.IsType(t, []*FirecrawlDocumentV0{}, crawlResponse)
assert.Greater(t, len(crawlResponse), 0)
assert.Contains(t, crawlResponse[0].Content, "_Roast_")
}
func TestCrawlURLWithIdempotencyKeyE2EV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
uniqueIdempotencyKey := uuid.New().String()
params := map[string]any{
"crawlerOptions": map[string]any{
"excludes": []string{"blog/*"},
},
}
response, err := app.CrawlURL("https://roastmywebsite.ai", params, true, 2, uniqueIdempotencyKey)
require.NoError(t, err)
assert.NotNil(t, response)
data := response.([]*FirecrawlDocumentV0)
require.Greater(t, len(data), 0)
require.IsType(t, []*FirecrawlDocumentV0{}, data)
assert.Contains(t, data[0].Content, "_Roast_")
_, err = app.CrawlURL("https://firecrawl.dev", params, true, 2, uniqueIdempotencyKey)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Conflict: Failed to start crawl job due to a conflict. Idempotency key already used")
}
func TestCheckCrawlStatusE2EV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
params := map[string]any{
"crawlerOptions": map[string]any{
"excludes": []string{"blog/*"},
},
}
response, err := app.CrawlURL("https://firecrawl.dev", params, false, 2, "")
require.NoError(t, err)
assert.NotNil(t, response)
jobID, ok := response.(string)
assert.True(t, ok)
assert.NotEqual(t, "", jobID)
time.Sleep(30 * time.Second) // wait for 30 seconds
statusResponse, err := app.CheckCrawlStatus(jobID)
require.NoError(t, err)
assert.NotNil(t, statusResponse)
checkCrawlStatusResponse := statusResponse.(*JobStatusResponseV0)
assert.Equal(t, "completed", checkCrawlStatusResponse.Status)
assert.Greater(t, len(checkCrawlStatusResponse.Data), 0)
}
func TestSearchE2EV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
response, err := app.Search("test query", nil)
require.NoError(t, err)
assert.NotNil(t, response)
assert.Greater(t, len(response), 2)
assert.NotEqual(t, response[0].Content, "")
}
func TestSearchInvalidAPIKeyV0(t *testing.T) {
app, err := NewFirecrawlApp("invalid_api_key", API_URL_V0, "v0")
require.NoError(t, err)
_, err = app.Search("test query", nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unexpected error during search: Status code 401. Unauthorized: Invalid token")
}
func TestLLMExtractionV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
params := map[string]any{
"extractorOptions": ExtractorOptions{
Mode: "llm-extraction",
ExtractionPrompt: "Based on the information on the page, find what the company's mission is and whether it supports SSO, and whether it is open source",
ExtractionSchema: map[string]any{
"type": "object",
"properties": map[string]any{
"company_mission": map[string]string{"type": "string"},
"supports_sso": map[string]string{"type": "boolean"},
"is_open_source": map[string]string{"type": "boolean"},
},
"required": []string{"company_mission", "supports_sso", "is_open_source"},
},
},
}
response, err := app.ScrapeURL("https://mendable.ai", params)
require.NoError(t, err)
assert.NotNil(t, response)
scrapeResponse := response.(*FirecrawlDocumentV0)
assert.Contains(t, scrapeResponse.LLMExtraction, "company_mission")
assert.IsType(t, true, scrapeResponse.LLMExtraction["supports_sso"])
assert.IsType(t, true, scrapeResponse.LLMExtraction["is_open_source"])
}
func TestCancelCrawlJobInvalidAPIKeyV0(t *testing.T) {
app, err := NewFirecrawlApp("invalid_api_key", API_URL_V0, "v0")
require.NoError(t, err)
_, err = app.CancelCrawlJob("test query")
assert.Error(t, err)
assert.Contains(t, err.Error(), "Unexpected error during cancel crawl job: Status code 401. Unauthorized: Invalid token")
}
func TestCancelNonExistingCrawlJobV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
jobID := uuid.New().String()
_, err = app.CancelCrawlJob(jobID)
assert.Error(t, err)
assert.Contains(t, err.Error(), "Job not found")
}
func TestCancelCrawlJobE2EV0(t *testing.T) {
app, err := NewFirecrawlApp(TEST_API_KEY_V0, API_URL_V0, "v0")
require.NoError(t, err)
response, err := app.CrawlURL("https://firecrawl.dev", nil, false, 2, "")
require.NoError(t, err)
assert.NotNil(t, response)
jobID, ok := response.(string)
assert.True(t, ok)
assert.NotEqual(t, "", jobID)
status, err := app.CancelCrawlJob(jobID)
require.NoError(t, err)
assert.Equal(t, "cancelled", status)
}