File size: 11,434 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
package com.firecrawl;

import com.firecrawl.client.FirecrawlClient;
import com.firecrawl.models.MapData;
import com.firecrawl.models.MapOptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;

import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

/**
 * Comprehensive Map Tests
 * 
 * Tests the map 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.MapTest"
 */
class MapTest {

    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 testMapMinimal() {
        System.out.println("\n=== Test: Map - Minimal Request ===");
        
        MapData data = client.map("https://docs.firecrawl.dev");

        assertNotNull(data, "Map data should not be null");
        assertNotNull(data.getLinks(), "Links should not be null");
        assertTrue(!data.getLinks().isEmpty(), "Should have at least one link");
        
        // Verify link structure (v2 links are MapDocument objects with url, title, description)
        Map<String, Object> firstLink = data.getLinks().get(0);
        assertNotNull(firstLink, "Link should not be null");
        assertNotNull(firstLink.get("url"), "Link should have url");
        assertTrue(firstLink.get("url").toString().startsWith("http"), "URL should start with http");
        
        System.out.println("βœ“ Map completed successfully");
        System.out.println("  Total links found: " + data.getLinks().size());
        System.out.println("  Sample URL: " + firstLink.get("url"));
        if (firstLink.get("title") != null) {
            System.out.println("  Title: " + firstLink.get("title"));
        }
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapWithLimit() {
        System.out.println("\n=== Test: Map with Limit ===");
        
        MapData data = client.map("https://docs.firecrawl.dev",
                MapOptions.builder()
                        .limit(10)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        assertTrue(data.getLinks().size() <= 10,
                "Should respect limit of 10: got " + data.getLinks().size());
        
        System.out.println("βœ“ Map with limit completed");
        System.out.println("  Requested limit: 10");
        System.out.println("  Actual links: " + data.getLinks().size());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapWithSearch() {
        System.out.println("\n=== Test: Map with Search Filter ===");
        
        MapData data = client.map("https://docs.firecrawl.dev",
                MapOptions.builder()
                        .search("api")
                        .limit(20)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        
        // Verify that filtered results contain the search term
        long matchingLinks = data.getLinks().stream()
                .filter(link -> {
                    String url = link.get("url") != null ? link.get("url").toString().toLowerCase() : "";
                    String title = link.get("title") != null ? link.get("title").toString().toLowerCase() : "";
                    return url.contains("api") || title.contains("api");
                })
                .count();
        
        System.out.println("βœ“ Map with search completed");
        System.out.println("  Total links: " + data.getLinks().size());
        System.out.println("  Links matching 'api': " + matchingLinks);
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapWithSkipSitemap() {
        System.out.println("\n=== Test: Map with Sitemap Skip ===");
        
        MapData data = client.map("https://firecrawl.dev",
                MapOptions.builder()
                        .sitemap("skip")
                        .limit(15)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        assertTrue(data.getLinks().size() <= 15, "Should respect limit");
        
        // Verify all links are valid HTTP(S) URLs
        boolean allValidUrls = data.getLinks().stream()
                .allMatch(link -> {
                    String url = link.get("url") != null ? link.get("url").toString() : "";
                    return url.startsWith("http://") || url.startsWith("https://");
                });
        
        assertTrue(allValidUrls, "All URLs should be valid HTTP(S)");
        
        System.out.println("βœ“ Map with sitemap=skip completed");
        System.out.println("  Links found: " + data.getLinks().size());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapWithSitemapOnly() {
        System.out.println("\n=== Test: Map with Sitemap Only ===");
        
        MapData data = client.map("https://firecrawl.dev",
                MapOptions.builder()
                        .sitemap("only")
                        .limit(50)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        // Note: sitemapOnly may not always respect the limit strictly
        
        // Verify all links are valid HTTP(S) URLs
        boolean allValidUrls = data.getLinks().stream()
                .allMatch(link -> {
                    String url = link.get("url") != null ? link.get("url").toString() : "";
                    return url.startsWith("http://") || url.startsWith("https://");
                });
        
        assertTrue(allValidUrls, "All URLs should be valid HTTP(S)");
        
        System.out.println("βœ“ Map with sitemap=only completed");
        System.out.println("  Links found: " + data.getLinks().size());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapWithIncludeSubdomains() {
        System.out.println("\n=== Test: Map with Include Subdomains ===");
        
        MapData data = client.map("https://firecrawl.dev",
                MapOptions.builder()
                        .includeSubdomains(true)
                        .limit(20)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        
        System.out.println("βœ“ Map with subdomains completed");
        System.out.println("  Total links: " + data.getLinks().size());
        
        // Check if any subdomains were found
        boolean hasSubdomains = data.getLinks().stream()
                .anyMatch(link -> {
                    String url = link.get("url") != null ? link.get("url").toString() : "";
                    return url.contains("docs.firecrawl.dev") || 
                           url.contains("api.firecrawl.dev") ||
                           (url.contains(".firecrawl.dev") && !url.contains("www.firecrawl.dev"));
                });
        
        if (hasSubdomains) {
            System.out.println("  βœ“ Found subdomain links");
        }
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapFirecrawlDocs() {
        System.out.println("\n=== Test: Map Firecrawl Documentation ===");
        
        MapData data = client.map("https://docs.firecrawl.dev",
                MapOptions.builder()
                        .limit(50)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        assertFalse(data.getLinks().isEmpty(), "Should find documentation links");
        
        System.out.println("βœ“ Mapped Firecrawl documentation");
        System.out.println("  Total links: " + data.getLinks().size());
        
        // Print sample links
        System.out.println("  Sample documentation pages:");
        data.getLinks().stream()
                .limit(5)
                .forEach(link -> System.out.println("    - " + link.get("url")));
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapLinkStructure() {
        System.out.println("\n=== Test: Verify Map Link Structure ===");
        
        MapData data = client.map("https://firecrawl.dev",
                MapOptions.builder()
                        .limit(5)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        assertFalse(data.getLinks().isEmpty(), "Should have links");
        
        // Verify each link is a valid URL with expected fields
        for (Map<String, Object> link : data.getLinks()) {
            assertNotNull(link, "Link should not be null");
            assertNotNull(link.get("url"), "Link should have url field");
            assertTrue(link.get("url").toString().startsWith("http"), "URL should be valid: " + link.get("url"));
        }
        
        System.out.println("βœ“ All links have correct structure");
        System.out.println("  Verified " + data.getLinks().size() + " links");
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapWithTimeout() {
        System.out.println("\n=== Test: Map with Timeout ===");
        
        MapData data = client.map("https://firecrawl.dev",
                MapOptions.builder()
                        .timeout(15000)  // 15 seconds
                        .limit(10)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        
        System.out.println("βœ“ Map with timeout completed");
        System.out.println("  Timeout: 15000ms");
        System.out.println("  Links found: " + data.getLinks().size());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testMapComprehensive() {
        System.out.println("\n=== Test: Map with All Options ===");
        
        MapData data = client.map("https://docs.firecrawl.dev",
                MapOptions.builder()
                        .includeSubdomains(false)
                        .limit(25)
                        .sitemap("include")
                        .timeout(20000)
                        .build());

        assertNotNull(data.getLinks(), "Links should not be null");
        assertTrue(data.getLinks().size() <= 25, "Should respect limit");
        
        System.out.println("βœ“ Comprehensive map completed");
        System.out.println("  Configuration:");
        System.out.println("    - Include subdomains: false");
        System.out.println("    - Limit: 25");
        System.out.println("    - Ignore sitemap: false");
        System.out.println("    - Timeout: 20000ms");
        System.out.println("  Results:");
        System.out.println("    - Links found: " + data.getLinks().size());
    }
}