|
| 1 | +import { describe, test, expect, jest } from "@jest/globals"; |
| 2 | +import { scrape } from "../../../v2/methods/scrape"; |
| 3 | +import { startBatchScrape } from "../../../v2/methods/batch"; |
| 4 | +import { startCrawl } from "../../../v2/methods/crawl"; |
| 5 | +import { search } from "../../../v2/methods/search"; |
| 6 | +import { map } from "../../../v2/methods/map"; |
| 7 | +import { startExtract } from "../../../v2/methods/extract"; |
| 8 | +import { startAgent } from "../../../v2/methods/agent"; |
| 9 | +import type { ThreatProtectionOptions } from "../../../v2/types"; |
| 10 | + |
| 11 | +const threatProtection: ThreatProtectionOptions = { |
| 12 | + mode: "normal", |
| 13 | + riskScoreThreshold: 80, |
| 14 | + blacklist: ["*.blocked.example.com"], |
| 15 | + whitelist: ["allowed.example.com"], |
| 16 | + blockedTlds: ["zip"], |
| 17 | + failurePolicy: "open", |
| 18 | +}; |
| 19 | + |
| 20 | +function makeHttp(data: Record<string, unknown>) { |
| 21 | + const post = jest.fn(async () => ({ status: 200, data })); |
| 22 | + return { |
| 23 | + post, |
| 24 | + prepareHeaders: jest.fn(() => undefined), |
| 25 | + } as any; |
| 26 | +} |
| 27 | + |
| 28 | +describe("v2 threatProtection request serialization", () => { |
| 29 | + test("scrape sends threatProtection at top level", async () => { |
| 30 | + const http = makeHttp({ success: true, data: {} }); |
| 31 | + await scrape(http, "https://example.com", { threatProtection }); |
| 32 | + expect(http.post).toHaveBeenCalledWith( |
| 33 | + "/v2/scrape", |
| 34 | + expect.objectContaining({ url: "https://example.com", threatProtection }), |
| 35 | + {}, |
| 36 | + ); |
| 37 | + }); |
| 38 | + |
| 39 | + test("batch scrape sends threatProtection at top level", async () => { |
| 40 | + const http = makeHttp({ success: true, id: "job", url: "u" }); |
| 41 | + await startBatchScrape(http, ["https://example.com"], { |
| 42 | + options: { threatProtection }, |
| 43 | + }); |
| 44 | + expect(http.post).toHaveBeenCalledWith( |
| 45 | + "/v2/batch/scrape", |
| 46 | + expect.objectContaining({ |
| 47 | + urls: ["https://example.com"], |
| 48 | + threatProtection, |
| 49 | + }), |
| 50 | + expect.anything(), |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + test("crawl sends threatProtection under scrapeOptions", async () => { |
| 55 | + const http = makeHttp({ success: true, id: "job", url: "u" }); |
| 56 | + await startCrawl(http, { |
| 57 | + url: "https://example.com", |
| 58 | + scrapeOptions: { threatProtection }, |
| 59 | + }); |
| 60 | + expect(http.post).toHaveBeenCalledWith( |
| 61 | + "/v2/crawl", |
| 62 | + expect.objectContaining({ |
| 63 | + scrapeOptions: expect.objectContaining({ threatProtection }), |
| 64 | + }), |
| 65 | + ); |
| 66 | + }); |
| 67 | + |
| 68 | + test("search sends threatProtection at top level and under scrapeOptions", async () => { |
| 69 | + const http = makeHttp({ success: true, data: {} }); |
| 70 | + await search(http, { |
| 71 | + query: "firecrawl", |
| 72 | + threatProtection, |
| 73 | + scrapeOptions: { threatProtection }, |
| 74 | + }); |
| 75 | + expect(http.post).toHaveBeenCalledWith( |
| 76 | + "/v2/search", |
| 77 | + expect.objectContaining({ |
| 78 | + threatProtection, |
| 79 | + scrapeOptions: expect.objectContaining({ threatProtection }), |
| 80 | + }), |
| 81 | + {}, |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + test("map sends threatProtection at top level", async () => { |
| 86 | + const http = makeHttp({ success: true, links: [] }); |
| 87 | + await map(http, "https://example.com", { threatProtection }); |
| 88 | + expect(http.post).toHaveBeenCalledWith( |
| 89 | + "/v2/map", |
| 90 | + expect.objectContaining({ threatProtection }), |
| 91 | + {}, |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + test("extract sends threatProtection at top level", async () => { |
| 96 | + const http = makeHttp({ success: true, id: "job" }); |
| 97 | + await startExtract(http, { |
| 98 | + urls: ["https://example.com"], |
| 99 | + prompt: "extract", |
| 100 | + threatProtection, |
| 101 | + }); |
| 102 | + expect(http.post).toHaveBeenCalledWith( |
| 103 | + "/v2/extract", |
| 104 | + expect.objectContaining({ threatProtection }), |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + test("agent sends threatProtection at top level", async () => { |
| 109 | + const http = makeHttp({ success: true, id: "job", status: "processing" }); |
| 110 | + await startAgent(http, { prompt: "find pricing", threatProtection }); |
| 111 | + expect(http.post).toHaveBeenCalledWith( |
| 112 | + "/v2/agent", |
| 113 | + expect.objectContaining({ threatProtection }), |
| 114 | + ); |
| 115 | + }); |
| 116 | + |
| 117 | + test("threatProtection is omitted when not provided", async () => { |
| 118 | + const http = makeHttp({ success: true, data: {} }); |
| 119 | + await scrape(http, "https://example.com", { onlyMainContent: true }); |
| 120 | + const body = http.post.mock.calls[0][1] as Record<string, unknown>; |
| 121 | + expect(body).not.toHaveProperty("threatProtection"); |
| 122 | + }); |
| 123 | +}); |
0 commit comments