Files
magpie/internal/provider/backup_test.go
T
yetone 9c54a560cc provider: a providers.json that can't be read is said where the providers are listed, never backed up or synced as none, and is replaced through a rename (PR #415's review) — #415 made edits refuse a file they couldn't read or decode, but every read for the user still took it as empty: magpie providers said "no providers yet", magpie models "no models yet", the Providers page opened the Add sheet as for a first use, and backup.Collect, so an export and a WebDAV/S3 sync, carried it as no providers, which another computer's Mirror would then take as every provider removed. magpie providers and magpie models now list what is left (the signed-in accounts) and end in the error, the TUI says it over its list, and the Providers page shows a "Your providers file can't be read" box (无法读取供应商配置文件) over the list with magpie's reason and no Add sheet; start-up, the gateway and the catalog still go on with it as empty. Stored and StoredGroups return the read error, so a backup and a sync stop on it with nothing pushed. The error, for reads and refused writes alike, names the file, says magpie left it unchanged and to fix it or move it aside (errors.Is ErrUnreadable). store writes through writePrivate, a 0600 temp file renamed over it (steady.Rename waits out a Windows reader holding it, a symlink keeps its target), instead of os.WriteFile's truncate-then-write, and the read goes through steady.ReadFile. TestFileErrorSaysUnreadable, TestWriteErrorsSayLeftUnchanged, TestStoredRefusesUnreadable, TestStoreReplacesAtomically, TestSyncStopsOnUnreadableProviders, TestProvidersCmdSaysUnreadable and provider-file-error.test.cjs fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; provider-file-error, click-scroll, old-webkit, providers-off-fold, add-sheet pass.
2026-10-01 19:45:24 +08:00

71 lines
2.5 KiB
Go

package provider
import (
"path/filepath"
"slices"
"testing"
)
// Restoring and syncing a keyless provider keep this machine's balance
// token. A token supplied explicitly, or keys supplied with none, replaces it.
func TestBackupBalanceToken(t *testing.T) {
for _, op := range []struct {
name string
put func([]Provider) error
}{
{"restore", func(ps []Provider) error { _, _, err := Restore(ps); return err }},
{"mirror", func(ps []Provider) error { return Mirror(ps, nil) }},
} {
t.Run(op.name, func(t *testing.T) {
for _, c := range []struct {
name string
incoming Provider
want string
}{
{"without keys", Provider{}, "balance-here"},
{"explicit token without keys", Provider{BalanceToken: "balance-new"}, "balance-new"},
{"primary key with token", Provider{Key: "sk-new", BalanceToken: "balance-new"}, "balance-new"},
{"primary key without token", Provider{Key: "sk-new"}, ""},
{"spare key with token", Provider{Keys: []KeyAccount{{Key: "sk-new"}}, BalanceToken: "balance-new"}, "balance-new"},
{"spare key without token", Provider{Keys: []KeyAccount{{Key: "sk-new"}}}, ""},
} {
t.Run(c.name, func(t *testing.T) {
isolate(t)
h := t.TempDir()
t.Setenv("HOME", h)
t.Setenv("USERPROFILE", h)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(h, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(h, ".cache"))
t.Setenv("PATH", h)
here := Provider{ID: "relay", Name: "Relay old", Chat: "https://old.example.com/v1",
Key: "sk-here", KeyName: "main", KeyProtocol: Chat,
Keys: []KeyAccount{{Name: "spare", Key: "sk-spare", Protocol: Anthropic}}, BalanceToken: "balance-here"}
if err := Save(here); err != nil {
t.Fatal(err)
}
p := c.incoming
p.ID, p.Name, p.Chat = "relay", "Relay new", "https://new.example.com/v1"
if err := op.put([]Provider{p}); err != nil {
t.Fatal(err)
}
ps, _ := Stored()
if len(ps) != 1 {
t.Fatalf("providers: %+v", ps)
}
got := ps[0]
if got.BalanceToken != c.want || got.Name != p.Name || got.Chat != p.Chat {
t.Fatalf("restored: %+v, want balance token %q and the incoming name and URL", got, c.want)
}
wantKeys := p
if p.Key == "" && len(p.Keys) == 0 {
wantKeys = here
}
if got.Key != wantKeys.Key || got.KeyName != wantKeys.KeyName || got.KeyProtocol != wantKeys.KeyProtocol || !slices.Equal(got.Keys, wantKeys.Keys) {
t.Fatalf("keys: %+v, want %+v", got, wantKeys)
}
})
}
})
}
}