Files
magpie/internal/provider/migrate_movers_test.go
T
yetone 3bec6c0e40 providers: a built-in subscription moves onto its community plugin and back, its accounts with it (the user: 慢慢下掉内置的那些 Subscription Provider…不要让用户一升级完之后,突然发现他的所有服务都不可用了) — eleven built-ins (Zed, Qoder, Factory, MiMo, Devin, Grok, Command Code Plan, Cursor, Kiro, WorkBuddy, ZCode) each get a mover to their @magpie-community package. A move installs the plugin (updated when older than the mover needs), hands it every account in the plugin's shape, checks the plugin lists models for them, and only then lets the plugin serve the same provider id, so agents' model ids don't change; any failure rolls back and the built-in keeps serving. The built-in's accounts are kept as a backup, and "Run built-in again" (magpie plugin move-back <id>) takes the plugin's current sign-ins back, never copying a rotating token: they are taken out of the plugin in one step, and an account the built-in renewed meanwhile keeps its newer token. An account only the plugin can hold (Cursor's browser sign-in) stays with it. The provider editor shows "Runs on" with the button, a failed move's reason, and the CLI has magpie plugin move / move-back. Automatic moves (Retiring) are wired in but the list stays empty until the packages are on npm. Checked with real accounts in a sandbox HOME: Command Code Plan and Devin moved, answered a chat through the gateway on the plugin, moved back and answered again on the built-in. TestMove*, TestPutBackKeepsNewer, the movers' round-trip tests and plugin-move.test.cjs (Chromium, WebKit, en/zh) are new. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit pass.
2026-09-30 21:21:58 +08:00

138 lines
4.9 KiB
Go

package provider
import (
"encoding/json"
"reflect"
"testing"
"time"
"github.com/yetone/magpie/internal/qoder"
)
// roundTrip moves id's saved accounts out as the plugin keeps them and back
// again, through JSON as plugin-auth.json has them: each account's sign-in
// comes back as it was, the one in use first and the rest on or off as
// they were. Put back where nothing is saved, each is an account again.
func roundTrip(t *testing.T, id string, saved []savedLogin, same func(t *testing.T, a, b savedLogin)) {
t.Helper()
claudeHome(t)
mv := movers[id]
loginsMu.Lock()
if err := writeLogins(slicesClone(saved)); err != nil {
t.Fatal(err)
}
loginsMu.Unlock()
accts, err := mv.out()
if err != nil || len(accts) != len(saved) {
t.Fatalf("%s out: %d accounts, %v", id, len(accts), err)
}
for i, a := range accts {
if a.User != saved[i].User || a.First != saved[i].First || a.On != (saved[i].On || saved[i].First) || a.Own {
t.Fatalf("%s account %d: %+v", id, i, a)
}
// as the host writes it and Go reads it back
var auth map[string]any
if err := json.Unmarshal([]byte(jsonText(a.Auth)), &auth); err != nil {
t.Fatal(err)
}
accts[i].Auth = auth
}
for _, fresh := range []bool{false, true} {
ls := slicesClone(saved)
if fresh {
ls = nil
}
for i, a := range accts {
user := a.User
if fresh {
user = ""
}
next, u, err := mv.back(ls, user, a.Auth)
if err != nil {
t.Fatalf("%s back %s: %v", id, a.User, err)
}
ls = next
if u != saved[i].User {
t.Fatalf("%s back wrote %q, want %q", id, u, saved[i].User)
}
}
if len(ls) != len(saved) {
t.Fatalf("%s back (fresh %v): %d accounts", id, fresh, len(ls))
}
for i := range saved {
if ls[i].Lapsed != "" {
t.Fatalf("%s back left %s lapsed", id, ls[i].User)
}
same(t, saved[i], ls[i])
}
}
}
func slicesClone(ls []savedLogin) []savedLogin { return append([]savedLogin(nil), ls...) }
func authOf(t *testing.T, v any) json.RawMessage {
t.Helper()
b, err := json.Marshal(v)
if err != nil {
t.Fatal(err)
}
return b
}
func TestQoderMover(t *testing.T) {
c1 := qoder.Credential{UID: "u1", Email: "a@q.com", Name: "A", Token: "t1", RefreshToken: "r1", DeviceToken: "d1", DeviceRefresh: "dr1", ExpiresAt: 1_900_000_000_123, MachineID: "m1", Models: json.RawMessage(`[{"id":"x"}]`)}
c2 := qoder.Credential{UID: "u2", Token: "t2", RefreshToken: "r2", DeviceToken: "d2", ExpiresAt: 1_900_000_000_456, MachineID: "m2"}
roundTrip(t, "qoder", []savedLogin{
{Agent: "qoder", User: "a@q.com", First: true, On: true, Auth: authOf(t, c1), Lapsed: "expired"},
{Agent: "qoder", User: "u2", Auth: authOf(t, c2)},
}, func(t *testing.T, a, b savedLogin) {
ca, _ := qoderSaved(a)
cb, _ := qoderSaved(b)
if string(cb.Models) == "" {
cb.Models = ca.Models // a new entry: the model list is the built-in's own cache
}
if !reflect.DeepEqual(ca, cb) {
t.Fatalf("qoder %s: %+v, want %+v", a.User, cb, ca)
}
})
}
func TestFactoryMover(t *testing.T) {
c1 := factoryCreds{Access: "a1", Refresh: "r1", ExpiresAt: 1_900_000_000_000, Org: "org_1", Active: "fo1", Email: "a@f.com", UserID: "user_1", Region: "eu", Prem: "llm.example.com"}
c2 := factoryCreds{Access: "a2", Refresh: "r2", ExpiresAt: 1_900_000_000_001, UserID: "user_2"}
roundTrip(t, "factory", []savedLogin{
{Agent: "factory", User: "user_2", First: true, Auth: authOf(t, c2)},
{Agent: "factory", User: "a@f.com", On: true, Auth: authOf(t, c1)},
}, func(t *testing.T, a, b savedLogin) {
ca, _ := factorySaved(a)
cb, _ := factorySaved(b)
if ca != cb {
t.Fatalf("factory %s: %+v, want %+v", a.User, cb, ca)
}
})
}
func TestMiMoMover(t *testing.T) {
issued := time.Date(2026, 9, 29, 10, 0, 0, 0, time.UTC)
c1 := mimoCreds{UserID: "123", CUserID: "c123", PassToken: "p1", DeviceID: "dev1", Region: "sg", Base: "https://aistudio.xiaomimimo.com", Cookies: map[string]string{"serviceToken": "s1", "userId": "123"}, Issued: issued, Name: "Mi One"}
c2 := mimoCreds{UserID: "456", PassToken: "p2", DeviceID: "dev2", Base: "https://aistudio.xiaomimimo.com", Cookies: map[string]string{"serviceToken": "s2"}, Issued: issued.Add(time.Hour)}
roundTrip(t, MiMoID, []savedLogin{
{Agent: MiMoID, User: "123", First: true, On: true, Auth: authOf(t, c1)},
{Agent: MiMoID, User: "456", On: true, Auth: authOf(t, c2)},
}, func(t *testing.T, a, b savedLogin) {
ca, _ := mimoSaved(a)
cb, _ := mimoSaved(b)
if cb.Name == "" {
cb.Name = ca.Name // a new entry: the name is only the built-in's label
}
if !reflect.DeepEqual(ca, cb) {
t.Fatalf("mimo %s: %+v, want %+v", a.User, cb, ca)
}
})
// the plugin keeps a numeric id as a number
ls, u, err := movers[MiMoID].back(nil, "", map[string]any{"type": "oauth", "refresh": `{"userId":789,"passToken":"p","base":"https://b"}`, "access": `{}`, "expires": float64(issued.UnixMilli())})
if c, ok := mimoSaved(ls[0]); err != nil || u != "789" || !ok || c.UserID != "789" {
t.Fatalf("numeric id: %q %+v %v", u, ls, err)
}
}