mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 08:17:40 +00:00
test: migrate suites to e2e coverage layout
This commit is contained in:
226
src/agents/tools/web-search.e2e.test.ts
Normal file
226
src/agents/tools/web-search.e2e.test.ts
Normal file
@@ -0,0 +1,226 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { __testing } from "./web-search.js";
|
||||
|
||||
function withEnv<T>(env: Record<string, string | undefined>, fn: () => T): T {
|
||||
const prev: Record<string, string | undefined> = {};
|
||||
for (const [key, value] of Object.entries(env)) {
|
||||
prev[key] = process.env[key];
|
||||
if (value === undefined) {
|
||||
// Make tests hermetic even on machines with real keys set.
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
try {
|
||||
return fn();
|
||||
} finally {
|
||||
for (const [key, value] of Object.entries(prev)) {
|
||||
if (value === undefined) {
|
||||
delete process.env[key];
|
||||
} else {
|
||||
process.env[key] = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const {
|
||||
inferPerplexityBaseUrlFromApiKey,
|
||||
resolvePerplexityBaseUrl,
|
||||
isDirectPerplexityBaseUrl,
|
||||
resolvePerplexityRequestModel,
|
||||
normalizeFreshness,
|
||||
resolveGrokApiKey,
|
||||
resolveGrokModel,
|
||||
resolveGrokInlineCitations,
|
||||
extractGrokContent,
|
||||
} = __testing;
|
||||
|
||||
describe("web_search perplexity baseUrl defaults", () => {
|
||||
it("detects a Perplexity key prefix", () => {
|
||||
expect(inferPerplexityBaseUrlFromApiKey("pplx-123")).toBe("direct");
|
||||
});
|
||||
|
||||
it("detects an OpenRouter key prefix", () => {
|
||||
expect(inferPerplexityBaseUrlFromApiKey("sk-or-v1-123")).toBe("openrouter");
|
||||
});
|
||||
|
||||
it("returns undefined for unknown key formats", () => {
|
||||
expect(inferPerplexityBaseUrlFromApiKey("unknown-key")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("prefers explicit baseUrl over key-based defaults", () => {
|
||||
expect(resolvePerplexityBaseUrl({ baseUrl: "https://example.com" }, "config", "pplx-123")).toBe(
|
||||
"https://example.com",
|
||||
);
|
||||
});
|
||||
|
||||
it("defaults to direct when using PERPLEXITY_API_KEY", () => {
|
||||
expect(resolvePerplexityBaseUrl(undefined, "perplexity_env")).toBe("https://api.perplexity.ai");
|
||||
});
|
||||
|
||||
it("defaults to OpenRouter when using OPENROUTER_API_KEY", () => {
|
||||
expect(resolvePerplexityBaseUrl(undefined, "openrouter_env")).toBe(
|
||||
"https://openrouter.ai/api/v1",
|
||||
);
|
||||
});
|
||||
|
||||
it("defaults to direct when config key looks like Perplexity", () => {
|
||||
expect(resolvePerplexityBaseUrl(undefined, "config", "pplx-123")).toBe(
|
||||
"https://api.perplexity.ai",
|
||||
);
|
||||
});
|
||||
|
||||
it("defaults to OpenRouter when config key looks like OpenRouter", () => {
|
||||
expect(resolvePerplexityBaseUrl(undefined, "config", "sk-or-v1-123")).toBe(
|
||||
"https://openrouter.ai/api/v1",
|
||||
);
|
||||
});
|
||||
|
||||
it("defaults to OpenRouter for unknown config key formats", () => {
|
||||
expect(resolvePerplexityBaseUrl(undefined, "config", "weird-key")).toBe(
|
||||
"https://openrouter.ai/api/v1",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("web_search perplexity model normalization", () => {
|
||||
it("detects direct Perplexity host", () => {
|
||||
expect(isDirectPerplexityBaseUrl("https://api.perplexity.ai")).toBe(true);
|
||||
expect(isDirectPerplexityBaseUrl("https://api.perplexity.ai/")).toBe(true);
|
||||
expect(isDirectPerplexityBaseUrl("https://openrouter.ai/api/v1")).toBe(false);
|
||||
});
|
||||
|
||||
it("strips provider prefix for direct Perplexity", () => {
|
||||
expect(resolvePerplexityRequestModel("https://api.perplexity.ai", "perplexity/sonar-pro")).toBe(
|
||||
"sonar-pro",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps prefixed model for OpenRouter", () => {
|
||||
expect(
|
||||
resolvePerplexityRequestModel("https://openrouter.ai/api/v1", "perplexity/sonar-pro"),
|
||||
).toBe("perplexity/sonar-pro");
|
||||
});
|
||||
|
||||
it("keeps model unchanged when URL is invalid", () => {
|
||||
expect(resolvePerplexityRequestModel("not-a-url", "perplexity/sonar-pro")).toBe(
|
||||
"perplexity/sonar-pro",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("web_search freshness normalization", () => {
|
||||
it("accepts Brave shortcut values", () => {
|
||||
expect(normalizeFreshness("pd")).toBe("pd");
|
||||
expect(normalizeFreshness("PW")).toBe("pw");
|
||||
});
|
||||
|
||||
it("accepts valid date ranges", () => {
|
||||
expect(normalizeFreshness("2024-01-01to2024-01-31")).toBe("2024-01-01to2024-01-31");
|
||||
});
|
||||
|
||||
it("rejects invalid date ranges", () => {
|
||||
expect(normalizeFreshness("2024-13-01to2024-01-31")).toBeUndefined();
|
||||
expect(normalizeFreshness("2024-02-30to2024-03-01")).toBeUndefined();
|
||||
expect(normalizeFreshness("2024-03-10to2024-03-01")).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("web_search grok config resolution", () => {
|
||||
it("uses config apiKey when provided", () => {
|
||||
expect(resolveGrokApiKey({ apiKey: "xai-test-key" })).toBe("xai-test-key");
|
||||
});
|
||||
|
||||
it("returns undefined when no apiKey is available", () => {
|
||||
withEnv({ XAI_API_KEY: undefined }, () => {
|
||||
expect(resolveGrokApiKey({})).toBeUndefined();
|
||||
expect(resolveGrokApiKey(undefined)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
it("uses default model when not specified", () => {
|
||||
expect(resolveGrokModel({})).toBe("grok-4-1-fast");
|
||||
expect(resolveGrokModel(undefined)).toBe("grok-4-1-fast");
|
||||
});
|
||||
|
||||
it("uses config model when provided", () => {
|
||||
expect(resolveGrokModel({ model: "grok-3" })).toBe("grok-3");
|
||||
});
|
||||
|
||||
it("defaults inlineCitations to false", () => {
|
||||
expect(resolveGrokInlineCitations({})).toBe(false);
|
||||
expect(resolveGrokInlineCitations(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it("respects inlineCitations config", () => {
|
||||
expect(resolveGrokInlineCitations({ inlineCitations: true })).toBe(true);
|
||||
expect(resolveGrokInlineCitations({ inlineCitations: false })).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("web_search grok response parsing", () => {
|
||||
it("extracts content from Responses API message blocks", () => {
|
||||
const result = extractGrokContent({
|
||||
output: [
|
||||
{
|
||||
type: "message",
|
||||
content: [{ type: "output_text", text: "hello from output" }],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.text).toBe("hello from output");
|
||||
expect(result.annotationCitations).toEqual([]);
|
||||
});
|
||||
|
||||
it("extracts url_citation annotations from content blocks", () => {
|
||||
const result = extractGrokContent({
|
||||
output: [
|
||||
{
|
||||
type: "message",
|
||||
content: [
|
||||
{
|
||||
type: "output_text",
|
||||
text: "hello with citations",
|
||||
annotations: [
|
||||
{
|
||||
type: "url_citation",
|
||||
url: "https://example.com/a",
|
||||
start_index: 0,
|
||||
end_index: 5,
|
||||
},
|
||||
{
|
||||
type: "url_citation",
|
||||
url: "https://example.com/b",
|
||||
start_index: 6,
|
||||
end_index: 10,
|
||||
},
|
||||
{
|
||||
type: "url_citation",
|
||||
url: "https://example.com/a",
|
||||
start_index: 11,
|
||||
end_index: 15,
|
||||
}, // duplicate
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(result.text).toBe("hello with citations");
|
||||
expect(result.annotationCitations).toEqual(["https://example.com/a", "https://example.com/b"]);
|
||||
});
|
||||
|
||||
it("falls back to deprecated output_text", () => {
|
||||
const result = extractGrokContent({ output_text: "hello from output_text" });
|
||||
expect(result.text).toBe("hello from output_text");
|
||||
expect(result.annotationCitations).toEqual([]);
|
||||
});
|
||||
|
||||
it("returns undefined text when no content found", () => {
|
||||
const result = extractGrokContent({});
|
||||
expect(result.text).toBeUndefined();
|
||||
expect(result.annotationCitations).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user