|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { createServer } from 'node:http'; |
| 3 | +import test from 'node:test'; |
| 4 | +import { |
| 5 | + getFreePort, |
| 6 | + parseSseJson, |
| 7 | + spawnServer, |
| 8 | + stopChild, |
| 9 | + waitForHealth, |
| 10 | +} from './helpers/exchange-mcp.mjs'; |
| 11 | + |
| 12 | +const CRAWL_ID = 'crawl-1'; |
| 13 | + |
| 14 | +async function startCrawlApi() { |
| 15 | + const requests = []; |
| 16 | + let statusReads = 0; |
| 17 | + const server = createServer(async (req, res) => { |
| 18 | + for await (const _chunk of req); |
| 19 | + requests.push({ url: req.url, at: Date.now() }); |
| 20 | + const base = `http://127.0.0.1:${server.address().port}`; |
| 21 | + const json = (status, payload, headers = {}) => { |
| 22 | + res.writeHead(status, { 'content-type': 'application/json', ...headers }); |
| 23 | + res.end(JSON.stringify(payload)); |
| 24 | + }; |
| 25 | + if (req.method === 'POST' && req.url === '/v2/crawl') { |
| 26 | + return json(200, { success: true, id: CRAWL_ID }); |
| 27 | + } |
| 28 | + if (req.url === `/v2/crawl/${CRAWL_ID}?skip=1`) { |
| 29 | + return json(200, { success: true, data: [{ markdown: 'page two' }], next: null }); |
| 30 | + } |
| 31 | + if (req.url === `/v2/crawl/${CRAWL_ID}`) { |
| 32 | + statusReads += 1; |
| 33 | + if (statusReads === 2) { |
| 34 | + return json(429, { success: false, error: 'Rate limit exceeded.', retry_after_seconds: 1 }, { 'retry-after': '1' }); |
| 35 | + } |
| 36 | + const done = statusReads >= 3; |
| 37 | + return json(200, { |
| 38 | + success: true, |
| 39 | + status: done ? 'completed' : 'scraping', |
| 40 | + completed: done ? 2 : 1, |
| 41 | + total: 2, |
| 42 | + data: [{ markdown: 'page one' }], |
| 43 | + next: `${base}/v2/crawl/${CRAWL_ID}?skip=1`, |
| 44 | + }); |
| 45 | + } |
| 46 | + json(404, { success: false, error: `Unhandled ${req.method} ${req.url}` }); |
| 47 | + }); |
| 48 | + await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); |
| 49 | + return { |
| 50 | + requests, |
| 51 | + url: `http://127.0.0.1:${server.address().port}`, |
| 52 | + close: () => new Promise((resolve) => server.close(resolve)), |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +test('crawl waits out a rate-limited status read and pages results only once it ends', async (t) => { |
| 57 | + const api = await startCrawlApi(); |
| 58 | + t.after(() => api.close()); |
| 59 | + const port = await getFreePort(); |
| 60 | + const child = spawnServer({ |
| 61 | + CLOUD_SERVICE: 'true', |
| 62 | + HTTP_STREAMABLE_SERVER: 'true', |
| 63 | + FASTMCP_ENDPOINT: '/v2/mcp', |
| 64 | + FIRECRAWL_API_KEY: '', |
| 65 | + FIRECRAWL_OAUTH_TOKEN: '', |
| 66 | + FIRECRAWL_API_URL: api.url, |
| 67 | + FIRECRAWL_MCP_SEARCH_ENABLED: 'false', |
| 68 | + PORT: String(port), |
| 69 | + }); |
| 70 | + t.after(() => stopChild(child)); |
| 71 | + await waitForHealth(port, child); |
| 72 | + |
| 73 | + const response = await fetch(`http://127.0.0.1:${port}/v2/mcp`, { |
| 74 | + body: JSON.stringify({ |
| 75 | + id: 1, |
| 76 | + jsonrpc: '2.0', |
| 77 | + method: 'tools/call', |
| 78 | + params: { name: 'firecrawl_crawl', arguments: { url: 'https://example.com' } }, |
| 79 | + }), |
| 80 | + headers: { |
| 81 | + accept: 'application/json, text/event-stream', |
| 82 | + 'content-type': 'application/json', |
| 83 | + 'x-api-key': 'fc-hosted-test', |
| 84 | + }, |
| 85 | + method: 'POST', |
| 86 | + signal: AbortSignal.timeout(20_000), |
| 87 | + }); |
| 88 | + assert.equal(response.status, 200); |
| 89 | + const message = parseSseJson(await response.text()); |
| 90 | + assert.notEqual(message.result?.isError, true, JSON.stringify(message)); |
| 91 | + const result = JSON.parse(message.result.content[0].text); |
| 92 | + assert.equal(result.status, 'completed'); |
| 93 | + assert.deepEqual(result.data.map((doc) => doc.markdown), ['page one', 'page two']); |
| 94 | + const statusReads = api.requests.filter(({ url }) => url === `/v2/crawl/${CRAWL_ID}`); |
| 95 | + assert.equal(statusReads.length, 3); |
| 96 | + assert.ok(statusReads[2].at - statusReads[1].at >= 1000, 'the read after a 429 waits for Retry-After'); |
| 97 | + assert.equal(api.requests.filter(({ url }) => url.endsWith('?skip=1')).length, 1); |
| 98 | +}); |
0 commit comments