The reporter has endpoints that answer as Tavily, Exa and others do, at
addresses that aren't the vendors', and Settings only took a key. The
saved SearchAPI already had a url, and the CLI's url= set it, but the
Settings row showed the field only for SearXNG and sent "" for the rest.
- Settings › Models › Web search: the add row has a base URL field on a
line of its own under the key, for every API, with the vendor's own
address as its placeholder (searchVendorJSON.base). Picking an API
already added fills in its saved URL, so Add with a new key alone keeps
it; Add with no key typed changes only the URL, the key kept (the key
placeholder says so). Strings in zh, zh-TW, ja, de.
- provider: check() also refuses a URL with a ?query or #fragment, since
the vendor's path goes after it.
- gateway: the key goes only to the URL set. Go drops Authorization on a
redirect to another host but keeps Brave's X-Subscription-Token and
Exa's x-api-key, so a search API's redirect to another host is not
followed; the error names where it was sent. The request still goes
through the gateway's client and so magpie's proxy settings.
- CLI: `magpie search add <api> <key>` keeps the url set before; `url=`
with nothing after it puts the vendor's own back (was: a new key alone
dropped the url).
- docs/subsystems/gateway-routing.md: "A search API's base URL";
gui-shell.md: the field.
- search-api.test.cjs: Settings is drawn twice (/api/state, then
/api/settings), so scrolling to the add row can hit a detached node
(WebKit, 1 in ~6 runs here); it retries the scroll.
magpie ships and lists no third-party search endpoint; the URL is only
the user's.
Tests (sandbox HOME, pinned caches):
- TestSearchAPIAtItsOwnBaseURL: an httptest fake at <fake>/relay/<vendor>/
gets tavily POST /relay/tavily/search with Bearer, exa POST
/relay/exa/search with x-api-key, brave GET
/relay/brave/res/v1/web/search with X-Subscription-Token, firecrawl
POST /relay/firecrawl/v2/search with Bearer. Passes on the old gateway
too: the backend already honoured a url.
- TestSearchKeyStaysAtItsBaseURL: with search_api.go put back, FAIL
"a key reached another host: exa-secret / brave-secret".
- TestSearchCLI: with search_cli.go put back, FAIL "a new key took the
base URL away"; with provider/search_api.go put back, FAIL
"url=https://x.example.com/?k=1 taken".
- TestSearchAPIBaseURLFromSettings: POST /api/settings/search-api saves,
keeps the key, refuses ftp/no-scheme/query URLs, lists vendor bases.
- search-api.test.cjs in Chromium and WebKit, en and zh, 980px and
420px: 8/8; with the old app.js, FAIL "Tavily takes a base URL"
(false !== true). WebKit 980px 10 runs in a row green after the retry.
search-first, search-mcp, searcher-pick, i18n-keys, gui-ja, gui-de,
gui-zh-tw, click-scroll with it: 45/45 in each engine.
- Screenshots at 980/420 in both engines checked by eye.
- go test -tags nogui -timeout 40m ./... on 5fa908c08 + this: all 48 packages ok. gofmt -l clean; go vet, GOOS=linux and GOOS=windows go vet -tags nogui on ., internal/gateway, internal/gui, internal/provider: clean.
Not tried against a real third-party endpoint or a real vendor key.
94 lines
3.5 KiB
Go
94 lines
3.5 KiB
Go
package gui
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// A search API's saved key comes back whole for its row's Show and Copy
|
|
// (OnurBen on Discord), while the settings still list it masked; one not
|
|
// added is an error.
|
|
func TestSearchKeyShown(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
const key = "tvly-dev-0123456789abcdef"
|
|
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "tavily", Key: key}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
post := func(path, body string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", path, strings.NewReader(body)))
|
|
return rec
|
|
}
|
|
rec := post("/api/settings/search-key", `{"vendor":"tavily"}`)
|
|
var got struct{ Key string }
|
|
if rec.Code != 200 || json.Unmarshal(rec.Body.Bytes(), &got) != nil || got.Key != key {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if rec := post("/api/settings/search-key", `{"vendor":"exa"}`); rec.Code < 400 {
|
|
t.Fatalf("a search API not added: %d %s", rec.Code, rec.Body)
|
|
}
|
|
var s settingsJSON
|
|
searchState(&s)
|
|
if len(s.SearchAPIs) != 1 || s.SearchAPIs[0].Key == key || s.SearchAPIs[0].Key == "" {
|
|
t.Fatalf("the settings list %+v", s.SearchAPIs)
|
|
}
|
|
}
|
|
|
|
// Settings sets a search API's base URL (#1580): Add with an address saves
|
|
// it, an address alone keeps the key, a URL that isn't http(s) is refused,
|
|
// and each vendor says the address it is asked at while none is set.
|
|
func TestSearchAPIBaseURLFromSettings(t *testing.T) {
|
|
h := t.TempDir()
|
|
t.Setenv("HOME", h)
|
|
t.Setenv("USERPROFILE", h)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
|
|
post := func(body string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
Handler(nil, nil).ServeHTTP(rec, httptest.NewRequest("POST", "/api/settings/search-api", strings.NewReader(body)))
|
|
return rec
|
|
}
|
|
if rec := post(`{"vendor":"tavily","key":"tvly-1","url":"https://search.example.com/tavily/"}`); rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if as := provider.SearchAPIs(); len(as) != 1 || as[0].Base() != "https://search.example.com/tavily" || as[0].Key != "tvly-1" {
|
|
t.Fatalf("%+v", as)
|
|
}
|
|
// an address alone keeps the key
|
|
if rec := post(`{"vendor":"tavily","key":"","url":"https://other.example.com"}`); rec.Code != 200 {
|
|
t.Fatalf("%d %s", rec.Code, rec.Body)
|
|
}
|
|
if as := provider.SearchAPIs(); len(as) != 1 || as[0] != (provider.SearchAPI{Vendor: "tavily", Key: "tvly-1", URL: "https://other.example.com"}) {
|
|
t.Fatalf("%+v", as)
|
|
}
|
|
for _, bad := range []string{"ftp://x.example.com", "search.example.com", "https://x.example.com/?key=1"} {
|
|
if rec := post(`{"vendor":"tavily","key":"tvly-1","url":"` + bad + `"}`); rec.Code < 400 {
|
|
t.Errorf("%s taken: %d", bad, rec.Code)
|
|
}
|
|
}
|
|
if as := provider.SearchAPIs(); as[0].URL != "https://other.example.com" {
|
|
t.Fatalf("a refused address changed it: %+v", as)
|
|
}
|
|
var s settingsJSON
|
|
searchState(&s)
|
|
if len(s.SearchAPIs) != 1 || s.SearchAPIs[0].URL != "https://other.example.com" {
|
|
t.Fatalf("the settings list %+v", s.SearchAPIs)
|
|
}
|
|
bases := map[string]string{}
|
|
for _, v := range s.SearchVendors {
|
|
bases[v.ID] = v.Base
|
|
}
|
|
if bases["tavily"] != "https://api.tavily.com" || bases["exa"] != "https://api.exa.ai" || bases["searxng"] != "" {
|
|
t.Fatalf("vendors' own addresses %v", bases)
|
|
}
|
|
}
|