-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin-provider.test.ts
More file actions
94 lines (83 loc) · 3.26 KB
/
Copy pathplugin-provider.test.ts
File metadata and controls
94 lines (83 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { describe, expect, test } from "bun:test";
import {
collectWebPlugins,
selectWebPlugin,
resolveWebProviderFromPlugins,
webBrand,
type WebPluginCandidate,
} from "./plugin-provider.js";
import type { PluginModule } from "../plugins/loader.js";
import type { WebProvider } from "./types.js";
function stubProvider(name: string): WebProvider {
return {
name,
search: async () => [{ title: "t", url: "https://x", snippet: "s" }],
fetch: async () => "body",
};
}
function webModule(id: string, name: string): PluginModule {
return {
manifest: { id, name, kind: "web", credentials: [{ key: "apiKey", label: "Key", secret: true }] },
createWebProvider: (() => stubProvider(id)) as unknown,
};
}
describe("collectWebPlugins", () => {
test("keeps only web-kind modules with a factory", () => {
const modules: PluginModule[] = [
webModule("exa", "Exa Search"),
{ manifest: { id: "cmd", name: "Cmd", kind: "command" } },
{ manifest: { id: "broken", name: "Broken", kind: "web" } }, // no factory
];
const candidates = collectWebPlugins(modules);
expect(candidates.map((c) => c.id)).toEqual(["exa"]);
expect(candidates[0]!.credentials[0]!.key).toBe("apiKey");
});
});
describe("selectWebPlugin", () => {
const candidates: WebPluginCandidate[] = [
{ id: "exa", name: "Exa Search", credentials: [], factory: () => stubProvider("exa") },
{ id: "other", name: "Other", credentials: [], factory: () => stubProvider("other") },
];
test("explicit override wins", () => {
expect(selectWebPlugin(candidates, {}, "other")?.id).toBe("other");
});
test("falls back to the single enabled plugin when no override", () => {
expect(selectWebPlugin(candidates, { exa: { enabled: true } }, undefined)?.id).toBe("exa");
});
test("returns undefined when multiple enabled and no override (ambiguous)", () => {
expect(selectWebPlugin(candidates, { exa: { enabled: true }, other: { enabled: true } }, undefined)).toBeUndefined();
});
test("returns undefined when none enabled and no override", () => {
expect(selectWebPlugin(candidates, {}, undefined)).toBeUndefined();
});
});
describe("resolveWebProviderFromPlugins", () => {
test("builds the selected provider with stored credentials", async () => {
const candidates = collectWebPlugins([webModule("exa", "Exa Search")]);
const active = await resolveWebProviderFromPlugins({
candidates,
pluginConfig: { exa: { enabled: true, credentials: { apiKey: "k" } } },
webOverride: undefined,
});
expect(active?.name).toBe("Exa Search");
expect(active?.provider.name).toBe("exa");
});
test("returns undefined (falls back to local) when the factory throws", async () => {
const candidates: WebPluginCandidate[] = [
{ id: "exa", name: "Exa Search", credentials: [], factory: () => { throw new Error("bad key"); } },
];
const active = await resolveWebProviderFromPlugins({
candidates,
pluginConfig: {},
webOverride: "exa",
});
expect(active).toBeUndefined();
});
});
describe("webBrand", () => {
test("strips trailing Search/Fetch", () => {
expect(webBrand("Exa Search")).toBe("Exa");
expect(webBrand("Tavily Fetch")).toBe("Tavily");
expect(webBrand("Brave")).toBe("Brave");
});
});