Files
magpie/provider_refresh_cli_test.go
T
yetone 2719dd6ac3 tui, cli: adding a provider says how many models its list had, or why it had none, and m (magpie provider refresh <id>) fetches the list again (akic404 on Discord: 还是tui cli问题,没有获取上游模型功能,新建之后模型数是0)
The TUI's add fetched the new provider's list and dropped any error, so a remote magpie that couldn't be listed (wrong address, the other magpie not shared, a gateway key it refuses) showed just "added Remote magpie" and 0 models, and the Providers page had no way to ask again: t tests the APIs, the models picker said "t tests it, which asks the vendor for them", which it doesn't. Now the add says "added X · N models from its list · M for agents (↵ picks)", or "added X · no model list · <the first URL's answer> · m asks again" in red; m on the Providers page is the app editor's Refresh (Refetch: picks the new list no longer has are dropped and named), with the same note; the footer lists it and the empty picker points at it. The CLI already said a failed list on add and fetched with provider models <id>; provider refresh <id> (and fetch) is that by its GUI name, and models <id> now goes through Refetch too, naming dropped picks. The ids were never names: a remote magpie's are <its id>/<the other magpie's provider id>/<model> (remote-magpie/custom/qwen3.6-max); the Chinese is the label, "qwen3.6-max · 中转站", the other magpie's provider name it sends as magpie_label, which magpie models and the TUI's picker show beside the id. README says so.

TestTUIFetchesAProvidersModels (add over a fake magpie: 2 models; its list grows, m: 3; it refuses, m: the error and the 3 kept) and TestProviderRefreshCmd fail on the old code; TestTUIAddsARemoteMagpieWithItsAddress now expects the error at a dead address. End to end with two sandbox magpies (temp HOMEs, 3461/3462, a fake upstream on 3463): A with "中转站" over the upstream (id custom), B's TUI in tmux adding A at 127.0.0.1:3461: "added Remote magpie · 2 models from its list · 2 for agents", row 2 models, m again the same; a second at 127.0.0.1:3464: "added Remote magpie 2 · no model list · … connection refused · m asks again", 0 models; provider refresh remote-magpie lists remote-magpie/custom/qwen3.6-max "qwen3.6-max · 中转站". go vet, builds (darwin, GOOS=linux, GOOS=windows) and go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-04 01:24:47 +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 Refresh 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")
}
}