|
1 | 1 | import { isPlainRecord } from '@sim/utils/object' |
2 | 2 | import { describe, expect, it } from 'vitest' |
3 | 3 | import { crawlTool } from '@/tools/firecrawl/crawl' |
4 | | -import type { FirecrawlCrawlParams } from '@/tools/firecrawl/types' |
5 | 4 |
|
6 | 5 | function crawlBody(options: { |
7 | | - url?: string |
8 | 6 | maxDepth: number |
| 7 | + url?: string |
9 | 8 | maxDiscoveryDepth?: number |
10 | 9 | includePaths?: string[] |
11 | 10 | excludePaths?: string[] |
@@ -68,40 +67,35 @@ describe('Firecrawl legacy crawl depth', () => { |
68 | 67 | expect(patterns.some((pattern) => pattern.test(path))).toBe(excluded) |
69 | 68 | }) |
70 | 69 |
|
71 | | - it.each([-1, 1.5, Number.NaN, Number.POSITIVE_INFINITY])( |
72 | | - 'rejects an invalid legacy depth: %s', |
73 | | - (maxDepth) => { |
74 | | - expect(() => crawlBody({ maxDepth })).toThrow(/maxDepth/) |
75 | | - } |
76 | | - ) |
77 | | - |
78 | | - it('measures depth below the start URL, as Firecrawl v1 did', () => { |
79 | | - const body = crawlBody({ url: 'https://example.com/guide/intro', maxDepth: 1 }) |
| 70 | + it.each<[string, string, boolean]>([ |
| 71 | + ['https://example.com/docs/guide', '/docs/guide', true], |
| 72 | + ['https://example.com/docs/guide/', '/docs/guide/setup', true], |
| 73 | + ['https://example.com/docs/guide/index.html', '/docs/guide/setup', true], |
| 74 | + ['https://example.com/docs/guide', '/docs/guide/setup/advanced', false], |
| 75 | + ['docs.example.com/guide', '/guide/setup', true], |
| 76 | + ])('counts depth from the start URL %s for %s', (url, path, accepted) => { |
| 77 | + const body = crawlBody({ maxDepth: 1, url }) |
80 | 78 | const patterns = (body.includePaths as string[]).map((pattern) => new RegExp(pattern)) |
81 | | - const accepts = (path: string) => patterns.some((pattern) => pattern.test(path)) |
82 | | - |
83 | | - expect(accepts('/guide/intro')).toBe(true) |
84 | | - expect(accepts('/guide/intro/setup')).toBe(true) |
85 | | - expect(accepts('/guide/intro/setup/advanced')).toBe(false) |
| 79 | + expect(patterns.some((pattern) => pattern.test(path))).toBe(accepted) |
86 | 80 | }) |
87 | 81 |
|
88 | | - it('ignores an index file in the start URL when measuring its depth', () => { |
89 | | - const body = crawlBody({ url: 'https://example.com/docs/index.html', maxDepth: 0 }) |
90 | | - const patterns = (body.includePaths as string[]).map((pattern) => new RegExp(pattern)) |
91 | | - expect(patterns.some((pattern) => pattern.test('/docs'))).toBe(true) |
92 | | - expect(patterns.some((pattern) => pattern.test('/docs/guide'))).toBe(false) |
| 82 | + it('counts depth from the start URL alongside existing path filters', () => { |
| 83 | + const body = crawlBody({ |
| 84 | + maxDepth: 1, |
| 85 | + url: 'https://example.com/docs/guide', |
| 86 | + includePaths: ['^/docs'], |
| 87 | + }) |
| 88 | + const patterns = (body.excludePaths as string[]).map((pattern) => new RegExp(pattern)) |
| 89 | + const excludes = (path: string) => patterns.some((pattern) => pattern.test(path)) |
| 90 | + expect(excludes('/docs/guide')).toBe(false) |
| 91 | + expect(excludes('/docs/guide/setup')).toBe(false) |
| 92 | + expect(excludes('/docs/guide/setup/advanced')).toBe(true) |
93 | 93 | }) |
94 | 94 |
|
95 | | - it('treats a blank depth as unset instead of a zero-depth crawl', () => { |
96 | | - if (typeof crawlTool.request.body !== 'function') throw new Error('Missing request formatter') |
97 | | - const blankDepth: Record<string, unknown> = { |
98 | | - apiKey: 'test', |
99 | | - url: 'https://example.com', |
100 | | - maxDepth: '', |
| 95 | + it.each([-1, 1.5, Number.NaN, Number.POSITIVE_INFINITY])( |
| 96 | + 'rejects an invalid legacy depth: %s', |
| 97 | + (maxDepth) => { |
| 98 | + expect(() => crawlBody({ maxDepth })).toThrow(/maxDepth/) |
101 | 99 | } |
102 | | - const body = crawlTool.request.body(blankDepth as FirecrawlCrawlParams) |
103 | | - if (!isPlainRecord(body)) throw new Error('Expected a JSON request body') |
104 | | - expect(body.includePaths).toBeUndefined() |
105 | | - expect(body.regexOnFullURL).toBeUndefined() |
106 | | - }) |
| 100 | + ) |
107 | 101 | }) |
0 commit comments