Files
magpie/internal/davsync/search_api_test.go
T
yetone c8eb7b3877 search: a search API's keys are a list, each switched off, moved and removed on its own
Web search took one key per API in one field (辰林鸭~ on Discord: only one
Tavily key). Settings › Models › Web search now lists an API's keys under
it as a provider's accounts are: each masked on its own row with a tick
(off / on), Show, Copy and Remove; several pasted at once (commas,
semicolons, lines), the reply saying how many were new and how many it
had; the order changed by dragging or Alt+↑/↓. A resting key says why and
until when on its row ("asked last until 14:05: rate limited"), from the
gateway's rest (SearchKeyRest, SearchQuota.Ref); rotation on 401/403,
402/432/433 and 429 (#1477) is unchanged.

provider.SearchAPI keeps Key as the keys on, comma-joined, so the gateway
and an older magpie never ask one switched off; keyList is written only
while a key is off. A Key that differs from the list (an older magpie, a
sync) wins, its keys on in its order, the list's off keys after. The last
key on can't be switched off or removed. The GUI names a key by
SearchKeyRef (a hash): POST /api/settings/search-keys (add, ref+on,
ref+remove, arrange) and /api/settings/search-key with ref. Saving an API
with no key keeps its keys, off ones too. Backup and WebDAV sync carry
the off keys, or leave them out with the rest. `magpie search` says
"+N off". docs/subsystems/gateway-routing.md has "The keys as a list".

Run (mote, at this tree, base 3441035a2):
- go test -count=1 ./internal/gui ./internal/provider ./internal/backup
  ./internal/davsync ./internal/cli: ok but gui's
  TestPanelHiddenByWailsMarksPageOutOfSight (#5322, mote, untouched) and
  cli's TestEveryRouteHasACommandOrIsNamed, then fixed (search-keys named
  S2) and run again: ok. ./internal/gateway search tests ok earlier.
- node --test search-key-list, search-keys, search-api, search-signs-in,
  search-order: 84/84, Chromium and WebKit, en/zh/zh-TW/ja/de, 1100 and
  420px (search-api/order at their own widths). i18n-keys: only "a
  plugin's words are its own" red, as on main (#5332, #5426, #5482).
- go build ./..., go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. gofmt -l: clean.

Broken by hand, each red then restored:
- SetSearchAPI drops KeyList: provider and gui tests red.
- backup clears Key only / davsync merges Key only: their tests red.
- off keys still put in Key: provider, gui, backup and gateway red.
- the whole key in /api/state: TestSearchKeyListFromSettings red.
- no last-key-on check: TestSearchKeyList red.
- settings.js at HEAD: search-key-list.test.cjs red.

Not tried: a real Tavily account with several keys (the vendor is faked;
no test reaches the network); the real app window (the GUI ran in
Playwright only); Windows and Linux runs of the tests (vet only). The
full Go and GUI suites run after the push, under mote's suite lock.

Reported-by: 辰林鸭~ (Discord)
2026-11-07 21:21:48 +08:00

63 lines
2.4 KiB
Go

package davsync
import (
"slices"
"testing"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/provider"
)
// The search APIs (#419) sync with the providers: sent without keys, the
// server's keys stay; a change to them is a change to the providers; with
// none, the providers' hash is what it was before them.
func TestSyncSearchAPIs(t *testing.T) {
server := backup.Bundle{Keys: true, Searches: &[]provider.SearchAPI{{Vendor: "tavily", Key: "tvly-k"}}}
sent := backup.Bundle{Searches: &[]provider.SearchAPI{{Vendor: "tavily"}, {Vendor: "brave"}}}
take(&server, sent, "providers")
if s := *server.Searches; len(s) != 2 || s[0].Key != "tvly-k" || s[1].Key != "" {
t.Fatalf("taken %+v", s)
}
before := backup.Bundle{Providers: []provider.Provider{{ID: "a"}}}
none := before
none.Searches = &[]provider.SearchAPI{}
if hashes(before)["providers"] != hashes(none)["providers"] {
t.Error("no search API changed the providers' hash")
}
one := before
one.Searches = &[]provider.SearchAPI{{Vendor: "exa", Key: "k"}}
if hashes(before)["providers"] == hashes(one)["providers"] {
t.Error("a search API isn't a change")
}
// brought from a magpie before them, the ones here stay
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := provider.SetSearchAPI(provider.SearchAPI{Vendor: "exa", Key: "k"}); err != nil {
t.Fatal(err)
}
if err := bring(backup.Bundle{}, "providers"); err != nil {
t.Fatal(err)
}
if len(provider.StoredSearchAPIs()) != 1 {
t.Fatal("an older magpie's providers took them away")
}
if err := bring(backup.Bundle{Searches: &[]provider.SearchAPI{}}, "providers"); err != nil {
t.Fatal(err)
}
if len(provider.StoredSearchAPIs()) != 0 {
t.Fatal("removed elsewhere, kept here")
}
}
// Sent without keys, a search API keeps the server's keys switched off
// too (辰林鸭~ on Discord), not only the ones on.
func TestSyncSearchKeysOff(t *testing.T) {
list := []provider.SearchKey{{Key: "tvly-a", Off: true}, {Key: "tvly-b"}}
server := backup.Bundle{Keys: true, Searches: &[]provider.SearchAPI{{Vendor: "tavily", Key: "tvly-b", KeyList: list}}}
take(&server, backup.Bundle{Searches: &[]provider.SearchAPI{{Vendor: "tavily", Pick: provider.SearchPickOrder}}}, "providers")
if s := *server.Searches; len(s) != 1 || s[0].Pick != provider.SearchPickOrder || !slices.Equal(s[0].SearchKeys(), list) {
t.Fatalf("taken %+v", s)
}
}