Files
magpie/internal/gui/search_key_test.go
T
yetone 47eb27296f settings: a search API's saved key can be shown, hidden and copied in its row, as a provider's can (OnurBen on Discord: the Providers page shows a key, Settings → Models → Search APIs only listed Firecrawl/Tavily/Exa keys masked, so copying one meant going back to the vendor's site)
Each saved search API with a key gets Show and Copy next to Remove. Show asks the new POST /api/settings/search-key for the key (served on loopback, as provider/key is) and puts it in the row's line in place of the masked one, selectable; Hide puts the masked one back. Copy copies the key without showing it. The settings still list keys masked. search-api.test.cjs checks Show/Hide/Copy (fails on the old app.js) and TestSearchKeyShown the endpoint. GUI: search-api, searcher-pick, click-scroll pass on WebKit; go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 17:29:11 +08:00

45 lines
1.4 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)
}
}