Files
magpie/internal/provider/picks_legacy_test.go
T
yetone 952309abcf provider: a provider of keys' picks saved before PickedFrom are served as they are (yuancjun)
yuancjun (#help on Discord, magpie web on Linux at v0.1.2679) found that
the provider editor saved every model when one was picked, and sent the
fix from their fork (yuancjun/magpie, fix/legacy-picks-serve-all,
2d82267d2). Ported here, with the Save side added.

withNewlyListed reads picks saved before PickedFrom was kept as "held
every model" when they hold every model released up to the newest pick,
and adds every model released after it. A pick of one OLD model reads
the same way, so every newer model was served beside it, the editor
showed them all as picked, and the next Save wrote them all down. With
10 models released 2026-01..10 and "models":["m0"] with no "pickedFrom",
Exposed() was 10. PickedFrom is nil too for a provider whose picks were
saved before its list was fetched (an import), not only for old ones.

- A provider of keys (no Account) with no PickedFrom: its picks are
  served as they are (yuancjun's change). A subscription keeps the date
  rule: its list is the catalog's and Select all is a state its editor
  offers (TestClaudePicksOfEveryModelTakeOnesListedSince).
- listedWith: such a provider's next Save keeps the list with its
  picks, the same picks too (it used to keep was.PickedFrom, nil, so a
  key provider whose every model was picked before PickedFrom would
  never be given a model listed later). A subscription's same-picks
  Save still keeps what it had.

Every path that writes picks reads them through Picks(): the GUI editor
(PicksOf in /api/providers' chosen), the TUI's model toggles
(pages.go). The CLI's `provider set models=` and the importers write the
ids given, and their Save goes through listedWith.

Users whose picks were already saved as every model can't be told from
a real Select all: those picks have PickedFrom kept and are served as
saved. They unpick in the editor (Picked only, or Select none, then the
models wanted). A provider that showed every model but was never saved
since is back to its own picks at once.

TestLegacyPicksServeWhatWasPicked (yuancjun's, plus a Save as the editor
does it) and TestLegacyPicksOfEveryModelGetPickedFromAtSave.

Run on mote at this commit: go test ./internal/provider ./internal/tui
ok; go vet GOOS=linux and GOOS=windows ./internal/provider ok; gofmt -l
clean. Broken by hand: the Account==nil branch off, the first test is
red with all 10 served [m1..m9 m0]; the listedWith change off, both
tests are red (PickedFrom [] after the Save; m3 not added).
Not tried: a real relay with a real providers.json from before
PickedFrom.
2026-11-02 22:28:57 +08:00

135 lines
4.3 KiB
Go

package provider
// A provider saved before PickedFrom was kept: its picks are taken as they
// are. Read as "everything released after the newest pick", a single pick
// of an older model served every newer one, and the next Save wrote them
// all down as the user's picks — a relay's 3 models all picked after one
// was clicked, and "no matter how I save, it saves every model".
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
func TestLegacyPicksServeWhatWasPicked(t *testing.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"))
const chat = "http://127.0.0.1:1/v1"
var ms []catalog.Model
for i := range 10 {
ms = append(ms, catalog.Model{ID: fmt.Sprintf("m%d", i), Released: fmt.Sprintf("2026-%02d-01", i+1)})
}
if err := catalog.SaveLive("relay", chat, ms); err != nil {
t.Fatal(err)
}
// the state of a provider saved before PickedFrom: one old pick, no
// PickedFrom, the list fetched since
dir := filepath.Join(home, ".config", "magpie")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
b, _ := json.Marshal(map[string]any{"providers": []map[string]any{{
"id": "relay", "name": "Relay", "key": "k", "chat": chat,
"models": []string{"m0"},
}}})
if err := os.WriteFile(filepath.Join(dir, "providers.json"), b, 0o644); err != nil {
t.Fatal(err)
}
p, err := Find("relay")
if err != nil {
t.Fatal(err)
}
served := func(p *Provider) []string {
var ids []string
for _, m := range p.Exposed() {
ids = append(ids, m.ID)
}
return ids
}
if got := served(p); !slices.Equal(got, []string{"m0"}) {
t.Fatalf("picked m0 alone, %d models served: %v", len(got), got)
}
if got := p.Picks(); !slices.Equal(got, []string{"m0"}) {
t.Fatalf("picked m0 alone, picks = %v", got)
}
// Save as an editor does, from the picks it was shown: m0 is still
// all that is picked, and all that is served
p.Models = p.Picks()
if err := Save(*p); err != nil {
t.Fatal(err)
}
q, err := Find("relay")
if err != nil {
t.Fatal(err)
}
if !slices.Equal(q.Models, []string{"m0"}) || !slices.Equal(served(q), []string{"m0"}) {
t.Fatalf("after a Save: picks %v, served %v", q.Models, served(q))
}
// and the Save kept what was listed with them: from now on the
// provider is read as any saved since PickedFrom was kept
if len(q.PickedFrom) != len(ms) {
t.Fatalf("listed with the picks after a Save: %v", q.PickedFrom)
}
}
// A provider of keys whose every model was picked before PickedFrom was
// kept is served those picks alone until its next Save, which keeps the
// list with them; a model the vendor lists after that Save is added.
func TestLegacyPicksOfEveryModelGetPickedFromAtSave(t *testing.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"))
const chat = "http://127.0.0.1:1/v1"
var ms []catalog.Model
var all []string
for i := range 3 {
ms = append(ms, catalog.Model{ID: fmt.Sprintf("m%d", i), Released: fmt.Sprintf("2026-%02d-01", i+1)})
all = append(all, fmt.Sprintf("m%d", i))
}
if err := catalog.SaveLive("relay", chat, ms); err != nil {
t.Fatal(err)
}
dir := filepath.Join(home, ".config", "magpie")
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatal(err)
}
b, _ := json.Marshal(map[string]any{"providers": []map[string]any{{
"id": "relay", "name": "Relay", "key": "k", "chat": chat, "models": all,
}}})
if err := os.WriteFile(filepath.Join(dir, "providers.json"), b, 0o644); err != nil {
t.Fatal(err)
}
p, err := Find("relay")
if err != nil {
t.Fatal(err)
}
// Save with the same picks, as an editor left as it was does
p.Models = p.Picks()
if err := Save(*p); err != nil {
t.Fatal(err)
}
if err := catalog.SaveLive("relay", chat, append([]catalog.Model{{ID: "m3", Released: "2026-04-01"}}, ms...)); err != nil {
t.Fatal(err)
}
q, err := Find("relay")
if err != nil {
t.Fatal(err)
}
if got := q.Picks(); !slices.Equal(got, append([]string{"m3"}, all...)) {
t.Fatalf("m3 listed after the Save: picks %v (listed with them %v)", got, q.PickedFrom)
}
}