Files
magpie/internal/provider/devin_test.go
T
yetone 6546033109 kiro: removing kiro-cli's or the Kiro IDE's own sign-in hides it in magpie, and it shows again when Kiro signs in anew (Discord 碳碳双键: 本机没安装 kiro,但旧的 Kiro 数据还在,magpie 列出一个"Kiro account"(登录已失效,请重新添加此账号),添加新账号并设为首位后,旧的数据无法移除) — magpie reads Kiro's own sign-in from kiro-cli's database or ~/.aws/sso/cache and lists it among the accounts it signed in; Remove on it said "that is kiro-cli's or the Kiro IDE's own sign-in; run kiro-cli logout or sign out in the IDE", which with neither installed left a dead account there for good. The same refusal was shared by every agent whose own sign-in magpie only reads (forgetSideLogin: Kiro, Devin, Grok, Copilot, ZCode, WorkBuddy and WorkBuddy AI, Command Code, Gemini CLI, Antigravity). Now Remove on the agent's own sign-in hides it: logins.json's entry for it keeps a Hidden mark, and it is left out of the account list and so of the gateway's tries (the first and the ones behind it both come from that list); the agent's files are never deleted or written. It shows again when that sign-in changes: for Kiro, by a mark that is a digest of where the sign-in is (kiro-cli's row or the IDE's file) and its refresh token (never the token itself), so a new kiro-cli login or IDE sign-in, to any account, brings it back; for the others, which have no such mark, when it is another account. Removing it while it is first is allowed now, the next account being put first (saved, so it stays first when the own one comes back); removed when it was the only one, the agent has no account left. One of magpie's own accounts is removed with its home as before, and still can't be while it is first. The error and its per-agent sign-out hints are gone. Logins carry own, and the accounts list shows Remove on the agent's own sign-in also while it is first, its title "magpie stops showing and using Kiro's own sign-in; its files are left as they are, and it shows again when Kiro signs in anew" (magpie 不再显示和使用 Kiro 自己的登录;它的文件保持原样,Kiro 重新登录后会再次出现). Tested with a new internal/provider/side_logins_hide_test.go in a sandbox HOME: an expired kiro-cli sign-in ("Kiro account") behind a Google one magpie signed in is removed, stays gone across reads, isn't the account used nor among AlsoOn, kiro-cli's database byte-for-byte and mtime as it was, the refresh token nowhere in magpie's files; a new kiro-cli sign-in brings it back behind the one first; put first and removed, the next is first; one of magpie's removed takes its home and leaves the other's; the only account removed leaves Kiro with none; Grok's CLI sign-in hidden with auth.json untouched, back once it is another account. The agents' tests that expected the refusal (Command Code, Copilot, Devin, Grok, WorkBuddy, ZCode) no longer do. A new internal/gui/tests/own-signin-remove.test.cjs in Chromium and WebKit, en and zh: kiro-cli's sign-in behind another and alone first both have Remove with that title and post login/forget, magpie's first account has none. go build -tags nogui (and GOOS=linux, GOOS=windows), go test -tags nogui ./... and the whole GUI suite (138) pass. Not tried with a real Kiro, kiro-cli or the Kiro IDE.
2026-09-29 23:52:33 +08:00

345 lines
12 KiB
Go

package provider
import (
"encoding/json"
"github.com/yetone/magpie/internal/catalog"
"net/http"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
func TestParseDevinStatus(t *testing.T) {
user, plan, ok := parseDevinStatus(`Logged in (via Devin).
Credentials:
File: /home/u/.local/share/devin/credentials.toml
API server: https://server.codeium.com
User:
Name: someuser
Email: someuser@example.com
User ID: user-123
Account:
Tier: Devin Pro
Plan: Pro
Enterprise: no
`)
if !ok || user != "someuser@example.com" || plan != "Devin Pro" {
t.Fatalf("user=%q plan=%q ok=%v", user, plan, ok)
}
if _, _, ok := parseDevinStatus("Not logged in."); ok {
t.Fatal("a signed-out status parsed as signed in")
}
// no email field: the handle still identifies the account
user, _, ok = parseDevinStatus("Logged in (via Devin).\n\nUser:\n Name: handle\n")
if !ok || user != "handle" {
t.Fatalf("handle fallback: %q %v", user, ok)
}
}
func TestParseDevinModels(t *testing.T) {
families := parseDevinModels([]byte(`{"families":[
{"family_label":"SWE-2","family_uid":"swe-2","slug":"swe-2","aliases":["swe"],
"variants":[{"model_uid":"swe-2-max","label":"SWE-2 Max","max_context_tokens":262000,"max_output_tokens":128000},
{"model_uid":"swe-2-min","label":"SWE-2 Min"}]},
{"family_label":"Claude Opus 5.5","family_uid":"claude-opus-5-5","slug":"claude-opus-5.5","aliases":["opus"],
"variants":[{"model_uid":"claude-opus-5-5-high","label":"Claude Opus 5.5 High","max_context_tokens":1000000,"max_output_tokens":128000}]},
{"family_label":"No Numbers","family_uid":"no-numbers","slug":"no-numbers",
"variants":[{"model_uid":"no-numbers-high","label":"No Numbers High"}]}
]}`))
if len(families) != 3 {
t.Fatalf("families: %v", families)
}
f := families[0]
if f.UID != "swe-2" || f.Label != "SWE-2" || len(f.Models) != 2 || f.Models[0].ID != "swe-2-max" {
t.Fatalf("family: %+v", f)
}
// Devin's own numbers, which its list gives each variant
if f.Models[0].Context != 262000 || f.Models[0].Output != 128000 {
t.Fatalf("a variant's numbers: %+v", f.Models[0])
}
flat := devinModelsFlatten(families)
if len(flat) != 7 || flat[0].ID != "swe-2" || flat[1].ID != "swe-2-max" || flat[4].ID != "claude-opus-5-5-high" {
t.Fatalf("flat: %v", flat)
}
// the family takes the numbers of the variant its id follows, a variant
// without numbers takes its family's (swe-2-min), and models.dev is left
// for the families Devin gave none for (so swe-2's 262K is used at all)
for _, want := range []struct {
id string
context, most int
}{{"swe-2", 262000, 128000}, {"swe-2-max", 262000, 128000}, {"swe-2-min", 262000, 128000},
{"claude-opus-5-5", 1000000, 128000}, {"claude-opus-5-5-high", 1000000, 128000}} {
var got *catalog.Model
for i := range flat {
if flat[i].ID == want.id {
got = &flat[i]
}
}
if got == nil || got.Context != want.context || got.Output != want.most {
t.Fatalf("%s: got %+v, want %d/%d", want.id, got, want.context, want.most)
}
}
if w := catalog.ContextOf("no-numbers"); w > 0 && flat[5].Context != w {
t.Fatalf("a family Devin didn't size takes models.dev's %d: %+v", w, flat[5])
}
for _, m := range flat {
if m.Provider != "devin" {
t.Fatalf("provider: %v", m)
}
}
if parseDevinModels([]byte("not json")) != nil || parseDevinModels([]byte(`{"families":[]}`)) != nil {
t.Fatal("bad payloads must not parse")
}
}
func TestWithDevinContexts(t *testing.T) {
// the CLI list last read is the truth for what it names: models.dev has no
// swe-2, and its glm-5.2 (1M) isn't the 200K Devin serves
devinFamiliesCached([]DevinFamily{
{UID: "swe-2", Label: "SWE-2", Models: []catalog.Model{{ID: "swe-2-high", Context: 262000, Output: 128000}}},
{UID: "glm-5.2", Label: "GLM-5.2", Models: []catalog.Model{
{ID: "glm-5-2", Context: 200000, Output: 128000},
{ID: "glm-5-2-1m", Context: 1000000, Output: 128000},
}},
})
t.Cleanup(func() { devinFamiliesCached(nil) })
saved := withDevinContexts([]catalog.Model{
{ID: "glm-5-2", Context: 1000000, Output: 131072}, // an older magpie's models.dev numbers
{ID: "glm-5-2-1m"},
{ID: "swe-2-high"},
{ID: "glm-5.2"},
{ID: "nobody-high"},
})
if saved[0].Context != 200000 || saved[0].Output != 128000 {
t.Fatalf("Devin's numbers over models.dev's: %+v", saved[0])
}
if saved[1].Context != 1000000 || saved[1].Output != 128000 {
t.Fatalf("a variant's own numbers: %+v", saved[1])
}
if saved[2].Context != 262000 || saved[2].Output != 128000 {
t.Fatalf("a model models.dev doesn't have: %+v", saved[2])
}
if saved[3].Context != 200000 || saved[3].Output != 128000 {
t.Fatalf("a family id: %+v", saved[3])
}
if saved[4].Context != 0 || saved[4].Output != 0 {
t.Fatalf("a model nobody names stays unset: %+v", saved[4])
}
// what Devin's list doesn't name (once it has been read) keeps models.dev's
if w := catalog.ContextOf("claude-opus-5-5"); w > 0 {
if m := withDevinContexts([]catalog.Model{{ID: "claude-opus-5-5-high-fast"}})[0]; m.Context != w {
t.Fatalf("models.dev fallback: %+v", m)
}
}
}
func TestDevinCredentialsPath(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("XDG paths are unix")
}
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data"))
got := DevinCredentialsPath()
if got != filepath.Join(home, "data", "devin", "credentials.toml") {
t.Fatalf("path: %q", got)
}
t.Setenv("XDG_DATA_HOME", "")
if got := DevinCredentialsPath(); got != filepath.Join(home, ".local", "share", "devin", "credentials.toml") {
t.Fatalf("default path: %q", got)
}
_ = os.Getenv("HOME")
}
func TestDevinCredentials(t *testing.T) {
b := string(devinCredentials(`k"ey`, "", "", ""))
for _, want := range []string{
`windsurf_api_key = "k\"ey"`,
`api_server_url = "https://server.codeium.com"`,
`devin_webapp_host = "app.devin.ai"`,
`devin_api_url = "https://api.devin.ai"`,
} {
if !strings.Contains(b, want) {
t.Fatalf("credentials %s", b)
}
}
}
func TestDevinSignIn(t *testing.T) {
home := claudeHome(t)
t.Setenv("XDG_DATA_HOME", filepath.Join(home, "data"))
// a CLI that answers `auth status` once the exchange wrote its file
exe := filepath.Join(home, "devin")
os.WriteFile(exe, []byte("#!/bin/sh\ncat <<'X'\nLogged in (via Devin).\n\nUser:\n Email: dev@example.com\n\nAccount:\n Tier: Devin Pro\nX\n"), 0o755)
oldExe := DevinExecutable
DevinExecutable = func() string { return exe }
t.Cleanup(func() { DevinExecutable = oldExe })
var gotCode, gotVerifier, gotRedirect string
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]string
json.NewDecoder(r.Body).Decode(&body)
gotCode, gotVerifier, gotRedirect = body["code"], body["code_verifier"], body["redirect_uri"]
json.NewEncoder(w).Encode(map[string]any{
"sessionToken": "devin-session-token$sk-test",
"devinWebappHost": "app.devin.ai", "devinApiUrl": "https://api.devin.ai",
})
}))
defer fake.Close()
oldTok := devinExchangeURL
devinExchangeURL = fake.URL
t.Cleanup(func() { devinExchangeURL = oldTok })
st, err := StartSignIn("devin")
if err != nil {
t.Fatal(err)
}
u, _ := url.Parse(st.URL)
if u.Host != "app.devin.ai" || u.Path != "/auth/cli/continue" {
t.Fatalf("url %s", st.URL)
}
q := u.Query()
if q.Get("prompt") != "select_account" || q.Get("code_challenge") == "" ||
q.Get("code_challenge_method") != "S256" || q.Get("redirect_uri") == "" || q.Get("state") == "" {
t.Fatalf("params %s", st.URL)
}
page := finishInBrowser(t, st, "dv-code")
if !strings.Contains(page, "signed in") {
t.Fatalf("page %s", page)
}
if st = waitDone(t, st.ID); st.State != "done" || st.User != "dev@example.com" || st.Plan != "Devin Pro" || !st.Using {
t.Fatalf("state %+v", st)
}
if gotCode != "dv-code" || gotVerifier == "" || gotRedirect == "" {
t.Fatalf("exchange code=%q verifier=%q redirect=%q", gotCode, gotVerifier, gotRedirect)
}
// credentials.toml as `devin auth login` would write it
b, err := os.ReadFile(DevinCredentialsPath())
if err != nil {
t.Fatal(err)
}
for _, want := range []string{`windsurf_api_key = "devin-session-token$sk-test"`, `api_server_url = "https://server.codeium.com"`} {
if !strings.Contains(string(b), want) {
t.Fatalf("credentials %s", b)
}
}
// the CLI's own account, the one in use
if ls := Logins("devin"); len(ls) != 1 || ls[0].User != "dev@example.com" || !ls[0].Active {
t.Fatalf("logins %v", ls)
}
}
// TestDevinSeveralAccounts signs a second Devin account in beside the CLI's
// own: it gets a home of magpie's, the CLI's file is left alone, and the
// gateway can use either.
func TestDevinSeveralAccounts(t *testing.T) {
home := claudeHome(t)
data := filepath.Join(home, "data")
t.Setenv("XDG_DATA_HOME", data)
os.MkdirAll(filepath.Join(data, "devin"), 0o700)
own := string(devinCredentials("devin-session-token$own", "", "", ""))
os.WriteFile(filepath.Join(data, "devin", "credentials.toml"), []byte(own), 0o600)
// a CLI that says whose key is in the credentials.toml of its data folder
exe := filepath.Join(home, "devin")
os.WriteFile(exe, []byte(`#!/bin/sh
f="$XDG_DATA_HOME/devin/credentials.toml"
[ -f "$f" ] || { echo "Not logged in"; exit 1; }
if grep -q own "$f"; then who=dev@example.com; tier="Devin Pro"; else who=two@example.com; tier="Devin Max"; fi
printf 'Logged in (via Devin).
User:
Email: %s
Account:
Tier: %s
' "$who" "$tier"
`), 0o755)
oldExe := DevinExecutable
DevinExecutable = func() string { return exe }
t.Cleanup(func() { DevinExecutable = oldExe })
forgetDevinStatus()
t.Cleanup(forgetDevinStatus)
fake := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(map[string]any{
"sessionToken": "devin-session-token$two",
"devinWebappHost": "app.devin.ai", "devinApiUrl": "https://api.devin.ai",
})
}))
defer fake.Close()
oldTok := devinExchangeURL
devinExchangeURL = fake.URL
t.Cleanup(func() { devinExchangeURL = oldTok })
signIn := func() SignInState {
st, err := StartSignIn("devin")
if err != nil {
t.Fatal(err)
}
finishInBrowser(t, st, "dv-code")
return waitDone(t, st.ID)
}
if st := signIn(); st.State != "done" || st.User != "two@example.com" || st.Plan != "Devin Max" || st.Using {
t.Fatalf("state %+v", st)
}
// the CLI's own sign-in is as it was
if b, _ := os.ReadFile(DevinCredentialsPath()); string(b) != own {
t.Fatalf("CLI credentials %s", b)
}
ls := devinLogins()
if len(ls) != 2 || ls[0].User != "dev@example.com" || ls[0].Home != "" || !ls[0].Active ||
ls[1].User != "two@example.com" || ls[1].Home == "" || !ls[1].On {
t.Fatalf("logins %+v", ls)
}
if key, _, err := DevinAuthAt(ls[1].Home); err != nil || key != "devin-session-token$two" {
t.Fatalf("second key %q %v", key, err)
}
p, ok := devinAccount()
if !ok || p.Account.User != "dev@example.com" || p.Account.Plan != "Devin Pro" {
t.Fatalf("account %+v", p.Account)
}
also := p.AlsoOn()
if len(also) != 1 || also[0].Account.User != "two@example.com" || also[0].Account.Home != ls[1].Home {
t.Fatalf("also on %+v", also)
}
// signed in again, it is still the one account, in its newer home
first := ls[1].Home
signIn()
if ls = devinLogins(); len(ls) != 2 || ls[1].Home == first {
t.Fatalf("again %+v", ls)
}
if _, err := os.Stat(first); !os.IsNotExist(err) {
t.Fatalf("old home kept: %v", err)
}
// switched, the gateway uses it first
if err := SwitchLogin("devin", "two@example.com"); err != nil {
t.Fatal(err)
}
if p, _ := devinAccount(); p.Account.User != "two@example.com" || p.Account.Home != ls[1].Home {
t.Fatalf("switched %+v", p.Account)
}
if err := SwitchLogin("devin", "dev@example.com"); err != nil {
t.Fatal(err)
}
if err := ForgetLogin("devin", "two@example.com"); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(ls[1].Home); !os.IsNotExist(err) {
t.Fatalf("home kept: %v", err)
}
if ls = devinLogins(); len(ls) != 1 || ls[0].User != "dev@example.com" {
t.Fatalf("after forget %+v", ls)
}
}