fix(mcp): rewrite tests to call createWebsearchConfig directly
Previously tests were tautological - they defined local logic instead of invoking the actual implementation. Now all tests properly exercise createWebsearchConfig.
This commit is contained in:
parent
fea7bd2dcf
commit
17cb49543a
@ -1,4 +1,5 @@
|
|||||||
import { describe, expect, test, beforeEach, afterEach } from "bun:test"
|
import { describe, expect, test, beforeEach, afterEach } from "bun:test"
|
||||||
|
import { createWebsearchConfig } from "./websearch"
|
||||||
|
|
||||||
describe("websearch MCP provider configuration", () => {
|
describe("websearch MCP provider configuration", () => {
|
||||||
const originalEnv = { ...process.env }
|
const originalEnv = { ...process.env }
|
||||||
@ -13,28 +14,27 @@ describe("websearch MCP provider configuration", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
test("returns Exa config when no config provided", () => {
|
test("returns Exa config when no config provided", () => {
|
||||||
//#given
|
//#given - no config
|
||||||
const provider = undefined
|
|
||||||
const exaKey = undefined
|
|
||||||
const tavilyKey = undefined
|
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
process.env.EXA_API_KEY = exaKey
|
const result = createWebsearchConfig()
|
||||||
process.env.TAVILY_API_KEY = tavilyKey
|
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(provider).toBeUndefined()
|
expect(result.url).toContain("mcp.exa.ai")
|
||||||
|
expect(result.type).toBe("remote")
|
||||||
|
expect(result.enabled).toBe(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
test("returns Exa config when provider is 'exa'", () => {
|
test("returns Exa config when provider is 'exa'", () => {
|
||||||
//#given
|
//#given
|
||||||
const provider = "exa"
|
const config = { provider: "exa" as const }
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const selectedProvider = provider
|
const result = createWebsearchConfig(config)
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(selectedProvider).toBe("exa")
|
expect(result.url).toContain("mcp.exa.ai")
|
||||||
|
expect(result.type).toBe("remote")
|
||||||
})
|
})
|
||||||
|
|
||||||
test("includes x-api-key header when EXA_API_KEY is set", () => {
|
test("includes x-api-key header when EXA_API_KEY is set", () => {
|
||||||
@ -43,91 +43,74 @@ describe("websearch MCP provider configuration", () => {
|
|||||||
process.env.EXA_API_KEY = apiKey
|
process.env.EXA_API_KEY = apiKey
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const headers = process.env.EXA_API_KEY
|
const result = createWebsearchConfig()
|
||||||
? { "x-api-key": process.env.EXA_API_KEY }
|
|
||||||
: undefined
|
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(headers).toEqual({ "x-api-key": "test-exa-key-12345" })
|
expect(result.headers).toEqual({ "x-api-key": apiKey })
|
||||||
expect(headers?.["x-api-key"]).toBe(apiKey)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test("returns Tavily config when provider is 'tavily' and TAVILY_API_KEY set", () => {
|
test("returns Tavily config when provider is 'tavily' and TAVILY_API_KEY set", () => {
|
||||||
//#given
|
//#given
|
||||||
const provider = "tavily"
|
|
||||||
const tavilyKey = "test-tavily-key-67890"
|
const tavilyKey = "test-tavily-key-67890"
|
||||||
process.env.TAVILY_API_KEY = tavilyKey
|
process.env.TAVILY_API_KEY = tavilyKey
|
||||||
|
const config = { provider: "tavily" as const }
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const headers = process.env.TAVILY_API_KEY
|
const result = createWebsearchConfig(config)
|
||||||
? { Authorization: `Bearer ${process.env.TAVILY_API_KEY}` }
|
|
||||||
: undefined
|
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(provider).toBe("tavily")
|
expect(result.url).toContain("mcp.tavily.com")
|
||||||
expect(headers).toEqual({ Authorization: "Bearer test-tavily-key-67890" })
|
expect(result.headers).toEqual({ Authorization: `Bearer ${tavilyKey}` })
|
||||||
expect(headers?.Authorization).toContain("Bearer")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test("throws error when provider is 'tavily' but TAVILY_API_KEY missing", () => {
|
test("throws error when provider is 'tavily' but TAVILY_API_KEY missing", () => {
|
||||||
//#given
|
//#given
|
||||||
const provider = "tavily"
|
|
||||||
delete process.env.TAVILY_API_KEY
|
delete process.env.TAVILY_API_KEY
|
||||||
|
const config = { provider: "tavily" as const }
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const createTavilyConfig = () => {
|
const createTavilyConfig = () => createWebsearchConfig(config)
|
||||||
if (provider === "tavily" && !process.env.TAVILY_API_KEY) {
|
|
||||||
throw new Error("TAVILY_API_KEY environment variable is required for Tavily provider")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(createTavilyConfig).toThrow("TAVILY_API_KEY environment variable is required")
|
expect(createTavilyConfig).toThrow("TAVILY_API_KEY environment variable is required")
|
||||||
})
|
})
|
||||||
|
|
||||||
test("returns Exa when both keys present but no explicit provider (conflict resolution)", () => {
|
test("returns Exa when both keys present but no explicit provider", () => {
|
||||||
//#given
|
//#given
|
||||||
const exaKey = "test-exa-key"
|
process.env.EXA_API_KEY = "test-exa-key"
|
||||||
const tavilyKey = "test-tavily-key"
|
process.env.TAVILY_API_KEY = "test-tavily-key"
|
||||||
const provider = undefined
|
|
||||||
process.env.EXA_API_KEY = exaKey
|
|
||||||
process.env.TAVILY_API_KEY = tavilyKey
|
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const selectedProvider = provider || "exa"
|
const result = createWebsearchConfig()
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(selectedProvider).toBe("exa")
|
expect(result.url).toContain("mcp.exa.ai")
|
||||||
expect(process.env.EXA_API_KEY).toBe(exaKey)
|
expect(result.headers).toEqual({ "x-api-key": "test-exa-key" })
|
||||||
expect(process.env.TAVILY_API_KEY).toBe(tavilyKey)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Tavily config uses Authorization Bearer header", () => {
|
test("Tavily config uses Authorization Bearer header format", () => {
|
||||||
//#given
|
//#given
|
||||||
const tavilyKey = "tavily-secret-key-xyz"
|
const tavilyKey = "tavily-secret-key-xyz"
|
||||||
process.env.TAVILY_API_KEY = tavilyKey
|
process.env.TAVILY_API_KEY = tavilyKey
|
||||||
|
const config = { provider: "tavily" as const }
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const headers = {
|
const result = createWebsearchConfig(config)
|
||||||
Authorization: `Bearer ${process.env.TAVILY_API_KEY}`,
|
|
||||||
}
|
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(headers.Authorization).toMatch(/^Bearer /)
|
expect(result.headers?.Authorization).toMatch(/^Bearer /)
|
||||||
expect(headers.Authorization).toBe(`Bearer ${tavilyKey}`)
|
expect(result.headers?.Authorization).toBe(`Bearer ${tavilyKey}`)
|
||||||
expect(headers.Authorization).not.toContain("x-api-key")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test("Tavily config points to mcp.tavily.com", () => {
|
test("Exa config has no headers when EXA_API_KEY not set", () => {
|
||||||
//#given
|
//#given
|
||||||
const tavilyUrl = "https://mcp.tavily.com/mcp/"
|
delete process.env.EXA_API_KEY
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const url = tavilyUrl
|
const result = createWebsearchConfig()
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(url).toContain("mcp.tavily.com")
|
expect(result.url).toContain("mcp.exa.ai")
|
||||||
expect(url).toMatch(/^https:\/\//)
|
expect(result.headers).toBeUndefined()
|
||||||
expect(url).toEndWith("/")
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user