Files
magpie/internal/provider/hidden_parsed_once_test.go
T
yetone 83b2be2b25 provider: Hidden reads providers.json as parsed once, not once per account
Hidden lists the signed-in accounts the user removed from magpie. The
Providers page asks it on every load, and for each signed-in account it
parsed the whole providers.json again (load). With the owner's real
config that was 68 MB of the 1.4 GB a main magpie allocated over 20
window polls. It now reads storedFile(), the file as parsed once until
it changes, which only reads it; Excluded, its other caller, does too.

TestHiddenParsedOnceUntilTheFileChanges: a signed-in Claude account
hidden beside 30 providers of 100 models, the file dated an hour back.
Hidden costs less than a quarter of a parse over what Accounts costs,
and an account shown again since is seen at once. Broken by hand on
mote (load().Providers in the loop again): red, 3353 allocations over
Accounts' 396 against a parse's 3352.

Run on mote at 1f9f74d1c (go test -timeout 45m): internal/provider ok
171s; internal/gui FAIL on TestSettingsSaveKeepsWhatItDoesNotSend (#4514)
and TestMainWindowCorners (#3949), both red on main already. go vet of
provider darwin, windows and linux ok, of gui darwin and windows ok
(gui's linux vet needs cgo for wails); gofmt -l clean.

Rebased on 80bcef18f: internal/provider ok 202s at that head.
2026-11-01 03:23:52 +08:00

49 lines
1.5 KiB
Go

package provider
import (
"fmt"
"os"
"testing"
"time"
)
// Hidden reads providers.json as parsed once until it changes, not a parse
// for each signed-in account: the Providers page asks it on every load. A
// save since is seen at once.
func TestHiddenParsedOnceUntilTheFileChanges(t *testing.T) {
home := claudeHome(t)
claudeSignIn(t, home, time.Now().Add(24*time.Hour))
var models []string
for i := range 100 {
models = append(models, fmt.Sprintf("m-%d", i))
}
save := func(hidden bool) {
saved := []map[string]any{{"id": "claude", "hidden": hidden}}
for i := range 30 {
id := fmt.Sprintf("r%d", i)
saved = append(saved, map[string]any{"id": id, "name": id, "key": "k", "chat": "http://127.0.0.1:1/v1", "models": models})
}
writeFile(t, Path(), map[string]any{"providers": saved})
}
save(true)
// written long enough ago that its time tells a later write apart
old := time.Now().Add(-time.Hour)
if err := os.Chtimes(Path(), old, old); err != nil {
t.Fatal(err)
}
if h := Hidden(); len(h) != 1 || h[0].ID != "claude" {
t.Fatalf("Hidden = %+v", h)
}
parsed := testing.AllocsPerRun(5, func() { load() })
accounts := testing.AllocsPerRun(10, func() { Accounts() })
hidden := testing.AllocsPerRun(10, func() { Hidden() })
if hidden-accounts > parsed/4 {
t.Errorf("Hidden with the file unchanged: %.0f allocations over its accounts' %.0f, a parse %.0f", hidden-accounts, accounts, parsed)
}
save(false)
if h := Hidden(); len(h) != 0 {
t.Fatalf("an account shown again since is still hidden: %+v", h)
}
}