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

import com.firecrawl.client.FirecrawlClient;
import com.firecrawl.models.AgentOptions;
import com.firecrawl.models.AgentResponse;
import com.firecrawl.models.AgentStatusResponse;
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 Agent Tests
 * 
 * Tests the AI agent 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.AgentTest"
 */
class AgentTest {

    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 testAgentWithPrompt() {
        System.out.println("\n=== Test: Agent with Prompt ===");
        
        AgentStatusResponse result = client.agent(
                AgentOptions.builder()
                        .prompt("Find information about Firecrawl's main features and pricing")
                        .build());

        assertNotNull(result, "Agent result should not be null");
        assertNotNull(result.getStatus(), "Status should not be null");
        assertTrue(List.of("completed", "failed").contains(result.getStatus()),
                "Status should be completed or failed: " + result.getStatus());
        
        System.out.println("βœ“ Agent task completed");
        System.out.println("  Status: " + result.getStatus());
        if (result.getData() != null) {
            System.out.println("  Data returned: βœ“");
        }
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testAgentWithURLs() {
        System.out.println("\n=== Test: Agent with Specific URLs ===");
        
        AgentStatusResponse result = client.agent(
                AgentOptions.builder()
                        .urls(List.of("https://firecrawl.dev", "https://docs.firecrawl.dev"))
                        .prompt("What are the main features of Firecrawl?")
                        .build());

        assertNotNull(result, "Agent result should not be null");
        assertTrue(List.of("completed", "failed").contains(result.getStatus()),
                "Status should be completed or failed");
        
        System.out.println("βœ“ Agent with URLs completed");
        System.out.println("  URLs provided: 2");
        System.out.println("  Status: " + result.getStatus());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testAgentWithSchema() {
        System.out.println("\n=== Test: Agent with Schema ===");
        
        Map<String, Object> schema = Map.of(
                "type", "object",
                "properties", Map.of(
                        "features", Map.of(
                                "type", "array",
                                "items", Map.of("type", "string")
                        ),
                        "pricing", Map.of(
                                "type", "object",
                                "properties", Map.of(
                                        "plans", Map.of("type", "array")
                                )
                        )
                ),
                "required", List.of("features")
        );

        AgentStatusResponse result = client.agent(
                AgentOptions.builder()
                        .urls(List.of("https://firecrawl.dev"))
                        .prompt("Extract features and pricing information")
                        .schema(schema)
                        .build());

        assertNotNull(result, "Agent result should not be null");
        assertTrue(List.of("completed", "failed").contains(result.getStatus()),
                "Status should be completed or failed");
        
        System.out.println("βœ“ Agent with schema completed");
        System.out.println("  Schema provided: βœ“");
        System.out.println("  Status: " + result.getStatus());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testStartAgent() {
        System.out.println("\n=== Test: Start Agent (Async) ===");
        
        AgentResponse response = client.startAgent(
                AgentOptions.builder()
                        .prompt("Research Firecrawl features")
                        .build());

        assertNotNull(response, "Agent response should not be null");
        assertNotNull(response.getId(), "Agent ID should not be null");
        assertTrue(response.isSuccess(), "Response should be successful");
        
        System.out.println("βœ“ Agent started successfully");
        System.out.println("  Job ID: " + response.getId());
        System.out.println("  Success: " + response.isSuccess());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testAgentStatusCheck() {
        System.out.println("\n=== Test: Check Agent Status ===");
        
        // Start an agent
        AgentResponse start = client.startAgent(
                AgentOptions.builder()
                        .prompt("Find information about web scraping")
                        .build());

        // Check status
        AgentStatusResponse status = client.getAgentStatus(start.getId());
        
        assertNotNull(status, "Status should not be null");
        assertNotNull(status.getStatus(), "Status field should not be null");
        assertTrue(List.of("scraping", "completed", "failed", "cancelled").contains(status.getStatus()),
                "Status should be valid: " + status.getStatus());
        
        System.out.println("βœ“ Agent status retrieved");
        System.out.println("  Status: " + status.getStatus());
        System.out.println("  Job ID: " + start.getId());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testCancelAgent() {
        System.out.println("\n=== Test: Cancel Agent ===");
        
        AgentResponse start = client.startAgent(
                AgentOptions.builder()
                        .prompt("Long-running research task")
                        .build());

        Map<String, Object> result = client.cancelAgent(start.getId());
        
        assertNotNull(result, "Cancel result should not be null");
        
        System.out.println("βœ“ Agent cancelled successfully");
        System.out.println("  Job ID: " + start.getId());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testAgentWithStrictURLConstraints() {
        System.out.println("\n=== Test: Agent with Strict URL Constraints ===");
        
        AgentStatusResponse result = client.agent(
                AgentOptions.builder()
                        .urls(List.of("https://docs.firecrawl.dev"))
                        .prompt("Extract API documentation structure")
                        .strictConstrainToURLs(true)
                        .build());

        assertNotNull(result, "Agent result should not be null");
        assertTrue(List.of("completed", "failed").contains(result.getStatus()),
                "Status should be completed or failed");
        
        System.out.println("βœ“ Agent with strict constraints completed");
        System.out.println("  Strict URL constraint: true");
        System.out.println("  Status: " + result.getStatus());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testAgentWithMaxCredits() {
        System.out.println("\n=== Test: Agent with Max Credits Limit ===");
        
        AgentStatusResponse result = client.agent(
                AgentOptions.builder()
                        .prompt("Quick research on Firecrawl")
                        .maxCredits(10)
                        .build());

        assertNotNull(result, "Agent result should not be null");
        
        System.out.println("βœ“ Agent with credit limit completed");
        System.out.println("  Max credits: 10");
        System.out.println("  Status: " + result.getStatus());
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testAgentResearchTask() {
        System.out.println("\n=== Test: Agent Research - Firecrawl Features ===");
        
        AgentStatusResponse result = client.agent(
                AgentOptions.builder()
                        .urls(List.of("https://firecrawl.dev", "https://docs.firecrawl.dev"))
                        .prompt("Research and summarize the key features of Firecrawl, including scraping, crawling, and extraction capabilities")
                        .build());

        assertNotNull(result, "Agent result should not be null");
        assertEquals("completed", result.getStatus(), "Agent should complete successfully");
        assertNotNull(result.getData(), "Agent should return data");
        
        System.out.println("βœ“ Research task completed");
        System.out.println("  Status: " + result.getStatus());
        System.out.println("  Data collected: βœ“");
        
        if (result.getData() != null) {
            System.out.println("  Data summary: " + 
                    result.getData().toString().substring(0, 
                            Math.min(200, result.getData().toString().length())) + "...");
        }
    }

    @Test
    @EnabledIfEnvironmentVariable(named = "FIRECRAWL_API_KEY", matches = ".*\\S.*")
    void testAgentComprehensive() {
        System.out.println("\n=== Test: Agent with All Options ===");
        
        Map<String, Object> schema = Map.of(
                "type", "object",
                "properties", Map.of(
                        "product_name", Map.of("type", "string"),
                        "features", Map.of(
                                "type", "array",
                                "items", Map.of("type", "string")
                        ),
                        "pricing", Map.of("type", "string")
                ),
                "required", List.of("product_name", "features")
        );

        AgentStatusResponse result = client.agent(
                AgentOptions.builder()
                        .urls(List.of("https://firecrawl.dev"))
                        .prompt("Extract comprehensive product information including name, features, and pricing")
                        .schema(schema)
                        .maxCredits(20)
                        .strictConstrainToURLs(true)
                        .build());

        assertNotNull(result, "Agent result should not be null");
        assertTrue(List.of("completed", "failed").contains(result.getStatus()),
                "Status should be completed or failed");
        
        System.out.println("βœ“ Comprehensive agent task completed");
        System.out.println("  Configuration:");
        System.out.println("    - URLs: 1");
        System.out.println("    - Schema: βœ“");
        System.out.println("    - Max credits: 20");
        System.out.println("    - Strict constraints: true");
        System.out.println("  Results:");
        System.out.println("    - Status: " + result.getStatus());
        if (result.getData() != null) {
            System.out.println("    - Data returned: βœ“");
        }
    }
}