Files
magpie/provider_refresh_cli_test.go
yetone e3bd766fae providers: a saved provider's editor keeps the add form's Fetch models button, "Fetch models again" once a list was fetched, to re-fetch after a key moves to another group (耍赖天都爱 on Discord)
耍赖天都爱 on Discord: 「provider中模型拉取一次就没有这个按钮了 能不能加上 有
的provider换一个分组要从新拉取模型」. The add form offers "Fetch models"
(获取模型); once saved, the editor's same action was a generic "Refresh"
(刷新) in the models footer, so after the first fetch the button looked
gone, while a relay key moved to another group serves another list.

The editor's button now says "Fetch models", and "Fetch models again"
(重新获取模型) when a vendor list was fetched before. Its title says it asks
with the key typed here and what happens to the picks. The fetch itself is
unchanged: it sends the typed form (asTyped), and Refetch replaces the list,
drops picks the new list no longer has and keeps ids added by hand. The
hints that named Refresh ("from models.dev · …", "magpie's list · …",
"Jev's names · …", Forget's title, the empty-list hint) name the button now,
in zh, zh-TW, ja and de. The TUI's "m fetch models" and `magpie provider
refresh` already re-fetch at any time; the CLI help names the app button.

Verified:
- new internal/gui/tests/provider-fetch-again.test.cjs (Chromium + WebKit;
  en and zh at 900 and 420px, zh-TW and ja at 900, de at 420): a never
  fetched relay shows Fetch models and its hint names it; a fetched one
  shows Fetch models again; with group B's key typed it asks with that key,
  shows group B's list, drops the pick B lacks, keeps an id added by hand,
  the button is still there and asks again; nothing scrolls. On the old
  app.js/i18n.js it fails: "waiting for ... getByRole('button', { name:
  'Fetch models', exact: true }) to be visible" (en), "again has its zh".
- provider-typed, provider-picks-stay (labels updated), gui-zh-tw, gui-ja,
  gui-de, click-scroll: 51 pass, 0 fail, both engines.
- go test -tags nogui . ./internal/tui ./internal/gui under a temp HOME: ok.
  go vet -tags nogui . ./internal/tui, GOOS=linux and GOOS=windows: ok.
2026-10-10 10:13:43 +08:00

62 lines
1.9 KiB
Go

package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"reflect"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// magpie provider refresh <id> fetches a provider's list again, as the
// app editor's Fetch models does: a pick the new list no longer has is dropped
// (akic404 on Discord: the TUI and CLI had no way to fetch a provider's
// models after it was added).
func TestProviderRefreshCmd(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
t.Setenv("DSH_HOME", "")
var mu sync.Mutex
ids := []string{"alpha", "beta"}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
defer mu.Unlock()
var data []map[string]any
for _, id := range ids {
data = append(data, map[string]any{"id": id, "object": "model"})
}
json.NewEncoder(w).Encode(map[string]any{"object": "list", "data": data})
}))
defer srv.Close()
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: srv.URL + "/v1", Models: []string{"alpha", "beta"}}); err != nil {
t.Fatal(err)
}
if err := providerCmd([]string{"provider", "refresh", "relay"}); err != nil {
t.Fatal(err)
}
if _, ok := mustFind(t, "relay").Fetched(); !ok {
t.Fatal("refresh fetched no list")
}
mu.Lock()
ids = []string{"alpha", "gamma"}
mu.Unlock()
if err := providerCmd([]string{"provider", "refresh", "relay"}); err != nil {
t.Fatal(err)
}
if got := mustFind(t, "relay").Models; !reflect.DeepEqual(got, []string{"alpha"}) {
t.Fatalf("picks after the list lost beta: %v", got)
}
if err := providerCmd([]string{"provider", "refresh", "relay", "alpha"}); err == nil {
t.Fatal("refresh took model ids to pick")
}
if err := providerCmd([]string{"provider", "refresh"}); err == nil {
t.Fatal("refresh without an id")
}
}