Files
plugins/scripts/check.test.mjs
T
wang2 6188ab5be7 search1api 0.1.0: Search1API's six tools with API key or OAuth sign-in (#384)
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.
2026-10-25 17:53:00 +08:00

56 lines
2.4 KiB
JavaScript

import { expect, test } from "bun:test"
import { copyFileSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"
import { tmpdir } from "node:os"
import { join } from "node:path"
// Exercise the checker in a tiny independent repository, without loading any
// real plugin, credential file or network client.
function check(source) {
const dir = mkdtempSync(join(tmpdir(), "magpie-check-test-"))
try {
mkdirSync(join(dir, "scripts"))
mkdirSync(join(dir, "packages", "fixture"), { recursive: true })
copyFileSync(new URL("./check.mjs", import.meta.url), join(dir, "scripts", "check.mjs"))
writeFileSync(join(dir, "packages", "fixture", "package.json"), JSON.stringify({ main: "index.mjs" }))
writeFileSync(join(dir, "packages", "fixture", "index.mjs"), source)
const result = Bun.spawnSync([process.execPath, join(dir, "scripts", "check.mjs")])
return { code: result.exitCode, output: result.stdout.toString() + result.stderr.toString() }
} finally {
rmSync(dir, { recursive: true, force: true })
}
}
const tool = `{search: {description: "Search the web", args: {type: "object"}, execute() {}}}`
const auth = `{provider: "fixture", methods: [{type: "api", label: "API key"}]}`
test("check accepts tools with sign-in and no chat models or loader", () => {
const result = check(`export const Plugin = async () => ({
auth: ${auth}, tool: ${tool},
config: async (cfg) => { cfg.provider.fixture = {name: "Fixture", models: {}} }
})`)
expect(result.code).toBe(0)
expect(result.output).toContain("✓ fixture")
})
test("check still checks tool definitions and auth methods together", () => {
const result = check(`export const Plugin = async () => ({
auth: {provider: "fixture", methods: [{type: "unknown"}]},
tool: {search: {args: {type: "object"}}}
})`)
expect(result.code).toBe(1)
expect(result.output).toContain("search has no execute")
expect(result.output).toContain("search has no description")
expect(result.output).toContain("method type unknown")
})
test("check still requires loaders for providers, including providers with tools", () => {
for (const tools of ["", `tool: ${tool},`]) {
const result = check(`export const Plugin = async () => ({
auth: ${auth}, ${tools}
config: async (cfg) => { cfg.provider.fixture = {npm: "@ai-sdk/openai", models: {example: {}}} }
})`)
expect(result.code).toBe(1)
expect(result.output).toContain("auth.loader")
}
})