Files
magpie/internal/gui/provider_models_order_test.go
T
yetone 616405061a gui: a provider row's models and agents are made once at their size
providerInfoWith grew a row's model list by append for the decision
models the vendor list lacks and for the picks it lacked, then grew it
again with slices.Insert to put those picks in front, and grew Agents
one append at a time. The rows are now counted first and each list is
made once at its size, in the same order as before.

Real accounts (sandbox copy of the owner's HOME, network blocked),
40 polls of /api/state, /api/providers and drift, pprof alloc_space
between the two heap snapshots, v0.1.2484 (mhost) vs this (mrows):
  providerInfoWith flat 50.7 MB -> 38.5 MB, cum 164.9 MB -> 138.8 MB
  providersState cum 273.2 MB -> 270.5 MB (the total is noisy +-25 MB
  from the state encoder's pooled buffers: 621 vs 637 MB)
  /api/state and drift bodies byte-identical; /api/providers equal
  but for `fetched` and one login's `lapsed`, set by a refused refresh
  in the blocked sandbox when a token came due during one of the runs
  (logins_on.go), a path this change doesn't touch.

TestProviderRowListsMadeAtTheirSize: 107 rows from 100 listed models,
7 picks the list lacks plus one it has, 3 agents; picks in front in
their order, cap == len for Models and Agents. On the old
providers.go (at HEAD) it fails: "models made for 219, 107 rows".
Forcing growth by hand in the new code fails it the same way.

Run on mote at this tree: gofmt, go vet, GOOS=windows go vet ok; the
new test and the model-order tests 3/3 ok; go test ./internal/gui:
only the known reds TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners (#3949) failed.
Not run: the full ./... suite.
2026-11-01 06:01:05 +08:00

71 lines
2.6 KiB
Go

package gui
import (
"fmt"
"os"
"slices"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/provider"
)
// Picks the vendor's list doesn't know are shown first, the last one
// picked foremost, then the listed models in the list's order.
func TestUnlistedPicksShownFirst(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())
p := provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.example/v1"}
ms := func(ids ...string) (out []catalog.Model) {
for _, id := range ids {
out = append(out, catalog.Model{ID: id})
}
return
}
info := providerInfoWith(p, provider.Listing{Available: ms("a", "b"), Exposed: ms("a", "x", "y", "z")}, nil, nil)
var ids []string
for _, m := range info.Models {
ids = append(ids, m.ID)
}
if want := []string{"z", "y", "x", "a", "b"}; !slices.Equal(ids, want) {
t.Fatalf("models shown as %v, want %v", ids, want)
}
if info.Exposed != 4 {
t.Fatalf("%d exposed, want 4", info.Exposed)
}
}
// A row's models and agents are each made once, at their size: grown by
// append for the picks the list lacks and again to put them in front, it
// was 8 MB of what twenty polls of the Providers page allocated with real
// accounts.
func TestProviderRowListsMadeAtTheirSize(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())
p := provider.Provider{ID: "relay", Name: "Relay", Chat: "https://relay.example/v1"}
var avail, exposed []catalog.Model
for i := range 100 {
avail = append(avail, catalog.Model{ID: fmt.Sprintf("m-%d", i)})
}
for i := range 7 {
exposed = append(exposed, catalog.Model{ID: fmt.Sprintf("pick-%d", i)})
}
exposed = append(exposed, avail[3])
agents := []agentUse{{Agent: &agent.Agent{ID: "a"}}, {Agent: &agent.Agent{ID: "b"}}, {Agent: &agent.Agent{ID: "c"}}}
info := providerInfoWith(p, provider.Listing{Available: avail, Exposed: exposed}, agents, nil)
if len(info.Models) != 107 || info.Models[0].ID != "pick-6" || info.Models[7].ID != "m-0" {
t.Fatalf("%d models, first %q, eighth %q", len(info.Models), info.Models[0].ID, info.Models[7].ID)
}
if cap(info.Models) != len(info.Models) {
t.Fatalf("models made for %d, %d rows: grown on the way", cap(info.Models), len(info.Models))
}
if len(info.Agents) != 3 || cap(info.Agents) != 3 {
t.Fatalf("agents made for %d, %d rows", cap(info.Agents), len(info.Agents))
}
}