Files
magpie/internal/provider/off_asked_nothing_test.go
T
yetone 4dfdd0cd63 provider: a provider switched off is asked for nothing, its plugin's model list included
ChangQiu on Discord switched a provider off long ago, and magpie still
reached it. Routing, fallback, usage and balance reads, warmups,
check-ins and keepalive were already held back (On(), leftAlone,
KeepListsFresh). These were not:

- The plugin host's providers listing ran every plugin's models hook,
  which asks the vendor, and its sign-in renewal (fresh), for every
  plugin provider, on or off. Go now passes the ids switched off
  (plugin.ListingOff, provider.pluginsOff: the provider's id and the
  plugin's own id, read from providers.json alone). host.js lists such
  an id without running a hook (info(..., quiet)), so its row stays and
  keepListed keeps the last list.
- Sync (POST /api/sync, `magpie sync`) fetched every ready provider's
  list. Both now go through provider.RefreshLists, which skips one
  switched off.
- Saving or switching on in the GUI fetched the list even for one left
  off; PrefillNewKeys asked one switched off too.

What still asks one switched off, by the user's own click: the TUI's
refetch, the provider Test button and `magpie provider models <id>`
refetch.

Tests: TestOffPluginProviderAskedNoModels (the fake plugin's models
endpoint counts its hits: 0 while off, the list kept, asked again once
switched on), TestPrefillAsksNoProviderSwitchedOff,
TestPluginsOffNamesThePluginsOwnIDs and
TestRefreshListsAsksNoProviderSwitchedOff.

Run on mote at this head (first at a4916aec1):
- go test -timeout 45m ./internal/provider ./internal/plugin
  ./internal/gui ./internal/cli ./internal/tui ./internal/gateway:
  provider, plugin and tui ok. Three reds, each also red on origin/main
  and filed: gui TestMainWindowCorners/macOS_26_SDK (#3949), cli
  TestEveryRouteHasACommandOrIsNamed (#4826), gateway
  TestCodexPoolSaysTheClientsVersion (#4807).
- After rebasing on ce2e81da1: provider and plugin again, plus darwin
  vet of the four packages. One provider run went red on
  TestClaudeAccountServesModelsListedSinceItsFetch (#3174, full-run
  only). It is green alone 3/3 on this head and on origin/main, and the
  next full provider run was ok on both.
- GOOS=windows go vet of plugin, provider, cli and gui: clean.
  gofmt -l: clean. Linux go vet (WSL magpie-desktop) of plugin and
  provider: clean. cli and gui can't be vetted there: the distro has no
  gtk4/webkitgtk-6.0 headers for wails' cgo.
Broken by hand: with each guard taken out, its test went red ("switched
off, the vendor was asked 2 times", "the vendor asked 2 times",
"pluginsOff = [fakeco onco openai openai-plugin]", "the provider
switched off was asked 1 times").
Not tried: a real plugin provider switched off in the app. Which
provider ChangQiu had switched off isn't known, so his exact case is
not reproduced.
2026-11-02 14:30:45 +08:00

101 lines
3.3 KiB
Go

package provider
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"slices"
"sync/atomic"
"testing"
)
// ChangQiu on Discord: a provider switched off long ago was still asked.
// A key added to a provider switched off isn't given its own list: the
// vendor is asked nothing.
func TestPrefillAsksNoProviderSwitchedOff(t *testing.T) {
prefillHome(t)
var hits atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
hits.Add(1)
_, _ = rw.Write([]byte(groupAll))
}))
t.Cleanup(srv.Close)
if err := Save(Provider{ID: "relay", Name: "Relay", Chat: srv.URL + "/v1", Key: "sk-all", Off: true,
Keys: []KeyAccount{{Key: "sk-new"}}}); err != nil {
t.Fatal(err)
}
if n := PrefillNewKeys(context.Background(), map[string]map[string]bool{}); n != 0 || hits.Load() != 0 {
t.Fatalf("switched off: %d keys given a list, the vendor asked %d times", n, hits.Load())
}
// the same provider on is asked: the test can tell
if err := SetOff("relay", false); err != nil {
t.Fatal(err)
}
PrefillNewKeys(context.Background(), map[string]map[string]bool{})
if hits.Load() == 0 {
t.Fatal("switched on, the vendor was never asked")
}
}
// The plugin providers a listing of the plugins asks nothing are those
// switched off in magpie, by the plugin's own id: a plugin's provider is
// magpie's as it is, or with "-plugin" taken off when the id is a
// built-in's. A built-in switched off is no plugin's.
func TestPluginsOffNamesThePluginsOwnIDs(t *testing.T) {
prefillHome(t)
// as providers.json keeps them: a plugin's provider has no base of its own
b, _ := json.Marshal(file{Providers: []Provider{
{ID: "fakeco", Name: "Fake", Off: true},
{ID: "openai-plugin", Name: "OpenAI (plugin)", Off: true},
{ID: "onco", Name: "On"},
{ID: "deepseek", Name: "DeepSeek", Key: "sk-d", Off: true},
}})
if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(Path(), b, 0o600); err != nil {
t.Fatal(err)
}
got := pluginsOff()
slices.Sort(got)
// "openai-plugin" is both: a plugin's own "openai-plugin" is magpie's
// as it is, as its "openai" is magpie's "openai-plugin"
if want := []string{"fakeco", "openai", "openai-plugin"}; !slices.Equal(got, want) {
t.Fatalf("pluginsOff = %v, want %v", got, want)
}
}
// Refreshing every list (the GUI's Refresh, `magpie sync`) asks no
// provider switched off, and keeps its list; one on is asked.
func TestRefreshListsAsksNoProviderSwitchedOff(t *testing.T) {
prefillHome(t)
var off, on atomic.Int32
vendor := func(n *atomic.Int32) *httptest.Server {
srv := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
n.Add(1)
_, _ = rw.Write([]byte(groupAll))
}))
t.Cleanup(srv.Close)
return srv
}
offSrv, onSrv := vendor(&off), vendor(&on)
for _, p := range []Provider{
{ID: "gone", Name: "Gone", Chat: offSrv.URL + "/v1", Key: "sk-gone", Off: true},
{ID: "here", Name: "Here", Chat: onSrv.URL + "/v1", Key: "sk-here"},
} {
if err := Save(p); err != nil {
t.Fatal(err)
}
}
got := RefreshLists(context.Background())
if off.Load() != 0 {
t.Fatalf("the provider switched off was asked %d times", off.Load())
}
if on.Load() == 0 || !slices.Equal(got, []Listed{{"here", 3}}) {
t.Fatalf("RefreshLists = %v, the one on asked %d times", got, on.Load())
}
}