Files
oh-my-pi/packages/coding-agent/test/web/search/provider-chain.test.ts
T

94 lines
3.0 KiB
TypeScript

import { afterEach, describe, expect, it } from "bun:test";
import type { AuthStorage } from "@oh-my-pi/pi-ai";
import { SelectorController } from "@oh-my-pi/pi-coding-agent/modes/controllers/selector-controller";
import {
resolveProviderCandidates,
resolveProviderChain,
setExcludedSearchProviders,
setPreferredSearchProvider,
} from "@oh-my-pi/pi-coding-agent/web/search/provider";
import { SEARCH_PROVIDER_ORDER } from "@oh-my-pi/pi-coding-agent/web/search/types";
const authStorage = {} as AuthStorage;
const originalBraveApiKey = process.env.BRAVE_API_KEY;
const originalJinaApiKey = process.env.JINA_API_KEY;
function enableKeyBackedProviders(): void {
process.env.BRAVE_API_KEY = "test-brave-key";
process.env.JINA_API_KEY = "test-jina-key";
}
function restoreEnv(): void {
if (originalBraveApiKey === undefined) {
delete process.env.BRAVE_API_KEY;
} else {
process.env.BRAVE_API_KEY = originalBraveApiKey;
}
if (originalJinaApiKey === undefined) {
delete process.env.JINA_API_KEY;
} else {
process.env.JINA_API_KEY = originalJinaApiKey;
}
}
afterEach(() => {
setPreferredSearchProvider("auto");
setExcludedSearchProviders([]);
restoreEnv();
});
describe("resolveProviderCandidates", () => {
it("orders the preferred provider before unloaded fallbacks", () => {
const candidates = resolveProviderCandidates("exa");
expect(candidates[0]).toEqual({ id: "exa", explicit: true });
expect(candidates.slice(1).map(candidate => candidate.id)).toEqual(
SEARCH_PROVIDER_ORDER.filter(id => id !== "exa"),
);
});
it("omits excluded providers without resolving them", () => {
setExcludedSearchProviders(["duckduckgo", "google"]);
const candidates = resolveProviderCandidates("exa");
expect(candidates.map(candidate => candidate.id)).not.toContain("duckduckgo");
expect(candidates.map(candidate => candidate.id)).not.toContain("google");
});
});
describe("resolveProviderChain", () => {
it("omits excluded providers from the fallback chain", async () => {
enableKeyBackedProviders();
setExcludedSearchProviders(SEARCH_PROVIDER_ORDER.filter(id => id !== "jina"));
const providers = await resolveProviderChain(authStorage, "auto");
expect(providers.map(provider => provider.id)).toEqual(["jina"]);
});
it("ignores the preferred provider when it is excluded", async () => {
enableKeyBackedProviders();
setExcludedSearchProviders(SEARCH_PROVIDER_ORDER.filter(id => id !== "jina"));
const providers = await resolveProviderChain(authStorage, "brave");
expect(providers.map(provider => provider.id)).toEqual(["jina"]);
});
it("applies live settings edits to the exclusion chain", async () => {
enableKeyBackedProviders();
const controller = new SelectorController({} as unknown as ConstructorParameters<typeof SelectorController>[0]);
controller.handleSettingChange(
"providers.webSearchExclude",
SEARCH_PROVIDER_ORDER.filter(id => id !== "jina"),
);
const providers = await resolveProviderChain(authStorage, "auto");
expect(providers.map(provider => provider.id)).toEqual(["jina"]);
});
});