test(tavily): cover config and search helpers

This commit is contained in:
Vincent Koc
2026-03-22 17:42:08 -07:00
parent dcef96e6d4
commit e1c0e94d0c
4 changed files with 214 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
DEFAULT_TAVILY_BASE_URL,
DEFAULT_TAVILY_EXTRACT_TIMEOUT_SECONDS,
DEFAULT_TAVILY_SEARCH_TIMEOUT_SECONDS,
resolveTavilyApiKey,
resolveTavilyBaseUrl,
resolveTavilyExtractTimeoutSeconds,
resolveTavilySearchConfig,
resolveTavilySearchTimeoutSeconds,
} from "./config.js";
describe("tavily config helpers", () => {
afterEach(() => {
vi.unstubAllEnvs();
});
it("reads plugin web search config and prefers it over env defaults", () => {
vi.stubEnv("TAVILY_API_KEY", "env-key");
vi.stubEnv("TAVILY_BASE_URL", "https://env.tavily.test");
const cfg = {
plugins: {
entries: {
tavily: {
config: {
webSearch: {
apiKey: "plugin-key",
baseUrl: "https://plugin.tavily.test",
},
},
},
},
},
} as OpenClawConfig;
expect(resolveTavilySearchConfig(cfg)).toEqual({
apiKey: "plugin-key",
baseUrl: "https://plugin.tavily.test",
});
expect(resolveTavilyApiKey(cfg)).toBe("plugin-key");
expect(resolveTavilyBaseUrl(cfg)).toBe("https://plugin.tavily.test");
});
it("falls back to environment values and defaults", () => {
vi.stubEnv("TAVILY_API_KEY", "env-key");
vi.stubEnv("TAVILY_BASE_URL", "https://env.tavily.test");
expect(resolveTavilyApiKey()).toBe("env-key");
expect(resolveTavilyBaseUrl()).toBe("https://env.tavily.test");
expect(resolveTavilyBaseUrl({} as OpenClawConfig)).not.toBe(DEFAULT_TAVILY_BASE_URL);
expect(resolveTavilySearchTimeoutSeconds()).toBe(DEFAULT_TAVILY_SEARCH_TIMEOUT_SECONDS);
expect(resolveTavilyExtractTimeoutSeconds()).toBe(DEFAULT_TAVILY_EXTRACT_TIMEOUT_SECONDS);
});
it("accepts positive numeric timeout overrides and floors them", () => {
expect(resolveTavilySearchTimeoutSeconds(19.9)).toBe(19);
expect(resolveTavilyExtractTimeoutSeconds(42.7)).toBe(42);
expect(resolveTavilySearchTimeoutSeconds(0)).toBe(DEFAULT_TAVILY_SEARCH_TIMEOUT_SECONDS);
expect(resolveTavilyExtractTimeoutSeconds(Number.NaN)).toBe(
DEFAULT_TAVILY_EXTRACT_TIMEOUT_SECONDS,
);
});
});

View File

@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { __testing } from "./tavily-client.js";
describe("tavily client helpers", () => {
it("appends endpoints to reverse-proxy base urls", () => {
expect(__testing.resolveEndpoint("https://proxy.example/api/tavily", "/search")).toBe(
"https://proxy.example/api/tavily/search",
);
expect(__testing.resolveEndpoint("https://proxy.example/api/tavily/", "/extract")).toBe(
"https://proxy.example/api/tavily/extract",
);
});
it("falls back to the default host for invalid base urls", () => {
expect(__testing.resolveEndpoint("not a url", "/search")).toBe(
"https://api.tavily.com/search",
);
expect(__testing.resolveEndpoint("", "/extract")).toBe("https://api.tavily.com/extract");
});
});

View File

@@ -0,0 +1,44 @@
import { describe, expect, it, vi } from "vitest";
const runTavilySearch = vi.fn(async (params: Record<string, unknown>) => params);
vi.mock("./tavily-client.js", () => ({
runTavilySearch,
}));
describe("tavily web search provider", () => {
it("exposes the expected metadata and selection wiring", async () => {
const { createTavilyWebSearchProvider } = await import("./tavily-search-provider.js");
const provider = createTavilyWebSearchProvider();
const applied = provider.applySelectionConfig({});
expect(provider.id).toBe("tavily");
expect(provider.credentialPath).toBe("plugins.entries.tavily.config.webSearch.apiKey");
expect(applied.plugins?.entries?.tavily?.enabled).toBe(true);
});
it("maps generic tool arguments into Tavily search params", async () => {
const { createTavilyWebSearchProvider } = await import("./tavily-search-provider.js");
const provider = createTavilyWebSearchProvider();
const tool = provider.createTool({
config: { test: true },
} as never);
const result = await tool.execute({
query: "weather sf",
count: 7,
});
expect(runTavilySearch).toHaveBeenCalledWith({
cfg: { test: true },
query: "weather sf",
maxResults: 7,
});
expect(result).toEqual({
cfg: { test: true },
query: "weather sf",
maxResults: 7,
});
});
});

View File

@@ -0,0 +1,85 @@
import { describe, expect, it, vi } from "vitest";
const runTavilySearch = vi.fn(async (params: Record<string, unknown>) => ({
ok: true,
params,
}));
vi.mock("./tavily-client.js", () => ({
runTavilySearch,
}));
describe("tavily search tool", () => {
it("normalizes optional parameters before invoking Tavily", async () => {
const { createTavilySearchTool } = await import("./tavily-search-tool.js");
const tool = createTavilySearchTool({
config: { env: "test" },
} as never);
const result = await tool.execute("call-1", {
query: "best docs",
search_depth: "advanced",
topic: "news",
max_results: 5,
include_answer: true,
time_range: "week",
include_domains: ["docs.openclaw.ai", "", "openclaw.ai"],
exclude_domains: ["bad.example", ""],
});
expect(runTavilySearch).toHaveBeenCalledWith({
cfg: { env: "test" },
query: "best docs",
searchDepth: "advanced",
topic: "news",
maxResults: 5,
includeAnswer: true,
timeRange: "week",
includeDomains: ["docs.openclaw.ai", "openclaw.ai"],
excludeDomains: ["bad.example"],
});
expect(result).toMatchObject({
details: {
ok: true,
params: {
cfg: { env: "test" },
query: "best docs",
searchDepth: "advanced",
topic: "news",
maxResults: 5,
includeAnswer: true,
timeRange: "week",
includeDomains: ["docs.openclaw.ai", "openclaw.ai"],
excludeDomains: ["bad.example"],
},
},
});
expect(result.content[0]).toMatchObject({
type: "text",
});
});
it("requires a query and drops empty domain arrays", async () => {
const { createTavilySearchTool } = await import("./tavily-search-tool.js");
const tool = createTavilySearchTool({
config: { env: "test" },
} as never);
await expect(
tool.execute("call-2", {
query: "simple",
include_domains: [""],
exclude_domains: [],
}),
).resolves.toMatchObject({
details: {
ok: true,
params: {
cfg: { env: "test" },
query: "simple",
includeAnswer: false,
},
},
});
});
});