Files
magpie/internal/plugin/search_test.go
T
yetone 90bf96a23d search: a plugin can give a web search API (its search hook)
treeman on Discord #help asked for web search to be pluggable so users can
add search vendors. A plugin's `search` hook, {search: {<id>: {name, keysURL?,
keyOptional?, url?, execute({query, count, key, url}, ctx)}}}, adds an API to
Settings > Models > Web search as `<plugin>:<id>`. It is added, keyed, ordered,
switched off and removed like Tavily or Brave. The built-in APIs stay built in.

How: host.js collects the hook's entries (id [a-z0-9-], an execute) into
Loaded.Searches and runs one with the `search` op (timeout from ctx, an error
with a numeric status comes back as a refusal). The host notes them in
plugin-searches.json (internal/plugin/search.go); provider.AllSearchVendors
adds the noted ones of plugins that are on to SearchVendors, so Ready, check,
the CLI and the GUI list see them. askSearchAPI hands a plugin vendor to
askPluginSearch below askSearchAPIKeys, so keys, rest on 401/402/403/429,
order, the masked query and the request-log row are the built-ins' own.

Run (mote, on this tree, whose base df7c94bdf was just fetched):
- go test -timeout 45m ./internal/plugin ./internal/provider ./internal/gateway
  ./internal/cli: ok.
- go vet native; GOOS=windows go vet plugin, provider, gateway, cli: ok;
  GOOS=linux go vet plugin, provider, gateway: ok. gofmt -l: empty.
- internal/gui: TestCoreScriptBudget and TestMainWindowCorners fail in a
  full run. This change touches no script (only api.go's loop), so the
  budget red is the base's (re-checked after the rebase, below);
  TestMainWindowCorners is AppKit on mote (macOS 27).
Broken by hand: skipping off plugins in NotedSearches turns TestPluginSearch
red; the refusal mapping removed turns TestPluginSearchAPI red.
Not tried: a real plugin against a real vendor's API; the GUI JS suite (no
GUI script changed); a Linux or Windows window; site/public/docs (en, zh, ja)
do not describe the hook yet. A plugin just added is listed once the host has
loaded it; RestoreSearchAPIs drops a plugin's entry on a machine without it.
The community README has the template (magpie-community/plugins 3ec42d4).

Reported by treeman.
2026-11-04 08:37:21 +08:00

96 lines
3.4 KiB
Go

package plugin
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
)
// A plugin's search hook (testdata/search): the host lists the APIs with an
// id magpie can use and an execute, leaves out the rest, and notes them for
// the provider package, which reads the note without starting the host and
// only for plugins that are on. A search is run with the user's key and
// address, its results read from a list or {results}, a throw with a
// status is a refused key, and one that never answers is ended.
func TestPluginSearch(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/search")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
loaded, err := Plugins(ctx)
if err != nil || len(loaded) != 1 {
t.Fatalf("%v %v", loaded, err)
}
var ids []string
for _, d := range loaded[0].Searches {
ids = append(ids, d.ID)
}
if strings.Join(ids, " ") != "fake own" {
t.Fatalf("searches = %+v", loaded[0].Searches)
}
if d := loaded[0].Searches[0]; d.Name != "Fake Search" || d.KeysURL != "https://example.com/keys" || d.KeyOptional || d.NeedURL {
t.Errorf("fake = %+v", d)
}
if d := loaded[0].Searches[1]; !d.NeedURL {
t.Errorf("own = %+v", d)
}
ns, err := NotedSearches()
if err != nil || len(ns) != 2 || ns[0].VendorID() != ShortName(Name(abs))+":fake" || ns[0].Spec != abs {
t.Fatalf("noted = %+v, %v", ns, err)
}
pages, err := RunSearch(ctx, abs, "fake", "go", 6, "k1", "http://x")
if err != nil || len(pages) != 2 || pages[0].Title != "T go" || pages[0].URL != "https://example.com/6" ||
pages[0].Text != "key k1 url http://x" || pages[0].Age != "2026" {
t.Errorf("pages = %+v, %v", pages, err)
}
_, err = RunSearch(ctx, abs, "fake", "go", 6, "spent", "")
var r SearchRefused
if !errors.As(err, &r) || r.Status != 429 || r.RetryAfter != "30" || !strings.Contains(r.Msg, "quota spent") {
t.Errorf("a refused key: %#v", err)
}
if _, err := RunSearch(ctx, abs, "fake", "go", 6, "boom", ""); err == nil || !strings.Contains(err.Error(), "it broke") || errors.As(err, &r) {
t.Errorf("a failure: %v", err)
}
if _, err := RunSearch(ctx, abs, "fake", "go", 6, "shape", ""); err == nil || !strings.Contains(err.Error(), "neither a list") {
t.Errorf("a wrong shape: %v", err)
}
if _, err := RunSearch(ctx, abs, "nothing", "go", 6, "", ""); err == nil || !strings.Contains(err.Error(), "no search API nothing") {
t.Errorf("an unknown API: %v", err)
}
short, stop := context.WithTimeout(ctx, 400*time.Millisecond)
t0 := time.Now()
_, err = RunSearch(short, abs, "fake", "go", 6, "hang", "")
stop()
// the host ends it at 400ms; Go's own backstop 5s later
if err == nil || time.Since(t0) > 4*time.Second {
t.Errorf("a search that never answers: %v after %s", err, time.Since(t0))
}
if _, err := RunSearch(ctx, abs, "fake", "go", 6, "k1", ""); err != nil {
t.Errorf("the host after a search ran out of time: %v", err)
}
// off: not listed
if err := SetOff(abs, true); err != nil {
t.Fatal(err)
}
if ns, err := NotedSearches(); err != nil || len(ns) != 0 {
t.Errorf("with the plugin off: %+v, %v", ns, err)
}
// a note that can't be read is an error, never "none, so remove"
if err := os.WriteFile(searchesPath(), []byte("{half"), 0o600); err != nil {
t.Fatal(err)
}
if _, err := NotedSearches(); err == nil {
t.Error("a corrupt note counted as no searches")
}
}