Files
magpie/internal/provider/stored_id_test.go
T
yetone 11ec99de71 providers: a provider whose id isn't one magpie would make (b.ai, put in providers.json by hand) saves, and its editor's ID renames it, where every Save said "provider id must be lowercase letters, digits and dashes"
01huadalang on Discord (screenshot): 我不管改成什么都显示不能用 b.ai,我没用 b.ai
做名字和 id. The provider was stored under the id "b.ai". The editor's Save
first saves the rest under the id the provider has, then renames it, and
provider.Save refused any id that isn't a slug, stored or not, so the
provider could be neither changed nor renamed to one that is right. An id
providers.json has already is now saved as it is; a new provider still
can't be given one (Add, imports, backups and the rename's target are
checked as before).

Verified: TestStoredOddIDSavedAndRenamed (stored b.ai saved, renamed to bai
with key and URL kept, a new c.ai refused) fails without the change. In a
sandbox HOME with the built binary and the real GUI: a provider stored as
"b.ai", ID typed as "bai" and Save showed the reporter's exact error on a build without
this change (Save as on origin/main); with it the editor closes and providers.json has
id bai, was ["b.ai"].
2026-10-07 06:14:20 +08:00

46 lines
1.4 KiB
Go

package provider
import (
"os"
"path/filepath"
"testing"
)
// A provider whose id was put in providers.json by hand, not one magpie
// would make ("b.ai"), is saved as it is and renamed to one that is
// right; a new provider still can't be given such an id (01huadalang on
// Discord: 我不管改成什么都显示不能用 b.ai).
func TestStoredOddIDSavedAndRenamed(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"))
if err := os.MkdirAll(filepath.Dir(Path()), 0o700); err != nil {
t.Fatal(err)
}
raw := `{"providers":[{"id":"b.ai","name":"b.ai","responses":"https://api.b.ai/v1","key":"sk-bai"}]}`
if err := os.WriteFile(Path(), []byte(raw), 0o600); err != nil {
t.Fatal(err)
}
p, err := Find("b.ai")
if err != nil {
t.Fatal(err)
}
p.Name = "bai"
if err := Save(*p); err != nil {
t.Fatalf("saving the stored b.ai: %v", err)
}
if err := Rename("b.ai", "bai"); err != nil {
t.Fatalf("renaming b.ai: %v", err)
}
q, err := Find("bai")
if err != nil || q.ID != "bai" || q.Name != "bai" || q.Key != "sk-bai" || q.Responses != "https://api.b.ai/v1" {
t.Fatalf("after the rename: %+v, %v", q, err)
}
if err := Save(Provider{ID: "c.ai", Name: "c", Chat: "https://c.example/v1", Key: "sk-c"}); err == nil {
t.Fatal("a new provider was saved under the id c.ai")
}
}