Adds packages/search1api as @magpie-community/tools-search1api 0.1.0
(fatwang2, replacing #340). It has six tools: search, news, ask, crawl,
sitemap and trending. Sign-in is one account by API key or OAuth: PKCE
S256, dynamic client registration and a loopback callback. Tokens refresh
through auth.refresh and on a 401. scripts/check.mjs now accepts a tools
plugin with auth and no loader, as long as its config gives the provider
no npm and no models.
Run at 7cf6a5c merged onto main, in a sandbox HOME, with HTTPS_PROXY
pointed at a dead port:
- bun test packages/search1api scripts/check.test.mjs: 19 pass.
- node --test on the package's own tests: 16 pass.
- tests/host.integration.mjs against main magpie's host.js: 2 pass.
- bun test across the whole repo: 2647 pass, 0 fail. This needs 017f05c.
Before it, gcp-ai's fetch guard refused this package's loopback
fixtures in the shared bun process.
- bun scripts/check.mjs: all ok.
Broken by hand, each one red:
- Dropping the SAME_SESSION check before a refresh: 1 test.
- Never refreshing an expired token: 3 tests.
- Ignoring the apiKey option: 4 tests.
- Not red: dropping the second check, which catches a logout during an
in-flight refresh. The first check covers it in the logout test.
Real use:
- A sandbox magpie took the package with `plugin add`. `plugin list`
shows its 6 tools. `plugin login search1api 2` with a key showed
Providers as "Search1API · signed in as API key …-384 · no models
exposed".
- The search tool ran against the real api.search1api.com with a fake
key and got 401.
- OAuth discovery is live: oauth-protected-resource points to
clerk.s1.dev, which has a registration endpoint and S256.
Not tried:
- A real OAuth consent, or a search with a real key. There is no
Search1API account here.
Notes:
- The plugin reads the signed-in record from
`<directory>/plugin-auth.json` itself, since tools get no getAuth.
magpie writes that file by temp-and-rename, so a read never sees half a
write.
- magpie's login line says "its models are search1api/<model>" for a
provider with no models. That is magpie's wording, not this PR's.
219 lines
7.9 KiB
JavaScript
219 lines
7.9 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import test from "node:test";
|
|
import { Search1APIPlugin } from "../index.mjs";
|
|
import { fixture, context } from "./helpers.mjs";
|
|
|
|
test("all six tools forward arguments to the API and preserve result metadata", async (t) => {
|
|
const f = await fixture(t);
|
|
const plugin = await Search1APIPlugin(f.input, {
|
|
apiKey: "test-option-key",
|
|
baseUrl: f.baseUrl,
|
|
});
|
|
assert.deepEqual(Object.keys(plugin.tool), [
|
|
"search",
|
|
"news",
|
|
"ask",
|
|
"crawl",
|
|
"sitemap",
|
|
"trending",
|
|
]);
|
|
const cases = [
|
|
[
|
|
"search",
|
|
{
|
|
query: "Bun",
|
|
searchService: "grokipedia",
|
|
page: 2,
|
|
timeRange: "week",
|
|
maxResults: 3,
|
|
includeSites: ["example.com"],
|
|
crawlResults: 0,
|
|
},
|
|
{
|
|
query: "Bun",
|
|
search_service: "grokipedia",
|
|
page: 2,
|
|
time_range: "week",
|
|
max_results: 3,
|
|
include_sites: ["example.com"],
|
|
crawl_results: 0,
|
|
},
|
|
],
|
|
[
|
|
"news",
|
|
{
|
|
query: "Bun",
|
|
searchService: "reuters",
|
|
timeRange: "day",
|
|
excludeSites: ["example.com"],
|
|
},
|
|
{
|
|
query: "Bun",
|
|
search_service: "reuters",
|
|
max_results: 10,
|
|
crawl_results: 0,
|
|
time_range: "day",
|
|
exclude_sites: ["example.com"],
|
|
},
|
|
],
|
|
["ask", { query: "Bun this week" }, { query: "Bun this week" }],
|
|
["crawl", { url: "https://example.com" }, { url: "https://example.com" }],
|
|
["sitemap", { url: "https://example.com" }, { url: "https://example.com" }],
|
|
[
|
|
"trending",
|
|
{ service: "github", maxResults: 4 },
|
|
{ search_service: "github", max_results: 4 },
|
|
],
|
|
];
|
|
for (const [name, args, body] of cases) {
|
|
const output = JSON.parse(await plugin.tool[name].execute(args, context()));
|
|
const request = f.calls.at(-1);
|
|
assert.equal(request.path, `/${name}`);
|
|
assert.equal(request.headers.authorization, "Bearer test-option-key");
|
|
assert.deepEqual(JSON.parse(request.raw), body);
|
|
assert.deepEqual(output.results[0].metadata, { stars: 0 });
|
|
assert.equal(output.results[0].relevance, 0.9);
|
|
assert.deepEqual(output.errors, []);
|
|
}
|
|
assert.deepEqual(Object.keys(plugin.tool.ask.args.properties), ["query"]);
|
|
await assert.rejects(plugin.tool.search.execute({ query: " " }, context()));
|
|
await assert.rejects(plugin.tool.ask.execute({ query: "x".repeat(501) }, context()));
|
|
await assert.rejects(plugin.tool.search.execute({ query: "test", searchService: "sogou" }, context()));
|
|
await plugin.tool.search.execute({ query: "default search" }, context());
|
|
assert.deepEqual(JSON.parse(f.calls.at(-1).raw), {
|
|
query: "default search",
|
|
max_results: 10,
|
|
search_service: "google",
|
|
crawl_results: 0,
|
|
});
|
|
await plugin.tool.news.execute({ query: "default news" }, context());
|
|
assert.deepEqual(JSON.parse(f.calls.at(-1).raw), {
|
|
query: "default news",
|
|
max_results: 10,
|
|
search_service: "bing",
|
|
crawl_results: 0,
|
|
});
|
|
});
|
|
|
|
test("API key precedence and missing credentials leave plugin installation usable", async (t) => {
|
|
const f = await fixture(t);
|
|
const original = process.env.SEARCH1API_API_KEY;
|
|
t.after(() => {
|
|
if (original === undefined) delete process.env.SEARCH1API_API_KEY;
|
|
else process.env.SEARCH1API_API_KEY = original;
|
|
});
|
|
delete process.env.SEARCH1API_API_KEY;
|
|
const plugin = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
await assert.rejects(
|
|
plugin.tool.search.execute({ query: "test" }, context()),
|
|
/magpie plugin login search1api/,
|
|
);
|
|
await f.save({ type: "api", key: "test-native-key" });
|
|
await plugin.tool.search.execute({ query: "test" }, context());
|
|
assert.equal(f.calls.at(-1).headers.authorization, "Bearer test-native-key");
|
|
process.env.SEARCH1API_API_KEY = "test-env-key";
|
|
await plugin.tool.search.execute({ query: "test" }, context());
|
|
assert.equal(f.calls.at(-1).headers.authorization, "Bearer test-env-key");
|
|
const override = await Search1APIPlugin(f.input, {
|
|
baseUrl: f.baseUrl,
|
|
apiKey: "test-option-key",
|
|
});
|
|
await override.tool.search.execute({ query: "test" }, context());
|
|
assert.equal(f.calls.at(-1).headers.authorization, "Bearer test-option-key");
|
|
});
|
|
|
|
test("expired OAuth sessions refresh once across concurrent plugin instances and survive reload", async (t) => {
|
|
const f = await fixture(t);
|
|
await f.save({ ...f.oauth(), expires: Date.now() - 1 });
|
|
const a = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
const b = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
await Promise.all(
|
|
Array.from({ length: 8 }, (_, i) =>
|
|
(i % 2 ? a : b).tool.search.execute({ query: "test" }, context()),
|
|
),
|
|
);
|
|
assert.equal(f.state.refreshes, 1);
|
|
assert.equal((await f.readAuth()).search1api.refresh, "test-refresh-1");
|
|
const reload = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
await reload.tool.search.execute({ query: "test" }, context());
|
|
assert.equal(f.state.refreshes, 1);
|
|
assert.equal(f.calls.at(-1).headers.authorization, "Bearer test-access-1");
|
|
});
|
|
|
|
test("a 401 renews an OAuth session once; API keys are not renewed or retried", async (t) => {
|
|
const f = await fixture(t);
|
|
await f.save(f.oauth());
|
|
f.state.rejectAccess = true;
|
|
const plugin = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
await plugin.tool.search.execute({ query: "test" }, context());
|
|
assert.equal(f.state.refreshes, 1);
|
|
assert.equal(f.calls.filter((c) => c.path === "/search").length, 2);
|
|
const keyPlugin = await Search1APIPlugin(f.input, {
|
|
baseUrl: f.baseUrl,
|
|
apiKey: "test-access-0",
|
|
});
|
|
await assert.rejects(
|
|
keyPlugin.tool.search.execute({ query: "test" }, context()),
|
|
);
|
|
assert.equal(f.state.refreshes, 1);
|
|
assert.equal(f.calls.filter((c) => c.path === "/search").length, 3);
|
|
});
|
|
|
|
test("revoked refresh tokens surface a safe sign-in error and do not alter other accounts", async (t) => {
|
|
const f = await fixture(t);
|
|
const auth = { ...f.oauth(), expires: Date.now() - 1 };
|
|
await writeFile(
|
|
f.authPath,
|
|
JSON.stringify({
|
|
other: { type: "api", key: "test-other-key" },
|
|
search1api: auth,
|
|
}),
|
|
);
|
|
f.state.rejectRefresh = true;
|
|
const plugin = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
await assert.rejects(
|
|
plugin.tool.search.execute({ query: "test" }, context()),
|
|
(error) => {
|
|
assert.match(error.message, /Sign in again/);
|
|
assert.doesNotMatch(error.message, /must-not-appear/);
|
|
return true;
|
|
},
|
|
);
|
|
assert.equal((await f.readAuth()).other.key, "test-other-key");
|
|
});
|
|
|
|
test("logout during refresh wins and no old session is resurrected", async (t) => {
|
|
const f = await fixture(t);
|
|
await f.save({ ...f.oauth(), expires: Date.now() - 1 });
|
|
f.state.refreshDelay = 100;
|
|
const plugin = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
const call = plugin.tool.search.execute({ query: "test" }, context());
|
|
while (!f.calls.some((c) => c.path === "/token"))
|
|
await new Promise((r) => setTimeout(r, 5));
|
|
await writeFile(f.authPath, "{}");
|
|
await assert.rejects(call, /Sign in/);
|
|
assert.deepEqual(await f.readAuth(), {});
|
|
});
|
|
|
|
test("already aborted calls make no upstream request; corrupt auth is never overwritten", async (t) => {
|
|
const f = await fixture(t);
|
|
const plugin = await Search1APIPlugin(f.input, {
|
|
baseUrl: f.baseUrl,
|
|
apiKey: "test-key",
|
|
});
|
|
const controller = new AbortController();
|
|
controller.abort();
|
|
await assert.rejects(
|
|
plugin.tool.search.execute({ query: "test" }, context(controller.signal)),
|
|
);
|
|
assert.equal(f.calls.length, 0);
|
|
await writeFile(f.authPath, "broken auth");
|
|
const native = await Search1APIPlugin(f.input, { baseUrl: f.baseUrl });
|
|
await assert.rejects(
|
|
native.tool.search.execute({ query: "test" }, context()),
|
|
/cannot be read/,
|
|
);
|
|
assert.equal(await readFile(f.authPath, "utf8"), "broken auth");
|
|
});
|