test(brave): cover subscription token auth

This commit is contained in:
Peter Steinberger
2026-05-02 06:47:55 +01:00
parent 2d2f492102
commit dc72a2aa42

View File

@@ -33,6 +33,14 @@ function installBraveLlmContextFetch() {
return mockFetch;
}
function readHeader(init: unknown, name: string): string | null {
const headers = (init as { headers?: HeadersInit } | undefined)?.headers;
if (!headers) {
return null;
}
return new Headers(headers).get(name);
}
describe("brave web search provider", () => {
const priorFetch = global.fetch;
@@ -230,6 +238,59 @@ describe("brave web search provider", () => {
expect(requestUrl.searchParams.get("freshness")).toBe("pw");
});
it("sends Brave web auth in the X-Subscription-Token header", async () => {
vi.stubEnv("BRAVE_API_KEY", "");
const mockFetch = vi.fn(async (_input?: unknown, _init?: unknown) => {
return {
ok: true,
json: async () => ({ web: { results: [] } }),
} as Response;
});
global.fetch = mockFetch as typeof global.fetch;
const provider = createBraveWebSearchProvider();
const tool = provider.createTool({
config: {},
searchConfig: {
apiKey: "brave-test-key",
brave: { mode: "web" },
},
});
if (!tool) {
throw new Error("Expected tool definition");
}
await tool.execute({ query: "latest ai news" });
const requestUrl = new URL(String(mockFetch.mock.calls[0]?.[0]));
expect(requestUrl.searchParams.get("apikey")).toBeNull();
expect(requestUrl.searchParams.get("key")).toBeNull();
expect(readHeader(mockFetch.mock.calls[0]?.[1], "X-Subscription-Token")).toBe("brave-test-key");
});
it("sends Brave llm-context auth in the X-Subscription-Token header", async () => {
vi.stubEnv("BRAVE_API_KEY", "");
const mockFetch = installBraveLlmContextFetch();
const provider = createBraveWebSearchProvider();
const tool = provider.createTool({
config: {},
searchConfig: {
apiKey: "brave-test-key",
brave: { mode: "llm-context" },
},
});
if (!tool) {
throw new Error("Expected tool definition");
}
await tool.execute({ query: "latest ai news" });
const requestUrl = new URL(String(mockFetch.mock.calls[0]?.[0]));
expect(requestUrl.searchParams.get("apikey")).toBeNull();
expect(requestUrl.searchParams.get("key")).toBeNull();
expect(readHeader(mockFetch.mock.calls[0]?.[1], "X-Subscription-Token")).toBe("brave-test-key");
});
it("passes bounded date ranges to Brave llm-context endpoint", async () => {
vi.stubEnv("BRAVE_API_KEY", "test-key");
const mockFetch = installBraveLlmContextFetch();