Files
magpie/internal/gui/provider_autolist_test.go
T
yetone ee4a7ab471 provider: a provider of your own can fetch its model list on a schedule and take newly listed models by rule (#2135, tianxia3111)
tianxia3111 runs a headless `magpie serve` with relays of 10–70 models.
A model the relay adds never reached the list until Fetch models was
clicked, and was then picked by hand.

- RefreshEvery (a Go duration or Nd, at least 10m): the gateway
  (KeepListsFresh, started beside KeepPluginsListed, so serve and the
  app) looks each minute and fetches a list once Fetched is that old,
  through the same Refetch as Fetch models, so dropped models leave
  the picks exactly as they do there. A failure waits min(every, 15m).
- AutoInclude (globs with *, or re: patterns, the same ones routing
  groups read; a plain id is refused): RefetchTaking adds a model the
  fetch newly lists that a rule matches. It starts from what is served
  now (Exposed), so nothing served is removed, and a model the user
  unticks stays out because it isn't new to the next fetch. Setting
  the rules takes the current list's matches. Rules only ever add.
- Only providers of the user's own take them: an account's list is
  fetched for it, and a plugin's by the plugin.
- Every save keeps them, the window's editor (/api/provider/save)
  among them, which has no fields for them yet.

Tests (each fails without the change, broken by hand and watched go
red: GUI keep line removed, takeMatching skipped, retry guard removed):
TestAutoIncludeTakesNewlyListedMatches, ...KeepsWhatIsServedWithNothingPicked,
...RefusesWhatIsNoRule, TestParseRefreshEvery, TestKeepListsFreshFetchesWhatIsDue,
TestSaveKeepsTheListsScheduleAndRules, gui TestProviderSaveKeepsListScheduleAndRules.

Run: gofmt -l; go vet ./... (darwin), GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...; go test internal/provider internal/gui
internal/gateway internal/cli with pipefail in a sandbox HOME.
Not tried against a real relay; the GUI editor fields are not added.
2026-10-14 16:40:23 +08:00

48 lines
1.5 KiB
Go

package gui
import (
"net/http"
"net/http/httptest"
"os"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// A provider's list schedule and auto-include rules (#2135) are set from
// the CLI; the editor's Save, which doesn't send them, keeps them.
func TestProviderSaveKeepsListScheduleAndRules(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())
mux := http.NewServeMux()
providerRoutes(mux, nil)
post := func(body string) {
t.Helper()
w := httptest.NewRecorder()
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/provider/save", strings.NewReader(body)))
if w.Code != 200 {
t.Fatalf("save: %d %s", w.Code, w.Body)
}
}
const u = "http://127.0.0.1:1/v1"
post(`{"id":"relay","name":"Relay","key":"sk-a","chat":"` + u + `","icon":"generic","new":true}`)
if err := provider.SetRefreshEvery("relay", "6h"); err != nil {
t.Fatal(err)
}
if _, err := provider.SetAutoInclude("relay", []string{"gpt-*"}); err != nil {
t.Fatal(err)
}
post(`{"id":"relay","from":"relay","name":"Relay 2","chat":"` + u + `","icon":"generic"}`)
p, err := provider.Find("relay")
if err != nil {
t.Fatal(err)
}
if p.Name != "Relay 2" || p.RefreshEvery != "6h" || !slices.Equal(p.AutoInclude, []string{"gpt-*"}) {
t.Fatalf("after the editor's save: name %q, every %q, rules %v", p.Name, p.RefreshEvery, p.AutoInclude)
}
}