test: consolidate redundant suites and speed up timers

This commit is contained in:
Peter Steinberger
2026-02-23 04:44:42 +00:00
parent a6a2a9276e
commit 86a8b65e9d
10 changed files with 171 additions and 224 deletions

View File

@@ -1,61 +0,0 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { withFetchPreconnect } from "../../test-utils/fetch-mock.js";
vi.mock("../../infra/net/fetch-guard.js", () => {
return {
fetchWithSsrFGuard: vi.fn(async () => {
throw new Error("network down");
}),
};
});
describe("web_fetch firecrawl apiKey normalization", () => {
const priorFetch = global.fetch;
afterEach(() => {
global.fetch = priorFetch;
vi.restoreAllMocks();
});
it("strips embedded CR/LF before sending Authorization header", async () => {
const fetchSpy = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : "";
expect(url).toContain("/v2/scrape");
const auth = (init?.headers as Record<string, string> | undefined)?.Authorization;
expect(auth).toBe("Bearer firecrawl-test-key");
return new Response(
JSON.stringify({
success: true,
data: { markdown: "ok", metadata: { title: "t" } },
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
);
});
global.fetch = withFetchPreconnect(fetchSpy);
const { createWebFetchTool } = await import("./web-tools.js");
const tool = createWebFetchTool({
config: {
tools: {
web: {
fetch: {
cacheTtlMinutes: 0,
firecrawl: { apiKey: "firecrawl-test-\r\nkey" },
readability: false,
},
},
},
},
});
const result = await tool?.execute?.("call", {
url: "https://example.com",
extractMode: "text",
});
expect(result?.details).toMatchObject({ extractor: "firecrawl" });
expect(fetchSpy).toHaveBeenCalled();
});
});

View File

@@ -91,8 +91,12 @@ function requestUrl(input: RequestInfo | URL): string {
return "";
}
function installMockFetch(impl: (input: RequestInfo | URL) => Promise<Response>) {
const mockFetch = vi.fn(async (input: RequestInfo | URL) => await impl(input));
function installMockFetch(
impl: (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>,
) {
const mockFetch = vi.fn(
async (input: RequestInfo | URL, init?: RequestInit) => await impl(input, init),
);
global.fetch = withFetchPreconnect(mockFetch);
return mockFetch;
}
@@ -253,6 +257,36 @@ describe("web_fetch extraction fallbacks", () => {
expect(details.text).toContain("firecrawl content");
});
it("normalizes firecrawl Authorization header values", async () => {
const fetchSpy = installMockFetch((input: RequestInfo | URL) => {
const url = requestUrl(input);
if (url.includes("api.firecrawl.dev/v2/scrape")) {
return Promise.resolve(firecrawlResponse("firecrawl normalized")) as Promise<Response>;
}
return Promise.resolve(
htmlResponse("<!doctype html><html><head></head><body></body></html>", url),
) as Promise<Response>;
});
const tool = createFetchTool({
firecrawl: { apiKey: "firecrawl-test-\r\nkey" },
});
const result = await tool?.execute?.("call", {
url: "https://example.com/firecrawl",
extractMode: "text",
});
expect(result?.details).toMatchObject({ extractor: "firecrawl" });
const firecrawlCall = fetchSpy.mock.calls.find((call) =>
requestUrl(call[0]).includes("/v2/scrape"),
);
expect(firecrawlCall).toBeTruthy();
const init = firecrawlCall?.[1];
const authHeader = new Headers(init?.headers).get("Authorization");
expect(authHeader).toBe("Bearer firecrawl-test-key");
});
it("throws when readability is disabled and firecrawl is unavailable", async () => {
installMockFetch(
(input: RequestInfo | URL) =>