Spaces:
Sleeping
Sleeping
File size: 14,275 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 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 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | package com.firecrawl;
import com.firecrawl.client.FirecrawlClient;
import com.firecrawl.models.*;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
/**
* Comprehensive Search Tests
*
* Tests the search functionality with various configurations.
* Based on Node.js SDK patterns and tested against live firecrawl.dev.
*
* Run with: FIRECRAWL_API_KEY=fc-xxx gradle test --tests "com.firecrawl.SearchTest"
*/
class SearchTest {
private static FirecrawlClient client;
@BeforeAll
static void setup() {
String apiKey = System.getenv("FIRECRAWL_API_KEY");
if (apiKey != null && !apiKey.isBlank()) {
client = FirecrawlClient.fromEnv();
}
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchMinimal() {
System.out.println("\n=== Test: Search - Minimal Request ===");
SearchData results = client.search("What is Firecrawl?");
assertNotNull(results, "Search results should not be null");
assertNotNull(results.getWeb(), "Web results should not be null");
assertTrue(!results.getWeb().isEmpty(), "Should have at least one web result");
// Verify result structure
Map<String, Object> firstResult = results.getWeb().get(0);
assertNotNull(firstResult.get("url"), "Result should have URL");
assertTrue(firstResult.get("url").toString().startsWith("http"),
"URL should be valid");
System.out.println("β Search completed successfully");
System.out.println(" Web results: " + results.getWeb().size());
System.out.println(" Sample result: " + firstResult.get("url"));
if (firstResult.get("title") != null) {
System.out.println(" Title: " + firstResult.get("title"));
}
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchWithLimit() {
System.out.println("\n=== Test: Search with Limit ===");
SearchData results = client.search("artificial intelligence",
SearchOptions.builder()
.limit(5)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
assertTrue(results.getWeb().size() <= 5,
"Should respect limit of 5: got " + results.getWeb().size());
System.out.println("β Search with limit completed");
System.out.println(" Requested limit: 5");
System.out.println(" Actual results: " + results.getWeb().size());
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchWithMultipleSources() {
System.out.println("\n=== Test: Search with Multiple Sources ===");
SearchData results = client.search("Firecrawl web scraping",
SearchOptions.builder()
.sources(List.of("web", "news"))
.limit(3)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
assertTrue(results.getWeb().size() <= 3, "Web results should respect limit");
System.out.println("β Multi-source search completed");
System.out.println(" Web results: " + results.getWeb().size());
if (results.getNews() != null) {
System.out.println(" News results: " + results.getNews().size());
} else {
System.out.println(" News results: 0");
}
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchResultStructure() {
System.out.println("\n=== Test: Verify Search Result Structure ===");
SearchData results = client.search("test query",
SearchOptions.builder()
.limit(1)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
if (!results.getWeb().isEmpty()) {
Map<String, Object> result = results.getWeb().get(0);
assertNotNull(result.get("url"), "Result must have URL");
assertTrue(result.get("url") instanceof String, "URL should be string");
assertTrue(result.get("url").toString().startsWith("http"),
"URL should be valid");
// Title and description may be null but if present should be strings
if (result.get("title") != null) {
assertTrue(result.get("title") instanceof String,
"Title should be string");
}
if (result.get("description") != null) {
assertTrue(result.get("description") instanceof String,
"Description should be string");
}
System.out.println("β Result structure verified");
System.out.println(" URL: β");
System.out.println(" Title: " + (result.get("title") != null ? "β" : "null"));
System.out.println(" Description: " + (result.get("description") != null ? "β" : "null"));
}
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchWithLocation() {
System.out.println("\n=== Test: Search with Location ===");
SearchData results = client.search("restaurants near me",
SearchOptions.builder()
.location("US")
.limit(5)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
System.out.println("β Search with location completed");
System.out.println(" Location: US");
System.out.println(" Results: " + results.getWeb().size());
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchWithTimeFilter() {
System.out.println("\n=== Test: Search with Time Filter ===");
SearchData results = client.search("latest AI news",
SearchOptions.builder()
.tbs("qdr:m") // Past month
.limit(5)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
System.out.println("β Search with time filter completed");
System.out.println(" Time filter: Past month (qdr:m)");
System.out.println(" Results: " + results.getWeb().size());
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchWithScrapeOptions() {
System.out.println("\n=== Test: Search with Scrape Options ===");
SearchData results = client.search("Firecrawl documentation",
SearchOptions.builder()
.limit(2)
.scrapeOptions(ScrapeOptions.builder()
.formats(List.of("markdown"))
.onlyMainContent(true)
.build())
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
// When scrapeOptions with markdown format are provided, results should include markdown content
if (!results.getWeb().isEmpty()) {
Map<String, Object> first = results.getWeb().get(0);
Object markdown = first.get("markdown");
assertNotNull(markdown, "Scraped result should contain markdown content when formats=[markdown]");
assertFalse(markdown.toString().isEmpty(), "Markdown content should not be empty");
}
System.out.println("β Search with scrape options completed");
System.out.println(" Results: " + results.getWeb().size());
System.out.println(" Scrape formats: markdown");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchFirecrawlSpecific() {
System.out.println("\n=== Test: Search for Firecrawl ===");
SearchData results = client.search("Firecrawl web scraping API",
SearchOptions.builder()
.limit(10)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
assertFalse(results.getWeb().isEmpty(), "Should find Firecrawl results");
// Verify results contain Firecrawl-related content
boolean hasFirecrawlContent = results.getWeb().stream()
.anyMatch(result -> {
String url = result.get("url").toString().toLowerCase();
String title = result.get("title") != null ?
result.get("title").toString().toLowerCase() : "";
String desc = result.get("description") != null ?
result.get("description").toString().toLowerCase() : "";
return url.contains("firecrawl") ||
title.contains("firecrawl") ||
desc.contains("firecrawl");
});
assertTrue(hasFirecrawlContent, "Results should mention Firecrawl");
System.out.println("β Firecrawl search completed");
System.out.println(" Total results: " + results.getWeb().size());
System.out.println(" Results mentioning Firecrawl: β");
// Print sample results
System.out.println(" Sample results:");
results.getWeb().stream()
.limit(3)
.forEach(result -> {
System.out.println(" - " + result.get("title"));
System.out.println(" " + result.get("url"));
});
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchComprehensive() {
System.out.println("\n=== Test: Search with All Options ===");
SearchData results = client.search("web scraping tools",
SearchOptions.builder()
.sources(List.of("web"))
.limit(5)
.tbs("qdr:y") // Past year
.location("US")
.timeout(30000)
.scrapeOptions(ScrapeOptions.builder()
.formats(List.of("markdown"))
.onlyMainContent(true)
.waitFor(1000)
.build())
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
assertTrue(results.getWeb().size() <= 5, "Should respect limit");
System.out.println("β Comprehensive search completed");
System.out.println(" Configuration:");
System.out.println(" - Sources: web");
System.out.println(" - Limit: 5");
System.out.println(" - Time filter: Past year");
System.out.println(" - Location: US");
System.out.println(" - Timeout: 30000ms");
System.out.println(" - Scrape: markdown, main content only");
System.out.println(" Results:");
System.out.println(" - Web results: " + results.getWeb().size());
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchContentVerification() {
System.out.println("\n=== Test: Search Content Verification ===");
SearchData results = client.search("Python programming language",
SearchOptions.builder()
.limit(5)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
assertFalse(results.getWeb().isEmpty(), "Should have results");
// Verify results are relevant to the query
boolean hasRelevantContent = results.getWeb().stream()
.anyMatch(result -> {
String text = String.format("%s %s %s",
result.get("url"),
result.get("title"),
result.get("description")
).toLowerCase();
return text.contains("python");
});
assertTrue(hasRelevantContent, "Results should be relevant to query");
System.out.println("β Content verification passed");
System.out.println(" Query: Python programming language");
System.out.println(" Relevant results found: β");
}
@Test
@EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
void testSearchIgnoreInvalidURLs() {
System.out.println("\n=== Test: Search with Ignore Invalid URLs ===");
SearchData results = client.search("technology news",
SearchOptions.builder()
.limit(5)
.ignoreInvalidURLs(true)
.build());
assertNotNull(results.getWeb(), "Web results should not be null");
// Verify all URLs are valid
boolean allValidUrls = results.getWeb().stream()
.allMatch(result -> {
String url = result.get("url").toString();
return url.startsWith("http://") || url.startsWith("https://");
});
assertTrue(allValidUrls, "All URLs should be valid HTTP(S)");
System.out.println("β Search with URL validation completed");
System.out.println(" Results: " + results.getWeb().size());
System.out.println(" All URLs valid: β");
}
}
|