Files
magpie/internal/provider/listing_kept_test.go
T
yetone f598edf6d5 providers: a provider's model lists are kept across polls with the catalog
The Providers page's state built each provider's Available,
ExposedOf(Available) and NewestInFamily(Available) lists again on every
poll. Over twenty polls of the real-account sandbox that was 159 MB of
the 371 MB providerInfoWith allocated (Available 72, NewestInFamily 60,
ExposedOf 27). provider.KeptListing keeps the three lists in keptOf, the
cross-request cache that drops them when the catalog, providers.json,
settings or a plugin changes (StampNow) and after keptFor, and only while
a request holds. providersState passes the kept Listing; providerInfo,
for one provider after an edit, still builds a fresh one. The lists are
clipped, so an append makes the caller's own.

TestProvidersListingKeptAcrossPolls: twenty polls with nothing changed
build the listing 0 times and match a fresh Listing; after a Save of the
provider's models the next poll shows the new ones. With keptOf swapped
for a direct call by hand it is red: "built the listing 20 times, want 0".

Run on mote at this tree (on 2597c9636): gofmt; go vet for darwin,
windows and linux (provider); internal/provider ok; internal/gui has
TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and TestMainWindowCorners
(#3949) red, both red on main before this.
Not run: the full suite at the rebased head.
2026-11-01 04:16:57 +08:00

61 lines
2.0 KiB
Go

package provider
import (
"path/filepath"
"slices"
"testing"
"time"
)
// The Providers page's polls read each provider's models from a Listing
// kept with the catalog while nothing it is built from moved, not built
// again on each poll; a provider saved has it built again, and the next
// poll draws what was saved.
func TestProvidersListingKeptAcrossPolls(t *testing.T) {
isolate(t)
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
defer func(d, k time.Duration) { heldFor, keptFor = d, k }(heldFor, keptFor)
heldFor = 0 // each poll after the last one's hold has gone
keptFor = time.Minute // a loaded machine's pause isn't the bound
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m1", "m2"}}); err != nil {
t.Fatal(err)
}
poll := func() []string {
defer Hold()()
var ids []string
for _, p := range All() {
if p.ID != "relay" {
continue
}
l := KeptListing(p)
for _, m := range l.Exposed {
ids = append(ids, m.ID)
}
if fresh := p.Listing(); len(fresh.Exposed) != len(l.Exposed) || len(fresh.Available) != len(l.Available) || len(fresh.Newest) != len(l.Newest) {
t.Fatalf("kept %d/%d/%d models, built afresh %d/%d/%d", len(l.Available), len(l.Exposed), len(l.Newest), len(fresh.Available), len(fresh.Exposed), len(fresh.Newest))
}
}
return ids
}
if got := poll(); !slices.Equal(got, []string{"m1", "m2"}) {
t.Fatalf("exposed %v", got)
}
before := listingsBuilt.Load()
for range 20 {
poll()
}
if n := listingsBuilt.Load() - before; n != 0 {
t.Fatalf("20 polls with nothing changed built the listing %d times, want 0", n)
}
if err := Save(Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"m3"}}); err != nil {
t.Fatal(err)
}
if got := poll(); !slices.Equal(got, []string{"m3"}) {
t.Fatalf("after a save the poll draws %v, want [m3]", got)
}
}